Skip to content

Commit

Permalink
feat(Select Translate): keep previous result if selected text is empt…
Browse files Browse the repository at this point in the history
…y when open Select Translate (#375)

* feat(Select Translate): keep previous result if selected text is empty when open Select Translate

* feat(Select Translate): make auto select query text behavior when window got activated a standalone option

* feat(Select Translate): Update Chinese value for key "select_query_text_when_window_activate" in Localizable.xcstrings

Co-authored-by: Tisfeng <[email protected]>

---------

Co-authored-by: Tisfeng <[email protected]>
  • Loading branch information
yam-liu and tisfeng authored Feb 4, 2024
1 parent 35e600b commit 2b00b93
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 28 deletions.
67 changes: 51 additions & 16 deletions Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -635,22 +635,6 @@
}
}
},
"clear_input" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Clear Input:"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "清空查询内容:"
}
}
}
},
"clear_input_when_translating" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -1548,6 +1532,40 @@
}
}
},
"keep_prev_result_when_selected_text_is_empty" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Keep previous result when selected text is empty"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "划词翻译未选中文本时,保留上次结果"
}
}
}
},
"keep_result" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Keep Result:"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "保留结果:"
}
}
}
},
"language_detect_optimize" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -2347,6 +2365,23 @@
}
}
},
"select_query_text_when_window_activate" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Select query text when window activate"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "needs_review",
"value" : "打开窗口时自动选中查询文本"
}
}
}
},
"select_translate" : {
"localizations" : {
"en" : {
Expand Down
6 changes: 6 additions & 0 deletions Easydict/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ let kHideMenuBarIconKey = "EZConfiguration_kHideMenuBarIconKey"
@DefaultsWrapper(.clearInput)
var clearInput: Bool

@DefaultsWrapper(.keepPrevResultWhenEmpty)
var keepPrevResultWhenEmpty: Bool

@DefaultsWrapper(.selectQueryTextWhenWindowActivate)
var selectQueryTextWhenWindowActivate: Bool

var disabledAutoSelect: Bool = false

var isRecordingSelectTextShortcutKey: Bool = false
Expand Down
1 change: 1 addition & 0 deletions Easydict/Feature/Configuration/EZConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ typedef NS_ENUM(NSUInteger, EZAppearenceType) {
@property (nonatomic, assign) BOOL allowCrashLog;
@property (nonatomic, assign) BOOL allowAnalytics;
@property (nonatomic, assign) BOOL clearInput;
@property (nonatomic, assign) BOOL keepPrevResult;

// TODO: Need to move them. These are read/write properties only and will not be stored locally, only for external use.
/// Only use when showing NSOpenPanel to select disabled apps.
Expand Down
10 changes: 10 additions & 0 deletions Easydict/Feature/Configuration/EZConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
static NSString *const kAllowCrashLogKey = @"EZConfiguration_kAllowCrashLogKey";
static NSString *const kAllowAnalyticsKey = @"EZConfiguration_kAllowAnalyticsKey";
static NSString *const kClearInputKey = @"EZConfiguration_kClearInputKey";
static NSString *const kKeepPrevResultKey = @"EZConfiguration_kKeepPrevResultKey";
static NSString *const kTranslationControllerFontKey = @"EZConfiguration_kTranslationControllerFontKey";
static NSString *const kApperanceKey = @"EZConfiguration_kApperanceKey";

Expand Down Expand Up @@ -126,6 +127,7 @@ - (void)setup {
self.allowCrashLog = [NSUserDefaults mm_readBool:kAllowCrashLogKey defaultValue:YES];
self.allowAnalytics = [NSUserDefaults mm_readBool:kAllowAnalyticsKey defaultValue:YES];
self.clearInput = [NSUserDefaults mm_readBool:kClearInputKey defaultValue:NO];
self.keepPrevResult = [NSUserDefaults mm_readBool:kKeepPrevResultKey defaultValue:YES];

self.fontSizes = @[@(1), @(1.1), @(1.2), @(1.3), @(1.4)];
[[NSUserDefaults standardUserDefaults]registerDefaults:@{kTranslationControllerFontKey: @(0)}];
Expand Down Expand Up @@ -432,6 +434,14 @@ - (void)setClearInput:(BOOL)clearInput {
[self logSettings:@{@"clear_input" : @(clearInput)}];
}

- (void)setKeepPrevResult:(BOOL)keepPrevResult {
_keepPrevResult = keepPrevResult;

[NSUserDefaults mm_write:@(keepPrevResult) forKey:kKeepPrevResultKey];

[self logSettings:@{@"keep_prev_result": @(keepPrevResult)}];
}

- (void)setFontSizeIndex:(NSInteger)fontSizeIndex {
NSInteger targetIndex = MIN(_fontSizes.count-1, MAX(fontSizeIndex, 0));

Expand Down
48 changes: 38 additions & 10 deletions Easydict/Feature/PerferenceWindow/EZSettingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ @interface EZSettingViewController () <NSComboBoxDelegate>
@property (nonatomic, strong) NSTextField *playAudioLabel;
@property (nonatomic, strong) NSButton *autoPlayAudioButton;

@property (nonatomic, strong) NSTextField *clearInputLabel;
@property (nonatomic, strong) NSTextField *inputFieldLabel;
@property (nonatomic, strong) NSButton *clearInputButton;
@property (nonatomic, strong) NSButton *keepPrevResultButton;
@property (nonatomic, strong) NSButton *selectQueryTextWhenWindowActivateButton;

@property (nonatomic, strong) NSTextField *autoQueryLabel;
@property (nonatomic, strong) NSButton *autoQueryOCRTextButton;
Expand Down Expand Up @@ -395,14 +397,23 @@ - (void)setupUI {
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 *inputFieldLabelTitle = [NSString stringWithFormat:@"%@:", NSLocalizedString(@"setting.general.input.header", nil)];
NSTextField *inputFieldLabel = [NSTextField labelWithString:inputFieldLabelTitle];
inputFieldLabel.font = font;
[self.contentView addSubview:inputFieldLabel];
self.inputFieldLabel = inputFieldLabel;

NSString *clearInputTitle = NSLocalizedString(@"clear_input_when_translating", nil);
self.clearInputButton = [NSButton checkboxWithTitle:clearInputTitle target:self action:@selector(clearInputButtonClicked:)];
[self.contentView addSubview:self.clearInputButton];

NSString *keepPrevResultTitle = NSLocalizedString(@"keep_prev_result_when_selected_text_is_empty", nil);
self.keepPrevResultButton = [NSButton checkboxWithTitle:keepPrevResultTitle target:self action:@selector(keepPrevResultButtonClicked:)];
[self.contentView addSubview:self.keepPrevResultButton];

NSString *selectQueryTextWhenWindowActivateTitle = NSLocalizedString(@"select_query_text_when_window_activate", nil);
self.selectQueryTextWhenWindowActivateButton = [NSButton checkboxWithTitle:selectQueryTextWhenWindowActivateTitle target:self action:@selector(selectQueryTextWhenWindowActivateButtonClicked:)];
[self.contentView addSubview:self.selectQueryTextWhenWindowActivateButton];

NSTextField *autoQueryLabel = [NSTextField labelWithString:NSLocalizedString(@"auto_query", nil)];
autoQueryLabel.font = font;
Expand Down Expand Up @@ -546,6 +557,8 @@ - (void)setupUI {

self.autoPlayAudioButton.mm_isOn = self.config.autoPlayAudio;
self.clearInputButton.mm_isOn = self.config.clearInput;
self.keepPrevResultButton.mm_isOn = self.config.keepPrevResultWhenEmpty;
self.selectQueryTextWhenWindowActivateButton.mm_isOn = self.config.selectQueryTextWhenWindowActivate;
self.launchAtStartupButton.mm_isOn = self.config.launchAtStartup;
self.hideMainWindowButton.mm_isOn = self.config.hideMainWindow;
self.autoQueryOCRTextButton.mm_isOn = self.config.autoQueryOCRText;
Expand Down Expand Up @@ -747,19 +760,26 @@ - (void)updateViewConstraints {
}];


[self.clearInputLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
[self.inputFieldLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.autoGetSelectedTextLabel);
make.top.equalTo(self.autoPlayAudioButton.mas_bottom).offset(self.verticalPadding);
}];
[self.clearInputButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.clearInputLabel.mas_right).offset(self.horizontalPadding);
make.centerY.equalTo(self.clearInputLabel);
make.left.equalTo(self.inputFieldLabel.mas_right).offset(self.horizontalPadding);
make.centerY.equalTo(self.inputFieldLabel);
}];
[self.keepPrevResultButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.clearInputButton);
make.top.equalTo(self.clearInputButton.mas_bottom).offset(self.verticalPadding);
}];
[self.selectQueryTextWhenWindowActivateButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.clearInputButton);
make.top.equalTo(self.keepPrevResultButton.mas_bottom).offset(self.verticalPadding);
}];


