Skip to content

Commit

Permalink
add none and last config options
Browse files Browse the repository at this point in the history
  • Loading branch information
rieonke committed Jun 16, 2017
1 parent 97c6e03 commit fc5a4f5
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 70 deletions.
16 changes: 16 additions & 0 deletions src/main/java/cn/rieon/idea/plugin/AutoSwitchIm/Constraints.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cn.rieon.idea.plugin.AutoSwitchIm

/**
* @author Rieon Ke <rieon></rieon>@rieon.cn>
* *
* @version 1.0.0
* *
* @since 2017/6/16
*/
object Constraints {

val CONFIG_INPUT_LAST = "Last"
val CONFIG_INPUT_NONE = "None"
val CONFIG_INPUT_DEFAULT = "com.apple.keylayout.ABC"

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.rieon.idea.plugin.AutoSwitchIm.component

import cn.rieon.idea.plugin.AutoSwitchIm.Constraints
import cn.rieon.idea.plugin.AutoSwitchIm.provider.ConfigurationProvider
import cn.rieon.idea.plugin.AutoSwitchIm.util.InputSourceUtil
import com.intellij.ide.FrameStateListener
Expand All @@ -24,10 +25,15 @@ import java.util.*
*/
class AutoSwitchComponent : ApplicationComponent {

private var currentInputSource: String? = null

/**
* last input source
*/
private var lastInputSource: String? = null
private var lastOutVimInsert: String? = null
private var lastInVimInsert: String? = null
private var lastOutIdea: String? = null
private var lastInIdea: String? = null

private var inNormal: Boolean? = true

Expand All @@ -42,37 +48,42 @@ class AutoSwitchComponent : ApplicationComponent {

LOG.info("INIT COMPONENT")

lastInputSource = currentInputSourceId
currentInputSource = currentInputSourceId
CommandProcessor.getInstance().addCommandListener(commandListener)
FrameStateManager.getInstance().addListener(frameStateListener)

/**
* Reload configuration from configuration provider
*/

configurationProvider = ConfigurationProvider.instance

if (configurationProvider!!.OutVimInsertInput != null) {
if (configurationProvider!!.outVimInsertConfig != null) {

OutVimInsertInput = configurationProvider!!.OutVimInsertInput!!
outVimInsertConfig = configurationProvider!!.outVimInsertConfig!!

LOG.info("USE CONFIG INPUT SOURCE FOR OUT VIM INSERT MODE" + OutVimInsertInput)
LOG.info("USE CONFIG INPUT SOURCE FOR OUT VIM INSERT MODE" + outVimInsertConfig)
}

if (configurationProvider!!.InVimInsertInput != null) {
if (configurationProvider!!.InVimInsertConfig != null) {

InVimInsertInput = configurationProvider!!.InVimInsertInput!!
inVimInsertConfig = configurationProvider!!.InVimInsertConfig!!

LOG.info("USE CONFIG INPUT SOURCE FOR In VIM INSERT MODE" + InVimInsertInput)
LOG.info("USE CONFIG INPUT SOURCE FOR In VIM INSERT MODE" + inVimInsertConfig)
}
if (configurationProvider!!.OutOfIdeaInput != null) {
if (configurationProvider!!.outOfIdeaConfig != null) {

OutOfIdeaInput = configurationProvider!!.OutOfIdeaInput!!
outOfIdeaConfig = configurationProvider!!.outOfIdeaConfig!!

LOG.info("USE CONFIG INPUT SOURCE FOR OUT OF IDEA" + OutOfIdeaInput)
LOG.info("USE CONFIG INPUT SOURCE FOR OUT OF IDEA" + outOfIdeaConfig)
}
if (configurationProvider!!.IdeaFocusedInput != null) {
if (configurationProvider!!.inIdeaConfig != null) {

IdeaFocusedInput = configurationProvider!!.IdeaFocusedInput!!
inIdeaConfig = configurationProvider!!.inIdeaConfig!!

LOG.info("USE CONFIG INPUT SOURCE FOR IDEA FOCUSED" + inIdeaConfig)
} //load complete

LOG.info("USE CONFIG INPUT SOURCE FOR IDEA FOCUSED" + IdeaFocusedInput)
}
LOG.info("INIT SUCCESSFUL")
}

Expand All @@ -86,23 +97,31 @@ class AutoSwitchComponent : ApplicationComponent {

val commandName = event!!.commandName

/**
* out of vim insert mode
*/
if (OUT_VIM_INSERT_MODE.contains(commandName)) {
lastInputSource = currentInputSourceId
inNormal = true
if (lastInputSource == null || lastInputSource == OutVimInsertInput)
return
switchTo(OutVimInsertInput)
handleOutVimInsertSwitch()
/**
* into vim insert mode
*/
} else if (IN_VIM_INSERT_MODE.contains(commandName)) {
val current = currentInputSourceId
inNormal = false
if (current == null || current == InVimInsertInput )
return
switchTo(InVimInsertInput)
handleInVimInsertSwitch()

/**
* start typing => not in vim normal mode
*/
} else if ("Typing" == commandName) {

// set current vim mode to normal
inNormal = false

} else if ("" == commandName) {

// if current in normal mode
if (inNormal!!)
switchTo(OutVimInsertInput)
handleOutVimInsertSwitch()

}
}
}
Expand All @@ -114,17 +133,11 @@ class AutoSwitchComponent : ApplicationComponent {
private val frameStateListener: FrameStateListener
get() = object : FrameStateListener.Adapter() {
override fun onFrameDeactivated() {
val current = currentInputSourceId
if (current == null || current == OutOfIdeaInput)
return
switchTo(OutOfIdeaInput)
handleOutIdeaSwitch()
}

override fun onFrameActivated() {
val current = currentInputSourceId
if (current == null || current == IdeaFocusedInput)
return
switchTo(IdeaFocusedInput)
handInIdeaSwitch()
}
}

Expand Down Expand Up @@ -170,10 +183,10 @@ class AutoSwitchComponent : ApplicationComponent {

private val LOG = Logger.getInstance(AutoSwitchComponent::class.java)

var OutVimInsertInput = "com.apple.keylayout.ABC"
var InVimInsertInput = "com.apple.keylayout.ABC"
var IdeaFocusedInput = "com.apple.keylayout.ABC"
var OutOfIdeaInput = "com.apple.keylayout.ABC"
var outVimInsertConfig = Constraints.CONFIG_INPUT_DEFAULT
var inVimInsertConfig = Constraints.CONFIG_INPUT_DEFAULT
var inIdeaConfig = Constraints.CONFIG_INPUT_DEFAULT
var outOfIdeaConfig = Constraints.CONFIG_INPUT_DEFAULT


/**
Expand Down Expand Up @@ -212,4 +225,76 @@ class AutoSwitchComponent : ApplicationComponent {
"Vim Delete Previous Word")
}

internal fun handleInVimInsertSwitch(){

//get current input source
currentInputSource = currentInputSourceId
//record last input source state for last-in-vim-insert-mode
lastOutVimInsert = currentInputSource

inNormal = false

handle(currentInputSource,lastInVimInsert, inVimInsertConfig)

}

internal fun handleOutVimInsertSwitch(){

//get current input source
currentInputSource = currentInputSourceId
//record last input source state for last-in-vim-insert-mode
lastInVimInsert = currentInputSource

inNormal = true

handle(currentInputSource,lastOutVimInsert, outVimInsertConfig)

}

internal fun handleOutIdeaSwitch(){

//get current input source
currentInputSource = currentInputSourceId
//record last input source state for last-in-vim-insert-mode
lastInIdea = currentInputSource

handle(currentInputSource,lastOutIdea, outOfIdeaConfig)

}

internal fun handInIdeaSwitch(){

//get current input source
currentInputSource = currentInputSourceId
//record last input source state for last-in-vim-insert-mode
lastOutIdea = currentInputSource

handle(currentInputSourceId,lastInIdea, inIdeaConfig)

}

internal fun handle(current:String?,last:String?,config:String) {

if (current == null || current == config) return

when (config) {

Constraints.CONFIG_INPUT_LAST -> {

if (last == null || current == last) {
return
} else {
switchTo(last)
}

}

Constraints.CONFIG_INPUT_NONE -> return

// use input source in configuration
else -> {
switchTo(config)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ConfigurationProvider : PersistentStateComponent<ConfigurationProvider.Sta
myState = state
}

var IdeaFocusedInput: String?
var inIdeaConfig: String?
get() {

if (myState != null) {
Expand All @@ -44,11 +44,11 @@ class ConfigurationProvider : PersistentStateComponent<ConfigurationProvider.Sta
set(input) {

myState!!.IdeaFocusedInput = input
AutoSwitchComponent.IdeaFocusedInput = myState!!.IdeaFocusedInput!!
AutoSwitchComponent.inIdeaConfig = myState!!.IdeaFocusedInput!!

}

var InVimInsertInput: String?
var InVimInsertConfig: String?
get() {

if (myState != null) {
Expand All @@ -61,11 +61,11 @@ class ConfigurationProvider : PersistentStateComponent<ConfigurationProvider.Sta
set(input) {

myState!!.InVimInsertInput = input
AutoSwitchComponent.InVimInsertInput = myState!!.InVimInsertInput!!
AutoSwitchComponent.inVimInsertConfig = myState!!.InVimInsertInput!!

}

var OutVimInsertInput: String?
var outVimInsertConfig: String?
get() {

if (myState != null) {
Expand All @@ -78,11 +78,11 @@ class ConfigurationProvider : PersistentStateComponent<ConfigurationProvider.Sta
set(input) {

myState!!.OutVimInsertInput = input
AutoSwitchComponent.OutVimInsertInput = myState!!.OutVimInsertInput!!
AutoSwitchComponent.outVimInsertConfig = myState!!.OutVimInsertInput!!

}

var OutOfIdeaInput: String?
var outOfIdeaConfig: String?
get() {

if (myState != null) {
Expand All @@ -95,7 +95,7 @@ class ConfigurationProvider : PersistentStateComponent<ConfigurationProvider.Sta
set(input) {

myState!!.OutOfIdeaInput = input
AutoSwitchComponent.OutOfIdeaInput = myState!!.OutOfIdeaInput!!
AutoSwitchComponent.outOfIdeaConfig = myState!!.OutOfIdeaInput!!

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.rieon.idea.plugin.AutoSwitchIm.ui

import cn.rieon.idea.plugin.AutoSwitchIm.Constraints
import cn.rieon.idea.plugin.AutoSwitchIm.provider.ConfigurationProvider
import cn.rieon.idea.plugin.AutoSwitchIm.util.InputSourceUtil
import com.intellij.openapi.options.Configurable
Expand Down Expand Up @@ -49,7 +50,11 @@ class InputSourceConfigure : Configurable {
override fun createComponent(): JComponent? {
settingPanel = InputSourceConfigureForm()
configurationProvider = ConfigurationProvider.instance
val sources = InputSourceUtil.allInputSources
var sources = InputSourceUtil.allInputSources
val nonePair:Pair<String,String> = Pair(Constraints.CONFIG_INPUT_NONE,"Never Switch It!")
val lastPair:Pair<String,String> = Pair(Constraints.CONFIG_INPUT_LAST,"Last Input Source")
sources.add(nonePair)
sources.add(lastPair)
return settingPanel!!.createPanel(configurationProvider, sources)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ void apply() {
String outVimInsertValue = exactValueFromInput(outVimInsertInput);

if (ideaFocusedValue != null){
configurationProvider.setIdeaFocusedInput(ideaFocusedValue);
configurationProvider.setInIdeaConfig(ideaFocusedValue);
}

if (outOfIdeaValue != null){
configurationProvider.setOutOfIdeaInput(outOfIdeaValue);
configurationProvider.setOutOfIdeaConfig(outOfIdeaValue);
}
if (inVimInsertValue != null){
configurationProvider.setInVimInsertInput(inVimInsertValue);
configurationProvider.setInVimInsertConfig(inVimInsertValue);
}
if (outVimInsertValue != null){
configurationProvider.setOutVimInsertInput(outVimInsertValue);
configurationProvider.setOutVimInsertConfig(outVimInsertValue);
}

}
Expand Down Expand Up @@ -131,31 +131,31 @@ private void setUpPanel(ArrayList<Pair<String, String>> sources) {
InVimInsertCbx.addItem(item);
OutVimInsertCbx.addItem(item);

if (Objects.equals(configurationProvider.getIdeaFocusedInput(), pair.getKey())) {
if (Objects.equals(configurationProvider.getInIdeaConfig(), pair.getKey())) {
IdeaFocusedCbx.setSelectedItem(item);
}
if (Objects.equals(configurationProvider.getOutOfIdeaInput(), pair.getKey())) {
if (Objects.equals(configurationProvider.getOutOfIdeaConfig(), pair.getKey())) {
OutOfIdeaCbx.setSelectedItem(item);
}
if (Objects.equals(configurationProvider.getInVimInsertInput(), pair.getKey())) {
if (Objects.equals(configurationProvider.getInVimInsertConfig(), pair.getKey())) {
InVimInsertCbx.setSelectedItem(item);
}
if (Objects.equals(configurationProvider.getOutVimInsertInput(), pair.getKey())) {
if (Objects.equals(configurationProvider.getOutVimInsertConfig(), pair.getKey())) {
OutVimInsertCbx.setSelectedItem(item);
}

});

if (configurationProvider.getIdeaFocusedInput() == null) {
if (configurationProvider.getInIdeaConfig() == null) {
IdeaFocusedCbx.setSelectedItem("ABC(com.apple.keylayout.ABC)");
}
if (configurationProvider.getOutOfIdeaInput() == null) {
if (configurationProvider.getOutOfIdeaConfig() == null) {
OutOfIdeaCbx.setSelectedItem("ABC(com.apple.keylayout.ABC)");
}
if (configurationProvider.getOutVimInsertInput() == null) {
if (configurationProvider.getOutVimInsertConfig() == null) {
OutVimInsertCbx.setSelectedItem("ABC(com.apple.keylayout.ABC)");
}
if (configurationProvider.getInVimInsertInput() == null){
if (configurationProvider.getInVimInsertConfig() == null){
InVimInsertCbx.setSelectedItem("ABC(com.apple.keylayout.ABC)");
}

Expand Down
Loading

0 comments on commit fc5a4f5

Please sign in to comment.