diff --git a/gradle.properties b/gradle.properties index 169cfee..5b80ae2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,11 +8,11 @@ pluginVersion = 0.0.1 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild = 223 -pluginUntilBuild = 232.* +pluginUntilBuild = # IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension platformType = IC -platformVersion = 2022.3.3 +platformVersion = 2023.3.2 # Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html # Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22 diff --git a/src/main/kotlin/com/github/lauvsong/languagecursor/listeners/NotEnglishKeyListener.kt b/src/main/kotlin/com/github/lauvsong/languagecursor/CursorColorManager.kt similarity index 84% rename from src/main/kotlin/com/github/lauvsong/languagecursor/listeners/NotEnglishKeyListener.kt rename to src/main/kotlin/com/github/lauvsong/languagecursor/CursorColorManager.kt index df6800c..468ad72 100644 --- a/src/main/kotlin/com/github/lauvsong/languagecursor/listeners/NotEnglishKeyListener.kt +++ b/src/main/kotlin/com/github/lauvsong/languagecursor/CursorColorManager.kt @@ -1,29 +1,22 @@ -package com.github.lauvsong.languagecursor.listeners +package com.github.lauvsong.languagecursor import com.github.lauvsong.languagecursor.settings.AppSettingsState import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.colors.EditorColors import com.intellij.openapi.editor.colors.EditorColorsManager -import com.intellij.openapi.editor.event.CaretEvent -import com.intellij.openapi.editor.event.CaretListener import com.intellij.ui.JBColor import java.awt.Color import java.awt.im.InputContext import java.util.Locale -class NotEnglishKeyListener : CaretListener { +object CursorColorManager { private val originalCursorColor: Color = EditorColorsManager.getInstance() .globalScheme .getColor(EditorColors.CARET_COLOR) ?: JBColor.BLACK - override fun caretPositionChanged(event: CaretEvent) { - val editor = event.editor - updateCursorColor(editor) - } - - private fun updateCursorColor(editor: Editor) { + fun updateCursorColor(editor: Editor) { val settings = AppSettingsState.instance val isEnglishInput = isEnglishInput() val cursorColor = if (!isEnglishInput) settings.cursorColor else editor.colorsScheme.defaultForeground diff --git a/src/main/kotlin/com/github/lauvsong/languagecursor/MyBundle.kt b/src/main/kotlin/com/github/lauvsong/languagecursor/MyBundle.kt deleted file mode 100644 index ccdeb8a..0000000 --- a/src/main/kotlin/com/github/lauvsong/languagecursor/MyBundle.kt +++ /dev/null @@ -1,20 +0,0 @@ -package com.github.lauvsong.languagecursor - -import com.intellij.DynamicBundle -import org.jetbrains.annotations.NonNls -import org.jetbrains.annotations.PropertyKey - -@NonNls -private const val BUNDLE = "messages.MyBundle" - -object MyBundle : DynamicBundle(BUNDLE) { - - @JvmStatic - fun message(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Any) = - getMessage(key, *params) - - @Suppress("unused") - @JvmStatic - fun messagePointer(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Any) = - getLazyMessage(key, *params) -} diff --git a/src/main/kotlin/com/github/lauvsong/languagecursor/ProjectOpenStartUpActivity.kt b/src/main/kotlin/com/github/lauvsong/languagecursor/ProjectOpenStartUpActivity.kt index 9e99e16..affe148 100644 --- a/src/main/kotlin/com/github/lauvsong/languagecursor/ProjectOpenStartUpActivity.kt +++ b/src/main/kotlin/com/github/lauvsong/languagecursor/ProjectOpenStartUpActivity.kt @@ -1,16 +1,58 @@ package com.github.lauvsong.languagecursor -import com.github.lauvsong.languagecursor.listeners.NotEnglishKeyListener -import com.intellij.openapi.editor.EditorFactory +import com.github.lauvsong.languagecursor.utils.NotifyUtil +import com.intellij.ide.IdeEventQueue +import com.intellij.openapi.Disposable +import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project +import com.intellij.openapi.project.ProjectManager +import com.intellij.openapi.project.ProjectManagerListener import com.intellij.openapi.startup.StartupActivity +import org.apache.commons.lang3.SystemUtils class ProjectOpenStartUpActivity : StartupActivity.DumbAware { override fun runActivity(project: Project) { - val editorFactory = EditorFactory.getInstance() - val listener = NotEnglishKeyListener() - editorFactory.eventMulticaster.addCaretListener(listener, project) + if (isNotSupportedOs()) { + notifyNotSupportedOs(project) + } + + listenNotEnglishKeyLayout(project) + } + + private fun listenNotEnglishKeyLayout(project: Project) { + // Why trigger listener logic by all type of event: + // In some cases, `KeyEvent` may not trigger for system keys. + // Although monitoring all events may impact performance, this approach is the only way I found + // to detect language changes when `KeyEvent` is not working, + IdeEventQueue.getInstance().addDispatcher({ _ -> + val editor = FileEditorManager.getInstance(project).selectedTextEditor + if (editor != null) { + CursorColorManager.updateCursorColor(editor) + } + false + }, createDisposableIfProjectClosed(project)) + } + + private fun createDisposableIfProjectClosed(project: Project): Disposable { + val connection = project.messageBus.connect() + connection.subscribe(ProjectManager.TOPIC, object : ProjectManagerListener { + override fun projectClosed(project: Project) { + connection.dispose() + } + }) + return connection + } + + private fun isNotSupportedOs(): Boolean { + return !(SystemUtils.IS_OS_WINDOWS || SystemUtils.IS_OS_MAC) + } + + private fun notifyNotSupportedOs(project: Project) { + NotifyUtil.byBalloonWarning(project, """ + Language Cursor may not supported on your OS. + Windows and macOS are supported. + """.trimIndent()) } } diff --git a/src/main/kotlin/com/github/lauvsong/languagecursor/utils/NotifyUtil.kt b/src/main/kotlin/com/github/lauvsong/languagecursor/utils/NotifyUtil.kt new file mode 100644 index 0000000..b2273a1 --- /dev/null +++ b/src/main/kotlin/com/github/lauvsong/languagecursor/utils/NotifyUtil.kt @@ -0,0 +1,15 @@ +package com.github.lauvsong.languagecursor.utils + +import com.intellij.notification.NotificationGroupManager +import com.intellij.notification.NotificationType +import com.intellij.openapi.project.Project + +object NotifyUtil { + + fun byBalloonWarning(project: Project, content: String) { + NotificationGroupManager.getInstance() + .getNotificationGroup("balloon") + .createNotification(content, NotificationType.WARNING) + .notify(project) + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 121f163..60d95d8 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -6,8 +6,6 @@ com.intellij.modules.platform - messages.MyBundle - + diff --git a/src/main/resources/messages/MyBundle.properties b/src/main/resources/messages/MyBundle.properties deleted file mode 100644 index 2e041d8..0000000 --- a/src/main/resources/messages/MyBundle.properties +++ /dev/null @@ -1,3 +0,0 @@ -projectService=Project service: {0} -randomLabel=The random number is: {0} -shuffle=Shuffle