oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1#if os(macOS)
2 import CoreGraphics
3 import Foundation
4
5 /// Where the mini window sits on screen. A corner by default, like
6 /// FaceTime's and iOS's picture in picture: a corner is a place you can
7 /// find again without looking. Since 2026-10-30 it may also rest
8 /// anywhere it is put, with the corners magnetic (MiniWindowPreferences).
9 public enum MiniWindowCorner: String, CaseIterable, Identifiable, Sendable {
10 case bottomRight, bottomLeft, topRight, topLeft
11
12 public var id: String { rawValue }
13
14 public var title: String {
15 switch self {
16 case .bottomRight: "Bottom right"
17 case .bottomLeft: "Bottom left"
18 case .topRight: "Top right"
19 case .topLeft: "Top left"
20 }
21 }
22
23 var right: Bool { self == .bottomRight || self == .topRight }
24 var top: Bool { self == .topRight || self == .topLeft }
25
26 /// The window's frame in this corner of `visible`, which is a screen's
27 /// `visibleFrame`: already clear of the menu bar and the Dock, so the
28 /// inset is only breathing room. AppKit's origin is bottom-left.
29 public func frame(size: CGSize, in visible: CGRect, inset: CGFloat = MiniWindowRules.inset)
30 -> CGRect
31 {
32 CGRect(
33 x: right ? visible.maxX - inset - size.width : visible.minX + inset,
34 y: top ? visible.maxY - inset - size.height : visible.minY + inset,
35 width: size.width, height: size.height)
36 }
37
38 /// The corner a magnet pulls `frame` into: the one whose snapped frame
39 /// is within `distance` of it (between top-left origins, so in a
40 /// corner's own terms how far the window is from sitting in it), the
41 /// nearest if two are; nil in the open, where it stays put.
42 public static func magnet(
43 for frame: CGRect, in visible: CGRect, distance: CGFloat = MiniWindowRules.snapDistance
44 ) -> MiniWindowCorner? {
45 let away = allCases.map { corner -> (MiniWindowCorner, CGFloat) in
46 let snapped = corner.frame(size: frame.size, in: visible)
47 return (corner, hypot(snapped.minX - frame.minX, snapped.minY - frame.minY))
48 }
49 return away.filter { $0.1 <= distance }.min { $0.1 < $1.1 }?.0
50 }
51
52 /// The corner a dragged window belongs to: whichever side of the
53 /// screen's middle its own middle landed on.
54 public static func nearest(to frame: CGRect, in visible: CGRect) -> MiniWindowCorner {
55 let right = frame.midX >= visible.midX
56 let top = frame.midY >= visible.midY
57 switch (top, right) {
58 case (false, true): return .bottomRight
59 case (false, false): return .bottomLeft
60 case (true, true): return .topRight
61 case (true, false): return .topLeft
62 }
63 }
64 }
65
66 public enum MiniWindowRules {
67 /// 16:9 at the width of a small picture in picture: big enough to
68 /// read a face, small enough to leave the app you went to usable.
69 /// The size it opens at until someone resizes it.
70 public static let size = CGSize(width: 320, height: 180)
71 public static let inset: CGFloat = 20
72 /// How near its snapped place a window let go has to be for a
73 /// magnetic corner to take it, with "Move freely" and "Snap to
74 /// corners" both on: 96 pt, a fixed distance of the pointer's travel
75 /// rather than a share of the window, so the target is the same
76 /// whatever size the window has been made — about a third of the
77 /// default window's width, half the smallest's, and well short of the
78 /// middle of any screen, where a window is clearly meant to stay put.
79 public static let snapDistance: CGFloat = 96
80 public static let aspect: CGFloat = 16 / 9
81 /// Narrower than this and Mute, Leave and Collapse no longer fit
82 /// across the top.
83 public static let minimumWidth: CGFloat = 200
84 /// Wider than this share of the screen and it is no longer a mini
85 /// window, it is a second call window covering the app you went to.
86 public static let maximumShare: CGFloat = 0.4
87
88 /// The size for a width someone chose, held to 16:9 and to the limits
89 /// of the screen it is on now — which may be a smaller one than the
90 /// width was chosen on.
91 public static func size(width: CGFloat, in visible: CGRect) -> CGSize {
92 let widest = maximumSize(in: visible).width
93 let chosen = min(max(width.rounded(), minimumWidth), widest)
94 return CGSize(width: chosen, height: chosen / aspect)
95 }
96
97 public static func minimumSize() -> CGSize {
98 CGSize(width: minimumWidth, height: minimumWidth / aspect)
99 }
100
101 /// Never below the minimum, even on a screen too small for 40% of it
102 /// to be that wide.
103 public static func maximumSize(in visible: CGRect) -> CGSize {
104 let widest = max(minimumWidth, (visible.width * maximumShare).rounded(.down))
105 return CGSize(width: widest, height: widest / aspect)
106 }
107
108 /// `frame` moved the least distance that puts all of it inside
109 /// `visible`. Its size is kept; one bigger than the screen is pinned
110 /// to the top left, where the controls are.
111 public static func clamp(_ frame: CGRect, into visible: CGRect) -> CGRect {
112 var out = frame
113 out.origin.x = min(max(frame.minX, visible.minX), visible.maxX - frame.width)
114 out.origin.x = max(out.origin.x, visible.minX)
115 out.origin.y = max(min(frame.minY, visible.maxY - frame.height), visible.minY)
116 out.origin.y = min(out.origin.y, visible.maxY - frame.height)
117 return out
118 }
119
120 /// Where a freely placed window is, relative to its screen's visible
121 /// frame. Kept this way rather than in global coordinates so that
122 /// moving a display in System Settings → Displays → Arrange does not
123 /// throw the window off it.
124 public static func offset(of frame: CGRect, in visible: CGRect) -> CGPoint {
125 CGPoint(x: frame.minX - visible.minX, y: frame.minY - visible.minY)
126 }
127
128 /// A remembered offset put back on a screen, clamped: the display may
129 /// have changed resolution, or been swapped for a smaller one.
130 public static func frame(offset: CGPoint, size: CGSize, in visible: CGRect) -> CGRect {
131 clamp(
132 CGRect(
133 origin: CGPoint(x: visible.minX + offset.x, y: visible.minY + offset.y), size: size),
134 into: visible)
135 }
136 }
137
138 /// Resizing the mini window from its edges and corners, always at
139 /// 16:9. Done by the window's own surface rather than by AppKit's
140 /// `.resizable`, whose edge handling on a borderless panel is not
141 /// something to rely on, and which could not be held to this aspect
142 /// and these limits in one place that tests can reach.
143 public enum MiniWindowResize {
144 public struct Handle: OptionSet, Hashable, Sendable {
145 public let rawValue: Int
146 public init(rawValue: Int) { self.rawValue = rawValue }
147 public static let left = Handle(rawValue: 1)
148 public static let right = Handle(rawValue: 2)
149 public static let top = Handle(rawValue: 4)
150 public static let bottom = Handle(rawValue: 8)
151 }
152
153 /// How close to an edge, inside the window, a press starts a resize.
154 public static let grip: CGFloat = 8
155
156 /// The edges a press at `point` (in the window's own coordinates,
157 /// origin bottom-left) takes hold of. Two at a corner, none in the
158 /// middle, where a press is a click or a move.
159 public static func handle(at point: CGPoint, in size: CGSize) -> Handle {
160 var handle: Handle = []
161 if point.x <= grip { handle.insert(.left) }
162 if point.x >= size.width - grip { handle.insert(.right) }
163 if point.y <= grip { handle.insert(.bottom) }
164 if point.y >= size.height - grip { handle.insert(.top) }
165 return handle
166 }
167
168 /// The frame after dragging `handle` by (dx, dy) from `start`. The
169 /// opposite edge or corner stays put; an edge on its own grows the
170 /// window evenly either side of its middle. At a corner, whichever
171 /// direction asks for the bigger change wins, so the corner follows
172 /// the pointer as closely as 16:9 allows.
173 public static func frame(
174 from start: CGRect, dragging handle: Handle, dx: CGFloat, dy: CGFloat, in visible: CGRect
175 ) -> CGRect {
176 let horizontal: CGFloat? =
177 handle.contains(.right) ? start.width + dx : handle.contains(.left) ? start.width - dx : nil
178 let vertical: CGFloat? =
179 handle.contains(.top)
180 ? (start.height + dy) * MiniWindowRules.aspect
181 : handle.contains(.bottom) ? (start.height - dy) * MiniWindowRules.aspect : nil
182 let proposed: CGFloat
183 switch (horizontal, vertical) {
184 case (let h?, let v?): proposed = abs(h - start.width) >= abs(v - start.width) ? h : v
185 case (let h?, nil): proposed = h
186 case (nil, let v?): proposed = v
187 case (nil, nil): return start
188 }
189 let size = MiniWindowRules.size(width: proposed, in: visible)
190 let x: CGFloat =
191 handle.contains(.left)
192 ? start.maxX - size.width
193 : handle.contains(.right) ? start.minX : start.midX - size.width / 2
194 let y: CGFloat =
195 handle.contains(.bottom)
196 ? start.maxY - size.height
197 : handle.contains(.top) ? start.minY : start.midY - size.height / 2
198 return CGRect(origin: CGPoint(x: x, y: y), size: size)
199 }
200 }
201
202 /// The screen edge a collapsed mini window sits against.
203 public enum MiniWindowEdge: String, Sendable {
204 case left, right, top, bottom
205 }
206
207 /// The mini window collapsed to a tab: small, flush against the nearest
208 /// edge of the screen, out of the way of whatever the person went to
209 /// the other app for.
210 public enum MiniWindowTab {
211 public static let size = CGSize(width: 156, height: 40)
212 /// How far from its edge a dragged tab has to be let go to count as
213 /// pulled out, rather than slid along the edge to somewhere else.
214 public static let pullOut: CGFloat = 48
215
216 /// The edge `frame` is closest to. Ties go left and right before top
217 /// and bottom, because the corners are where a snapped window lives
218 /// and a tab beside it reads as belonging to the side it came from.
219 public static func nearestEdge(to frame: CGRect, in visible: CGRect) -> MiniWindowEdge {
220 let distances: [(MiniWindowEdge, CGFloat)] = [
221 (.right, visible.maxX - frame.maxX), (.left, frame.minX - visible.minX),
222 (.bottom, frame.minY - visible.minY), (.top, visible.maxY - frame.maxY),
223 ]
224 return distances.min { $0.1 < $1.1 }!.0
225 }
226
227 /// The tab for a window at `frame`: against its nearest edge, level
228 /// with the window's middle along that edge, and on screen.
229 public static func dock(_ frame: CGRect, in visible: CGRect) -> (MiniWindowEdge, CGRect) {
230 let edge = nearestEdge(to: frame, in: visible)
231 let x: CGFloat
232 let y: CGFloat
233 switch edge {
234 case .left: (x, y) = (visible.minX, frame.midY - size.height / 2)
235 case .right: (x, y) = (visible.maxX - size.width, frame.midY - size.height / 2)
236 case .bottom: (x, y) = (frame.midX - size.width / 2, visible.minY)
237 case .top: (x, y) = (frame.midX - size.width / 2, visible.maxY - size.height)
238 }
239 return (
240 edge,
241 MiniWindowRules.clamp(CGRect(origin: CGPoint(x: x, y: y), size: size), into: visible)
242 )
243 }
244
245 public enum Release: Equatable {
246 /// Back against an edge — the same one, or another it was slid to.
247 case dock(MiniWindowEdge, CGRect)
248 /// Pulled out: open the mini window again.
249 case expand
250 }
251
252 /// What letting go of a dragged tab means.
253 public static func release(_ frame: CGRect, in visible: CGRect) -> Release {
254 let away = min(
255 frame.minX - visible.minX, visible.maxX - frame.maxX,
256 frame.minY - visible.minY, visible.maxY - frame.maxY)
257 if away > pullOut { return .expand }
258 let (edge, docked) = dock(frame, in: visible)
259 return .dock(edge, docked)
260 }
261
262 /// A window of `size` opened from a tab: centred where the tab is,
263 /// and on screen.
264 public static func expanded(from tab: CGRect, size: CGSize, in visible: CGRect) -> CGRect {
265 MiniWindowRules.clamp(
266 CGRect(
267 x: tab.midX - size.width / 2, y: tab.midY - size.height / 2,
268 width: size.width, height: size.height),
269 into: visible)
270 }
271
272 /// Who the call is with, in the few words a tab has room for: the
273 /// other person's name when there is one, a count when there are
274 /// more, and "Just you" when nobody else has come.
275 public static func caption(others: [String]) -> String {
276 switch others.count {
277 case 0: "Just you"
278 case 1: others[0]
279 default: "\(others.count + 1) people"
280 }
281 }
282 }
283
284 /// The preferences, read and written in one place so the Settings
285 /// window and the mini window cannot disagree about a key or a default.
286 ///
287 /// On by default, because the person who asked for it switches apps
288 /// during most calls, and a setting nobody knows about that is off is a
289 /// feature that does not exist.
290 ///
291 /// Placement is two switches, both on by default (2026-10-30, Paul:
292 /// "That way I can pull the mini window from the corner if I want, but
293 /// easily snap it back"): "Move freely" leaves a dragged window where it
294 /// is dropped; "Snap to corners" takes it into a corner. Both on, it
295 /// rests anywhere and a corner pulls it in when it is let go near one
296 /// (MiniWindowRules.snapDistance). Snapping alone is the old "Snap to
297 /// corners", always into the nearest; moving alone, the old "Anywhere I
298 /// put it". Never both off: there would be nowhere for it to go.
299 public struct MiniWindowPreferences {
300 public static let enabledKey = "miniWindow.enabled"
301 public static let cornerKey = "miniWindow.corner"
302 public static let moveFreelyKey = "miniWindow.moveFreely"
303 public static let snapToCornersKey = "miniWindow.snapToCorners"
304 /// Until 0.4.16, one choice: "corners" or "anywhere". Read once, by
305 /// `migrate`, and removed.
306 public static let legacyPlacementKey = "miniWindow.placement"
307 public static let widthKey = "miniWindow.width"
308 public static let positionsKey = "miniWindow.positions"
309 public static let defaultEnabled = true
310 public static let defaultCorner = MiniWindowCorner.bottomRight
311 public static let defaultMoveFreely = true
312 public static let defaultSnapToCorners = true
313
314 private let defaults: UserDefaults
315 public init(defaults: UserDefaults = .standard) {
316 self.defaults = defaults
317 Self.migrate(defaults)
318 }
319
320 /// The one choice of 0.4.15 and before, as the two switches. "Anywhere
321 /// I put it" was someone turning snapping off, so it stays off. "Snap
322 /// to corners" was everyone else — chosen or, far more often, the
323 /// default — and becomes the new default, both on, which is what the
324 /// only person to have used it asked for; snapping alone remains a
325 /// click away. Idempotent: the old key goes once it has been read.
326 public static func migrate(_ defaults: UserDefaults) {
327 guard let legacy = defaults.string(forKey: legacyPlacementKey) else { return }
328 if defaults.object(forKey: moveFreelyKey) == nil, defaults.object(forKey: snapToCornersKey) == nil {
329 defaults.set(true, forKey: moveFreelyKey)
330 defaults.set(legacy != "anywhere", forKey: snapToCornersKey)
331 }
332 defaults.removeObject(forKey: legacyPlacementKey)
333 }
334
335 public var enabled: Bool {
336 get { defaults.object(forKey: Self.enabledKey) as? Bool ?? Self.defaultEnabled }
337 nonmutating set { defaults.set(newValue, forKey: Self.enabledKey) }
338 }
339
340 public var corner: MiniWindowCorner {
341 get {
342 defaults.string(forKey: Self.cornerKey).flatMap(MiniWindowCorner.init(rawValue:))
343 ?? Self.defaultCorner
344 }
345 nonmutating set { defaults.set(newValue.rawValue, forKey: Self.cornerKey) }
346 }
347
348 /// A dragged window stays where it is dropped (unless a corner takes it).
349 public var moveFreely: Bool {
350 get { defaults.object(forKey: Self.moveFreelyKey) as? Bool ?? Self.defaultMoveFreely }
351 nonmutating set {
352 defaults.set(newValue, forKey: Self.moveFreelyKey)
353 if !newValue, !snapToCorners { defaults.set(true, forKey: Self.snapToCornersKey) }
354 }
355 }
356
357 /// A dragged window settles in a corner: the nearest, or with
358 /// `moveFreely`, one it is let go near.
359 public var snapToCorners: Bool {
360 get { defaults.object(forKey: Self.snapToCornersKey) as? Bool ?? Self.defaultSnapToCorners }
361 nonmutating set {
362 defaults.set(newValue, forKey: Self.snapToCornersKey)
363 if !newValue, !moveFreely { defaults.set(true, forKey: Self.moveFreelyKey) }
364 }
365 }
366
367 /// The width last resized to. Only the width: the height follows
368 /// from 16:9, so there is nothing to disagree about.
369 public var width: CGFloat {
370 get {
371 (defaults.object(forKey: Self.widthKey) as? Double).map { CGFloat($0) }
372 ?? MiniWindowRules.size.width
373 }
374 nonmutating set { defaults.set(Double(newValue), forKey: Self.widthKey) }
375 }
376
377 /// Where the window was left placed freely, per display, as an
378 /// offset from that display's visible frame (see
379 /// MiniWindowRules.offset). Keyed by the display's ID as a string, and
380 /// kept as one JSON string so that Settings can clear it through
381 /// AppStorage.
382 public func position(on display: String) -> CGPoint? {
383 guard let pair = positions[display], pair.count == 2 else { return nil }
384 return CGPoint(x: pair[0], y: pair[1])
385 }
386
387 public func setPosition(_ offset: CGPoint, on display: String) {
388 var all = positions
389 all[display] = [Double(offset.x), Double(offset.y)]
390 guard let data = try? JSONEncoder().encode(all) else { return }
391 defaults.set(String(decoding: data, as: UTF8.self), forKey: Self.positionsKey)
392 }
393
394 private var positions: [String: [Double]] {
395 guard let text = defaults.string(forKey: Self.positionsKey),
396 let all = try? JSONDecoder().decode([String: [Double]].self, from: Data(text.utf8))
397 else { return [:] }
398 return all
399 }
400
401 public func forgetPositions() { defaults.removeObject(forKey: Self.positionsKey) }
402
403 /// One display's free position forgotten: it went into a corner.
404 public func forgetPosition(on display: String) {
405 var all = positions
406 guard all.removeValue(forKey: display) != nil else { return }
407 guard let data = try? JSONEncoder().encode(all) else { return }
408 defaults.set(String(decoding: data, as: UTF8.self), forKey: Self.positionsKey)
409 }
410 }
411
412 /// What the mini window remembers for one call and forgets when it
413 /// ends: whether it was collapsed to a tab, and any position it was
414 /// ⌥-dragged to while it may only snap to corners. Every call
415 /// starts expanded — a tab left over from yesterday's call would be a
416 /// call nobody can see they are in.
417 public struct MiniWindowCallMemory: Equatable {
418 public var collapsed = false
419 /// ⌥-dropped positions by display, as offsets. One-offs: they win
420 /// over the corner for the rest of this call and are never saved.
421 public private(set) var oneOff: [String: CGPoint] = [:]
422
423 public init() {}
424
425 /// Where a newly shown window of `size` goes on `display`.
426 public func frame(
427 size: CGSize, in visible: CGRect, display: String, preferences: MiniWindowPreferences
428 ) -> CGRect {
429 if let offset = oneOff[display] {
430 return MiniWindowRules.frame(offset: offset, size: size, in: visible)
431 }
432 if preferences.moveFreely, let offset = preferences.position(on: display) {
433 return MiniWindowRules.frame(offset: offset, size: size, in: visible)
434 }
435 return preferences.corner.frame(size: size, in: visible)
436 }
437
438 /// A drag ended with the window at `frame` on `display`. Returns
439 /// where it settles.
440 ///
441 /// - ⌥ held: exactly there (clamped on screen), whatever the corners
442 /// would have done — saved for that display if it may move freely,
443 /// for this call only if it may only snap.
444 /// - Snapping alone: the nearest corner.
445 /// - Both: a corner if it was let go within `snapDistance` of one,
446 /// and otherwise exactly there, saved for that display.
447 /// - Moving freely alone: exactly there, saved for that display.
448 ///
449 /// A corner it settles in becomes the setting, and that display's
450 /// free position and any one-off on it are forgotten.
451 public mutating func dropped(
452 _ frame: CGRect, in visible: CGRect, display: String, option: Bool,
453 preferences: MiniWindowPreferences
454 ) -> CGRect {
455 let placed = MiniWindowRules.clamp(frame, into: visible)
456 let free = preferences.moveFreely
457 if option, !free {
458 oneOff[display] = MiniWindowRules.offset(of: placed, in: visible)
459 return placed
460 }
461 if !option, preferences.snapToCorners,
462 let corner = free
463 ? MiniWindowCorner.magnet(for: placed, in: visible)
464 : MiniWindowCorner.nearest(to: frame, in: visible)
465 {
466 oneOff[display] = nil
467 preferences.corner = corner
468 preferences.forgetPosition(on: display)
469 return corner.frame(size: frame.size, in: visible)
470 }
471 oneOff[display] = nil
472 preferences.setPosition(MiniWindowRules.offset(of: placed, in: visible), on: display)
473 return placed
474 }
475
476 /// A resize ended with the window at `frame`. It settles as a drop
477 /// would, except that a window ⌥-placed for this call stays placed
478 /// rather than snapping: resizing it is not asking for a corner.
479 public mutating func resized(
480 _ frame: CGRect, in visible: CGRect, display: String, preferences: MiniWindowPreferences
481 ) -> CGRect {
482 dropped(
483 frame, in: visible, display: display, option: oneOff[display] != nil,
484 preferences: preferences)
485 }
486 }
487
488 /// When the mini window comes and goes. Kept apart from the windows it
489 /// moves, like KnockWatch, because this is the part with rules in it and
490 /// the windows need a real app to exist.
491 ///
492 /// The rules:
493 /// - It appears only when the app stops being frontmost during a call,
494 /// with the setting on and the call window on screen. A minimised or
495 /// closed call window is one the person already put away themselves.
496 /// - ⌘H wins. Hiding the app is asking for all of it to go, so a hidden
497 /// app shows nothing, and a mini window already up is put away before
498 /// the hide so that unhiding brings back the ordinary window. AppKit's
499 /// order between "will hide" and "resigned active" is not something to
500 /// rely on, so both orders end the same way.
501 /// - Coming back to the app, by any route, restores the call window.
502 /// - The call ending puts it away too — someone who pressed Leave in the
503 /// mini window is done, and the lobby is waiting for them underneath.
504 public struct MiniWindowPolicy: Equatable {
505 public struct Conditions: Equatable {
506 public var enabled: Bool
507 public var inCall: Bool
508 public var mainWindowVisible: Bool
509 public var mainWindowMiniaturized: Bool
510 public var mainWindowFullScreen: Bool
511
512 public init(
513 enabled: Bool, inCall: Bool, mainWindowVisible: Bool,
514 mainWindowMiniaturized: Bool = false, mainWindowFullScreen: Bool = false
515 ) {
516 self.enabled = enabled
517 self.inCall = inCall
518 self.mainWindowVisible = mainWindowVisible
519 self.mainWindowMiniaturized = mainWindowMiniaturized
520 self.mainWindowFullScreen = mainWindowFullScreen
521 }
522 }
523
524 public enum Action: Equatable {
525 case none
526 /// Show the mini window. `hideMain` is false for a full-screen call
527 /// window: hiding one leaves its Space black and empty, and it is
528 /// already out of the way in a Space of its own.
529 case show(hideMain: Bool)
530 /// Close the mini window and put the call window back as it was.
531 /// `activate` is false when the app is not coming forward — the call
532 /// ended from the mini window, or the app is being hidden — so the
533 /// window returns where it was in the stack, behind whatever the
534 /// person is doing, rather than jumping on top of it.
535 case restore(activate: Bool)
536 }
537
538 public private(set) var showing = false
539 /// Whether the main window was hidden, as opposed to left alone in
540 /// its full-screen Space.
541 public private(set) var mainHidden = false
542 public private(set) var appHidden = false
543
544 public init() {}
545
546 public mutating func resignedActive(_ conditions: Conditions) -> Action {
547 guard !showing, !appHidden, conditions.enabled, conditions.inCall,
548 conditions.mainWindowVisible, !conditions.mainWindowMiniaturized
549 else { return .none }
550 showing = true
551 mainHidden = !conditions.mainWindowFullScreen
552 return .show(hideMain: mainHidden)
553 }
554
555 public mutating func becameActive() -> Action {
556 appHidden = false
557 return restore(activate: true)
558 }
559
560 public mutating func willHide() -> Action {
561 appHidden = true
562 return restore(activate: false)
563 }
564
565 public mutating func didUnhide() { appHidden = false }
566
567 public mutating func callEnded() -> Action { restore(activate: false) }
568
569 private mutating func restore(activate: Bool) -> Action {
570 guard showing else { return .none }
571 showing = false
572 mainHidden = false
573 return .restore(activate: activate)
574 }
575 }
576
577/// What one press on the mini window's surface is (2026-10-05, Paul:
578/// "Sometimes I can't click the mini window to either bring the call to
579/// the front or move it around"): apart from any view, so tests hold it.
580/// A press that ends within `slack` of where it began is a click, which
581/// brings the call forward (the tab: opens the window; twice, the call).
582/// One that travels further is a drag: from an edge or corner of the
583/// expanded window it resizes, from anywhere else it moves. The tab has
584/// no edges to resize by. Controls are SwiftUI buttons over the surface
585/// and never reach it.
586public enum MiniWindowGesture {
587 public enum Surface: String, Sendable { case window, tab }
588 public enum Outcome: Equatable, Sendable {
589 case click(count: Int)
590 case move(option: Bool)
591 case resize(MiniWindowResize.Handle)
592 }
593
594 /// How far a press may wander and still be a click: a shaky hand's.
595 public static let slack: CGFloat = 4
596
597 /// The edges a press at `point` takes hold of: none on the tab, and none
598 /// on a window too small for its grips to leave a middle to move by.
599 public static func grab(at point: CGPoint, in size: CGSize, surface: Surface) -> MiniWindowResize.Handle {
600 guard surface == .window, size.width > 4 * MiniWindowResize.grip,
601 size.height > 4 * MiniWindowResize.grip
602 else { return [] }
603 return MiniWindowResize.handle(at: point, in: size)
604 }
605
606 public static func dragged(dx: CGFloat, dy: CGFloat) -> Bool { hypot(dx, dy) > slack }
607
608 /// The press's outcome once the button is up.
609 public static func outcome(
610 grabbed: MiniWindowResize.Handle, dragged: Bool, clickCount: Int, option: Bool
611 ) -> Outcome {
612 guard dragged else { return .click(count: max(1, clickCount)) }
613 return grabbed.isEmpty ? .move(option: option) : .resize(grabbed)
614 }
615}
616#endif
617