[self.autoQueryLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.autoGetSelectedTextLabel);
make.top.equalTo(self.clearInputButton.mas_bottom).offset(self.verticalPadding);
make.top.equalTo(self.selectQueryTextWhenWindowActivateButton.mas_bottom).offset(self.verticalPadding);
}];
[self.autoQueryOCRTextButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.autoQueryLabel.mas_right).offset(self.horizontalPadding);
Expand Down Expand Up @@ -967,6 +987,14 @@ - (void)clearInputButtonClicked:(NSButton *)sender {
self.config.clearInput = sender.mm_isOn;
}

- (void)keepPrevResultButtonClicked:(NSButton *)sender {
self.config.keepPrevResultWhenEmpty = sender.mm_isOn;
}

- (void)selectQueryTextWhenWindowActivateButtonClicked:(NSButton *)sender {
self.config.selectQueryTextWhenWindowActivate = sender.mm_isOn;
}

- (void)autoCopySelectedTextButtonClicked:(NSButton *)sender {
self.config.autoCopySelectedText = sender.mm_isOn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ - (void)focusInputTextView {
[NSApp activateIgnoringOtherApps:YES];

[self.baseQueryWindow makeFirstResponder:self.queryView.textView];
if (Configuration.shared.selectQueryTextWhenWindowActivate) {
self.queryView.textView.selectedRange = NSMakeRange(0, self.inputText.length);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,14 @@ - (void)selectTextTranslate {
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;

// Clear query if text is nil and user don't want to keep the last result.
if (!text && !Configuration.shared.keepPrevResultWhenEmpty) {
text = @"";
}
self.selectedText = [text trim];

[self showFloatingWindowType:windowType queryText:self.selectedText];
}];
}
Expand Down
2 changes: 2 additions & 0 deletions Easydict/NewApp/Configuration/Configuration+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ extension Defaults.Keys {
static let allowCrashLog = Key<Bool>("EZConfiguration_kAllowCrashLogKey", default: true)
static let allowAnalytics = Key<Bool>("EZConfiguration_kAllowAnalyticsKey", default: true)
static let clearInput = Key<Bool>("EZConfiguration_kClearInputKey", default: true)
static let keepPrevResultWhenEmpty = Key<Bool>("EZConfiguration_kKeepPrevResultKey", default: true)
static let selectQueryTextWhenWindowActivate = Key<Bool>("EZConfiguration_kSelectQueryTextWhenWindowActivate", default: false)
static let enableBetaNewApp = Key<Bool>("EZConfiguration_kEnableBetaNewAppKey", default: false)

static let enableBetaFeature = Key<Bool>("EZBetaFeatureKey", default: false)
Expand Down
4 changes: 4 additions & 0 deletions Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ struct GeneralTab: View {

Section {
Toggle("clear_input_when_translating", isOn: $clearInput)
Toggle("keep_prev_result_when_selected_text_is_empty", isOn: $keepPrevResultWhenEmpty)
Toggle("select_query_text_when_window_activate", isOn: $selectQueryTextWhenWindowActivate)
} header: {
Text("setting.general.input.header")
}
Expand Down Expand Up @@ -161,6 +163,8 @@ struct GeneralTab: View {
@Default(.adjustPopButtonOrigin) private var adjustPopButtonOrigin

@Default(.clearInput) private var clearInput
@Default(.keepPrevResultWhenEmpty) private var keepPrevResultWhenEmpty
@Default(.selectQueryTextWhenWindowActivate) private var selectQueryTextWhenWindowActivate

@Default(.disableEmptyCopyBeep) private var disableEmptyCopyBeep
@Default(.autoPlayAudio) private var autoPlayAudio
Expand Down

0 comments on commit 2b00b93

Please sign in to comment.