From 35ab923e92c562ee2f375e4e7ae2e61ece530322 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Mon, 28 Jun 2021 23:01:26 -0400 Subject: [PATCH] Initial Jetbrains Compose 0.5.0 support --- build.gradle.kts | 6 +-- .../kotlin/ca/gosyer/ui/base/WindowDialog.kt | 41 +++++++++++------- src/main/kotlin/ca/gosyer/ui/main/main.kt | 10 ++++- .../kotlin/ca/gosyer/ui/reader/ReaderMenu.kt | 42 ++++++++++++------- 4 files changed, 65 insertions(+), 34 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f993d524..963e540d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,3 @@ -import Config.serverCode import Config.tachideskVersion import org.gradle.jvm.tasks.Jar import org.jetbrains.compose.compose @@ -11,7 +10,7 @@ plugins { kotlin("jvm") version "1.5.10" kotlin("kapt") version "1.5.10" kotlin("plugin.serialization") version "1.5.10" - id("org.jetbrains.compose") version "0.4.0" + id("org.jetbrains.compose") version "0.5.0-build227" id("de.fuerstenau.buildconfig") version "1.1.8" id("org.jmailen.kotlinter") version "3.4.5" id("com.github.ben-manes.versions") version "0.39.0" @@ -112,7 +111,8 @@ tasks { "-Xopt-in=com.russhwolf.settings.ExperimentalSettingsImplementation", "-Xopt-in=com.google.accompanist.pager.ExperimentalPagerApi", "-Xopt-in=androidx.compose.animation.ExperimentalAnimationApi", - "-Xopt-in=androidx.compose.material.ExperimentalMaterialApi" + "-Xopt-in=androidx.compose.material.ExperimentalMaterialApi", + "-Xopt-in=androidx.compose.ui.ExperimentalComposeUiApi" ) } } diff --git a/src/main/kotlin/ca/gosyer/ui/base/WindowDialog.kt b/src/main/kotlin/ca/gosyer/ui/base/WindowDialog.kt index ba7559ae..5329754e 100644 --- a/src/main/kotlin/ca/gosyer/ui/base/WindowDialog.kt +++ b/src/main/kotlin/ca/gosyer/ui/base/WindowDialog.kt @@ -25,7 +25,8 @@ import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.input.key.Key -import androidx.compose.ui.input.key.KeysSet +import androidx.compose.ui.input.key.KeyEvent +import androidx.compose.ui.input.key.key import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.dp @@ -48,7 +49,7 @@ fun WindowDialog( onNegativeButton: (() -> Unit)? = null, positiveButtonText: String = "OK", onPositiveButton: (() -> Unit)? = null, - keyboardShortcuts: List = emptyList(), + keyboardShortcuts: Map Boolean> = emptyMap(), row: @Composable (RowScope.() -> Unit) ) = launchUI { val window = AppWindow( @@ -74,11 +75,21 @@ fun WindowDialog( window.close() } - window.keyboard.setShortcut(Key.Enter, onPositiveButton.plusClose()) - window.keyboard.setShortcut(Key.Escape, onNegativeButton.plusClose()) - - keyboardShortcuts.forEach { - window.keyboard.setShortcut(it.key) { it.shortcut(window) } + window.keyboard.onKeyEvent = { + when { + it.key == Key.Enter -> { + onPositiveButton.plusClose()() + true + } + it.key == Key.Escape -> { + onNegativeButton.plusClose()() + true + } + keyboardShortcuts[it.key] != null -> { + keyboardShortcuts[it.key]?.invoke(it, window) ?: false + } + else -> false + } } val resources = AppScope.getInstance() @@ -123,7 +134,7 @@ fun WindowDialog( size: IntSize = IntSize(400, 200), onDismissRequest: (() -> Unit)? = null, forceFocus: Boolean = true, - keyboardShortcuts: List = emptyList(), + keyboardShortcuts: Map Boolean> = emptyMap(), buttons: @Composable (AppWindow) -> Unit, content: @Composable (AppWindow) -> Unit ) = launchUI { @@ -145,8 +156,13 @@ fun WindowDialog( } } - keyboardShortcuts.forEach { - window.keyboard.setShortcut(it.key) { it.shortcut(window) } + window.keyboard.onKeyEvent = { + when { + keyboardShortcuts[it.key] != null -> { + keyboardShortcuts[it.key]?.invoke(it, window) ?: false + } + else -> false + } } val resources = AppScope.getInstance() @@ -168,8 +184,3 @@ fun WindowDialog( } } } - -data class KeyboardShortcut(val key: KeysSet, val shortcut: (AppWindow) -> Unit) { - constructor(key: Key, shortcut: (AppWindow) -> Unit) : - this(KeysSet(key), shortcut) -} diff --git a/src/main/kotlin/ca/gosyer/ui/main/main.kt b/src/main/kotlin/ca/gosyer/ui/main/main.kt index 251294e8..253f6598 100644 --- a/src/main/kotlin/ca/gosyer/ui/main/main.kt +++ b/src/main/kotlin/ca/gosyer/ui/main/main.kt @@ -12,6 +12,7 @@ import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.key import androidx.compose.ui.unit.IntOffset import ca.gosyer.BuildConfig import ca.gosyer.core.logging.initializeLogger @@ -104,8 +105,13 @@ fun main() { } val backPressHandler = BackPressHandler() - window.keyboard.setShortcut(Key.Home) { - backPressHandler.handle() + window.keyboard.onKeyEvent = { + when (it.key) { + Key.Home -> { + backPressHandler.handle() + } + else -> false + } } window.events.onClose = { diff --git a/src/main/kotlin/ca/gosyer/ui/reader/ReaderMenu.kt b/src/main/kotlin/ca/gosyer/ui/reader/ReaderMenu.kt index 3d6f0ad2..c8b02b3b 100644 --- a/src/main/kotlin/ca/gosyer/ui/reader/ReaderMenu.kt +++ b/src/main/kotlin/ca/gosyer/ui/reader/ReaderMenu.kt @@ -32,6 +32,8 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEvent +import androidx.compose.ui.input.key.key import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp @@ -43,7 +45,6 @@ import ca.gosyer.data.reader.model.NavigationMode import ca.gosyer.data.translation.XmlResourceBundle import ca.gosyer.data.ui.UiPreferences import ca.gosyer.data.ui.model.WindowSettings -import ca.gosyer.ui.base.KeyboardShortcut import ca.gosyer.ui.base.components.ErrorScreen import ca.gosyer.ui.base.components.LoadingScreen import ca.gosyer.ui.base.components.mangaAspectRatio @@ -88,9 +89,9 @@ fun openReaderMenu(chapterIndex: Int, mangaId: Long) { window.maximize() } - val setHotkeys: (List) -> Unit = { shortcuts -> - shortcuts.forEach { - window.keyboard.setShortcut(it.key) { it.shortcut(window) } + val setHotkeys: (Map Boolean)>) -> Unit = { shortcuts -> + window.keyboard.onKeyEvent = { + shortcuts[it.key]?.invoke(it, window) ?: false } } @@ -125,7 +126,12 @@ fun openReaderMenu(chapterIndex: Int, mangaId: Long) { } @Composable -fun ReaderMenu(chapterIndex: Int, mangaId: Long, setHotkeys: (List) -> Unit, setOnCloseEvent: (() -> Unit) -> Unit) { +fun ReaderMenu( + chapterIndex: Int, + mangaId: Long, + setHotkeys: (Map Boolean)>) -> Unit, + setOnCloseEvent: (() -> Unit) -> Unit +) { val vm = viewModel { ReaderMenuViewModel.Params(chapterIndex, mangaId) } @@ -144,17 +150,25 @@ fun ReaderMenu(chapterIndex: Int, mangaId: Long, setHotkeys: (List Unit): (KeyEvent, AppWindow) -> Boolean { + return { _, _ -> + block() + true + } + } + LaunchedEffect(Unit) { setHotkeys( - listOf( - KeyboardShortcut(Key.W) { vm.navigate(Navigation.PREV) }, - KeyboardShortcut(Key.DirectionUp) { vm.navigate(Navigation.PREV) }, - KeyboardShortcut(Key.S) { vm.navigate(Navigation.NEXT) }, - KeyboardShortcut(Key.DirectionDown) { vm.navigate(Navigation.NEXT) }, - KeyboardShortcut(Key.A) { vm.navigate(Navigation.LEFT) }, - KeyboardShortcut(Key.DirectionLeft) { vm.navigate(Navigation.LEFT) }, - KeyboardShortcut(Key.D) { vm.navigate(Navigation.RIGHT) }, - KeyboardShortcut(Key.DirectionRight) { vm.navigate(Navigation.RIGHT) } + mapOf( + Key.W to hotkey { vm.navigate(Navigation.PREV) }, + Key.DirectionUp to hotkey { vm.navigate(Navigation.PREV) }, + Key.S to hotkey { vm.navigate(Navigation.NEXT) }, + Key.DirectionDown to hotkey { vm.navigate(Navigation.NEXT) }, + Key.A to hotkey { vm.navigate(Navigation.LEFT) }, + Key.DirectionLeft to hotkey { vm.navigate(Navigation.LEFT) }, + Key.D to hotkey { vm.navigate(Navigation.RIGHT) }, + Key.DirectionRight to hotkey { vm.navigate(Navigation.RIGHT) } ) ) setOnCloseEvent(vm::sendProgress)