Skip to content

Commit

Permalink
More work
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jun 21, 2024
1 parent 13bd207 commit bd1802f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class LightComponentApplier(val container: LightContainer) : AbstractApplier<Lig

override fun up() {
if (current is LightContainer) {
current.bounds = LightRect(0, 0, 1000, 400)
//current.bounds = current.parent?.bounds ?: LightRect(0, 0, 200, 200)
current.bounds = LightRect(0, 0, 300, 200)
(current as LightContainer).doRelayout()
}
//println("up: $current")
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/korge/composable/light/LightComponents.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package korge.composable.light

import androidx.compose.runtime.*
import korge.composable.light.awt.*

@Composable
fun LButton(text: String, enabled: Boolean = true, onClick: () -> Unit = { }) {
Expand Down Expand Up @@ -32,7 +33,7 @@ interface LightLabel : LightComponent {
}

@Composable
fun LContainer(relayout: (LightContainer) -> Unit = { }, content: @Composable () -> Unit) {
fun LContainer(relayout: LightLayout = DummyLightLayout, content: @Composable () -> Unit) {
val current = LocalLightComponents.current
ComposeLightComponent(
{ current.create<LightContainer>() },
Expand All @@ -45,20 +46,20 @@ fun LContainer(relayout: (LightContainer) -> Unit = { }, content: @Composable ()

interface LightContainer : LightComponent {
val componentCount: Int
var relayout: (LightContainer) -> Unit
var relayout: LightLayout
fun add(component: LightComponent, index: Int)
fun getComponent(index: Int): LightComponent
fun remove(index: Int)
fun removeAll() { repeat(componentCount) { remove(componentCount - 1) } }
fun doRelayout() {
relayout(this)
}
fun beginChanges() {
}
fun endChanges() {
}
}

fun LightContainer.doRelayout(apply: Boolean = true): LightSize {
return relayout(this, apply)
}

class LightContainerList(val container: LightContainer) : AbstractList<LightComponent>() {
override val size: Int get() = container.componentCount
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/korge/composable/light/LightComposable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ interface LightComponents {

inline fun <reified T : LightComponent> LightComponents.create(): T = create(T::class)

data class LightRect(val x: Int, val y: Int, val width: Int, val height: Int)
data class LightSize(val width: Int, val height: Int)
data class LightRect(val x: Int, val y: Int, val width: Int, val height: Int) {
val size = LightSize(width, height)
}

interface LightComponent {
val parent: LightContainer?
val components: LightComponents
var enabled: Boolean
var bounds: LightRect
val size: LightSize get() = bounds.size
var preferredSize: LightSize
}
28 changes: 18 additions & 10 deletions src/main/kotlin/korge/composable/light/LightSampleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ package korge.composable.light
import androidx.compose.runtime.*
import korge.composable.*

fun FillXLayout(it: LightContainer, apply: Boolean): LightSize {
var x = 0
//println("CHLDREN: ${it.children.size}")
val children = it.children
val bounds = it.bounds
val childWidth = bounds.width / children.size
for (c in children) {
val cpsize = c.preferredSize
//println(" - $c")
//c.bounds = LightRect(x, 0, width = childWidth, height = bounds.height)
if (apply) c.bounds = c.bounds.copy(x = x, y = 0, width = cpsize.width, height = cpsize.height)
//c.bounds = it.bounds.copy(x)
x += cpsize.width
}
return LightSize(x, 32)
}

@Composable
fun LightSampleApp() {
LContainer({
var x = 0
//println("CHLDREN: ${it.children.size}")
for (c in it.children) {
//println(" - $c")
c.bounds = LightRect(x, 0, 100, 40)
//c.bounds = it.bounds.copy(x)
x += 100
}
}) {
LContainer(::FillXLayout) {
var n by state(0)
LLabel(if (n < 0) "NEGATIVE WORLD" else "Hello world!")
LButton("-") { n-- }
Expand Down
57 changes: 39 additions & 18 deletions src/main/kotlin/korge/composable/light/awt/LightAwtComponents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object AwtLightComponentsExperiment {
LightSampleApp()
}

frame.minimumSize = Dimension(100, 100)
frame.minimumSize = Dimension(200, 200)
frame.setLocationRelativeTo(null)
frame.pack()
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
Expand All @@ -26,21 +26,26 @@ object AwtLightComponentsExperiment {
object AwtLightComponents : LightComponents {
override fun <T : LightComponent> create(clazz: KClass<T>): T {
val comp: AwtLightComponent<*> = when (clazz) {
LightLabel::class -> AwtLightLabel().also { it.component = JLabel() }
LightContainer::class -> AwtLightContainer().also { it.component = JPanel(); it.component.layout = null }
LightButton::class -> AwtLightButton().also { it.component = JButton(); it.registerEvent() }
LightLabel::class -> AwtLightLabel()
LightContainer::class -> AwtLightContainer()
LightButton::class -> AwtLightButton()
else -> TODO("Unsupported $clazz")
}
//comp.component.size = comp.component.preferredSize
return comp.also { it.components = this } as T
}
}

fun Rectangle.toLight(): LightRect = LightRect(x, y, width, height)
fun LightRect.toAwt(): Rectangle = Rectangle(x, y, width, height)

open class AwtLightComponent<TComponent : Component>() : LightComponent {
fun Dimension.toLight(): LightSize = LightSize(width, height)
fun LightSize.toAwt(): Dimension = Dimension(width, height)

abstract class AwtLightComponent<TComponent : Component>() : LightComponent {
override var parent: LightContainer? = null
override lateinit var components: AwtLightComponents
lateinit var component: TComponent
abstract val component: TComponent

override var enabled: Boolean
get() = component.isEnabled
Expand All @@ -49,60 +54,77 @@ open class AwtLightComponent<TComponent : Component>() : LightComponent {
override var bounds: LightRect
get() = component.bounds.toLight()
set(value) { component.bounds = value.toAwt() }
override var preferredSize: LightSize
get() = component.preferredSize.toLight()
set(value) { component.preferredSize = value.toAwt() }

val LightComponent.comp get() = (this as AwtLightComponent<*>).component
}

open class AwtLightButton() : AwtLightComponent<JButton>(), LightButton {
open class AwtLightButton(override val component: JButton = JButton()) : AwtLightComponent<JButton>(), LightButton {
override var text: String
get() = component.text
set(value) { component.text = value }
override var onClick: () -> Unit = {}

fun registerEvent() {
init {
component.addActionListener {
onClick()
}
}
}

open class AwtLightLabel() : AwtLightComponent<JLabel>(), LightLabel {
open class AwtLightLabel(override val component: JLabel = JLabel()) : AwtLightComponent<JLabel>(), LightLabel {
override var text: String
get() = component.text
set(value) { component.text = value }
}

class MyLayout : LayoutManager {
class MyLayout(val container: AwtLightContainer) : LayoutManager {
override fun addLayoutComponent(name: String?, comp: Component?) {
}

override fun removeLayoutComponent(comp: Component?) {
}

override fun preferredLayoutSize(parent: Container): Dimension {
return parent.size
return container.doRelayout(apply = false).toAwt()
//return parent.size
}


override fun minimumLayoutSize(parent: Container): Dimension {
//TODO("Not yet implemented")
return parent.minimumSize
}

override fun layoutContainer(parent: Container) {
TODO("Not yet implemented")
println("layoutContainer: $parent")
//this.container.bounds = LightRect(0, 0, parent.width, parent.height)
//println(this.container.bounds)
this.container.doRelayout(true)
}
}

class AwtLightContainer() : AwtLightComponent<Container>(), LightContainer {
typealias LightLayout = (parent: LightContainer, apply: Boolean) -> LightSize

val DummyLightLayout: LightLayout = { parent, apply -> parent.size }

class AwtLightContainer(override val component: JPanel = JPanel()) : AwtLightComponent<Container>(), LightContainer {
val comps = arrayListOf<LightComponent>()
val container get() = this.component

override var relayout: (LightContainer) -> Unit = {}
init {
component.layout = MyLayout(this)
}

override var relayout: LightLayout = DummyLightLayout

override val componentCount: Int get() = comps.size

override fun add(component: LightComponent, index: Int) {
//println("ADD: $component, index=$index")
(component as AwtLightComponent<*>).parent = this
container.add(component.comp, index)
//comps.add(index, component)
if (index < 0) comps.add(component) else comps.add(index, component)
Expand All @@ -113,21 +135,20 @@ class AwtLightContainer() : AwtLightComponent<Container>(), LightContainer {

override fun remove(index: Int) {
//println("REMOVE: index=$index")
(component as AwtLightComponent<*>).parent = null
container.remove(index)
comps.removeAt(index)
//doRelayout()
}
override fun removeAll() {
//println("REMOVE_ALL")
comps.forEach { (it as AwtLightComponent<*>).parent = null }

container.removeAll()
comps.clear()
//doRelayout()
}

override fun doRelayout() {
super.doRelayout()
}

override fun endChanges() {
super.endChanges()
component.repaint()
Expand Down

0 comments on commit bd1802f

Please sign in to comment.