|
| 1 | +import SwiftUI |
| 2 | +import UIKit |
| 3 | + |
| 4 | +extension EnvironmentValues { |
| 5 | + @Entry var featureWorkspaceFoldSidebarWidth: CGFloat? = nil |
| 6 | +} |
| 7 | + |
| 8 | +@MainActor |
| 9 | +enum PlatformWorkspaceOrientation { |
| 10 | + private static let hingeWindows = NSHashTable<UIWindow>.weakObjects() |
| 11 | + |
| 12 | + static func allowHingeOrientations(in window: UIWindow) { |
| 13 | + guard !hingeWindows.contains(window) else { return } |
| 14 | + hingeWindows.add(window) |
| 15 | + window.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations() |
| 16 | + } |
| 17 | + |
| 18 | + static func supportedOrientations(in window: UIWindow?) -> UIInterfaceOrientationMask { |
| 19 | + if window?.traitCollection.userInterfaceIdiom == .pad { return .all } |
| 20 | + return window.map { hingeWindows.contains($0) } == true ? .allButUpsideDown : .portrait |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// Keeps hardware capability separate from layout size, which changes on either display. |
| 25 | +struct PlatformWorkspaceGeometryObserver: UIViewRepresentable { |
| 26 | + let onFoldSidebarWidthChange: (CGFloat?) -> Void |
| 27 | + |
| 28 | + func makeUIView(context: Context) -> GeometryView { |
| 29 | + GeometryView(onFoldSidebarWidthChange: onFoldSidebarWidthChange) |
| 30 | + } |
| 31 | + |
| 32 | + func updateUIView(_ view: GeometryView, context: Context) { |
| 33 | + view.onFoldSidebarWidthChange = onFoldSidebarWidthChange |
| 34 | + } |
| 35 | + |
| 36 | + final class GeometryView: UIView { |
| 37 | + var onFoldSidebarWidthChange: (CGFloat?) -> Void |
| 38 | + private var hasHinge = false |
| 39 | + private var lastFoldSidebarWidth: CGFloat? |
| 40 | + |
| 41 | + init(onFoldSidebarWidthChange: @escaping (CGFloat?) -> Void) { |
| 42 | + self.onFoldSidebarWidthChange = onFoldSidebarWidthChange |
| 43 | + super.init(frame: .zero) |
| 44 | + isUserInteractionEnabled = false |
| 45 | + #if canImport(UIKit, _version: 9127.0.85) |
| 46 | + if #available(iOS 27.1, *) { |
| 47 | + addInteraction(UIHingeInteraction { [weak self] _, update in |
| 48 | + guard let self else { return } |
| 49 | + // Capability survives a fold and temporary window detachment. |
| 50 | + hasHinge = hasHinge || update.hinge != nil |
| 51 | + updateGeometry() |
| 52 | + }) |
| 53 | + } |
| 54 | + #endif |
| 55 | + } |
| 56 | + |
| 57 | + @available(*, unavailable) |
| 58 | + required init?(coder: NSCoder) { nil } |
| 59 | + |
| 60 | + override func didMoveToWindow() { |
| 61 | + super.didMoveToWindow() |
| 62 | + updateGeometry() |
| 63 | + } |
| 64 | + |
| 65 | + override func layoutSubviews() { |
| 66 | + super.layoutSubviews() |
| 67 | + updateGeometry() |
| 68 | + } |
| 69 | + |
| 70 | + private func updateGeometry() { |
| 71 | + guard let window else { return } |
| 72 | + if hasHinge { PlatformWorkspaceOrientation.allowHingeOrientations(in: window) } |
| 73 | + var foldSidebarWidth: CGFloat? |
| 74 | + #if canImport(UIKit, _version: 9127.0.85) |
| 75 | + if #available(iOS 27.1, *) { |
| 76 | + for region in reservedRegions(kind: .division) where region.isActive { |
| 77 | + let frame = region.frame.intersection(bounds) |
| 78 | + guard !frame.isNull, frame.height >= bounds.height / 2 else { continue } |
| 79 | + let leading = effectiveUserInterfaceLayoutDirection == .rightToLeft |
| 80 | + ? bounds.maxX - frame.maxX : frame.minX - bounds.minX |
| 81 | + let trailing = bounds.width - leading - frame.width |
| 82 | + let rtl = effectiveUserInterfaceLayoutDirection == .rightToLeft |
| 83 | + let leadingInset = rtl ? safeAreaInsets.right : safeAreaInsets.left |
| 84 | + let trailingInset = rtl ? safeAreaInsets.left : safeAreaInsets.right |
| 85 | + guard leading - leadingInset >= T3Metrics.minimumSidebarWidth, |
| 86 | + trailing - trailingInset >= 320 else { continue } |
| 87 | + foldSidebarWidth = leading |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + #endif |
| 92 | + guard lastFoldSidebarWidth != foldSidebarWidth else { return } |
| 93 | + lastFoldSidebarWidth = foldSidebarWidth |
| 94 | + // Layout may run during a SwiftUI update. Publish only the final reservation. |
| 95 | + DispatchQueue.main.async { [weak self] in |
| 96 | + guard let self, lastFoldSidebarWidth == foldSidebarWidth else { return } |
| 97 | + onFoldSidebarWidthChange(foldSidebarWidth) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments