1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 03:25:03 +03:00

refactor IME switch.

NvimView/NvimView.swift: Add lastMode and currentMode
NvimView/NvimView+UiBridge.swift: Change IME by mode.
This commit is contained in:
Albert 2022-02-13 01:01:37 +08:00
parent 359b2b582f
commit c523a4816e
2 changed files with 19 additions and 28 deletions

View File

@ -61,22 +61,19 @@ extension NvimView {
return
}
self.lastMode = self.currentMode
self.currentMode = self.name(ofCursorMode: mode)
self.bridgeLogger.info("\(self.lastMode) -> \(self.currentMode)")
// self.bridgeLogger.debug(self.name(ofCursorMode: mode))
self.bridgeLogger.info(self.name(ofCursorMode: mode))
gui.async {
self.mode = mode
self.markForRender(
region: self.cursorRegion(for: self.ugrid.cursorPosition)
)
if self.name(ofCursorMode: mode) == "Normal" {
self.activateIm(enabled: false)
}
if self.name(ofCursorMode: mode) == "Insert" {
self.is_insert_mode = true
self.activateIm(enabled: true)
}
self.activateIm()
}
}
@ -399,32 +396,25 @@ extension NvimView {
return min(0, top)
}
private func activateIm(enabled: Bool) {
private func activateIm() {
if (self.asciiImSource == nil) {
self.asciiImSource = TISCopyCurrentASCIICapableKeyboardInputSource().takeRetainedValue()
// self.asciiImSource = TISCopyCurrentASCIICapableKeyboardInputSource().takeUnretainedValue()
self.bridgeLogger.info("ascii IME id: \(asciiImSource!.id), source: \(asciiImSource)")
}
if enabled {
// In insert mode, set ime to last ime source
TISSelectInputSource(self.lastImSource)
} else {
// In normal mode, set ime to ascii input source
if self.is_insert_mode {
self.lastImSource = TISCopyCurrentKeyboardInputSource().takeRetainedValue()
self.bridgeLogger.info("lastImSource id: \(lastImSource!.id), source: \(lastImSource)")
// Set is_insert_mode flag to avoid copy the wrong input source.
// We could enter Normal mode from Visual and OperaterPending mode.
self.is_insert_mode = false
}
// Exit from Insert mode, save ime used in Insert mode.
if self.lastMode == "Insert" && self.currentMode == "Normal" {
self.lastImSource = TISCopyCurrentKeyboardInputSource().takeRetainedValue()
TISSelectInputSource(self.asciiImSource)
self.bridgeLogger.info("lastImSource id: \(lastImSource!.id), source: \(lastImSource)")
}
// self.bridgeLogger.info("lastImSource id: \(lastImSource!.id), source: \(lastImSource)")
// os_log("lastImSource id: %@, source: %@", log: self.bridgeLogger, type: .debug, lastImSource!.id, lastImSource!.sourceLanguages)
// Enter into Insert mode, set ime to last used ime in Insert mode.
// Visual -> Insert
// Normal -> Insert
if self.currentMode == "Insert" {
TISSelectInputSource(self.lastImSource)
}
}
}

View File

@ -304,7 +304,8 @@ public class NvimView: NSView,
var asciiImSource : TISInputSource?
var lastImSource : TISInputSource?
var is_insert_mode = false
var lastMode = ""
var currentMode = "Normal"
// MARK: - Private
private var _linespacing = NvimView.defaultLinespacing