Skip to content

Commit

Permalink
Add min_w, min_h. Change TextElement to Label
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Corry committed Nov 7, 2023
1 parent d9cb20f commit 7c93627
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 47 deletions.
69 changes: 53 additions & 16 deletions src/element.toit
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ abstract class Element extends ElementOrTexture_:

abstract draw canvas/AbstractCanvas -> none

abstract min_w -> int
abstract min_h -> int

interface ColoredElement:
color -> int?
color= value/int -> none
Expand Down Expand Up @@ -80,6 +83,9 @@ abstract class ResizableElement extends Element:
h_ = h
invalidate

min_w: return w_
min_h: return h_

abstract class RectangleElement extends ResizableElement implements ColoredElement:
color_ /int := ?

Expand Down Expand Up @@ -382,16 +388,18 @@ class OutlineRectangleElement extends RectangleElement:
canvas.rectangle (x_ + w_ - thickness_) y_ --w=thickness_ --h=h_ --color=color_
canvas.rectangle x_ (y + h_ - thickness_) --w=w_ --h=thickness_ --color=color_

class TextElement extends Element implements ColoredElement:
class Label extends Element implements ColoredElement:
color_/int := ?
text_/string? := null
label_/string? := null
alignment_/int := ?
orientation_/int := ?
font_/Font := ?
left_/int? := null
top_/int? := null
width_/int? := null
height_/int? := null
min_w_/int? := null
min_h_/int? := null

color -> int: return color_

Expand All @@ -400,25 +408,43 @@ class TextElement extends Element implements ColoredElement:
color_ = value
invalidate

constructor --x/int --y/int --color/int --text/string?=null --font/Font --orientation/int=ORIENTATION_0 --alignment/int=ALIGN_LEFT:
constructor --x/int --y/int --color/int --label/string?=null --font/Font --orientation/int=ORIENTATION_0 --alignment/int=ALIGN_LEFT:
color_ = color
text_ = text
label_ = label
alignment_ = alignment
orientation_ = orientation
font_ = font
super --x=x --y=y

min_w -> int:
if not label_: return 0
if not min_w_:
if orientation_ == ORIENTATION_0 or orientation_ == ORIENTATION_180:
min_w_ = font_.pixel_width label_
else:
min_w_ = (font_.text_extent label_)[1]
return min_w_

min_h -> int:
if not label_: return 0
if not min_h_:
if orientation_ == ORIENTATION_0 or orientation_ == ORIENTATION_180:
min_h_ = (font_.text_extent label_)[1]
else:
min_h_ = font_.pixel_width label_
return min_h_

/**
Calls the block with the left, top, width, and height.
For zero sized objects, doesn't call the block.
*/
xywh_ [block]:
if not text_: return
if not label_: return
if not left_:
extent/List := font_.text_extent text_
extent/List := font_.text_extent label_
displacement := 0
if alignment_ != ALIGN_LEFT:
displacement = (font_.pixel_width text_)
displacement = (font_.pixel_width label_)
if alignment_ == ALIGN_CENTER: displacement >>= 1
l := extent[2] - displacement
r := extent[2] - displacement + extent[0]
Expand Down Expand Up @@ -448,26 +474,31 @@ class TextElement extends Element implements ColoredElement:
block.call (x_ + left_) (y_ + top_) width_ height_

invalidate:
if change_tracker and text_:
if change_tracker and label_:
xywh_: | x y w h |
change_tracker.child_invalidated_element x y w h

text= value/string? -> none:
if value == text_: return
if orientation_ == ORIENTATION_0 and change_tracker and text_:
text_get_bounding_boxes_ text_ value alignment_ font_: | old/TextExtent_ new/TextExtent_ |
label= value/string? -> none:
if value == label_: return
if orientation_ == ORIENTATION_0 and change_tracker and label_:
text_get_bounding_boxes_ label_ value alignment_ font_: | old/TextExtent_ new/TextExtent_ |
change_tracker.child_invalidated_element (x_ + old.x) (y_ + old.y) old.w old.h
change_tracker.child_invalidated_element (x_ + new.x) (y_ + new.y) new.w new.h
text_ = value
label_ = value
min_w_ = null // Trigger recalculation.
left_ = null // Trigger recalculation.
return
invalidate
text_ = value
label_ = value
min_w_ = null
min_h_ = null
left_ = null // Trigger recalculation.
invalidate

