Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: choose window type for shortcut and mouse select translate #156

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -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" : {
Expand Down Expand Up @@ -2013,4 +2093,4 @@
}
},
"version" : "1.0"
}
}
2 changes: 2 additions & 0 deletions Easydict/Feature/Configuration/EZConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 32 additions & 17 deletions Easydict/Feature/Configuration/EZConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";
Expand All @@ -56,7 +59,7 @@ @implementation EZConfiguration
static EZConfiguration *_instance;

+ (instancetype)shared {
@synchronized (self) {
@synchronized(self) {
if (!_instance) {
_instance = [[super allocWithZone:NULL] init];
[_instance setup];
Expand All @@ -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];

Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -191,7 +196,7 @@ - (void)setHideMainWindow:(BOOL)hideMainWindow {
_hideMainWindow = hideMainWindow;

[NSUserDefaults mm_write:@(hideMainWindow) forKey:kHideMainWindowKey];

EZWindowManager *windowManager = EZWindowManager.shared;
[windowManager updatePopButtonQueryAction];
if (hideMainWindow) {
Expand Down Expand Up @@ -258,7 +263,7 @@ - (void)setShowGoogleQuickLink:(BOOL)showGoogleLink {

[NSUserDefaults mm_write:@(showGoogleLink) forKey:kShowGoogleLinkKey];
[self postUpdateQuickLinkButtonNotification];

EZMenuItemManager.shared.googleItem.hidden = !showGoogleLink;
}

Expand All @@ -267,7 +272,7 @@ - (void)setShowEudicQuickLink:(BOOL)showEudicLink {

[NSUserDefaults mm_write:@(showEudicLink) forKey:kShowEudicLinkKey];
[self postUpdateQuickLinkButtonNotification];

EZMenuItemManager.shared.eudicItem.hidden = !showEudicLink;
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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\
Expand All @@ -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 {
Expand All @@ -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
//
Expand All @@ -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);
Expand Down
Loading