-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from fmasa/pop-window
Pop screen to a new window
- Loading branch information
Showing
5 changed files
with
162 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
desktop/src/jvmMain/kotlin/cz/frantisekmasa/wfrp_master/desktop/ApplicationWindow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cz.frantisekmasa.wfrp_master.desktop | ||
|
||
import androidx.compose.material.DrawerValue | ||
import androidx.compose.material.rememberDrawerState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.window.Window | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cafe.adriel.voyager.navigator.Navigator | ||
import cz.frantisekmasa.wfrp_master.common.core.ui.navigation.ProvideNavigationTransaction | ||
import cz.frantisekmasa.wfrp_master.common.core.ui.responsive.ScreenWithBreakpoints | ||
import cz.frantisekmasa.wfrp_master.common.core.ui.scaffolding.KeyboardDispatcher | ||
import cz.frantisekmasa.wfrp_master.common.core.ui.scaffolding.LocalKeyboardDispatcher | ||
import cz.frantisekmasa.wfrp_master.common.core.ui.theme.Theme | ||
import cz.frantisekmasa.wfrp_master.common.localization.FixedStrings | ||
import cz.frantisekmasa.wfrp_master.common.shell.DrawerShell | ||
import cz.frantisekmasa.wfrp_master.common.shell.SnackbarScaffold | ||
import kotlinx.coroutines.launch | ||
import java.util.UUID | ||
|
||
@Immutable | ||
data class ApplicationWindowState( | ||
val initialScreen: Screen, | ||
val key: UUID, | ||
val isPrimary: Boolean, | ||
) | ||
|
||
@Composable | ||
fun ApplicationWindow( | ||
initialScreen: Screen, | ||
onCloseRequest: () -> Unit, | ||
onNewWindowRequest: (initialScreen: Screen) -> Unit, | ||
) { | ||
val keyboardDispatcher = remember { KeyboardDispatcher() } | ||
|
||
CompositionLocalProvider( | ||
LocalKeyboardDispatcher provides keyboardDispatcher, | ||
) { | ||
Window( | ||
title = FixedStrings.appName, | ||
onCloseRequest = onCloseRequest, | ||
onPreviewKeyEvent = { | ||
keyboardDispatcher.dispatch(it, beforeChildren = true) | ||
}, | ||
onKeyEvent = { | ||
keyboardDispatcher.dispatch(it, beforeChildren = false) | ||
}, | ||
) { | ||
Theme { | ||
SnackbarScaffold { | ||
Startup { | ||
ScreenWithBreakpoints { | ||
val drawerState = rememberDrawerState(DrawerValue.Closed) | ||
val coroutineScope = rememberCoroutineScope() | ||
|
||
Navigator( | ||
screens = listOf(initialScreen), | ||
onBackPressed = { | ||
if (drawerState.isOpen) { | ||
coroutineScope.launch { drawerState.close() } | ||
return@Navigator false | ||
} | ||
|
||
true | ||
} | ||
) { navigator -> | ||
DrawerShell(drawerState) { | ||
val screen = navigator.lastItem | ||
|
||
navigator.saveableState("currentScreen") { | ||
ProvideNavigationTransaction(screen) { | ||
Shortcuts( | ||
onNewWindowRequest = onNewWindowRequest, | ||
) | ||
screen.Content() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
desktop/src/jvmMain/kotlin/cz/frantisekmasa/wfrp_master/desktop/NavigationShortcuts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cz.frantisekmasa.wfrp_master.desktop | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.input.key.Key | ||
import androidx.compose.ui.input.key.KeyEventType | ||
import androidx.compose.ui.input.key.isAltPressed | ||
import androidx.compose.ui.input.key.isCtrlPressed | ||
import androidx.compose.ui.input.key.isShiftPressed | ||
import androidx.compose.ui.input.key.key | ||
import androidx.compose.ui.input.key.type | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cz.frantisekmasa.wfrp_master.common.core.ui.navigation.LocalNavigationTransaction | ||
import cz.frantisekmasa.wfrp_master.common.core.ui.scaffolding.KeyboardEffect | ||
|
||
@Composable | ||
fun Shortcuts(onNewWindowRequest: (initialScreen: Screen) -> Unit) { | ||
val transaction = LocalNavigationTransaction.current | ||
|
||
KeyboardEffect("global-shortcuts") { | ||
if (it.type != KeyEventType.KeyDown) { | ||
return@KeyboardEffect false | ||
} | ||
|
||
if (it.isAltPressed && it.key == Key.DirectionLeft) { | ||
transaction.goBack(popLast = false) | ||
return@KeyboardEffect true | ||
} | ||
|
||
if (it.isCtrlPressed && it.isShiftPressed && it.key == Key.T && transaction.canPop) { | ||
onNewWindowRequest(transaction.currentScreen) | ||
transaction.goBack() | ||
|
||
return@KeyboardEffect true | ||
} | ||
|
||
return@KeyboardEffect false | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
desktop/src/jvmMain/kotlin/cz/frantisekmasa/wfrp_master/desktop/Shortcuts.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters