From 7d7c8d4e026267ada2bf03749027a9951626413b Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 12 Apr 2024 18:21:12 +0800 Subject: [PATCH] perf: improve closing floating window --- Easydict/objc/EventMonitor/EZEventMonitor.h | 3 +-- Easydict/objc/EventMonitor/EZEventMonitor.m | 15 ++---------- .../BaseQueryWindow/EZBaseQueryWindow.m | 4 ++-- .../Window/WindowManager/EZWindowManager.m | 24 +++++++++---------- 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/Easydict/objc/EventMonitor/EZEventMonitor.h b/Easydict/objc/EventMonitor/EZEventMonitor.h index 32c13a379..7031e005a 100644 --- a/Easydict/objc/EventMonitor/EZEventMonitor.h +++ b/Easydict/objc/EventMonitor/EZEventMonitor.h @@ -29,8 +29,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, copy) void (^selectedTextBlock)(NSString *selectedText); @property (nonatomic, copy) void (^dismissPopButtonBlock)(void); -@property (nonatomic, copy) void (^dismissMiniWindowBlock)(void); -@property (nonatomic, copy) void (^dismissFixedWindowBlock)(void); +@property (nonatomic, copy) void (^dismissAllNotPinndFloatingWindowBlock)(void); @property (nonatomic, copy) void (^doubleCommandBlock)(void); @property (nonatomic, copy) void (^leftMouseDownBlock)(CGPoint clickPoint); @property (nonatomic, copy) void (^rightMouseDownBlock)(CGPoint clickPoint); diff --git a/Easydict/objc/EventMonitor/EZEventMonitor.m b/Easydict/objc/EventMonitor/EZEventMonitor.m index 4246cd10b..efeaa298b 100644 --- a/Easydict/objc/EventMonitor/EZEventMonitor.m +++ b/Easydict/objc/EventMonitor/EZEventMonitor.m @@ -829,19 +829,8 @@ - (void)handleMonitorEvent:(NSEvent *)event { - (void)dismissWindowsIfMouseLocationOutsideFloatingWindow { - EZWindowManager *windowManager = EZWindowManager.shared; - if (windowManager.floatingWindowType == EZWindowTypeMini) { - BOOL outsideMiniWindow = ![self checkIfMouseLocationInWindow:windowManager.miniWindow]; - if (outsideMiniWindow && self.dismissMiniWindowBlock) { - self.dismissMiniWindowBlock(); - } - } else { - if (windowManager.floatingWindowType == EZWindowTypeFixed) { - BOOL outsideFixedWindow = ![self checkIfMouseLocationInWindow:windowManager.fixedWindow]; - if (outsideFixedWindow && self.dismissFixedWindowBlock) { - self.dismissFixedWindowBlock(); - } - } + if (![self checkIfMouseLocationInWindow:EZWindowManager.shared.floatingWindow]) { + self.dismissAllNotPinndFloatingWindowBlock(); } } diff --git a/Easydict/objc/ViewController/Window/BaseQueryWindow/EZBaseQueryWindow.m b/Easydict/objc/ViewController/Window/BaseQueryWindow/EZBaseQueryWindow.m index 5f7f97e71..35613f5e6 100644 --- a/Easydict/objc/ViewController/Window/BaseQueryWindow/EZBaseQueryWindow.m +++ b/Easydict/objc/ViewController/Window/BaseQueryWindow/EZBaseQueryWindow.m @@ -128,8 +128,8 @@ - (void)windowDidResignKey:(NSNotification *)notification { } - (void)windowDidResize:(NSNotification *)aNotification { - // NSLog(@"MainWindow 窗口拉伸, (%.2f, %.2f)", self.width, self.height); - +// NSLog(@"windowDidResize: %@, windowType: %ld", @(self.frame), self.windowType); + [[EZLayoutManager shared] updateWindowFrame:self]; if (self.resizeWindowBlock) { diff --git a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m index 0b36eabaf..d54cf1b25 100644 --- a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m +++ b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m @@ -125,14 +125,10 @@ - (void)setupEventMonitor { [self.popButtonWindow close]; }]; - [self.eventMonitor setDismissMiniWindowBlock:^{ + [self.eventMonitor setDismissAllNotPinndFloatingWindowBlock:^{ mm_strongify(self); - [self closeFloatingWindowIfNotPinned]; - }]; - - [self.eventMonitor setDismissFixedWindowBlock:^{ - mm_strongify(self); - [self closeFloatingWindowIfNotPinned]; + [self closeFloatingWindowIfNotPinnedOrMain:EZWindowTypeMini]; + [self closeFloatingWindowIfNotPinnedOrMain:EZWindowTypeFixed]; }]; [self.eventMonitor setDoubleCommandBlock:^{ @@ -777,7 +773,7 @@ - (void)inputTranslate { EZWindowType windowType = Configuration.shared.shortcutSelectTranslateWindowType; - if (self.floatingWindowType == windowType) { + if (self.floatingWindowType == windowType && self.floatingWindow.isVisible) { [self closeFloatingWindow]; return; } @@ -797,7 +793,7 @@ - (void)showMiniFloatingWindow { EZWindowType windowType = Configuration.shared.mouseSelectTranslateWindowType; - if (self.floatingWindowType == windowType) { + if (self.floatingWindowType == windowType && self.floatingWindow.isVisible) { [self closeFloatingWindow]; return; } @@ -920,6 +916,10 @@ - (void)closeFloatingWindowIfNotPinned { [self closeFloatingWindowIfNotPinned:self.floatingWindowType exceptWindowType:EZWindowTypeNone]; } +- (void)closeFloatingWindowIfNotPinnedOrMain:(EZWindowType)windowType { + [self closeFloatingWindowIfNotPinned:windowType exceptWindowType:EZWindowTypeMain]; +} + - (void)closeFloatingWindowIfNotPinned:(EZWindowType)windowType exceptWindowType:(EZWindowType)exceptWindowType { EZBaseQueryWindow *window = [self windowWithType:windowType]; if (!window.isPin && windowType != exceptWindowType) { @@ -928,13 +928,13 @@ - (void)closeFloatingWindowIfNotPinned:(EZWindowType)windowType exceptWindowType } - (void)closeFloatingWindow:(EZWindowType)windowType { - NSLog(@"close window type: %ld", windowType); - EZBaseQueryWindow *floatingWindow = [self windowWithType:windowType]; - if (!floatingWindow) { + if (!floatingWindow || !floatingWindow.isVisible) { return; } + NSLog(@"close window type: %ld", windowType); + // Stop playing audio [floatingWindow.queryViewController stopPlayingQueryText]; floatingWindow.titleBar.pin = NO;