From 194b798e1da617d11d81956c39ae5543c9ec813a Mon Sep 17 00:00:00 2001 From: rainzm Date: Sun, 1 Oct 2023 01:05:18 +0800 Subject: [PATCH] feat: choose window type for shortcut and mouse select translate --- .../Feature/Configuration/EZConfiguration.h | 3 +- .../Feature/Configuration/EZConfiguration.m | 57 ++-- .../EZSettingViewController.m | 310 ++++++++++-------- Easydict/Feature/Service/Model/EZEnumTypes.h | 10 +- Easydict/Feature/Service/Model/EZEnumTypes.m | 26 +- .../Window/WindowManager/EZWindowManager.m | 248 +++++++------- en.lproj/Localizable.strings | 8 +- zh-Hans.lproj/Localizable.strings | 8 +- 8 files changed, 365 insertions(+), 305 deletions(-) diff --git a/Easydict/Feature/Configuration/EZConfiguration.h b/Easydict/Feature/Configuration/EZConfiguration.h index 366feab67..1a50e3371 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.h +++ b/Easydict/Feature/Configuration/EZConfiguration.h @@ -32,7 +32,6 @@ typedef NS_ENUM(NSUInteger, EZLanguageDetectOptimize) { @property (nonatomic, assign) BOOL autoSelectText; @property (nonatomic, assign) BOOL forceAutoGetSelectedText; -@property (nonatomic, assign) BOOL selectUseMiniWindow; @property (nonatomic, assign) BOOL disableEmptyCopyBeep; // Some apps will beep when empty copy. @property (nonatomic, assign) BOOL clickQuery; @property (nonatomic, assign) BOOL launchAtStartup; @@ -50,6 +49,8 @@ typedef NS_ENUM(NSUInteger, EZLanguageDetectOptimize) { @property (nonatomic, assign) BOOL showEudicQuickLink; @property (nonatomic, assign) BOOL hideMenuBarIcon; @property (nonatomic, assign) EZShowWindowPosition fixedWindowPosition; +@property (nonatomic, assign) EZWindowType shortcutSelectTranslateWindowType; +@property (nonatomic, assign) EZWindowType mouseSelectTranslateWindowType; @property (nonatomic, assign) BOOL adjustPopButtomOrigin; @property (nonatomic, assign) BOOL allowCrashLog; @property (nonatomic, assign) BOOL allowAnalytics; diff --git a/Easydict/Feature/Configuration/EZConfiguration.m b/Easydict/Feature/Configuration/EZConfiguration.m index 83e16aaae..e4a0487fa 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.m +++ b/Easydict/Feature/Configuration/EZConfiguration.m @@ -20,12 +20,12 @@ static NSString *const kFirstLanguageKey = @"EZConfiguration_kFirstLanguageKey"; static NSString *const kSecondLanguageKey = @"EZConfiguration_kSecondLanguageKey"; + static NSString *const kFromKey = @"EZConfiguration_kFromKey"; static NSString *const kToKey = @"EZConfiguration_kToKey"; static NSString *const kAutoSelectTextKey = @"EZConfiguration_kAutoSelectTextKey"; static NSString *const kForceAutoGetSelectedText = @"EZConfiguration_kForceAutoGetSelectedText"; -static NSString *const kSelectUseMiniWindowKey = @"EZConfiguration_kSelectUseMiniWindowKey"; static NSString *const kDisableEmptyCopyBeepKey = @"EZConfiguration_kDisableEmptyCopyBeepKey"; static NSString *const kClickQueryKey = @"EZConfiguration_kClickQueryKey"; static NSString *const kLaunchAtStartupKey = @"EZConfiguration_kLaunchAtStartupKey"; @@ -42,6 +42,8 @@ static NSString *const kShowEudicLinkKey = @"EZConfiguration_kShowEudicLinkKey"; static NSString *const kHideMenuBarIconKey = @"EZConfiguration_kHideMenuBarIconKey"; static NSString *const kShowFixedWindowPositionKey = @"EZConfiguration_kShowFixedWindowPositionKey"; +static NSString *const kShortcutSelectTranslateWindowTypeKey = @"EZConfiguration_kShortcutSelectTranslateWindowTypeKey"; +static NSString *const kMouseSelectTranslateWindowTypeKey = @"EZConfiguration_kMouseSelectTranslateWindowTypeKey"; static NSString *const kWindowFrameKey = @"EZConfiguration_kWindowFrameKey"; static NSString *const kAutomaticallyChecksForUpdatesKey = @"EZConfiguration_kAutomaticallyChecksForUpdatesKey"; static NSString *const kAdjustPopButtomOriginKey = @"EZConfiguration_kAdjustPopButtomOriginKey"; @@ -55,7 +57,7 @@ @implementation EZConfiguration static EZConfiguration *_instance; + (instancetype)shared { - @synchronized (self) { + @synchronized(self) { if (!_instance) { _instance = [[super allocWithZone:NULL] init]; [_instance setup]; @@ -75,13 +77,12 @@ + (instancetype)allocWithZone:(struct _NSZone *)zone { - (void)setup { self.firstLanguage = [NSUserDefaults mm_read:kFirstLanguageKey]; self.secondLanguage = [NSUserDefaults mm_read:kSecondLanguageKey]; - + self.from = [NSUserDefaults mm_readString:kFromKey defaultValue:EZLanguageAuto]; self.to = [NSUserDefaults mm_readString:kToKey defaultValue:EZLanguageAuto]; self.autoSelectText = [NSUserDefaults mm_readBool:kAutoSelectTextKey defaultValue:YES]; self.forceAutoGetSelectedText = [NSUserDefaults mm_readBool:kForceAutoGetSelectedText defaultValue:NO]; - self.selectUseMiniWindow = [NSUserDefaults mm_readBool:kSelectUseMiniWindowKey defaultValue:NO]; self.disableEmptyCopyBeep = [NSUserDefaults mm_readBool:kDisableEmptyCopyBeepKey defaultValue:YES]; self.clickQuery = [NSUserDefaults mm_readBool:kClickQueryKey defaultValue:NO]; self.autoPlayAudio = [NSUserDefaults mm_readBool:kAutoPlayAudioKey defaultValue:YES]; @@ -98,6 +99,8 @@ - (void)setup { self.showEudicQuickLink = [NSUserDefaults mm_readBool:kShowEudicLinkKey defaultValue:YES]; self.hideMenuBarIcon = [NSUserDefaults mm_readBool:kHideMenuBarIconKey defaultValue:NO]; self.fixedWindowPosition = [NSUserDefaults mm_readInteger:kShowFixedWindowPositionKey defaultValue:EZShowWindowPositionRight]; + self.shotcutSelectTranslateWindowType = [NSUserDefaults mm_readInteger:kShortcutSelectTranslateWindowTypeKey defaultValue:EZWindowTypeFixed]; + self.mouseSelectTranslateWindowType = [NSUserDefaults mm_readInteger:kMouseSelectTranslateWindowTypeKey defaultValue:EZWindowTypeMini]; self.automaticallyChecksForUpdates = [NSUserDefaults mm_readBool:kAutomaticallyChecksForUpdatesKey defaultValue:YES]; self.adjustPopButtomOrigin = [NSUserDefaults mm_readBool:kAdjustPopButtomOriginKey defaultValue:NO]; self.allowCrashLog = [NSUserDefaults mm_readBool:kAllowCrashLogKey defaultValue:YES]; @@ -154,12 +157,6 @@ - (void)setForceAutoGetSelectedText:(BOOL)forceGetSelectedText { [NSUserDefaults mm_write:@(forceGetSelectedText) forKey:kForceAutoGetSelectedText]; } -- (void)setSelectUseMiniWindow:(BOOL)selectUseMiniWindow { - _selectUseMiniWindow = selectUseMiniWindow; - - [NSUserDefaults mm_write:@(selectUseMiniWindow) forKey:kSelectUseMiniWindowKey]; -} - - (void)setDisableEmptyCopyBeep:(BOOL)disableEmptyCopyBeep { _disableEmptyCopyBeep = disableEmptyCopyBeep; @@ -170,15 +167,15 @@ - (void)setClickQuery:(BOOL)clickQuery { _clickQuery = clickQuery; [NSUserDefaults mm_write:@(clickQuery) forKey:kClickQueryKey]; - + [EZWindowManager.shared updatePopButtonQueryAction]; } - (void)setLaunchAtStartup:(BOOL)launchAtStartup { BOOL oldLaunchAtStartup = self.launchAtStartup; - + [NSUserDefaults mm_write:@(launchAtStartup) forKey:kLaunchAtStartupKey]; - + // Avoid redundant calls, run AppleScript will ask for permission, trigger notification. if (launchAtStartup != oldLaunchAtStartup) { [self updateLoginItemWithLaunchAtStartup:launchAtStartup]; @@ -195,7 +192,7 @@ - (void)setHideMainWindow:(BOOL)hideMainWindow { _hideMainWindow = hideMainWindow; [NSUserDefaults mm_write:@(hideMainWindow) forKey:kHideMainWindowKey]; - + EZWindowManager *windowManager = EZWindowManager.shared; [windowManager updatePopButtonQueryAction]; if (hideMainWindow) { @@ -256,7 +253,7 @@ - (void)setShowGoogleQuickLink:(BOOL)showGoogleLink { [NSUserDefaults mm_write:@(showGoogleLink) forKey:kShowGoogleLinkKey]; [self postUpdateQuickLinkButtonNotification]; - + EZMenuItemManager.shared.googleItem.hidden = !showGoogleLink; } @@ -265,7 +262,7 @@ - (void)setShowEudicQuickLink:(BOOL)showEudicLink { [NSUserDefaults mm_write:@(showEudicLink) forKey:kShowEudicLinkKey]; [self postUpdateQuickLinkButtonNotification]; - + EZMenuItemManager.shared.eudicItem.hidden = !showEudicLink; } @@ -283,6 +280,18 @@ - (void)setFixedWindowPosition:(EZShowWindowPosition)showFixedWindowPosition { [NSUserDefaults mm_write:@(showFixedWindowPosition) forKey:kShowFixedWindowPositionKey]; } +- (void)setShotcutSelectTranslateWindowType:(EZWindowType)shortcutSelectTranslateWindowType { + _shortcutSelectTranslateWindowType = shortcutSelectTranslateWindowType; + + [NSUserDefaults mm_write:@(shortcutSelectTranslateWindowType) forKey:(kShortcutSelectTranslateWindowTypeKey)]; +} + +- (void)setMouseSelectTranslateWindowType:(EZWindowType)mouseSelectTranslateWindowType { + _mouseSelectTranslateWindowType = mouseSelectTranslateWindowType; + + [NSUserDefaults mm_write:@(mouseSelectTranslateWindowType) forKey:(kMouseSelectTranslateWindowTypeKey)]; +} + - (void)setAdjustPopButtomOrigin:(BOOL)adjustPopButtomOrigin { _adjustPopButtomOrigin = adjustPopButtomOrigin; @@ -335,7 +344,7 @@ - (void)updateLoginItemWithLaunchAtStartup:(BOOL)launchAtStartup { // ???: name is CFBundleExecutable, or CFBundleName ? NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleExecutable"]; NSString *appBundlePath = [[NSBundle mainBundle] bundlePath]; - + NSString *script = [NSString stringWithFormat:@"\ tell application \"System Events\" to get the name of every login item\n\ tell application \"System Events\"\n\ @@ -348,14 +357,12 @@ - (void)updateLoginItemWithLaunchAtStartup:(BOOL)launchAtStartup { if %@ then\n\ make login item at end with properties {path:\"%@\", hidden:false}\n\ end if\n\ - end tell" - , appName, - launchAtStartup ? @"true" : @"false", - appBundlePath - ]; + end tell", appName, + launchAtStartup ? @"true" : @"false", + appBundlePath]; EZExeCommand *exeCommand = [[EZExeCommand alloc] init]; - [exeCommand runAppleScriptWithTask:script completionHandler:^(NSString * _Nonnull result, NSError * _Nonnull error) { + [exeCommand runAppleScriptWithTask:script completionHandler:^(NSString *_Nonnull result, NSError *_Nonnull error) { if (error) { NSLog(@"launchAtStartup error: %@", error); } else { @@ -378,7 +385,7 @@ - (void)updateLoginItemWithLaunchAtStartup:(BOOL)launchAtStartup { // Ref: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-1114741-CJADDEIB // */ // -//#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1300 +// #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1300 // // code only compiled when targeting OS X and not iOS // // note use of 1050 instead of __MAC_10_5 // @@ -397,7 +404,7 @@ - (void)updateLoginItemWithLaunchAtStartup:(BOOL)launchAtStartup { // if (!success) { // MMLogInfo(@"SMAppService fail"); // } -//#endif +// #endif // } else { // // Ref: https://nyrra33.com/2019/09/03/cocoa-launch-at-startup-best-practice/ // BOOL success = SMLoginItemSetEnabled((__bridge CFStringRef)helperBundleId, launchAtStartup); diff --git a/Easydict/Feature/PerferenceWindow/EZSettingViewController.m b/Easydict/Feature/PerferenceWindow/EZSettingViewController.m index f619cd599..1b15609d8 100644 --- a/Easydict/Feature/PerferenceWindow/EZSettingViewController.m +++ b/Easydict/Feature/PerferenceWindow/EZSettingViewController.m @@ -42,9 +42,6 @@ @interface EZSettingViewController () @property (nonatomic, strong) NSButton *showQueryIconButton; @property (nonatomic, strong) NSButton *forceGetSelectedTextButton; -@property (nonatomic, strong) NSTextField *selectTranslateConfigLabel; -@property (nonatomic, strong) NSButton *selectUseMiniWindowButton; - @property (nonatomic, strong) NSTextField *disableEmptyCopyBeepLabel; @property (nonatomic, strong) NSButton *disableEmptyCopyBeepButton; @@ -60,6 +57,12 @@ @interface EZSettingViewController () @property (nonatomic, strong) NSTextField *fixedWindowPositionLabel; @property (nonatomic, strong) NSPopUpButton *fixedWindowPositionPopUpButton; +@property (nonatomic, strong) NSTextField *shortcutSelectTranslateWindowTypeLabel; +@property (nonatomic, strong) NSPopUpButton *shortcutSelectTranslateWindowTypePopUpButton; + +@property (nonatomic, strong) NSTextField *mouseSelectTranslateWindowTypeLabel; +@property (nonatomic, strong) NSPopUpButton *mouseSelectTranslateWindowTypePopUpButton; + @property (nonatomic, strong) NSTextField *playAudioLabel; @property (nonatomic, strong) NSButton *autoPlayAudioButton; @@ -114,71 +117,71 @@ @implementation EZSettingViewController - (void)viewDidLoad { [super viewDidLoad]; // Do view setup here. - + self.config = [EZConfiguration shared]; - + [self setupUI]; - + self.leftMargin = 110; self.rightMargin = 100; self.maxViewHeightRatio = 0.7; - + [self updateViewSize]; } - (void)setupUI { NSFont *font = [NSFont systemFontOfSize:13]; - + NSTextField *inputLabel = [NSTextField labelWithString:NSLocalizedString(@"input_translate", nil)]; inputLabel.font = font; [self.contentView addSubview:inputLabel]; self.inputLabel = inputLabel; self.inputShortcutView = [[MASShortcutView alloc] init]; [self.contentView addSubview:self.inputShortcutView]; - + NSTextField *snipLabel = [NSTextField labelWithString:NSLocalizedString(@"snip_translate", nil)]; snipLabel.font = font; [self.contentView addSubview:snipLabel]; self.snipLabel = snipLabel; self.snipShortcutView = [[MASShortcutView alloc] init]; [self.contentView addSubview:self.snipShortcutView]; - + NSTextField *selectLabel = [NSTextField labelWithString:NSLocalizedString(@"select_translate", nil)]; selectLabel.font = font; [self.contentView addSubview:selectLabel]; self.selectLabel = selectLabel; self.selectionShortcutView = [[MASShortcutView alloc] init]; [self.contentView addSubview:self.selectionShortcutView]; - + NSTextField *showMiniLabel = [NSTextField labelWithString:NSLocalizedString(@"show_mini_window", nil)]; showMiniLabel.font = font; [self.contentView addSubview:showMiniLabel]; self.showMiniLabel = showMiniLabel; self.showMiniShortcutView = [[MASShortcutView alloc] init]; [self.contentView addSubview:self.showMiniShortcutView]; - + if ([EZLanguageManager.shared isSystemEnglishFirstLanguage]) { self.leftmostView = self.showMiniLabel; } - + NSTextField *screenshotOCRLabel = [NSTextField labelWithString:NSLocalizedString(@"silent_screenshot_ocr", nil)]; screenshotOCRLabel.font = font; [self.contentView addSubview:screenshotOCRLabel]; self.screenshotOCRLabel = screenshotOCRLabel; self.screenshotOCRShortcutView = [[MASShortcutView alloc] init]; [self.contentView addSubview:self.screenshotOCRShortcutView]; - - + + [self.inputShortcutView setAssociatedUserDefaultsKey:EZInputShortcutKey]; [self.snipShortcutView setAssociatedUserDefaultsKey:EZSnipShortcutKey]; [self.selectionShortcutView setAssociatedUserDefaultsKey:EZSelectionShortcutKey]; [self.showMiniShortcutView setAssociatedUserDefaultsKey:EZShowMiniShortcutKey]; [self.screenshotOCRShortcutView setAssociatedUserDefaultsKey:EZScreenshotOCRShortcutKey]; - - + + NSColor *separatorLightColor = [NSColor mm_colorWithHexString:@"#D9DADA"]; NSColor *separatorDarkColor = [NSColor mm_colorWithHexString:@"#3C3C3C"]; - + NSView *separatorView = [[NSView alloc] init]; [self.contentView addSubview:separatorView]; self.separatorView = separatorView; @@ -188,91 +191,81 @@ - (void)setupUI { } dark:^(NSView *view) { view.layer.backgroundColor = separatorDarkColor.CGColor; }]; - - + NSTextField *firstLanguageLabel = [NSTextField labelWithString:NSLocalizedString(@"first_language", nil)]; firstLanguageLabel.font = font; [self.contentView addSubview:firstLanguageLabel]; self.firstLanguageLabel = firstLanguageLabel; - + self.firstLanguagePopUpButton = [[NSPopUpButton alloc] init]; [self.contentView addSubview:self.firstLanguagePopUpButton]; [self.firstLanguagePopUpButton addItemsWithTitles:[self.allLanguageDict sortedValues]]; self.firstLanguagePopUpButton.target = self; self.firstLanguagePopUpButton.action = @selector(firstLangaugePopUpButtonClicked:); - + NSTextField *secondLanguageLabel = [NSTextField labelWithString:NSLocalizedString(@"second_language", nil)]; secondLanguageLabel.font = font; [self.contentView addSubview:secondLanguageLabel]; self.secondLanguageLabel = secondLanguageLabel; - + self.secondLanguagePopUpButton = [[NSPopUpButton alloc] init]; [self.contentView addSubview:self.secondLanguagePopUpButton]; [self.secondLanguagePopUpButton addItemsWithTitles:[self.allLanguageDict sortedValues]]; self.secondLanguagePopUpButton.target = self; self.secondLanguagePopUpButton.action = @selector(secondLangaugePopUpButtonClicked:); - - + + NSTextField *showQueryIconLabel = [NSTextField labelWithString:NSLocalizedString(@"auto_get_selected_text", nil)]; showQueryIconLabel.font = font; [self.contentView addSubview:showQueryIconLabel]; self.autoGetSelectedTextLabel = showQueryIconLabel; - + NSString *showQueryIconTitle = NSLocalizedString(@"auto_show_query_icon", nil); self.showQueryIconButton = [NSButton checkboxWithTitle:showQueryIconTitle target:self action:@selector(autoSelectTextButtonClicked:)]; [self.contentView addSubview:self.showQueryIconButton]; - + NSString *forceGetSelectedText = NSLocalizedString(@"force_auto_get_selected_text", nil); self.forceGetSelectedTextButton = [NSButton checkboxWithTitle:forceGetSelectedText target:self action:@selector(forceGetSelectedTextButtonClicked:)]; [self.contentView addSubview:self.forceGetSelectedTextButton]; - - NSTextField *selectTranslateConfigLabel = [NSTextField labelWithString:NSLocalizedString(@"select_translate", nil)]; - selectTranslateConfigLabel.font = font; - [self.contentView addSubview:selectTranslateConfigLabel]; - self.selectTranslateConfigLabel = selectTranslateConfigLabel; - - NSString *selectUseMiniWindow = NSLocalizedString(@"use_mini_window", nil); - self.selectUseMiniWindowButton = [NSButton checkboxWithTitle:selectUseMiniWindow target:self action:@selector(selectUseMiniWindowButtonClicked:)]; - [self.contentView addSubview:self.selectUseMiniWindowButton]; - + + NSTextField *disableEmptyCopyBeepLabel = [NSTextField labelWithString:NSLocalizedString(@"disable_empty_copy_beep", nil)]; disableEmptyCopyBeepLabel.font = font; [self.contentView addSubview:disableEmptyCopyBeepLabel]; self.disableEmptyCopyBeepLabel = disableEmptyCopyBeepLabel; - + NSString *disableEmptyCopyBeepTitle = NSLocalizedString(@"disable_empty_copy_beep_msg", nil); self.disableEmptyCopyBeepButton = [NSButton checkboxWithTitle:disableEmptyCopyBeepTitle target:self action:@selector(disableEmptyCopyBeepButtonClicked:)]; [self.contentView addSubview:self.disableEmptyCopyBeepButton]; - + NSTextField *clickQueryLabel = [NSTextField labelWithString:NSLocalizedString(@"click_icon_query", nil)]; clickQueryLabel.font = font; [self.contentView addSubview:clickQueryLabel]; self.clickQueryLabel = clickQueryLabel; - + NSString *clickQueryTitle = NSLocalizedString(@"click_icon_query_info", nil); self.clickQueryButton = [NSButton checkboxWithTitle:clickQueryTitle target:self action:@selector(clickQueryButtonClicked:)]; [self.contentView addSubview:self.clickQueryButton]; - - + + NSTextField *adjustQueryIconPostionLabel = [NSTextField labelWithString:NSLocalizedString(@"adjust_pop_button_origin", nil)]; adjustQueryIconPostionLabel.font = font; [self.contentView addSubview:adjustQueryIconPostionLabel]; self.adjustQueryIconPostionLabel = adjustQueryIconPostionLabel; - + NSString *adjustQueryIconPostionTitle = NSLocalizedString(@"avoid_conflict_with_PopClip_display", nil); self.adjustQueryIconPostionButton = [NSButton checkboxWithTitle:adjustQueryIconPostionTitle target:self action:@selector(adjustQueryIconPostionButtonClicked:)]; [self.contentView addSubview:self.adjustQueryIconPostionButton]; - - + + NSTextField *usesLanguageCorrectionLabel = [NSTextField labelWithString:NSLocalizedString(@"language_detect_optimize", nil)]; usesLanguageCorrectionLabel.font = font; [self.contentView addSubview:usesLanguageCorrectionLabel]; self.languageDetectLabel = usesLanguageCorrectionLabel; - - + self.languageDetectOptimizePopUpButton = [[NSPopUpButton alloc] init]; [self.contentView addSubview:self.languageDetectOptimizePopUpButton]; - + NSArray *languageDetectOptimizeItems = @[ NSLocalizedString(@"language_detect_optimize_none", nil), NSLocalizedString(@"language_detect_optimize_baidu", nil), @@ -281,13 +274,13 @@ - (void)setupUI { [self.languageDetectOptimizePopUpButton addItemsWithTitles:languageDetectOptimizeItems]; self.languageDetectOptimizePopUpButton.target = self; self.languageDetectOptimizePopUpButton.action = @selector(languageDetectOptimizePopUpButtonClicked:); - - + + NSTextField *fixedWindowPositionLabel = [NSTextField labelWithString:NSLocalizedString(@"fixed_window_position", nil)]; fixedWindowPositionLabel.font = font; [self.contentView addSubview:fixedWindowPositionLabel]; self.fixedWindowPositionLabel = fixedWindowPositionLabel; - + self.fixedWindowPositionPopUpButton = [[NSPopUpButton alloc] init]; [self.contentView addSubview:self.fixedWindowPositionPopUpButton]; MMOrderedDictionary *fixedWindowPostionDict = [EZEnumTypes fixedWindowPositionDict]; @@ -295,76 +288,101 @@ - (void)setupUI { [self.fixedWindowPositionPopUpButton addItemsWithTitles:fixedWindowPositionItems]; self.fixedWindowPositionPopUpButton.target = self; self.fixedWindowPositionPopUpButton.action = @selector(fixedWindowPositionPopUpButtonClicked:); - - + + NSTextField *shortcutSelectTranslateWindowTypeLabel = [NSTextField labelWithString:NSLocalizedString(@"shortcut_select_translate_window_type", nil)]; + shortcutSelectTranslateWindowTypeLabel.font = font; + [self.contentView addSubview:shortcutSelectTranslateWindowTypeLabel]; + self.shortcutSelectTranslateWindowTypeLabel = shortcutSelectTranslateWindowTypeLabel; + + self.shortcutSelectTranslateWindowTypePopUpButton = [[NSPopUpButton alloc] init]; + [self.contentView addSubview:self.shortcutSelectTranslateWindowTypePopUpButton]; + MMOrderedDictionary *shortcutSelectTranslateWindowTypeDict = [EZEnumTypes translateWindowTypeDict]; + NSArray *shortcutSelectTranslateWindowTypeItems = [shortcutSelectTranslateWindowTypeDict sortedValues]; + [self.shortcutSelectTranslateWindowTypePopUpButton addItemsWithTitles:shortcutSelectTranslateWindowTypeItems]; + self.shortcutSelectTranslateWindowTypePopUpButton.target = self; + self.shortcutSelectTranslateWindowTypePopUpButton.action = @selector(shortcutSelectTranslateWindowTypePopUpButtonClicked:); + + NSTextField *mouseSelectTranslateWindowTypeLabel = [NSTextField labelWithString:NSLocalizedString(@"mouse_select_translate_window_type", nil)]; + mouseSelectTranslateWindowTypeLabel.font = font; + [self.contentView addSubview:mouseSelectTranslateWindowTypeLabel]; + self.mouseSelectTranslateWindowTypeLabel = mouseSelectTranslateWindowTypeLabel; + + self.mouseSelectTranslateWindowTypePopUpButton = [[NSPopUpButton alloc] init]; + [self.contentView addSubview:self.mouseSelectTranslateWindowTypePopUpButton]; + MMOrderedDictionary *mouseSelectTranslateWindowTypeDict = [EZEnumTypes translateWindowTypeDict]; + NSArray *mouseSelectTranslateWindowTypeItems = [mouseSelectTranslateWindowTypeDict sortedValues]; + [self.mouseSelectTranslateWindowTypePopUpButton addItemsWithTitles:mouseSelectTranslateWindowTypeItems]; + self.mouseSelectTranslateWindowTypePopUpButton.target = self; + self.mouseSelectTranslateWindowTypePopUpButton.action = @selector(mouseSelectTranslateWindowTypePopUpButtonClicked:); + NSTextField *playAudioLabel = [NSTextField labelWithString:NSLocalizedString(@"play_word_audio", nil)]; playAudioLabel.font = font; [self.contentView addSubview:playAudioLabel]; self.playAudioLabel = playAudioLabel; - + NSString *autoPlayAudioTitle = NSLocalizedString(@"auto_play_word_audio", nil); self.autoPlayAudioButton = [NSButton checkboxWithTitle:autoPlayAudioTitle target:self action:@selector(autoPlayAudioButtonClicked:)]; [self.contentView addSubview:self.autoPlayAudioButton]; - + NSTextField *clearInputLabel = [NSTextField labelWithString:NSLocalizedString(@"clear_input", nil)]; clearInputLabel.font = font; [self.contentView addSubview:clearInputLabel]; self.clearInputLabel = clearInputLabel; - + NSString *clearInputTitle = NSLocalizedString(@"clear_input_when_translating", nil); self.clearInputButton = [NSButton checkboxWithTitle:clearInputTitle target:self action:@selector(clearInputButtonClicked:)]; [self.contentView addSubview:self.clearInputButton]; - + NSTextField *autoQueryLabel = [NSTextField labelWithString:NSLocalizedString(@"auto_query", nil)]; autoQueryLabel.font = font; [self.contentView addSubview:autoQueryLabel]; self.autoQueryLabel = autoQueryLabel; - + NSString *autoQueryOCTText = NSLocalizedString(@"auto_query_ocr_text", nil); self.autoQueryOCRTextButton = [NSButton checkboxWithTitle:autoQueryOCTText target:self action:@selector(autoQueryOCRTextButtonClicked:)]; [self.contentView addSubview:self.autoQueryOCRTextButton]; - + NSString *autoQuerySelectedText = NSLocalizedString(@"auto_query_selected_text", nil); self.autoQuerySelectedTextButton = [NSButton checkboxWithTitle:autoQuerySelectedText target:self action:@selector(autoQuerySelectedTextButtonClicked:)]; [self.contentView addSubview:self.autoQuerySelectedTextButton]; - + NSString *autoQueryPastedTextButton = NSLocalizedString(@"auto_query_pasted_text", nil); self.autoQueryPastedTextButton = [NSButton checkboxWithTitle:autoQueryPastedTextButton target:self action:@selector(autoQueryPastedTextButtonClicked:)]; [self.contentView addSubview:self.autoQueryPastedTextButton]; - - + + NSTextField *autoCopyTextLabel = [NSTextField labelWithString:NSLocalizedString(@"auto_copy_text", nil)]; autoCopyTextLabel.font = font; [self.contentView addSubview:autoCopyTextLabel]; self.autoCopyTextLabel = autoCopyTextLabel; - + NSString *autoCopyOCRText = NSLocalizedString(@"auto_copy_ocr_text", nil); self.autoCopyOCRTextButton = [NSButton checkboxWithTitle:autoCopyOCRText target:self action:@selector(autoCopyOCRTextButtonClicked:)]; [self.contentView addSubview:self.autoCopyOCRTextButton]; - + NSString *autoCopySelectedText = NSLocalizedString(@"auto_copy_selected_text", nil); self.autoCopySelectedTextButton = [NSButton checkboxWithTitle:autoCopySelectedText target:self action:@selector(autoCopySelectedTextButtonClicked:)]; [self.contentView addSubview:self.autoCopySelectedTextButton]; - + NSString *autoCopyFirstTranslatedText = NSLocalizedString(@"auto_copy_first_translated_text", nil); self.autoCopyFirstTranslatedTextButton = [NSButton checkboxWithTitle:autoCopyFirstTranslatedText target:self action:@selector(autoCopyFirstTranslatedTextButtonClicked:)]; [self.contentView addSubview:self.autoCopyFirstTranslatedTextButton]; - - + + NSTextField *showQuickLinkLabel = [NSTextField labelWithString:NSLocalizedString(@"quick_link", nil)]; showQuickLinkLabel.font = font; [self.contentView addSubview:showQuickLinkLabel]; self.showQuickLinkLabel = showQuickLinkLabel; - + NSString *showGoogleQuickLink = NSLocalizedString(@"show_google_quick_link", nil); self.showGoogleQuickLinkButton = [NSButton checkboxWithTitle:showGoogleQuickLink target:self action:@selector(showGoogleQuickLinkButtonClicked:)]; [self.contentView addSubview:self.showGoogleQuickLinkButton]; - + NSString *showEudicQuickLink = NSLocalizedString(@"show_eudic_quick_link", nil); self.showEudicQuickLinkButton = [NSButton checkboxWithTitle:showEudicQuickLink target:self action:@selector(showEudicQuickLinkButtonClicked:)]; [self.contentView addSubview:self.showEudicQuickLinkButton]; - - + + NSView *separatorView2 = [[NSView alloc] init]; [self.contentView addSubview:separatorView2]; self.separatorView2 = separatorView2; @@ -374,45 +392,46 @@ - (void)setupUI { } dark:^(NSView *view) { view.layer.backgroundColor = separatorDarkColor.CGColor; }]; - + NSTextField *hideMainWindowLabel = [NSTextField labelWithString:NSLocalizedString(@"show_main_window", nil)]; hideMainWindowLabel.font = font; [self.contentView addSubview:hideMainWindowLabel]; self.hideMainWindowLabel = hideMainWindowLabel; - + NSString *hideMainWindowTitle = NSLocalizedString(@"hide_main_window", nil); self.hideMainWindowButton = [NSButton checkboxWithTitle:hideMainWindowTitle target:self action:@selector(hideMainWindowButtonClicked:)]; [self.contentView addSubview:self.hideMainWindowButton]; - + NSTextField *launchLabel = [NSTextField labelWithString:NSLocalizedString(@"launch", nil)]; launchLabel.font = font; [self.contentView addSubview:launchLabel]; self.launchLabel = launchLabel; - + NSString *launchAtStartupTitle = NSLocalizedString(@"launch_at_startup", nil); self.launchAtStartupButton = [NSButton checkboxWithTitle:launchAtStartupTitle target:self action:@selector(launchAtStartupButtonClicked:)]; [self.contentView addSubview:self.launchAtStartupButton]; - + NSTextField *menubarIconLabel = [NSTextField labelWithString:NSLocalizedString(@"menu_bar_icon", nil)]; menubarIconLabel.font = font; [self.contentView addSubview:menubarIconLabel]; self.menuBarIconLabel = menubarIconLabel; - + NSString *hideMenuBarIcon = NSLocalizedString(@"hide_menu_bar_icon", nil); self.hideMenuBarIconButton = [NSButton checkboxWithTitle:hideMenuBarIcon target:self action:@selector(hideMenuBarIconButtonClicked:)]; [self.contentView addSubview:self.hideMenuBarIconButton]; - - + + [self updatePreferredLanguagesPopUpButton]; - + self.showQueryIconButton.mm_isOn = self.config.autoSelectText; self.forceGetSelectedTextButton.mm_isOn = self.config.forceAutoGetSelectedText; - self.selectUseMiniWindowButton.mm_isOn = self.config.selectUseMiniWindow; self.disableEmptyCopyBeepButton.mm_isOn = self.config.disableEmptyCopyBeep; self.clickQueryButton.mm_isOn = self.config.clickQuery; self.adjustQueryIconPostionButton.mm_isOn = self.config.adjustPopButtomOrigin; [self.languageDetectOptimizePopUpButton selectItemAtIndex:self.config.languageDetectOptimize]; [self.fixedWindowPositionPopUpButton selectItemAtIndex:self.config.fixedWindowPosition]; + [self.shortcutSelectTranslateWindowTypePopUpButton selectItemAtIndex:self.config.shortcutSelectTranslateWindowType]; + [self.mouseSelectTranslateWindowTypePopUpButton selectItemAtIndex:self.config.mouseSelectTranslateWindowType]; self.autoPlayAudioButton.mm_isOn = self.config.autoPlayAudio; self.clearInputButton.mm_isOn = self.config.clearInput; self.launchAtStartupButton.mm_isOn = self.config.launchAtStartup; @@ -430,7 +449,7 @@ - (void)setupUI { - (void)updateViewConstraints { CGFloat separatorMargin = 40; - + [self.inputLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.selectLabel); make.top.equalTo(self.contentView).offset(self.topMargin).priorityLow(); @@ -440,7 +459,7 @@ - (void)updateViewConstraints { make.centerY.equalTo(self.inputLabel); make.height.equalTo(self.selectionShortcutView); }]; - + [self.snipLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.selectLabel); make.top.equalTo(self.inputShortcutView.mas_bottom).offset(self.verticalPadding); @@ -450,7 +469,7 @@ - (void)updateViewConstraints { make.centerY.equalTo(self.snipLabel); make.height.equalTo(self.selectionShortcutView); }]; - + [self.selectLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(self.leftMargin).priorityLow(); make.top.equalTo(self.snipShortcutView.mas_bottom).offset(self.verticalPadding); @@ -460,7 +479,7 @@ - (void)updateViewConstraints { make.centerY.equalTo(self.selectLabel); make.height.mas_equalTo(25); }]; - + [self.showMiniLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.selectLabel); make.top.equalTo(self.selectionShortcutView.mas_bottom).offset(self.verticalPadding); @@ -470,7 +489,7 @@ - (void)updateViewConstraints { make.centerY.equalTo(self.showMiniLabel); make.height.equalTo(self.selectionShortcutView); }]; - + [self.screenshotOCRLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.selectLabel); make.top.equalTo(self.showMiniShortcutView.mas_bottom).offset(self.verticalPadding); @@ -480,14 +499,14 @@ - (void)updateViewConstraints { make.centerY.equalTo(self.screenshotOCRLabel); make.height.equalTo(self.selectionShortcutView); }]; - - + + [self.separatorView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.inset(separatorMargin); make.top.equalTo(self.screenshotOCRLabel.mas_bottom).offset(1.5 * self.verticalPadding); make.height.mas_equalTo(1); }]; - + [self.firstLanguageLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.selectLabel); make.top.equalTo(self.separatorView.mas_bottom).offset(1.5 * self.verticalPadding); @@ -496,7 +515,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.firstLanguageLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.firstLanguageLabel); }]; - + [self.secondLanguageLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.selectLabel); make.top.equalTo(self.firstLanguagePopUpButton.mas_bottom).offset(self.verticalPadding); @@ -505,7 +524,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.secondLanguageLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.secondLanguageLabel); }]; - + [self.autoGetSelectedTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.selectLabel); make.top.equalTo(self.secondLanguagePopUpButton.mas_bottom).offset(1.5 * self.verticalPadding); @@ -518,25 +537,16 @@ - (void)updateViewConstraints { make.left.equalTo(self.showQueryIconButton); make.top.equalTo(self.showQueryIconButton.mas_bottom).offset(self.verticalPadding); }]; - - [self.selectTranslateConfigLabel mas_remakeConstraints:^(MASConstraintMaker *make) { + + [self.disableEmptyCopyBeepLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.forceGetSelectedTextButton.mas_bottom).offset(self.verticalPadding); }]; - [self.selectUseMiniWindowButton mas_remakeConstraints:^(MASConstraintMaker *make) { - make.left.equalTo(self.selectTranslateConfigLabel.mas_right).offset(self.horizontalPadding); - make.centerY.equalTo(self.selectTranslateConfigLabel); - }]; - - [self.disableEmptyCopyBeepLabel mas_remakeConstraints:^(MASConstraintMaker *make) { - make.right.equalTo(self.selectTranslateConfigLabel); - make.top.equalTo(self.selectUseMiniWindowButton.mas_bottom).offset(self.verticalPadding); - }]; [self.disableEmptyCopyBeepButton mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.disableEmptyCopyBeepLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.disableEmptyCopyBeepLabel); }]; - + [self.clickQueryLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.disableEmptyCopyBeepButton.mas_bottom).offset(self.verticalPadding); @@ -545,8 +555,8 @@ - (void)updateViewConstraints { make.left.equalTo(self.clickQueryLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.clickQueryLabel); }]; - - + + [self.adjustQueryIconPostionLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.clickQueryLabel); make.top.equalTo(self.clickQueryButton.mas_bottom).offset(self.verticalPadding); @@ -555,7 +565,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.adjustQueryIconPostionLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.adjustQueryIconPostionLabel); }]; - + [self.languageDetectLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.adjustQueryIconPostionButton.mas_bottom).offset(self.verticalPadding); @@ -564,7 +574,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.languageDetectLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.languageDetectLabel); }]; - + [self.fixedWindowPositionLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.languageDetectOptimizePopUpButton.mas_bottom).offset(self.verticalPadding); @@ -573,17 +583,35 @@ - (void)updateViewConstraints { make.left.equalTo(self.fixedWindowPositionLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.fixedWindowPositionLabel); }]; - - [self.playAudioLabel mas_remakeConstraints:^(MASConstraintMaker *make) { + + [self.shortcutSelectTranslateWindowTypeLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.fixedWindowPositionPopUpButton.mas_bottom).offset(self.verticalPadding); }]; + [self.shortcutSelectTranslateWindowTypePopUpButton mas_remakeConstraints:^(MASConstraintMaker *make) { + make.left.equalTo(self.shortcutSelectTranslateWindowTypeLabel.mas_right).offset(self.horizontalPadding); + make.centerY.equalTo(self.shortcutSelectTranslateWindowTypeLabel); + }]; + + [self.mouseSelectTranslateWindowTypeLabel mas_remakeConstraints:^(MASConstraintMaker *make) { + make.right.equalTo(self.autoGetSelectedTextLabel); + make.top.equalTo(self.shortcutSelectTranslateWindowTypePopUpButton.mas_bottom).offset(self.verticalPadding); + }]; + [self.mouseSelectTranslateWindowTypePopUpButton mas_remakeConstraints:^(MASConstraintMaker *make) { + make.left.equalTo(self.mouseSelectTranslateWindowTypeLabel.mas_right).offset(self.horizontalPadding); + make.centerY.equalTo(self.mouseSelectTranslateWindowTypeLabel); + }]; + + [self.playAudioLabel mas_remakeConstraints:^(MASConstraintMaker *make) { + make.right.equalTo(self.autoGetSelectedTextLabel); + make.top.equalTo(self.mouseSelectTranslateWindowTypePopUpButton.mas_bottom).offset(self.verticalPadding); + }]; [self.autoPlayAudioButton mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.playAudioLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.playAudioLabel); }]; - - + + [self.clearInputLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.autoPlayAudioButton.mas_bottom).offset(self.verticalPadding); @@ -592,8 +620,8 @@ - (void)updateViewConstraints { make.left.equalTo(self.clearInputLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.clearInputLabel); }]; - - + + [self.autoQueryLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.clearInputButton.mas_bottom).offset(self.verticalPadding); @@ -610,8 +638,8 @@ - (void)updateViewConstraints { make.left.equalTo(self.autoQuerySelectedTextButton); make.top.equalTo(self.autoQuerySelectedTextButton.mas_bottom).offset(self.verticalPadding); }]; - - + + [self.autoCopyTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.autoQueryPastedTextButton.mas_bottom).offset(self.verticalPadding); @@ -628,8 +656,8 @@ - (void)updateViewConstraints { make.left.equalTo(self.autoCopySelectedTextButton); make.top.equalTo(self.autoCopySelectedTextButton.mas_bottom).offset(self.verticalPadding); }]; - - + + [self.showQuickLinkLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.autoCopyFirstTranslatedTextButton.mas_bottom).offset(self.verticalPadding); @@ -642,14 +670,14 @@ - (void)updateViewConstraints { make.left.equalTo(self.showGoogleQuickLinkButton); make.top.equalTo(self.showGoogleQuickLinkButton.mas_bottom).offset(self.verticalPadding); }]; - - + + [self.separatorView2 mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.separatorView); make.top.equalTo(self.showEudicQuickLinkButton.mas_bottom).offset(1.5 * self.verticalPadding); make.height.equalTo(self.separatorView); }]; - + [self.hideMainWindowLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.separatorView2.mas_bottom).offset(1.5 * self.verticalPadding); @@ -658,7 +686,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.hideMainWindowLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.hideMainWindowLabel); }]; - + [self.launchLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.hideMainWindowButton.mas_bottom).offset(self.verticalPadding); @@ -667,7 +695,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.launchLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.launchLabel); }]; - + [self.menuBarIconLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.launchAtStartupButton.mas_bottom).offset(self.verticalPadding); @@ -676,20 +704,20 @@ - (void)updateViewConstraints { make.left.equalTo(self.menuBarIconLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.menuBarIconLabel); }]; - + self.topmostView = self.inputLabel; self.bottommostView = self.hideMenuBarIconButton; - + if ([EZLanguageManager.shared isSystemChineseFirstLanguage]) { self.leftmostView = self.adjustQueryIconPostionLabel; self.rightmostView = self.forceGetSelectedTextButton; } - + if ([EZLanguageManager.shared isSystemEnglishFirstLanguage]) { self.leftmostView = self.adjustQueryIconPostionLabel; self.rightmostView = self.forceGetSelectedTextButton; } - + [super updateViewConstraints]; } @@ -698,13 +726,13 @@ - (void)updateViewConstraints { - (BOOL)checkAppIsTrusted { BOOL isTrusted = AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef) @{(__bridge NSString *)kAXTrustedCheckOptionPrompt : @YES}); NSLog(@"isTrusted: %d", isTrusted); - + return isTrusted == YES; } - (void)autoSelectTextButtonClicked:(NSButton *)sender { self.config.autoSelectText = sender.mm_isOn; - + if (sender.mm_isOn) { [self checkAppIsTrusted]; } @@ -775,10 +803,6 @@ - (void)autoCopyFirstTranslatedTextButtonClicked:(NSButton *)sender { self.config.autoCopyFirstTranslatedText = sender.mm_isOn; } -- (void)selectUseMiniWindowButtonClicked:(NSButton *)sender { - self.config.selectUseMiniWindow = sender.mm_isOn; -} - - (void)showGoogleQuickLinkButtonClicked:(NSButton *)sender { self.config.showGoogleQuickLink = sender.mm_isOn; } @@ -809,6 +833,16 @@ - (void)hideMenuBarIconButtonClicked:(NSButton *)sender { } } +- (void)shortcutSelectTranslateWindowTypePopUpButtonClicked:(NSPopUpButton *)button { + NSInteger selectedIndex = button.indexOfSelectedItem; + self.config.shortcutSelectTranslateWindowType = selectedIndex; +} + +- (void)mouseSelectTranslateWindowTypePopUpButtonClicked:(NSPopUpButton *)button { + NSInteger selectedIndex = button.indexOfSelectedItem; + self.config.mouseSelectTranslateWindowType = selectedIndex; +} + - (void)fixedWindowPositionPopUpButtonClicked:(NSPopUpButton *)button { NSInteger selectedIndex = button.indexOfSelectedItem; self.config.fixedWindowPosition = selectedIndex; @@ -833,14 +867,14 @@ - (void)firstLangaugePopUpButtonClicked:(NSPopUpButton *)button { NSInteger selectedIndex = button.indexOfSelectedItem; EZLanguage language = EZLanguageManager.shared.allLanguageFlagDict.sortedKeys[selectedIndex]; self.config.firstLanguage = language; - + [self checkIfEqualFirstLanguage:YES]; } - (void)secondLangaugePopUpButtonClicked:(NSPopUpButton *)button { NSInteger selectedIndex = button.indexOfSelectedItem; EZLanguage language = EZLanguageManager.shared.allLanguageFlagDict.sortedKeys[selectedIndex]; self.config.secondLanguage = language; - + [self checkIfEqualFirstLanguage:NO]; } @@ -848,7 +882,7 @@ - (void)checkIfEqualFirstLanguage:(BOOL)fistLanguageFlag { if ([self.config.firstLanguage isEqualToString:self.config.secondLanguage]) { NSAlert *alert = [[NSAlert alloc] init]; [alert addButtonWithTitle:NSLocalizedString(@"ok", nil)]; - + NSString *warningText = NSLocalizedString(@"equal_first_and_second_language", nil); NSString *showingLanguage = [EZLanguageManager.shared showingLanguageName:self.config.firstLanguage]; alert.messageText = [NSString stringWithFormat:@"%@: %@", warningText, showingLanguage]; @@ -857,13 +891,13 @@ - (void)checkIfEqualFirstLanguage:(BOOL)fistLanguageFlag { // If isFistLanguage is YES, means we need to auto correct second language according to first language. EZLanguage sourceLanguage = fistLanguageFlag ? self.config.firstLanguage : self.config.secondLanguage; EZLanguage autoTargetLanguage = [EZLanguageManager.shared userTargetLanguageWithSourceLanguage:sourceLanguage]; - + if (fistLanguageFlag) { self.config.secondLanguage = autoTargetLanguage; } else { self.config.firstLanguage = autoTargetLanguage; } - + [self updatePreferredLanguagesPopUpButton]; } }]; @@ -873,7 +907,7 @@ - (void)checkIfEqualFirstLanguage:(BOOL)fistLanguageFlag { - (void)updatePreferredLanguagesPopUpButton { NSInteger firstLanguageIndex = [self.allLanguageDict.sortedKeys indexOfObject:EZLanguageManager.shared.userFirstLanguage]; [self.firstLanguagePopUpButton selectItemAtIndex:firstLanguageIndex]; - + NSInteger secondLanguageIndex = [self.allLanguageDict.sortedKeys indexOfObject:EZLanguageManager.shared.userSecondLanguage]; [self.secondLanguagePopUpButton selectItemAtIndex:secondLanguageIndex]; } diff --git a/Easydict/Feature/Service/Model/EZEnumTypes.h b/Easydict/Feature/Service/Model/EZEnumTypes.h index 06804d662..7e55d2878 100644 --- a/Easydict/Feature/Service/Model/EZEnumTypes.h +++ b/Easydict/Feature/Service/Model/EZEnumTypes.h @@ -42,10 +42,10 @@ FOUNDATION_EXPORT NSString *const EZQueryTextTypeKey; FOUNDATION_EXPORT NSString *const EZIntelligentQueryTextTypeKey; typedef NS_OPTIONS(NSUInteger, EZQueryTextType) { - EZQueryTextTypeNone = 0, // 0 + EZQueryTextTypeNone = 0, // 0 EZQueryTextTypeTranslation = 1 << 0, // 01 = 1 - EZQueryTextTypeDictionary = 1 << 1, // 10 = 2 - EZQueryTextTypeSentence = 1 << 2, // 100 = 4 + EZQueryTextTypeDictionary = 1 << 1, // 10 = 2 + EZQueryTextTypeSentence = 1 << 2, // 100 = 4 }; @@ -91,7 +91,7 @@ typedef NS_OPTIONS(NSUInteger, EZTriggerType) { }; -@interface EZEnumTypes: NSObject +@interface EZEnumTypes : NSObject + (NSString *)stringValueOfTriggerType:(EZTriggerType)triggerType; @@ -99,6 +99,8 @@ typedef NS_OPTIONS(NSUInteger, EZTriggerType) { + (MMOrderedDictionary *)fixedWindowPositionDict; ++ (MMOrderedDictionary *)translateWindowTypeDict; + @end diff --git a/Easydict/Feature/Service/Model/EZEnumTypes.m b/Easydict/Feature/Service/Model/EZEnumTypes.m index 0fe6a2413..e8699c74c 100644 --- a/Easydict/Feature/Service/Model/EZEnumTypes.m +++ b/Easydict/Feature/Service/Model/EZEnumTypes.m @@ -69,15 +69,23 @@ + (NSString *)windowName:(EZWindowType)type { } + (MMOrderedDictionary *)fixedWindowPositionDict { - MMOrderedDictionary *dict = [ - [MMOrderedDictionary alloc] initWithKeysAndObjects: - @(EZShowWindowPositionRight), NSLocalizedString(@"fixed_window_position_right", nil), - @(EZShowWindowPositionMouse), NSLocalizedString(@"fixed_window_position_mouse", nil), - @(EZShowWindowPositionFormer), NSLocalizedString(@"fixed_window_position_former", nil), - @(EZShowWindowPositionCenter), NSLocalizedString(@"fixed_window_position_center", nil), - nil - ]; - + MMOrderedDictionary *dict = [[MMOrderedDictionary alloc] initWithKeysAndObjects: + @(EZShowWindowPositionRight), NSLocalizedString(@"fixed_window_position_right", nil), + @(EZShowWindowPositionMouse), NSLocalizedString(@"fixed_window_position_mouse", nil), + @(EZShowWindowPositionFormer), NSLocalizedString(@"fixed_window_position_former", nil), + @(EZShowWindowPositionCenter), NSLocalizedString(@"fixed_window_position_center", nil), + nil]; + + return dict; +} + ++ (MMOrderedDictionary *)translateWindowTypeDict { + MMOrderedDictionary *dict = [[MMOrderedDictionary alloc] initWithKeysAndObjects: + @(EZWindowTypeMain), NSLocalizedString(@"tranalte_window_type_main", nil), + @(EZWindowTypeMini), NSLocalizedString(@"tranalte_window_type_mini", nil), + @(EZWindowTypeFixed), NSLocalizedString(@"tranalte_window_type_fixed", nil), + nil]; + return dict; } diff --git a/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m b/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m index c46880d1e..255d4c19f 100644 --- a/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m +++ b/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m @@ -21,7 +21,7 @@ @interface EZWindowManager () @property (nonatomic, strong) NSRunningApplication *lastFrontmostApplication; @property (nonatomic, strong) EZEventMonitor *eventMonitor; -@property ( nonatomic, copy, nullable) NSString *selectedText; +@property (nonatomic, copy, nullable) NSString *selectedText; @property (nonatomic, assign) CGPoint startPoint; @property (nonatomic, assign) CGPoint endPoint; @@ -67,79 +67,79 @@ - (instancetype)init { - (void)setup { self.offsetPoint = CGPointMake(18, -12); self.screen = NSScreen.mainScreen; - self.floatingWindowTypeArray = [NSMutableArray arrayWithArray:@[@(EZWindowTypeNone)]]; + self.floatingWindowTypeArray = [NSMutableArray arrayWithArray:@[ @(EZWindowTypeNone) ]]; self.actionType = EZActionTypeAutoSelectQuery; - + self.eventMonitor = [[EZEventMonitor alloc] init]; [self setupEventMonitor]; - + // NSLog(@"%@", self.floatingWindowTypeArray); } - (void)setupEventMonitor { [self.eventMonitor startMonitor]; - + mm_weakify(self); [self.eventMonitor setSelectedTextBlock:^(NSString *_Nonnull selectedText) { mm_strongify(self); - + // if ([self hasEasydictRunningInDebugMode]) { // return; // } - + self.selectedText = selectedText ?: @""; self.actionType = self.eventMonitor.actionType; - + // !!!: Record current selected start and end point, eventMonitor's startPoint will change every valid event. self.startPoint = self.eventMonitor.startPoint; self.endPoint = self.eventMonitor.endPoint; - + CGPoint point = [self getPopButtonWindowLocation]; // This is top-left point CGPoint bottomLeftPoint = CGPointMake(point.x, point.y - self.popButtonWindow.height); CGPoint safePoint = [EZCoordinateUtils getFrameSafePoint:self.popButtonWindow.frame moveToPoint:bottomLeftPoint inScreen:self.screen]; [self.popButtonWindow setFrameOrigin:safePoint]; - + [self.popButtonWindow orderFrontRegardless]; // Set a high level to make sure it's always on top of other windows, such as PopClip. self.popButtonWindow.level = kCGScreenSaverWindowLevel; - + if ([EZMainQueryWindow isAlive]) { [self.mainWindow orderBack:nil]; } }]; - + [self updatePopButtonQueryAction]; - + [self.eventMonitor setMouseClickBlock:^(CGPoint clickPoint) { mm_strongify(self); self.startPoint = clickPoint; self.screen = [EZCoordinateUtils screenForPoint:clickPoint]; EZLayoutManager.shared.screen = self.screen; }]; - + [self.eventMonitor setDismissPopButtonBlock:^{ mm_strongify(self); -// NSLog(@"dismiss pop button"); + // NSLog(@"dismiss pop button"); [self.popButtonWindow close]; }]; - + [self.eventMonitor setDismissMiniWindowBlock:^{ mm_strongify(self); if (!self.floatingWindow.pin && self.floatingWindow.visible) { [self closeFloatingWindow]; } }]; - + [self.eventMonitor setDismissFixedWindowBlock:^{ mm_strongify(self); if (!self.floatingWindow.pin) { [self closeFloatingWindow]; } }]; - + [self.eventMonitor setDoubleCommandBlock:^{ NSLog(@"double command"); - + // TODO: Let users customize double-click shortcuts later on #if DEBUG mm_strongify(self); @@ -152,17 +152,17 @@ - (void)setupEventMonitor { /// Update pop button query action. - (void)updatePopButtonQueryAction { mm_weakify(self); - + EZButton *popButton = self.popButtonWindow.popButton; EZConfiguration *config = [EZConfiguration shared]; - + if (config.hideMainWindow) { // FIXME: Click pop button will also show preferences window. [popButton setClickBlock:^(EZButton *button) { mm_strongify(self); [self popButtonWindowClicked]; }]; - + if (config.clickQuery) { popButton.mouseEnterBlock = nil; } else { @@ -173,7 +173,7 @@ - (void)updatePopButtonQueryAction { } } else { popButton.clickBlock = nil; - + [popButton setMouseEnterBlock:^(EZButton *button) { mm_strongify(self); [self popButtonWindowClicked]; @@ -182,8 +182,9 @@ - (void)updatePopButtonQueryAction { } - (void)popButtonWindowClicked { + EZWindowType windowType = EZConfiguration.shared.mouseSelectTranslateWindowType; self.actionType = EZActionTypeAutoSelectQuery; - [self showFloatingWindowType:EZWindowTypeMini queryText:self.selectedText]; + [self showFloatingWindowType:windowType queryText:self.selectedText]; [self->_popButtonWindow close]; } @@ -287,12 +288,12 @@ - (void)showFloatingWindowType:(EZWindowType)type queryText:(nullable NSString * // if ([self hasEasydictRunningInDebugMode]) { // return; // } - + self.selectedText = text; - + EZBaseQueryWindow *window = [self windowWithType:type]; __block CGPoint location = location = [self floatingWindowLocationWithType:type]; - + // If text is nil, means we don't need to query anything, just show the window. if (!text) { // !!!: location is top-left point, so we need to change it to bottom-left point. @@ -300,25 +301,25 @@ - (void)showFloatingWindowType:(EZWindowType)type queryText:(nullable NSString * [self showFloatingWindow:window atPoint:location]; return; } - + // Log selected text when querying. [self logSelectedTextEvent]; - + // Reset tableView and window height first, avoid being affected by previous window height. - + EZBaseQueryViewController *queryViewController = window.queryViewController; [queryViewController resetTableView:^{ [queryViewController updateQueryTextAndParagraphStyle:text actionType:self.actionType]; [queryViewController detectQueryText:nil]; - + // !!!: window height has changed, so we need to update location again. location = CGPointMake(location.x, location.y - window.height); [self showFloatingWindow:window atPoint:location]; - + if ([EZConfiguration.shared autoQuerySelectedText]) { [queryViewController startQueryText:text actionType:self.actionType]; } - + if ([EZConfiguration.shared autoCopySelectedText]) { [text copyToPasteboard]; } @@ -333,41 +334,41 @@ - (void)detectQueryText:(NSString *)text completion:(nullable void (^)(NSString - (void)showFloatingWindow:(EZBaseQueryWindow *)window atPoint:(CGPoint)point { // NSLog(@"show floating window: %@, %@", window, @(point)); - + [self saveFrontmostApplication]; - + if (Snip.shared.isSnapshotting) { return; } - + EZPreferencesWindowController *preferencesWindowController = [EZPreferencesWindowController shared]; if (preferencesWindowController.isShowing) { [preferencesWindowController.window close]; } - + // get safe window position CGPoint safeLocation = [EZCoordinateUtils getFrameSafePoint:window.frame moveToPoint:point inScreen:self.screen]; [window setFrameOrigin:safeLocation]; window.level = EZFloatingWindowLevel; - + // FIXME: need to optimize. we have to remove it temporary, and orderBack: when close floating window. if ([EZMainQueryWindow isAlive]) { [self.mainWindow orderOut:nil]; } - -// NSLog(@"window frame: %@", @(window.frame)); + + // NSLog(@"window frame: %@", @(window.frame)); // ???: This code will cause warning: [Window] Warning: Window EZFixedQueryWindow 0x107f04db0 ordered front from a non-active application and may order beneath the active application's windows. [window makeKeyAndOrderFront:nil]; - + /// ???: orderFrontRegardless will cause OCR show blank window when window has shown. // [window orderFrontRegardless]; - + // !!!: Focus input textView should behind makeKeyAndOrderFront:, otherwise it will not work in the first time. [window.queryViewController focusInputTextView]; - + [self updateFloatingWindowType:window.windowType]; - + // mainWindow has been ordered out before, so we need to order back. if ([EZMainQueryWindow isAlive]) { [self.mainWindow orderBack:nil]; @@ -382,7 +383,7 @@ - (void)updateFloatingWindowType:(EZWindowType)floatingWindowType { - (NSScreen *)getMouseLocatedScreen { NSPoint mouseLocation = [NSEvent mouseLocation]; // ???: self.endPoint - + // 找到鼠标所在屏幕 NSScreen *screen = [NSScreen.screens mm_find:^id(NSScreen *_Nonnull obj, NSUInteger idx) { return NSPointInRect(mouseLocation, obj.frame) ? obj : nil; @@ -393,7 +394,7 @@ - (NSScreen *)getMouseLocatedScreen { return MMPointInRect(mouseLocation, obj.frame) ? obj : nil; }]; } - + return screen; } @@ -402,40 +403,40 @@ - (NSScreen *)getMouseLocatedScreen { - (CGPoint)getPopButtonWindowLocation { NSPoint location = [NSEvent mouseLocation]; // NSLog(@"mouseLocation: (%.1f, %.1f)", location.x, location.y); - + if (CGPointEqualToPoint(location, CGPointZero)) { return CGPointZero; } - + NSPoint startLocation = self.startPoint; NSPoint endLocation = self.endPoint; - + // Direction from left to right. BOOL isDirectionRight = endLocation.x >= startLocation.x; // Direction from top to bottom. BOOL isDirectionDown = YES; - + CGFloat minLineHeight = 20; - + CGFloat deltaY = endLocation.y - startLocation.y; // Direction up. if (deltaY > minLineHeight / 2) { isDirectionDown = NO; isDirectionRight = NO; } - + CGFloat x = location.x; CGFloat y = location.y; - + // self.offsetPoint is (15, -15) - + x += self.offsetPoint.x; y += self.offsetPoint.y; - + // FIXME: If adjust y when Direction is Up, it will cause some UI bugs 😢 // TODO: This codo is too ugly, need to optimize. - - + + // if (isDirectionDown) { // x += self.offsetPoint.x; // y += self.offsetPoint.y; @@ -444,29 +445,29 @@ - (CGPoint)getPopButtonWindowLocation { // // Direction up, show pop button window above the selected text. // y = location.y - self.offsetPoint.y + self.popButtonWindow.height + 5; // } - + // CGRect selectedTextFrame = self.eventMonitor.selectedTextFrame; // NSLog(@"selected text frame: %@", NSStringFromRect(selectedTextFrame)); // NSLog(@"start point: %@", NSStringFromPoint(startLocation)); // NSLog(@"end point: %@", NSStringFromPoint(endLocation)); - + if (EZConfiguration.shared.adjustPopButtomOrigin) { // Since the pop button may cover selected text, we need to move it to the left. CGFloat horizontalOffset = 20; - + x = location.x; if (isDirectionRight) { x += horizontalOffset; } else { x -= (horizontalOffset + self.popButtonWindow.width); } - + y = location.y - self.offsetPoint.y; } - + NSPoint popLocation = CGPointMake(x, y); // NSLog(@"popLocation: %@", NSStringFromPoint(popLocation)); - + return popLocation; } @@ -475,20 +476,21 @@ - (CGPoint)getMiniWindowLocation { if (EZConfiguration.shared.adjustPopButtomOrigin) { position.y = position.y - 8; } - - + + // If not query text, just show mini window, then show window at last position. if (!self.selectedText) { CGRect formerFrame = [EZLayoutManager.shared windowFrameWithType:EZWindowTypeMini]; position = [EZCoordinateUtils getFrameTopLeftPoint:formerFrame]; } - + return position; } - (CGPoint)getShowingMouseLocation { BOOL offsetFlag = self.popButtonWindow.isVisible; - return [self getMouseLocation:offsetFlag];; + return [self getMouseLocation:offsetFlag]; + ; } - (CGPoint)getMouseLocation:(BOOL)offsetFlag { @@ -496,24 +498,24 @@ - (CGPoint)getMouseLocation:(BOOL)offsetFlag { if (CGPointEqualToPoint(popButtonLocation, CGPointZero)) { return CGPointZero; } - + CGPoint mouseLocation = NSEvent.mouseLocation; CGPoint showingPosition = mouseLocation; - + if (offsetFlag) { CGFloat x = popButtonLocation.x + 5; // Move slightly to the right to avoid covering the cursor. - - + + // if pop button is left to selected text, we need to move showing mouse location to a bit right, to show query window properly. if (mouseLocation.x > popButtonLocation.x) { x = NSEvent.mouseLocation.x + 5; } - + CGFloat y = popButtonLocation.y + 0; - + showingPosition = CGPointMake(x, y); } - + return showingPosition; } @@ -547,29 +549,29 @@ - (CGPoint)getFixedWindowLocation { - (CGPoint)getFloatingWindowInRightSideOfScreenPoint:(NSWindow *)floatingWindow { CGPoint position = CGPointZero; - + NSScreen *targetScreen = self.screen; NSRect screenRect = [targetScreen visibleFrame]; - + CGFloat x = screenRect.origin.x + screenRect.size.width - floatingWindow.width; CGFloat y = screenRect.origin.y + screenRect.size.height; position = CGPointMake(x, y); - + return position; } /// Get the position of floatingWindow that make sure floatingWindow show in the center of self.screen. - (CGPoint)getFloatingWindowInCenterOfScreenPoint:(NSWindow *)floatingWindow { CGPoint position = CGPointZero; - + NSScreen *targetScreen = self.screen; NSRect screenRect = [targetScreen visibleFrame]; - + // top-left point CGFloat x = screenRect.origin.x + (screenRect.size.width - floatingWindow.width) / 2; CGFloat y = screenRect.origin.y + (screenRect.size.height - floatingWindow.height) / 2 + floatingWindow.height; position = CGPointMake(x, y); - + return position; } @@ -580,7 +582,7 @@ - (void)saveFrontmostApplication { if ([frontmostApplication.bundleIdentifier isEqualToString:identifier]) { return; } - + self.lastFrontmostApplication = frontmostApplication; } @@ -588,7 +590,7 @@ - (void)showMainWindowIfNedded { BOOL showFlag = !EZConfiguration.shared.hideMainWindow; NSApplicationActivationPolicy activationPolicy = showFlag ? NSApplicationActivationPolicyRegular : NSApplicationActivationPolicyAccessory; [NSApp setActivationPolicy:activationPolicy]; - + if (showFlag) { [self.floatingWindowTypeArray insertObject:@(EZWindowTypeMain) atIndex:0]; @@ -600,7 +602,7 @@ - (void)showMainWindowIfNedded { - (void)closeMainWindowIfNeeded { [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory]; - + [self.floatingWindowTypeArray removeObject:@(EZWindowTypeMain)]; if ([EZMainQueryWindow isAlive]) { @@ -617,16 +619,14 @@ - (void)selectTextTranslate { NSLog(@"App is not trusted"); return; } - + [self saveFrontmostApplication]; if (Snip.shared.isSnapshotting) { return; } - - EZWindowType windowType = EZWindowTypeFixed; - if ([EZConfiguration.shared selectUseMiniWindow]) { - windowType = EZWindowTypeMini; - } + + EZWindowType windowType = EZConfiguration.shared.shortcutSelectTranslateWindowType; + NSLog(@"selectTextTranslate windowType: %@", @(windowType)); self.eventMonitor.actionType = EZActionTypeShortcutQuery; [self.eventMonitor getSelectedText:^(NSString *_Nullable text) { // If text is nil, currently, we choose to clear input. @@ -642,22 +642,22 @@ - (void)snipTranslate { // if ([self hasEasydictRunningInDebugMode]) { // return; // } - + [self saveFrontmostApplication]; - + if (Snip.shared.isSnapshotting) { return; } - + // Since ocr detect may be inaccurate, sometimes need to set sourceLanguage manually, so show Fixed window. EZWindowType windowType = EZWindowTypeFixed; EZBaseQueryWindow *window = [self windowWithType:windowType]; - + // FIX https://github.com/tisfeng/Easydict/issues/126 if (!self.floatingWindow.pin) { [self closeFloatingWindow]; } - + // Wait to close floating window if need. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [Snip.shared startWithCompletion:^(NSImage *_Nullable image) { @@ -665,9 +665,9 @@ - (void)snipTranslate { NSLog(@"not get screenshot"); return; } - + NSLog(@"get screenshot: %@", image); - + // 缓存最后一张图片,统一放到 MMLogs 文件夹,方便管理 static NSString *_imagePath = nil; static dispatch_once_t onceToken; @@ -677,7 +677,7 @@ - (void)snipTranslate { [[NSFileManager defaultManager] removeItemAtPath:_imagePath error:nil]; [image mm_writeToFileAsPNG:_imagePath]; NSLog(@"已保存图片: %@", _imagePath); - + // Reset window height first, avoid being affected by previous window height. [window.queryViewController resetTableView:^{ [self showFloatingWindowType:windowType queryText:nil]; @@ -689,23 +689,23 @@ - (void)snipTranslate { - (void)inputTranslate { MMLogInfo(@"inputTranslate"); - + [self saveFrontmostApplication]; if (Snip.shared.isSnapshotting) { return; } - + EZWindowType windowType = EZWindowTypeFixed; if (self.floatingWindowType == windowType) { [self closeFloatingWindow]; return; } - + NSString *queryText = nil; if ([EZConfiguration.shared clearInput]) { queryText = @""; } - + self.actionType = EZActionTypeInputQuery; [self showFloatingWindowType:windowType queryText:queryText]; } @@ -719,7 +719,7 @@ - (void)showMiniFloatingWindow { [self closeFloatingWindow]; return; } - + self.actionType = EZActionTypeInputQuery; [self showFloatingWindowType:windowType queryText:nil]; } @@ -728,19 +728,19 @@ - (void)screenshotOCR { MMLogInfo(@"screenshotOCR"); [self saveFrontmostApplication]; - + if (Snip.shared.isSnapshotting) { return; } - + [Snip.shared startWithCompletion:^(NSImage *_Nullable image) { if (!image) { NSLog(@"not get screenshot"); return; } - + NSLog(@"get screenshot: %@", image); - + // 缓存最后一张图片,统一放到 MMLogs 文件夹,方便管理 static NSString *_imagePath = nil; static dispatch_once_t onceToken; @@ -750,7 +750,7 @@ - (void)screenshotOCR { [[NSFileManager defaultManager] removeItemAtPath:_imagePath error:nil]; [image mm_writeToFileAsPNG:_imagePath]; NSLog(@"已保存图片: %@", _imagePath); - + [self.backgroundQueryViewController startOCRImage:image actionType:EZActionTypeScreenshotOCR]; }]; } @@ -761,7 +761,7 @@ - (void)rerty { if (Snip.shared.isSnapshotting) { return; } - + if ([[NSApplication sharedApplication] keyWindow] == self.floatingWindow) { // 执行重试 [self.floatingWindow.queryViewController retryQuery]; @@ -770,13 +770,13 @@ - (void)rerty { - (void)clearInput { NSLog(@"Clear input"); - + [self.floatingWindow.queryViewController clearInput]; } - (void)clearAll { NSLog(@"Clear All"); - + [self.floatingWindow.queryViewController clearAll]; } @@ -790,14 +790,14 @@ - (void)copyFirstTranslatedText { - (void)pin { NSLog(@"Pin"); - + EZBaseQueryWindow *queryWindow = EZWindowManager.shared.floatingWindow; queryWindow.titleBar.pin = !queryWindow.titleBar.pin; } - (void)closeWindowOrExitSreenshot { NSLog(@"Close window, or exit screenshot"); - + if (Snip.shared.isSnapshotting) { [Snip.shared stop]; } else { @@ -826,28 +826,28 @@ - (void)playOrStopQueryTextAudio { /// Close floating window, and record last floating window type. - (void)closeFloatingWindow { NSLog(@"close floating window: %@", self.floatingWindow); - + if (!self.floatingWindow) { return; } - + // stop playing audio [self.floatingWindow.queryViewController stopPlayingAudio]; - + self.floatingWindow.titleBar.pin = NO; [self.floatingWindow close]; - + if (![EZPreferencesWindowController.shared isShowing]) { // recover last app. [self activeLastFrontmostApplication]; } - + if ([EZMainQueryWindow isAlive]) { [self.mainWindow orderBack:nil]; } - + // Move floating window type to second. - + NSNumber *windowType = @(self.floatingWindowType); [self.floatingWindowTypeArray removeObject:windowType]; [self.floatingWindowTypeArray insertObject:windowType atIndex:1]; @@ -893,17 +893,17 @@ - (BOOL)isAppRunningWithBundleId:(NSString *)bundleID { - (void)logSelectedTextEvent { NSString *text = self.selectedText; - + if (!text) { return; } - + NSRunningApplication *application = self.eventMonitor.frontmostApplication; NSString *appName = application.localizedName; NSString *bundleID = application.bundleIdentifier; NSString *textLength = [EZLog textLengthRange:text]; NSString *triggerType = [EZEnumTypes stringValueOfTriggerType:self.eventMonitor.triggerType]; - + NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{ @"actionType" : self.actionType, @"selectTextType" : self.eventMonitor.selectTextType, @@ -912,14 +912,14 @@ - (void)logSelectedTextEvent { @"appName" : appName, @"bundleID" : bundleID, }]; - + NSString *browserTabURLString = self.eventMonitor.browserTabURLString; if (browserTabURLString.length) { NSURL *tabURL = [NSURL URLWithString:browserTabURLString]; NSString *host = tabURL.host ?: browserTabURLString; dict[@"host"] = host; } - + [EZLog logEventWithName:@"getSelectedText" parameters:dict]; } diff --git a/en.lproj/Localizable.strings b/en.lproj/Localizable.strings index 5f3f6e4ea..85534fb4b 100644 --- a/en.lproj/Localizable.strings +++ b/en.lproj/Localizable.strings @@ -26,8 +26,6 @@ "force_auto_get_selected_text_title" = "Force Auto Get Selected Text"; "force_auto_get_selected_text_msg" = "Note: In some applications, forced word retrieval may cause empty copy prompt tones, abnormal clipboard content, etc."; -"use_mini_window" = "Use mini window"; - "disable_empty_copy_beep" = "Disable Empty Copy Beep:"; "disable_empty_copy_beep_msg" = "Activated when selected text is empty"; @@ -48,6 +46,12 @@ "fixed_window_position_former" = "Last position"; "fixed_window_position_center" = "Center of Screen"; +"shortcut_select_translate_window_type" = "Shortcut Select Translate Window:"; +"mouse_select_translate_window_type" = "Mouse Select Translate Window:"; +"tranalte_window_type_main" = "Main window"; +"tranalte_window_type_fixed" = "Fixed float window"; +"tranalte_window_type_mini" = "Mini window"; + "play_word_audio" = "Play Word Pronunciation:"; "auto_play_word_audio" = "Auto play pronunciation after querying English words"; diff --git a/zh-Hans.lproj/Localizable.strings b/zh-Hans.lproj/Localizable.strings index 7ca9c2b7e..150eaf423 100644 --- a/zh-Hans.lproj/Localizable.strings +++ b/zh-Hans.lproj/Localizable.strings @@ -26,8 +26,6 @@ "force_auto_get_selected_text_title" = "强制划词"; "force_auto_get_selected_text_msg" = "注意:在某些应用中,强制划词可能会导致空复制提示音,影响剪贴板内容等异常情况。"; -"use_mini_window" = "使用迷你窗口"; - "disable_empty_copy_beep" = "禁用空复制提示音:"; "disable_empty_copy_beep_msg" = "划词内容为空时生效"; @@ -48,6 +46,12 @@ "fixed_window_position_former" = "上次位置"; "fixed_window_position_center" = "屏幕中间"; +"shortcut_select_translate_window_type" = "快捷键划词窗口:"; +"mouse_select_translate_window_type" = "鼠标划词窗口:"; +"tranalte_window_type_main" = "主窗口"; +"tranalte_window_type_fixed" = "侧悬浮窗口"; +"tranalte_window_type_mini" = "迷你窗口"; + "play_word_audio" = "播放单词发音:"; "auto_play_word_audio" = "查询英语单词后自动播放发音";