diff --git a/README.md b/README.md index 54cadc6..e7a92db 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,10 @@ ideavim.rc中还可以通过以下三个变量控制插件行为: 注意:上面两个变量仅在windows和macOS中有效 -`` let context_aware=1进入insert模式时根据上下文判断是否恢复输入法,0禁用,1启用 +``` let context_aware=1进入insert模式时根据上下文判断是否恢复输入法,0禁用,1启用``` +``` let focus_auto=1当编辑器丢失焦点的时候,自动切换到非英文输入法,获得焦点的时候, +自动根据输入模式恢复输入法(如果是norma模式,自动恢复英文输入法,insert模式自动回复成非英文输入法),0禁用,1启用 +``` ## 更新历史 * 1.6.6 diff --git a/src/main/kotlin/io/github/hadixlin/iss/InputMethodAutoSwitcher.kt b/src/main/kotlin/io/github/hadixlin/iss/InputMethodAutoSwitcher.kt index a11862e..3f82fc1 100644 --- a/src/main/kotlin/io/github/hadixlin/iss/InputMethodAutoSwitcher.kt +++ b/src/main/kotlin/io/github/hadixlin/iss/InputMethodAutoSwitcher.kt @@ -34,9 +34,11 @@ object InputMethodAutoSwitcher { var contextAware: Boolean = false - @Volatile - var enabled: Boolean = false - private set + var focusAuto: Boolean = false + + @Volatile + var enabled: Boolean = false + private set private var executor: ThreadPoolExecutor? = null @@ -130,18 +132,27 @@ object InputMethodAutoSwitcher { private val focusListener = object : FocusChangeListener { - override fun focusLost(editor: Editor) {} - - override fun focusGained(editor: Editor) { - if (!enabled || !VimPlugin.isEnabled()) { - return - } - val state = CommandState.getInstance(editor) - if (state.mode !in EDITING_MODES) { - executor?.execute { switcher.switchToEnglish() } - } - } - } + override fun focusLost(editor: Editor) { + if (focusAuto) { + executor?.execute { switcher.restore() } + } + } + + override fun focusGained(editor: Editor) { + if (!enabled || !VimPlugin.isEnabled()) { + return + } + val state = CommandState.getInstance(editor) + if (state.mode !in EDITING_MODES) { + executor?.execute { switcher.switchToEnglish() } + } + if (focusAuto) { + if (state.mode in EDITING_MODES) { + executor?.execute { switcher.restore() } + } + } + } + } fun disable() { diff --git a/src/main/kotlin/io/github/hadixlin/iss/KeepEnglishInNormalAndRestoreInInsertExtension.kt b/src/main/kotlin/io/github/hadixlin/iss/KeepEnglishInNormalAndRestoreInInsertExtension.kt index 62c970f..efd72ac 100644 --- a/src/main/kotlin/io/github/hadixlin/iss/KeepEnglishInNormalAndRestoreInInsertExtension.kt +++ b/src/main/kotlin/io/github/hadixlin/iss/KeepEnglishInNormalAndRestoreInInsertExtension.kt @@ -12,23 +12,26 @@ import com.maddyhome.idea.vim.vimscript.services.OptionService */ class KeepEnglishInNormalAndRestoreInInsertExtension : VimExtension { - override fun getName(): String { - return NAME - } + override fun getName(): String { + return NAME + } - override fun init() { - InputMethodAutoSwitcher.restoreInInsert = true - InputMethodAutoSwitcher.contextAware = - VimPlugin.getVariableService().getGlobalVariableValue(CONTEXT_WARE)?.asBoolean() ?: true - VimPlugin.getOptionService().setOption(OptionService.Scope.GLOBAL, KeepEnglishInNormalExtension.NAME) - } + override fun init() { + InputMethodAutoSwitcher.restoreInInsert = true + InputMethodAutoSwitcher.contextAware = + VimPlugin.getVariableService().getGlobalVariableValue(CONTEXT_WARE)?.asBoolean() ?: true + InputMethodAutoSwitcher.focusAuto = + VimPlugin.getVariableService().getGlobalVariableValue(FOCUS_AUTO)?.asBoolean() ?: true + VimPlugin.getOptionService().setOption(OptionService.Scope.GLOBAL, KeepEnglishInNormalExtension.NAME) + } - override fun dispose() { - InputMethodAutoSwitcher.restoreInInsert = false - } + override fun dispose() { + InputMethodAutoSwitcher.restoreInInsert = false + } - companion object { - private const val CONTEXT_WARE = "context_aware" - private const val NAME = "keep-english-in-normal-and-restore-in-insert" - } + companion object { + private const val CONTEXT_WARE = "context_aware" + private const val FOCUS_AUTO = "focus_auto" + private const val NAME = "keep-english-in-normal-and-restore-in-insert" + } } \ No newline at end of file diff --git a/src/main/kotlin/io/github/hadixlin/iss/mac/MacInputMethodSwitcher.kt b/src/main/kotlin/io/github/hadixlin/iss/mac/MacInputMethodSwitcher.kt index 9a31ad1..65aa511 100644 --- a/src/main/kotlin/io/github/hadixlin/iss/mac/MacInputMethodSwitcher.kt +++ b/src/main/kotlin/io/github/hadixlin/iss/mac/MacInputMethodSwitcher.kt @@ -43,10 +43,13 @@ class MacInputMethodSwitcher( } } - override fun restore() { - val nonEnglish = this.nonEnglish ?: return - switchInputSource(nonEnglish) - } + override fun restore() { + val currentInputSourceID = getCurrentInputSourceID() + val nonEnglish = this.nonEnglish ?: return + if (currentInputSourceID != nonEnglish) { + switchInputSource(nonEnglish) + } + } companion object { private const val KEY_LAYOUT_US = "com.apple.keylayout.US" diff --git a/src/main/kotlin/io/github/hadixlin/iss/win/WinInputMethodSwitcher.kt b/src/main/kotlin/io/github/hadixlin/iss/win/WinInputMethodSwitcher.kt index ec84e12..9a1c743 100644 --- a/src/main/kotlin/io/github/hadixlin/iss/win/WinInputMethodSwitcher.kt +++ b/src/main/kotlin/io/github/hadixlin/iss/win/WinInputMethodSwitcher.kt @@ -30,11 +30,14 @@ class WinInputMethodSwitcher( ) } - override fun restore() { - val nonEnglish = this.nonEnglish ?: return - val hwnd = WinNative.INSTANCE.GetForegroundWindow() ?: return - switchToInputSource(hwnd, nonEnglish) - } + override fun restore() { + val hwnd = WinNative.INSTANCE.GetForegroundWindow() ?: return + val currentInputSource = getCurrentInputSource(hwnd) + val nonEnglish = this.nonEnglish ?: return + if (currentInputSource != nonEnglish) { + switchToInputSource(hwnd, nonEnglish) + } + } override fun switchToEnglish() { val hwnd = WinNative.INSTANCE.GetForegroundWindow() ?: return