Files
Sprachmittler/App/Panel/PanelController.swift
T
schihei a78572adc3 feat(m4-m6): session, panel, Replace, settings and onboarding
M4: TranslationSession actor with epochs/one-in-flight streaming, PanelViewModel, non-activating NSPanel, streaming panel UI, searchable/recent target languages, warnings/errors and Copy/Retry/Replace actions.

M5: InsertionCoordinator with target capture, AX direct replacement, secure-field/own-PID refusal, frontmost and selected-text verification, snapshot/⌘V/restore fallback, cancellation-safe restore and blocking-warning gate.

M6: versioned AppSettings/SettingsStore, onboarding for Ollama/model/target/Accessibility, General/Model/Translation/Privacy/Diagnostics settings, launch-at-login via SMAppService, status menu (target, copy/replace last, setup, diagnostics, about), text-free diagnostics and first-run setup.

Docs: PROGRESS M4–M6, ARCHITECTURE §10–§12, ADR-019/020, SELECTION_AND_CLIPBOARD and TESTING. Manual 10-app matrices, real AX/paste delivery, VoiceOver and focus checks remain open.

Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, 217 package tests); app launches and a two-copy clipboard smoke test opens the panel.
2026-11-26 14:03:16 +01:00

76 lines
2.7 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.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 = NSHostingView(
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() {
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 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)
}
}