From d05bc54bb8af6423a8a9cc71801c5cd12fa005d5 Mon Sep 17 00:00:00 2001 From: phlpsong <103433299+phlpsong@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:25:37 +0800 Subject: [PATCH] bugfix: settings window display issue (#410) * fix: settings window display issue * perf: activate last frontmost app when closing query window if settings window is not showing --------- Co-authored-by: tisfeng --- .../Window/WindowManager/EZWindowManager.m | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m b/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m index 7eb580313..aba0cecb8 100644 --- a/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m +++ b/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m @@ -408,10 +408,7 @@ - (void)showFloatingWindow:(EZBaseQueryWindow *)window atPoint:(CGPoint)point { return; } - EZPreferencesWindowController *preferencesWindowController = [EZPreferencesWindowController shared]; - if (preferencesWindowController.isShowing) { - [preferencesWindowController.window close]; - } + [[self currentShowingSettingsWindow] close]; // get safe window position CGPoint safeLocation = [EZCoordinateUtils getFrameSafePoint:window.frame moveToPoint:point inScreen:self.screen]; @@ -437,6 +434,22 @@ - (void)showFloatingWindow:(EZBaseQueryWindow *)window atPoint:(CGPoint)point { [self updateFloatingWindowType:window.windowType]; } +- (nullable NSWindow *)currentShowingSettingsWindow { + EZPreferencesWindowController *preferencesWindowController = [EZPreferencesWindowController shared]; + if (preferencesWindowController.isShowing) { + return preferencesWindowController.window; + } + + // Workaround for SwiftUI Settings window, fix https://github.com/tisfeng/Easydict/issues/362 + for (NSWindow *window in [NSApp windows]) { + if ([window.identifier isEqualToString:@"com_apple_SwiftUI_Settings_window"] && window.visible) { + return window; + } + } + + return nil; +} + - (void)updateFloatingWindowType:(EZWindowType)floatingWindowType { NSNumber *windowType = @(floatingWindowType); [self.floatingWindowTypeArray removeObject:windowType]; @@ -904,7 +917,7 @@ - (void)closeFloatingWindow { self.floatingWindow.titleBar.pin = NO; [self.floatingWindow close]; - if (![EZPreferencesWindowController.shared isShowing]) { + if (![self currentShowingSettingsWindow]) { // recover last app. [self activeLastFrontmostApplication]; }