mac: global outside-click monitor and highlight helper

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

View File

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