Skip to content

Commit 709613b

Browse files
yusixianSSBunCSL
authored
fix(PopupPanel): restore focus to previous app after dismissing input panel (#55)
* fix(PopupPanel): restore focus to previous app after dismissing input panel (#53) When using the input translation mode (⌥A), the app activates itself to receive keyboard events. After dismissing the panel, focus was not being returned to the user's previous working app, interrupting the workflow. Fix by recording NSWorkspace.shared.frontmostApplication before activation and restoring focus to the previous app after panel dismissal. Co-authored-by: CSL <caishilin@zhihu.com> Co-authored-by: cos <i@cosine.ren> * fix(PopupPanel): use non-deprecated activate API and guard focus restore - Replace deprecated activate(options:) with activate() - Only restore focus when MoePeek is still frontmost, avoiding pulling focus back if user has already switched away manually - Clean up property grouping for readability --------- Co-authored-by: Silas <caishilin@yahoo.com> Co-authored-by: CSL <caishilin@zhihu.com>
1 parent 5db355e commit 709613b

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

Sources/UI/PopupPanel/PopupPanelController.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ final class PopupPanelController {
1414
private let ttsCoordinator: TTSCoordinator?
1515
private let settingsController: SettingsWindowController?
1616

17+
/// The app that was frontmost before we activated ourselves for input mode.
18+
private var previouslyActiveApp: NSRunningApplication?
19+
1720
init(
1821
coordinator: TranslationCoordinator,
1922
ttsCoordinator: TTSCoordinator? = nil,
@@ -63,6 +66,8 @@ final class PopupPanelController {
6366
// Input mode needs the app activated so the panel can receive keyboard events.
6467
// Without this, makeKeyAndOrderFront alone won't route keystrokes to our panel
6568
// because macOS keeps delivering them to the previously active app.
69+
// Remember the previously active app so we can restore focus after dismissal.
70+
previouslyActiveApp = NSWorkspace.shared.frontmostApplication
6671
NSApp.activate(ignoringOtherApps: true)
6772
panel.makeKeyAndOrderFront(nil)
6873

@@ -80,6 +85,12 @@ final class PopupPanelController {
8085
panel = nil
8186
coordinator.dismiss()
8287
onDismiss?()
88+
89+
// Restore focus only when MoePeek is still frontmost (user hasn't switched away manually).
90+
if let previousApp = previouslyActiveApp, NSRunningApplication.current.isActive {
91+
previousApp.activate()
92+
}
93+
previouslyActiveApp = nil
8394
}
8495

8596
var isVisible: Bool {

0 commit comments

Comments
 (0)