From 7c9362720bfd5a3dd8b9c6eca4f55c5460786154 Mon Sep 17 00:00:00 2001 From: Erik Corry Date: Tue, 7 Nov 2023 14:17:11 +0100 Subject: [PATCH] Add min_w, min_h. Change TextElement to Label --- src/element.toit | 69 ++++++++++++++----- tests/drop_shadow_gray_window_visualized.toit | 2 +- tests/drop_shadow_window_visualized.toit | 2 +- tests/gradient_bounds_visualized.toit | 2 +- tests/gradient_visualized.toit | 2 +- tests/mixed_text_rotated_visualized.toit | 4 +- tests/mixed_text_visualized.toit | 4 +- tests/rounded_gray_window_visualized.toit | 2 +- tests/rounded_several_window_visualized.toit | 2 +- ...rounded_three_color_window_visualized.toit | 2 +- .../rounded_two_color_window_visualized.toit | 2 +- tests/rounded_window_visualized.toit | 2 +- tests/simple_gray_window_visualized.toit | 2 +- tests/simple_several_window_visualized.toit | 2 +- .../simple_three_color_window_visualized.toit | 2 +- tests/simple_two_color_window_visualized.toit | 2 +- tests/simple_window_visualized.toit | 2 +- tests/text_visualized.toit | 26 +++---- 18 files changed, 84 insertions(+), 47 deletions(-) diff --git a/src/element.toit b/src/element.toit index 009acc6..419a6b9 100644 --- a/src/element.toit +++ b/src/element.toit @@ -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 @@ -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 := ? @@ -382,9 +388,9 @@ 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 := ? @@ -392,6 +398,8 @@ class TextElement extends Element implements ColoredElement: top_/int? := null width_/int? := null height_/int? := null + min_w_/int? := null + min_h_/int? := null color -> int: return color_ @@ -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] @@ -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. @@ -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 @@ -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 @@ -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: @@ -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. */ diff --git a/tests/drop_shadow_gray_window_visualized.toit b/tests/drop_shadow_gray_window_visualized.toit index 365f131..e7f56ed 100644 --- a/tests/drop_shadow_gray_window_visualized.toit +++ b/tests/drop_shadow_gray_window_visualized.toit @@ -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 diff --git a/tests/drop_shadow_window_visualized.toit b/tests/drop_shadow_window_visualized.toit index aafd73a..dbacbfe 100644 --- a/tests/drop_shadow_window_visualized.toit +++ b/tests/drop_shadow_window_visualized.toit @@ -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 diff --git a/tests/gradient_bounds_visualized.toit b/tests/gradient_bounds_visualized.toit index f75b69d..0fc83a3 100644 --- a/tests/gradient_bounds_visualized.toit +++ b/tests/gradient_bounds_visualized.toit @@ -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 * diff --git a/tests/gradient_visualized.toit b/tests/gradient_visualized.toit index eab95e6..3136322 100644 --- a/tests/gradient_visualized.toit +++ b/tests/gradient_visualized.toit @@ -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 * diff --git a/tests/mixed_text_rotated_visualized.toit b/tests/mixed_text_rotated_visualized.toit index 2c8e219..fa06fb4 100644 --- a/tests/mixed_text_rotated_visualized.toit +++ b/tests/mixed_text_rotated_visualized.toit @@ -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 diff --git a/tests/mixed_text_visualized.toit b/tests/mixed_text_visualized.toit index 15e59b8..cf8ef60 100644 --- a/tests/mixed_text_visualized.toit +++ b/tests/mixed_text_visualized.toit @@ -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 diff --git a/tests/rounded_gray_window_visualized.toit b/tests/rounded_gray_window_visualized.toit index 1942122..d76857f 100644 --- a/tests/rounded_gray_window_visualized.toit +++ b/tests/rounded_gray_window_visualized.toit @@ -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 diff --git a/tests/rounded_several_window_visualized.toit b/tests/rounded_several_window_visualized.toit index fb9ed52..443bd50 100644 --- a/tests/rounded_several_window_visualized.toit +++ b/tests/rounded_several_window_visualized.toit @@ -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 diff --git a/tests/rounded_three_color_window_visualized.toit b/tests/rounded_three_color_window_visualized.toit index 91e5ee5..fa8e15e 100644 --- a/tests/rounded_three_color_window_visualized.toit +++ b/tests/rounded_three_color_window_visualized.toit @@ -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 diff --git a/tests/rounded_two_color_window_visualized.toit b/tests/rounded_two_color_window_visualized.toit index b17d26a..266b1e6 100644 --- a/tests/rounded_two_color_window_visualized.toit +++ b/tests/rounded_two_color_window_visualized.toit @@ -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 diff --git a/tests/rounded_window_visualized.toit b/tests/rounded_window_visualized.toit index d1f7575..2985f53 100644 --- a/tests/rounded_window_visualized.toit +++ b/tests/rounded_window_visualized.toit @@ -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 diff --git a/tests/simple_gray_window_visualized.toit b/tests/simple_gray_window_visualized.toit index 3a2de2b..2e499af 100644 --- a/tests/simple_gray_window_visualized.toit +++ b/tests/simple_gray_window_visualized.toit @@ -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 diff --git a/tests/simple_several_window_visualized.toit b/tests/simple_several_window_visualized.toit index dc949aa..a54d83d 100644 --- a/tests/simple_several_window_visualized.toit +++ b/tests/simple_several_window_visualized.toit @@ -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 diff --git a/tests/simple_three_color_window_visualized.toit b/tests/simple_three_color_window_visualized.toit index d486dab..bc0c950 100644 --- a/tests/simple_three_color_window_visualized.toit +++ b/tests/simple_three_color_window_visualized.toit @@ -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 diff --git a/tests/simple_two_color_window_visualized.toit b/tests/simple_two_color_window_visualized.toit index 2ca36b7..a3a46a6 100644 --- a/tests/simple_two_color_window_visualized.toit +++ b/tests/simple_two_color_window_visualized.toit @@ -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 diff --git a/tests/simple_window_visualized.toit b/tests/simple_window_visualized.toit index 8833bba..1f65a6e 100644 --- a/tests/simple_window_visualized.toit +++ b/tests/simple_window_visualized.toit @@ -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 diff --git a/tests/text_visualized.toit b/tests/text_visualized.toit index 045c258..6b027d0 100644 --- a/tests/text_visualized.toit +++ b/tests/text_visualized.toit @@ -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 * @@ -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