Nick
01/08/2023, 5:12 PMlayout
is protected
to prevent arbitrary code from changing it for a view that assumes it has a particular one. Same goes for the render. But it’s easy to make a new view with whatever you’d like for these. The first option is to use the view builder. This will let you specify the render behavior (but not layout, though this might make sense to expose there as well).
val view = view {
render = {
// this points to a Canvas
rect(…)
}
}
The second is to override render or set layout in a derived class:
object: View() {
init {
layout = …
}
override render(…) {…}
}
The container
builder lets you set the layout
by the way.Cherrio LLC
01/08/2023, 7:52 PMNick
01/08/2023, 8:04 PMCherrio LLC
01/08/2023, 9:23 PMCherrio LLC
01/08/2023, 9:26 PMNick
01/08/2023, 9:29 PMDisplay
size. do you have a layout
set on the Display
? how do you position your views?Nick
01/08/2023, 9:32 PMDisplay
size will change when to top-level window (or element if your app is embedded) changes like this. the `Display`’s layout
(if it has one) will be called to reposition the top level views (just like a container’s layout would be called if it changes size). so you can set a layout on the Display to manage this automatically.
Can you share how you position the views?Cherrio LLC
01/08/2023, 10:01 PMconstrain(views.first()){
<http://it.top|it.top> eq 0
it.left eq 0
Cherrio LLC
01/08/2023, 10:02 PMCherrio LLC
01/08/2023, 10:22 PMNick
01/08/2023, 10:33 PMit.width eq parent.width
it.height eq parent.height
or
it.right eq parent.right
it.bottom eq parent.bottom
you could also use a fill
like this
display.layout = constrain(view, fill)
there’s also a version of fill that takes an Insets
if you’d like some padding around the view.Cherrio LLC
01/08/2023, 10:35 PMNick
01/08/2023, 10:44 PMCherrio LLC
01/08/2023, 10:51 PMCherrio LLC
01/08/2023, 10:51 PMCherrio LLC
01/09/2023, 7:26 PMNick
01/09/2023, 8:06 PMView
only has a non-null
parent
if it has been added as a child to another View
. they key here is that top-level views (those added directly to the Display
) will have a null
parent
. this is intentional. it avoids having Display
share an interface with View
. which avoids it being added into the view hierarchy.
the docs explain this as well.Cherrio LLC
01/09/2023, 10:39 PMCherrio LLC
01/09/2023, 10:40 PMval button = PushButton("Welcome").apply {
size = Size(600.0, 40.0)
println("Parent: ${parent?.size}")
}
val text = TextField().apply {
size = Size(display.size.width, 40.0)
}
val container = container {
children += button
size = button.size
layout = constrain(button){
<http://it.top|it.top> eq 0
it.left eq 0
it.right eq parent.right
}
}
display += container
display.layout = constrain(display.children.first()){
<http://it.top|it.top> eq 10
it.left eq 10
it.right eq parent.right - 10
it.bottom eq parent.bottom - 10
}
So the println in PushButton prints null. Is that supposed to be so?Nick
01/10/2023, 1:01 AMparent
after adding it. also, View
has a parentChanged
event you can listen to if you wan my to.Cherrio LLC
01/10/2023, 12:48 PMconstraint(button,text){v1, v2 ->
<http://v1.top|v1.top> eq 0
v1.left eq 0
v1.height.preserve
v1.right eq parent.right - v1.right
<http://v2.top|v2.top> eq v1.bottom
v2.left eq 0
v2.right eq parent.right - v2.right
v2.height.preserve
}
Because i want the view to resize only if the parent width is less than it's width. It should maintain width if parent is bigger.
But with my code above my view's width is always bigger than specified widthNick
01/10/2023, 3:24 PMCherrio LLC
01/10/2023, 7:59 PMNick
01/10/2023, 8:05 PMconstrain(view1, view2) { v1, v2 ->
<http://v1.top|v1.top> eq 0
v1.left eq 0
v1.height.preserve
v1.width eq min(parent.width, 50)
<http://v2.top|v2.top> eq v1.bottom
v2.left eq v1.left
v2.height.preserve
v2.width eq min(100, parent.width)
}
Cherrio LLC
01/10/2023, 8:26 PMNick
01/10/2023, 8:27 PMNick
01/10/2023, 8:34 PMCherrio LLC
01/10/2023, 8:37 PMconstrain(views.first()){
<http://it.top|it.top> eq 0
it.left eq 0
it.width eq min(parent.width, it.width)
}
Nick
01/10/2023, 8:44 PMCherrio LLC
01/10/2023, 8:52 PMCherrio LLC
01/10/2023, 8:58 PMCherrio LLC
01/12/2023, 4:53 PMv1.width + v2.width eq min(parent.right, 600)
Nick
01/12/2023, 8:10 PMCherrio LLC
01/12/2023, 8:59 PMp1.width + p2.width + p3.width eq parent.width - 2 * inset
i want implement that too but I won't know how many views will be in the constrain. So it'll be a list of bounds,
how can I have same effect?Nick
01/13/2023, 2:41 AMLayout
like this:
layout = simpleLayout { container ->
var x = 0.0
val width = container.width / container.children.size
container.children.forEach { view ->
view.bounds = Rectangle(x, 0, width, container.height)
x += width
}
}
this just stacks the views horizontally with no padding and gives then same fraction of the parent’s width, while anchoring them at the top and bottom of it.
not sure if that helps.