Skip to content

Commit

Permalink
Change the actions in the quick settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverAgain11 committed Mar 5, 2024
1 parent 196b74b commit e844216
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 22 deletions.
34 changes: 34 additions & 0 deletions Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@
}
},
"automatic_word_segmentation" : {
"extractionState" : "stale",
"localizations" : {
"en" : {
"stringUnit" : {
Expand All @@ -502,6 +503,7 @@
}
},
"automatically_remove_code_comment_symbols" : {
"extractionState" : "stale",
"localizations" : {
"en" : {
"stringUnit" : {
Expand Down Expand Up @@ -2383,6 +2385,22 @@
}
}
},
"remove_code_comment_symbols" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Remove code comment symbols「/*#」"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "去除代码注释符号「/*#」"
}
}
}
},
"replace_text" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -4241,6 +4259,22 @@
}
}
},
"word_segmentation" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Word splitting: key_value —> key value"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "单词分词 key_value —> key value"
}
}
}
},
"Youdao" : {
"extractionState" : "manual",
"localizations" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ NS_ASSUME_NONNULL_BEGIN
/// Check if all line starts with a comment symbol, #,//,*
- (BOOL)allLineStartsWithCommentSymbol;

/**
Split camel and snake case text
https://github.com/tisfeng/Easydict/issues/135#issuecomment-1750498120
_anchoredDraggable_State --> anchored Draggable State
*/
- (NSString *)segmentWords;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "NSString+EZUtils.h"
#import "NSString+EZSplit.h"
#import "EZAppleService.h"
#import "EZAppleDictionary.h"

static NSString *const kCommentSymbolPrefixPattern = @"^\\s*(//+|#+|\\*+)";

Expand Down Expand Up @@ -208,4 +209,27 @@ - (NSString *)removeCommentSymbols {
return cleanedText;
}

/**
Split camel and snake case text
https://github.com/tisfeng/Easydict/issues/135#issuecomment-1750498120
_anchoredDraggable_State --> anchored Draggable State
*/
- (NSString *)segmentWords {
NSString *queryText = self;
if ([queryText isSingleWord]) {
// If text is an English word, like LaTeX, we don't split it.
BOOL isEnglishWord = [EZAppleDictionary.shared queryDictionaryForText:queryText language:EZLanguageEnglish];
if (!isEnglishWord) {
// If text has quotes, like 'UIKit', we don't split it.
if ([queryText hasQuotesPair]) {
queryText = [queryText tryToRemoveQuotes];
} else {
queryText = [queryText splitCodeText];
}
}
}
return queryText;
}

@end
13 changes: 1 addition & 12 deletions Easydict/Feature/ViewController/Model/EZQueryModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,7 @@ - (NSString *)handleInputText:(NSString *)inputText {
_anchoredDraggable_State --> anchored Draggable State
*/
if (Configuration.shared.automaticWordSegmentation) {
if ([queryText isSingleWord]) {
// If text is an English word, like LaTeX, we don't split it.
BOOL isEnglishWord = [EZAppleDictionary.shared queryDictionaryForText:queryText language:EZLanguageEnglish];
if (!isEnglishWord) {
// If text has quotes, like 'UIKit', we don't split it.
if ([queryText hasQuotesPair]) {
queryText = [queryText tryToRemoveQuotes];
} else {
queryText = [queryText splitCodeText];
}
}
}
queryText = [queryText segmentWords];
}

if (Configuration.shared.automaticallyRemoveCodeCommentSymbols) {
Expand Down
9 changes: 9 additions & 0 deletions Easydict/Feature/ViewController/View/Titlebar/EZTitlebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, EZTitlebarAction) {
EZTitlebarActionRemoveCommentBlockSymbols,
EZTitlebarActionWordsSegmentation,
};

typedef void(^EZTitlebarActionBlock)(EZTitlebarAction);

@interface EZTitlebar : NSView

@property (nonatomic, assign) BOOL pin;
Expand All @@ -26,6 +33,8 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, strong) EZOpenLinkButton *settingButton;

@property (nonatomic, copy) EZTitlebarActionBlock menuActionBlock;

@end

NS_ASSUME_NONNULL_END
14 changes: 4 additions & 10 deletions Easydict/Feature/ViewController/View/Titlebar/EZTitlebar.m
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,11 @@ - (void)updatePinButtonImage {

- (void)showMenu {
NSMenu * menu = [[NSMenu alloc]initWithTitle:@"Menu"];
NSMenuItem * item1 = [[NSMenuItem alloc]initWithTitle:NSLocalizedString(@"automatically_remove_code_comment_symbols", nil) action:@selector(clickAutomaticallyRemoveCodeCommentSymbols) keyEquivalent:@""];
NSMenuItem * item1 = [[NSMenuItem alloc]initWithTitle:NSLocalizedString(@"remove_code_comment_symbols", nil) action:@selector(clickAutomaticallyRemoveCodeCommentSymbols) keyEquivalent:@""];
item1.target = self;
if (Configuration.shared.automaticallyRemoveCodeCommentSymbols) {
item1.state = NSControlStateValueOn;
}

NSMenuItem * item2 = [[NSMenuItem alloc]initWithTitle:NSLocalizedString(@"automatic_word_segmentation", nil) action:@selector(clickAutomaticWordSegmentation) keyEquivalent:@""];
NSMenuItem * item2 = [[NSMenuItem alloc]initWithTitle:NSLocalizedString(@"word_segmentation", nil) action:@selector(clickAutomaticWordSegmentation) keyEquivalent:@""];
item2.target = self;
if (Configuration.shared.automaticWordSegmentation) {
item2.state = NSControlStateValueOn;
}

NSMenuItem * item3 = [[NSMenuItem alloc]initWithTitle:NSLocalizedString(@"go_to_settings", nil) action:@selector(goToSettings) keyEquivalent:@""];
item3.target = self;
Expand All @@ -260,11 +254,11 @@ - (void)showMenu {
}

- (void)clickAutomaticallyRemoveCodeCommentSymbols {
Configuration.shared.automaticallyRemoveCodeCommentSymbols = !Configuration.shared.automaticallyRemoveCodeCommentSymbols;
_menuActionBlock(EZTitlebarActionRemoveCommentBlockSymbols);
}

- (void)clickAutomaticWordSegmentation {
Configuration.shared.automaticWordSegmentation = !Configuration.shared.automaticWordSegmentation;
_menuActionBlock(EZTitlebarActionWordsSegmentation);
}

- (void)goToSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "EZLayoutManager.h"
#import "EZQueryModel.h"
#import "EZQueryResult.h"
#import "EZTitlebar.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -61,6 +62,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)disableReplaceTextButton;

- (void)receiveTitlebarAction:(EZTitlebarAction)action;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#import "NSString+EZUtils.h"
#import "EZEventMonitor.h"
#import "Easydict-Swift.h"
#import "NSString+EZHandleInputText.h"

static NSString *const EZQueryViewId = @"EZQueryViewId";
static NSString *const EZSelectLanguageCellId = @"EZSelectLanguageCellId";
Expand Down Expand Up @@ -627,6 +628,26 @@ - (void)scrollToEndOfTextView {
[self.queryView scrollToEndOfTextView];
}

- (void)receiveTitlebarAction:(EZTitlebarAction)action {

switch (action) {
case EZTitlebarActionWordsSegmentation:
{
NSString *text = [[self.queryView.textView.string trim]segmentWords];
self.queryView.textView.string = text;
}
break;
case EZTitlebarActionRemoveCommentBlockSymbols:
{
NSString *text = [[self.queryView.textView.string trim]removeCommentBlockSymbols];
self.queryView.textView.string = text;
}
break;
default:
break;
}
}

#pragma mark - Query Methods

- (void)startQueryText {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ - (void)setupUI {
[self.titleBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(titleView);
}];

mm_weakify(self);
self.titleBar.menuActionBlock = ^(EZTitlebarAction action) {
mm_strongify(self);
if (self.queryViewController) {
[self.queryViewController receiveTitlebarAction:action];
}
};
}


Expand Down

0 comments on commit e844216

Please sign in to comment.