diff --git a/Easydict/Swift/Feature/Configuration/Configuration.swift b/Easydict/Swift/Feature/Configuration/Configuration.swift index 761453500..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 { [weak self] _ in - self?.updateWindowTitlebar() - } - .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) } } @@ -576,11 +577,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 210105929..c318b80e1 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; @@ -63,19 +59,11 @@ - (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) { [subview removeFromSuperview]; } - - // Reset buttons to update toolTip. - _quickActionMenu = nil; - _quickActionButton = nil; - _googleButton = nil; - _eudicButton = nil; - _appleDictionaryButton = nil; _stackView = nil; [self addSubview:self.pinButton]; @@ -101,28 +89,26 @@ - (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 *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 @@ -276,52 +262,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; } @@ -339,8 +300,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 = @""; 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 d54cf1b25..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:^{ @@ -451,10 +455,10 @@ - (void)updateFloatingWindowType:(EZWindowType)floatingWindowType isShowing:(BOO // NSLog(@"after floatingWindowTypeArray: %@", self.floatingWindowTypeArray); } -- (void)updateWindowsTitlebar { - [_mainWindow.titleBar updateButtonsToolTip]; - [self.fixedWindow.titleBar updateButtonsToolTip]; - [self.miniWindow.titleBar updateButtonsToolTip]; +- (void)updateWindowsTitlebarButtonsToolTip { + [_mainWindow.titleBar updateShortcutButtonsToolTip]; + [_miniWindow.titleBar updateShortcutButtonsToolTip]; + [_fixedWindow.titleBar updateShortcutButtonsToolTip]; } - (NSScreen *)getMouseLocatedScreen {