Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #19

Merged
merged 7 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 0 additions & 20 deletions src/main/kotlin/com/github/lauvsong/languagecursor/MyBundle.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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())
}

}
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 5 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

<depends>com.intellij.modules.platform</depends>

<resource-bundle>messages.MyBundle</resource-bundle>

<extensions defaultExtensionNs="com.intellij">
<applicationConfigurable
parentId="tools"
Expand All @@ -17,5 +15,10 @@
/>
<applicationService serviceImplementation="com.github.lauvsong.languagecursor.settings.AppSettingsState"/>
<postStartupActivity implementation="com.github.lauvsong.languagecursor.ProjectOpenStartUpActivity"/>
<notificationGroup
id="balloon"
displayType="BALLOON"
key="balloon"
/>
</extensions>
</idea-plugin>
3 changes: 0 additions & 3 deletions src/main/resources/messages/MyBundle.properties

This file was deleted.

Loading