| 1 | #if canImport(RastrilloMacUpdates) |
| 2 | import RastrilloMacUpdates |
| 3 | #endif |
| 4 | import AppKit |
| 5 | import BaseKit |
| 6 | import KeyboardShortcuts |
| 7 | import RastrilloApple |
| 8 | import ServiceManagement |
| 9 | import SwiftUI |
| 10 | |
| 11 | extension KeyboardShortcuts.Name { |
| 12 | /// Ask Base from anywhere: off until the person records one (P11). |
| 13 | static let askBase = Self("askBase") |
| 14 | } |
| 15 | |
| 16 | /// Base for the Mac. One window (B): the sky, a floating sidebar and Ask. |
| 17 | /// Ask is also a menu-bar panel (on by default), presented by Base's own |
| 18 | /// StatusPanelController so the global shortcut, once the person records |
| 19 | /// one, can open it. Settings (⌘,) has General, Account and |
| 20 | /// Ask Base; the Colophon is under Help. |
| 21 | @main struct BaseMacApp: App { |
| 22 | @NSApplicationDelegateAdaptor(BaseMacDelegate.self) private var delegate |
| 23 | @AppStorage("showMenuBar") private var showMenuBar = true |
| 24 | @AppStorage(OpenPreference.key) private var openIn = OpenPreference.app.rawValue |
| 25 | #if canImport(RastrilloMacUpdates) |
| 26 | // Quiet, as the other Oficina apps: a scheduled check never opens a |
| 27 | // window; an update downloads in the background and installs on quit. |
| 28 | @State private var updater = NativeUpdater(policy: .quiet) |
| 29 | #endif |
| 30 | |
| 31 | init() { |
| 32 | // Before the first frame, so nothing paints in a fallback face. |
| 33 | BaseFonts.register() |
| 34 | } |
| 35 | |
| 36 | private var session: BaseSession { delegate.session } |
| 37 | |
| 38 | var body: some Scene { |
| 39 | Window(BaseText.text("native.sidebar.ask"), id: "main") { |
| 40 | MacRoot(session: session, actions: delegate.actions) |
| 41 | .onOpenURL { delegate.openURL.handle($0) } |
| 42 | .onChange(of: showMenuBar, initial: true) { _, shown in delegate.panel?.itemShown = shown } |
| 43 | .modifier(KeepOpenWindow(delegate: delegate)) |
| 44 | #if canImport(RastrilloMacUpdates) |
| 45 | .safeAreaInset(edge: .bottom) { |
| 46 | if updater.canInstallPendingUpdate { NativeUpdateNotice(updater: updater).padding(.bottom, 12) } |
| 47 | } |
| 48 | #endif |
| 49 | } |
| 50 | .windowStyle(.hiddenTitleBar) |
| 51 | .windowToolbarStyle(.unified(showsTitle: false)) |
| 52 | .defaultSize(width: 1280, height: 800) |
| 53 | .commands { |
| 54 | // First: About, Hide and Quit say "Base", not the Finder name. |
| 55 | PlainAppNameCommands() |
| 56 | #if canImport(RastrilloMacUpdates) |
| 57 | NativeUpdateCommands(updater: updater) |
| 58 | #endif |
| 59 | // One window: ⌘N opens nothing new. |
| 60 | CommandGroup(replacing: .newItem) {} |
| 61 | CommandGroup(replacing: .help) { ColophonCommand() } |
| 62 | } |
| 63 | |
| 64 | Settings { |
| 65 | MacSettingsRoot(session: session, actions: delegate.actions, showMenuBar: $showMenuBar, |
| 66 | openIn: Binding(get: { OpenPreference(rawValue: openIn) ?? .app }, set: { openIn = $0.rawValue })) |
| 67 | .onChange(of: showMenuBar) { _, shown in delegate.panel?.itemShown = shown } |
| 68 | } |
| 69 | |
| 70 | Window(ColophonView.title, id: "colophon") { ColophonView() } |
| 71 | .windowResizability(.contentMinSize) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | struct ColophonCommand: View { |
| 76 | @Environment(\.openWindow) private var openWindow |
| 77 | var body: some View { |
| 78 | Button(ColophonView.title) { openWindow(id: "colophon") } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// The app's session and what its screens do, made once, before the first |
| 83 | /// window, and the shortcut's handler. |
| 84 | @MainActor final class BaseMacDelegate: NSObject, NSApplicationDelegate { |
| 85 | let session = BaseSession.live() |
| 86 | lazy var actions = MacActions.make(session: session) |
| 87 | private(set) var panel: StatusPanelController? |
| 88 | /// The panel's foot shows the shortcut as it is when the panel opens. |
| 89 | let panelState = PanelState() |
| 90 | /// The window's scene's openWindow, kept from the window's first |
| 91 | /// appearance, so Open Base can bring it back after it was closed. |
| 92 | var openMain: (() -> Void)? |
| 93 | lazy var openURL = BaseOpenURL(showAsk: { [weak self] in self?.showMain() }, openWeb: { [actions] in actions.openWeb($0) }) |
| 94 | |
| 95 | func applicationDidFinishLaunching(_ notification: Notification) { |
| 96 | UserDefaults.standard.register(defaults: ["showMenuBar": true]) |
| 97 | let session = self.session, actions = self.actions, state = panelState |
| 98 | panel = StatusPanelController(itemShown: UserDefaults.standard.bool(forKey: "showMenuBar"), |
| 99 | accessibilityLabel: BaseText.text("native.sidebar.ask")) { |
| 100 | MenuPanelRoot(session: session, actions: actions, state: state) { [weak self] in |
| 101 | self?.panel?.hide() |
| 102 | self?.showMain() |
| 103 | } |
| 104 | } |
| 105 | panel?.onShow = { state.shortcut = KeyboardShortcuts.getShortcut(for: .askBase)?.description } |
| 106 | KeyboardShortcuts.onKeyUp(for: .askBase) { [weak self] in self?.panel?.shortcutPressed() } |
| 107 | Task { await session.oficina.start() } |
| 108 | } |
| 109 | |
| 110 | /// Open Base: the window, forward (opened again if it was closed). |
| 111 | func showMain() { |
| 112 | NSApp.activate() |
| 113 | if let window = NSApp.windows.first(where: { $0.identifier?.rawValue.hasPrefix("main") == true && $0.isVisible }) { |
| 114 | window.makeKeyAndOrderFront(nil) |
| 115 | } else { |
| 116 | openMain?() |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func applicationDidBecomeActive(_ notification: Notification) { |
| 121 | Task { await session.foreground() } |
| 122 | } |
| 123 | |
| 124 | func applicationDidResignActive(_ notification: Notification) {} |
| 125 | |
| 126 | func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { |
| 127 | if !flag { showMain() } |
| 128 | return true |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// Sign-in until there is a session, then the window (B). |
| 133 | struct MacRoot: View { |
| 134 | let session: BaseSession |
| 135 | let actions: BaseActions |
| 136 | @SceneStorage("sidebarShown") private var sidebarShown = true |
| 137 | @Environment(\.scenePhase) private var phase |
| 138 | |
| 139 | var body: some View { |
| 140 | Group { |
| 141 | if let state = session.signedIn { |
| 142 | MacMainView(me: session.me, home: session.home, ask: session.ask, transport: session.iconTransport, |
| 143 | actions: actions, sidebarShown: $sidebarShown) |
| 144 | .overlay(alignment: .top) { |
| 145 | NativeOfflineBanner(origin: state.origin, message: BaseText.text("native.offline")).padding(.top, 8) |
| 146 | } |
| 147 | } else { |
| 148 | BaseSignIn(model: session.oficina, openURL: { NSWorkspace.shared.open($0) }) |
| 149 | } |
| 150 | } |
| 151 | .ignoresSafeArea() |
| 152 | .frame(minWidth: 860, minHeight: 560) |
| 153 | .baseAppearance(session.me?.appearance ?? .system) |
| 154 | .onChange(of: phase) { _, now in if now != .active { session.background() } } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// The menu-bar panel, from the session. |
| 159 | struct MenuPanelRoot: View { |
| 160 | let session: BaseSession |
| 161 | let actions: BaseActions |
| 162 | let state: PanelState |
| 163 | let openBase: () -> Void |
| 164 | var body: some View { |
| 165 | MenuPanel(me: session.me, ask: session.signedIn == nil ? nil : session.ask, |
| 166 | shortcut: state.shortcut, |
| 167 | openBase: openBase, actions: actions) |
| 168 | .baseAppearance(session.me?.appearance ?? .system) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// What the panel shows that lives outside the session: the shortcut, |
| 173 | /// read again each time the panel opens. |
| 174 | @MainActor @Observable final class PanelState { |
| 175 | var shortcut: String? = KeyboardShortcuts.getShortcut(for: .askBase)?.description |
| 176 | } |
| 177 | |
| 178 | /// Keeps the window scene's openWindow for the app delegate. |
| 179 | struct KeepOpenWindow: ViewModifier { |
| 180 | let delegate: BaseMacDelegate |
| 181 | @Environment(\.openWindow) private var openWindow |
| 182 | func body(content: Content) -> some View { |
| 183 | content.onAppear { delegate.openMain = { openWindow(id: "main") } } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /// Settings, from the session and this Mac. |
| 188 | struct MacSettingsRoot: View { |
| 189 | let session: BaseSession |
| 190 | let actions: BaseActions |
| 191 | @Binding var showMenuBar: Bool |
| 192 | @Binding var openIn: OpenPreference |
| 193 | @State private var openAtLogin = SMAppService.mainApp.status == .enabled |
| 194 | |
| 195 | var body: some View { |
| 196 | MacSettingsView( |
| 197 | model: .init(me: session.me, identity: session.signedIn?.identity.label ?? "", askPhase: session.ask?.phase ?? .loading, |
| 198 | installed: Opener.installedNames(session.home?.apps ?? [], installed: MacActions.installed), |
| 199 | shortcutSet: KeyboardShortcuts.getShortcut(for: .askBase) != nil), |
| 200 | showMenuBar: $showMenuBar, |
| 201 | openAtLogin: Binding(get: { openAtLogin }, set: { on in |
| 202 | do { |
| 203 | if on { try SMAppService.mainApp.register() } else { try SMAppService.mainApp.unregister() } |
| 204 | } catch {} |
| 205 | openAtLogin = SMAppService.mainApp.status == .enabled |
| 206 | }), |
| 207 | openIn: $openIn, |
| 208 | setAppearance: { appearance in Task { await session.setAppearance(appearance) } }, |
| 209 | disconnect: { Task { await session.disconnectAsk() } }, |
| 210 | actions: actions |
| 211 | ) { |
| 212 | KeyboardShortcuts.Recorder(for: .askBase) |
| 213 | } |
| 214 | .baseAppearance(session.me?.appearance ?? .system) |
| 215 | } |
| 216 | } |
| 217 | |