orientation= value/int -> none:
if value == orientation_: return
min_w_ = null
min_h_ = null
invalidate
orientation_ = value
left_ = null // Trigger recalculation.
Expand All @@ -484,7 +515,7 @@ class TextElement extends Element implements ColoredElement:
x := x_
y := y_
if alignment_ != ALIGN_LEFT:
text_width := font_.pixel_width text_
text_width := font_.pixel_width label_
if alignment_ == ALIGN_CENTER: text_width >>= 1
if orientation_ == ORIENTATION_0:
x -= text_width
Expand All @@ -495,7 +526,7 @@ class TextElement extends Element implements ColoredElement:
else:
assert: orientation_ == ORIENTATION_270
y -= text_width
canvas.text x y --text=text_ --color=color_ --font=font_ --orientation=orientation_
canvas.text x y --text=label_ --color=color_ --font=font_ --orientation=orientation_

/**
A superclass for elements that can draw themselves. Override the
Expand Down Expand Up @@ -526,6 +557,9 @@ class BarCodeEanElement extends CustomElement:
sans10_ ::= Font.get "sans10"
number_height_ := EAN_13_BOTTOM_SPACE

min_w: return w
min_h: return h

code_ := ? // 13 digit code as a string.
code= value/string -> none:
Expand Down Expand Up @@ -707,6 +741,9 @@ abstract class WindowElement extends BorderlessWindowElement implements Window:
h -> int:
return inner_h_

min_w: return inner_w_
min_h: return inner_h_

/**
Changes the top left corner (without any borders) of the window.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/drop_shadow_gray_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main args:
]
win.add gradient

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=0x10
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=0x10
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/drop_shadow_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main args:
]
win.add gradient

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=0x101040
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=0x101040
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/gradient_bounds_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the TESTS_LICENSE file.
// Tests for TextElement that the change box is smaller when we only
// Tests for Label that the change box is smaller when we only
// change part of the text.
import bitmap show *
Expand Down
2 changes: 1 addition & 1 deletion tests/gradient_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the TESTS_LICENSE file.
// Tests for TextElement that the change box is smaller when we only
// Tests for Label that the change box is smaller when we only
// change part of the text.
import bitmap show *
Expand Down
4 changes: 2 additions & 2 deletions tests/mixed_text_rotated_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ main args:
texture_text := display.text (ctx.with --color=SEVERAL_BLUE) 10 20 "Texture 1"
texture_text_2 := display.text (ctx.with --color=SEVERAL_BLUE) 10 65 "Texture 2"
// Element-based text.
element_text := TextElement --x=10 --y=30 --color=SEVERAL_GREEN --font=sans10 --text="Element 1"
element_text_2 := TextElement --x=10 --y=110 --color=SEVERAL_GREEN --font=sans10 --text="Element 2"
element_text := Label --x=10 --y=30 --color=SEVERAL_GREEN --font=sans10 --label="Element 1"
element_text_2 := Label --x=10 --y=110 --color=SEVERAL_GREEN --font=sans10 --label="Element 2"
display.add element_text
display.add element_text_2
display.draw
Expand Down
4 changes: 2 additions & 2 deletions tests/mixed_text_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ main args:
texture_text := display.text (ctx.with --color=SEVERAL_BLUE) 30 20 "Texture"
texture_text_2 := display.text (ctx.with --color=SEVERAL_BLUE) 80 20 "Texture"
// Element-based rectangles.
element_text := TextElement --x=30 --y=30 --color=SEVERAL_ORANGE --text="joo%" --font=sans10
element_text_2 := TextElement --x=130 --y=20 --color=SEVERAL_ORANGE --text="joo%" --font=sans10
element_text := Label --x=30 --y=30 --color=SEVERAL_ORANGE --label="joo%" --font=sans10
element_text_2 := Label --x=130 --y=20 --color=SEVERAL_ORANGE --label="joo%" --font=sans10
display.add element_text
display.add element_text_2
display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/rounded_gray_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main args:
]
win.add gradient

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=0x10
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=0x10
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/rounded_several_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main args:
win := RoundedCornerWindowElement --x=30 --y=30 --w=180 --h=100 --corner_radius=15 --background_color=SEVERAL_LIGHT_GRAY
display.add win

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=SEVERAL_BLACK
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=SEVERAL_BLACK
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/rounded_three_color_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main args:
win := RoundedCornerWindowElement --x=30 --y=30 --w=180 --h=100 --corner_radius=17 --background_color=RED
display.add win

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=BLACK
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=BLACK
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/rounded_two_color_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main args:
win := RoundedCornerWindowElement --x=30 --y=30 --w=180 --h=100 --corner_radius=17 --background_color=WHITE
display.add win

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=BLACK
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=BLACK
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/rounded_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main args:
]
win.add gradient

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=0x101040
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=0x101040
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/simple_gray_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main args:
]
win.add gradient

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=0x10
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=0x10
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/simple_several_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main args:
win := SimpleWindowElement --x=30 --y=30 --w=180 --h=100 --border_width=0 --background_color=SEVERAL_LIGHT_GRAY
display.add win

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=SEVERAL_BLACK
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=SEVERAL_BLACK
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/simple_three_color_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main args:
win := SimpleWindowElement --x=30 --y=30 --w=180 --h=100 --border_width=0 --background_color=RED
display.add win

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=BLACK
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=BLACK
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/simple_two_color_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main args:
win := SimpleWindowElement --x=30 --y=30 --w=180 --h=100 --border_width=0 --background_color=WHITE
display.add win

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=BLACK
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=BLACK
win.add text

display.draw
Expand Down
2 changes: 1 addition & 1 deletion tests/simple_window_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main args:
]
win.add gradient

text := TextElement --x=90 --y=55 --text="Hello, World!" --font=sans10 --color=0x101040
text := Label --x=90 --y=55 --label="Hello, World!" --font=sans10 --color=0x101040
win.add text

display.draw
Expand Down
26 changes: 13 additions & 13 deletions tests/text_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the TESTS_LICENSE file.
// Tests for TextElement that the change box is smaller when we only
// Tests for Label that the change box is smaller when we only
// change part of the text.
import bitmap show *
Expand All @@ -23,29 +23,29 @@ main args:

sans10 := Font.get "sans10"

element_text := TextElement --x=30 --y=20 --color=SEVERAL_ORANGE --text="Testing 123" --font=sans10
element_text_2 := TextElement --x=180 --y=50 --color=SEVERAL_ORANGE --text="123 Testing" --font=sans10 --alignment=ALIGN_RIGHT
element_text_3 := TextElement --x=96 --y=80 --color=SEVERAL_ORANGE --text="T 123 For the win" --font=sans10 --alignment=ALIGN_CENTER
element_text := Label --x=30 --y=20 --color=SEVERAL_ORANGE --label="Testing 123" --font=sans10
element_text_2 := Label --x=180 --y=50 --color=SEVERAL_ORANGE --label="123 Testing" --font=sans10 --alignment=ALIGN_RIGHT
element_text_3 := Label --x=96 --y=80 --color=SEVERAL_ORANGE --label="T 123 For the win" --font=sans10 --alignment=ALIGN_CENTER
display.add element_text
display.add element_text_2
display.add element_text_3
display.draw

element_text.text = "Testing 42"
element_text_2.text = "42 Testing"
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.text = "T MM For the win"
element_text_3.label = "T MM For the win"
display.draw

element_text.text = "Test the West"
element_text_2.text = "Test the Folketing"
element_text_3.text = "Test * For the win"
element_text.label = "Test the West"
element_text_2.label = "Test the Folketing"
element_text_3.label = "Test * For the win"
display.draw

element_text.text = "Test the West"
element_text_2.text = "Test the Folketlng"
element_text_3.text = "Test * For the win" // Both ends move because its centered.
element_text.label = "Test the West"
element_text_2.label = "Test the Folketlng"
element_text_3.label = "Test * For the win" // Both ends move because its centered.
display.draw

driver.write_png

0 comments on commit 7c93627

Please sign in to comment.