From c422bee0671cba0f6bc12dcd528ca30cd9d4d5b6 Mon Sep 17 00:00:00 2001 From: Ivan Morgillo Date: Mon, 29 Jul 2024 16:40:02 +0200 Subject: [PATCH] Add basic shortcuts to Standalone L1 navigation screens (#502) * add basic shortcuts to L1 screens Signed-off-by: Ivan Morgillo * make linter happy Signed-off-by: Ivan Morgillo * hoist main screen shortcuts navigation code Signed-off-by: Ivan Morgillo --------- Signed-off-by: Ivan Morgillo --- .../jewel/samples/standalone/Main.kt | 52 +++++++++++++++++-- .../standalone/viewmodel/MainViewModel.kt | 4 ++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/Main.kt b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/Main.kt index 6dff5e969..95c2026cb 100644 --- a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/Main.kt +++ b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/Main.kt @@ -1,6 +1,12 @@ package org.jetbrains.jewel.samples.standalone import androidx.compose.ui.graphics.painter.Painter +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEvent +import androidx.compose.ui.input.key.KeyEventType +import androidx.compose.ui.input.key.isAltPressed +import androidx.compose.ui.input.key.key +import androidx.compose.ui.input.key.type import androidx.compose.ui.res.ResourceLoader import androidx.compose.ui.res.loadSvgPainter import androidx.compose.ui.text.font.FontFamily @@ -22,6 +28,7 @@ import org.jetbrains.jewel.intui.window.styling.light import org.jetbrains.jewel.intui.window.styling.lightWithLightHeader import org.jetbrains.jewel.samples.standalone.view.TitleBarView import org.jetbrains.jewel.samples.standalone.viewmodel.MainViewModel +import org.jetbrains.jewel.samples.standalone.viewmodel.MainViewModel.currentView import org.jetbrains.jewel.ui.ComponentStyling import org.jetbrains.jewel.window.DecoratedWindow import org.jetbrains.jewel.window.styling.TitleBarStyle @@ -68,11 +75,48 @@ fun main() { onCloseRequest = { exitApplication() }, title = "Jewel standalone sample", icon = icon, - ) { - TitleBarView() - MainViewModel.currentView.content() - } + onKeyEvent = { keyEvent -> + processKeyShortcuts( + keyEvent = keyEvent, + onNavigateTo = MainViewModel::onNavigateTo, + ) + }, + content = { + TitleBarView() + currentView.content() + }, + ) + } + } +} + +/* + Alt + W -> Welcome + Alt + M -> Markdown + Alt + C -> Components + */ +private fun processKeyShortcuts( + keyEvent: KeyEvent, + onNavigateTo: (String) -> Unit, +): Boolean { + if (!keyEvent.isAltPressed || keyEvent.type != KeyEventType.KeyDown) return false + return when (keyEvent.key) { + Key.W -> { + onNavigateTo("Welcome") + true } + + Key.M -> { + onNavigateTo("Markdown") + true + } + + Key.C -> { + onNavigateTo("Components") + true + } + + else -> false } } diff --git a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/viewmodel/MainViewModel.kt b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/viewmodel/MainViewModel.kt index 3939abd6e..95977091b 100644 --- a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/viewmodel/MainViewModel.kt +++ b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/viewmodel/MainViewModel.kt @@ -9,6 +9,10 @@ import org.jetbrains.jewel.samples.standalone.IntUiThemes import org.jetbrains.jewel.samples.standalone.reflection.findViews object MainViewModel { + fun onNavigateTo(destination: String) { + currentView = views.first { viewInfo -> viewInfo.title == destination } + } + var theme: IntUiThemes by mutableStateOf(IntUiThemes.Light) var swingCompat: Boolean by mutableStateOf(false)