diff --git a/README.md b/README.md index 22b71e5..266f724 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ add `Toyimk` from `English`. switch to another input method, `pkill Toyimk`, then switch back. -* You may increase the number in [controller.swift](src/controller.swift) to make sure your changes take effect. +* You may change what is committed at `client.insert` in [controller.swift](src/controller.swift) to make sure your changes take effect. ## Debug Yes, though being a system module, input method is debuggable. diff --git a/src/controller.swift b/src/controller.swift index 19043e7..cd0d618 100644 --- a/src/controller.swift +++ b/src/controller.swift @@ -1,12 +1,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 } - client.insertText(string + "1", replacementRange: NSRange(location: NSNotFound, length: NSNotFound)) + 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)) + } } diff --git a/src/server.swift b/src/server.swift index 6b5acfb..da6cde1 100644 --- a/src/server.swift +++ b/src/server.swift @@ -16,11 +16,14 @@ class NSManualApplication: NSApplication { @main class AppDelegate: NSObject, NSApplicationDelegate { - var server = IMKServer() + static var server = IMKServer() + static var candidates = IMKCandidates() func applicationDidFinishLaunching(_ notification: Notification) { - server = IMKServer(name: Bundle.main.infoDictionary?["InputMethodConnectionName"] as? String, + AppDelegate.server = IMKServer(name: Bundle.main.infoDictionary?["InputMethodConnectionName"] as? String, bundleIdentifier: Bundle.main.bundleIdentifier) + AppDelegate.candidates = IMKCandidates(server: AppDelegate.server, + panelType: kIMKSingleRowSteppingCandidatePanel) NSLog("tried connection") }