@@ -21,8 +21,12 @@ class InputController: IMKInputController {
2121
2222 var emittedSequences : [ EmittedSequence ] = [ ]
2323
24- func insertCandidate( _: Candidate ) {
25-
24+ func insertCandidate( offset: Int ) {
25+ if let currentIndex = selectedIndex {
26+ let pageStart = ( currentIndex - currentCandidates. startIndex) / pageSize * pageSize + currentCandidates. startIndex;
27+ selectedIndex = pageStart + offset
28+ insertSelectedCandidate ( client: client ( ) )
29+ }
2630 }
2731
2832 override func activateServer( _ sender: Any ! ) {
@@ -79,14 +83,6 @@ class InputController: IMKInputController {
7983 CandidateWindow . shared. close ( )
8084 }
8185
82- override func candidateSelectionChanged( _ candidateString: NSAttributedString ! ) {
83-
84- }
85-
86- override func candidateSelected( _ candidateString: NSAttributedString ! ) {
87-
88- }
89-
9086 func getOriginPoint( ) -> NSPoint {
9187 let xd : CGFloat = 0
9288 let yd : CGFloat = 4
@@ -95,6 +91,33 @@ class InputController: IMKInputController {
9591 return NSPoint ( x: rect. minX + xd, y: rect. minY - yd)
9692 }
9793
94+ private func insertSelectedCandidate( client: IMKTextInput ) {
95+ if !currentCandidates. isEmpty {
96+ let text = currentCandidates [ selectedIndex!] . text
97+ let marked = client. markedRange ( )
98+ client. insertText ( text, replacementRange: marked)
99+ for s in emittedSequences {
100+ if s. range. endIndex > marked. location {
101+ s. range = s. range. startIndex..< ( s. range. endIndex+ text. utf16. count)
102+ }
103+ if s. range. startIndex >= marked. location {
104+ s. range = ( s. range. startIndex+ text. utf16. count) ..< s. range. endIndex
105+ }
106+ }
107+ if text. unicodeScalars. count > 1 {
108+ let emitted = NSRange . init ( location: marked. location, length: text. utf16. count)
109+ if emittedSequences. count >= 128 {
110+ emittedSequences. removeFirst ( )
111+ }
112+ emittedSequences. append ( EmittedSequence ( range: Range ( emitted) !, text: text) )
113+ }
114+ currentComposition = " " ;
115+ currentCandidates = [ ]
116+ selectedIndex = nil
117+ CandidateWindow . shared. close ( )
118+ }
119+ }
120+
98121 override func inputText( _ string: String ! , key keyCode: Int , modifiers rawFlags: Int , client sender: Any ! ) -> Bool {
99122 let flags = NSEvent . ModifierFlags ( rawValue: UInt ( rawFlags) )
100123 guard let client = sender as? IMKTextInput else {
@@ -129,32 +152,10 @@ class InputController: IMKInputController {
129152 keyCode == kVK_ANSI_KeypadEnter {
130153 if currentComposition. isEmpty {
131154 return false
155+ } else {
156+ insertSelectedCandidate ( client: client)
157+ return true
132158 }
133- if !currentCandidates. isEmpty {
134- let text = currentCandidates [ selectedIndex!] . text
135- let marked = client. markedRange ( )
136- client. insertText ( text, replacementRange: marked)
137- for s in emittedSequences {
138- if s. range. endIndex > marked. location {
139- s. range = s. range. startIndex..< ( s. range. endIndex+ text. utf16. count)
140- }
141- if s. range. startIndex >= marked. location {
142- s. range = ( s. range. startIndex+ text. utf16. count) ..< s. range. endIndex
143- }
144- }
145- if text. unicodeScalars. count > 1 {
146- let emitted = NSRange . init ( location: marked. location, length: text. utf16. count)
147- if emittedSequences. count >= 128 {
148- emittedSequences. removeFirst ( )
149- }
150- emittedSequences. append ( EmittedSequence ( range: Range ( emitted) !, text: text) )
151- }
152- currentComposition = " " ;
153- currentCandidates = [ ]
154- selectedIndex = nil
155- CandidateWindow . shared. close ( )
156- }
157- return true ;
158159 }
159160 if keyCode == kVK_Delete {
160161 if currentComposition. isEmpty {
0 commit comments