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)
2import RastrilloApple
3import RastrilloMacUpdates
4#endif
5import AppKit
6@preconcurrency import CallsBridge
7@_spi(Fixtures) import CallsKit
8import SwiftUI
9import LiveKitWebRTC
10
11@main struct CallsMacApp: App {
12 #if os(macOS)
13 // Quiet: an update downloads in the background and installs when Calls quits,
14 // and nothing it finds opens a window. `isBusy` follows the call, so no check
15 // or download starts during one and the restart waits until it ends.
16 @State private var updater = NativeUpdater(policy: .quiet)
17 #endif
18 @NSApplicationDelegateAdaptor(CallsAppDelegate.self) private var appDelegate
19 @StateObject private var model = CallsModel()
20 var body: some Scene {
21 Window("Calls", id: "main") {
22 ContentView(model: model, updater: updater).frame(minWidth: 760, minHeight: 540).onDisappear { model.leave() }
23 .onOpenURL { model.openMeetingURL($0) }
24 .onAppear {
25 CallsAppDelegate.model = model
26 model.restore()
27 AudioWarmUp.afterLaunch()
28 }
29 .miniWindow(for: model)
30 .onChange(of: model.joining, initial: true) { updater.isBusy = model.joining }
31 }
32 .defaultSize(width: 1000, height: 760)
33 .commands {
34 // First, so that Check for Updates (after About) survives the groups it
35 // replaces. About, Hide, Quit and Help say "Calls", not the display name
36 // "Oficina Calls" SwiftUI would use. Quit is NSApp.terminate, so it still
37 // reaches applicationShouldTerminate below and a call is left first. The
38 // name is passed, not read from CFBundleName, so `swift run` says Calls too.
39 PlainAppNameCommands(name: "Calls")
40 CommandGroup(replacing: .newItem) {}
41 NativeUpdateCommands(updater: updater)
42 CommandMenu("Call") { CallMenuItems(model: model) }
43 }
44 Settings {
45 CallsSettingsView(
46 model: model,
47 automaticUpdates: updater.canInstallUpdatesAutomatically
48 ? Binding(get: { updater.automaticallyInstallsUpdates }, set: { updater.automaticallyInstallsUpdates = $0 })
49 : nil)
50 // SwiftUI titles a Settings window without tabs from the display
51 // name ("Oficina Calls Settings"); inside the app it is plain Calls.
52 .navigationTitle("Calls")
53 }
54 }
55}
56
57final class CallsAppDelegate: NSObject, NSApplicationDelegate {
58 /// Set by the app so Apple's answer can reach the session that asked.
59 /// Weak-ish by convention rather than by keyword: the model outlives
60 /// the delegate for the whole life of the process.
61 @MainActor static var model: CallsModel?
62
63 func application(
64 _ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken token: Data
65 ) {
66 Task { @MainActor in CallsAppDelegate.model?.registerDevice(token: token) }
67 }
68
69 func application(
70 _ application: NSApplication, didFailToRegisterForRemoteNotificationsWithError error: Error
71 ) {
72 // Expected on any build without the push entitlement, which is every
73 // ad-hoc signed local build. Silent: the person did not ask for this
74 // and the consequence is simply no push.
75 }
76
77 /// Quitting mid-call leaves the call first. Without this the process
78 /// ended with its participation still in the room, inside the server's
79 /// thirty-second resume window, and the next launch — an update's
80 /// relaunch, most often — joined as a second participation of the same
81 /// session and was told "This meeting is open elsewhere". (The core now
82 /// takes its own place back in that case too; leaving is still the
83 /// right thing to do, because the others in the call see you go at
84 /// once rather than thirty seconds later.) The leave is given three
85 /// seconds; the core's own request gives up after two.
86 func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
87 guard let model = CallsAppDelegate.model, model.joining else { return .terminateNow }
88 var replied = false
89 let reply = { @MainActor in
90 guard !replied else { return }
91 replied = true
92 sender.reply(toApplicationShouldTerminate: true)
93 }
94 Task { @MainActor in
95 await model.leaveNow()
96 reply()
97 }
98 DispatchQueue.main.asyncAfter(deadline: .now() + 3) { MainActor.assumeIsolated { reply() } }
99 return .terminateLater
100 }
101
102 func applicationDidFinishLaunching(_ notification: Notification) {
103 // 0.4.15's one placement choice, as 0.4.16's two switches, before
104 // Settings can read them.
105 MiniWindowPreferences.migrate(.standard)
106 // On macOS 26 and later a bundled app's Dock icon comes from Assets.car,
107 // which the system masks and renders in the person's icon style. Setting
108 // a picture here would replace that with a flat PNG, so only a run
109 // without the bundle's icon (swift run) or an older macOS sets one.
110 if #available(macOS 26, *), Bundle.main.object(forInfoDictionaryKey: "CFBundleIconName") != nil {
111 return
112 }
113 if let url = Bundle.main.url(forResource: "MacIcon", withExtension: "png")
114 ?? Bundle.module.url(forResource: "MacIcon", withExtension: "png")
115 {
116 NSApp.applicationIconImage = NSImage(contentsOf: url)
117 }
118 }
119}
120
121/// The Call menu. Mute is here so that its shortcut can be found, and so
122/// that there is one when Space belongs to a text field or a button. Copy
123/// Invite Link puts the meeting's link on the clipboard without the share
124/// sheet. Both work from the mini window too: the menu is the app's.
125struct CallMenuItems: View {
126 @ObservedObject var model: CallsModel
127 var body: some View { CallMenuButtons(call: model.call, inCall: model.joining) }
128}
129
130private struct CallMenuButtons: View {
131 @ObservedObject var call: Call
132 let inCall: Bool
133 var body: some View {
134 ForEach(CallMenuItem.allCases, id: \.self) { item in
135 switch item {
136 case .mute:
137 Button(item.title(muted: call.muted)) { call.mute() }
138 .keyboardShortcut(item.shortcut)
139 .disabled(!inCall)
140 case .copyInviteLink:
141 Button(item.title(muted: call.muted)) {
142 Task { await call.copyInviteLink(inCall: inCall) }
143 }
144 .keyboardShortcut(item.shortcut)
145 .disabled(
146 InviteLinkCopy.rule(inCall: inCall, phase: call.state?.phase, standing: call.standing)
147 == .unavailable)
148 }
149 }
150 Divider()
151 // Spec §15.26: during a call, and for ten minutes after it.
152 Button("Report a Problem\u{2026}") { call.reportProblem() }
153 .disabled(!call.canReportProblem)
154 }
155}
156
157struct ContentView: View {
158 @ObservedObject var model: CallsModel
159 let updater: NativeUpdater
160 var body: some View {
161 Group {
162 if let fixture = MacCallFixtureView.requested {
163 MacCallFixtureView(fixture: fixture)
164 } else if model.joining {
165 // The layout pads itself: the classic view sits in a margin, the
166 // full-window one goes to the window's edges.
167 MacCallView(call: model.call, leave: model.leave, rejoin: model.join)
168 } else {
169 // The home screen is the only place an update is mentioned: never the call.
170 CallsLobby(model: model).safeAreaInset(edge: .bottom) {
171 if updater.canInstallPendingUpdate { NativeUpdateNotice(updater: updater).padding(.bottom, 12) }
172 }
173 }
174 }.tint(CallsAppearance.accent)
175 }
176}
177
178/// The full-window call with no server behind it, in the real window, to
179/// see the title bar, the traffic lights and the drag strip for real.
180/// Debug builds only: `-CallsCallFixture focus` (or grid, screen,
181/// notices), optionally `-CallsFixtureBackdrop black|white|busy`,
182/// `-CallsFixtureSelf muted|cameraOff|sharing` and `-CallsFixtureControls
183/// shown|hidden`; without that last, the pointer decides as in a call.
184struct MacCallFixtureView: View {
185 static var requested: CallFixture? {
186 #if DEBUG
187 UserDefaults.standard.string(forKey: "CallsCallFixture").flatMap(CallFixture.init(rawValue:))
188 #else
189 nil
190 #endif
191 }
192
193 let fixture: CallFixture
194 @StateObject private var call = Call(playAudio: false)
195 /// `-CallsFixtureJoinAfter <s>` starts on a stand-in for the lobby and
196 /// joins after that long; `-CallsFixtureLeaveAfter <s>` goes back to it:
197 /// the window's title bar has to come and go with the call.
198 @State private var inCall = UserDefaults.standard.object(forKey: "CallsFixtureJoinAfter") == nil
199
200 var body: some View {
201 Group {
202 if inCall {
203 callView
204 } else {
205 Text("Lobby stand-in").frame(maxWidth: .infinity, maxHeight: .infinity)
206 }
207 }
208 .task {
209 let defaults = UserDefaults.standard
210 if defaults.object(forKey: "CallsFixtureJoinAfter") != nil {
211 try? await Task.sleep(for: .seconds(defaults.double(forKey: "CallsFixtureJoinAfter")))
212 inCall = true
213 }
214 if defaults.object(forKey: "CallsFixtureLeaveAfter") != nil {
215 try? await Task.sleep(for: .seconds(defaults.double(forKey: "CallsFixtureLeaveAfter")))
216 inCall = false
217 }
218 }
219 }
220
221 private var callView: some View {
222 let defaults = UserDefaults.standard
223 return MacCallView(
224 call: call, grid: fixture == .grid,
225 controlsShown: defaults.string(forKey: "CallsFixtureControls").map { $0 == "shown" },
226 leave: {}, rejoin: {}
227 )
228 .environment(
229 \.fixtureBackdrop,
230 defaults.string(forKey: "CallsFixtureBackdrop").flatMap(FixtureBackdrop.init(rawValue:)))
231 .onAppear {
232 fixture.apply(to: call)
233 defaults.string(forKey: "CallsFixtureSelf").flatMap(FixtureSelf.init(rawValue:))?
234 .apply(to: call)
235 }
236 }
237}
238
239