| 1 | import CoreGraphics |
| 2 | |
| 3 | /// How the full-window call's control bar fits the width it has (2026-10-03: |
| 4 | /// Paul's 0.4.6 bar ran off the left of the window, mute and camera cut |
| 5 | /// off). The bar is always whole and centred: first its spacing tightens, |
| 6 | /// then the controls that matter least move into a ⋯ menu, one at a time — |
| 7 | /// never mute, camera, chat or Leave. |
| 8 | public struct CallBarFit: Equatable, Sendable { |
| 9 | /// A control the bar can hold. `essential` ones never move into ⋯. |
| 10 | public enum Control: String, CaseIterable, Sendable { |
| 11 | case mute, camera, share, devices, layout, link, chat, leave |
| 12 | |
| 13 | public var essential: Bool { [.mute, .camera, .chat, .leave].contains(self) } |
| 14 | |
| 15 | /// Its width on the bar: every button is 44 pt round; Leave is a |
| 16 | /// capsule 1.4 times as wide (OverlayButtonBody). |
| 17 | var width: CGFloat { self == .leave ? CallBarFit.button * 1.4 : CallBarFit.button } |
| 18 | } |
| 19 | |
| 20 | public static let button: CGFloat = 44 |
| 21 | /// The widest the bar grows, and its margin from the window's edges. |
| 22 | public static let maxWidth: CGFloat = 760 |
| 23 | public static let margin: CGFloat = 16 |
| 24 | /// The order the optional controls move into ⋯: the least used first. |
| 25 | public static let overflowOrder: [Control] = [.link, .layout, .devices, .share] |
| 26 | |
| 27 | /// One step of tightening: the gap between buttons, the least room |
| 28 | /// between the left group and chat, and the pane's padding. |
| 29 | public struct Spacing: Equatable, Sendable { |
| 30 | public var between: CGFloat |
| 31 | public var gap: CGFloat |
| 32 | public var padding: CGFloat |
| 33 | } |
| 34 | |
| 35 | public static let roomy = Spacing(between: 10, gap: 24, padding: 10) |
| 36 | public static let snug = Spacing(between: 6, gap: 12, padding: 8) |
| 37 | public static let tight = Spacing(between: 4, gap: 4, padding: 6) |
| 38 | |
| 39 | public var spacing: Spacing |
| 40 | /// The controls in ⋯, in the bar's own order; empty when everything fits. |
| 41 | public var overflow: [Control] |
| 42 | /// The pane's width: as wide as it may be, never wider than it has. |
| 43 | public var width: CGFloat |
| 44 | |
| 45 | /// The bar for a window `windowWidth` wide showing `controls` (Share |
| 46 | /// meeting link is only there for a member; Video layout only here). |
| 47 | public static func fit(windowWidth: CGFloat, controls: [Control]) -> CallBarFit { |
| 48 | fit(room: max(0, windowWidth - 2 * margin), controls: controls) |
| 49 | } |
| 50 | |
| 51 | /// The bar for `room` points of width, centred in it: the window's less |
| 52 | /// its margins, or less what a self view in a bottom corner keeps clear |
| 53 | /// (SelfViewBarLayout). |
| 54 | public static func fit(room: CGFloat, controls: [Control]) -> CallBarFit { |
| 55 | let room = max(0, room) |
| 56 | for spacing in [roomy, snug, tight] where needed(controls, spacing, more: false) <= room { |
| 57 | return CallBarFit(spacing: spacing, overflow: [], width: min(maxWidth, room)) |
| 58 | } |
| 59 | var shown = controls |
| 60 | var moved: [Control] = [] |
| 61 | for control in overflowOrder where shown.contains(control) { |
| 62 | shown.removeAll { $0 == control } |
| 63 | moved.append(control) |
| 64 | if needed(shown, tight, more: true) <= room { break } |
| 65 | } |
| 66 | let order = controls.filter { moved.contains($0) } |
| 67 | return CallBarFit(spacing: tight, overflow: order, width: min(maxWidth, room)) |
| 68 | } |
| 69 | |
| 70 | /// The least width `controls` take with `spacing`, with a ⋯ button if |
| 71 | /// `more`: the buttons, a gap between each (the spacer before chat is |
| 72 | /// one more child of the row), the spacer's own least, and the padding. |
| 73 | public static func needed(_ controls: [Control], _ spacing: Spacing, more: Bool) -> CGFloat { |
| 74 | let buttons = controls.reduce(0) { $0 + $1.width } + (more ? button : 0) |
| 75 | let children = controls.count + (more ? 1 : 0) + 1 |
| 76 | return buttons + CGFloat(children - 1) * spacing.between + spacing.gap + 2 * spacing.padding |
| 77 | } |
| 78 | |
| 79 | public func shows(_ control: Control) -> Bool { !overflow.contains(control) } |
| 80 | |
| 81 | /// The pane's height: a button and the padding above and below it. |
| 82 | public var height: CGFloat { Self.button + 2 * spacing.padding } |
| 83 | |
| 84 | /// Whether `room` holds the bar at its least: the essentials and ⋯, |
| 85 | /// tight. Narrower than that and something would be cut off. |
| 86 | public static func holds(_ room: CGFloat, controls: [Control]) -> Bool { |
| 87 | let essentials = controls.filter(\.essential) |
| 88 | return needed(essentials, tight, more: essentials.count < controls.count) <= room |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// The floating self view and the full-window bar, placed together |
| 93 | /// (2026-10-30, Paul on 0.4.15: "The picture-in-picture is weirdly close |
| 94 | /// to the in-call chrome … Should the picture in picture just stay close |
| 95 | /// to the edge of the screen from the start?"). |
| 96 | /// |
| 97 | /// Until 0.4.15 the self view rose above the bar whenever the controls |
| 98 | /// showed, and settled back when they went: a few points above a bar |
| 99 | /// that ran on underneath it, and moving every time the pointer did. Now |
| 100 | /// it stays put, `edge` in from the window's edges in whichever corner it |
| 101 | /// was put, and the bar makes room instead: still centred on the window, |
| 102 | /// it narrows to keep `clearance` from a self view in a bottom corner on |
| 103 | /// both sides — the side the self view is on, and, to stay centred, the |
| 104 | /// other — moving the least used controls into ⋯ as it does for a narrow |
| 105 | /// window (CallBarFit). Centred and narrower rather than shifted aside, |
| 106 | /// because a bar that slides off centre as the self view crosses from |
| 107 | /// one corner to another reads as a bar that moved; and the room is |
| 108 | /// there at every size the window allows (at its 760 × 540 least the bar |
| 109 | /// has 392 pt, which holds everything but a member's Video layout and |
| 110 | /// Share meeting link, which go into ⋯). |
| 111 | /// |
| 112 | /// Only where even the essentials would not fit beside it does the self |
| 113 | /// view rise, `rise` above the bar, while the bar shows: a window the |
| 114 | /// system made narrower than the app allows, or the self view pushed |
| 115 | /// towards the middle by an open chat — which holds the controls up, so |
| 116 | /// there it sits level with the chat's own bottom edge, and stays. |
| 117 | public struct SelfViewBarLayout: Equatable, Sendable { |
| 118 | /// The self view's distance from the window's edges, as the bar's |
| 119 | /// margin and the devices panel's. |
| 120 | public static let edge: CGFloat = 16 |
| 121 | /// The gap between the bar and a self view beside it: the window's |
| 122 | /// own margin again, so the edges, the bar and the self view are |
| 123 | /// spaced by one measure (Paul asked for at least 12). |
| 124 | public static let clearance: CGFloat = 16 |
| 125 | /// The gap between the bar and a self view that has to rise above it, |
| 126 | /// and an open panel's: 12, as the panels have always kept. |
| 127 | public static let rise: CGFloat = 12 |
| 128 | |
| 129 | public var bar: CallBarFit |
| 130 | /// Where the self view is while the bar is hidden. |
| 131 | public var resting: CGRect |
| 132 | /// Where it is while the bar shows: `resting`, unless it has to rise. |
| 133 | public var raised: CGRect |
| 134 | |
| 135 | public var rises: Bool { resting != raised } |
| 136 | public func selfView(barShown: Bool) -> CGRect { barShown ? raised : resting } |
| 137 | |
| 138 | /// - Parameters: |
| 139 | /// - window: the call's size. |
| 140 | /// - selfView: the floating self view's size; nil when nobody floats |
| 141 | /// (Grid, or a filmstrip you are in), and the bar is the window's. |
| 142 | /// - corner: the corner it was put in. |
| 143 | /// - top: what the top of the window keeps clear (the title strip). |
| 144 | /// - trailing: what the right of the window keeps clear (an open chat). |
| 145 | /// - controls: the bar's controls. |
| 146 | public static func layout( |
| 147 | window: CGSize, selfView size: CGSize?, corner: PiPCorner, top: CGFloat = 0, |
| 148 | trailing: CGFloat = 0, controls: [CallBarFit.Control] |
| 149 | ) -> SelfViewBarLayout { |
| 150 | let whole = CallBarFit.fit(windowWidth: window.width, controls: controls) |
| 151 | guard let size else { return SelfViewBarLayout(bar: whole, resting: .zero, raised: .zero) } |
| 152 | let x = corner.leading ? edge : window.width - trailing - edge - size.width |
| 153 | let y = corner.top ? top + edge : window.height - edge - size.height |
| 154 | // A window too small for it still keeps the view on it. |
| 155 | let resting = CGRect( |
| 156 | x: min(max(x, 0), max(window.width - size.width, 0)), |
| 157 | y: min(max(y, 0), max(window.height - size.height, 0)), |
| 158 | width: size.width, height: size.height) |
| 159 | guard !corner.top else { return SelfViewBarLayout(bar: whole, resting: resting, raised: resting) } |
| 160 | let half = |
| 161 | corner.leading |
| 162 | ? window.width / 2 - resting.maxX - clearance |
| 163 | : resting.minX - clearance - window.width / 2 |
| 164 | let room = min(window.width - 2 * CallBarFit.margin, 2 * half) |
| 165 | let beside = corner.leading || trailing == 0 |
| 166 | if beside, CallBarFit.holds(room, controls: controls) { |
| 167 | return SelfViewBarLayout( |
| 168 | bar: CallBarFit.fit(room: room, controls: controls), resting: resting, raised: resting) |
| 169 | } |
| 170 | let barTop = window.height - CallBarFit.margin - whole.height |
| 171 | var raised = resting |
| 172 | raised.origin.y = max(0, barTop - rise - size.height) |
| 173 | return SelfViewBarLayout(bar: whole, resting: resting, raised: raised) |
| 174 | } |
| 175 | } |
| 176 | |