Skip to content

Commit

Permalink
#2 Improve MacOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
siropkin committed Oct 25, 2023
1 parent 27c7094 commit 49b2735
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
## [Unreleased]


## [1.0.4] - 2023-10-25
### Changed
- Improve MacOS support


## [1.0.3] - 2023-07-28
### Changed
- Improve Linux support
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.siropkin.kursor
pluginName = Kursor
pluginRepositoryUrl = https://github.com/siropkin/kursor
# SemVer format -> https://semver.org
pluginVersion = 1.0.3
pluginVersion = 1.0.4

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 221
Expand Down
33 changes: 20 additions & 13 deletions src/main/kotlin/com/github/siropkin/kursor/Kursor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ class Kursor(private var editor: Editor): JComponent(), ComponentListener, Caret
}
}

private fun getUbuntuKeyboardLayout(): String {
private fun getLinuxUbuntuKeyboardLayout(): String {
val commandOutput = executeNativeCommand("gsettings get org.gnome.desktop.input-sources mru-sources")
return commandOutput
.substringAfter("('xkb', '")
.substringBefore("')")
.substring(0, 2)
}

private fun getNonUbuntuKeyboardLayouts(): List<String> {
private fun getLinuxNonUbuntuKeyboardLayouts(): List<String> {
if (linuxKeyboardLayouts.isNotEmpty()) {
return linuxKeyboardLayouts
}
Expand All @@ -91,7 +91,7 @@ class Kursor(private var editor: Editor): JComponent(), ComponentListener, Caret
return linuxKeyboardLayouts
}

private fun getNonUbuntuKeyboardLayoutIndex(): Int {
private fun getLinuxNonUbuntuKeyboardLayoutIndex(): Int {
return executeNativeCommand("xset -q")
.substringAfter("LED mask:")
.substringBefore("\n")
Expand All @@ -100,26 +100,33 @@ class Kursor(private var editor: Editor): JComponent(), ComponentListener, Caret
.toInt(16)
}

private fun getNonUbuntuKeyboardLayout(): String {
val linuxKeyboardLayouts = getNonUbuntuKeyboardLayouts()
val linuxCurrentKeyboardLayoutIndex = getNonUbuntuKeyboardLayoutIndex()
private fun getLinuxNonUbuntuKeyboardLayout(): String {
val linuxKeyboardLayouts = getLinuxNonUbuntuKeyboardLayouts()
val linuxCurrentKeyboardLayoutIndex = getLinuxNonUbuntuKeyboardLayoutIndex()
return linuxKeyboardLayouts[linuxCurrentKeyboardLayoutIndex]
}

private fun getOtherOsKeyboardLayout(): String {
// if locale in format _US_UserDefined_252 then we need to take the first two letters without _ symbol
// otherwise we are expecting the locale in format en_US and taking the first two letters
val locale = InputContext.getInstance().locale.toString()
if (locale.startsWith("_")) {
return locale.substring(1, 3)
}
return locale.substring(0, 2)
}

private fun getKeyboardLayout(): String {
// This is not the ideal solution because it involves executing a shell command to know the current keyboard layout
// which might affect the performance. And we have different commands for different Linux distributions.
// But it is the only solution I found that works on Linux.
var language = when (os) {
"linux" -> when (linuxDistribution) {
"ubuntu" -> getUbuntuKeyboardLayout()
else -> getNonUbuntuKeyboardLayout()
"ubuntu" -> getLinuxUbuntuKeyboardLayout()
else -> getLinuxNonUbuntuKeyboardLayout()
}
else -> InputContext.getInstance()
.locale
.toString()
.substring(0, 2)
}
else -> getOtherOsKeyboardLayout()
}.lowercase()
if (language == "us") {
language = "en"
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
]]></description>

<change-notes><![CDATA[
1.0.4 (2023-10-25)<br />
++++++++++++++++++<br />
- Improve MacOS support<br />
<br />
<br />
1.0.3 (2023-07-28)<br />
++++++++++++++++++<br />
- Improve Linux support<br />
Expand Down

0 comments on commit 49b2735

Please sign in to comment.