mac: global outside-click monitor and highlight helper

main
Peter Steinberger 2025-12-10 00:51:02 +01:00
parent 1a17de9d39
commit 3b9d84e2b1
2 changed files with 18 additions and 8 deletions

View File

@ -15,6 +15,11 @@ struct ClawdisApp: App {
@State private var statusItem: NSStatusItem? @State private var statusItem: NSStatusItem?
@State private var isMenuPresented = false @State private var isMenuPresented = false
@State private var isPanelVisible = false @State private var isPanelVisible = false
@MainActor
private func updateStatusHighlight() {
self.statusItem?.button?.highlight(self.isPanelVisible)
}
init() { init() {
_state = StateObject(wrappedValue: AppStateStore.shared) _state = StateObject(wrappedValue: AppStateStore.shared)
@ -49,6 +54,9 @@ struct ClawdisApp: App {
} }
.defaultSize(width: SettingsTab.windowWidth, height: SettingsTab.windowHeight) .defaultSize(width: SettingsTab.windowWidth, height: SettingsTab.windowHeight)
.windowResizability(.contentSize) .windowResizability(.contentSize)
.onChange(of: self.isMenuPresented) { _, _ in
self.updateStatusHighlight()
}
} }
private func applyStatusItemAppearance(paused: Bool) { private func applyStatusItemAppearance(paused: Bool) {
@ -62,7 +70,7 @@ struct ClawdisApp: App {
WebChatManager.shared.onPanelVisibilityChanged = { [self] visible in WebChatManager.shared.onPanelVisibilityChanged = { [self] visible in
self.isPanelVisible = visible self.isPanelVisible = visible
self.statusItem?.button?.highlight(visible) self.updateStatusHighlight()
} }
let handler = StatusItemMouseHandlerView() let handler = StatusItemMouseHandlerView()
@ -70,8 +78,8 @@ struct ClawdisApp: App {
handler.onLeftClick = { [self] in self.toggleWebChatPanel() } handler.onLeftClick = { [self] in self.toggleWebChatPanel() }
handler.onRightClick = { [self] in handler.onRightClick = { [self] in
WebChatManager.shared.closePanel() WebChatManager.shared.closePanel()
self.statusItem?.button?.highlight(false)
self.isMenuPresented = true self.isMenuPresented = true
self.updateStatusHighlight()
} }
button.addSubview(handler) button.addSubview(handler)

View File

@ -353,14 +353,16 @@ final class WebChatWindowController: NSWindowController, WKNavigationDelegate, N
private func installDismissMonitor() { private func installDismissMonitor() {
guard self.localDismissMonitor == nil, let panel = self.window else { return } guard self.localDismissMonitor == nil, let panel = self.window else { return }
self.localDismissMonitor = NSEvent.addLocalMonitorForEvents( self.localDismissMonitor = NSEvent.addGlobalMonitorForEvents(
matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown] matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown]
) { [weak self] event in ) { [weak self] _ in
guard let self else { return event } guard let self else { return }
if event.window !== panel { let pt = NSEvent.mouseLocation // screen coordinates
self.closePanel() if !panel.frame.contains(pt) {
Task { @MainActor in
self.closePanel()
}
} }
return event
} }
} }