-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.swift
40 lines (35 loc) · 1.17 KB
/
controller.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import InputMethodKit
class ToyimkInputController: IMKInputController {
var candidateWord: String = ""
var currentClient: Any! = nil
override func inputText(_ string: String!, client sender: Any!) -> Bool {
NSLog(string)
guard let client = sender as? IMKTextInput else {
return false
}
if string == " " {
commit(client, candidateWord + " ")
return true
}
currentClient = client
candidateWord += string
AppDelegate.candidates.update()
AppDelegate.candidates.show()
return true
}
override func candidates(_ sender: Any!) -> [Any]! {
return [candidateWord]
}
override func candidateSelected(_ candidateString: NSAttributedString!) {
guard let aString = candidateString,
let client = currentClient as? IMKTextInput else {
return
}
commit(client, aString.string)
}
func commit(_ client: IMKTextInput, _ text: String) {
candidateWord = ""
AppDelegate.candidates.hide()
client.insertText(text, replacementRange: NSRange(location: NSNotFound, length: NSNotFound))
}
}