Skip to content

Commit

Permalink
rework jvm precompose setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Oct 16, 2023
1 parent daf7db5 commit 19a7521
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 180 deletions.
3 changes: 1 addition & 2 deletions precompose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ version = rootProject.extra.get("precomposeVersion") as String
kotlin {
targetHierarchy.default {
common {
group("nonAndroid") {
group("nonAndroidJvm") {
withMacosArm64()
withMacosX64()
withIosX64()
withIosArm64()
withIosSimulatorArm64()
withJvm()
withJs()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
package moe.tlaster.precompose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.uikit.ComposeUIViewControllerConfiguration
import androidx.compose.ui.window.ComposeUIViewController
import moe.tlaster.precompose.lifecycle.LifecycleOwner
import moe.tlaster.precompose.lifecycle.LifecycleRegistry
import moe.tlaster.precompose.lifecycle.LocalLifecycleOwner
import moe.tlaster.precompose.stateholder.LocalStateHolder
import moe.tlaster.precompose.stateholder.StateHolder
import moe.tlaster.precompose.ui.BackDispatcher
import moe.tlaster.precompose.ui.BackDispatcherOwner
import moe.tlaster.precompose.ui.LocalBackDispatcherOwner
import platform.UIKit.UIViewController

@Suppress("FunctionName")
Expand All @@ -21,41 +11,8 @@ fun PreComposeApplication(
content: @Composable () -> Unit,
): UIViewController {
return ComposeUIViewController(configure) {
val holder = remember {
PreComposeWindowHolder()
}
ProvidePreComposeCompositionLocals(
holder,
) {
PreComposeApp {
content.invoke()
}
}
}

@Composable
fun ProvidePreComposeCompositionLocals(
holder: PreComposeWindowHolder = remember {
PreComposeWindowHolder()
},
content: @Composable () -> Unit,
) {
CompositionLocalProvider(
LocalLifecycleOwner provides holder,
LocalStateHolder provides holder.stateHolder,
LocalBackDispatcherOwner provides holder,
) {
content.invoke()
}
}

class PreComposeWindowHolder : LifecycleOwner, BackDispatcherOwner {
override val lifecycle by lazy {
LifecycleRegistry()
}
val stateHolder by lazy {
StateHolder()
}
override val backDispatcher by lazy {
BackDispatcher()
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
package moe.tlaster.precompose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.window.CanvasBasedWindow
import moe.tlaster.precompose.lifecycle.LifecycleOwner
import moe.tlaster.precompose.lifecycle.LifecycleRegistry
import moe.tlaster.precompose.lifecycle.LocalLifecycleOwner
import moe.tlaster.precompose.stateholder.LocalStateHolder
import moe.tlaster.precompose.stateholder.StateHolder
import moe.tlaster.precompose.ui.BackDispatcher
import moe.tlaster.precompose.ui.BackDispatcherOwner
import moe.tlaster.precompose.ui.LocalBackDispatcherOwner

/**
* Creates a new [CanvasBasedWindow] with the given [title] and [content].
Expand All @@ -32,40 +22,9 @@ fun preComposeWindow(
requestResize = requestResize,
applyDefaultStyles = applyDefaultStyles,
content = {
val holder = remember {
PreComposeWindowHolder()
}
ProvidePreComposeCompositionLocals(holder) {
content()
PreComposeApp {
content.invoke()
}
},
)
}

@Composable
fun ProvidePreComposeCompositionLocals(
holder: PreComposeWindowHolder = remember {
PreComposeWindowHolder()
},
content: @Composable () -> Unit,
) {
CompositionLocalProvider(
LocalLifecycleOwner provides holder,
LocalStateHolder provides holder.stateHolder,
LocalBackDispatcherOwner provides holder,
) {
content.invoke()
}
}

class PreComposeWindowHolder : LifecycleOwner, BackDispatcherOwner {
override val lifecycle by lazy {
LifecycleRegistry()
}
val stateHolder by lazy {
StateHolder()
}
override val backDispatcher by lazy {
BackDispatcher()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package moe.tlaster.precompose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.window.FrameWindowScope
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.rememberWindowState
import kotlinx.coroutines.flow.distinctUntilChanged
import moe.tlaster.precompose.lifecycle.Lifecycle
import moe.tlaster.precompose.lifecycle.LifecycleOwner
import moe.tlaster.precompose.lifecycle.LifecycleRegistry
Expand All @@ -21,6 +20,8 @@ import moe.tlaster.precompose.stateholder.StateHolder
import moe.tlaster.precompose.ui.BackDispatcher
import moe.tlaster.precompose.ui.BackDispatcherOwner
import moe.tlaster.precompose.ui.LocalBackDispatcherOwner
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent

@Composable
fun PreComposeWindow(
Expand All @@ -39,54 +40,65 @@ fun PreComposeWindow(
onKeyEvent: (KeyEvent) -> Boolean = { false },
content: @Composable FrameWindowScope.() -> Unit,
) {
val holder = remember {
PreComposeWindowHolder()
}
LaunchedEffect(Unit) {
snapshotFlow { state.isMinimized }
.distinctUntilChanged()
.collect {
holder.lifecycle.currentState = if (it) {
Lifecycle.State.InActive
} else {
Lifecycle.State.Active
}
}
}
ProvidePreComposeCompositionLocals(
holder,
) {
Window(
onCloseRequest = {
holder.lifecycle.currentState = Lifecycle.State.Destroyed
onCloseRequest.invoke()
},
state = state,
visible = visible,
title = title,
icon = icon,
undecorated = undecorated,
transparent = transparent,
resizable = resizable,
enabled = enabled,
focusable = focusable,
alwaysOnTop = alwaysOnTop,
onPreviewKeyEvent = onPreviewKeyEvent,
onKeyEvent = onKeyEvent,
content = {
Window(
onCloseRequest = onCloseRequest,
state = state,
visible = visible,
title = title,
icon = icon,
undecorated = undecorated,
transparent = transparent,
resizable = resizable,
enabled = enabled,
focusable = focusable,
alwaysOnTop = alwaysOnTop,
onPreviewKeyEvent = onPreviewKeyEvent,
onKeyEvent = onKeyEvent,
content = {
PreComposeApp {
content.invoke(this)
},
)
}
}
},
)
}

@Suppress("INVISIBLE_MEMBER")
@Composable
fun ProvidePreComposeCompositionLocals(
holder: PreComposeWindowHolder = remember {
PreComposeWindowHolder()
},
actual fun PreComposeApp(
modifier: Modifier,
content: @Composable () -> Unit,
) {
val holder = remember {
PreComposeWindowHolder()
}
val window = androidx.compose.ui.window.LocalWindow.current
if (window != null) {
val listener = remember {
object : WindowAdapter() {
override fun windowClosing(e: WindowEvent?) {
holder.lifecycle.currentState = Lifecycle.State.Destroyed
}
override fun windowStateChanged(e: WindowEvent?) {
when (e?.newState) {
java.awt.Frame.ICONIFIED -> {
holder.lifecycle.currentState = Lifecycle.State.InActive
}
else -> {
holder.lifecycle.currentState = Lifecycle.State.Active
}
}
}
}
}
DisposableEffect(window) {
window.addWindowListener(listener)
window.addWindowStateListener(listener)
onDispose {
window.removeWindowListener(listener)
window.removeWindowStateListener(listener)
}
}
}
CompositionLocalProvider(
LocalLifecycleOwner provides holder,
LocalStateHolder provides holder.stateHolder,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
package moe.tlaster.precompose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import moe.tlaster.precompose.lifecycle.LifecycleOwner
import moe.tlaster.precompose.lifecycle.LifecycleRegistry
import moe.tlaster.precompose.lifecycle.LocalLifecycleOwner
import moe.tlaster.precompose.stateholder.LocalStateHolder
import moe.tlaster.precompose.stateholder.StateHolder
import moe.tlaster.precompose.ui.BackDispatcher
import moe.tlaster.precompose.ui.BackDispatcherOwner
import moe.tlaster.precompose.ui.LocalBackDispatcherOwner

fun PreComposeWindow(
title: String,
Expand All @@ -25,42 +15,9 @@ fun PreComposeWindow(
onCloseRequest = onCloseRequest,
).apply {
setContent {
val holder = remember {
PreComposeWindowHolder()
}
ProvidePreComposeCompositionLocals(
holder,
) {
PreComposeApp {
content.invoke()
}
}
}
}

@Composable
fun ProvidePreComposeCompositionLocals(
holder: PreComposeWindowHolder = remember {
PreComposeWindowHolder()
},
content: @Composable () -> Unit,
) {
CompositionLocalProvider(
LocalLifecycleOwner provides holder,
LocalStateHolder provides holder.stateHolder,
LocalBackDispatcherOwner provides holder,
) {
content.invoke()
}
}

class PreComposeWindowHolder : LifecycleOwner, BackDispatcherOwner {
override val lifecycle by lazy {
LifecycleRegistry()
}
val stateHolder by lazy {
StateHolder()
}
override val backDispatcher by lazy {
BackDispatcher()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ actual fun PreComposeApp(
modifier: Modifier,
content: @Composable () -> Unit,
) {
val holder = remember {
PreComposeWindowHolder()
ProvidePreComposeCompositionLocals {
content.invoke()
}
}

@Composable
fun ProvidePreComposeCompositionLocals(
holder: PreComposeWindowHolder = remember {
PreComposeWindowHolder()
},
content: @Composable () -> Unit,
) {
CompositionLocalProvider(
LocalLifecycleOwner provides holder,
LocalStateHolder provides holder.stateHolder,
Expand Down

0 comments on commit 19a7521

Please sign in to comment.