Преглед изворни кода

a game with focus state, will be improved later

milan jurkulák пре 2 година
родитељ
комит
4d4e0c5b31

+ 12 - 12
src/main/kotlin/eu/mjdev/desktop/Main.kt

@@ -21,24 +21,22 @@ fun main() = application(
     val panelState = rememberVisibilityState()
     val menuState = rememberVisibilityState()
 
-    val handleMenuFocus: (Boolean) -> Unit = {
-        if (!menuState.isWindowFocus) {
+    val handleMenuFocus: (Boolean) -> Unit = { focus ->
+        if (!menuState.isWindowFocus && !focus) {
             menuState.hide()
         }
     }
 
-    val handlePanelFocus: (Boolean) -> Unit = {
-
+    val handlePanelFocus: (Boolean) -> Unit = { //focus ->
+//        val somethingVisible = menuState.isVisible || controlCenterState.isVisible
+//        if (!focus && !somethingVisible) {
+//            panelState.hide()
+//        }
     }
 
     val handleControlCenterFocus: (Boolean) -> Unit = { focus ->
-        if (focus) {
-//            menuState.hide()
-//            panelState.hide()
-        } else {
-            if (!controlCenterState.isWindowFocus) {
-                controlCenterState.hide()
-            }
+        if (!focus) {
+            controlCenterState.hide()
         }
     }
 
@@ -56,7 +54,9 @@ fun main() = application(
             ) {
                 DesktopPanel(
                     panelState = panelState,
-                    onMenuIconClicked = { if (!menuState.isVisible) menuState.show() },
+                    onMenuIconClicked = {
+                        menuState.toggle()
+                    },
                     onFocusChange = { focus -> handlePanelFocus(focus) }
                 )
                 AppsMenu(

+ 12 - 13
src/main/kotlin/eu/mjdev/desktop/components/sliding/VisibilityState.kt

@@ -1,25 +1,25 @@
 package eu.mjdev.desktop.components.sliding
 
-import androidx.compose.runtime.*
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.MutableState
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
 import androidx.compose.ui.layout.LayoutCoordinates
 import androidx.compose.ui.unit.DpSize
 import eu.mjdev.desktop.helpers.DpBounds
 import eu.mjdev.desktop.helpers.DpBounds.Companion.toDpBounds
 import eu.mjdev.desktop.provider.DesktopProvider
 import eu.mjdev.desktop.provider.DesktopProvider.Companion.LocalDesktop
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.launch
 
-@Suppress("CanBeParameter", "unused")
+@Suppress("CanBeParameter", "unused", "MemberVisibilityCanBePrivate")
 class VisibilityState(
-    private val scope: CoroutineScope,
     private val startState: Boolean = false,
     var enabled: Boolean = true,
     val containerSize: DpSize = DpSize.Zero
 ) {
-    val visibleState: MutableState<Boolean> = mutableStateOf(startState)
-    val boundsState: MutableState<DpBounds> = mutableStateOf(DpBounds.Zero)
-    val focusState: MutableState<Boolean> = mutableStateOf(false)
+    private val visibleState: MutableState<Boolean> = mutableStateOf(startState)
+    private val boundsState: MutableState<DpBounds> = mutableStateOf(DpBounds.Zero)
+    private val focusState: MutableState<Boolean> = mutableStateOf(false)
 
     var bounds: DpBounds
         get() = boundsState.value
@@ -38,11 +38,11 @@ class VisibilityState(
         }
 
     val isWindowFocus
-        get() = isVisible && focusState.value
+        get() = focusState.value
 
     fun show() {
         if (!isVisible) {
-            if (enabled) scope.launch {
+            if (enabled) {
                 visibleState.value = true
             }
         }
@@ -50,7 +50,7 @@ class VisibilityState(
 
     fun hide() {
         if (isVisible) {
-            if (enabled) scope.launch {
+            if (enabled) {
                 visibleState.value = false
             }
         }
@@ -75,9 +75,8 @@ class VisibilityState(
             enabled: Boolean = true,
             api: DesktopProvider = LocalDesktop.current,
         ): VisibilityState {
-            val scope = rememberCoroutineScope()
             return remember(startState, enabled, api.containerSize) {
-                VisibilityState(scope, startState, enabled, api.containerSize)
+                VisibilityState(startState, enabled, api.containerSize)
             }
         }
     }

+ 9 - 1
src/main/kotlin/eu/mjdev/desktop/windows/ChromeWindow.kt

@@ -25,6 +25,7 @@ fun ChromeWindow(
     enabled: Boolean = true,
     focusable: Boolean = true,
     alwaysOnTop: Boolean = true,
+    alwaysOnBottom: Boolean = false,
     enterAnimation: EnterTransition = fadeIn() + slideInVertically(initialOffsetY = { it }),
     exitAnimation: ExitTransition = slideOutVertically(targetOffsetY = { it }) + fadeOut(),
     onCloseRequest: () -> Unit = {},
@@ -42,6 +43,7 @@ fun ChromeWindow(
     enabled,
     focusable,
     alwaysOnTop,
+    alwaysOnBottom,
     enterAnimation,
     exitAnimation,
     onCloseRequest,
@@ -63,6 +65,7 @@ fun ChromeWindow(
     enabled: Boolean = true,
     focusable: Boolean = true,
     alwaysOnTop: Boolean = true,
+    alwaysOnBottom: Boolean = false,
     enterAnimation: EnterTransition = fadeIn() + slideInVertically(initialOffsetY = { it }),
     exitAnimation: ExitTransition = slideOutVertically(targetOffsetY = { it }) + fadeOut(),
     onCloseRequest: () -> Unit = {},
@@ -80,6 +83,7 @@ fun ChromeWindow(
     enabled,
     focusable,
     alwaysOnTop,
+    alwaysOnBottom,
     enterAnimation,
     exitAnimation,
     onCloseRequest,
@@ -100,6 +104,7 @@ fun ChromeWindow(
     enabled: Boolean = true,
     focusable: Boolean = true,
     alwaysOnTop: Boolean = true,
+    alwaysOnBottom: Boolean = false,
     enterAnimation: EnterTransition = fadeIn() + slideInVertically(initialOffsetY = { it }),
     exitAnimation: ExitTransition = slideOutVertically(targetOffsetY = { it }) + fadeOut(),
     onCloseRequest: () -> Unit = {},
@@ -113,7 +118,9 @@ fun ChromeWindow(
     val animState = rememberAnimState(visible)
     val wnState = rememberWnState()
     windowState.onOpened.add {
-        if (focusable) {
+        if (alwaysOnBottom) {
+            windowState.window?.toBack()
+        } else {
             windowState.requestFocus()
         }
         windowState.updatePositionAndSize()
@@ -136,6 +143,7 @@ fun ChromeWindow(
         enabled,
         focusable,
         alwaysOnTop,
+        alwaysOnBottom,
         onPreviewKeyEvent,
         onKeyEvent,
         stateHelper = windowState.stateHelper,

+ 6 - 0
src/main/kotlin/eu/mjdev/desktop/windows/WindowEx.kt

@@ -21,6 +21,7 @@ fun WindowEx(
     enabled: Boolean = true,
     focusable: Boolean = true,
     alwaysOnTop: Boolean = false,
+    alwaysOnBottom: Boolean = false,
     onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
     onKeyEvent: (KeyEvent) -> Boolean = { false },
     focusHelper: WindowFocusHelper,
@@ -43,6 +44,11 @@ fun WindowEx(
     content = {
         content()
         with(window) {
+            if (alwaysOnBottom) {
+                toBack()
+            } else if (alwaysOnTop) {
+                toFront()
+            }
             stateHelper.register(this)
             focusHelper.register(this)
         }

+ 1 - 1
src/main/kotlin/eu/mjdev/desktop/windows/WindowStateHelper.kt

@@ -83,6 +83,6 @@ class WindowStateHelper(
     }
 
     fun requestFocus() {
-        window?.requestFocus()
+        window?.toFront()
     }
 }