Skip to content

Commit

Permalink
add IMKCandidates
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Dec 20, 2023
1 parent 1554a48 commit 3bd22bb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 29 additions & 1 deletion src/controller.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}
7 changes: 5 additions & 2 deletions src/server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down

0 comments on commit 3bd22bb

Please sign in to comment.