From c6b49fdbd5752de75f3281aa6a6ef10c4f401815 Mon Sep 17 00:00:00 2001 From: Rain Zheng Date: Wed, 4 Oct 2023 20:17:38 +0800 Subject: [PATCH] feat: choose window type for shortcut and mouse select translate (#156) --- Easydict/App/Localizable.xcstrings | 82 ++++- .../Feature/Configuration/EZConfiguration.h | 2 + .../Feature/Configuration/EZConfiguration.m | 49 +-- .../EZSettingViewController.m | 286 +++++++++++------- Easydict/Feature/Service/Model/EZEnumTypes.h | 10 +- Easydict/Feature/Service/Model/EZEnumTypes.m | 26 +- .../Window/WindowManager/EZWindowManager.m | 246 +++++++-------- 7 files changed, 435 insertions(+), 266 deletions(-) diff --git a/Easydict/App/Localizable.xcstrings b/Easydict/App/Localizable.xcstrings index 2c649542c..d71257fb2 100644 --- a/Easydict/App/Localizable.xcstrings +++ b/Easydict/App/Localizable.xcstrings @@ -935,6 +935,86 @@ } } }, + "shortcut_select_translate_window_type" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Shortcut Select Translate Window:" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "悬浮窗口位置:" + } + } + } + }, + "mouse_select_translate_window_type" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Mouse Select Translate Window:" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "鼠标划词窗口:" + } + } + } + }, + "tranalte_window_type_main" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Main window" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "主窗口" + } + } + } + }, + "tranalte_window_type_fixed" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Fixed float window" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "侧悬浮窗口" + } + } + } + }, + "tranalte_window_type_mini" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Mini window" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "迷你窗口" + } + } + } + }, "force_auto_get_selected_text" : { "localizations" : { "en" : { @@ -2013,4 +2093,4 @@ } }, "version" : "1.0" -} \ No newline at end of file +} diff --git a/Easydict/Feature/Configuration/EZConfiguration.h b/Easydict/Feature/Configuration/EZConfiguration.h index d9cc80e89..d13985b3b 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.h +++ b/Easydict/Feature/Configuration/EZConfiguration.h @@ -51,6 +51,8 @@ typedef NS_ENUM(NSUInteger, EZLanguageDetectOptimize) { @property (nonatomic, assign) BOOL showAppleDictionaryQuickLink; @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 1cab822c4..0ed4f316a 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.m +++ b/Easydict/Feature/Configuration/EZConfiguration.m @@ -20,6 +20,7 @@ static NSString *const kFirstLanguageKey = @"EZConfiguration_kFirstLanguageKey"; static NSString *const kSecondLanguageKey = @"EZConfiguration_kSecondLanguageKey"; + static NSString *const kFromKey = @"EZConfiguration_kFromKey"; static NSString *const kToKey = @"EZConfiguration_kToKey"; @@ -43,6 +44,8 @@ static NSString *const kShowAppleDictionaryLinkKey = @"EZConfiguration_kShowAppleDictionaryLinkKey"; 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"; @@ -56,7 +59,7 @@ @implementation EZConfiguration static EZConfiguration *_instance; + (instancetype)shared { - @synchronized (self) { + @synchronized(self) { if (!_instance) { _instance = [[super allocWithZone:NULL] init]; [_instance setup]; @@ -76,7 +79,7 @@ + (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]; @@ -100,6 +103,8 @@ - (void)setup { self.showAppleDictionaryQuickLink = [NSUserDefaults mm_readBool:kShowAppleDictionaryLinkKey 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]; @@ -166,15 +171,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]; @@ -191,7 +196,7 @@ - (void)setHideMainWindow:(BOOL)hideMainWindow { _hideMainWindow = hideMainWindow; [NSUserDefaults mm_write:@(hideMainWindow) forKey:kHideMainWindowKey]; - + EZWindowManager *windowManager = EZWindowManager.shared; [windowManager updatePopButtonQueryAction]; if (hideMainWindow) { @@ -258,7 +263,7 @@ - (void)setShowGoogleQuickLink:(BOOL)showGoogleLink { [NSUserDefaults mm_write:@(showGoogleLink) forKey:kShowGoogleLinkKey]; [self postUpdateQuickLinkButtonNotification]; - + EZMenuItemManager.shared.googleItem.hidden = !showGoogleLink; } @@ -267,7 +272,7 @@ - (void)setShowEudicQuickLink:(BOOL)showEudicLink { [NSUserDefaults mm_write:@(showEudicLink) forKey:kShowEudicLinkKey]; [self postUpdateQuickLinkButtonNotification]; - + EZMenuItemManager.shared.eudicItem.hidden = !showEudicLink; } @@ -295,6 +300,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; @@ -347,7 +364,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\ @@ -360,14 +377,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 { @@ -390,7 +405,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 // @@ -409,7 +424,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 aae84cae5..a06a94f69 100644 --- a/Easydict/Feature/PerferenceWindow/EZSettingViewController.m +++ b/Easydict/Feature/PerferenceWindow/EZSettingViewController.m @@ -60,6 +60,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; @@ -119,71 +125,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; @@ -193,81 +199,80 @@ - (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 *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]; - + // language detect 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), @@ -276,16 +281,16 @@ - (void)setupUI { [self.languageDetectOptimizePopUpButton addItemsWithTitles:languageDetectOptimizeItems]; self.languageDetectOptimizePopUpButton.target = self; self.languageDetectOptimizePopUpButton.action = @selector(languageDetectOptimizePopUpButtonClicked:); - + // default tts service NSTextField *defaultTTSServiceLabel = [NSTextField labelWithString:NSLocalizedString(@"default_tts_service", nil)]; defaultTTSServiceLabel.font = font; [self.contentView addSubview:defaultTTSServiceLabel]; self.defaultTTSServiceLabel = defaultTTSServiceLabel; - + self.defaultTTSServicePopUpButton = [[NSPopUpButton alloc] init]; [self.contentView addSubview:self.defaultTTSServicePopUpButton]; - + // Note: Bing API has frequency limit NSArray *enabledTTSServiceTypes = @[ EZServiceTypeYoudao, @@ -297,13 +302,12 @@ - (void)setupUI { [self.defaultTTSServicePopUpButton addItemsWithTitles:enabledTTSServiceTypes]; self.defaultTTSServicePopUpButton.target = self; self.defaultTTSServicePopUpButton.action = @selector(defaultTTSServicePopUpButtonClicked:); - - + 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]; @@ -311,80 +315,105 @@ - (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]; - + NSString *showAppleDictionaryQuickLink = NSLocalizedString(@"show_apple_dictionary_quick_link", nil); self.showAppleDictionaryQuickLinkButton = [NSButton checkboxWithTitle:showAppleDictionaryQuickLink target:self action:@selector(showAppleDictionaryQuickLinkButtonClicked:)]; [self.contentView addSubview:self.showAppleDictionaryQuickLinkButton]; - - + + NSView *separatorView2 = [[NSView alloc] init]; [self.contentView addSubview:separatorView2]; self.separatorView2 = separatorView2; @@ -394,37 +423,37 @@ - (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.disableEmptyCopyBeepButton.mm_isOn = self.config.disableEmptyCopyBeep; @@ -433,6 +462,8 @@ - (void)setupUI { [self.languageDetectOptimizePopUpButton selectItemAtIndex:self.config.languageDetectOptimize]; [self.defaultTTSServicePopUpButton selectItemWithTitle:self.config.defaultTTSServiceType]; [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; @@ -451,7 +482,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(); @@ -461,7 +492,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); @@ -471,7 +502,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); @@ -481,7 +512,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); @@ -491,7 +522,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); @@ -501,14 +532,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); @@ -517,7 +548,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); @@ -526,7 +557,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); @@ -539,7 +570,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.showQueryIconButton); make.top.equalTo(self.showQueryIconButton.mas_bottom).offset(self.verticalPadding); }]; - + [self.disableEmptyCopyBeepLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.forceGetSelectedTextButton.mas_bottom).offset(self.verticalPadding); @@ -548,7 +579,7 @@ - (void)updateViewConstraints { 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); @@ -557,8 +588,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); @@ -567,7 +598,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); @@ -576,7 +607,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.languageDetectLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.languageDetectLabel); }]; - + [self.defaultTTSServiceLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.languageDetectOptimizePopUpButton.mas_bottom).offset(self.verticalPadding); @@ -585,7 +616,7 @@ - (void)updateViewConstraints { make.left.equalTo(self.defaultTTSServiceLabel.mas_right).offset(self.horizontalPadding); make.centerY.equalTo(self.defaultTTSServiceLabel); }]; - + [self.fixedWindowPositionLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.autoGetSelectedTextLabel); make.top.equalTo(self.defaultTTSServicePopUpButton.mas_bottom).offset(self.verticalPadding); @@ -594,17 +625,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); @@ -613,8 +662,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); @@ -631,8 +680,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); @@ -649,8 +698,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); @@ -667,14 +716,13 @@ - (void)updateViewConstraints { make.left.equalTo(self.showEudicQuickLinkButton); make.top.equalTo(self.showEudicQuickLinkButton.mas_bottom).offset(self.verticalPadding); }]; - - + [self.separatorView2 mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.separatorView); make.top.equalTo(self.showAppleDictionaryQuickLinkButton.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); @@ -683,7 +731,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); @@ -692,7 +740,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); @@ -701,20 +749,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]; } @@ -723,13 +771,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]; } @@ -835,6 +883,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; @@ -864,14 +922,14 @@ - (void)firstLangaugePopUpButtonClicked:(NSPopUpButton *)button { NSInteger selectedIndex = button.indexOfSelectedItem; EZLanguage language = self.allLanguageDict.sortedKeys[selectedIndex]; self.config.firstLanguage = language; - + [self checkIfEqualFirstLanguage:YES]; } - (void)secondLangaugePopUpButtonClicked:(NSPopUpButton *)button { NSInteger selectedIndex = button.indexOfSelectedItem; EZLanguage language = self.allLanguageDict.sortedKeys[selectedIndex]; self.config.secondLanguage = language; - + [self checkIfEqualFirstLanguage:NO]; } @@ -879,7 +937,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]; @@ -888,13 +946,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]; } }]; @@ -904,7 +962,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 ee1fc3854..e246fe6cd 100644 --- a/Easydict/Feature/Service/Model/EZEnumTypes.h +++ b/Easydict/Feature/Service/Model/EZEnumTypes.h @@ -44,10 +44,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 }; @@ -90,7 +90,7 @@ typedef NS_OPTIONS(NSUInteger, EZTriggerType) { }; -@interface EZEnumTypes: NSObject +@interface EZEnumTypes : NSObject + (NSString *)stringValueOfTriggerType:(EZTriggerType)triggerType; @@ -98,6 +98,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 578fb96be..53bf201e7 100644 --- a/Easydict/Feature/Service/Model/EZEnumTypes.m +++ b/Easydict/Feature/Service/Model/EZEnumTypes.m @@ -73,15 +73,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 b82182023..e91a25203 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,18 +619,20 @@ - (void)selectTextTranslate { NSLog(@"App is not trusted"); return; } - + [self saveFrontmostApplication]; if (Snip.shared.isSnapshotting) { return; } - + + 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. self.selectedText = [text trim] ?: @""; self.actionType = self.eventMonitor.actionType; - [self showFloatingWindowType:EZWindowTypeFixed queryText:self.selectedText]; + [self showFloatingWindowType:windowType queryText:self.selectedText]; }]; } @@ -638,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) { @@ -661,9 +665,9 @@ - (void)snipTranslate { NSLog(@"not get screenshot"); return; } - + NSLog(@"get screenshot: %@", image); - + // 缓存最后一张图片,统一放到 MMLogs 文件夹,方便管理 static NSString *_imagePath = nil; static dispatch_once_t onceToken; @@ -673,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]; @@ -685,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]; } @@ -715,7 +719,7 @@ - (void)showMiniFloatingWindow { [self closeFloatingWindow]; return; } - + self.actionType = EZActionTypeInputQuery; [self showFloatingWindowType:windowType queryText:nil]; } @@ -724,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; @@ -746,7 +750,7 @@ - (void)screenshotOCR { [[NSFileManager defaultManager] removeItemAtPath:_imagePath error:nil]; [image mm_writeToFileAsPNG:_imagePath]; NSLog(@"已保存图片: %@", _imagePath); - + [self.backgroundQueryViewController startOCRImage:image actionType:EZActionTypeScreenshotOCR]; }]; } @@ -757,7 +761,7 @@ - (void)rerty { if (Snip.shared.isSnapshotting) { return; } - + if ([[NSApplication sharedApplication] keyWindow] == self.floatingWindow) { // 执行重试 [self.floatingWindow.queryViewController retryQuery]; @@ -766,13 +770,13 @@ - (void)rerty { - (void)clearInput { NSLog(@"Clear input"); - + [self.floatingWindow.queryViewController clearInput]; } - (void)clearAll { NSLog(@"Clear All"); - + [self.floatingWindow.queryViewController clearAll]; } @@ -786,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 { @@ -822,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 stopPlayingQueryText]; - + 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]; @@ -889,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, @@ -908,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]; }