Skip to content

Doubao-powered voice input for Linux, Wayland and Omarchy

fix: 清除原生窗口焦点并稳定键鼠回归检查

quanru2b541826d0de
diff --git a/macos/Sources/SettingsWindow.swift b/macos/Sources/SettingsWindow.swift
index 0b75e6f..b9f00c9 100644
--- a/macos/Sources/SettingsWindow.swift
+++ b/macos/Sources/SettingsWindow.swift
@@ -47,6 +47,7 @@ final class SettingsModel: ObservableObject {
var installUpdate: (() -> Void)?
var releaseNotes: (() -> Void)?
var languageChanged: (() -> Void)?
+ var clearNavigationFocus: (() -> Void)?
private var testASRClient: APIASRClient?
var cancelCapture: (() -> Void)?
private var testClient: PolishClient?
@@ -283,6 +284,7 @@ struct SettingsView: View {
// Mouse navigation should not leave a keyboard focus ring behind.
if let event = NSApp.currentEvent, [.leftMouseDown, .leftMouseUp].contains(event.type) {
sidebarFocus = nil
+ model.clearNavigationFocus?()
}
} label: {
Label(page.1, systemImage: page.2)
@@ -321,12 +323,11 @@ struct SettingsView: View {
}.padding(18)
}.background(Color(nsColor: .windowBackgroundColor))
}.frame(width: 900, height: 650)
- .onAppear {
- // Allow the initial focus assignment to finish before clearing it.
- DispatchQueue.main.async { sidebarFocus = nil }
- }
.onChange(of: model.selectedTab) { page in
- if sidebarFocus != page { sidebarFocus = nil }
+ if sidebarFocus != page {
+ sidebarFocus = nil
+ model.clearNavigationFocus?()
+ }
}
}
@ViewBuilder private var selectedPage: some View {
@@ -577,6 +578,7 @@ final class SettingsWindow: NSObject, NSWindowDelegate {
window.title = "Doubao Say · " + L("Settings", "设置")
window.isReleasedWhenClosed = false; window.delegate = self
window.contentView = NSHostingView(rootView: SettingsView(model: model, state: state))
+ model.clearNavigationFocus = { [weak window] in window?.makeFirstResponder(nil) }
window.center()
}
func show(refresh: Bool = true) {
diff --git a/macos/Tests/Settings/SidebarFocusCheck.swift b/macos/Tests/Settings/SidebarFocusCheck.swift
index 1bb4667..5c75877 100644
--- a/macos/Tests/Settings/SidebarFocusCheck.swift
+++ b/macos/Tests/Settings/SidebarFocusCheck.swift
@@ -9,7 +9,10 @@ import SwiftUI
private var settings: SettingsWindow?
private func key(_ native: NSWindow, _ code: UInt16, _ characters: String) {
for type: NSEvent.EventType in [.keyDown, .keyUp] {
- if let event = NSEvent.keyEvent(with: type, location: .zero, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: native.windowNumber, context: nil, characters: characters, charactersIgnoringModifiers: characters, isARepeat: false, keyCode: code) { NSApp.sendEvent(event) }
+ if let event = NSEvent.keyEvent(with: type, location: .zero, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: native.windowNumber, context: nil, characters: characters, charactersIgnoringModifiers: characters, isARepeat: false, keyCode: code) {
+ if type == .keyUp { DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { NSApp.postEvent(event, atStart: false) } }
+ else { NSApp.postEvent(event, atStart: false) }
+ }
}
}
func applicationDidFinishLaunching(_ notification: Notification) {
@@ -21,28 +24,41 @@ import SwiftUI
window.show(refresh: false)
let mode = ProcessInfo.processInfo.environment["DOUBAO_FOCUS_CASE"] ?? "opened"
let check = ProcessInfo.processInfo.environment["DOUBAO_FOCUS_ASSERT"] != "0"
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
guard let native = NSApp.windows.first else { fatalError("Missing settings window") }
NSApp.activate(ignoringOtherApps: true)
native.makeKeyAndOrderFront(nil)
precondition(NSApp.isFullKeyboardAccessEnabled, "This opt-in check requires keyboard navigation enabled")
- if mode == "external" { window.model.selectedTab = "updates" }
- if ["keyboard", "activate", "mouse", "reopen"].contains(mode) {
- self.key(native, 48, "\t")
- if mode == "activate" { self.key(native, 48, "\t"); self.key(native, 49, " ") }
- if mode == "mouse" {
- let target = NSPoint(x: 80, y: 650 - 128)
- for type: NSEvent.EventType in [.leftMouseDown, .leftMouseUp] {
- if let event = NSEvent.mouseEvent(with: type, location: target, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: native.windowNumber, context: nil, eventNumber: 0, clickCount: 1, pressure: 1) { NSApp.sendEvent(event) }
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
+ if mode == "external" {
+ self.key(native, 48, "\t")
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { window.model.selectedTab = "updates" }
+ }
+ if ["keyboard", "activate", "mouse", "reopen"].contains(mode) {
+ self.key(native, 48, "\t")
+ if mode == "activate" {
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { self.key(native, 48, "\t") }
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self.key(native, 49, " ") }
+ }
+ if mode == "mouse" {
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
+ let target = NSPoint(x: 80, y: 650 - 128)
+ for type: NSEvent.EventType in [.leftMouseDown, .leftMouseUp] {
+ if let event = NSEvent.mouseEvent(with: type, location: target, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: native.windowNumber, context: nil, eventNumber: 0, clickCount: 1, pressure: 1) { NSApp.postEvent(event, atStart: false) }
+ }
+ }
+ }
+ if mode == "reopen" {
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { window.hide(); window.show(refresh: false) }
}
}
- if mode == "reopen" { window.hide(); window.show(refresh: false) }
}
}
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
+ DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
guard let native = NSApp.windows.first else { fatalError("Missing settings window") }
let view = native.firstResponder as? NSView
print("FOCUS: \(String(describing: native.firstResponder)) FRAME: \(String(describing: view?.frame)) PAGE: \(window.model.selectedTab)")
+ fflush(stdout)
if check {
if ["opened", "external", "mouse", "reopen"].contains(mode) { precondition(view == nil, "Stale sidebar focus remains in \(mode)") }
if mode == "keyboard" { precondition(view != nil && window.model.selectedTab == "setup", "Tab must focus without activating") }