Skip to content

Commit

Permalink
Allow to change labels when they aren't mounted.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Dec 22, 2024
1 parent e2ed215 commit 648fb61
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/element.toit
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ abstract class Element implements Window:
abstract invalidate -> none

is-mounted -> bool:
return change-tracker and change-tracker.is-mounted
return change-tracker != null and change-tracker.is-mounted

/**
Finds an Element in the tree with the given id.
Expand Down Expand Up @@ -484,7 +484,11 @@ class Label extends Element implements ColoredElement:

text= value/string -> none:
if value == text_: return
if orientation_ == ORIENTATION-0 and change-tracker and x and y:
if not is-mounted:
text_ = value
left_ = null // Trigger recalculation.
return
if orientation_ == ORIENTATION-0 and change-tracker and x and y and font_ and text_:
text-get-bounding-boxes_ text_ value alignment_ font_: | old/TextExtent_ new/TextExtent_ |
change-tracker.child-invalidated (x_ + old.x) (y_ + old.y) old.w old.h
change-tracker.child-invalidated (x_ + new.x) (y_ + new.y) new.w new.h
Expand Down
Binary file added tests/gold/label-unmounted-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.
50 changes: 50 additions & 0 deletions tests/label-unmounted-visualized.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 for Label that the change box is smaller when we only
// change part of the text.
import bitmap show *
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
driver := SeveralColorPngVisualizer 192 96 args[0] --outline=SEVERAL-WHITE
display := PixelDisplay.several-color driver
display.background = SEVERAL-DARK-GRAY

sans10 := Font.get "sans10"
sans12 := Font.get "sans12"

element-text := Label --x=30 --y=20 --color=SEVERAL-ORANGE --text="to be set"
element-text-2 := Label --x=180 --y=50 --color=SEVERAL-ORANGE --text="to be set" --font=sans12 --alignment=ALIGN-RIGHT
element-text-3 := Label --x=96 --y=80 --color=SEVERAL-ORANGE --text="T 123 For the win" --font=sans10 --alignment=ALIGN-CENTER

element-text.text = "Testing 123"
element-text.font = sans10
element-text-2.text = "123 Testing"
element-text-2.font = sans10
display.add element-text
display.add element-text-2
display.add element-text-3
display.draw

display.remove-all

element-text.label = "Testing 42"
element-text-2.label = "42 Testing"
// The "MM" has the same pixel width as the "123" above, so we can test the
// case where the ends are unchanged, but the middle changes.
element-text-3.label = "T MM For the win"
display.add element-text
display.add element-text-2
display.add element-text-3
display.draw

driver.write-png

0 comments on commit 648fb61

Please sign in to comment.