Skip to content

Commit

Permalink
fix: launch app called windowDidResize (#506)
Browse files Browse the repository at this point in the history
* fix: launch app called windowDidResize

* fix: closeFloatingWindowIfNotPinned called init widnow

* refactor: improve creating shortcut action buttons

* perf: only update shortcut buttons toolTip when changing shortcut keys

* fix: do not close floating mini window if not created

* perf: improve code with publisher keys and throttle

---------

Co-authored-by: Tisfeng <[email protected]>
  • Loading branch information
AkaShark and tisfeng authored Apr 16, 2024
1 parent 59a6890 commit f67b164
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 91 deletions.
32 changes: 14 additions & 18 deletions Easydict/Swift/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Easydict/objc/ViewController/View/Titlebar/EZTitlebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ typedef void(^EZTitlebarQuickActionBlock)(EZTitlebarQuickAction);

@property (nonatomic, copy) EZTitlebarQuickActionBlock menuActionBlock;


- (void)updateButtonsToolTip;
- (void)updateShortcutButtonsToolTip;

@end

Expand Down
152 changes: 89 additions & 63 deletions Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ - (instancetype)initWithFrame:(NSRect)frameRect {
return self;
}

- (void)updateButtonsToolTip {
[self updateConstraints];
}

- (void)setup {
self.buttonWidth = 24;
self.imageWidth = 20;
Expand All @@ -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];
Expand All @@ -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

Expand Down Expand Up @@ -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;
}
Expand All @@ -339,8 +300,73 @@ - (void)setPin:(BOOL)pin {
[self updatePinButton];
}

- (NSArray<NSNumber *> *)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 = @"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ NS_ASSUME_NONNULL_BEGIN

- (void)updateFloatingWindowType:(EZWindowType)floatingWindowType isShowing:(BOOL)isShowing;

- (void)updateWindowsTitlebar;
- (void)updateWindowsTitlebarButtonsToolTip;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:^{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f67b164

Please sign in to comment.