Skip to content

Commit f370963

Browse files
authored
Merge pull request openvanilla#824 from zonble/master
Reduce the amount of candidates in the auto trggered associated phrases in the smart mode
2 parents b7280df + 2767931 commit f370963

7 files changed

Lines changed: 306 additions & 158 deletions

McBopomofoTests/AssociatedPhrasesTests.swift

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,9 @@ final class AssociatedPhrasesTests {
4343
Preferences.chineseConversionEnabled = chineseConversionEnabled
4444
}
4545

46-
@Test(
47-
"Test building an associated phrase from characters",
48-
arguments: [
49-
("u6", "", "")
50-
])
51-
func testBuildingAssociatedPhrasesState(keySequence: String, reading: String, value: String) {
46+
private func typeKeys(_ keySequence: String) -> InputState {
5247
var state: InputState = InputState.Empty()
53-
let keys = Array(keySequence).map {
54-
String($0)
55-
}
48+
let keys = Array(keySequence).map(String.init)
5649
for key in keys {
5750
let input = KeyHandlerInput(
5851
inputText: key, keyCode: 0, charCode: charCode(key), flags: [],
@@ -62,10 +55,26 @@ final class AssociatedPhrasesTests {
6255
} errorCallback: {
6356
}
6457
}
58+
return state
59+
}
60+
61+
@Test(
62+
"Test building an associated phrase from characters",
63+
arguments: [
64+
("u6", "", "")
65+
])
66+
func testBuildingAssociatedPhrasesState(keySequence: String, reading: String, value: String) {
67+
let state = typeKeys(keySequence)
68+
let params = BuildAssociatedPhraseParams()
69+
params.previousState = state
70+
params.prefixCursorIndex = 1
71+
params.reading = reading
72+
params.value = value
73+
params.candidateIndex = 0
74+
params.useVerticalMode = false
75+
params.autoTriggered = false
6576
guard
66-
let associatedPhrases = handler.buildAssociatedPhraseState(
67-
withPreviousState: state, prefixCursorAt: 1, reading: reading, value: value,
68-
selectedCandidateIndex: 0, useVerticalMode: false, useShiftKey: false)
77+
let associatedPhrases = handler.buildAssociatedPhraseState(with: params)
6978
as? InputState.AssociatedPhrases
7079
else {
7180
Issue.record("There should be an associated phrase state")
@@ -81,24 +90,17 @@ final class AssociatedPhrasesTests {
8190
("", "《》"),
8291
])
8392
func testAssociatedPhrasesStatePunctuation1(input: String, result: String) {
84-
var state: InputState = InputState.Empty()
85-
let keys = Array("{").map {
86-
String($0)
87-
}
88-
for key in keys {
89-
let input = KeyHandlerInput(
90-
inputText: key, keyCode: 0, charCode: charCode(key), flags: [],
91-
isVerticalMode: false)
92-
handler.handle(input: input, state: state) { newState in
93-
state = newState
94-
} errorCallback: {
95-
}
96-
}
93+
let state = typeKeys("{")
94+
let params = BuildAssociatedPhraseParams()
95+
params.previousState = state
96+
params.prefixCursorIndex = 1
97+
params.reading = "_punctuation_{"
98+
params.value = input
99+
params.candidateIndex = 0
100+
params.useVerticalMode = false
101+
params.autoTriggered = false
97102
guard
98-
let associatedPhrases = handler.buildAssociatedPhraseState(
99-
withPreviousState: state, prefixCursorAt: 1, reading: "_punctuation_{",
100-
value: input,
101-
selectedCandidateIndex: 0, useVerticalMode: false, useShiftKey: false)
103+
let associatedPhrases = handler.buildAssociatedPhraseState(with: params)
102104
as? InputState.AssociatedPhrases
103105
else {
104106
Issue.record("There should be an associated phrase state")
@@ -118,4 +120,33 @@ final class AssociatedPhrasesTests {
118120
#expect(inputting.composingBuffer == result)
119121
}
120122

123+
@Test("Test building associated phrase state honors supported parameter object fields")
124+
func testBuildingAssociatedPhrasesStateHonorsSupportedParameterObjectFields() {
125+
let state = typeKeys("u6")
126+
let params = BuildAssociatedPhraseParams()
127+
params.previousState = state
128+
params.prefixCursorIndex = 1
129+
params.reading = ""
130+
params.value = ""
131+
params.candidateIndex = 1
132+
params.useVerticalMode = true
133+
params.autoTriggered = true
134+
135+
guard
136+
let associatedPhrases = handler.buildAssociatedPhraseState(with: params)
137+
as? InputState.AssociatedPhrases
138+
else {
139+
Issue.record("There should be an associated phrase state")
140+
return
141+
}
142+
143+
#expect(associatedPhrases.prefixCursorIndex == 1)
144+
#expect(associatedPhrases.prefixReading == "")
145+
#expect(associatedPhrases.prefixValue == "")
146+
#expect(associatedPhrases.selectedIndex == 1)
147+
#expect(associatedPhrases.useVerticalMode)
148+
#expect(associatedPhrases.autoTriggered)
149+
#expect(associatedPhrases.candidates.count > 0)
150+
}
151+
121152
}

McBopomofoTests/KeyHandlerBopomofoTests.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,9 +1275,12 @@ class KeyHandlerBopomofoTests: XCTestCase {
12751275

12761276
func testInputSpace() {
12771277
let enabled = Preferences.chooseCandidateUsingSpace
1278+
let associatedPhrases = Preferences.associatedPhrasesEnabled
12781279
Preferences.chooseCandidateUsingSpace = false
1280+
Preferences.associatedPhrasesEnabled = false
12791281
defer {
12801282
Preferences.chooseCandidateUsingSpace = enabled
1283+
Preferences.associatedPhrasesEnabled = associatedPhrases
12811284
}
12821285

12831286
var state: InputState = InputState.Empty()
@@ -1333,9 +1336,12 @@ class KeyHandlerBopomofoTests: XCTestCase {
13331336

13341337
func testInputSpaceInBetween() {
13351338
let enabled = Preferences.chooseCandidateUsingSpace
1339+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
13361340
Preferences.chooseCandidateUsingSpace = false
1341+
Preferences.associatedPhrasesEnabled = false
13371342
defer {
13381343
Preferences.chooseCandidateUsingSpace = enabled
1344+
Preferences.associatedPhrasesEnabled = asociatedPhrases
13391345
}
13401346
var state: InputState = InputState.Empty()
13411347
let keys = Array("su3cl3").map {
@@ -1375,6 +1381,12 @@ class KeyHandlerBopomofoTests: XCTestCase {
13751381
}
13761382

13771383
func testHomeAndEnd() {
1384+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
1385+
Preferences.associatedPhrasesEnabled = false
1386+
defer {
1387+
Preferences.associatedPhrasesEnabled = asociatedPhrases
1388+
}
1389+
13781390
var state: InputState = InputState.Empty()
13791391
let keys = Array("su3cl3").map {
13801392
String($0)
@@ -1570,6 +1582,11 @@ class KeyHandlerBopomofoTests: XCTestCase {
15701582
}
15711583

15721584
func testMarkingLeft() {
1585+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
1586+
Preferences.associatedPhrasesEnabled = false
1587+
defer {
1588+
Preferences.associatedPhrasesEnabled = asociatedPhrases
1589+
}
15731590
var state: InputState = InputState.Empty()
15741591
let keys = Array("su3cl3").map {
15751592
String($0)
@@ -1639,6 +1656,11 @@ class KeyHandlerBopomofoTests: XCTestCase {
16391656
}
16401657

16411658
func testMarkingRight() {
1659+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
1660+
Preferences.associatedPhrasesEnabled = false
1661+
defer {
1662+
Preferences.associatedPhrasesEnabled = asociatedPhrases
1663+
}
16421664
var state: InputState = InputState.Empty()
16431665
let keys = Array("su3cl3").map {
16441666
String($0)
@@ -1725,6 +1747,11 @@ class KeyHandlerBopomofoTests: XCTestCase {
17251747
}
17261748

17271749
func testCancelMarking() {
1750+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
1751+
Preferences.associatedPhrasesEnabled = false
1752+
defer {
1753+
Preferences.associatedPhrasesEnabled = asociatedPhrases
1754+
}
17281755
var state: InputState = InputState.Empty()
17291756
let keys = Array("su3cl3").map {
17301757
String($0)
@@ -1911,6 +1938,11 @@ class KeyHandlerBopomofoTests: XCTestCase {
19111938
}
19121939

19131940
func testEscKeyWithCandidate() {
1941+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
1942+
Preferences.associatedPhrasesEnabled = false
1943+
defer {
1944+
Preferences.associatedPhrasesEnabled = asociatedPhrases
1945+
}
19141946
var state: InputState = InputState.Empty()
19151947
let keys = Array("w8 ").map {
19161948
String($0)
@@ -1946,6 +1978,11 @@ class KeyHandlerBopomofoTests: XCTestCase {
19461978
}
19471979

19481980
func testHomeKey() {
1981+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
1982+
Preferences.associatedPhrasesEnabled = false
1983+
defer {
1984+
Preferences.associatedPhrasesEnabled = asociatedPhrases
1985+
}
19491986
var state: InputState = InputState.Empty()
19501987
let keys = Array("w8 ").map {
19511988
String($0)
@@ -1974,6 +2011,11 @@ class KeyHandlerBopomofoTests: XCTestCase {
19742011
}
19752012

19762013
func testHomeAndEndKey() {
2014+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
2015+
Preferences.associatedPhrasesEnabled = false
2016+
defer {
2017+
Preferences.associatedPhrasesEnabled = asociatedPhrases
2018+
}
19772019
var state: InputState = InputState.Empty()
19782020
let keys = Array("w8 ").map {
19792021
String($0)
@@ -2085,6 +2127,11 @@ class KeyHandlerBopomofoTests: XCTestCase {
20852127
}
20862128

20872129
func testLookUpCandidateInDictionaryAndCancelWithTabKey() {
2130+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
2131+
Preferences.associatedPhrasesEnabled = false
2132+
defer {
2133+
Preferences.associatedPhrasesEnabled = asociatedPhrases
2134+
}
20882135
var state: InputState = InputState.Empty()
20892136
let keys = Array("wu0 dj/ ").map {
20902137
String($0)
@@ -2126,6 +2173,11 @@ class KeyHandlerBopomofoTests: XCTestCase {
21262173
}
21272174

21282175
func testLookUpCandidateInDictionaryAndCancelWithEscKey() {
2176+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
2177+
Preferences.associatedPhrasesEnabled = false
2178+
defer {
2179+
Preferences.associatedPhrasesEnabled = asociatedPhrases
2180+
}
21292181
var state: InputState = InputState.Empty()
21302182
let keys = Array("wu0 dj/ ").map {
21312183
String($0)
@@ -2767,6 +2819,12 @@ extension KeyHandlerBopomofoTests {
27672819
}
27682820

27692821
func testBopomofoFontAnnotationSupport(input: String, expected: String) {
2822+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
2823+
Preferences.associatedPhrasesEnabled = false
2824+
defer {
2825+
Preferences.associatedPhrasesEnabled = asociatedPhrases
2826+
}
2827+
27702828
let bopomofoFontAnnotationSupportEnabled = Preferences.bopomofoFontAnnotationSupportEnabled
27712829
Preferences.bopomofoFontAnnotationSupportEnabled = true
27722830
defer {
@@ -2925,6 +2983,11 @@ extension KeyHandlerBopomofoTests {
29252983
}
29262984

29272985
func testInputBufferWithCharactersThenBacktickThenEsc() {
2986+
let asociatedPhrases = Preferences.associatedPhrasesEnabled
2987+
Preferences.associatedPhrasesEnabled = false
2988+
defer {
2989+
Preferences.associatedPhrasesEnabled = asociatedPhrases
2990+
}
29282991
// Type something in the buffer, then press ` to enter choosing punctuation list, then ESC to return to inputting state
29292992
var state: InputState = InputState.Empty()
29302993

Source/InputMethodController+CandidateControllerDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ extension McBopomofoInputMethodController: CandidateControllerDelegate {
9898
if Preferences.beepUponInputError {
9999
NSSound.beep()
100100
}
101-
}, useShiftKey: true)
101+
}, autoTriggered: true, maxCandidateCount: 0)
102102
}
103103
default:
104104
break

Source/InputMethodController.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -865,19 +865,18 @@ extension McBopomofoInputMethodController {
865865
let keyLabels =
866866
candidateKeys.count >= 4
867867
? Array(candidateKeys) : Array(Preferences.defaultCandidateKeys)
868-
let shouldUseShift =
869-
switch state {
870-
case let state as InputState.AssociatedPhrases:
871-
state.useShiftKey
872-
case is InputState.AssociatedPhrasesPlain,
873-
is InputState.Number:
874-
true
875-
default:
876-
false
877-
}
878-
let keyLabelPrefix = shouldUseShift ? "" : ""
868+
869+
let keyLabelFormat: (String)->String = switch state {
870+
case let state as InputState.AssociatedPhrases where state.autoTriggered:
871+
{ _ in "⇧ ⏎" }
872+
case is InputState.AssociatedPhrasesPlain,
873+
is InputState.Number:
874+
{ "" + $0 }
875+
default:
876+
{ $0 }
877+
}
879878
gCurrentCandidateController?.keyLabels = keyLabels.map {
880-
CandidateKeyLabel(key: String($0), displayedText: keyLabelPrefix + String($0))
879+
CandidateKeyLabel(key: String($0), displayedText: keyLabelFormat(String($0)))
881880
}
882881

883882
gCurrentCandidateController?.delegate = self

Source/InputState.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,12 +604,12 @@ class InputState: NSObject {
604604
@objc private(set) var selectedIndex: Int = 0
605605
@objc private(set) var candidates: [Candidate] = []
606606
@objc private(set) var useVerticalMode: Bool = false
607-
@objc private(set) var useShiftKey: Bool = false
607+
@objc private(set) var autoTriggered: Bool = false
608608

609609
@objc init(
610610
previousState: NotEmpty, prefixCursorIndex: Int, prefixReading: String,
611611
prefixValue: String,
612-
selectedIndex: Int, candidates: [Candidate], useVerticalMode: Bool, useShiftKey: Bool
612+
selectedIndex: Int, candidates: [Candidate], useVerticalMode: Bool, autoTriggered: Bool
613613
) {
614614
self.previousState = previousState
615615
self.prefixCursorIndex = prefixCursorIndex
@@ -618,7 +618,7 @@ class InputState: NSObject {
618618
self.selectedIndex = selectedIndex
619619
self.candidates = candidates
620620
self.useVerticalMode = useVerticalMode
621-
self.useShiftKey = useShiftKey
621+
self.autoTriggered = autoTriggered
622622
super.init(
623623
composingBuffer: previousState.composingBuffer,
624624
cursorIndex: previousState.cursorIndex)
@@ -629,7 +629,7 @@ class InputState: NSObject {
629629
}
630630

631631
var candidateCount: Int {
632-
candidates.count
632+
autoTriggered ? 1 : candidates.count
633633
}
634634

635635
func candidate(at index: Int) -> String {
@@ -640,6 +640,18 @@ class InputState: NSObject {
640640
candidates[index].reading
641641
}
642642

643+
@objc
644+
func toggle(autoTriggered: Bool) -> AssociatedPhrases {
645+
AssociatedPhrases(previousState: previousState,
646+
prefixCursorIndex: prefixCursorIndex,
647+
prefixReading: prefixReading,
648+
prefixValue: prefixValue,
649+
selectedIndex: selectedIndex,
650+
candidates: candidates,
651+
useVerticalMode: useVerticalMode,
652+
autoTriggered: autoTriggered)
653+
}
654+
643655
}
644656

645657
/// Represents that the user is choosing in a candidates list

0 commit comments

Comments
 (0)