import AppKit import SwiftUI // StripViewModel holds the state for the tab bar view. The view model is // observable. The strip controller sets the active window and the action // closures. The view reads the window store through the view model. @MainActor @Observable final class StripViewModel { let store: WindowStore var activeWindowID: CGWindowID? var availableWidth: CGFloat = 800 @ObservationIgnored var onActivate: ((CGWindowID) -> Void)? @ObservationIgnored var onMinimize: ((CGWindowID) -> Void)? @ObservationIgnored var onClose: ((CGWindowID) -> Void)? init(store: WindowStore) { self.store = store } // appIcon returns the icon of the frontmost application. func appIcon() -> NSImage? { guard let pid = store.frontmostPID, let app = NSRunningApplication(processIdentifier: pid) else { return nil } return app.icon } // appName returns the name of the frontmost application. func appName() -> String { guard let pid = store.frontmostPID, let app = NSRunningApplication(processIdentifier: pid) else { return "" } return app.localizedName ?? "" } } // StripController owns the strip panel and the tab bar view. The controller // positions the strip and shows or hides the strip. The controller forwards // the tab actions through the closures. See spec sections 7.7, 7.8, 7.9. // The controller does not read AX and does not detect full screen. The // integration layer calls setVisible for the full-screen case (decision D7). @MainActor final class StripController { private let panel: StripPanel private let model: StripViewModel private let settings: Settings private let hostingView: NSHostingView var onActivate: ((CGWindowID) -> Void)? { get { model.onActivate } set { model.onActivate = newValue } } var onMinimize: ((CGWindowID) -> Void)? { get { model.onMinimize } set { model.onMinimize = newValue } } var onClose: ((CGWindowID) -> Void)? { get { model.onClose } set { model.onClose = newValue } } // activeWindowID is the focused window of the frontmost application. The // integration layer sets this value. The tab bar marks this tab as active. var activeWindowID: CGWindowID? { get { model.activeWindowID } set { model.activeWindowID = newValue } } // stripFrame is the current frame of the strip panel. The constraint // service reads this frame for the limit line (spec section 7.10). var stripFrame: CGRect { return panel.frame } init(store: WindowStore, settings: Settings) { self.settings = settings self.model = StripViewModel(store: store) let screen = StripPositioner.dockScreen(NSScreen.screens, main: NSScreen.main) ?? NSScreen.main let frame = screen.map { StripPositioner.stripFrame(for: $0, height: CGFloat(settings.stripHeight)) } ?? NSRect(x: 0, y: 0, width: 800, height: CGFloat(settings.stripHeight)) panel = StripPanel(contentRect: frame) hostingView = NSHostingView(rootView: TabBarView(model: model)) hostingView.frame = NSRect(origin: .zero, size: frame.size) hostingView.autoresizingMask = [.width, .height] panel.contentView?.addSubview(hostingView) model.availableWidth = frame.width } func start() { reposition() setVisible(true) } // reposition recomputes the strip frame. The integration layer calls this // function on a screen change, a Dock change, and after wake from sleep. func reposition() { guard let screen = StripPositioner.dockScreen(NSScreen.screens, main: NSScreen.main) else { return } let frame = StripPositioner.stripFrame(for: screen, height: CGFloat(settings.stripHeight)) panel.setFrame(frame, display: true) model.availableWidth = frame.width Log.strip.debug("Strip repositioned to \(String(describing: frame), privacy: .public)") } // setVisible shows or hides the strip. func setVisible(_ visible: Bool) { if visible { panel.orderFrontRegardless() } else { panel.orderOut(nil) } } }