Close the panel when a mouse-down is sent to another app. The panel observes didResignKey for clicks that make another window key, and installs a transient global mouse-down monitor while visible for clicks outside an open popover or when the panel was not made key. Global monitors ignore events sent to this app, so clicks in the panel and its popover are unaffected; the event is discarded and the monitor is removed on close. Add the dialog guards for recursive close and document the monitor in ADR-009, the architecture, the privacy table and the user guide.\n\nVerified: make verify green; inside clicks keep the panel open; outside clicks close it with closeWithEscape on and off; an outside click while the language popover is open closes both. Replace still works.
147 lines
5.1 KiB
Swift
147 lines
5.1 KiB
Swift
import AppKit
|
|
import SprachmittlerCore
|
|
import SwiftUI
|
|
|
|
/// The non-activating floating panel. It never activates the app. While visible it can be the key
|
|
/// window so Esc and the keyboard shortcuts work without a click.
|
|
@MainActor
|
|
final class PanelController: NSObject, NSWindowDelegate {
|
|
private let panel: TranslationPanel
|
|
private let viewModel: PanelViewModel
|
|
private var isClosing = false
|
|
private var outsideClickMonitor: Any?
|
|
|
|
/// Creates the panel and its SwiftUI content.
|
|
init(viewModel: PanelViewModel) {
|
|
self.viewModel = viewModel
|
|
panel = TranslationPanel(
|
|
contentRect: NSRect(x: 0, y: 0, width: 540, height: 520),
|
|
styleMask: [.titled, .fullSizeContentView, .nonactivatingPanel],
|
|
backing: .buffered,
|
|
defer: false
|
|
)
|
|
super.init()
|
|
panel.delegate = self
|
|
NotificationCenter.default.addObserver(
|
|
self,
|
|
selector: #selector(panelDidResignKey(_:)),
|
|
name: NSWindow.didResignKeyNotification,
|
|
object: panel
|
|
)
|
|
panel.isFloatingPanel = true
|
|
panel.isMovable = true
|
|
panel.becomesKeyOnlyIfNeeded = false
|
|
panel.level = .floating
|
|
panel.hidesOnDeactivate = false
|
|
panel.isMovableByWindowBackground = true
|
|
panel.isReleasedWhenClosed = false
|
|
panel.titleVisibility = .hidden
|
|
panel.titlebarAppearsTransparent = true
|
|
panel.backgroundColor = .clear
|
|
panel.isOpaque = false
|
|
panel.hasShadow = true
|
|
panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
|
|
panel.standardWindowButton(.closeButton)?.isHidden = true
|
|
panel.standardWindowButton(.miniaturizeButton)?.isHidden = true
|
|
panel.standardWindowButton(.zoomButton)?.isHidden = true
|
|
panel.contentView = PanelHostingView(
|
|
rootView: PanelView(
|
|
model: viewModel,
|
|
onClose: { [weak self] in self?.close() }
|
|
)
|
|
)
|
|
panel.setContentSize(NSSize(width: 540, height: 520))
|
|
}
|
|
|
|
/// Shows the panel near the mouse without activating the app.
|
|
func show() {
|
|
if !panel.isVisible {
|
|
positionNearMouse()
|
|
}
|
|
panel.orderFrontRegardless()
|
|
if viewModel.configuration.closeWithEscape {
|
|
panel.makeKey()
|
|
}
|
|
installOutsideClickMonitor()
|
|
}
|
|
|
|
/// Hides the panel and cancels/releases its content.
|
|
func close() {
|
|
guard !isClosing else { return }
|
|
isClosing = true
|
|
defer { isClosing = false }
|
|
removeOutsideClickMonitor()
|
|
panel.orderOut(nil)
|
|
Task { await viewModel.close() }
|
|
}
|
|
|
|
/// A click outside the panel makes another window key. Treat that as a dismissal, but not
|
|
/// while the language popover (a child window) is open.
|
|
@objc private func panelDidResignKey(_ notification: Notification) {
|
|
guard panel.isVisible, !isClosing else { return }
|
|
if panel.childWindows?.contains(where: \.isVisible) == true {
|
|
return
|
|
}
|
|
close()
|
|
}
|
|
|
|
/// While the panel is visible, a click sent to another app dismisses it. Global monitors do not
|
|
/// receive events sent to this app, so clicks in the panel and its popover are ignored. The
|
|
/// event itself is never read.
|
|
private func installOutsideClickMonitor() {
|
|
guard outsideClickMonitor == nil else { return }
|
|
outsideClickMonitor = NSEvent.addGlobalMonitorForEvents(
|
|
matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown]
|
|
) { [weak self] _ in
|
|
Task { @MainActor [weak self] in
|
|
guard let self, panel.isVisible else { return }
|
|
close()
|
|
}
|
|
}
|
|
}
|
|
|
|
private func removeOutsideClickMonitor() {
|
|
if let outsideClickMonitor {
|
|
NSEvent.removeMonitor(outsideClickMonitor)
|
|
self.outsideClickMonitor = nil
|
|
}
|
|
}
|
|
|
|
deinit {
|
|
NotificationCenter.default.removeObserver(self)
|
|
}
|
|
|
|
func windowWillClose(_ notification: Notification) {
|
|
removeOutsideClickMonitor()
|
|
Task { await viewModel.close() }
|
|
}
|
|
|
|
private final class PanelHostingView: NSHostingView<PanelView> {
|
|
required init(rootView: PanelView) {
|
|
super.init(rootView: rootView)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
nil
|
|
}
|
|
|
|
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
|
|
true
|
|
}
|
|
}
|
|
|
|
private func positionNearMouse() {
|
|
let mouse = NSEvent.mouseLocation
|
|
let screen = NSScreen.screens.first { $0.frame.contains(mouse) } ?? NSScreen.main
|
|
guard let visible = screen?.visibleFrame else {
|
|
panel.setFrameOrigin(mouse)
|
|
return
|
|
}
|
|
let size = panel.frame.size
|
|
var origin = NSPoint(x: mouse.x - size.width / 2, y: mouse.y + 16)
|
|
origin.x = max(visible.minX + 12, min(origin.x, visible.maxX - size.width - 12))
|
|
origin.y = max(visible.minY + 12, min(origin.y, visible.maxY - size.height - 12))
|
|
panel.setFrameOrigin(origin)
|
|
}
|
|
}
|