Skip to content

Commit

Permalink
Be more forgiving when no x,y,w,h is provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Dec 22, 2024
1 parent 648fb61 commit 04cd0cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/element.toit
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ class Div extends Element:
children, so they can accidentally extend beyond the bounds of the Div.
This is a efficiency win, but if you want clipping, use the $Div.clipping
constructor.
The $background or $border is only drawn if the div has a width and height.
To provide these values, pass $w and $h parameters, call the corresponding
setters, or apply a style.
Because it has no clipping and compositing, it is restricted to simple
borders without rounded corners and shadows.
See $Element.constructor for a description of the other parameters.
Expand Down Expand Up @@ -276,15 +279,15 @@ class Div extends Element:
children

invalidate:
if change-tracker and x and y and w and h:
change-tracker.child-invalidated x y w h
if change-tracker and w_ and h_:
change-tracker.child-invalidated (x_ or 0) (y_ or 0) w_ h_

child-invalidated x/int y/int w/int h/int --clip/bool=false -> none:
if clip:
super x y w h
else if change-tracker:
x2 := x_ + x
y2 := y_ + y
x2 := (x_ or 0) + x
y2 := (y_ or 0) + y
change-tracker.child-invalidated x2 y2 w h

w -> int?: return w_
Expand Down Expand Up @@ -319,10 +322,12 @@ class Div extends Element:

draw canvas/Canvas -> none:
old-transform := canvas.transform
canvas.transform = old-transform.translate x_ y_
Background.draw background_ canvas 0 0 w h --no-autoclipped
if x_ or y_:
canvas.transform = old-transform.translate (x_ or 0) (y_ or 0)
if background_ and w_ and h_:
Background.draw background_ canvas 0 0 w_ h_ --no-autoclipped
custom-draw canvas
if border_: border_.draw canvas 0 0 w h
if border_ and w_ and h_: border_.draw canvas 0 0 w_ h_
canvas.transform = old-transform

custom-draw canvas/Canvas -> none:
Expand Down
16 changes: 16 additions & 0 deletions tests/div-visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ main args:
label.text = "bar"
display.draw

label.y = 25
// As long as there isn't any background or border we don't need the
// dimensions of non-clipping divs.
div = Div [label]
display.remove-all
display.add div
display.draw

div.background = 0x404040
// Without dimensions, the background is ignored.
display.draw

div.w = WIDTH - 60
div.h = HEIGHT - 60
display.draw

driver.write-png
Binary file modified tests/gold/div-visualized.toit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 04cd0cb

Please sign in to comment.