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
  • Loading branch information
yam-liu committed Jan 31, 2024
1 parent bece937 commit 70a6f62
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 5 deletions.
36 changes: 35 additions & 1 deletion Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,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 @@ -3467,4 +3501,4 @@
}
},
"version" : "1.0"
}
}
3 changes: 3 additions & 0 deletions Easydict/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ let kHideMenuBarIconKey = "EZConfiguration_kHideMenuBarIconKey"
@DefaultsWrapper(.clearInput)
var clearInput: Bool

@DefaultsWrapper(.keepPrevResultWhenEmpty)
var keepPrevResultWhenEmpty: 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
28 changes: 26 additions & 2 deletions Easydict/Feature/PerferenceWindow/EZSettingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ @interface EZSettingViewController () <NSComboBoxDelegate>
@property (nonatomic, strong) NSTextField *clearInputLabel;
@property (nonatomic, strong) NSButton *clearInputButton;

@property (nonatomic, strong) NSTextField *keepPrevResultLabel;
@property (nonatomic, strong) NSButton *keepPrevResultButton;

@property (nonatomic, strong) NSTextField *autoQueryLabel;
@property (nonatomic, strong) NSButton *autoQueryOCRTextButton;
@property (nonatomic, strong) NSButton *autoQuerySelectedTextButton;
Expand Down Expand Up @@ -403,6 +406,14 @@ - (void)setupUI {
NSString *clearInputTitle = NSLocalizedString(@"clear_input_when_translating", nil);
self.clearInputButton = [NSButton checkboxWithTitle:clearInputTitle target:self action:@selector(clearInputButtonClicked:)];
[self.contentView addSubview:self.clearInputButton];

self.keepPrevResultLabel = [NSTextField labelWithString:NSLocalizedString(@"keep_result", nil)];
self.keepPrevResultLabel.font = font;
[self.contentView addSubview:self.keepPrevResultLabel];

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];

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

self.autoPlayAudioButton.mm_isOn = self.config.autoPlayAudio;
self.clearInputButton.mm_isOn = self.config.clearInput;
self.keepPrevResultButton.mm_isOn = self.config.keepPrevResultWhenEmpty;
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 @@ -753,11 +765,19 @@ - (void)updateViewConstraints {
make.left.equalTo(self.clearInputLabel.mas_right).offset(self.horizontalPadding);
make.centerY.equalTo(self.clearInputLabel);
}];


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

[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.keepPrevResultButton.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,10 @@ - (void)clearInputButtonClicked:(NSButton *)sender {
self.config.clearInput = sender.mm_isOn;
}

- (void)keepPrevResultButtonClicked:(NSButton *)sender {
self.config.keepPrevResultWhenEmpty = 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,7 @@ - (void)focusInputTextView {
[NSApp activateIgnoringOtherApps:YES];

[self.baseQueryWindow makeFirstResponder:self.queryView.textView];
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
1 change: 1 addition & 0 deletions Easydict/NewApp/Configuration/Configuration+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ 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 enableBetaNewApp = Key<Bool>("EZConfiguration_kEnableBetaNewAppKey", default: false)

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

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

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

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

0 comments on commit 70a6f62

Please sign in to comment.