Files
Sprachmittler/App/Panel/PanelController.swift
T
schihei 40733473c5 fix(panel): draggable header and configurable translation text size
Add a native AppKit drag grip to the non-activating panel header; it accepts the first mouse and calls performDrag, so the panel can be moved before it becomes key. Keep a moved position while the panel stays visible and only recenter near the cursor when it is shown again (DeepL-like transient position).

Add persisted translationFontSize (default 17 pt, range 12–28) to AppSettings and PanelConfiguration, apply it to the translation text, and expose a native Settings stepper. Add English/German strings and AppSettings clamp/round-trip tests.

Verified: make verify green; synthetic double-copy opened the panel, dragging the grip moved the window, and a 22 pt capture showed the larger translation text.
2026-11-26 14:35:34 +01:00

93 lines
3.1 KiB
Swift

import AppKit
import SprachmittlerCore
import SwiftUI
/// The non-activating floating panel. It never activates the app or steals focus.
@MainActor
final class PanelController: NSObject, NSWindowDelegate {
private let panel: NSPanel
private let viewModel: PanelViewModel
/// Creates the panel and its SwiftUI content.
init(viewModel: PanelViewModel) {
self.viewModel = viewModel
panel = NSPanel(
contentRect: NSRect(x: 0, y: 0, width: 540, height: 520),
styleMask: [.titled, .fullSizeContentView, .nonactivatingPanel],
backing: .buffered,
defer: false
)
super.init()
panel.delegate = self
panel.isFloatingPanel = true
panel.isMovable = true
panel.becomesKeyOnlyIfNeeded = true
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()
}
/// Hides the panel and cancels/releases its content.
func close() {
panel.orderOut(nil)
Task { await viewModel.close() }
}
func windowWillClose(_ notification: Notification) {
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)
}
}