Skip to content

Commit

Permalink
修正编辑器重新获取焦点时保存的输入法状态不正确的问题 #48
Browse files Browse the repository at this point in the history
  • Loading branch information
hadix-lin committed Jan 24, 2020
1 parent 37bb295 commit bccc03e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ gradle buildPlugin
也可以通过将`set keep-english-in-normal[-and-restore-in-insert]`加入到`~/.ideavimrc`文件中并重启IDE来启用插件功能。

## 更新历史
* 1.4.4
修正编辑器重新获取焦点时保存的输入法状态不正确的问题[#48](https://github.com/hadix-lin/ideavim_extension/issues/48)

* 1.4.3
修正非预期的输入法回复问题 [#44](https://github.com/hadix-lin/ideavim_extension/issues/44)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object InputMethodAutoSwitcher {
return
}
if (commandName == VIM_INSERT_EXIT_MODE_ACTION) {
executor?.execute { switcher.switchToEnglish() }
executor?.execute { switcher.storeCurrentThenSwitchToEnglish() }
return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ package io.github.hadixlin.iss
* @date 2018-12-23
*/
interface InputMethodSwitcher {
fun switchToEnglish()

/** 保存当前输入法,然后切换输入法到英文 */
fun storeCurrentThenSwitchToEnglish()

/** 将系统恢复到上次调用 [storeCurrentThenSwitchToEnglish] 时保存的输入法 */
fun restore()

/** 切换输入法到英文 */
fun switchToEnglish()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class SystemInputMethodSwitcher : InputMethodSwitcher {
delegate.switchToEnglish()
}

override fun storeCurrentThenSwitchToEnglish() {
delegate.storeCurrentThenSwitchToEnglish()
}

override fun restore() {
delegate.restore()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ import java.util.concurrent.TimeUnit
class LinFcitxRemoteSwitcher : InputMethodSwitcher {
private var lastStatus: Int = STATUS_UNKNOWN

override fun switchToEnglish() {
override fun storeCurrentThenSwitchToEnglish() {
val current = getFcitxStatus()
if (current != STATUS_UNKNOWN) {
lastStatus = current
}
if (current == STATUS_INACTIVE) {
return
}
execFcitxRemote(FCITX_INACTIVE)
}

override fun restore() {
if (lastStatus == STATUS_ACTIVE) {
execFcitxRemote(FCITX_ACTIVE)
lastStatus = STATUS_UNKNOWN
}
switchToEnglish()
}

private fun execFcitxRemote(cmd: Array<String>): Process {
Expand All @@ -42,10 +35,21 @@ class LinFcitxRemoteSwitcher : InputMethodSwitcher {
}
}

override fun restore() {
if (lastStatus == STATUS_ACTIVE) {
execFcitxRemote(FCITX_ACTIVE)
lastStatus = STATUS_UNKNOWN
}
}

override fun switchToEnglish() {
execFcitxRemote(FCITX_INACTIVE)
}

companion object {
private const val FCITX_REMOTE = "fcitx-remote"
private const val STATUS_UNKNOWN = -1
private const val STATUS_INACTIVE = 1;
private const val STATUS_INACTIVE = 1
private const val STATUS_ACTIVE = 2 // 0 OFF,1 INACTIVE,2 ACTIVE

private val FCITX_ACTIVE = arrayOf(FCITX_REMOTE, "-o") //activate input method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.hadixlin.iss.mac
import io.github.hadixlin.iss.InputMethodSwitcher
import io.github.hadixlin.iss.mac.MacNative.getCurrentInputSourceID
import io.github.hadixlin.iss.mac.MacNative.switchInputSource
import org.apache.commons.lang.StringUtils
import org.apache.commons.lang.StringUtils.EMPTY

/**
Expand All @@ -15,12 +14,16 @@ class MacInputMethodSwitcher : InputMethodSwitcher {
@Volatile
private var lastInputSource: String = EMPTY

override fun switchToEnglish() {
override fun storeCurrentThenSwitchToEnglish() {
val current = getCurrentInputSourceID()
lastInputSource = current
if (StringUtils.equals(current, ENGLISH_INPUT_SOURCE)) {
if (ENGLISH_INPUT_SOURCE == current) {
return
}
switchToEnglish()
}

override fun switchToEnglish() {
val code = switchInputSource(ENGLISH_INPUT_SOURCE)
if (code < 0) {
ENGLISH_INPUT_SOURCE = KEY_LAYOUT_US
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class WinInputMethodSwitcher : InputMethodSwitcher {

private var lastInputSource: Long = -1

override fun switchToEnglish() {
override fun storeCurrentThenSwitchToEnglish() {
val hwnd = WinNative.INSTANCE.GetForegroundWindow()
val current = getCurrentInputSource(hwnd)
lastInputSource = current
Expand Down Expand Up @@ -36,6 +36,11 @@ class WinInputMethodSwitcher : InputMethodSwitcher {
lastInputSource = -1
}

override fun switchToEnglish() {
val hwnd = WinNative.INSTANCE.GetForegroundWindow()
switchToInputSource(hwnd, KEY_LAYOUT_US)
}

companion object {
private const val KEY_LAYOUT_US: Long = 0x4090409
private const val WM_INPUTLANGCHANGEREQUEST = 0x0050
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>IdeaVimExtension</id>
<name>IdeaVimExtension</name>
<version>1.4.3</version>
<version>1.4.4</version>

<vendor
email="hadix.lin@gmail.com"
Expand Down Expand Up @@ -55,6 +55,8 @@
]]></description>

<change-notes><![CDATA[
<p>1.4.4<br/>
Fix the [issue](https://github.com/hadix-lin/ideavim_extension/issues/48) caused by incorrect input method state saved when the editor regains focus
<p>1.4.3<br/>
Fixed the unexpected input method restoration <a href=https://github.com/hadix-lin/ideavim_extension/issues/44>issue</a>
<p>1.4.2<br/>
Expand All @@ -65,8 +67,10 @@
<p>1.4.0<br/>
support IdeaVim 0.54, keep consistent compatibility strategy with IdeaVim starting with this version.
<p>1.4.4<br/>
修正编辑器重新获取焦点时保存的输入法状态不正确的[问题](https://github.com/hadix-lin/ideavim_extension/issues/48)
<p>1.4.3<br/>
修正非预期的输入法回复<a href=https://github.com/hadix-lin/ideavim_extension/issues/44>问题</a>
修正非预期的输入法恢复<a href=https://github.com/hadix-lin/ideavim_extension/issues/44>问题</a>
<p>1.4.2<br/>
1.根据vim指令执行后编辑器状态来判断是否需要恢复输入法
2.修正某些情况下恢复输入法出错的问题
Expand Down

0 comments on commit bccc03e

Please sign in to comment.