From 232a944953589fc974b2cd500a4ebacfc4b943e4 Mon Sep 17 00:00:00 2001 From: zonble Date: Thu, 13 Jan 2022 17:07:22 +0800 Subject: [PATCH] Implements half-size punctuations. --- Source/InputMethodController.h | 1 + Source/InputMethodController.mm | 26 +++++++++++++++++++----- Source/en.lproj/Localizable.strings | 2 ++ Source/zh-Hant.lproj/Localizable.strings | 2 ++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Source/InputMethodController.h b/Source/InputMethodController.h index 404c0ab4..f1dbaea1 100644 --- a/Source/InputMethodController.h +++ b/Source/InputMethodController.h @@ -75,5 +75,6 @@ // if Chinese conversion is enabled BOOL _chineseConversionEnabled; + BOOL _halfSizePunctuationEnabled; } @end diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index a7fe79a0..26901fab 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -78,6 +78,7 @@ static NSString *const kUseHorizontalCandidateListPreferenceKey = @"UseHorizonta static NSString *const kComposingBufferSizePreferenceKey = @"ComposingBufferSize"; static NSString *const kChooseCandidateUsingSpaceKey = @"ChooseCandidateUsingSpaceKey"; static NSString *const kChineseConversionEnabledKey = @"ChineseConversionEnabledKey"; +static NSString *const kHalfSizePunctuationEnabledKey = @"HalfSizePunctuationEnabledKey"; static NSString *const kEscToCleanInputBufferKey = @"EscToCleanInputBufferKey"; // advanced (usually optional) settings @@ -189,6 +190,7 @@ static double FindHighestScore(const vector& nodes, double epsilon) _inputMode = kBopomofoModeIdentifier; _chineseConversionEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kChineseConversionEnabledKey]; + _halfSizePunctuationEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kHalfSizePunctuationEnabledKey]; } return self; @@ -201,11 +203,15 @@ static double FindHighestScore(const vector& nodes, double epsilon) NSMenuItem *preferenceMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"McBopomofo Preferences", @"") action:@selector(showPreferences:) keyEquivalent:@""]; [menu addItem:preferenceMenuItem]; - NSMenuItem *chineseConversionMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Chinese Conversion", @"") action:@selector(toggleChineseConverter:) keyEquivalent:@"G"]; + NSMenuItem *chineseConversionMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Chinese Conversion", @"") action:@selector(toggleChineseConverter:) keyEquivalent:@"g"]; chineseConversionMenuItem.keyEquivalentModifierMask = NSEventModifierFlagCommand | NSEventModifierFlagControl; chineseConversionMenuItem.state = _chineseConversionEnabled ? NSControlStateValueOn : NSControlStateValueOff; [menu addItem:chineseConversionMenuItem]; + NSMenuItem *halfSizePunctuationMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Use Half-Size Punctuations", @"") action:@selector(toggleHalfSizePunctuation:) keyEquivalent:@""]; + halfSizePunctuationMenuItem.state = _halfSizePunctuationEnabled ? NSControlStateValueOn : NSControlStateValueOff; + [menu addItem:halfSizePunctuationMenuItem]; + [menu addItem:[NSMenuItem separatorItem]]; [menu addItemWithTitle:NSLocalizedString(@"User Phrases", @"") action:NULL keyEquivalent:@""]; @@ -382,7 +388,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]; } @@ -542,7 +549,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)]; @@ -1062,13 +1070,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 = (_halfSizePunctuationEnabled ? 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; } @@ -1548,6 +1557,13 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } [[NSUserDefaults standardUserDefaults] setBool:_chineseConversionEnabled forKey:kChineseConversionEnabledKey]; } +- (void)toggleHalfSizePunctuation:(id)sender +{ + _halfSizePunctuationEnabled = !_halfSizePunctuationEnabled; + [[NSUserDefaults standardUserDefaults] setBool:_halfSizePunctuationEnabled forKey:kHalfSizePunctuationEnabledKey]; +} + + @end #pragma mark - diff --git a/Source/en.lproj/Localizable.strings b/Source/en.lproj/Localizable.strings index a9b5048e..7cfe431d 100644 --- a/Source/en.lproj/Localizable.strings +++ b/Source/en.lproj/Localizable.strings @@ -62,3 +62,5 @@ "Please check the permission of at \"%@\"." = "Please check the permission of at \"%@\"."; "Edit Excluded Phrases" = "Edit Excluded Phrases"; + +"Use Half-Size Punctuations" = "Use Half-Size Punctuations"; diff --git a/Source/zh-Hant.lproj/Localizable.strings b/Source/zh-Hant.lproj/Localizable.strings index cdd2b6d2..015c4538 100644 --- a/Source/zh-Hant.lproj/Localizable.strings +++ b/Source/zh-Hant.lproj/Localizable.strings @@ -63,3 +63,5 @@ "Edit Excluded Phrases" = "編輯要排除的詞彙"; +"Use Half-Size Punctuations" = "使用半型標點符號"; +