From 4bbccca283a4098a9270de6550258543ffaf3092 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 15 Jan 2022 17:27:50 +0800 Subject: [PATCH] Zonble: Input Experience // Half-Size Punctuation Support --- Source/InputMethodController.h | 3 +++ Source/InputMethodController.mm | 29 ++++++++++++++++++------ Source/en.lproj/Localizable.strings | 1 + Source/zh-Hans.lproj/Localizable.strings | 1 + Source/zh-Hant.lproj/Localizable.strings | 1 + 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Source/InputMethodController.h b/Source/InputMethodController.h index 7fc13239..4649650b 100644 --- a/Source/InputMethodController.h +++ b/Source/InputMethodController.h @@ -83,5 +83,8 @@ // if Chinese conversion is enabled BOOL _chineseConversionEnabled; + + // if half-width punctuation is enabled + BOOL _halfWidthPunctuationEnabled; } @end diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index efb15c5b..2ce71eae 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -81,6 +81,7 @@ static NSString *const kUseHorizontalCandidateListPreferenceKey = @"UseHorizonta static NSString *const kComposingBufferSizePreferenceKey = @"ComposingBufferSize"; static NSString *const kChooseCandidateUsingSpaceKey = @"ChooseCandidateUsingSpaceKey"; static NSString *const kChineseConversionEnabledKey = @"ChineseConversionEnabledKey"; +static NSString *const kHalfWidthPunctuationEnabledKey = @"HalfWidthPunctuationEnabledKey"; static NSString *const kEscToCleanInputBufferKey = @"EscToCleanInputBufferKey"; // advanced (usually optional) settings @@ -192,6 +193,7 @@ static double FindHighestScore(const vector& nodes, double epsilon) _inputMode = kBopomofoModeIdentifier; _chineseConversionEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kChineseConversionEnabledKey]; + _halfWidthPunctuationEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kHalfWidthPunctuationEnabledKey]; } return self; @@ -208,9 +210,12 @@ static double FindHighestScore(const vector& nodes, double epsilon) chineseConversionMenuItem.keyEquivalentModifierMask = NSEventModifierFlagCommand | NSEventModifierFlagControl; chineseConversionMenuItem.state = _chineseConversionEnabled ? NSControlStateValueOn : NSControlStateValueOff; [menu addItem:chineseConversionMenuItem]; + + NSMenuItem *halfWidthPunctuationMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Use Half-Width Punctuations", @"") action:@selector(toggleHalfWidthPunctuation:) keyEquivalent:@""]; + halfWidthPunctuationMenuItem.state = _halfWidthPunctuationEnabled ? NSControlStateValueOn : NSControlStateValueOff; + [menu addItem:halfWidthPunctuationMenuItem]; - [menu addItem:[NSMenuItem separatorItem]]; - [menu addItemWithTitle:NSLocalizedString(@"User Phrases", @"") action:NULL keyEquivalent:@""]; + [menu addItem:[NSMenuItem separatorItem]]; // ------------------------------ if (_inputMode == kSimpBopomofoModeIdentifier) { NSMenuItem *editExcludedPhrasesItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Edit Excluded Phrases", @"") action:@selector(openExcludedPhrasesSimpBopomofo:) keyEquivalent:@""]; @@ -226,7 +231,8 @@ static double FindHighestScore(const vector& nodes, double epsilon) NSMenuItem *reloadUserPhrasesItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Reload User Phrases", @"") action:@selector(reloadUserPhrases:) keyEquivalent:@""]; [menu addItem:reloadUserPhrasesItem]; - [menu addItem:[NSMenuItem separatorItem]]; + + [menu addItem:[NSMenuItem separatorItem]]; // ------------------------------ NSMenuItem *updateCheckItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Check for Updates…", @"") action:@selector(checkForUpdate:) keyEquivalent:@""]; [menu addItem:updateCheckItem]; @@ -385,7 +391,8 @@ static double FindHighestScore(const vector& nodes, double epsilon) // Chinese conversion. NSString *buffer = _composingBuffer; - if (_chineseConversionEnabled) { + BOOL chineseConversionEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kChineseConversionEnabledKey]; + if (chineseConversionEnabled) { buffer = [OpenCCBridge convert:_composingBuffer]; } @@ -545,7 +552,8 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } NodeAnchor &anchor = _walkedNodes[0]; NSString *popedText = [NSString stringWithUTF8String:anchor.node->currentKeyValue().value.c_str()]; // Chinese conversion. - if (_chineseConversionEnabled) { + BOOL chineseConversionEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kChineseConversionEnabledKey]; + if (chineseConversionEnabled) { popedText = [OpenCCBridge convert:popedText]; } [client insertText:popedText replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; @@ -1065,13 +1073,14 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } // if nothing is matched, see if it's a punctuation key for current layout. string layout = [self currentLayout]; - string customPunctuation = string("_punctuation_") + layout + string(1, (char)charCode); + string punctuationNamePrefix = (_halfWidthPunctuationEnabled ? string("_half_punctuation_"): string("_punctuation_")); + string customPunctuation = punctuationNamePrefix + layout + string(1, (char)charCode); if ([self handlePunctuation:customPunctuation usingVerticalMode:useVerticalMode client:client]) { return YES; } // if nothing is matched, see if it's a punctuation key. - string punctuation = string("_punctuation_") + string(1, (char)charCode); + string punctuation = punctuationNamePrefix + string(1, (char)charCode); if ([self handlePunctuation:punctuation usingVerticalMode:useVerticalMode client:client]) { return YES; } @@ -1552,6 +1561,12 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } [[NSUserDefaults standardUserDefaults] setBool:_chineseConversionEnabled forKey:kChineseConversionEnabledKey]; } +- (void)toggleHalfWidthPunctuation:(id)sender +{ + _halfWidthPunctuationEnabled = !_halfWidthPunctuationEnabled; + [[NSUserDefaults standardUserDefaults] setBool:_halfWidthPunctuationEnabled forKey:kHalfWidthPunctuationEnabledKey]; +} + @end #pragma mark - diff --git a/Source/en.lproj/Localizable.strings b/Source/en.lproj/Localizable.strings index 8678d274..8d205a43 100644 --- a/Source/en.lproj/Localizable.strings +++ b/Source/en.lproj/Localizable.strings @@ -24,3 +24,4 @@ "Unable to create the user phrase file." = "Unable to create the user phrase file."; "Please check the permission of at \"%@\"." = "Please check the permission of at \"%@\"."; "Edit Excluded Phrases" = "Edit Excluded Phrases"; +"Use Half-Width Punctuations" = "Use Half-Width Punctuations"; diff --git a/Source/zh-Hans.lproj/Localizable.strings b/Source/zh-Hans.lproj/Localizable.strings index 6f5d9566..15ef591e 100644 --- a/Source/zh-Hans.lproj/Localizable.strings +++ b/Source/zh-Hans.lproj/Localizable.strings @@ -24,3 +24,4 @@ "Unable to create the user phrase file." = "无法创建自订语汇档案。"; "Please check the permission of at \"%@\"." = "请检查此处的存取权限:\"%@\"."; "Edit Excluded Phrases" = "编辑要滤除的语汇"; +"Use Half-Width Punctuations" = "啟用半角標點輸出"; diff --git a/Source/zh-Hant.lproj/Localizable.strings b/Source/zh-Hant.lproj/Localizable.strings index 719a3757..385f2931 100644 --- a/Source/zh-Hant.lproj/Localizable.strings +++ b/Source/zh-Hant.lproj/Localizable.strings @@ -24,3 +24,4 @@ "Unable to create the user phrase file." = "無法創建自訂語彙檔案。"; "Please check the permission of at \"%@\"." = "請檢查此處的存取權限:\"%@\"."; "Edit Excluded Phrases" = "編輯要濾除的語彙"; +"Use Half-Width Punctuations" = "啟用半形標點輸出";