Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: launch app called windowDidResize #506

Merged
merged 10 commits into from
Apr 16, 2024
9 changes: 2 additions & 7 deletions Easydict/Swift/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
phlpsong marked this conversation as resolved.
Show resolved Hide resolved
}
.store(in: &cancellables)
}
Expand Down Expand Up @@ -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
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 @@ -277,52 +263,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 @@ -340,8 +301,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