Skip to content

Commit

Permalink
feat: 增加focus_auto参数,可以让editor在丢失焦点的时候,切换成非英文输入法,重新获得焦点的时候,会根据mode,恢复…
Browse files Browse the repository at this point in the history
…英文或者中文输入法。
  • Loading branch information
chaozwn authored and hadix-lin committed Apr 5, 2023
1 parent b999f0a commit b7fdfc0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 41 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 26 additions & 15 deletions src/main/kotlin/io/github/hadixlin/iss/InputMethodAutoSwitcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b7fdfc0

Please sign in to comment.