From e038428475469a4a8251c76806ce4a665dbd34f8 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 20 Jan 2022 14:40:14 +0800 Subject: [PATCH] Limit User Phrase Length to a hidden variable. - Default: 10. --- Source/InputMethodController.mm | 14 +++++++++++--- Source/PreferencesModule.swift | 5 +++++ Source/en.lproj/Localizable.strings | 1 + Source/ja.lproj/Localizable.strings | 1 + Source/zh-Hans.lproj/Localizable.strings | 1 + Source/zh-Hant.lproj/Localizable.strings | 1 + 6 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index 10251a0a..545f5ede 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -1372,7 +1372,7 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } size_t begin = min(_builder->markerCursorIndex(), _builder->cursorIndex()); size_t end = max(_builder->markerCursorIndex(), _builder->cursorIndex()); - // A phrase should contian at least two characters. + // A phrase should contain at least two characters. if (end - begin < 1) { return @""; } @@ -1393,10 +1393,13 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } size_t begin = min(_builder->markerCursorIndex(), _builder->cursorIndex()); size_t end = max(_builder->markerCursorIndex(), _builder->cursorIndex()); - // A phrase should contian at least two characters. + // A phrase should contain at least two characters. if (end - begin < 2) { return @""; } + if (end - begin > Preferences.maxCandidateLength) { + return @""; + } NSRange range = NSMakeRange((NSInteger)begin, (NSInteger)(end - begin)); NSString *selectedText = [_composingBuffer substringWithRange:range]; @@ -1416,6 +1419,7 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } { NSString *currentMarkedPhrase = [self _currentMarkedTextAndReadings]; if (![currentMarkedPhrase length]) { + [self beep]; return NO; } @@ -1437,10 +1441,14 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } NSString *message = [NSString stringWithFormat:NSLocalizedString(@"⚠︎ Phrase replacement mode enabled, interfering user phrase entry.", @""), text]; [self _showTooltip:message client:client]; } - else if (length == 1) { + else if (length < 2) { NSString *message = [NSString stringWithFormat:NSLocalizedString(@"\"%@\" length must ≥ 2 for a user phrase.", @""), text]; [self _showTooltip:message client:client]; } + else if (length > Preferences.maxCandidateLength) { + NSString *message = [NSString stringWithFormat:NSLocalizedString(@"\"%@\" length too long for a user phrase.", @""), text]; + [self _showTooltip:message client:client]; + } else { NSString *message = [NSString stringWithFormat:NSLocalizedString(@"\"%@\" selected. ENTER to add user phrase.", @""), text]; [self _showTooltip:message client:client]; diff --git a/Source/PreferencesModule.swift b/Source/PreferencesModule.swift index f3068d85..c1aafc60 100644 --- a/Source/PreferencesModule.swift +++ b/Source/PreferencesModule.swift @@ -21,6 +21,7 @@ private let kChineseConversionEnabledKey = "ChineseConversionEnabled" private let kHalfWidthPunctuationEnabledKey = "HalfWidthPunctuationEnable" private let kEscToCleanInputBufferKey = "EscToCleanInputBuffer" private let kUseWinNT351BPMF = "UseWinNT351BPMF" +private let kMaxCandidateLength = "MaxCandidateLength" private let kShouldNotFartInLieuOfBeep = "ShouldNotFartInLieuOfBeep" private let kCandidateTextFontName = "CandidateTextFontName" @@ -183,6 +184,7 @@ struct ComposingBufferSize { defaults.removeObject(forKey: kPhraseReplacementEnabledKey) defaults.removeObject(forKey: kChineseConversionEngineKey) defaults.removeObject(forKey: kUseWinNT351BPMF) + defaults.removeObject(forKey: kMaxCandidateLength) defaults.removeObject(forKey: kShouldNotFartInLieuOfBeep) } @@ -226,6 +228,9 @@ struct ComposingBufferSize { return useWinNT351BPMF } + @UserDefault(key: kMaxCandidateLength, defaultValue: 10) + @objc static var maxCandidateLength: Int + @UserDefault(key: kShouldNotFartInLieuOfBeep, defaultValue: true) @objc static var shouldNotFartInLieuOfBeep: Bool diff --git a/Source/en.lproj/Localizable.strings b/Source/en.lproj/Localizable.strings index 90086cbf..ce7224ab 100644 --- a/Source/en.lproj/Localizable.strings +++ b/Source/en.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Edit Excluded Phrases" = "Edit Excluded Phrases"; "Use Half-Width Punctuations" = "Use Half-Width Punctuations"; "\"%@\" length must ≥ 2 for a user phrase." = "\"%@\" length must ≥ 2 for a user phrase."; +"\"%@\" length too long for a user phrase." = "\"%@\" length too long for a user phrase."; "\"%@\" selected. ENTER to add user phrase." = "\"%@\" selected. ENTER to add user phrase."; "Edit Phrase Replacement Table" = "Edit Phrase Replacement Table"; "Use Phrase Replacement" = "Use Phrase Replacement"; diff --git a/Source/ja.lproj/Localizable.strings b/Source/ja.lproj/Localizable.strings index 24610a0d..ae4f5088 100644 --- a/Source/ja.lproj/Localizable.strings +++ b/Source/ja.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Edit Excluded Phrases" = "辞書条目排除表を編集"; "Use Half-Width Punctuations" = "半角句読機能を起用"; "\"%@\" length must ≥ 2 for a user phrase." = "「%@」もう1つ文字のお選びを。"; +"\"%@\" length too long for a user phrase." = "「%@」文字数過剰で登録不可。"; "\"%@\" selected. ENTER to add user phrase." = "「%@」を ENTER で辞書に登録。"; "Edit Phrase Replacement Table" = "言葉置換表を編集"; "Use Phrase Replacement" = "言葉置換機能"; diff --git a/Source/zh-Hans.lproj/Localizable.strings b/Source/zh-Hans.lproj/Localizable.strings index fe669f53..aaa0bbf7 100644 --- a/Source/zh-Hans.lproj/Localizable.strings +++ b/Source/zh-Hans.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Edit Excluded Phrases" = "编辑要滤除的语汇"; "Use Half-Width Punctuations" = "啟用半角標點輸出"; "\"%@\" length must ≥ 2 for a user phrase." = "「%@」字数不足以自订语汇。"; +"\"%@\" length too long for a user phrase." = "「%@」字数太长、无法自订。"; "\"%@\" selected. ENTER to add user phrase." = "「%@」敲 Enter 添入自订语汇。"; "Edit Phrase Replacement Table" = "编辑语汇置换表"; "Use Phrase Replacement" = "使用语汇置换"; diff --git a/Source/zh-Hant.lproj/Localizable.strings b/Source/zh-Hant.lproj/Localizable.strings index 1c4c95dc..a551893c 100644 --- a/Source/zh-Hant.lproj/Localizable.strings +++ b/Source/zh-Hant.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Edit Excluded Phrases" = "編輯要濾除的語彙"; "Use Half-Width Punctuations" = "啟用半形標點輸出"; "\"%@\" length must ≥ 2 for a user phrase." = "「%@」字數不足以自訂語彙。"; +"\"%@\" length too long for a user phrase." = "「%@」字數太長、無法自訂。"; "\"%@\" selected. ENTER to add user phrase." = "「%@」敲 Enter 添入自訂語彙。"; "Edit Phrase Replacement Table" = "編輯語彙置換表"; "Use Phrase Replacement" = "使用語彙置換";