diff --git a/CHANGELOG.md b/CHANGELOG.md index fccb5b68a..eece4ed83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,11 @@ - `TitleBar` and `ModalFrame` can draw their title strong, with the same emphasis as Text's strong ranges, and a `ModalFrame` can start its title at its content's edge, or after its left action, instead of centring it. +- `Text` can type its text out: while `typing` is set it paints only the + first `typed` characters, never part of an accented letter, emoji or + multibyte character, and keeps the whole text's layout and accessible + label, so no word moves while it appears and screen readers read it all. + `TextCharactersEnd` gives where those characters end. ### Fixed diff --git a/src/backend/widget_codec.zi b/src/backend/widget_codec.zi index 0eb78b63b..f136ce103 100644 --- a/src/backend/widget_codec.zi +++ b/src/backend/widget_codec.zi @@ -159,7 +159,7 @@ WidgetValidTextRange :: (value: HostValue) -> bool { } WidgetTextPropsCodec :: struct { - fields: [16]HostField + fields: [18]HostField bounds: WidgetRectangleCodec strong_values: [1024]HostValue strong_codecs: [1024]WidgetTextRangeCodec @@ -171,7 +171,7 @@ WidgetEncodeTextProps :: (data: TextProps, codec: *WidgetTextPropsCodec) -> Host value.kind = cast(s32)HostRecord value.type_name = HostTextData("TextProps") value.fields = *codec.fields[0] - value.field_count = 16 + value.field_count = 18 codec.fields[0].name = HostTextData("key") codec.fields[0].value = HostScalar("u64", cast(s64)data.key) codec.fields[1].name = HostTextData("bounds") @@ -215,6 +215,10 @@ WidgetEncodeTextProps :: (data: TextProps, codec: *WidgetTextPropsCodec) -> Host index += cast(usize)1 } } + codec.fields[16].name = HostTextData("typing") + codec.fields[16].value = HostScalar("bool", ifx data.typing then 1 else 0) + codec.fields[17].name = HostTextData("typed") + codec.fields[17].value = HostScalar("s32", cast(s64)data.typed) return value } @@ -323,6 +327,18 @@ WidgetDecodeTextProps :: (value: HostValue, codec: *WidgetTextPropsCodec) -> Tex // Slice storage is attached by the rendering effect after decoding. } } + { + field_at := HostFieldAt(value, 16, "typing") + if field_at >= 0 { + data.typing = value.fields[field_at].value.integer != 0 + } + } + { + field_at := HostFieldAt(value, 17, "typed") + if field_at >= 0 { + data.typed = cast(s32)value.fields[field_at].value.integer + } + } return data } @@ -434,6 +450,18 @@ WidgetValidTextProps :: (value: HostValue) -> bool { } } } + { + field_at := HostFieldAt(value, 16, "typing") + if field_at >= 0 { + if !HostScalarValid(value.fields[field_at].value, "bool", cast(s32)HostInteger) { return false } + } + } + { + field_at := HostFieldAt(value, 17, "typed") + if field_at >= 0 { + if !HostScalarValid(value.fields[field_at].value, "s32", cast(s32)HostInteger) { return false } + } + } return true } diff --git a/src/ui/text_layout.zi b/src/ui/text_layout.zi index 71d462a02..134089f48 100644 --- a/src/ui/text_layout.zi +++ b/src/ui/text_layout.zi @@ -2,6 +2,7 @@ #import "text_align" #import "text_props" #import "utf8" +Grapheme :: #import "std/grapheme"; using TextAlign; using TextWrap; @@ -165,6 +166,20 @@ NextTextLine :: (value: string, start: s32, max_width: float32, return line } +// The byte offset just after the first count characters of value. A +// character is a grapheme cluster, so typed text never splits an accent, an +// emoji or a multibyte letter. No characters end at 0; a count past the +// last character ends at the whole text. +#program_export +TextCharactersEnd :: (value: string, count: s32) -> s32 { + end: s32 = 0 + while count > 0 && end < cast(s32)value.count { + end = Grapheme.Next(value, end) + count -= 1 + } + return end +} + #program_export TextDefaultLineGap :: (scale: float32) -> s32 { if scale <= 0.0 { scale = 1.0 } diff --git a/src/ui/text_props.zi b/src/ui/text_props.zi index c9df070be..119b93dff 100644 --- a/src/ui/text_props.zi +++ b/src/ui/text_props.zi @@ -46,4 +46,9 @@ TextProps :: struct { selectable: bool strikethrough: bool strong: []TextRange + // While typing, only the first typed characters are painted, as if they + // were being typed. A character is a grapheme cluster. The layout, the + // size and the accessible label always keep the whole text. + typing: bool + typed: s32 } diff --git a/src/ui/text_widget.zi b/src/ui/text_widget.zi index 10e494140..12ecb9b9a 100644 --- a/src/ui/text_widget.zi +++ b/src/ui/text_widget.zi @@ -169,11 +169,20 @@ y: s32 = cast(s32)prepared.bounds.y + cast(s32)TextAlignmentOffset(prepared.bounds.height, cast(float32)total_height, cast(s32)props.vertical_align) + // Text being typed is laid out whole and painted up to the end of + // its typed characters, so no word moves while it is typed. + typed_end: s32 = cast(s32)props.text.count + if props.typing { + typed_end = TextCharactersEnd(props.text, props.typed) + } start: s32 = 0 while start <= (cast(s32)props.text.count) { line: TextLinePlan = NextTextLine(props.text, start, prepared.bounds.width, prepared.wrap, prepared.font, prepared.typeface) + if line.start >= typed_end && typed_end < cast(s32)props.text.count { + break + } value: string = TextSpan(props.text, line.start, line.end - line.start) // A line too wide for its bounds keeps the start that fits and @@ -193,6 +202,14 @@ } x: s32 = TextLineXFor(cast(s32)prepared.bounds.x, cast(s32)prepared.bounds.width, line.width, cast(s32)props.align) + // A line typed partway keeps its place and shows its typed start; + // an ellipsis appears once its line is typed to the end. + if line.end > typed_end { + value = TextSpan(props.text, line.start, typed_end - line.start) + line.end = typed_end + line.width = GlyphWidth(value, prepared.font, prepared.typeface) + ellipsis = false + } // A line that starts inside the box is drawn whole: a box a little // shorter than the line must not cut off the bottoms of its glyphs. // Lines that start below the box are still clipped away. diff --git a/tests/ziran_text_widget_host.zi b/tests/ziran_text_widget_host.zi index deffaab71..18d116b45 100644 --- a/tests/ziran_text_widget_host.zi +++ b/tests/ziran_text_widget_host.zi @@ -5,6 +5,30 @@ texts: s32; fills: s32; failed: bool; emphasis: bool; +typing: bool; +typed_values: [4]string; +typed_x: [4]s32; + +// Typed text records what each painted line shows and where it starts. +#program_export +TextHostTyping :: () { + typing = true + emphasis = false + texts = 0 + fills = 0 +} + +#program_export +TextHostTypedValue :: (index: s32) -> string { + if index < 0 || index >= 4 { return "" } + return typed_values[index] +} + +#program_export +TextHostTypedX :: (index: s32) -> s32 { + if index < 0 || index >= 4 { return -1 } + return typed_x[index] +} #program_export TextHostEmphasis :: () { @@ -45,6 +69,14 @@ RasterText :: (value: string, x: s32, y: s32, font: s32, #program_export RasterTextClipped :: (value: string, x: s32, y: s32, font: s32, color: Color, clip: Rectangle) { + if typing { + if texts < 4 { + typed_values[texts] = value + typed_x[texts] = x + } + texts += 1 + return + } if emphasis { values: [5]string = .["ab cd", "b cd", "é水", "é", "水"] positions: [5]s32 = .[0, 11, 0, 1, 21] diff --git a/tests/ziran_text_widget_test.zi b/tests/ziran_text_widget_test.zi index a7df82561..dfff0a27e 100644 --- a/tests/ziran_text_widget_test.zi +++ b/tests/ziran_text_widget_test.zi @@ -81,5 +81,50 @@ main :: () -> s32 { TreeNodeAt(TestSession(), 1).semantic_label != props.text { return 3 } + // Typed text keeps the whole text's layout and label and paints only + // its typed characters, never part of one. + TextHostTyping() + typed := TextProps.{.key = cast(u64)9, .bounds = Rectangle.{0.0, 0.0, 60.0, 28.0}, + .text = "ab cd\né水", .class_name = 7, .align = cast(TextAlign)TextAlignCenter, + .typing = true, .typed = 4} + if TypedFrame(typed, 3) != 1 || TextHostTypedValue(0) != "ab c" || + TextHostTypedX(0) != 5 || + TreeNodeAt(TestSession(), 1).semantic_label != typed.text { + return 4 + } + TextHostTyping() + typed.typed = 7 + if TypedFrame(typed, 4) != 2 || TextHostTypedValue(0) != "ab cd" || + TextHostTypedValue(1) != "é" || TextHostTypedX(1) != 5 { + return 5 + } + TextHostTyping() + typed.typed = 0 + if TypedFrame(typed, 5) != 0 { + return 6 + } + TextHostTyping() + typed.text = "éx" + typed.typed = 1 + if TypedFrame(typed, 6) != 1 || TextHostTypedValue(0) != "é" || + TextHostTypedX(0) != 10 { + return 7 + } + TextHostTyping() + typed.typing = false + if TypedFrame(typed, 7) != 1 || TextHostTypedValue(0) != typed.text { + return 8 + } return 0 } + +// Paints one frame of typed text and returns how many lines it painted, or +// -1 when the frame failed. +TypedFrame :: (props: TextProps, frame: u64) -> s32 { + BeginFrame(TestSession(), frame, Rectangle.{0.0, 0.0, 200.0, 100.0}) + Text(TestSession(), props) + if EndFrame(TestSession()) != cast(FrameStatus)FrameOk || TextHostFailed() { + return -1 + } + return TextHostLabels() +}