diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/GtkEnvBackend.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/GtkEnvBackend.kt index a8c672b..7740590 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/GtkEnvBackend.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/GtkEnvBackend.kt @@ -74,54 +74,50 @@ object GtkEnvBackend : EnvBackend { ok: Runnable?, cancel: Runnable? ) { - run { - val dialog = AlertDialog("") - dialog.message = title - dialog.detail = markup - dialog.modal = true - when { - cancel == null -> { - dialog.setButtons(arrayOf(I18n["ok"])) - dialog.defaultButton = 0 - dialog.cancelButton = -1 - } - ok == null -> { - dialog.setButtons(arrayOf("Cancel")) - dialog.defaultButton = -1 - dialog.cancelButton = 0 - } - else -> { - dialog.setButtons(arrayOf("OK", "Cancel")) - dialog.defaultButton = 0 - dialog.cancelButton = 1 - } + val dialog = AlertDialog("") + dialog.message = title + dialog.detail = markup + dialog.modal = true + when { + cancel == null -> { + dialog.setButtons(arrayOf(I18n["ok"])) + dialog.defaultButton = 0 + dialog.cancelButton = -1 } - dialog.choose(parent, Cancellable()) { _, res, _ -> - val result = dialog.chooseFinish(res) - val cancelIdx = dialog.cancelButton - val defaultIdx = dialog.defaultButton - if (result == cancelIdx) cancel?.run() - if (result == defaultIdx) ok?.run() + ok == null -> { + dialog.setButtons(arrayOf("Cancel")) + dialog.defaultButton = -1 + dialog.cancelButton = 0 } + else -> { + dialog.setButtons(arrayOf("OK", "Cancel")) + dialog.defaultButton = 0 + dialog.cancelButton = 1 + } + } + dialog.choose(parent, Cancellable()) { _, res, _ -> + val result = dialog.chooseFinish(res) + val cancelIdx = dialog.cancelButton + val defaultIdx = dialog.defaultButton + if (result == cancelIdx) cancel?.run() + if (result == defaultIdx) ok?.run() } } - private fun processResponses(dialog: Dialog, ok: Runnable?, cancel: Runnable?): Dialog.Response { - return Dialog.Response { responseId: Int -> - when (ResponseType.of(responseId)) { - ResponseType.OK -> { - dialog.close() - ok?.run() - } - - ResponseType.CLOSE, ResponseType.CANCEL -> { - dialog.close() - cancel?.run() - } - - ResponseType.DELETE_EVENT -> dialog.destroy() - else -> Utils.LOGGER.error("Unexpected response type: $responseId") + private fun processResponses(dialog: Dialog, ok: Runnable?, cancel: Runnable?): Dialog.Response = Dialog.Response { responseId: Int -> + when (ResponseType.of(responseId)) { + ResponseType.OK -> { + dialog.close() + ok?.run() } + + ResponseType.CLOSE, ResponseType.CANCEL -> { + dialog.close() + cancel?.run() + } + + ResponseType.DELETE_EVENT -> dialog.destroy() + else -> Utils.LOGGER.error("Unexpected response type: $responseId") } } } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/GtkMain.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/GtkMain.kt index e271718..925be2a 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/GtkMain.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/GtkMain.kt @@ -31,19 +31,17 @@ object GtkMain { } @JvmStatic - fun showGui(args: Array): Int { - return setupApplication(args) { - //TODO update check - AccountManager.loadAccounts() - GtkMenubar.create(this) - val window = MainWindow(this) - window.visible = true - GtkEnvBackend.dialogParent = window - window.onCloseRequest { - GtkEnvBackend.dialogParent = null - this.quit() - false - } + fun showGui(args: Array): Int = setupApplication(args) { + //TODO update check + AccountManager.loadAccounts() + GtkMenubar.create(this) + val window = MainWindow(this) + window.visible = true + GtkEnvBackend.dialogParent = window + window.onCloseRequest { + GtkEnvBackend.dialogParent = null + this.quit() + false } } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/ILabel.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/ILabel.kt index fd9d8a2..bac3214 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/ILabel.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/ILabel.kt @@ -7,8 +7,11 @@ import org.gnome.gtk.Gtk import org.gnome.gtk.Label import org.jetbrains.annotations.PropertyKey -class ILabel(str: @PropertyKey(resourceBundle = I18n.BUNDLE) String, mode: Mode, vararg args: Any?) : - Label(I18n.get(str, *args)) { +class ILabel( + str: @PropertyKey(resourceBundle = I18n.BUNDLE) String, + mode: Mode, + vararg args: Any? +) : Label(I18n.get(str, *args)) { constructor(str: @PropertyKey(resourceBundle = I18n.BUNDLE) String, vararg args: Any?) : this(str, Mode.NORMAL, *args) init { @@ -36,17 +39,14 @@ class ILabel(str: @PropertyKey(resourceBundle = I18n.BUNDLE) String, mode: Mode, provider } - @JvmStatic - fun theme(label: Label, mode: Mode) { - when (mode) { - Mode.HEADING -> label.addCssClass("heading") - Mode.SUBTITLE -> { - label.addCssClass("jf-subtitle") - label.styleContext.addProvider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) - } - - Mode.NORMAL -> {} + fun theme(label: Label, mode: Mode) = when (mode) { + Mode.HEADING -> label.addCssClass("heading") + Mode.SUBTITLE -> { + label.addCssClass("jf-subtitle") + label.styleContext.addProvider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) } + + Mode.NORMAL -> {} } } } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/InstanceThumbnail.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/InstanceThumbnail.kt index 7983b0f..208d20b 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/InstanceThumbnail.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/InstanceThumbnail.kt @@ -41,8 +41,6 @@ class InstanceThumbnail : Stack { private const val IMAGE = "image" private const val GENERIC = "generic" @JvmStatic - fun castFrom(stack: Stack): InstanceThumbnail { - return InstanceThumbnail(stack.handle()) - } + fun castFrom(stack: Stack): InstanceThumbnail = InstanceThumbnail(stack.handle()) } } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/IRow.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/IRow.kt index bfb4504..549ffe7 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/IRow.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/IRow.kt @@ -36,22 +36,20 @@ class IRow( append(head) } - fun setButton(text: @PropertyKey(resourceBundle = I18n.BUNDLE) String, action: Button.Clicked?): Button { - return Button.newWithLabel(I18n[text]).apply { + fun setButton(text: @PropertyKey(resourceBundle = I18n.BUNDLE) String, action: Button.Clicked?): Button = + Button.newWithLabel(I18n[text]).apply { packSmallEnd() onClicked(action) } - } - fun setDropdown(options: Array, defaultIndex: Int, changed: IntConsumer): KDropDown { - return KDropDown(options, { it } , defaultIndex).apply { + fun setDropdown(options: Array, defaultIndex: Int, changed: IntConsumer): KDropDown = + KDropDown(options, { it } , defaultIndex).apply { onChange(changed) packSmallEnd() } - } - fun setSwitch(value: Boolean, changed: Consumer): Switch { - return Switch().apply { + fun setSwitch(value: Boolean, changed: Consumer): Switch = + Switch().apply { packSmallEnd() active = value onStateSet { state: Boolean -> @@ -59,25 +57,22 @@ class IRow( false } } - } - fun setSpinButton(value: Double, min: Double, max: Double, step: Double, changed: DoubleConsumer): SpinButton { - return SpinButton.newWithRange(min, max, step).apply { + fun setSpinButton(value: Double, min: Double, max: Double, step: Double, changed: DoubleConsumer): SpinButton = + SpinButton.newWithRange(min, max, step).apply { packSmallEnd() this.value = value onValueChanged { changed.accept(this.value) } } - } - fun setEntry(value: String?, changed: Consumer): KEntry { - return KEntry(value).apply { + fun setEntry(value: String?, changed: Consumer): KEntry = + KEntry(value).apply { hexpand = true valign = Align.CENTER halign = Align.FILL onChange(changed) append(this) } - } private fun Widget.packSmallEnd() { firstChild!!.hexpand = true diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsTab.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsTab.kt index a5a94e3..1081c6b 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsTab.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsTab.kt @@ -9,7 +9,7 @@ open class SettingsTab( protected val window: Window?, val content: T ) { - protected fun showError(message: String, t: Throwable) { + protected fun showError(message: String, t: Throwable) = GtkEnvBackend.simpleDialog( window, StringFormatter.toString(t), @@ -17,5 +17,4 @@ open class SettingsTab( null, null ) - } } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsWindow.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsWindow.kt index 0676c25..959ae5e 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsWindow.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsWindow.kt @@ -10,7 +10,6 @@ import org.gnome.gtk.* import org.jetbrains.annotations.PropertyKey open class SettingsWindow(app: Application?) : Window() { - @JvmField protected val stack: ViewStack init { diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltMenuItem.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltMenuItem.kt index 6ef490b..d21a780 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltMenuItem.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltMenuItem.kt @@ -4,7 +4,7 @@ import org.gnome.gio.MenuItem import org.gnome.gio.SimpleAction import org.gnome.gio.ThemedIcon -abstract class BuiltMenuItem protected constructor(action: SimpleAction, @JvmField protected val menuItem: MenuItem?) { +abstract class BuiltMenuItem protected constructor(action: SimpleAction, protected val menuItem: MenuItem?) { @JvmField protected val action: SimpleAction diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltRadioItem.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltRadioItem.kt index fdc2653..63a10eb 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltRadioItem.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltRadioItem.kt @@ -5,8 +5,8 @@ import org.gnome.glib.Variant class BuiltRadioItem(action: SimpleAction, private val options: List) : BuiltMenuItem(action, null) { var selected: T - get() = options[action.getState()!!.getInt32()] + get() = options[action.state!!.int32] set(selected) { - action.setState(Variant.newInt32(options.indexOf(selected))) + action.state = Variant.newInt32(options.indexOf(selected)) } } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltToggleItem.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltToggleItem.kt index 9d05712..b52c931 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltToggleItem.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/BuiltToggleItem.kt @@ -6,7 +6,7 @@ import org.gnome.glib.Variant class BuiltToggleItem(action: SimpleAction, menuItem: MenuItem?) : BuiltMenuItem(action, menuItem) { var state: Boolean - get() = action.getState()!!.boolean + get() = action.state!!.boolean set(state) { action.state = Variant.newBoolean(state) } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/MenuBuilder.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/MenuBuilder.kt index 8091ed6..1b490d5 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/MenuBuilder.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/menu/MenuBuilder.kt @@ -92,7 +92,7 @@ class MenuBuilder private constructor(map: ActionMap, menu: Menu, prefix: String addAction(name, action) action.onActivate { variant: Variant? -> action.state = variant - onCheck.accept(options[variant!!.getInt32()]) + onCheck.accept(options[variant!!.int32]) } for ((i, option) in options.withIndex()) { menu.appendItem(MenuItem(stringifier.apply(i, option), "$groupName$name($i)")) diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/I18n.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/I18n.kt index ad4208e..fa44eb8 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/I18n.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/I18n.kt @@ -8,12 +8,10 @@ object I18n { private val bundle = ResourceBundle.getBundle(BUNDLE) @JvmStatic - operator fun get(key: @PropertyKey(resourceBundle = BUNDLE) String): String { - return bundle.getString(key) - } + operator fun get(key: @PropertyKey(resourceBundle = BUNDLE) String): String = + bundle.getString(key) @JvmStatic - operator fun get(key: @PropertyKey(resourceBundle = BUNDLE) String, vararg args: Any?): String { - return String.format(bundle.getString(key), *args) - } + operator fun get(key: @PropertyKey(resourceBundle = BUNDLE) String, vararg args: Any?): String = + String.format(bundle.getString(key), *args) } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/Memory.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/Memory.kt index 039534e..ed0a972 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/Memory.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/Memory.kt @@ -12,9 +12,9 @@ object Memory { const val MB = KB * 1024 const val GB = MB * 1024 private val impl = when (OSUtils.TYPE) { - OSUtils.Type.LINUX -> LinuxMI() - OSUtils.Type.WINDOWS -> WindowsMI() - OSUtils.Type.MAC_OS -> MacOsMI() + OSUtils.Type.LINUX -> LinuxMI + OSUtils.Type.WINDOWS -> WindowsMI + OSUtils.Type.MAC_OS -> MacOsMI } private val totalMemory by lazy { impl.getTotalMemory() } val maxMBForInstance: Long get() = (totalMemory / MB - 1024).coerceAtLeast(1024) @@ -23,7 +23,7 @@ object Memory { fun getTotalMemory(): Long } - private class LinuxMI : MI { + private object LinuxMI : MI { override fun getTotalMemory(): Long { try { Files.lines(Path.of("/proc/meminfo")).use { stream -> @@ -45,49 +45,47 @@ object Memory { } } - companion object { - // Taken from oshi - private val BYTES_PATTERN = Pattern.compile("(\\d+) ?([kKMGT]?B?).*") - private val WHITESPACES = Pattern.compile("\\s+") - private fun parseDecimalMemorySizeToBinary(size: String): Long { - var mem = WHITESPACES.split(size) - if (mem.size < 2) { - // If no spaces, use regexp - val matcher = BYTES_PATTERN.matcher(size.trim { it <= ' ' }) - if (matcher.find() && matcher.groupCount() == 2) { - mem = arrayOfNulls(2) - mem[0] = matcher.group(1) - mem[1] = matcher.group(2) - } + // Taken from oshi + private val BYTES_PATTERN = Pattern.compile("(\\d+) ?([kKMGT]?B?).*") + private val WHITESPACES = Pattern.compile("\\s+") + private fun parseDecimalMemorySizeToBinary(size: String): Long { + var mem = WHITESPACES.split(size) + if (mem.size < 2) { + // If no spaces, use regexp + val matcher = BYTES_PATTERN.matcher(size.trim { it <= ' ' }) + if (matcher.find() && matcher.groupCount() == 2) { + mem = arrayOfNulls(2) + mem[0] = matcher.group(1) + mem[1] = matcher.group(2) } - var capacity = parseLongOrDefault(mem[0], 0L) - if (mem.size == 2 && mem[1]!!.length > 1) { - when (mem[1]!![0]) { - 'T' -> capacity = capacity shl 40 - 'G' -> capacity = capacity shl 30 - 'M' -> capacity = capacity shl 20 - 'K', 'k' -> capacity = capacity shl 10 - else -> {} - } - } - return capacity } + var capacity = parseLongOrDefault(mem[0], 0L) + if (mem.size == 2 && mem[1]!!.length > 1) { + when (mem[1]!![0]) { + 'T' -> capacity = capacity shl 40 + 'G' -> capacity = capacity shl 30 + 'M' -> capacity = capacity shl 20 + 'K', 'k' -> capacity = capacity shl 10 + else -> {} + } + } + return capacity + } - private fun parseLongOrDefault(s: String, defaultLong: Long): Long = try { - s.toLong() - } catch (e: NumberFormatException) { - defaultLong - } + private fun parseLongOrDefault(s: String, defaultLong: Long): Long = try { + s.toLong() + } catch (e: NumberFormatException) { + defaultLong } } - private class WindowsMI : MI { + private object WindowsMI : MI { override fun getTotalMemory(): Long { return 32 * GB // This is currently unsupported, but any implementations by Windows user using panama are welcome } } - private class MacOsMI : MI { + private object MacOsMI : MI { override fun getTotalMemory(): Long { return 32 * GB // This is currently unsupported, but any implementations by MacOS user using panama are welcome } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/StreamUtil.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/StreamUtil.kt index 6416721..224a329 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/StreamUtil.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/util/StreamUtil.kt @@ -2,4 +2,4 @@ package io.gitlab.jfronny.inceptum.gtk.util import java.util.stream.Stream -inline fun Stream.toTypedArray(): Array = toArray { arrayOfNulls(it) } \ No newline at end of file +inline fun Stream.toTypedArray(): Array = toArray(::arrayOfNulls) \ No newline at end of file diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/dialog/MicrosoftLoginDialog.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/dialog/MicrosoftLoginDialog.kt index 433139b..e4ba9f4 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/dialog/MicrosoftLoginDialog.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/dialog/MicrosoftLoginDialog.kt @@ -9,7 +9,7 @@ import org.gnome.gtk.* import java.net.URI import java.net.URISyntaxException -class MicrosoftLoginDialog @JvmOverloads constructor( +class MicrosoftLoginDialog( parent: Window?, account: MicrosoftAccount? = null, onClose: Runnable? = null