Skip to content

Commit

Permalink
Don't clip elements of non-clipping div.
Browse files Browse the repository at this point in the history
The default `Div` is non-clipping. However, when children changed we
clipped them. This meant that the initial draw would happily draw
elements outside the borders, but further updates would not.
  • Loading branch information
floitsch committed Dec 10, 2024
1 parent 0afbfb5 commit 652d435
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/element.toit
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ class Div extends Element:
if change-tracker and x and y and w and h:
change-tracker.child-invalidated x y 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
change-tracker.child-invalidated x2 y2 w h

w -> int?: return w_
h -> int?: return h_

Expand Down Expand Up @@ -599,6 +608,9 @@ class ClippingDiv_ extends Div:
extent --x=x --y=y --w=w --h=h: | outer-x outer-y outer-w outer-h |
change-tracker.child-invalidated outer-x outer-y outer-w outer-h

child-invalidated x/int y/int w/int h/int -> none:
super x y w h --clip

static is-all-transparent opacity -> bool:
if opacity is not ByteArray: return false
return opacity.size == 1 and opacity[0] == 0
Expand Down
37 changes: 37 additions & 0 deletions tests/div-visualized.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2023 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the TESTS_LICENSE file.

// Tests some simple vertical sliders where there is a movable boundary between
// two different backgrounds.

import expect show *
import font show *
import pixel-display show *
import .png-visualizer

main args:
if args.size != 1:
print "Usage: script.toit png-basename"
exit 1
WIDTH ::= 220
HEIGHT ::= 140
driver := TrueColorPngVisualizer WIDTH HEIGHT args[0] --outline=0x4040ff
display := PixelDisplay.true-color driver
display.background = 0x808080

sans10 := Font.get "sans10"
label := Label --x=0 --y=0 --text="foo" --font=sans10
div := Div --x=30 --y=30 --w=(WIDTH - 60) --h=(HEIGHT - 60) [
// Since this isn't a clipping div, the label is drawn, even though it is
// outside the div.
label
]
display.add div

display.draw

label.text = "bar"
display.draw

driver.write-png
Binary file added 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 652d435

Please sign in to comment.