Resize testing
This commit is contained in:
24
src/main/kotlin/nl/astraeus/komp/ComponentSize.kt
Normal file
24
src/main/kotlin/nl/astraeus/komp/ComponentSize.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
package nl.astraeus.komp
|
||||
|
||||
/**
|
||||
* User: rnentjes
|
||||
* Date: 10-5-17
|
||||
* Time: 16:48
|
||||
*/
|
||||
|
||||
enum class SizeType {
|
||||
NONE,
|
||||
ABSOLUTE,
|
||||
PERCENTAGE,
|
||||
FLEX,
|
||||
FILL
|
||||
}
|
||||
|
||||
open class ComponentSize(
|
||||
val xType: SizeType,
|
||||
val yType: SizeType,
|
||||
val xValue: Float,
|
||||
val yValue: Float
|
||||
)
|
||||
|
||||
class NotSized : ComponentSize(SizeType.NONE, SizeType.NONE, 0f, 0f)
|
||||
@@ -36,4 +36,6 @@ abstract class HtmlComponent {
|
||||
open fun refresh() {
|
||||
Komp.refresh(element)
|
||||
}
|
||||
|
||||
open fun getSize(): ComponentSize? = null
|
||||
}
|
||||
|
||||
@@ -52,10 +52,43 @@ object Komp {
|
||||
if (element is HTMLElement && comp != null) {
|
||||
val parent = element.parentElement
|
||||
val newElement = comp.create()
|
||||
val size = comp.getSize()
|
||||
|
||||
if (size != null) {
|
||||
sizeElement(newElement, size)
|
||||
}
|
||||
|
||||
parent?.replaceChild(newElement, element)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun sizeElement(element: HTMLElement, size: ComponentSize) {
|
||||
var width = ""
|
||||
var height = ""
|
||||
val parent = element.parentElement as HTMLElement
|
||||
|
||||
when(size.xType) {
|
||||
SizeType.ABSOLUTE -> {
|
||||
width = "${size.xValue.toInt()}px"
|
||||
}
|
||||
SizeType.PERCENTAGE -> {
|
||||
width = "${(parent.clientWidth * size.xValue / 100f).toInt()}px"
|
||||
}
|
||||
SizeType.FILL -> {
|
||||
|
||||
}
|
||||
SizeType.FLEX -> {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (width.isNotBlank()) {
|
||||
element.style.width = width
|
||||
}
|
||||
if (height.isNotBlank()) {
|
||||
element.style.height = height
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user