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 tool tips #500

Merged
merged 13 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Easydict/Swift/Feature/Configuration/Configuration+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,35 @@ class DefaultsWrapper<T: Defaults.Serializable> {
}
}

// MARK: - ShortcutWrapper

@propertyWrapper
class ShortcutWrapper<T: KeyCombo> {
// MARK: Lifecycle

init(_ key: Defaults.Key<T?>) {
self.key = key
}

// MARK: Internal

let key: Defaults.Key<T?>

var wrappedValue: String {
let keyCombo = Defaults[key]
var keyEquivalent = ""
var modifier = ""
if let key = keyCombo, key.doubledModifiers {
modifier = keyCombo?.keyEquivalentModifierMaskString ?? ""
keyEquivalent = keyCombo?.keyEquivalentModifierMaskString ?? ""
} else {
modifier = keyCombo?.keyEquivalentModifierMaskString ?? ""
keyEquivalent = keyCombo?.keyEquivalent ?? ""
}
return modifier + keyEquivalent
AkaShark marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Service Configuration
extension Defaults.Keys {
// OpenAI
Expand Down
56 changes: 56 additions & 0 deletions Easydict/Swift/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ class Configuration: NSObject {

var cancellables: [AnyCancellable] = []

@ShortcutWrapper(.pinShortcut) var pinShortcutString: String
@ShortcutWrapper(.googleShortcut) var googleShortcutString: String
@ShortcutWrapper(.appleDictionaryShortcut) var appleDictShortcutString: String
@ShortcutWrapper(.eudicShortcut) var eudicDictShortcutString: String

var automaticallyChecksForUpdates: Bool {
get {
updater.automaticallyChecksForUpdates
Expand Down Expand Up @@ -419,6 +424,37 @@ class Configuration: NSObject {
self?.didSetAppearance(newValue)
}
)
cancellables.append(
Defaults.publisher(.pinShortcut)
.removeDuplicates()
.sink { [weak self] _ in
self?.didSetPinShortcut()
}
)

cancellables.append(
Defaults.publisher(.appleDictionaryShortcut)
.removeDuplicates()
.sink { [weak self] _ in
self?.didSetAppleDictShortcut()
}
)

cancellables.append(
Defaults.publisher(.googleShortcut)
.removeDuplicates()
.sink { [weak self] _ in
self?.didSetGoogleShortcut()
}
)

cancellables.append(
Defaults.publisher(.eudicShortcut)
.removeDuplicates()
.sink { [weak self] _ in
self?.didSetEudicDictShortcut()
}
)
}
}

Expand Down Expand Up @@ -588,6 +624,26 @@ extension Configuration {
fileprivate func didSetAppearance(_ appearance: AppearenceType) {
DarkModeManager.sharedManager().updateDarkMode(appearance.rawValue)
}

fileprivate func didSetAppleDictShortcut() {
let windowManager = EZWindowManager.shared()
windowManager.updateWindowsTitlebar()
}

fileprivate func didSetPinShortcut() {
let windowManager = EZWindowManager.shared()
windowManager.updateWindowsTitlebar()
}

fileprivate func didSetGoogleShortcut() {
let windowManager = EZWindowManager.shared()
windowManager.updateWindowsTitlebar()
}

fileprivate func didSetEudicDictShortcut() {
let windowManager = EZWindowManager.shared()
windowManager.updateWindowsTitlebar()
}
phlpsong marked this conversation as resolved.
Show resolved Hide resolved
}

// MARK: Window Frame
Expand Down
15 changes: 0 additions & 15 deletions Easydict/Swift/Feature/Shortcut/Shortcut+Bind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,83 +8,68 @@

// App shortcut binding func
extension Shortcut {
@objc
func clearInput() {
EZWindowManager.shared().clearInput()
}

@objc
func clearAll() {
EZWindowManager.shared().clearAll()
}

@objc
func shortcutCopy() {
EZWindowManager.shared().copyQueryText()
}

@objc
func shortcutCopyFirstResult() {
EZWindowManager.shared().copyFirstTranslatedText()
}

@objc
func shortcutFocus() {
EZWindowManager.shared().focusInputTextView()
}

@objc
func shortcutPlay() {
EZWindowManager.shared().playOrStopQueryTextAudio()
}

@objc
func shortcutRetry() {
EZWindowManager.shared().rerty()
}

@objc
func shortcutToggle() {
EZWindowManager.shared().toggleTranslationLanguages()
}

@objc
func shortcutPin() {
EZWindowManager.shared().pin()
}

@objc
func shortcutHide() {
EZWindowManager.shared().closeWindowOrExitSreenshot()
}

@objc
func increaseFontSize() {
if Configuration.shared.fontSizeIndex < Configuration.shared.fontSizes.count - 1 {
Configuration.shared.fontSizeIndex += 1
}
}

@objc
func decreaseFontSize() {
if Configuration.shared.fontSizeIndex > 0 {
Configuration.shared.fontSizeIndex -= 1
}
}

@objc
func shortcutGoogle() {
let window = EZWindowManager.shared().floatingWindow
window?.titleBar.googleButton.openLink()
}

@objc
func shortcutEudic() {
let window = EZWindowManager.shared().floatingWindow
window?.titleBar.eudicButton.openLink()
}

@objc
func shortcutAppleDic() {
let window = EZWindowManager.shared().floatingWindow
window?.titleBar.appleDictionaryButton.openLink()
Expand Down
3 changes: 3 additions & 0 deletions Easydict/objc/ViewController/View/Titlebar/EZTitlebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ typedef void(^EZTitlebarQuickActionBlock)(EZTitlebarQuickAction);

@property (nonatomic, copy) EZTitlebarQuickActionBlock menuActionBlock;


- (void)updateButtonsToolTip;

@end

NS_ASSUME_NONNULL_END
41 changes: 35 additions & 6 deletions Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ - (instancetype)initWithFrame:(NSRect)frameRect {
return self;
}

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

- (void)setup {
self.buttonWidth = 24;
Expand Down Expand Up @@ -272,7 +275,14 @@ - (EZOpenLinkButton *)googleButton {

googleButton.link = EZGoogleWebSearchURL;
googleButton.image = [[NSImage imageNamed:@"google_icon"] resizeToSize:self.imageSize];
googleButton.toolTip = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"open_in_google", nil), @" ⌘+⏎"];
NSString *shortcutStr = Configuration.shared.googleShortcutString;
NSString *toolTipStr = @"";
if (shortcutStr.length != 0) {
toolTipStr = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"open_in_google", nil), shortcutStr];
} else {
toolTipStr = [NSString stringWithFormat:@"%@", NSLocalizedString(@"open_in_google", nil)];
}
googleButton.toolTip = toolTipStr;
googleButton.contentTintColor = NSColor.clearColor;

[googleButton mas_remakeConstraints:^(MASConstraintMaker *make) {
Expand All @@ -289,7 +299,14 @@ - (EZOpenLinkButton *)appleDictionaryButton {

appleDictButton.link = EZAppleDictionaryAppURLScheme;
appleDictButton.image = [[NSImage imageNamed:EZServiceTypeAppleDictionary] resizeToSize:self.imageSize];
appleDictButton.toolTip = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"open_in_apple_dictionary", nil), @"⌘+⇧+D"];
NSString *shortcutStr = Configuration.shared.appleDictShortcutString;
NSString *toolTipStr = @"";
if (shortcutStr.length != 0) {
toolTipStr = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"open_in_apple_dictionary", nil), shortcutStr];
} else {
toolTipStr = [NSString stringWithFormat:@"%@", NSLocalizedString(@"open_in_apple_dictionary", nil)];
}
appleDictButton.toolTip = toolTipStr;
appleDictButton.contentTintColor = NSColor.clearColor;

[appleDictButton mas_remakeConstraints:^(MASConstraintMaker *make) {
Expand All @@ -307,7 +324,14 @@ - (EZOpenLinkButton *)eudicButton {

eudicButton.link = EZEudicAppURLScheme;
eudicButton.image = [[NSImage imageNamed:@"Eudic"] resizeToSize:self.imageSize];
eudicButton.toolTip = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"open_in_eudic", nil), @"⌘+⇧+⏎"];
NSString *shortcutStr = Configuration.shared.eudicDictShortcutString;
NSString *toolTipStr = @"";
if (shortcutStr.length != 0) {
toolTipStr = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"open_in_eudic", nil), shortcutStr];
} else {
toolTipStr = [NSString stringWithFormat:@"%@", NSLocalizedString(@"open_in_eudic", nil)];
}
eudicButton.toolTip = toolTipStr;
tisfeng marked this conversation as resolved.
Show resolved Hide resolved
eudicButton.contentTintColor = NSColor.clearColor;

[eudicButton mas_remakeConstraints:^(MASConstraintMaker *make) {
Expand All @@ -334,10 +358,15 @@ - (void)setPin:(BOOL)pin {


- (void)updatePinButton {
NSString *shortcut = @"⌘+P";
NSString *shortcutStr = Configuration.shared.pinShortcutString;
NSString *action = self.pin ? NSLocalizedString(@"unpin", nil) : NSLocalizedString(@"pin", nil);
self.pinButton.toolTip = [NSString stringWithFormat:@"%@, %@", action, shortcut];

NSString *toolTipStr = @"";
if (shortcutStr.length != 0) {
toolTipStr = [NSString stringWithFormat:@"%@, %@", action, shortcutStr];
} else {
toolTipStr = [NSString stringWithFormat:@"%@", action];
}
self.pinButton.toolTip = toolTipStr;

CGFloat imageWidth = 18;
CGSize imageSize = CGSizeMake(imageWidth, imageWidth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ NS_ASSUME_NONNULL_BEGIN

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

- (void)updateWindowsTitlebar;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ - (void)updateFloatingWindowType:(EZWindowType)floatingWindowType isShowing:(BOO
// NSLog(@"after floatingWindowTypeArray: %@", self.floatingWindowTypeArray);
}

- (void)updateWindowsTitlebar {
[_mainWindow.titleBar updateButtonsToolTip];
[self.fixedWindow.titleBar updateButtonsToolTip];
[self.miniWindow.titleBar updateButtonsToolTip];
}

- (NSScreen *)getMouseLocatedScreen {
NSPoint mouseLocation = [NSEvent mouseLocation]; // ???: self.endPoint

Expand Down