Skip to content

Commit

Permalink
feat: choose window type for shortcut and mouse select translate
Browse files Browse the repository at this point in the history
  • Loading branch information
rainzm committed Sep 30, 2023
1 parent 1c0ad7f commit 194b798
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 305 deletions.
3 changes: 2 additions & 1 deletion Easydict/Feature/Configuration/EZConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
57 changes: 32 additions & 25 deletions Easydict/Feature/Configuration/EZConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -55,7 +57,7 @@ @implementation EZConfiguration
static EZConfiguration *_instance;

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

Expand All @@ -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];
Expand All @@ -195,7 +192,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 @@ -256,7 +253,7 @@ - (void)setShowGoogleQuickLink:(BOOL)showGoogleLink {

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

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

Expand All @@ -265,7 +262,7 @@ - (void)setShowEudicQuickLink:(BOOL)showEudicLink {

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

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

Expand All @@ -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;

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

0 comments on commit 194b798

Please sign in to comment.