From a776ccc4f15bb0a0f0e4d5bc8e4f5686d7788d7d Mon Sep 17 00:00:00 2001 From: Sharker <1548742234@qq.com> Date: Sun, 14 Apr 2024 00:01:49 +0800 Subject: [PATCH 1/6] fix: launch app called windowDidResize --- .../ViewController/Window/WindowManager/EZWindowManager.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m index d54cf1b25..9228b5f13 100644 --- a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m +++ b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m @@ -453,8 +453,8 @@ - (void)updateFloatingWindowType:(EZWindowType)floatingWindowType isShowing:(BOO - (void)updateWindowsTitlebar { [_mainWindow.titleBar updateButtonsToolTip]; - [self.fixedWindow.titleBar updateButtonsToolTip]; - [self.miniWindow.titleBar updateButtonsToolTip]; + [_fixedWindow.titleBar updateButtonsToolTip]; + [_miniWindow.titleBar updateButtonsToolTip]; } - (NSScreen *)getMouseLocatedScreen { From 305f4f5586bff19419c20c3bcbfcdc0170fa77ec Mon Sep 17 00:00:00 2001 From: Sharker <1548742234@qq.com> Date: Sun, 14 Apr 2024 00:37:02 +0800 Subject: [PATCH 2/6] fix: closeFloatingWindowIfNotPinned called init widnow --- .../objc/ViewController/Window/WindowManager/EZWindowManager.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m index 9228b5f13..8fa11506f 100644 --- a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m +++ b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m @@ -921,6 +921,9 @@ - (void)closeFloatingWindowIfNotPinnedOrMain:(EZWindowType)windowType { } - (void)closeFloatingWindowIfNotPinned:(EZWindowType)windowType exceptWindowType:(EZWindowType)exceptWindowType { + if (_fixedWindow == nil && _miniWindow == nil) { + return; + } EZBaseQueryWindow *window = [self windowWithType:windowType]; if (!window.isPin && windowType != exceptWindowType) { [self closeFloatingWindow:windowType]; From 2c944b2ba92916812fd55a0a0b27ab6fca004320 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Mon, 15 Apr 2024 16:42:11 +0800 Subject: [PATCH 3/6] refactor: improve creating shortcut action buttons --- .../ViewController/View/Titlebar/EZTitlebar.m | 128 +++++++++++------- 1 file changed, 76 insertions(+), 52 deletions(-) diff --git a/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m b/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m index 9f5b6bf21..1936339be 100644 --- a/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m +++ b/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m @@ -63,7 +63,6 @@ - (void)setup { [defaultCenter addObserver:self selector:@selector(updateConstraints) name:NSNotification.languagePreferenceChanged object:nil]; } - - (void)updateConstraints { // Remove and dealloc all views to refresh UI. for (NSView *subview in self.subviews) { @@ -101,23 +100,8 @@ - (void)updateConstraints { [self.stackView addArrangedSubview:self.quickActionButton]; } - // Google - if (Configuration.shared.showGoogleQuickLink) { - [self.stackView addArrangedSubview:self.googleButton]; - } - - // Apple Dictionary - if (Configuration.shared.showAppleDictionaryQuickLink) { - [self.stackView addArrangedSubview:self.appleDictionaryButton]; - } - - // Eudic - if (Configuration.shared.showEudicQuickLink) { - // !!!: Note that some applications have multiple channel versions. Refer: https://github.com/tisfeng/Raycast-Easydict/issues/16 - BOOL installedEudic = [self checkInstalledApp:@[ @"com.eusoft.freeeudic", @"com.eusoft.eudic" ]]; - if (installedEudic) { - [self.stackView addArrangedSubview:self.eudicButton]; - } + for (NSNumber *buttonType in [self shortcutButtonTypes]) { + [self.stackView addArrangedSubview:[self buttonWithType:buttonType.integerValue]]; } [super updateConstraints]; @@ -277,52 +261,27 @@ - (EZOpenLinkButton *)quickActionButton { - (EZOpenLinkButton *)googleButton { if (!_googleButton) { - EZOpenLinkButton *googleButton = [[EZOpenLinkButton alloc] init]; - _googleButton = googleButton; - - googleButton.link = EZGoogleWebSearchURL; - googleButton.image = [[NSImage imageNamed:@"google_icon"] resizeToSize:self.imageSize]; - googleButton.toolTip = [self toolTipStrWithButtonType:EZTitlebarButtonTypeGoogle]; - googleButton.contentTintColor = NSColor.clearColor; - - [googleButton mas_remakeConstraints:^(MASConstraintMaker *make) { - make.size.mas_equalTo(self.buttonSize); - }]; + _googleButton = [self createButtonWithLink:EZGoogleWebSearchURL + imageName:@"google_icon" + buttonType:EZTitlebarButtonTypeGoogle]; } return _googleButton; } - (EZOpenLinkButton *)appleDictionaryButton { if (!_appleDictionaryButton) { - EZOpenLinkButton *appleDictButton = [[EZOpenLinkButton alloc] init]; - _appleDictionaryButton = appleDictButton; - - appleDictButton.link = EZAppleDictionaryAppURLScheme; - appleDictButton.image = [[NSImage imageNamed:EZServiceTypeAppleDictionary] resizeToSize:self.imageSize]; - appleDictButton.toolTip = [self toolTipStrWithButtonType:EZTitlebarButtonTypeAppleDic]; - appleDictButton.contentTintColor = NSColor.clearColor; - - [appleDictButton mas_remakeConstraints:^(MASConstraintMaker *make) { - make.size.mas_equalTo(self.buttonSize); - }]; - + _appleDictionaryButton = [self createButtonWithLink:EZAppleDictionaryAppURLScheme + imageName:EZServiceTypeAppleDictionary + buttonType:EZTitlebarButtonTypeAppleDic]; } return _appleDictionaryButton; } - (EZOpenLinkButton *)eudicButton { if (!_eudicButton) { - EZOpenLinkButton *eudicButton = [[EZOpenLinkButton alloc] init]; - _eudicButton = eudicButton; - - eudicButton.link = EZEudicAppURLScheme; - eudicButton.image = [[NSImage imageNamed:@"Eudic"] resizeToSize:self.imageSize]; - eudicButton.toolTip = [self toolTipStrWithButtonType:EZTitlebarButtonTypeEudicDic]; - eudicButton.contentTintColor = NSColor.clearColor; - - [eudicButton mas_remakeConstraints:^(MASConstraintMaker *make) { - make.size.mas_equalTo(self.buttonSize); - }]; + _eudicButton = [self createButtonWithLink:EZEudicAppURLScheme + imageName:@"Eudic" + buttonType:EZTitlebarButtonTypeEudicDic]; } return _eudicButton; } @@ -340,8 +299,73 @@ - (void)setPin:(BOOL)pin { [self updatePinButton]; } +- (NSArray *)shortcutButtonTypes { + NSMutableArray *shortcutButtonTypes = [NSMutableArray array]; + + // Google + if (Configuration.shared.showGoogleQuickLink) { + [shortcutButtonTypes addObject:@(EZTitlebarButtonTypeGoogle)]; + } + + // Apple Dictionary + if (Configuration.shared.showAppleDictionaryQuickLink) { + [shortcutButtonTypes addObject:@(EZTitlebarButtonTypeAppleDic)]; + } + + // Eudic + if (Configuration.shared.showEudicQuickLink) { + // !!!: Note that some applications have multiple channel versions. Refer: https://github.com/tisfeng/Raycast-Easydict/issues/16 + BOOL installedEudic = [self checkInstalledApp:@[ @"com.eusoft.freeeudic", @"com.eusoft.eudic" ]]; + if (installedEudic) { + [shortcutButtonTypes addObject:@(EZTitlebarButtonTypeEudicDic)]; + } + } + + return shortcutButtonTypes.copy; +} + #pragma mark - +- (EZOpenLinkButton *)createButtonWithLink:(NSString *)link + imageName:(NSString *)imageName + buttonType:(EZTitlebarButtonType)buttonType +{ + EZOpenLinkButton *button = [[EZOpenLinkButton alloc] init]; + button.link = link; + button.image = [[NSImage imageNamed:imageName] resizeToSize:self.imageSize]; + button.toolTip = [self toolTipStrWithButtonType:buttonType]; + button.contentTintColor = NSColor.clearColor; + + [button mas_remakeConstraints:^(MASConstraintMaker *make) { + make.size.mas_equalTo(self.buttonSize); + }]; + + return button; +} + +- (EZOpenLinkButton *)buttonWithType:(EZTitlebarButtonType)buttonType { + EZOpenLinkButton *button; + switch (buttonType) { + case EZTitlebarButtonTypeGoogle: { + button = self.googleButton; + break; + } + case EZTitlebarButtonTypeAppleDic: { + button = self.appleDictionaryButton; + break; + } + case EZTitlebarButtonTypeEudicDic: { + button = self.eudicButton; + break; + } + default: + break; + } + + return button; +} + + - (NSString *)toolTipStrWithButtonType:(EZTitlebarButtonType)type { NSString *toolTipStr = @""; NSString *shortcutStr = @""; From 526519053608ee48692689381982e8701ab849b5 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Mon, 15 Apr 2024 17:35:45 +0800 Subject: [PATCH 4/6] perf: only update shortcut buttons toolTip when changing shortcut keys --- .../Feature/Configuration/Configuration.swift | 9 ++---- .../ViewController/View/Titlebar/EZTitlebar.h | 3 +- .../ViewController/View/Titlebar/EZTitlebar.m | 30 ++++++++++--------- .../BaseQueryWindow/EZBaseQueryWindow.m | 2 +- .../Window/WindowManager/EZWindowManager.h | 2 +- .../Window/WindowManager/EZWindowManager.m | 11 +++---- 6 files changed, 25 insertions(+), 32 deletions(-) diff --git a/Easydict/Swift/Feature/Configuration/Configuration.swift b/Easydict/Swift/Feature/Configuration/Configuration.swift index 761453500..6bd014b49 100644 --- a/Easydict/Swift/Feature/Configuration/Configuration.swift +++ b/Easydict/Swift/Feature/Configuration/Configuration.swift @@ -402,8 +402,8 @@ class Configuration: NSObject { for key in shortcutKeys { Defaults.publisher(key) .removeDuplicates() - .sink { [weak self] _ in - self?.updateWindowTitlebar() + .sink { _ in + EZWindowManager.shared().updateWindowsTitlebarButtonsToolTip() } .store(in: &cancellables) } @@ -576,11 +576,6 @@ extension Configuration { fileprivate func didSetAppearance(_ appearance: AppearenceType) { DarkModeManager.sharedManager().updateDarkMode(appearance.rawValue) } - - fileprivate func updateWindowTitlebar() { - let windowManager = EZWindowManager.shared() - windowManager.updateWindowsTitlebar() - } } // MARK: Window Frame diff --git a/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.h b/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.h index 5c5db88a7..563a5a3de 100644 --- a/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.h +++ b/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.h @@ -33,8 +33,7 @@ typedef void(^EZTitlebarQuickActionBlock)(EZTitlebarQuickAction); @property (nonatomic, copy) EZTitlebarQuickActionBlock menuActionBlock; - -- (void)updateButtonsToolTip; +- (void)updateShortcutButtonsToolTip; @end diff --git a/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m b/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m index 1936339be..5ba48e9e3 100644 --- a/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m +++ b/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m @@ -46,10 +46,6 @@ - (instancetype)initWithFrame:(NSRect)frameRect { return self; } -- (void)updateButtonsToolTip { - [self updateConstraints]; -} - - (void)setup { self.buttonWidth = 24; self.imageWidth = 20; @@ -68,13 +64,6 @@ - (void)updateConstraints { for (NSView *subview in self.subviews) { [subview removeFromSuperview]; } - - // Reset buttons to update toolTip. - _quickActionMenu = nil; - _quickActionButton = nil; - _googleButton = nil; - _eudicButton = nil; - _appleDictionaryButton = nil; _stackView = nil; [self addSubview:self.pinButton]; @@ -100,13 +89,26 @@ - (void)updateConstraints { [self.stackView addArrangedSubview:self.quickActionButton]; } - for (NSNumber *buttonType in [self shortcutButtonTypes]) { - [self.stackView addArrangedSubview:[self buttonWithType:buttonType.integerValue]]; + for (NSNumber *typeNumber in [self shortcutButtonTypes]) { + EZTitlebarButtonType buttonType = typeNumber.integerValue; + EZOpenLinkButton *button = [self buttonWithType:buttonType]; + [self.stackView addArrangedSubview:button]; } + [self updateShortcutButtonsToolTip]; [super updateConstraints]; } +#pragma mark - Public Methods + +- (void)updateShortcutButtonsToolTip { + for (NSNumber *typeNumber in [self shortcutButtonTypes]) { + EZTitlebarButtonType buttonType = typeNumber.integerValue; + EZOpenLinkButton *button = [self buttonWithType:buttonType]; + button.toolTip = [self toolTipStrWithButtonType:buttonType]; + } +} + #pragma mark - Actions @@ -299,7 +301,7 @@ - (void)setPin:(BOOL)pin { [self updatePinButton]; } -- (NSArray *)shortcutButtonTypes { +- (NSArray *)shortcutButtonTypes { NSMutableArray *shortcutButtonTypes = [NSMutableArray array]; // Google diff --git a/Easydict/objc/ViewController/Window/BaseQueryWindow/EZBaseQueryWindow.m b/Easydict/objc/ViewController/Window/BaseQueryWindow/EZBaseQueryWindow.m index 35613f5e6..39da722b1 100644 --- a/Easydict/objc/ViewController/Window/BaseQueryWindow/EZBaseQueryWindow.m +++ b/Easydict/objc/ViewController/Window/BaseQueryWindow/EZBaseQueryWindow.m @@ -128,7 +128,7 @@ - (void)windowDidResignKey:(NSNotification *)notification { } - (void)windowDidResize:(NSNotification *)aNotification { -// NSLog(@"windowDidResize: %@, windowType: %ld", @(self.frame), self.windowType); + NSLog(@"windowDidResize: %@, windowType: %ld", @(self.frame), self.windowType); [[EZLayoutManager shared] updateWindowFrame:self]; diff --git a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.h b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.h index 7da74dc54..59100cbb3 100644 --- a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.h +++ b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.h @@ -94,7 +94,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)updateFloatingWindowType:(EZWindowType)floatingWindowType isShowing:(BOOL)isShowing; -- (void)updateWindowsTitlebar; +- (void)updateWindowsTitlebarButtonsToolTip; @end diff --git a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m index 8fa11506f..237f9a408 100644 --- a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m +++ b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m @@ -451,10 +451,10 @@ - (void)updateFloatingWindowType:(EZWindowType)floatingWindowType isShowing:(BOO // NSLog(@"after floatingWindowTypeArray: %@", self.floatingWindowTypeArray); } -- (void)updateWindowsTitlebar { - [_mainWindow.titleBar updateButtonsToolTip]; - [_fixedWindow.titleBar updateButtonsToolTip]; - [_miniWindow.titleBar updateButtonsToolTip]; +- (void)updateWindowsTitlebarButtonsToolTip { + [_mainWindow.titleBar updateShortcutButtonsToolTip]; + [_miniWindow.titleBar updateShortcutButtonsToolTip]; + [_fixedWindow.titleBar updateShortcutButtonsToolTip]; } - (NSScreen *)getMouseLocatedScreen { @@ -921,9 +921,6 @@ - (void)closeFloatingWindowIfNotPinnedOrMain:(EZWindowType)windowType { } - (void)closeFloatingWindowIfNotPinned:(EZWindowType)windowType exceptWindowType:(EZWindowType)exceptWindowType { - if (_fixedWindow == nil && _miniWindow == nil) { - return; - } EZBaseQueryWindow *window = [self windowWithType:windowType]; if (!window.isPin && windowType != exceptWindowType) { [self closeFloatingWindow:windowType]; From 481191cfafadef65656c4df3a81c6aa27c22e38f Mon Sep 17 00:00:00 2001 From: tisfeng Date: Mon, 15 Apr 2024 17:40:38 +0800 Subject: [PATCH 5/6] fix: do not close floating mini window if not created --- .../ViewController/Window/WindowManager/EZWindowManager.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m index 237f9a408..78aea9afb 100644 --- a/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m +++ b/Easydict/objc/ViewController/Window/WindowManager/EZWindowManager.m @@ -127,8 +127,12 @@ - (void)setupEventMonitor { [self.eventMonitor setDismissAllNotPinndFloatingWindowBlock:^{ mm_strongify(self); - [self closeFloatingWindowIfNotPinnedOrMain:EZWindowTypeMini]; - [self closeFloatingWindowIfNotPinnedOrMain:EZWindowTypeFixed]; + if (self->_miniWindow) { + [self closeFloatingWindowIfNotPinnedOrMain:EZWindowTypeMini]; + } + if (self->_fixedWindow) { + [self closeFloatingWindowIfNotPinnedOrMain:EZWindowTypeFixed]; + } }]; [self.eventMonitor setDoubleCommandBlock:^{ From 57f17fee041fc1f98e756a49150e0b75ca1a936f Mon Sep 17 00:00:00 2001 From: tisfeng Date: Tue, 16 Apr 2024 11:41:33 +0800 Subject: [PATCH 6/6] perf: improve code with publisher keys and throttle --- .../Feature/Configuration/Configuration.swift | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Easydict/Swift/Feature/Configuration/Configuration.swift b/Easydict/Swift/Feature/Configuration/Configuration.swift index 6bd014b49..072d231c3 100644 --- a/Easydict/Swift/Feature/Configuration/Configuration.swift +++ b/Easydict/Swift/Feature/Configuration/Configuration.swift @@ -393,20 +393,21 @@ class Configuration: NSObject { } .store(in: &cancellables) - let shortcutKeys: [Defaults.Key] = [ - .pinShortcut, - .appleDictionaryShortcut, - .googleShortcut, - .eudicShortcut, - ] - for key in shortcutKeys { - Defaults.publisher(key) - .removeDuplicates() - .sink { _ in - EZWindowManager.shared().updateWindowsTitlebarButtonsToolTip() - } - .store(in: &cancellables) + Defaults.publisher( + keys: + [ + .pinShortcut, + .appleDictionaryShortcut, + .googleShortcut, + .eudicShortcut, + ], + options: [] + ) + .throttle(for: 0.5, scheduler: DispatchQueue.main, latest: true) + .sink { _ in + EZWindowManager.shared().updateWindowsTitlebarButtonsToolTip() } + .store(in: &cancellables) } }