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 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.debug(self.name(ofCursorMode: mode))
self.bridgeLogger.info(self.name(ofCursorMode: mode))
gui.async { gui.async {
self.mode = mode self.mode = mode
self.markForRender( self.markForRender(
region: self.cursorRegion(for: self.ugrid.cursorPosition) region: self.cursorRegion(for: self.ugrid.cursorPosition)
) )
if self.name(ofCursorMode: mode) == "Normal" { self.activateIm()
self.activateIm(enabled: false)
}
if self.name(ofCursorMode: mode) == "Insert" {
self.is_insert_mode = true
self.activateIm(enabled: true)
}
} }
} }
@ -399,32 +396,25 @@ extension NvimView {
return min(0, top) return min(0, top)
} }
private func activateIm(enabled: Bool) { private func activateIm() {
if (self.asciiImSource == nil) { if (self.asciiImSource == nil) {
self.asciiImSource = TISCopyCurrentASCIICapableKeyboardInputSource().takeRetainedValue() self.asciiImSource = TISCopyCurrentASCIICapableKeyboardInputSource().takeRetainedValue()
// self.asciiImSource = TISCopyCurrentASCIICapableKeyboardInputSource().takeUnretainedValue()
self.bridgeLogger.info("ascii IME id: \(asciiImSource!.id), source: \(asciiImSource)") self.bridgeLogger.info("ascii IME id: \(asciiImSource!.id), source: \(asciiImSource)")
} }
if enabled { // Exit from Insert mode, save ime used in Insert mode.
// In insert mode, set ime to last ime source if self.lastMode == "Insert" && self.currentMode == "Normal" {
TISSelectInputSource(self.lastImSource) self.lastImSource = TISCopyCurrentKeyboardInputSource().takeRetainedValue()
} 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
}
TISSelectInputSource(self.asciiImSource) TISSelectInputSource(self.asciiImSource)
self.bridgeLogger.info("lastImSource id: \(lastImSource!.id), source: \(lastImSource)")
} }
// self.bridgeLogger.info("lastImSource id: \(lastImSource!.id), source: \(lastImSource)") // Enter into Insert mode, set ime to last used ime in Insert mode.
// os_log("lastImSource id: %@, source: %@", log: self.bridgeLogger, type: .debug, lastImSource!.id, lastImSource!.sourceLanguages) // 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 asciiImSource : TISInputSource?
var lastImSource : TISInputSource? var lastImSource : TISInputSource?
var is_insert_mode = false var lastMode = ""
var currentMode = "Normal"
// MARK: - Private // MARK: - Private
private var _linespacing = NvimView.defaultLinespacing private var _linespacing = NvimView.defaultLinespacing