Skip to content

Commit

Permalink
feat: feat: Change cursor color on Caps Lock activation (#36)
Browse files Browse the repository at this point in the history
* CapsLock시 색 변경

* feat: give priority to capslock

- removed nonEnglishCapsLockColor
- rename CheckService to InputChecker

---------

Co-authored-by: Hyesong Kim <wisepine@kakao.com>
  • Loading branch information
myoun and lauvsong authored Feb 9, 2024
1 parent a50cd05 commit c6000b8
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.github.lauvsong.langcursor

import com.github.lauvsong.langcursor.services.LanguageCheckService
import com.intellij.ide.AppLifecycleListener

class IdeLifeCycleListener : AppLifecycleListener {

override fun appClosing() {
super.appClosing()
LanguageCheckService.shutdown()
InputChecker.shutdown()
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.github.lauvsong.langcursor.services
package com.github.lauvsong.langcursor

import com.github.lauvsong.langcursor.core.LanguageCheckStrategy
import com.github.lauvsong.langcursor.core.defaultLanguageCheckStrategy
import com.github.lauvsong.langcursor.core.windowsLanguageCheckStrategy
import com.github.lauvsong.langcursor.core.*
import com.github.lauvsong.langcursor.services.CursorColorService
import org.apache.commons.lang3.SystemUtils
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit

object LanguageCheckService {
object InputChecker {

private val languageCheckStrategy: LanguageCheckStrategy = setLanguageCheckStrategy()
private val capsLockCheckStrategy: CapsLockCheckStrategy = defaultCapsLockCheckStrategy
private val scheduledExecutor: ScheduledExecutorService = Executors.newScheduledThreadPool(1)
private const val INTERVAL_MILLIS = 200L

Expand All @@ -20,7 +20,7 @@ object LanguageCheckService {

private fun start() {
scheduledExecutor.scheduleAtFixedRate(
{ switchCursorColorByLanguage() },
{ switchCursorColor() },
0,
INTERVAL_MILLIS,
TimeUnit.MILLISECONDS
Expand All @@ -31,11 +31,13 @@ object LanguageCheckService {
scheduledExecutor.shutdown()
}

private fun switchCursorColorByLanguage() {
if (languageCheckStrategy.isEnglishInput()) {
CursorColorService.toOriginalCursorColor()
private fun switchCursorColor() {
if (capsLockCheckStrategy.isCapsLockOn()) {
CursorColorService.toCapsLockCursorColor()
} else if (!languageCheckStrategy.isEnglishInput()) {
CursorColorService.toNonEnglishCursorColor()
} else {
CursorColorService.toNotEnglishCursorColor()
CursorColorService.toOriginalCursorColor()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.lauvsong.langcursor

import com.github.lauvsong.langcursor.services.LanguageCheckService
import com.github.lauvsong.langcursor.services.NotifyService
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
Expand All @@ -21,6 +20,6 @@ class ProjectOpenStartUpActivity : StartupActivity.DumbAware {
}

private fun initLanguageCheckService() {
LanguageCheckService
InputChecker
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.lauvsong.langcursor.core

import java.awt.Toolkit
import java.awt.event.KeyEvent

fun interface CapsLockCheckStrategy {
fun isCapsLockOn() : Boolean

}

val defaultCapsLockCheckStrategy = CapsLockCheckStrategy {
val capsLock = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)
capsLock
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ object CursorColorService {
.getColor(EditorColors.CARET_COLOR)
?: JBColor.BLACK

fun toNotEnglishCursorColor() {
fun toNonEnglishCursorColor() {
val settings = AppSettingsState.instance
CursorColorUtil.setGlobalCursorColor(settings.cursorColor)
CursorColorUtil.setGlobalCursorColor(settings.nonEnglishCursorColor)
}

fun toCapsLockCursorColor() {
val settings = AppSettingsState.instance
CursorColorUtil.setGlobalCursorColor(settings.capsLockCursorColor)
}

fun toOriginalCursorColor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import javax.swing.JPanel
class AppSettingsComponent {
val mainPanel: JPanel by lazy {
FormBuilder.createFormBuilder()
.addLabeledComponent(JBLabel("Cursor color: "), cursorColor, 1, false)
.addLabeledComponent(JBLabel("Cursor color: "), nonEnglishCursorColor, 1, false)
.addLabeledComponent(JBLabel("CapsLock cursor color: "), capsLockCursorColor, 2, false)
.addComponentFillVertically(JPanel(), 0)
.panel
}

val cursorColor: ColorPanel = ColorPanel()
val nonEnglishCursorColor: ColorPanel = ColorPanel()

val capsLockCursorColor: ColorPanel = ColorPanel()

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ class AppSettingsConfigurable : Configurable {

override fun isModified(): Boolean {
val settings = AppSettingsState.instance
return settings.cursorColor != settingsComponent.cursorColor
return settings.nonEnglishCursorColor != settingsComponent.nonEnglishCursorColor
}

override fun apply() {
val settings = AppSettingsState.instance
settings.cursorColorAsRgb = settingsComponent.cursorColor.selectedColor?.rgb ?: settings.cursorColorAsRgb
settings.nonEnglishCursorColorAsRgb = settingsComponent.nonEnglishCursorColor.selectedColor?.rgb ?: settings.nonEnglishCursorColorAsRgb
settings.capsLockCursorColorAsRgb = settingsComponent.capsLockCursorColor.selectedColor?.rgb ?: settings.capsLockCursorColorAsRgb
}

override fun reset() {
val settings = AppSettingsState.instance
settingsComponent.cursorColor.selectedColor = settings.cursorColor
settingsComponent.nonEnglishCursorColor.selectedColor = settings.nonEnglishCursorColor
settingsComponent.capsLockCursorColor.selectedColor = settings.capsLockCursorColor
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import java.awt.Color
storages = [Storage("AppSettingsState.xml")]
)
class AppSettingsState : PersistentStateComponent<AppSettingsState> {
var cursorColorAsRgb: Int = JBColor.RED.rgb
var nonEnglishCursorColorAsRgb: Int = JBColor.RED.rgb
var capsLockCursorColorAsRgb: Int = JBColor.BLUE.rgb

val cursorColor: Color
get() = Color(cursorColorAsRgb)
val nonEnglishCursorColor: Color
get() = Color(nonEnglishCursorColorAsRgb)

val capsLockCursorColor: Color
get() = Color(capsLockCursorColorAsRgb)

override fun getState(): AppSettingsState {
return this
Expand Down

0 comments on commit c6000b8

Please sign in to comment.