From a1078ff20150a0816af4e57a511c1cd3124191b4 Mon Sep 17 00:00:00 2001 From: Liu Date: Wed, 14 Oct 2026 22:47:34 +0800 Subject: [PATCH] feat(widgets): add spinner and shimmer widgets --- docs/en/widgets/shimmer.mdx | 82 ++ docs/en/widgets/spinner.mdx | 58 + docs/examples/shimmer-angle-tsx/example.tsx | 18 + docs/examples/shimmer-angle-tsx/main.c | 11 + docs/examples/shimmer-angle-xml/main.c | 8 + docs/examples/shimmer-angle-xml/ui.xml | 35 + docs/examples/shimmer-angle/main.c | 56 + docs/examples/shimmer-basic-tsx/example.tsx | 11 + docs/examples/shimmer-basic-tsx/main.c | 11 + docs/examples/shimmer-basic-xml/main.c | 8 + docs/examples/shimmer-basic-xml/ui.xml | 25 + docs/examples/shimmer-basic/main.c | 37 + docs/examples/shimmer-color-tsx/example.tsx | 18 + docs/examples/shimmer-color-tsx/main.c | 11 + docs/examples/shimmer-color-xml/main.c | 8 + docs/examples/shimmer-color-xml/ui.xml | 35 + docs/examples/shimmer-color/main.c | 55 + .../examples/shimmer-duration-tsx/example.tsx | 18 + docs/examples/shimmer-duration-tsx/main.c | 11 + docs/examples/shimmer-duration-xml/main.c | 8 + docs/examples/shimmer-duration-xml/ui.xml | 35 + docs/examples/shimmer-duration/main.c | 54 + docs/examples/shimmer-running-tsx/example.tsx | 18 + docs/examples/shimmer-running-tsx/main.c | 11 + docs/examples/shimmer-running-xml/main.c | 8 + docs/examples/shimmer-running-xml/ui.xml | 35 + docs/examples/shimmer-running/main.c | 56 + docs/examples/shimmer-spread-tsx/example.tsx | 18 + docs/examples/shimmer-spread-tsx/main.c | 11 + docs/examples/shimmer-spread-xml/main.c | 8 + docs/examples/shimmer-spread-xml/ui.xml | 35 + docs/examples/shimmer-spread/main.c | 56 + docs/examples/spinner-basic-tsx/example.tsx | 5 + docs/examples/spinner-basic-tsx/main.c | 11 + docs/examples/spinner-basic-xml/main.c | 8 + docs/examples/spinner-basic-xml/ui.xml | 14 + docs/examples/spinner-basic/main.c | 10 + .../spinner-custom-content-tsx/example.tsx | 21 + .../spinner-custom-content-tsx/main.c | 11 + .../spinner-custom-content-xml/main.c | 8 + .../spinner-custom-content-xml/ui.xml | 61 + docs/examples/spinner-custom-content/main.c | 85 ++ docs/examples/spinner-running-tsx/example.tsx | 10 + docs/examples/spinner-running-tsx/main.c | 11 + docs/examples/spinner-running-xml/main.c | 8 + docs/examples/spinner-running-xml/ui.xml | 21 + docs/examples/spinner-running/main.c | 33 + docs/examples/spinner-size-tsx/example.tsx | 11 + docs/examples/spinner-size-tsx/main.c | 11 + docs/examples/spinner-size-xml/main.c | 8 + docs/examples/spinner-size-xml/ui.xml | 30 + docs/examples/spinner-size/main.c | 40 + docs/sidebars.json | 2 + docs/widget-fields/shimmer-content.ts | 5 + docs/widget-fields/shimmer.ts | 59 + docs/widget-fields/spinner-content.ts | 3 + docs/widget-fields/spinner.ts | 32 + docs/zh-CN/widgets/shimmer.mdx | 82 ++ docs/zh-CN/widgets/spinner.mdx | 58 + .../doc-viewer/app/en/widgets/shimmer/page.c | 54 + .../doc-viewer/app/en/widgets/shimmer/page.h | 9 + .../app/en/widgets/shimmer/page.tsx | 105 ++ .../doc-viewer/app/en/widgets/spinner/page.c | 54 + .../doc-viewer/app/en/widgets/spinner/page.h | 9 + .../app/en/widgets/spinner/page.tsx | 80 ++ .../app/examples/shimmer-angle/index.c | 43 + .../app/examples/shimmer-angle/index.tsx | 170 +++ .../app/examples/shimmer-basic/index.c | 41 + .../app/examples/shimmer-basic/index.tsx | 134 ++ .../app/examples/shimmer-color/index.c | 43 + .../app/examples/shimmer-color/index.tsx | 169 +++ .../app/examples/shimmer-duration/index.c | 43 + .../app/examples/shimmer-duration/index.tsx | 168 +++ .../app/examples/shimmer-running/index.c | 41 + .../app/examples/shimmer-running/index.tsx | 170 +++ .../app/examples/shimmer-spread/index.c | 43 + .../app/examples/shimmer-spread/index.tsx | 170 +++ .../app/examples/spinner-basic/index.c | 41 + .../app/examples/spinner-basic/index.tsx | 90 ++ .../examples/spinner-custom-content/index.c | 47 + .../examples/spinner-custom-content/index.tsx | 228 +++ .../app/examples/spinner-running/index.c | 41 + .../app/examples/spinner-running/index.tsx | 125 ++ .../app/examples/spinner-size/index.c | 41 + .../app/examples/spinner-size/index.tsx | 142 ++ .../app/widget-fields/shimmer-content.ts | 5 + .../doc-viewer/app/widget-fields/shimmer.ts | 59 + .../app/widget-fields/spinner-content.ts | 3 + .../doc-viewer/app/widget-fields/spinner.ts | 32 + .../app/zh-CN/widgets/shimmer/page.c | 57 + .../app/zh-CN/widgets/shimmer/page.h | 9 + .../app/zh-CN/widgets/shimmer/page.tsx | 105 ++ .../app/zh-CN/widgets/spinner/page.c | 57 + .../app/zh-CN/widgets/spinner/page.h | 9 + .../app/zh-CN/widgets/spinner/page.tsx | 80 ++ include/LCUI/widgets.h | 2 + include/LCUI/widgets/shimmer.h | 73 + include/LCUI/widgets/spinner.h | 53 + src/lcui_widgets.c | 2 + src/widgets/draw_backend.h | 182 +++ src/widgets/draw_backend_software.c | 828 +++++++++++ src/widgets/shimmer.c | 610 ++++++++ src/widgets/shimmer_priv.h | 24 + src/widgets/spinner.c | 535 ++++++++ src/widgets/spinner_priv.h | 21 + src/widgets/text.c | 52 + tests/fixtures/test_draw_backend.css | 5 + tests/fixtures/test_draw_backend.xml | 7 + tests/fixtures/test_shimmer.css | 33 + tests/fixtures/test_shimmer.xml | 11 + tests/fixtures/test_spinner.css | 47 + tests/fixtures/test_spinner.xml | 11 + tests/integration/test_draw_backend.c | 302 ++++ tests/integration/test_shimmer.c | 787 +++++++++++ tests/integration/test_spinner.c | 1222 +++++++++++++++++ tests/integration/test_spinner_perf.c | 141 ++ tests/main.c | 8 + tests/previews/preview_shimmer.c | 24 + tests/previews/preview_spinner.c | 24 + tests/previews/preview_spinner_perf.c | 46 + tests/previews/xmake.lua | 3 + tests/scenes/shimmer_scene.c | 29 + tests/scenes/shimmer_scene.h | 24 + tests/scenes/spinner_perf_scene.c | 138 ++ tests/scenes/spinner_perf_scene.h | 49 + tests/scenes/spinner_scene.c | 29 + tests/scenes/spinner_scene.h | 24 + 127 files changed, 9590 insertions(+) create mode 100644 docs/en/widgets/shimmer.mdx create mode 100644 docs/en/widgets/spinner.mdx create mode 100644 docs/examples/shimmer-angle-tsx/example.tsx create mode 100644 docs/examples/shimmer-angle-tsx/main.c create mode 100644 docs/examples/shimmer-angle-xml/main.c create mode 100644 docs/examples/shimmer-angle-xml/ui.xml create mode 100644 docs/examples/shimmer-angle/main.c create mode 100644 docs/examples/shimmer-basic-tsx/example.tsx create mode 100644 docs/examples/shimmer-basic-tsx/main.c create mode 100644 docs/examples/shimmer-basic-xml/main.c create mode 100644 docs/examples/shimmer-basic-xml/ui.xml create mode 100644 docs/examples/shimmer-basic/main.c create mode 100644 docs/examples/shimmer-color-tsx/example.tsx create mode 100644 docs/examples/shimmer-color-tsx/main.c create mode 100644 docs/examples/shimmer-color-xml/main.c create mode 100644 docs/examples/shimmer-color-xml/ui.xml create mode 100644 docs/examples/shimmer-color/main.c create mode 100644 docs/examples/shimmer-duration-tsx/example.tsx create mode 100644 docs/examples/shimmer-duration-tsx/main.c create mode 100644 docs/examples/shimmer-duration-xml/main.c create mode 100644 docs/examples/shimmer-duration-xml/ui.xml create mode 100644 docs/examples/shimmer-duration/main.c create mode 100644 docs/examples/shimmer-running-tsx/example.tsx create mode 100644 docs/examples/shimmer-running-tsx/main.c create mode 100644 docs/examples/shimmer-running-xml/main.c create mode 100644 docs/examples/shimmer-running-xml/ui.xml create mode 100644 docs/examples/shimmer-running/main.c create mode 100644 docs/examples/shimmer-spread-tsx/example.tsx create mode 100644 docs/examples/shimmer-spread-tsx/main.c create mode 100644 docs/examples/shimmer-spread-xml/main.c create mode 100644 docs/examples/shimmer-spread-xml/ui.xml create mode 100644 docs/examples/shimmer-spread/main.c create mode 100644 docs/examples/spinner-basic-tsx/example.tsx create mode 100644 docs/examples/spinner-basic-tsx/main.c create mode 100644 docs/examples/spinner-basic-xml/main.c create mode 100644 docs/examples/spinner-basic-xml/ui.xml create mode 100644 docs/examples/spinner-basic/main.c create mode 100644 docs/examples/spinner-custom-content-tsx/example.tsx create mode 100644 docs/examples/spinner-custom-content-tsx/main.c create mode 100644 docs/examples/spinner-custom-content-xml/main.c create mode 100644 docs/examples/spinner-custom-content-xml/ui.xml create mode 100644 docs/examples/spinner-custom-content/main.c create mode 100644 docs/examples/spinner-running-tsx/example.tsx create mode 100644 docs/examples/spinner-running-tsx/main.c create mode 100644 docs/examples/spinner-running-xml/main.c create mode 100644 docs/examples/spinner-running-xml/ui.xml create mode 100644 docs/examples/spinner-running/main.c create mode 100644 docs/examples/spinner-size-tsx/example.tsx create mode 100644 docs/examples/spinner-size-tsx/main.c create mode 100644 docs/examples/spinner-size-xml/main.c create mode 100644 docs/examples/spinner-size-xml/ui.xml create mode 100644 docs/examples/spinner-size/main.c create mode 100644 docs/widget-fields/shimmer-content.ts create mode 100644 docs/widget-fields/shimmer.ts create mode 100644 docs/widget-fields/spinner-content.ts create mode 100644 docs/widget-fields/spinner.ts create mode 100644 docs/zh-CN/widgets/shimmer.mdx create mode 100644 docs/zh-CN/widgets/spinner.mdx create mode 100644 examples/doc-viewer/app/en/widgets/shimmer/page.c create mode 100644 examples/doc-viewer/app/en/widgets/shimmer/page.h create mode 100644 examples/doc-viewer/app/en/widgets/shimmer/page.tsx create mode 100644 examples/doc-viewer/app/en/widgets/spinner/page.c create mode 100644 examples/doc-viewer/app/en/widgets/spinner/page.h create mode 100644 examples/doc-viewer/app/en/widgets/spinner/page.tsx create mode 100644 examples/doc-viewer/app/examples/shimmer-angle/index.c create mode 100644 examples/doc-viewer/app/examples/shimmer-angle/index.tsx create mode 100644 examples/doc-viewer/app/examples/shimmer-basic/index.c create mode 100644 examples/doc-viewer/app/examples/shimmer-basic/index.tsx create mode 100644 examples/doc-viewer/app/examples/shimmer-color/index.c create mode 100644 examples/doc-viewer/app/examples/shimmer-color/index.tsx create mode 100644 examples/doc-viewer/app/examples/shimmer-duration/index.c create mode 100644 examples/doc-viewer/app/examples/shimmer-duration/index.tsx create mode 100644 examples/doc-viewer/app/examples/shimmer-running/index.c create mode 100644 examples/doc-viewer/app/examples/shimmer-running/index.tsx create mode 100644 examples/doc-viewer/app/examples/shimmer-spread/index.c create mode 100644 examples/doc-viewer/app/examples/shimmer-spread/index.tsx create mode 100644 examples/doc-viewer/app/examples/spinner-basic/index.c create mode 100644 examples/doc-viewer/app/examples/spinner-basic/index.tsx create mode 100644 examples/doc-viewer/app/examples/spinner-custom-content/index.c create mode 100644 examples/doc-viewer/app/examples/spinner-custom-content/index.tsx create mode 100644 examples/doc-viewer/app/examples/spinner-running/index.c create mode 100644 examples/doc-viewer/app/examples/spinner-running/index.tsx create mode 100644 examples/doc-viewer/app/examples/spinner-size/index.c create mode 100644 examples/doc-viewer/app/examples/spinner-size/index.tsx create mode 100644 examples/doc-viewer/app/widget-fields/shimmer-content.ts create mode 100644 examples/doc-viewer/app/widget-fields/shimmer.ts create mode 100644 examples/doc-viewer/app/widget-fields/spinner-content.ts create mode 100644 examples/doc-viewer/app/widget-fields/spinner.ts create mode 100644 examples/doc-viewer/app/zh-CN/widgets/shimmer/page.c create mode 100644 examples/doc-viewer/app/zh-CN/widgets/shimmer/page.h create mode 100644 examples/doc-viewer/app/zh-CN/widgets/shimmer/page.tsx create mode 100644 examples/doc-viewer/app/zh-CN/widgets/spinner/page.c create mode 100644 examples/doc-viewer/app/zh-CN/widgets/spinner/page.h create mode 100644 examples/doc-viewer/app/zh-CN/widgets/spinner/page.tsx create mode 100644 include/LCUI/widgets/shimmer.h create mode 100644 include/LCUI/widgets/spinner.h create mode 100644 src/widgets/draw_backend.h create mode 100644 src/widgets/draw_backend_software.c create mode 100644 src/widgets/shimmer.c create mode 100644 src/widgets/shimmer_priv.h create mode 100644 src/widgets/spinner.c create mode 100644 src/widgets/spinner_priv.h create mode 100644 tests/fixtures/test_draw_backend.css create mode 100644 tests/fixtures/test_draw_backend.xml create mode 100644 tests/fixtures/test_shimmer.css create mode 100644 tests/fixtures/test_shimmer.xml create mode 100644 tests/fixtures/test_spinner.css create mode 100644 tests/fixtures/test_spinner.xml create mode 100644 tests/integration/test_draw_backend.c create mode 100644 tests/integration/test_shimmer.c create mode 100644 tests/integration/test_spinner.c create mode 100644 tests/integration/test_spinner_perf.c create mode 100644 tests/previews/preview_shimmer.c create mode 100644 tests/previews/preview_spinner.c create mode 100644 tests/previews/preview_spinner_perf.c create mode 100644 tests/scenes/shimmer_scene.c create mode 100644 tests/scenes/shimmer_scene.h create mode 100644 tests/scenes/spinner_perf_scene.c create mode 100644 tests/scenes/spinner_perf_scene.h create mode 100644 tests/scenes/spinner_scene.c create mode 100644 tests/scenes/spinner_scene.h diff --git a/docs/en/widgets/shimmer.mdx b/docs/en/widgets/shimmer.mdx new file mode 100644 index 00000000..cf42a670 --- /dev/null +++ b/docs/en/widgets/shimmer.mdx @@ -0,0 +1,82 @@ +# Shimmer + +Overlays a soft-edged, tiltable highlight band on text, icons, or other content and sweeps it from left to right; the highlight only lands on the content's opaque pixels while the content keeps its own colors. + + + +## Use cases + +- **Suitable**: letting a status line (an icon or plain text) sweep with a soft highlight to signal "in progress / loading" +- **Suitable**: adding a sliding highlight to a "generating" hint while text streams in +- **Not suitable**: a plain gray skeleton placeholder (Shimmer overlays real content and does not generate a skeleton) +- **Not suitable**: progress with a definite percentage or a known duration (use `progress`) +- **Not suitable**: a global busy indicator without a fixed shape (use `spinner`) + +## Usage + +```tsx +import { Shimmer, ShimmerContent } from "@lcui/react" +``` + +Shimmer is a self-drawn widget implemented in C: it first composites the content at full strength in its own colors, then overlays a tilted highlight band clipped to the content's opaque pixels. The highlight itself can be configured with a color, period, width, and tilt, either through widget properties or the matching `ui_shimmer_set_*` APIs: + +```tsx + + + Generating response… + + +``` + +## Composition + +Use the following composition to build a `Shimmer`: + +``` +Shimmer +└── ShimmerContent +``` + +## Color + +The highlight band takes its color from `color`, and its alpha is the peak strength where it sweeps. Overlaying different colors on the same gray text tints the sweep toward each hue as it passes. + + + +## Duration + +`duration` sets how long one sweep takes, in milliseconds; it defaults to `2000`. A smaller value sweeps faster. + + + +## Width + +`spread` sets the width of the highlight band in logical pixels. A narrow band reads as a sharp glint, while a wide one washes over the whole row at once, with the same peak strength and only a different span. + + + +## Angle + +`angle` tilts the band relative to vertical, in degrees. At 0° every row is swept at the same column; when tilted, the glint shears along the height, grazing the top before the lower part. + + + +## Disabling + +Set `running` to `false` to stop the animation: no highlight is added and the content shows in its own colors. + + + +## API Reference + +### Shimmer + +The shimmer container; it captures the `ShimmerContent` subtree, composites it at full strength in its own colors, then overlays a tiltable highlight band that fades out on both sides, clipped to the content's opaque pixels. The highlight sweeps from left to right and only shows on the content. + + + +### ShimmerContent + +The captured content subtree (such as a status row of text, or an icon plus text), used as a direct child of Shimmer. When omitted, a default placeholder block filling the whole row is used. Shimmer is block-level with a height of 20px by default, which can be overridden with `width`/`height`. + + diff --git a/docs/en/widgets/spinner.mdx b/docs/en/widgets/spinner.mdx new file mode 100644 index 00000000..f8341f68 --- /dev/null +++ b/docs/en/widgets/spinner.mdx @@ -0,0 +1,58 @@ +# Spinner + +A self-drawn spinning indicator used to express a "loading" state. + + + +## Use cases + +- **Suitable**: a loading state that waits for an async result whose progress cannot be predicted +- **Suitable**: a busy indicator for a local area such as a button or a card +- **Not suitable**: progress with a definite percentage or a known duration (use `progress`) + +## Usage + +```tsx +import { Spinner, SpinnerContent } from "@lcui/react" +``` + +```tsx + +``` + +## Composition + +Use the following composition to build a `Spinner`: + +``` +Spinner +└── SpinnerContent +``` + +## Examples + +### Custom Content + + + +### Sizes + + + +### Running State + + + +## API Reference + +### Spinner + +The spinner container; it captures the `SpinnerContent` subtree and draws it rotated about its center. It is 16×16 by default and can be overridden with `width`/`height`. + + + +### SpinnerContent + +The content subtree captured for rotation, used as a direct child of Spinner. When omitted, a default ring is used, with one side left transparent to form a gap that sweeps around as it rotates. + + diff --git a/docs/examples/shimmer-angle-tsx/example.tsx b/docs/examples/shimmer-angle-tsx/example.tsx new file mode 100644 index 00000000..0faff1a3 --- /dev/null +++ b/docs/examples/shimmer-angle-tsx/example.tsx @@ -0,0 +1,18 @@ +import { Shimmer, ShimmerContent, Text, Widget } from "@lcui/react"; + +export default function ShimmerAngleDemo() { + return ( + + + + Flat band + + + + + Tilted band + + + + ); +} diff --git a/docs/examples/shimmer-angle-tsx/main.c b/docs/examples/shimmer-angle-tsx/main.c new file mode 100644 index 00000000..9a7d9dd5 --- /dev/null +++ b/docs/examples/shimmer-angle-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_shimmer_angle_demo()); + return lcui_main(); +} diff --git a/docs/examples/shimmer-angle-xml/main.c b/docs/examples/shimmer-angle-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/shimmer-angle-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/shimmer-angle-xml/ui.xml b/docs/examples/shimmer-angle-xml/ui.xml new file mode 100644 index 00000000..58a82b33 --- /dev/null +++ b/docs/examples/shimmer-angle-xml/ui.xml @@ -0,0 +1,35 @@ + + + + .shimmer-angle-demo { + display: flex; + flex-direction: column; + gap: 8px; + } + .shimmer-angle-demo shimmer { + width: 220px; + height: 36px; + } + .shimmer-angle-demo shimmer-content { + display: flex; + align-items: center; + padding: 4px 8px; + font-size: 15px; + color: #46545f; + } + + + + + + Flat band + + + + + Tilted band + + + + + diff --git a/docs/examples/shimmer-angle/main.c b/docs/examples/shimmer-angle/main.c new file mode 100644 index 00000000..55628fac --- /dev/null +++ b/docs/examples/shimmer-angle/main.c @@ -0,0 +1,56 @@ +#include + +/* The angle tilts the highlight band. A flat band (0 degrees) crosses each row + * at the same column; a 45 degree band shears across the caption so the glint + * reaches the top before the bottom. */ +static const char *shimmer_angle_css = "\ +.shimmer-angle-demo {\ + display: flex;\ + flex-direction: column;\ + gap: 8px;\ +}\ +.shimmer-angle-demo shimmer {\ + width: 220px;\ + height: 36px;\ +}\ +.shimmer-angle-demo shimmer-content {\ + display: flex;\ + align-items: center;\ + padding: 4px 8px;\ + font-size: 15px;\ + color: #46545f;\ +}\ +"; + +static ui_widget_t *caption(const char *label) +{ + ui_widget_t *shimmer, *content, *text; + + shimmer = ui_create_shimmer(); + content = ui_create_widget("shimmer-content"); + text = ui_create_widget("text"); + ui_text_set_content(text, label); + ui_widget_append(content, text); + ui_shimmer_set_content(shimmer, content); + ui_shimmer_set_running(shimmer, true); + return shimmer; +} + +void shimmer_angle_init(ui_widget_t *parent) +{ + ui_widget_t *box, *flat, *tilted; + + ui_load_css_string(shimmer_angle_css, "shimmer-angle-demo"); + box = ui_create_widget(NULL); + ui_widget_add_class(box, "shimmer-angle-demo"); + + flat = caption("Flat band"); + ui_shimmer_set_angle(flat, 0.0f); + + tilted = caption("Tilted band"); + ui_shimmer_set_angle(tilted, 45.0f); + + ui_widget_append(box, flat); + ui_widget_append(box, tilted); + ui_widget_append(parent, box); +} diff --git a/docs/examples/shimmer-basic-tsx/example.tsx b/docs/examples/shimmer-basic-tsx/example.tsx new file mode 100644 index 00000000..fa56ab64 --- /dev/null +++ b/docs/examples/shimmer-basic-tsx/example.tsx @@ -0,0 +1,11 @@ +import { Shimmer, ShimmerContent, Text } from "@lcui/react"; + +export default function ShimmerDemo() { + return ( + + + Generating response… + + + ); +} diff --git a/docs/examples/shimmer-basic-tsx/main.c b/docs/examples/shimmer-basic-tsx/main.c new file mode 100644 index 00000000..407e306d --- /dev/null +++ b/docs/examples/shimmer-basic-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_shimmer_demo()); + return lcui_main(); +} diff --git a/docs/examples/shimmer-basic-xml/main.c b/docs/examples/shimmer-basic-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/shimmer-basic-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/shimmer-basic-xml/ui.xml b/docs/examples/shimmer-basic-xml/ui.xml new file mode 100644 index 00000000..157e0232 --- /dev/null +++ b/docs/examples/shimmer-basic-xml/ui.xml @@ -0,0 +1,25 @@ + + + + .shimmer-basic-demo shimmer { + width: 220px; + height: 28px; + } + .shimmer-basic-demo shimmer-content { + display: flex; + align-items: center; + padding: 4px 8px; + font-size: 15px; + color: #46545f; + } + + + + + + Generating response… + + + + + diff --git a/docs/examples/shimmer-basic/main.c b/docs/examples/shimmer-basic/main.c new file mode 100644 index 00000000..c37c14e1 --- /dev/null +++ b/docs/examples/shimmer-basic/main.c @@ -0,0 +1,37 @@ +#include + +/* The shimmer sweeps a soft sheen across real text, so the caption stays + * readable while it reads as "in progress". Scoped under a unique class; an + * inline background-color would be overridden by the theme `*` rule. */ +static const char *shimmer_basic_css = "\ +.shimmer-basic-demo shimmer {\ + width: 220px;\ + height: 28px;\ +}\ +.shimmer-basic-demo shimmer-content {\ + display: flex;\ + align-items: center;\ + padding: 4px 8px;\ + font-size: 15px;\ + color: #46545f;\ +}\ +"; + +void shimmer_basic_init(ui_widget_t *parent) +{ + ui_widget_t *box, *shimmer, *content, *text; + + ui_load_css_string(shimmer_basic_css, "shimmer-basic-demo"); + box = ui_create_widget(NULL); + ui_widget_add_class(box, "shimmer-basic-demo"); + + shimmer = ui_create_shimmer(); + content = ui_create_widget("shimmer-content"); + text = ui_create_widget("text"); + ui_text_set_content(text, "Generating response..."); + ui_widget_append(content, text); + ui_shimmer_set_content(shimmer, content); + + ui_widget_append(box, shimmer); + ui_widget_append(parent, box); +} diff --git a/docs/examples/shimmer-color-tsx/example.tsx b/docs/examples/shimmer-color-tsx/example.tsx new file mode 100644 index 00000000..cd0833f7 --- /dev/null +++ b/docs/examples/shimmer-color-tsx/example.tsx @@ -0,0 +1,18 @@ +import { Shimmer, ShimmerContent, Text, Widget } from "@lcui/react"; + +export default function ShimmerColorDemo() { + return ( + + + + Generating response… + + + + + Sending request… + + + + ); +} diff --git a/docs/examples/shimmer-color-tsx/main.c b/docs/examples/shimmer-color-tsx/main.c new file mode 100644 index 00000000..ad883a6f --- /dev/null +++ b/docs/examples/shimmer-color-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_shimmer_color_demo()); + return lcui_main(); +} diff --git a/docs/examples/shimmer-color-xml/main.c b/docs/examples/shimmer-color-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/shimmer-color-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/shimmer-color-xml/ui.xml b/docs/examples/shimmer-color-xml/ui.xml new file mode 100644 index 00000000..2ba8e168 --- /dev/null +++ b/docs/examples/shimmer-color-xml/ui.xml @@ -0,0 +1,35 @@ + + + + .shimmer-color-demo { + display: flex; + flex-direction: column; + gap: 8px; + } + .shimmer-color-demo shimmer { + width: 220px; + height: 28px; + } + .shimmer-color-demo shimmer-content { + display: flex; + align-items: center; + padding: 4px 8px; + font-size: 15px; + color: #46545f; + } + + + + + + Generating response… + + + + + Sending request… + + + + + diff --git a/docs/examples/shimmer-color/main.c b/docs/examples/shimmer-color/main.c new file mode 100644 index 00000000..c5c23f8e --- /dev/null +++ b/docs/examples/shimmer-color/main.c @@ -0,0 +1,55 @@ +#include + +/* Two captions, each with a different highlight colour. The `color` sheen is + * overlaid only on the text's opaque pixels, so a blue and a green highlight + * tint the same grey ink toward their own hue as they sweep. */ +static const char *shimmer_color_css = "\ +.shimmer-color-demo {\ + display: flex;\ + flex-direction: column;\ + gap: 8px;\ +}\ +.shimmer-color-demo shimmer {\ + width: 220px;\ + height: 28px;\ +}\ +.shimmer-color-demo shimmer-content {\ + display: flex;\ + align-items: center;\ + padding: 4px 8px;\ + font-size: 15px;\ + color: #46545f;\ +}\ +"; + +static ui_widget_t *caption(const char *label) +{ + ui_widget_t *shimmer, *content, *text; + + shimmer = ui_create_shimmer(); + content = ui_create_widget("shimmer-content"); + text = ui_create_widget("text"); + ui_text_set_content(text, label); + ui_widget_append(content, text); + ui_shimmer_set_content(shimmer, content); + return shimmer; +} + +void shimmer_color_init(ui_widget_t *parent) +{ + ui_widget_t *box, *blue, *green; + + ui_load_css_string(shimmer_color_css, "shimmer-color-demo"); + box = ui_create_widget(NULL); + ui_widget_add_class(box, "shimmer-color-demo"); + + blue = caption("Generating response..."); + ui_shimmer_set_color(blue, pd_argb(200, 55, 138, 221)); + + green = caption("Sending request..."); + ui_shimmer_set_color(green, pd_argb(200, 34, 197, 94)); + + ui_widget_append(box, blue); + ui_widget_append(box, green); + ui_widget_append(parent, box); +} diff --git a/docs/examples/shimmer-duration-tsx/example.tsx b/docs/examples/shimmer-duration-tsx/example.tsx new file mode 100644 index 00000000..a29f7d97 --- /dev/null +++ b/docs/examples/shimmer-duration-tsx/example.tsx @@ -0,0 +1,18 @@ +import { Shimmer, ShimmerContent, Text, Widget } from "@lcui/react"; + +export default function ShimmerDurationDemo() { + return ( + + + + Default duration + + + + + Faster duration + + + + ); +} diff --git a/docs/examples/shimmer-duration-tsx/main.c b/docs/examples/shimmer-duration-tsx/main.c new file mode 100644 index 00000000..ccf50836 --- /dev/null +++ b/docs/examples/shimmer-duration-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_shimmer_duration_demo()); + return lcui_main(); +} diff --git a/docs/examples/shimmer-duration-xml/main.c b/docs/examples/shimmer-duration-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/shimmer-duration-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/shimmer-duration-xml/ui.xml b/docs/examples/shimmer-duration-xml/ui.xml new file mode 100644 index 00000000..97b5ef31 --- /dev/null +++ b/docs/examples/shimmer-duration-xml/ui.xml @@ -0,0 +1,35 @@ + + + + .shimmer-duration-demo { + display: flex; + flex-direction: column; + gap: 8px; + } + .shimmer-duration-demo shimmer { + width: 220px; + height: 28px; + } + .shimmer-duration-demo shimmer-content { + display: flex; + align-items: center; + padding: 4px 8px; + font-size: 15px; + color: #46545f; + } + + + + + + Default duration + + + + + Faster duration + + + + + diff --git a/docs/examples/shimmer-duration/main.c b/docs/examples/shimmer-duration/main.c new file mode 100644 index 00000000..ad36d846 --- /dev/null +++ b/docs/examples/shimmer-duration/main.c @@ -0,0 +1,54 @@ +#include + +/* Two identical sweeps at different durations: the default 2000 ms and a + * snappier 1000 ms. Duration is only observable while the shimmer is running, + * so both lines keep the animation on. */ +static const char *shimmer_duration_css = "\ +.shimmer-duration-demo {\ + display: flex;\ + flex-direction: column;\ + gap: 8px;\ +}\ +.shimmer-duration-demo shimmer {\ + width: 220px;\ + height: 28px;\ +}\ +.shimmer-duration-demo shimmer-content {\ + display: flex;\ + align-items: center;\ + padding: 4px 8px;\ + font-size: 15px;\ + color: #46545f;\ +}\ +"; + +static ui_widget_t *caption(const char *label) +{ + ui_widget_t *shimmer, *content, *text; + + shimmer = ui_create_shimmer(); + content = ui_create_widget("shimmer-content"); + text = ui_create_widget("text"); + ui_text_set_content(text, label); + ui_widget_append(content, text); + ui_shimmer_set_content(shimmer, content); + ui_shimmer_set_running(shimmer, true); + return shimmer; +} + +void shimmer_duration_init(ui_widget_t *parent) +{ + ui_widget_t *box, *normal, *fast; + + ui_load_css_string(shimmer_duration_css, "shimmer-duration-demo"); + box = ui_create_widget(NULL); + ui_widget_add_class(box, "shimmer-duration-demo"); + + normal = caption("Default duration"); + fast = caption("Faster duration"); + ui_shimmer_set_duration(fast, 1000); + + ui_widget_append(box, normal); + ui_widget_append(box, fast); + ui_widget_append(parent, box); +} diff --git a/docs/examples/shimmer-running-tsx/example.tsx b/docs/examples/shimmer-running-tsx/example.tsx new file mode 100644 index 00000000..40b8828c --- /dev/null +++ b/docs/examples/shimmer-running-tsx/example.tsx @@ -0,0 +1,18 @@ +import { Shimmer, ShimmerContent, Text, Widget } from "@lcui/react"; + +export default function ShimmerRunningDemo() { + return ( + + + + Searching the web… + + + + + Reading 4 files + + + + ); +} diff --git a/docs/examples/shimmer-running-tsx/main.c b/docs/examples/shimmer-running-tsx/main.c new file mode 100644 index 00000000..c6c8d2ea --- /dev/null +++ b/docs/examples/shimmer-running-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_shimmer_running_demo()); + return lcui_main(); +} diff --git a/docs/examples/shimmer-running-xml/main.c b/docs/examples/shimmer-running-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/shimmer-running-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/shimmer-running-xml/ui.xml b/docs/examples/shimmer-running-xml/ui.xml new file mode 100644 index 00000000..616483e4 --- /dev/null +++ b/docs/examples/shimmer-running-xml/ui.xml @@ -0,0 +1,35 @@ + + + + .shimmer-running-demo { + display: flex; + flex-direction: column; + gap: 8px; + } + .shimmer-running-demo shimmer { + width: 220px; + height: 28px; + } + .shimmer-running-demo shimmer-content { + display: flex; + align-items: center; + padding: 4px 8px; + font-size: 15px; + color: #46545f; + } + + + + + + Searching the web… + + + + + Reading 4 files + + + + + diff --git a/docs/examples/shimmer-running/main.c b/docs/examples/shimmer-running/main.c new file mode 100644 index 00000000..ecdd1b0a --- /dev/null +++ b/docs/examples/shimmer-running/main.c @@ -0,0 +1,56 @@ +#include + +/* The first caption keeps sweeping; the second has the animation turned off, + * so its text is composited as-is with no sheen added. `running = false` is the + * shimmer's off state: the content renders normally. */ +static const char *shimmer_running_css = "\ +.shimmer-running-demo {\ + display: flex;\ + flex-direction: column;\ + gap: 8px;\ +}\ +.shimmer-running-demo shimmer {\ + width: 220px;\ + height: 28px;\ +}\ +.shimmer-running-demo shimmer-content {\ + display: flex;\ + align-items: center;\ + padding: 4px 8px;\ + font-size: 15px;\ + color: #46545f;\ +}\ +"; + +static ui_widget_t *caption(const char *label) +{ + ui_widget_t *shimmer, *content, *text; + + shimmer = ui_create_shimmer(); + content = ui_create_widget("shimmer-content"); + text = ui_create_widget("text"); + ui_text_set_content(text, label); + ui_widget_append(content, text); + ui_shimmer_set_content(shimmer, content); + return shimmer; +} + +void shimmer_running_init(ui_widget_t *parent) +{ + ui_widget_t *box, *running, *stopped; + + ui_load_css_string(shimmer_running_css, "shimmer-running-demo"); + box = ui_create_widget(NULL); + ui_widget_add_class(box, "shimmer-running-demo"); + + running = caption("Searching the web..."); + ui_shimmer_set_running(running, true); + ui_shimmer_set_duration(running, 1000); + + stopped = caption("Reading 4 files"); + ui_shimmer_set_running(stopped, false); + + ui_widget_append(box, running); + ui_widget_append(box, stopped); + ui_widget_append(parent, box); +} diff --git a/docs/examples/shimmer-spread-tsx/example.tsx b/docs/examples/shimmer-spread-tsx/example.tsx new file mode 100644 index 00000000..7c7d47ef --- /dev/null +++ b/docs/examples/shimmer-spread-tsx/example.tsx @@ -0,0 +1,18 @@ +import { Shimmer, ShimmerContent, Text, Widget } from "@lcui/react"; + +export default function ShimmerSpreadDemo() { + return ( + + + + Narrow spread + + + + + Wide spread + + + + ); +} diff --git a/docs/examples/shimmer-spread-tsx/main.c b/docs/examples/shimmer-spread-tsx/main.c new file mode 100644 index 00000000..96615fee --- /dev/null +++ b/docs/examples/shimmer-spread-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_shimmer_spread_demo()); + return lcui_main(); +} diff --git a/docs/examples/shimmer-spread-xml/main.c b/docs/examples/shimmer-spread-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/shimmer-spread-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/shimmer-spread-xml/ui.xml b/docs/examples/shimmer-spread-xml/ui.xml new file mode 100644 index 00000000..848b7ace --- /dev/null +++ b/docs/examples/shimmer-spread-xml/ui.xml @@ -0,0 +1,35 @@ + + + + .shimmer-spread-demo { + display: flex; + flex-direction: column; + gap: 8px; + } + .shimmer-spread-demo shimmer { + width: 220px; + height: 28px; + } + .shimmer-spread-demo shimmer-content { + display: flex; + align-items: center; + padding: 4px 8px; + font-size: 15px; + color: #46545f; + } + + + + + + Narrow spread + + + + + Wide spread + + + + + diff --git a/docs/examples/shimmer-spread/main.c b/docs/examples/shimmer-spread/main.c new file mode 100644 index 00000000..04d34062 --- /dev/null +++ b/docs/examples/shimmer-spread/main.c @@ -0,0 +1,56 @@ +#include + +/* The spread sets the highlight band's width in logical pixels. A narrow band + * reads as a tight glint; a wide one washes across the whole caption at once. + * The peak strength is unchanged, only how far it reaches. */ +static const char *shimmer_spread_css = "\ +.shimmer-spread-demo {\ + display: flex;\ + flex-direction: column;\ + gap: 8px;\ +}\ +.shimmer-spread-demo shimmer {\ + width: 220px;\ + height: 28px;\ +}\ +.shimmer-spread-demo shimmer-content {\ + display: flex;\ + align-items: center;\ + padding: 4px 8px;\ + font-size: 15px;\ + color: #46545f;\ +}\ +"; + +static ui_widget_t *caption(const char *label) +{ + ui_widget_t *shimmer, *content, *text; + + shimmer = ui_create_shimmer(); + content = ui_create_widget("shimmer-content"); + text = ui_create_widget("text"); + ui_text_set_content(text, label); + ui_widget_append(content, text); + ui_shimmer_set_content(shimmer, content); + ui_shimmer_set_running(shimmer, true); + return shimmer; +} + +void shimmer_spread_init(ui_widget_t *parent) +{ + ui_widget_t *box, *narrow, *wide; + + ui_load_css_string(shimmer_spread_css, "shimmer-spread-demo"); + box = ui_create_widget(NULL); + ui_widget_add_class(box, "shimmer-spread-demo"); + + narrow = caption("Narrow spread"); + ui_shimmer_set_spread(narrow, 40.0f); + + wide = caption("Wide spread"); + ui_shimmer_set_spread(wide, 160.0f); + + ui_widget_append(box, narrow); + ui_widget_append(box, wide); + ui_widget_append(parent, box); +} diff --git a/docs/examples/spinner-basic-tsx/example.tsx b/docs/examples/spinner-basic-tsx/example.tsx new file mode 100644 index 00000000..beb1b8ed --- /dev/null +++ b/docs/examples/spinner-basic-tsx/example.tsx @@ -0,0 +1,5 @@ +import { Spinner } from "@lcui/react"; + +export default function SpinnerDemo() { + return ; +} diff --git a/docs/examples/spinner-basic-tsx/main.c b/docs/examples/spinner-basic-tsx/main.c new file mode 100644 index 00000000..e9103557 --- /dev/null +++ b/docs/examples/spinner-basic-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_spinner_demo()); + return lcui_main(); +} diff --git a/docs/examples/spinner-basic-xml/main.c b/docs/examples/spinner-basic-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/spinner-basic-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/spinner-basic-xml/ui.xml b/docs/examples/spinner-basic-xml/ui.xml new file mode 100644 index 00000000..9f40abe2 --- /dev/null +++ b/docs/examples/spinner-basic-xml/ui.xml @@ -0,0 +1,14 @@ + + + + .spinner-basic-box spinner { + width: 40px; + height: 40px; + } + + + + + + + diff --git a/docs/examples/spinner-basic/main.c b/docs/examples/spinner-basic/main.c new file mode 100644 index 00000000..1e786ea0 --- /dev/null +++ b/docs/examples/spinner-basic/main.c @@ -0,0 +1,10 @@ +#include + +void spinner_basic_init(ui_widget_t *parent) +{ + ui_widget_t *spinner; + + spinner = ui_create_spinner(); + ui_widget_resize(spinner, 40, 40); + ui_widget_append(parent, spinner); +} diff --git a/docs/examples/spinner-custom-content-tsx/example.tsx b/docs/examples/spinner-custom-content-tsx/example.tsx new file mode 100644 index 00000000..7a52562c --- /dev/null +++ b/docs/examples/spinner-custom-content-tsx/example.tsx @@ -0,0 +1,21 @@ +import { Spinner, SpinnerContent, Text } from "@lcui/react"; + +export default function SpinnerCustomContentDemo() { + return ( +
+ + +
+
+ + + + + + Hello + + + +
+ ); +} diff --git a/docs/examples/spinner-custom-content-tsx/main.c b/docs/examples/spinner-custom-content-tsx/main.c new file mode 100644 index 00000000..22b23d21 --- /dev/null +++ b/docs/examples/spinner-custom-content-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_spinner_custom_content_demo()); + return lcui_main(); +} diff --git a/docs/examples/spinner-custom-content-xml/main.c b/docs/examples/spinner-custom-content-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/spinner-custom-content-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/spinner-custom-content-xml/ui.xml b/docs/examples/spinner-custom-content-xml/ui.xml new file mode 100644 index 00000000..8573a696 --- /dev/null +++ b/docs/examples/spinner-custom-content-xml/ui.xml @@ -0,0 +1,61 @@ + + + + .spinner-custom-content-demo { + display: flex; + align-items: center; + gap: 28px; + } + .spinner-custom-content-demo .shape { + width: 40px; + height: 40px; + } + .spinner-custom-content-demo .word { + width: 96px; + height: 32px; + } + .spinner-custom-content-demo spinner-content { + display: flex; + align-items: center; + justify-content: center; + } + .spinner-custom-content-demo .c-dot { + position: absolute; + left: 15px; + top: 3px; + width: 10px; + height: 10px; + background-color: #4a90d9; + border-radius: 8px; + } + .spinner-custom-content-demo .c-bar { + position: absolute; + left: 18px; + top: 6px; + width: 4px; + height: 20px; + background-color: #d9534f; + border-radius: 2px; + } + .spinner-custom-content-demo .c-text { + white-space: nowrap; + font-size: 18px; + color: #e5484d; + } + + + + + + + + + + + + Hello + + + + + diff --git a/docs/examples/spinner-custom-content/main.c b/docs/examples/spinner-custom-content/main.c new file mode 100644 index 00000000..44cdffdb --- /dev/null +++ b/docs/examples/spinner-custom-content/main.c @@ -0,0 +1,85 @@ +#include + +static const char *spinner_custom_content_css = "\ +.spinner-custom-content-demo {\ + display: flex;\ + align-items: center;\ + gap: 28px;\ +}\ +.spinner-custom-content-demo spinner-content {\ + display: flex;\ + align-items: center;\ + justify-content: center;\ +}\ +.spinner-custom-content-demo spinner-content .c-dot {\ + position: absolute;\ + left: 15px;\ + top: 3px;\ + width: 10px;\ + height: 10px;\ + background-color: #4a90d9;\ + border-radius: 8px;\ +}\ +.spinner-custom-content-demo spinner-content .c-bar {\ + position: absolute;\ + left: 18px;\ + top: 6px;\ + width: 4px;\ + height: 20px;\ + background-color: #d9534f;\ + border-radius: 2px;\ +}\ +.spinner-custom-content-demo spinner-content .c-text {\ + white-space: nowrap;\ + font-size: 18px;\ + color: #e5484d;\ +}\ +"; + +static ui_widget_t *create_shape_content(void) +{ + ui_widget_t *content, *dot, *bar; + + content = ui_create_widget("spinner-content"); + dot = ui_create_widget(NULL); + bar = ui_create_widget(NULL); + ui_widget_add_class(dot, "c-dot"); + ui_widget_add_class(bar, "c-bar"); + ui_widget_append(content, dot); + ui_widget_append(content, bar); + return content; +} + +static ui_widget_t *create_text_content(void) +{ + ui_widget_t *content, *text; + + content = ui_create_widget("spinner-content"); + text = ui_create_widget("text"); + ui_text_set_content(text, "Hello"); + ui_widget_add_class(text, "c-text"); + ui_widget_append(content, text); + return content; +} + +void spinner_custom_content_init(ui_widget_t *parent) +{ + ui_widget_t *row, *spinner; + + ui_load_css_string(spinner_custom_content_css, + "spinner-custom-content-demo"); + row = ui_create_widget(NULL); + ui_widget_add_class(row, "spinner-custom-content-demo"); + + spinner = ui_create_spinner(); + ui_widget_resize(spinner, 40, 40); + ui_spinner_set_content(spinner, create_shape_content()); + ui_widget_append(row, spinner); + + spinner = ui_create_spinner(); + ui_widget_resize(spinner, 96, 32); + ui_spinner_set_content(spinner, create_text_content()); + ui_widget_append(row, spinner); + + ui_widget_append(parent, row); +} diff --git a/docs/examples/spinner-running-tsx/example.tsx b/docs/examples/spinner-running-tsx/example.tsx new file mode 100644 index 00000000..fad65d4a --- /dev/null +++ b/docs/examples/spinner-running-tsx/example.tsx @@ -0,0 +1,10 @@ +import { Spinner, Widget } from "@lcui/react"; + +export default function SpinnerRunningDemo() { + return ( + + + + + ); +} diff --git a/docs/examples/spinner-running-tsx/main.c b/docs/examples/spinner-running-tsx/main.c new file mode 100644 index 00000000..50e15007 --- /dev/null +++ b/docs/examples/spinner-running-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_spinner_running_demo()); + return lcui_main(); +} diff --git a/docs/examples/spinner-running-xml/main.c b/docs/examples/spinner-running-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/spinner-running-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/spinner-running-xml/ui.xml b/docs/examples/spinner-running-xml/ui.xml new file mode 100644 index 00000000..f8cccb3e --- /dev/null +++ b/docs/examples/spinner-running-xml/ui.xml @@ -0,0 +1,21 @@ + + + + .spinner-running-demo { + display: flex; + flex-direction: row; + align-items: center; + gap: 24px; + } + .spinner-running-demo spinner { + width: 40px; + height: 40px; + } + + + + + + + + diff --git a/docs/examples/spinner-running/main.c b/docs/examples/spinner-running/main.c new file mode 100644 index 00000000..aed51bf7 --- /dev/null +++ b/docs/examples/spinner-running/main.c @@ -0,0 +1,33 @@ +#include + +static const char *spinner_running_css = "\ +.spinner-running-demo {\ + display: flex;\ + flex-direction: row;\ + align-items: center;\ + gap: 24px;\ +}\ +"; + +void spinner_running_init(ui_widget_t *parent) +{ + ui_widget_t *box, *running, *stopped; + + ui_load_css_string(spinner_running_css, "spinner-running-demo"); + box = ui_create_widget(NULL); + ui_widget_add_class(box, "spinner-running-demo"); + + running = ui_create_spinner(); + ui_widget_resize(running, 40, 40); + ui_spinner_set_running(running, true); + ui_spinner_set_speed(running, 0.5f); + + stopped = ui_create_spinner(); + ui_widget_resize(stopped, 40, 40); + ui_spinner_set_running(stopped, false); + ui_spinner_set_angle(stopped, 90.0); + + ui_widget_append(box, running); + ui_widget_append(box, stopped); + ui_widget_append(parent, box); +} diff --git a/docs/examples/spinner-size-tsx/example.tsx b/docs/examples/spinner-size-tsx/example.tsx new file mode 100644 index 00000000..ab0f6dfb --- /dev/null +++ b/docs/examples/spinner-size-tsx/example.tsx @@ -0,0 +1,11 @@ +import { Spinner, Widget } from "@lcui/react"; + +export default function SpinnerSizeDemo() { + return ( + + + + + + ); +} diff --git a/docs/examples/spinner-size-tsx/main.c b/docs/examples/spinner-size-tsx/main.c new file mode 100644 index 00000000..e08751c8 --- /dev/null +++ b/docs/examples/spinner-size-tsx/main.c @@ -0,0 +1,11 @@ +#include + +/* example.tsx is compiled to example.h by @lcui/cli */ +#include "example.h" + +int main(void) +{ + lcui_init(); + ui_root_append(ui_create_spinner_size_demo()); + return lcui_main(); +} diff --git a/docs/examples/spinner-size-xml/main.c b/docs/examples/spinner-size-xml/main.c new file mode 100644 index 00000000..9f6a1e96 --- /dev/null +++ b/docs/examples/spinner-size-xml/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + lcui_init(); + ui_load_xml_file("ui.xml"); + return lcui_main(); +} diff --git a/docs/examples/spinner-size-xml/ui.xml b/docs/examples/spinner-size-xml/ui.xml new file mode 100644 index 00000000..6c07076e --- /dev/null +++ b/docs/examples/spinner-size-xml/ui.xml @@ -0,0 +1,30 @@ + + + + .spinner-size-demo { + display: flex; + flex-direction: row; + align-items: center; + gap: 16px; + } + .spinner-size-demo .sz-sm { + width: 24px; + height: 24px; + } + .spinner-size-demo .sz-md { + width: 40px; + height: 40px; + } + .spinner-size-demo .sz-lg { + width: 64px; + height: 64px; + } + + + + + + + + + diff --git a/docs/examples/spinner-size/main.c b/docs/examples/spinner-size/main.c new file mode 100644 index 00000000..3ecc4532 --- /dev/null +++ b/docs/examples/spinner-size/main.c @@ -0,0 +1,40 @@ +#include + +static const char *spinner_size_css = "\ +.spinner-size-demo {\ + display: flex;\ + flex-direction: row;\ + align-items: center;\ + gap: 16px;\ +}\ +.spinner-size-demo .sz-sm {\ + width: 24px;\ + height: 24px;\ +}\ +.spinner-size-demo .sz-md {\ + width: 40px;\ + height: 40px;\ +}\ +.spinner-size-demo .sz-lg {\ + width: 64px;\ + height: 64px;\ +}\ +"; + +void spinner_size_init(ui_widget_t *parent) +{ + ui_widget_t *box; + ui_widget_t *spinner; + const char *sizes[] = { "sz-sm", "sz-md", "sz-lg" }; + size_t i; + + ui_load_css_string(spinner_size_css, "spinner-size-demo"); + box = ui_create_widget(NULL); + ui_widget_add_class(box, "spinner-size-demo"); + for (i = 0; i < 3; ++i) { + spinner = ui_create_spinner(); + ui_widget_add_class(spinner, sizes[i]); + ui_widget_append(box, spinner); + } + ui_widget_append(parent, box); +} diff --git a/docs/sidebars.json b/docs/sidebars.json index 5134ce62..786b562b 100644 --- a/docs/sidebars.json +++ b/docs/sidebars.json @@ -46,6 +46,8 @@ { "slug": "radio-group", "label": { "en": "RadioGroup", "zh-CN": "RadioGroup 单选组" } }, { "slug": "select", "label": { "en": "Select", "zh-CN": "Select 下拉选择" } }, { "slug": "scrollarea", "label": { "en": "ScrollArea", "zh-CN": "ScrollArea 滚动区域" } }, + { "slug": "spinner", "label": { "en": "Spinner", "zh-CN": "Spinner 加载指示器" } }, + { "slug": "shimmer", "label": { "en": "Shimmer", "zh-CN": "Shimmer 高光" } }, { "slug": "text", "label": { "en": "Text", "zh-CN": "Text 文本" } }, { "slug": "text-input", "label": { "en": "TextInput", "zh-CN": "TextInput 文本输入" } } ] diff --git a/docs/widget-fields/shimmer-content.ts b/docs/widget-fields/shimmer-content.ts new file mode 100644 index 00000000..a1e3e369 --- /dev/null +++ b/docs/widget-fields/shimmer-content.ts @@ -0,0 +1,5 @@ +import { widgetFields, FieldData } from "./widget"; + +// `shimmer-content` 继承公共字段;作为 Shimmer 的直属内容子树,其内部节点会被 +// 捕获为骨架遮罩,本身不暴露额外可配置属性。 +export const shimmerContentFields: FieldData[] = [...widgetFields]; diff --git a/docs/widget-fields/shimmer.ts b/docs/widget-fields/shimmer.ts new file mode 100644 index 00000000..c159fc5c --- /dev/null +++ b/docs/widget-fields/shimmer.ts @@ -0,0 +1,59 @@ +import { widgetFields, FieldData } from "./widget"; + +export const shimmerFields: FieldData[] = [ + ...widgetFields, + { + name: "running", + type: "boolean", + default: "true", + description: { + en: "Whether the sheen animation is running. True by default; when off the content is composited with no highlight added.", + "zh-CN": "是否播放扫光动画。默认为 true;关闭时不再叠加高光,内容按原色显示。", + }, + }, + { + name: "color", + type: "string", + default: "rgba(255, 255, 255, 0.86)", + description: { + en: "Highlight colour overlaid on the content's opaque pixels, given as a CSS colour string; its alpha is the peak strength of the sheen.", + "zh-CN": "叠加到内容不透明像素上的高光颜色,采用 CSS 颜色字符串;其 alpha 即扫过处的峰值强度。", + }, + }, + { + name: "duration", + type: "number", + default: "2000", + description: { + en: "Time for one full sweep in milliseconds. Defaults to 2000.", + "zh-CN": "单次扫过的时长,单位为毫秒。默认为 2000。", + }, + }, + { + name: "spread", + type: "number", + default: "-", + description: { + en: "Width of the highlight band in logical pixels. When unset it defaults to about 45% of the box width and is recomputed on resize; setting it pins a fixed width.", + "zh-CN": "高光带宽度,单位为逻辑像素。未设置时默认为框宽的约 45% 并在尺寸变化时重新计算;显式设置后固定为该值。", + }, + }, + { + name: "angle", + type: "number", + default: "20", + description: { + en: "Tilt of the highlight band in degrees, shearing its center line across the height. Defaults to 20.", + "zh-CN": "高光带倾斜角,单位为度,使反光中心线沿高度方向错切。默认为 20。", + }, + }, + { + name: "phase", + type: "number", + default: "0", + description: { + en: "Sweep phase in [0, 1). Added for deterministic tests and manual control; while running the animation advances it by the duration.", + "zh-CN": "高光带相位,取值范围 [0, 1),用于确定性测试或手动控制;运行状态下由 duration 持续推进。", + }, + }, +]; diff --git a/docs/widget-fields/spinner-content.ts b/docs/widget-fields/spinner-content.ts new file mode 100644 index 00000000..c150af39 --- /dev/null +++ b/docs/widget-fields/spinner-content.ts @@ -0,0 +1,3 @@ +import { widgetFields, FieldData } from "./widget"; + +export const spinnerContentFields: FieldData[] = [...widgetFields]; diff --git a/docs/widget-fields/spinner.ts b/docs/widget-fields/spinner.ts new file mode 100644 index 00000000..fcaa126f --- /dev/null +++ b/docs/widget-fields/spinner.ts @@ -0,0 +1,32 @@ +import { widgetFields, FieldData } from "./widget"; + +export const spinnerFields: FieldData[] = [ + ...widgetFields, + { + name: "running", + type: "boolean", + default: "true", + description: { + en: "Whether the rotation animation is running. True by default.", + "zh-CN": "是否播放旋转动画。默认为 true。", + }, + }, + { + name: "speed", + type: "number", + default: "1", + description: { + en: "Rotation speed in revolutions per second.", + "zh-CN": "旋转速度,单位为每秒转数 (rev/s)。", + }, + }, + { + name: "angle", + type: "number", + default: "0", + description: { + en: "Current rotation angle in degrees. While running, the animation drives this value.", + "zh-CN": "当前旋转角度,单位为度。运行状态下由动画持续推进。", + }, + }, +]; diff --git a/docs/zh-CN/widgets/shimmer.mdx b/docs/zh-CN/widgets/shimmer.mdx new file mode 100644 index 00000000..58d153c9 --- /dev/null +++ b/docs/zh-CN/widgets/shimmer.mdx @@ -0,0 +1,82 @@ +# Shimmer + +给文本、图标等内容叠加一道左右柔化、可倾斜的高光带,从左往右扫过;高光只落在内容的不透明像素上,内容本身保持原色。 + + + +## 适用场景 + +- **适用**:让一段状态文本(图标或纯文字)带着柔和高光扫过,表示“进行中/加载” +- **适用**:流式输出时给“正在生成”的提示文字加一层滑动高光 +- **不适用**:纯灰块的骨架占位(Shimmer 叠加在真实内容上,不生成占位骨架) +- **不适用**:有明确百分比或已知时长的任务进度(用 `progress`) +- **不适用**:无固定形状的全局忙碌指示(用 `spinner`) + +## 用法 + +```tsx +import { Shimmer, ShimmerContent } from "@lcui/react" +``` + +Shimmer 是 C 代码自绘部件:先按原色完整合成内容,再把一道带倾斜的高光带裁剪到内容的不透明像素上叠加。高光本身可配置颜色、周期、宽度与倾斜角,通过部件属性或对应的 `ui_shimmer_set_*` 接口设置: + +```tsx + + + Generating response… + + +``` + +## 组合 + +使用以下组合来构建 `Shimmer`: + +``` +Shimmer +└── ShimmerContent +``` + +## 颜色 + +高光带叠加的颜色取自 `color`,其 alpha 就是扫过处的峰值强度。同一段灰字上叠不同颜色,扫过时会被“染”向各自的色相。 + + + +## 时长 + +`duration` 决定高光扫过一次所需的时间,单位为毫秒,默认 `2000`。值越小扫得越快。 + + + +## 宽度 + +`spread` 决定高光带的宽度(逻辑像素)。窄带像一道锐利反光,宽带则一次漫过整行,峰值强度不变、只是覆盖范围不同。 + + + +## 倾斜 + +`angle` 让高光带相对竖直方向倾斜(度数)。0° 时各行在同一列被扫到,倾斜后反光沿高度错切,先从顶部掠过再到下方。 + + + +## 关闭高亮 + +把 `running` 设为 `false` 即可关闭动画:此时不再叠加高光,内容按原色正常显示。 + + + +## API 参考 + +### Shimmer + +扫光容器,捕获 `ShimmerContent` 子树;先按原色完整合成内容,再叠加一道可倾斜、左右渐隐的高光带并裁剪到内容不透明处。高光从左往右扫过,只在内容上显现。 + + + +### ShimmerContent + +被捕获的内容子树(如文本或图标加文字的状态行),作为 Shimmer 的直属子部件使用;省略时使用一个填满整行的默认占位块。Shimmer 默认块级、高度 20px,可用 `width`/`height` 覆盖。 + + diff --git a/docs/zh-CN/widgets/spinner.mdx b/docs/zh-CN/widgets/spinner.mdx new file mode 100644 index 00000000..0042f107 --- /dev/null +++ b/docs/zh-CN/widgets/spinner.mdx @@ -0,0 +1,58 @@ +# Spinner + +用于表达“加载中”的自绘旋转指示器部件。 + + + +## 适用场景 + +- **适用**:等待异步结果、无法预知进度的加载状态 +- **适用**:按钮、卡片等局部区域的忙碌指示 +- **不适用**:有明确百分比或已知时长的任务进度(用 `progress`) + +## 用法 + +```tsx +import { Spinner, SpinnerContent } from "@lcui/react" +``` + +```tsx + +``` + +## 组合 + +使用以下组合来构建 `Spinner`: + +``` +Spinner +└── SpinnerContent +``` + +## 示例 + +### 自定义内容 + + + +### 不同尺寸 + + + +### 运行状态控制 + + + +## API 参考 + +### Spinner + +旋转指示器容器,捕获 `SpinnerContent` 子树并绕其中心旋转绘制。默认自带 16×16 尺寸,可用 `width`/`height` 覆盖。 + + + +### SpinnerContent + +被旋转捕获的内容子树,作为 Spinner 的直接子部件使用;省略时使用默认的圆环,其中一边透明形成随旋转扫过的缺口。 + + diff --git a/examples/doc-viewer/app/en/widgets/shimmer/page.c b/examples/doc-viewer/app/en/widgets/shimmer/page.c new file mode 100644 index 00000000..e28a2759 --- /dev/null +++ b/examples/doc-viewer/app/en/widgets/shimmer/page.c @@ -0,0 +1,54 @@ +#include "page.tsx.h" +#include "page.h" + +typedef struct { + en_widgets_shimmer_page_react_t base; + // Add additional states to your component here + // ... +} en_widgets_shimmer_page_t; + +static void en_widgets_shimmer_page_init(ui_widget_t *w) +{ + ui_widget_add_data(w, en_widgets_shimmer_page_proto, + sizeof(en_widgets_shimmer_page_t)); + + en_widgets_shimmer_page_react_init(w); + // Write the initialization code for your component here + // such as state initialization, event binding, etc + // ... + + en_widgets_shimmer_page_update(w); +} + +static en_widgets_shimmer_page_t *en_widgets_shimmer_page_get(ui_widget_t *w) +{ + return ui_widget_get_data(w, en_widgets_shimmer_page_proto); +} + +static void en_widgets_shimmer_page_destroy(ui_widget_t *w) +{ + // Write code here to destroy the relevant resources of the component + // ... + + en_widgets_shimmer_page_react_destroy(w); +} + +void en_widgets_shimmer_page_update(ui_widget_t *w) +{ + en_widgets_shimmer_page_react_update(w); + // Write code here to update other content of your component + // ... +} + +ui_widget_t *ui_create_en_widgets_shimmer_page(void) +{ + return ui_create_widget_with_prototype(en_widgets_shimmer_page_proto); +} + +void ui_register_en_widgets_shimmer_page(void) +{ + en_widgets_shimmer_page_init_prototype(); + en_widgets_shimmer_page_proto->init = en_widgets_shimmer_page_init; + en_widgets_shimmer_page_proto->destroy = + en_widgets_shimmer_page_destroy; +} diff --git a/examples/doc-viewer/app/en/widgets/shimmer/page.h b/examples/doc-viewer/app/en/widgets/shimmer/page.h new file mode 100644 index 00000000..90b1210f --- /dev/null +++ b/examples/doc-viewer/app/en/widgets/shimmer/page.h @@ -0,0 +1,9 @@ +#include + +void ui_register_en_widgets_shimmer_page(void); + +ui_widget_t *ui_create_en_widgets_shimmer_page(void); + +void en_widgets_shimmer_page_update(ui_widget_t *w); + +void ui_load_en_widgets_shimmer_page_resources(void); diff --git a/examples/doc-viewer/app/en/widgets/shimmer/page.tsx b/examples/doc-viewer/app/en/widgets/shimmer/page.tsx new file mode 100644 index 00000000..75382c9a --- /dev/null +++ b/examples/doc-viewer/app/en/widgets/shimmer/page.tsx @@ -0,0 +1,105 @@ +import { Text, Widget } from "@lcui/react"; +import CodeBlockCopy from "../../../components/code-block-copy"; +import ShimmerBasicDemo from "../../../examples/shimmer-basic/index"; +import ShimmerColorDemo from "../../../examples/shimmer-color/index"; +import ShimmerDurationDemo from "../../../examples/shimmer-duration/index"; +import ShimmerSpreadDemo from "../../../examples/shimmer-spread/index"; +import ShimmerAngleDemo from "../../../examples/shimmer-angle/index"; +import ShimmerRunningDemo from "../../../examples/shimmer-running/index"; +import FieldTable from "../../../components/field-table"; +import { shimmerFields } from "../../../widget-fields/shimmer"; +import { shimmerContentFields } from "../../../widget-fields/shimmer-content"; + +export default function ShimmerDocPage() { + return ( + +

Shimmer

+

{`Overlays a soft-edged, tiltable highlight band on text, icons, or other content and sweeps it from left to right; the highlight only lands on the content's opaque pixels while the content keeps its own colors.`}

+ + +

Use cases

+
    +
  • {`[b]Suitable[/b]: letting a status line (an icon or plain text) sweep with a soft highlight to signal "in progress / loading"`}
  • +
  • {`[b]Suitable[/b]: adding a sliding highlight to a "generating" hint while text streams in`}
  • +
  • {`[b]Not suitable[/b]: a plain gray skeleton placeholder (Shimmer overlays real content and does not generate a skeleton)`}
  • +
  • {`[b]Not suitable[/b]: progress with a definite percentage or a known duration (use [bgcolor=#eee] progress [/bgcolor])`}
  • +
  • {`[b]Not suitable[/b]: a global busy indicator without a fixed shape (use [bgcolor=#eee] spinner [/bgcolor])`}
  • +
+
+ +

Usage

+ + + TypeScript + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Shimmer[/color], [color=#8250df]ShimmerContent[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color]`} + + +

{`Shimmer is a self-drawn widget implemented in C: it first composites the content at full strength in its own colors, then overlays a tilted highlight band clipped to the content's opaque pixels. The highlight itself can be configured with a color, period, width, and tilt, either through widget properties or the matching [bgcolor=#eee] ui_shimmer_set_* [/bgcolor] APIs:`}

+ + + TypeScript + + + + {`<[color=#8250df]Shimmer[/color] running={[color=#0550ae]true[/color]} duration={[color=#0550ae]2000[/color]} spread={[color=#0550ae]120[/color]} angle={[color=#0550ae]20[/color]} color=[color=#0a3069]"rgba(255,255,255,0.86)"[/color]>`} + {`  [color=#116329]<[color=#0550ae]ShimmerContent[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]Text[/color]>[/color]Generating response…[color=#116329][/color]`} + {`  [color=#116329][/color]`} + {``} + + +
+ +

Composition

+

{`Use the following composition to build a [bgcolor=#eee] Shimmer [/bgcolor]:`}

+ + + TEXT + + + + {`Shimmer`} + {`└── ShimmerContent`} + + +
+ +

Color

+

{`The highlight band takes its color from [bgcolor=#eee] color [/bgcolor], and its alpha is the peak strength where it sweeps. Overlaying different colors on the same gray text tints the sweep toward each hue as it passes.`}

+ +
+ +

Duration

+

{`[bgcolor=#eee] duration [/bgcolor] sets how long one sweep takes, in milliseconds; it defaults to [bgcolor=#eee] 2000 [/bgcolor]. A smaller value sweeps faster.`}

+ +
+ +

Width

+

{`[bgcolor=#eee] spread [/bgcolor] sets the width of the highlight band in logical pixels. A narrow band reads as a sharp glint, while a wide one washes over the whole row at once, with the same peak strength and only a different span.`}

+ +
+ +

Angle

+

{`[bgcolor=#eee] angle [/bgcolor] tilts the band relative to vertical, in degrees. At 0° every row is swept at the same column; when tilted, the glint shears along the height, grazing the top before the lower part.`}

+ +
+ +

Disabling

+

{`Set [bgcolor=#eee] running [/bgcolor] to [bgcolor=#eee] false [/bgcolor] to stop the animation: no highlight is added and the content shows in its own colors.`}

+ +
+ +

API Reference

+

Shimmer

+

{`The shimmer container; it captures the [bgcolor=#eee] ShimmerContent [/bgcolor] subtree, composites it at full strength in its own colors, then overlays a tiltable highlight band that fades out on both sides, clipped to the content's opaque pixels. The highlight sweeps from left to right and only shows on the content.`}

+ +

ShimmerContent

+

{`The captured content subtree (such as a status row of text, or an icon plus text), used as a direct child of Shimmer. When omitted, a default placeholder block filling the whole row is used. Shimmer is block-level with a height of 20px by default, which can be overridden with [bgcolor=#eee] width [/bgcolor]/[bgcolor=#eee] height [/bgcolor].`}

+ +
+
+ ); +} diff --git a/examples/doc-viewer/app/en/widgets/spinner/page.c b/examples/doc-viewer/app/en/widgets/spinner/page.c new file mode 100644 index 00000000..fe7c6bb4 --- /dev/null +++ b/examples/doc-viewer/app/en/widgets/spinner/page.c @@ -0,0 +1,54 @@ +#include "page.tsx.h" +#include "page.h" + +typedef struct { + en_widgets_spinner_page_react_t base; + // Add additional states to your component here + // ... +} en_widgets_spinner_page_t; + +static void en_widgets_spinner_page_init(ui_widget_t *w) +{ + ui_widget_add_data(w, en_widgets_spinner_page_proto, + sizeof(en_widgets_spinner_page_t)); + + en_widgets_spinner_page_react_init(w); + // Write the initialization code for your component here + // such as state initialization, event binding, etc + // ... + + en_widgets_spinner_page_update(w); +} + +static en_widgets_spinner_page_t *en_widgets_spinner_page_get(ui_widget_t *w) +{ + return ui_widget_get_data(w, en_widgets_spinner_page_proto); +} + +static void en_widgets_spinner_page_destroy(ui_widget_t *w) +{ + // Write code here to destroy the relevant resources of the component + // ... + + en_widgets_spinner_page_react_destroy(w); +} + +void en_widgets_spinner_page_update(ui_widget_t *w) +{ + en_widgets_spinner_page_react_update(w); + // Write code here to update other content of your component + // ... +} + +ui_widget_t *ui_create_en_widgets_spinner_page(void) +{ + return ui_create_widget_with_prototype(en_widgets_spinner_page_proto); +} + +void ui_register_en_widgets_spinner_page(void) +{ + en_widgets_spinner_page_init_prototype(); + en_widgets_spinner_page_proto->init = en_widgets_spinner_page_init; + en_widgets_spinner_page_proto->destroy = + en_widgets_spinner_page_destroy; +} diff --git a/examples/doc-viewer/app/en/widgets/spinner/page.h b/examples/doc-viewer/app/en/widgets/spinner/page.h new file mode 100644 index 00000000..873b0d89 --- /dev/null +++ b/examples/doc-viewer/app/en/widgets/spinner/page.h @@ -0,0 +1,9 @@ +#include + +void ui_register_en_widgets_spinner_page(void); + +ui_widget_t *ui_create_en_widgets_spinner_page(void); + +void en_widgets_spinner_page_update(ui_widget_t *w); + +void ui_load_en_widgets_spinner_page_resources(void); diff --git a/examples/doc-viewer/app/en/widgets/spinner/page.tsx b/examples/doc-viewer/app/en/widgets/spinner/page.tsx new file mode 100644 index 00000000..fbb4bc88 --- /dev/null +++ b/examples/doc-viewer/app/en/widgets/spinner/page.tsx @@ -0,0 +1,80 @@ +import { Text, Widget } from "@lcui/react"; +import CodeBlockCopy from "../../../components/code-block-copy"; +import SpinnerBasicDemo from "../../../examples/spinner-basic/index"; +import SpinnerCustomContentDemo from "../../../examples/spinner-custom-content/index"; +import SpinnerSizeDemo from "../../../examples/spinner-size/index"; +import SpinnerRunningDemo from "../../../examples/spinner-running/index"; +import FieldTable from "../../../components/field-table"; +import { spinnerFields } from "../../../widget-fields/spinner"; +import { spinnerContentFields } from "../../../widget-fields/spinner-content"; + +export default function SpinnerDocPage() { + return ( + +

Spinner

+

{`A self-drawn spinning indicator used to express a "loading" state.`}

+ + +

Use cases

+
    +
  • {`[b]Suitable[/b]: a loading state that waits for an async result whose progress cannot be predicted`}
  • +
  • {`[b]Suitable[/b]: a busy indicator for a local area such as a button or a card`}
  • +
  • {`[b]Not suitable[/b]: progress with a definite percentage or a known duration (use [bgcolor=#eee] progress [/bgcolor])`}
  • +
+
+ +

Usage

+ + + TypeScript + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Spinner[/color], [color=#8250df]SpinnerContent[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color]`} + + + + + TypeScript + + + + {`<[color=#8250df]Spinner[/color] running={[color=#0550ae]true[/color]} speed={[color=#0550ae]1[/color]} />`} + + +
+ +

Composition

+

{`Use the following composition to build a [bgcolor=#eee] Spinner [/bgcolor]:`}

+ + + TEXT + + + + {`Spinner`} + {`└── SpinnerContent`} + + +
+ +

Examples

+

Custom Content

+ +

Sizes

+ +

Running State

+ +
+ +

API Reference

+

Spinner

+

{`The spinner container; it captures the [bgcolor=#eee] SpinnerContent [/bgcolor] subtree and draws it rotated about its center. It is 16×16 by default and can be overridden with [bgcolor=#eee] width [/bgcolor]/[bgcolor=#eee] height [/bgcolor].`}

+ +

SpinnerContent

+

{`The content subtree captured for rotation, used as a direct child of Spinner. When omitted, a default ring is used, with one side left transparent to form a gap that sweeps around as it rotates.`}

+ +
+
+ ); +} diff --git a/examples/doc-viewer/app/examples/shimmer-angle/index.c b/examples/doc-viewer/app/examples/shimmer-angle/index.c new file mode 100644 index 00000000..0438da00 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-angle/index.c @@ -0,0 +1,43 @@ +#include "index.tsx.h" +#include "index.h" + +#define caption shimmer_angle__caption +#include "../../../../../docs/examples/shimmer-angle/main.c" +#undef caption + +typedef struct { + shimmer_angle_demo_react_t base; +} shimmer_angle_demo_t; + +void shimmer_angle_demo_update(ui_widget_t *w) +{ + shimmer_angle_demo_react_update(w); +} + +static void shimmer_angle_demo_init(ui_widget_t *w) +{ + shimmer_angle_demo_t *_that = ui_widget_add_data( + w, shimmer_angle_demo_proto, sizeof(shimmer_angle_demo_t)); + shimmer_angle_demo_proto->proto->init(w); + shimmer_angle_demo_react_init(w); + shimmer_angle_init(_that->base.refs.preview); + shimmer_angle_demo_update(w); +} + +static void shimmer_angle_demo_destroy(ui_widget_t *w) +{ + shimmer_angle_demo_proto->proto->destroy(w); + shimmer_angle_demo_react_destroy(w); +} + +ui_widget_t *ui_create_shimmer_angle_demo(void) +{ + return ui_create_widget_with_prototype(shimmer_angle_demo_proto); +} + +void ui_register_shimmer_angle_demo(void) +{ + shimmer_angle_demo_init_prototype(); + shimmer_angle_demo_proto->init = shimmer_angle_demo_init; + shimmer_angle_demo_proto->destroy = shimmer_angle_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/shimmer-angle/index.tsx b/examples/doc-viewer/app/examples/shimmer-angle/index.tsx new file mode 100644 index 00000000..cd82f1e0 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-angle/index.tsx @@ -0,0 +1,170 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function ShimmerAngleDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Shimmer[/color], [color=#8250df]ShimmerContent[/color], [color=#8250df]Text[/color], [color=#8250df]Widget[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]ShimmerAngleDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]Widget[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex flex-col gap-2"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-9"[/color] [color=#0550ae]running[/color] [color=#0550ae]angle[/color]=[color=#0a3069]{0}[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Flat band[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-9"[/color] [color=#0550ae]running[/color] [color=#0550ae]angle[/color]=[color=#0a3069]{45}[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Tilted band[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_shimmer_angle_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* The angle tilts the highlight band. A flat band (0 degrees) crosses each row[/color]`} + {`[color=#6e7781] * at the same column; a 45 degree band shears across the caption so the glint[/color]`} + {`[color=#6e7781] * reaches the top before the bottom. */[/color]`} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *shimmer_angle_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].shimmer-angle-demo {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] flex-direction: column;\\[/color]`} + {`[color=#0a3069] gap: 8px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-angle-demo shimmer {\\[/color]`} + {`[color=#0a3069] width: 220px;\\[/color]`} + {`[color=#0a3069] height: 36px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-angle-demo shimmer-content {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] padding: 4px 8px;\\[/color]`} + {`[color=#0a3069] font-size: 15px;\\[/color]`} + {`[color=#0a3069] color: #46545f;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]static[/color] [color=#953800]ui_widget_t[/color] *[color=#8250df]caption[/color][color=#0550ae]([color=#953800]const[/color] [color=#953800]char[/color] *label)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *shimmer, *content, *text;`} + {` `} + {`        shimmer = ui_create_shimmer();`} + {`        content = ui_create_widget([color=#0a3069]"shimmer-content"[/color]);`} + {`        text = ui_create_widget([color=#0a3069]"text"[/color]);`} + {`        ui_text_set_content(text, label);`} + {`        ui_widget_append(content, text);`} + {`        ui_shimmer_set_content(shimmer, content);`} + {`        ui_shimmer_set_running(shimmer, [color=#0550ae]true[/color]);`} + {`        [color=#cf222e]return[/color] shimmer;`} + {`}`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]shimmer_angle_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *box, *flat, *tilted;`} + {` `} + {`        ui_load_css_string(shimmer_angle_css, [color=#0a3069]"shimmer-angle-demo"[/color]);`} + {`        box = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(box, [color=#0a3069]"shimmer-angle-demo"[/color]);`} + {` `} + {`        flat = caption([color=#0a3069]"Flat band"[/color]);`} + {`        ui_shimmer_set_angle(flat, [color=#0550ae]0.0f[/color]);`} + {` `} + {`        tilted = caption([color=#0a3069]"Tilted band"[/color]);`} + {`        ui_shimmer_set_angle(tilted, [color=#0550ae]45.0f[/color]);`} + {` `} + {`        ui_widget_append(box, flat);`} + {`        ui_widget_append(box, tilted);`} + {`        ui_widget_append(parent, box);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .shimmer-angle-demo {`} + {`        display: flex;`} + {`        flex-direction: column;`} + {`        gap: 8px;`} + {`    }`} + {`    .shimmer-angle-demo shimmer {`} + {`        width: 220px;`} + {`        height: 36px;`} + {`    }`} + {`    .shimmer-angle-demo shimmer-content {`} + {`        display: flex;`} + {`        align-items: center;`} + {`        padding: 4px 8px;`} + {`        font-size: 15px;`} + {`        color: #46545f;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"shimmer-angle-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]running[/color]=[color=#0a3069]"true"[/color] [color=#0550ae]angle[/color]=[color=#0a3069]"0"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Flat band[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]running[/color]=[color=#0a3069]"true"[/color] [color=#0550ae]angle[/color]=[color=#0a3069]"45"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Tilted band[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/shimmer-basic/index.c b/examples/doc-viewer/app/examples/shimmer-basic/index.c new file mode 100644 index 00000000..cc7381f4 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-basic/index.c @@ -0,0 +1,41 @@ +#include "index.tsx.h" +#include "index.h" + +#include "../../../../../docs/examples/shimmer-basic/main.c" + +typedef struct { + shimmer_basic_demo_react_t base; +} shimmer_basic_demo_t; + +void shimmer_basic_demo_update(ui_widget_t *w) +{ + shimmer_basic_demo_react_update(w); +} + +static void shimmer_basic_demo_init(ui_widget_t *w) +{ + shimmer_basic_demo_t *_that = ui_widget_add_data( + w, shimmer_basic_demo_proto, sizeof(shimmer_basic_demo_t)); + shimmer_basic_demo_proto->proto->init(w); + shimmer_basic_demo_react_init(w); + shimmer_basic_init(_that->base.refs.preview); + shimmer_basic_demo_update(w); +} + +static void shimmer_basic_demo_destroy(ui_widget_t *w) +{ + shimmer_basic_demo_proto->proto->destroy(w); + shimmer_basic_demo_react_destroy(w); +} + +ui_widget_t *ui_create_shimmer_basic_demo(void) +{ + return ui_create_widget_with_prototype(shimmer_basic_demo_proto); +} + +void ui_register_shimmer_basic_demo(void) +{ + shimmer_basic_demo_init_prototype(); + shimmer_basic_demo_proto->init = shimmer_basic_demo_init; + shimmer_basic_demo_proto->destroy = shimmer_basic_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/shimmer-basic/index.tsx b/examples/doc-viewer/app/examples/shimmer-basic/index.tsx new file mode 100644 index 00000000..e0f4b828 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-basic/index.tsx @@ -0,0 +1,134 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function ShimmerBasicDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Shimmer[/color], [color=#8250df]ShimmerContent[/color], [color=#8250df]Text[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]ShimmerDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]Text[/color]>[/color]Generating response…[color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_shimmer_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* The shimmer sweeps a soft sheen across real text, so the caption stays[/color]`} + {`[color=#6e7781] * readable while it reads as "in progress". Scoped under a unique class; an[/color]`} + {`[color=#6e7781] * inline background-color would be overridden by the theme \`*\` rule. */[/color]`} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *shimmer_basic_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].shimmer-basic-demo shimmer {\\[/color]`} + {`[color=#0a3069] width: 220px;\\[/color]`} + {`[color=#0a3069] height: 28px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-basic-demo shimmer-content {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] padding: 4px 8px;\\[/color]`} + {`[color=#0a3069] font-size: 15px;\\[/color]`} + {`[color=#0a3069] color: #46545f;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]shimmer_basic_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *box, *shimmer, *content, *text;`} + {` `} + {`        ui_load_css_string(shimmer_basic_css, [color=#0a3069]"shimmer-basic-demo"[/color]);`} + {`        box = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(box, [color=#0a3069]"shimmer-basic-demo"[/color]);`} + {` `} + {`        shimmer = ui_create_shimmer();`} + {`        content = ui_create_widget([color=#0a3069]"shimmer-content"[/color]);`} + {`        text = ui_create_widget([color=#0a3069]"text"[/color]);`} + {`        ui_text_set_content(text, [color=#0a3069]"Generating response..."[/color]);`} + {`        ui_widget_append(content, text);`} + {`        ui_shimmer_set_content(shimmer, content);`} + {` `} + {`        ui_widget_append(box, shimmer);`} + {`        ui_widget_append(parent, box);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .shimmer-basic-demo shimmer {`} + {`        width: 220px;`} + {`        height: 28px;`} + {`    }`} + {`    .shimmer-basic-demo shimmer-content {`} + {`        display: flex;`} + {`        align-items: center;`} + {`        padding: 4px 8px;`} + {`        font-size: 15px;`} + {`        color: #46545f;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"shimmer-basic-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Generating response…[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/shimmer-color/index.c b/examples/doc-viewer/app/examples/shimmer-color/index.c new file mode 100644 index 00000000..8e510527 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-color/index.c @@ -0,0 +1,43 @@ +#include "index.tsx.h" +#include "index.h" + +#define caption shimmer_color__caption +#include "../../../../../docs/examples/shimmer-color/main.c" +#undef caption + +typedef struct { + shimmer_color_demo_react_t base; +} shimmer_color_demo_t; + +void shimmer_color_demo_update(ui_widget_t *w) +{ + shimmer_color_demo_react_update(w); +} + +static void shimmer_color_demo_init(ui_widget_t *w) +{ + shimmer_color_demo_t *_that = ui_widget_add_data( + w, shimmer_color_demo_proto, sizeof(shimmer_color_demo_t)); + shimmer_color_demo_proto->proto->init(w); + shimmer_color_demo_react_init(w); + shimmer_color_init(_that->base.refs.preview); + shimmer_color_demo_update(w); +} + +static void shimmer_color_demo_destroy(ui_widget_t *w) +{ + shimmer_color_demo_proto->proto->destroy(w); + shimmer_color_demo_react_destroy(w); +} + +ui_widget_t *ui_create_shimmer_color_demo(void) +{ + return ui_create_widget_with_prototype(shimmer_color_demo_proto); +} + +void ui_register_shimmer_color_demo(void) +{ + shimmer_color_demo_init_prototype(); + shimmer_color_demo_proto->init = shimmer_color_demo_init; + shimmer_color_demo_proto->destroy = shimmer_color_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/shimmer-color/index.tsx b/examples/doc-viewer/app/examples/shimmer-color/index.tsx new file mode 100644 index 00000000..740dd383 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-color/index.tsx @@ -0,0 +1,169 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function ShimmerColorDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Shimmer[/color], [color=#8250df]ShimmerContent[/color], [color=#8250df]Text[/color], [color=#8250df]Widget[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]ShimmerColorDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]Widget[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex flex-col gap-2"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color] [color=#0550ae]color[/color]=[color=#0a3069]"#378ADD"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Generating response…[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color] [color=#0550ae]color[/color]=[color=#0a3069]"rgb(34, 197, 94)"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Sending request…[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_shimmer_color_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* Two captions, each with a different highlight colour. The \`color\` sheen is[/color]`} + {`[color=#6e7781] * overlaid only on the text's opaque pixels, so a blue and a green highlight[/color]`} + {`[color=#6e7781] * tint the same grey ink toward their own hue as they sweep. */[/color]`} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *shimmer_color_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].shimmer-color-demo {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] flex-direction: column;\\[/color]`} + {`[color=#0a3069] gap: 8px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-color-demo shimmer {\\[/color]`} + {`[color=#0a3069] width: 220px;\\[/color]`} + {`[color=#0a3069] height: 28px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-color-demo shimmer-content {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] padding: 4px 8px;\\[/color]`} + {`[color=#0a3069] font-size: 15px;\\[/color]`} + {`[color=#0a3069] color: #46545f;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]static[/color] [color=#953800]ui_widget_t[/color] *[color=#8250df]caption[/color][color=#0550ae]([color=#953800]const[/color] [color=#953800]char[/color] *label)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *shimmer, *content, *text;`} + {` `} + {`        shimmer = ui_create_shimmer();`} + {`        content = ui_create_widget([color=#0a3069]"shimmer-content"[/color]);`} + {`        text = ui_create_widget([color=#0a3069]"text"[/color]);`} + {`        ui_text_set_content(text, label);`} + {`        ui_widget_append(content, text);`} + {`        ui_shimmer_set_content(shimmer, content);`} + {`        [color=#cf222e]return[/color] shimmer;`} + {`}`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]shimmer_color_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *box, *blue, *green;`} + {` `} + {`        ui_load_css_string(shimmer_color_css, [color=#0a3069]"shimmer-color-demo"[/color]);`} + {`        box = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(box, [color=#0a3069]"shimmer-color-demo"[/color]);`} + {` `} + {`        blue = caption([color=#0a3069]"Generating response..."[/color]);`} + {`        ui_shimmer_set_color(blue, pd_argb([color=#0550ae]200[/color], [color=#0550ae]55[/color], [color=#0550ae]138[/color], [color=#0550ae]221[/color]));`} + {` `} + {`        green = caption([color=#0a3069]"Sending request..."[/color]);`} + {`        ui_shimmer_set_color(green, pd_argb([color=#0550ae]200[/color], [color=#0550ae]34[/color], [color=#0550ae]197[/color], [color=#0550ae]94[/color]));`} + {` `} + {`        ui_widget_append(box, blue);`} + {`        ui_widget_append(box, green);`} + {`        ui_widget_append(parent, box);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .shimmer-color-demo {`} + {`        display: flex;`} + {`        flex-direction: column;`} + {`        gap: 8px;`} + {`    }`} + {`    .shimmer-color-demo shimmer {`} + {`        width: 220px;`} + {`        height: 28px;`} + {`    }`} + {`    .shimmer-color-demo shimmer-content {`} + {`        display: flex;`} + {`        align-items: center;`} + {`        padding: 4px 8px;`} + {`        font-size: 15px;`} + {`        color: #46545f;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"shimmer-color-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]color[/color]=[color=#0a3069]"#378ADD"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Generating response…[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]color[/color]=[color=#0a3069]"rgb(34 197 94)"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Sending request…[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/shimmer-duration/index.c b/examples/doc-viewer/app/examples/shimmer-duration/index.c new file mode 100644 index 00000000..a1fe3566 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-duration/index.c @@ -0,0 +1,43 @@ +#include "index.tsx.h" +#include "index.h" + +#define caption shimmer_duration__caption +#include "../../../../../docs/examples/shimmer-duration/main.c" +#undef caption + +typedef struct { + shimmer_duration_demo_react_t base; +} shimmer_duration_demo_t; + +void shimmer_duration_demo_update(ui_widget_t *w) +{ + shimmer_duration_demo_react_update(w); +} + +static void shimmer_duration_demo_init(ui_widget_t *w) +{ + shimmer_duration_demo_t *_that = ui_widget_add_data( + w, shimmer_duration_demo_proto, sizeof(shimmer_duration_demo_t)); + shimmer_duration_demo_proto->proto->init(w); + shimmer_duration_demo_react_init(w); + shimmer_duration_init(_that->base.refs.preview); + shimmer_duration_demo_update(w); +} + +static void shimmer_duration_demo_destroy(ui_widget_t *w) +{ + shimmer_duration_demo_proto->proto->destroy(w); + shimmer_duration_demo_react_destroy(w); +} + +ui_widget_t *ui_create_shimmer_duration_demo(void) +{ + return ui_create_widget_with_prototype(shimmer_duration_demo_proto); +} + +void ui_register_shimmer_duration_demo(void) +{ + shimmer_duration_demo_init_prototype(); + shimmer_duration_demo_proto->init = shimmer_duration_demo_init; + shimmer_duration_demo_proto->destroy = shimmer_duration_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/shimmer-duration/index.tsx b/examples/doc-viewer/app/examples/shimmer-duration/index.tsx new file mode 100644 index 00000000..4118a886 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-duration/index.tsx @@ -0,0 +1,168 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function ShimmerDurationDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Shimmer[/color], [color=#8250df]ShimmerContent[/color], [color=#8250df]Text[/color], [color=#8250df]Widget[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]ShimmerDurationDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]Widget[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex flex-col gap-2"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color] [color=#0550ae]running[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Default duration[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color] [color=#0550ae]running[/color] [color=#0550ae]duration[/color]=[color=#0a3069]{1000}[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Faster duration[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_shimmer_duration_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* Two identical sweeps at different durations: the default 2000 ms and a[/color]`} + {`[color=#6e7781] * snappier 1000 ms. Duration is only observable while the shimmer is running,[/color]`} + {`[color=#6e7781] * so both lines keep the animation on. */[/color]`} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *shimmer_duration_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].shimmer-duration-demo {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] flex-direction: column;\\[/color]`} + {`[color=#0a3069] gap: 8px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-duration-demo shimmer {\\[/color]`} + {`[color=#0a3069] width: 220px;\\[/color]`} + {`[color=#0a3069] height: 28px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-duration-demo shimmer-content {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] padding: 4px 8px;\\[/color]`} + {`[color=#0a3069] font-size: 15px;\\[/color]`} + {`[color=#0a3069] color: #46545f;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]static[/color] [color=#953800]ui_widget_t[/color] *[color=#8250df]caption[/color][color=#0550ae]([color=#953800]const[/color] [color=#953800]char[/color] *label)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *shimmer, *content, *text;`} + {` `} + {`        shimmer = ui_create_shimmer();`} + {`        content = ui_create_widget([color=#0a3069]"shimmer-content"[/color]);`} + {`        text = ui_create_widget([color=#0a3069]"text"[/color]);`} + {`        ui_text_set_content(text, label);`} + {`        ui_widget_append(content, text);`} + {`        ui_shimmer_set_content(shimmer, content);`} + {`        ui_shimmer_set_running(shimmer, [color=#0550ae]true[/color]);`} + {`        [color=#cf222e]return[/color] shimmer;`} + {`}`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]shimmer_duration_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *box, *normal, *fast;`} + {` `} + {`        ui_load_css_string(shimmer_duration_css, [color=#0a3069]"shimmer-duration-demo"[/color]);`} + {`        box = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(box, [color=#0a3069]"shimmer-duration-demo"[/color]);`} + {` `} + {`        normal = caption([color=#0a3069]"Default duration"[/color]);`} + {`        fast = caption([color=#0a3069]"Faster duration"[/color]);`} + {`        ui_shimmer_set_duration(fast, [color=#0550ae]1000[/color]);`} + {` `} + {`        ui_widget_append(box, normal);`} + {`        ui_widget_append(box, fast);`} + {`        ui_widget_append(parent, box);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .shimmer-duration-demo {`} + {`        display: flex;`} + {`        flex-direction: column;`} + {`        gap: 8px;`} + {`    }`} + {`    .shimmer-duration-demo shimmer {`} + {`        width: 220px;`} + {`        height: 28px;`} + {`    }`} + {`    .shimmer-duration-demo shimmer-content {`} + {`        display: flex;`} + {`        align-items: center;`} + {`        padding: 4px 8px;`} + {`        font-size: 15px;`} + {`        color: #46545f;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"shimmer-duration-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]running[/color]=[color=#0a3069]"true"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Default duration[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]running[/color]=[color=#0a3069]"true"[/color] [color=#0550ae]duration[/color]=[color=#0a3069]"1000"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Faster duration[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/shimmer-running/index.c b/examples/doc-viewer/app/examples/shimmer-running/index.c new file mode 100644 index 00000000..6bbbf8c7 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-running/index.c @@ -0,0 +1,41 @@ +#include "index.tsx.h" +#include "index.h" + +#include "../../../../../docs/examples/shimmer-running/main.c" + +typedef struct { + shimmer_running_demo_react_t base; +} shimmer_running_demo_t; + +void shimmer_running_demo_update(ui_widget_t *w) +{ + shimmer_running_demo_react_update(w); +} + +static void shimmer_running_demo_init(ui_widget_t *w) +{ + shimmer_running_demo_t *_that = ui_widget_add_data( + w, shimmer_running_demo_proto, sizeof(shimmer_running_demo_t)); + shimmer_running_demo_proto->proto->init(w); + shimmer_running_demo_react_init(w); + shimmer_running_init(_that->base.refs.preview); + shimmer_running_demo_update(w); +} + +static void shimmer_running_demo_destroy(ui_widget_t *w) +{ + shimmer_running_demo_proto->proto->destroy(w); + shimmer_running_demo_react_destroy(w); +} + +ui_widget_t *ui_create_shimmer_running_demo(void) +{ + return ui_create_widget_with_prototype(shimmer_running_demo_proto); +} + +void ui_register_shimmer_running_demo(void) +{ + shimmer_running_demo_init_prototype(); + shimmer_running_demo_proto->init = shimmer_running_demo_init; + shimmer_running_demo_proto->destroy = shimmer_running_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/shimmer-running/index.tsx b/examples/doc-viewer/app/examples/shimmer-running/index.tsx new file mode 100644 index 00000000..100f0c12 --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-running/index.tsx @@ -0,0 +1,170 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function ShimmerRunningDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Shimmer[/color], [color=#8250df]ShimmerContent[/color], [color=#8250df]Text[/color], [color=#8250df]Widget[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]ShimmerRunningDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]Widget[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex flex-col gap-2"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color] [color=#0550ae]running[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Searching the web…[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color] [color=#0550ae]running[/color]=[color=#0a3069]{false}[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Reading 4 files[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_shimmer_running_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* The first caption keeps sweeping; the second has the animation turned off,[/color]`} + {`[color=#6e7781] * so its text is composited as-is with no sheen added. \`running = false\` is the[/color]`} + {`[color=#6e7781] * shimmer's off state: the content renders normally. */[/color]`} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *shimmer_running_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].shimmer-running-demo {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] flex-direction: column;\\[/color]`} + {`[color=#0a3069] gap: 8px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-running-demo shimmer {\\[/color]`} + {`[color=#0a3069] width: 220px;\\[/color]`} + {`[color=#0a3069] height: 28px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-running-demo shimmer-content {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] padding: 4px 8px;\\[/color]`} + {`[color=#0a3069] font-size: 15px;\\[/color]`} + {`[color=#0a3069] color: #46545f;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]static[/color] [color=#953800]ui_widget_t[/color] *[color=#8250df]caption[/color][color=#0550ae]([color=#953800]const[/color] [color=#953800]char[/color] *label)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *shimmer, *content, *text;`} + {` `} + {`        shimmer = ui_create_shimmer();`} + {`        content = ui_create_widget([color=#0a3069]"shimmer-content"[/color]);`} + {`        text = ui_create_widget([color=#0a3069]"text"[/color]);`} + {`        ui_text_set_content(text, label);`} + {`        ui_widget_append(content, text);`} + {`        ui_shimmer_set_content(shimmer, content);`} + {`        [color=#cf222e]return[/color] shimmer;`} + {`}`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]shimmer_running_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *box, *running, *stopped;`} + {` `} + {`        ui_load_css_string(shimmer_running_css, [color=#0a3069]"shimmer-running-demo"[/color]);`} + {`        box = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(box, [color=#0a3069]"shimmer-running-demo"[/color]);`} + {` `} + {`        running = caption([color=#0a3069]"Searching the web..."[/color]);`} + {`        ui_shimmer_set_running(running, [color=#0550ae]true[/color]);`} + {`        ui_shimmer_set_duration(running, [color=#0550ae]1000[/color]);`} + {` `} + {`        stopped = caption([color=#0a3069]"Reading 4 files"[/color]);`} + {`        ui_shimmer_set_running(stopped, [color=#0550ae]false[/color]);`} + {` `} + {`        ui_widget_append(box, running);`} + {`        ui_widget_append(box, stopped);`} + {`        ui_widget_append(parent, box);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .shimmer-running-demo {`} + {`        display: flex;`} + {`        flex-direction: column;`} + {`        gap: 8px;`} + {`    }`} + {`    .shimmer-running-demo shimmer {`} + {`        width: 220px;`} + {`        height: 28px;`} + {`    }`} + {`    .shimmer-running-demo shimmer-content {`} + {`        display: flex;`} + {`        align-items: center;`} + {`        padding: 4px 8px;`} + {`        font-size: 15px;`} + {`        color: #46545f;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"shimmer-running-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]running[/color]=[color=#0a3069]"true"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Searching the web…[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]running[/color]=[color=#0a3069]"false"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Reading 4 files[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/shimmer-spread/index.c b/examples/doc-viewer/app/examples/shimmer-spread/index.c new file mode 100644 index 00000000..f158978b --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-spread/index.c @@ -0,0 +1,43 @@ +#include "index.tsx.h" +#include "index.h" + +#define caption shimmer_spread__caption +#include "../../../../../docs/examples/shimmer-spread/main.c" +#undef caption + +typedef struct { + shimmer_spread_demo_react_t base; +} shimmer_spread_demo_t; + +void shimmer_spread_demo_update(ui_widget_t *w) +{ + shimmer_spread_demo_react_update(w); +} + +static void shimmer_spread_demo_init(ui_widget_t *w) +{ + shimmer_spread_demo_t *_that = ui_widget_add_data( + w, shimmer_spread_demo_proto, sizeof(shimmer_spread_demo_t)); + shimmer_spread_demo_proto->proto->init(w); + shimmer_spread_demo_react_init(w); + shimmer_spread_init(_that->base.refs.preview); + shimmer_spread_demo_update(w); +} + +static void shimmer_spread_demo_destroy(ui_widget_t *w) +{ + shimmer_spread_demo_proto->proto->destroy(w); + shimmer_spread_demo_react_destroy(w); +} + +ui_widget_t *ui_create_shimmer_spread_demo(void) +{ + return ui_create_widget_with_prototype(shimmer_spread_demo_proto); +} + +void ui_register_shimmer_spread_demo(void) +{ + shimmer_spread_demo_init_prototype(); + shimmer_spread_demo_proto->init = shimmer_spread_demo_init; + shimmer_spread_demo_proto->destroy = shimmer_spread_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/shimmer-spread/index.tsx b/examples/doc-viewer/app/examples/shimmer-spread/index.tsx new file mode 100644 index 00000000..09c30f7c --- /dev/null +++ b/examples/doc-viewer/app/examples/shimmer-spread/index.tsx @@ -0,0 +1,170 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function ShimmerSpreadDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Shimmer[/color], [color=#8250df]ShimmerContent[/color], [color=#8250df]Text[/color], [color=#8250df]Widget[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]ShimmerSpreadDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]Widget[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex flex-col gap-2"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color] [color=#0550ae]running[/color] [color=#0550ae]spread[/color]=[color=#0a3069]{40}[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Narrow spread[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]Shimmer[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[220px] h-7"[/color] [color=#0550ae]running[/color] [color=#0550ae]spread[/color]=[color=#0a3069]{160}[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]ShimmerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center px-2 py-1 text-\\[#46545f]"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color]>[/color]Wide spread[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_shimmer_spread_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* The spread sets the highlight band's width in logical pixels. A narrow band[/color]`} + {`[color=#6e7781] * reads as a tight glint; a wide one washes across the whole caption at once.[/color]`} + {`[color=#6e7781] * The peak strength is unchanged, only how far it reaches. */[/color]`} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *shimmer_spread_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].shimmer-spread-demo {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] flex-direction: column;\\[/color]`} + {`[color=#0a3069] gap: 8px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-spread-demo shimmer {\\[/color]`} + {`[color=#0a3069] width: 220px;\\[/color]`} + {`[color=#0a3069] height: 28px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].shimmer-spread-demo shimmer-content {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] padding: 4px 8px;\\[/color]`} + {`[color=#0a3069] font-size: 15px;\\[/color]`} + {`[color=#0a3069] color: #46545f;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]static[/color] [color=#953800]ui_widget_t[/color] *[color=#8250df]caption[/color][color=#0550ae]([color=#953800]const[/color] [color=#953800]char[/color] *label)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *shimmer, *content, *text;`} + {` `} + {`        shimmer = ui_create_shimmer();`} + {`        content = ui_create_widget([color=#0a3069]"shimmer-content"[/color]);`} + {`        text = ui_create_widget([color=#0a3069]"text"[/color]);`} + {`        ui_text_set_content(text, label);`} + {`        ui_widget_append(content, text);`} + {`        ui_shimmer_set_content(shimmer, content);`} + {`        ui_shimmer_set_running(shimmer, [color=#0550ae]true[/color]);`} + {`        [color=#cf222e]return[/color] shimmer;`} + {`}`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]shimmer_spread_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *box, *narrow, *wide;`} + {` `} + {`        ui_load_css_string(shimmer_spread_css, [color=#0a3069]"shimmer-spread-demo"[/color]);`} + {`        box = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(box, [color=#0a3069]"shimmer-spread-demo"[/color]);`} + {` `} + {`        narrow = caption([color=#0a3069]"Narrow spread"[/color]);`} + {`        ui_shimmer_set_spread(narrow, [color=#0550ae]40.0f[/color]);`} + {` `} + {`        wide = caption([color=#0a3069]"Wide spread"[/color]);`} + {`        ui_shimmer_set_spread(wide, [color=#0550ae]160.0f[/color]);`} + {` `} + {`        ui_widget_append(box, narrow);`} + {`        ui_widget_append(box, wide);`} + {`        ui_widget_append(parent, box);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .shimmer-spread-demo {`} + {`        display: flex;`} + {`        flex-direction: column;`} + {`        gap: 8px;`} + {`    }`} + {`    .shimmer-spread-demo shimmer {`} + {`        width: 220px;`} + {`        height: 28px;`} + {`    }`} + {`    .shimmer-spread-demo shimmer-content {`} + {`        display: flex;`} + {`        align-items: center;`} + {`        padding: 4px 8px;`} + {`        font-size: 15px;`} + {`        color: #46545f;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"shimmer-spread-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]running[/color]=[color=#0a3069]"true"[/color] [color=#0550ae]spread[/color]=[color=#0a3069]"40"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Narrow spread[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]shimmer[/color] [color=#0550ae]running[/color]=[color=#0a3069]"true"[/color] [color=#0550ae]spread[/color]=[color=#0a3069]"160"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]shimmer-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color]>[/color]Wide spread[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/spinner-basic/index.c b/examples/doc-viewer/app/examples/spinner-basic/index.c new file mode 100644 index 00000000..b0f5e3a5 --- /dev/null +++ b/examples/doc-viewer/app/examples/spinner-basic/index.c @@ -0,0 +1,41 @@ +#include "index.tsx.h" +#include "index.h" + +#include "../../../../../docs/examples/spinner-basic/main.c" + +typedef struct { + spinner_basic_demo_react_t base; +} spinner_basic_demo_t; + +void spinner_basic_demo_update(ui_widget_t *w) +{ + spinner_basic_demo_react_update(w); +} + +static void spinner_basic_demo_init(ui_widget_t *w) +{ + spinner_basic_demo_t *_that = ui_widget_add_data( + w, spinner_basic_demo_proto, sizeof(spinner_basic_demo_t)); + spinner_basic_demo_proto->proto->init(w); + spinner_basic_demo_react_init(w); + spinner_basic_init(_that->base.refs.preview); + spinner_basic_demo_update(w); +} + +static void spinner_basic_demo_destroy(ui_widget_t *w) +{ + spinner_basic_demo_proto->proto->destroy(w); + spinner_basic_demo_react_destroy(w); +} + +ui_widget_t *ui_create_spinner_basic_demo(void) +{ + return ui_create_widget_with_prototype(spinner_basic_demo_proto); +} + +void ui_register_spinner_basic_demo(void) +{ + spinner_basic_demo_init_prototype(); + spinner_basic_demo_proto->init = spinner_basic_demo_init; + spinner_basic_demo_proto->destroy = spinner_basic_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/spinner-basic/index.tsx b/examples/doc-viewer/app/examples/spinner-basic/index.tsx new file mode 100644 index 00000000..00a4e755 --- /dev/null +++ b/examples/doc-viewer/app/examples/spinner-basic/index.tsx @@ -0,0 +1,90 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function SpinnerBasicDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Spinner[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]SpinnerDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] [color=#116329]<[color=#0550ae]Spinner[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-10 h-10"[/color] />[/color];`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_spinner_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]spinner_basic_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *spinner;`} + {` `} + {`        spinner = ui_create_spinner();`} + {`        ui_widget_resize(spinner, [color=#0550ae]40[/color], [color=#0550ae]40[/color]);`} + {`        ui_widget_append(parent, spinner);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .spinner-basic-box spinner {`} + {`        width: 40px;`} + {`        height: 40px;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]w[/color] [color=#0550ae]class[/color]=[color=#0a3069]"spinner-basic-box"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]spinner[/color] />[/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/spinner-custom-content/index.c b/examples/doc-viewer/app/examples/spinner-custom-content/index.c new file mode 100644 index 00000000..da132ba8 --- /dev/null +++ b/examples/doc-viewer/app/examples/spinner-custom-content/index.c @@ -0,0 +1,47 @@ +#include "index.tsx.h" +#include "index.h" + +#define create_ring_content spinner_custom_content__create_ring_content +#include "../../../../../docs/examples/spinner-custom-content/main.c" +#undef create_ring_content + +typedef struct { + spinner_custom_content_demo_react_t base; +} spinner_custom_content_demo_t; + +void spinner_custom_content_demo_update(ui_widget_t *w) +{ + spinner_custom_content_demo_react_update(w); +} + +static void spinner_custom_content_demo_init(ui_widget_t *w) +{ + spinner_custom_content_demo_t *_that = + ui_widget_add_data(w, spinner_custom_content_demo_proto, + sizeof(spinner_custom_content_demo_t)); + spinner_custom_content_demo_proto->proto->init(w); + spinner_custom_content_demo_react_init(w); + spinner_custom_content_init(_that->base.refs.preview); + spinner_custom_content_demo_update(w); +} + +static void spinner_custom_content_demo_destroy(ui_widget_t *w) +{ + spinner_custom_content_demo_proto->proto->destroy(w); + spinner_custom_content_demo_react_destroy(w); +} + +ui_widget_t *ui_create_spinner_custom_content_demo(void) +{ + return ui_create_widget_with_prototype( + spinner_custom_content_demo_proto); +} + +void ui_register_spinner_custom_content_demo(void) +{ + spinner_custom_content_demo_init_prototype(); + spinner_custom_content_demo_proto->init = + spinner_custom_content_demo_init; + spinner_custom_content_demo_proto->destroy = + spinner_custom_content_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/spinner-custom-content/index.tsx b/examples/doc-viewer/app/examples/spinner-custom-content/index.tsx new file mode 100644 index 00000000..18577916 --- /dev/null +++ b/examples/doc-viewer/app/examples/spinner-custom-content/index.tsx @@ -0,0 +1,228 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function SpinnerCustomContentDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Spinner[/color], [color=#8250df]SpinnerContent[/color], [color=#8250df]Text[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]SpinnerCustomContentDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]div[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex flex-row items-center gap-7"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]Spinner[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-10 h-10"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]SpinnerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"relative flex items-center justify-center"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]div[/color] [color=#0550ae]className[/color]=[color=#0a3069]"absolute left-\\[15px] top-\\[3px] w-\\[10px] h-\\[10px] rounded-full bg-blue-500"[/color] />[/color]`} + {`          [color=#116329]<[color=#0550ae]div[/color] [color=#0550ae]className[/color]=[color=#0a3069]"absolute left-\\[18px] top-\\[6px] w-1 h-5 rounded bg-red-500"[/color] />[/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]Spinner[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-\\[96px] h-8"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]SpinnerContent[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center justify-center"[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]Text[/color] [color=#0550ae]className[/color]=[color=#0a3069]"whitespace-nowrap text-lg text-\\[#e5484d]"[/color]>[/color]`} + {`            Hello`} + {`          [color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_spinner_custom_content_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *spinner_custom_content_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].spinner-custom-content-demo {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] gap: 28px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].spinner-custom-content-demo spinner-content {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] justify-content: center;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].spinner-custom-content-demo spinner-content .c-dot {\\[/color]`} + {`[color=#0a3069] position: absolute;\\[/color]`} + {`[color=#0a3069] left: 15px;\\[/color]`} + {`[color=#0a3069] top: 3px;\\[/color]`} + {`[color=#0a3069] width: 10px;\\[/color]`} + {`[color=#0a3069] height: 10px;\\[/color]`} + {`[color=#0a3069] background-color: #4a90d9;\\[/color]`} + {`[color=#0a3069] border-radius: 8px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].spinner-custom-content-demo spinner-content .c-bar {\\[/color]`} + {`[color=#0a3069] position: absolute;\\[/color]`} + {`[color=#0a3069] left: 18px;\\[/color]`} + {`[color=#0a3069] top: 6px;\\[/color]`} + {`[color=#0a3069] width: 4px;\\[/color]`} + {`[color=#0a3069] height: 20px;\\[/color]`} + {`[color=#0a3069] background-color: #d9534f;\\[/color]`} + {`[color=#0a3069] border-radius: 2px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].spinner-custom-content-demo spinner-content .c-text {\\[/color]`} + {`[color=#0a3069] white-space: nowrap;\\[/color]`} + {`[color=#0a3069] font-size: 18px;\\[/color]`} + {`[color=#0a3069] color: #e5484d;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]static[/color] [color=#953800]ui_widget_t[/color] *[color=#8250df]create_shape_content[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *content, *dot, *bar;`} + {` `} + {`        content = ui_create_widget([color=#0a3069]"spinner-content"[/color]);`} + {`        dot = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        bar = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(dot, [color=#0a3069]"c-dot"[/color]);`} + {`        ui_widget_add_class(bar, [color=#0a3069]"c-bar"[/color]);`} + {`        ui_widget_append(content, dot);`} + {`        ui_widget_append(content, bar);`} + {`        [color=#cf222e]return[/color] content;`} + {`}`} + {` `} + {`[color=#953800]static[/color] [color=#953800]ui_widget_t[/color] *[color=#8250df]create_text_content[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *content, *text;`} + {` `} + {`        content = ui_create_widget([color=#0a3069]"spinner-content"[/color]);`} + {`        text = ui_create_widget([color=#0a3069]"text"[/color]);`} + {`        ui_text_set_content(text, [color=#0a3069]"Hello"[/color]);`} + {`        ui_widget_add_class(text, [color=#0a3069]"c-text"[/color]);`} + {`        ui_widget_append(content, text);`} + {`        [color=#cf222e]return[/color] content;`} + {`}`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]spinner_custom_content_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *row, *spinner;`} + {` `} + {`        ui_load_css_string(spinner_custom_content_css,`} + {`                           [color=#0a3069]"spinner-custom-content-demo"[/color]);`} + {`        row = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(row, [color=#0a3069]"spinner-custom-content-demo"[/color]);`} + {` `} + {`        spinner = ui_create_spinner();`} + {`        ui_widget_resize(spinner, [color=#0550ae]40[/color], [color=#0550ae]40[/color]);`} + {`        ui_spinner_set_content(spinner, create_shape_content());`} + {`        ui_widget_append(row, spinner);`} + {` `} + {`        spinner = ui_create_spinner();`} + {`        ui_widget_resize(spinner, [color=#0550ae]96[/color], [color=#0550ae]32[/color]);`} + {`        ui_spinner_set_content(spinner, create_text_content());`} + {`        ui_widget_append(row, spinner);`} + {` `} + {`        ui_widget_append(parent, row);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .spinner-custom-content-demo {`} + {`        display: flex;`} + {`        align-items: center;`} + {`        gap: 28px;`} + {`    }`} + {`    .spinner-custom-content-demo .shape {`} + {`        width: 40px;`} + {`        height: 40px;`} + {`    }`} + {`    .spinner-custom-content-demo .word {`} + {`        width: 96px;`} + {`        height: 32px;`} + {`    }`} + {`    .spinner-custom-content-demo spinner-content {`} + {`        display: flex;`} + {`        align-items: center;`} + {`        justify-content: center;`} + {`    }`} + {`    .spinner-custom-content-demo .c-dot {`} + {`        position: absolute;`} + {`        left: 15px;`} + {`        top: 3px;`} + {`        width: 10px;`} + {`        height: 10px;`} + {`        background-color: #4a90d9;`} + {`        border-radius: 8px;`} + {`    }`} + {`    .spinner-custom-content-demo .c-bar {`} + {`        position: absolute;`} + {`        left: 18px;`} + {`        top: 6px;`} + {`        width: 4px;`} + {`        height: 20px;`} + {`        background-color: #d9534f;`} + {`        border-radius: 2px;`} + {`    }`} + {`    .spinner-custom-content-demo .c-text {`} + {`        white-space: nowrap;`} + {`        font-size: 18px;`} + {`        color: #e5484d;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"spinner-custom-content-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]spinner[/color] [color=#0550ae]class[/color]=[color=#0a3069]"shape"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]spinner-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]w[/color] [color=#0550ae]class[/color]=[color=#0a3069]"c-dot"[/color] />[/color]`} + {`          [color=#116329]<[color=#0550ae]w[/color] [color=#0550ae]class[/color]=[color=#0a3069]"c-bar"[/color] />[/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`      [color=#116329]<[color=#0550ae]spinner[/color] [color=#0550ae]class[/color]=[color=#0a3069]"word"[/color]>[/color]`} + {`        [color=#116329]<[color=#0550ae]spinner-content[/color]>[/color]`} + {`          [color=#116329]<[color=#0550ae]text[/color] [color=#0550ae]class[/color]=[color=#0a3069]"c-text"[/color]>[/color]Hello[color=#116329][/color]`} + {`        [color=#116329][/color]`} + {`      [color=#116329][/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/spinner-running/index.c b/examples/doc-viewer/app/examples/spinner-running/index.c new file mode 100644 index 00000000..7e4c106b --- /dev/null +++ b/examples/doc-viewer/app/examples/spinner-running/index.c @@ -0,0 +1,41 @@ +#include "index.tsx.h" +#include "index.h" + +#include "../../../../../docs/examples/spinner-running/main.c" + +typedef struct { + spinner_running_demo_react_t base; +} spinner_running_demo_t; + +void spinner_running_demo_update(ui_widget_t *w) +{ + spinner_running_demo_react_update(w); +} + +static void spinner_running_demo_init(ui_widget_t *w) +{ + spinner_running_demo_t *_that = ui_widget_add_data( + w, spinner_running_demo_proto, sizeof(spinner_running_demo_t)); + spinner_running_demo_proto->proto->init(w); + spinner_running_demo_react_init(w); + spinner_running_init(_that->base.refs.preview); + spinner_running_demo_update(w); +} + +static void spinner_running_demo_destroy(ui_widget_t *w) +{ + spinner_running_demo_proto->proto->destroy(w); + spinner_running_demo_react_destroy(w); +} + +ui_widget_t *ui_create_spinner_running_demo(void) +{ + return ui_create_widget_with_prototype(spinner_running_demo_proto); +} + +void ui_register_spinner_running_demo(void) +{ + spinner_running_demo_init_prototype(); + spinner_running_demo_proto->init = spinner_running_demo_init; + spinner_running_demo_proto->destroy = spinner_running_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/spinner-running/index.tsx b/examples/doc-viewer/app/examples/spinner-running/index.tsx new file mode 100644 index 00000000..3db14355 --- /dev/null +++ b/examples/doc-viewer/app/examples/spinner-running/index.tsx @@ -0,0 +1,125 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function SpinnerRunningDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Spinner[/color], [color=#8250df]Widget[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]SpinnerRunningDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]Widget[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center gap-6"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]Spinner[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-10 h-10"[/color] [color=#0550ae]running[/color] [color=#0550ae]speed[/color]=[color=#0a3069]{0.5}[/color] />[/color]`} + {`      [color=#116329]<[color=#0550ae]Spinner[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-10 h-10"[/color] [color=#0550ae]running[/color]=[color=#0a3069]{false}[/color] [color=#0550ae]angle[/color]=[color=#0a3069]{90}[/color] />[/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_spinner_running_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *spinner_running_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].spinner-running-demo {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] flex-direction: row;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] gap: 24px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]spinner_running_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *box, *running, *stopped;`} + {` `} + {`        ui_load_css_string(spinner_running_css, [color=#0a3069]"spinner-running-demo"[/color]);`} + {`        box = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(box, [color=#0a3069]"spinner-running-demo"[/color]);`} + {` `} + {`        running = ui_create_spinner();`} + {`        ui_widget_resize(running, [color=#0550ae]40[/color], [color=#0550ae]40[/color]);`} + {`        ui_spinner_set_running(running, [color=#0550ae]true[/color]);`} + {`        ui_spinner_set_speed(running, [color=#0550ae]0.5f[/color]);`} + {` `} + {`        stopped = ui_create_spinner();`} + {`        ui_widget_resize(stopped, [color=#0550ae]40[/color], [color=#0550ae]40[/color]);`} + {`        ui_spinner_set_running(stopped, [color=#0550ae]false[/color]);`} + {`        ui_spinner_set_angle(stopped, [color=#0550ae]90.0[/color]);`} + {` `} + {`        ui_widget_append(box, running);`} + {`        ui_widget_append(box, stopped);`} + {`        ui_widget_append(parent, box);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .spinner-running-demo {`} + {`        display: flex;`} + {`        flex-direction: row;`} + {`        align-items: center;`} + {`        gap: 24px;`} + {`    }`} + {`    .spinner-running-demo spinner {`} + {`        width: 40px;`} + {`        height: 40px;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"spinner-running-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]spinner[/color] [color=#0550ae]running[/color]=[color=#0a3069]"true"[/color] [color=#0550ae]speed[/color]=[color=#0a3069]"0.5"[/color] />[/color]`} + {`      [color=#116329]<[color=#0550ae]spinner[/color] [color=#0550ae]running[/color]=[color=#0a3069]"false"[/color] [color=#0550ae]angle[/color]=[color=#0a3069]"90"[/color] />[/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/examples/spinner-size/index.c b/examples/doc-viewer/app/examples/spinner-size/index.c new file mode 100644 index 00000000..f9ff6377 --- /dev/null +++ b/examples/doc-viewer/app/examples/spinner-size/index.c @@ -0,0 +1,41 @@ +#include "index.tsx.h" +#include "index.h" + +#include "../../../../../docs/examples/spinner-size/main.c" + +typedef struct { + spinner_size_demo_react_t base; +} spinner_size_demo_t; + +void spinner_size_demo_update(ui_widget_t *w) +{ + spinner_size_demo_react_update(w); +} + +static void spinner_size_demo_init(ui_widget_t *w) +{ + spinner_size_demo_t *_that = ui_widget_add_data( + w, spinner_size_demo_proto, sizeof(spinner_size_demo_t)); + spinner_size_demo_proto->proto->init(w); + spinner_size_demo_react_init(w); + spinner_size_init(_that->base.refs.preview); + spinner_size_demo_update(w); +} + +static void spinner_size_demo_destroy(ui_widget_t *w) +{ + spinner_size_demo_proto->proto->destroy(w); + spinner_size_demo_react_destroy(w); +} + +ui_widget_t *ui_create_spinner_size_demo(void) +{ + return ui_create_widget_with_prototype(spinner_size_demo_proto); +} + +void ui_register_spinner_size_demo(void) +{ + spinner_size_demo_init_prototype(); + spinner_size_demo_proto->init = spinner_size_demo_init; + spinner_size_demo_proto->destroy = spinner_size_demo_destroy; +} diff --git a/examples/doc-viewer/app/examples/spinner-size/index.tsx b/examples/doc-viewer/app/examples/spinner-size/index.tsx new file mode 100644 index 00000000..927b0c20 --- /dev/null +++ b/examples/doc-viewer/app/examples/spinner-size/index.tsx @@ -0,0 +1,142 @@ +import { Text, Widget } from "@lcui/react"; +import DemoProvider from "../../components/demo-provider"; +import CodeBlockCopy from "../../components/code-block-copy"; + +export default function SpinnerSizeDemo() { + return ( + + + + + + example.tsx + main.c + main.c + ui.xml + main.c + + + TSX + C + XML + + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Spinner[/color], [color=#8250df]Widget[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color];`} + {` `} + {`[color=#cf222e]export[/color] [color=#cf222e]default[/color] [color=#cf222e]function[/color] [color=#8250df]SpinnerSizeDemo[/color]([color=#0550ae][/color]) {`} + {`  [color=#cf222e]return[/color] (`} + {`    [color=#116329]<[color=#0550ae]Widget[/color] [color=#0550ae]className[/color]=[color=#0a3069]"flex items-center gap-4"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]Spinner[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-6 h-6"[/color] />[/color]`} + {`      [color=#116329]<[color=#0550ae]Spinner[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-10 h-10"[/color] />[/color]`} + {`      [color=#116329]<[color=#0550ae]Spinner[/color] [color=#0550ae]className[/color]=[color=#0a3069]"w-16 h-16"[/color] />[/color]`} + {`    [color=#116329][/color]`} + {`  );`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#6e7781]/* example.tsx is compiled to example.h by @lcui/cli */[/color]`} + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069]"example.h"[/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_root_append(ui_create_spinner_size_demo());`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]static[/color] [color=#953800]const[/color] [color=#953800]char[/color] *spinner_size_css = [color=#0a3069]"\\[/color]`} + {`[color=#0a3069].spinner-size-demo {\\[/color]`} + {`[color=#0a3069] display: flex;\\[/color]`} + {`[color=#0a3069] flex-direction: row;\\[/color]`} + {`[color=#0a3069] align-items: center;\\[/color]`} + {`[color=#0a3069] gap: 16px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].spinner-size-demo .sz-sm {\\[/color]`} + {`[color=#0a3069] width: 24px;\\[/color]`} + {`[color=#0a3069] height: 24px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].spinner-size-demo .sz-md {\\[/color]`} + {`[color=#0a3069] width: 40px;\\[/color]`} + {`[color=#0a3069] height: 40px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069].spinner-size-demo .sz-lg {\\[/color]`} + {`[color=#0a3069] width: 64px;\\[/color]`} + {`[color=#0a3069] height: 64px;\\[/color]`} + {`[color=#0a3069]}\\[/color]`} + {`[color=#0a3069]"[/color];`} + {` `} + {`[color=#953800]void[/color] [color=#8250df]spinner_size_init[/color][color=#0550ae]([color=#953800]ui_widget_t[/color] *parent)[/color]`} + {`{`} + {`        [color=#953800]ui_widget_t[/color] *box;`} + {`        [color=#953800]ui_widget_t[/color] *spinner;`} + {`        [color=#953800]const[/color] [color=#953800]char[/color] *sizes\\[] = { [color=#0a3069]"sz-sm"[/color], [color=#0a3069]"sz-md"[/color], [color=#0a3069]"sz-lg"[/color] };`} + {`        [color=#953800]size_t[/color] i;`} + {` `} + {`        ui_load_css_string(spinner_size_css, [color=#0a3069]"spinner-size-demo"[/color]);`} + {`        box = ui_create_widget([color=#0550ae]NULL[/color]);`} + {`        ui_widget_add_class(box, [color=#0a3069]"spinner-size-demo"[/color]);`} + {`        [color=#cf222e]for[/color] (i = [color=#0550ae]0[/color]; i < [color=#0550ae]3[/color]; ++i) {`} + {`                spinner = ui_create_spinner();`} + {`                ui_widget_add_class(spinner, sizes\\[i]);`} + {`                ui_widget_append(box, spinner);`} + {`        }`} + {`        ui_widget_append(parent, box);`} + {`}`} + + + + {`[color=#6e7781][/color]`} + {`[color=#116329]<[color=#0550ae]lcui-app[/color]>[/color]`} + {`  [color=#116329]<[color=#0550ae]resource[/color] [color=#0550ae]type[/color]=[color=#0a3069]"text/css"[/color]>[/color]`} + {`    .spinner-size-demo {`} + {`        display: flex;`} + {`        flex-direction: row;`} + {`        align-items: center;`} + {`        gap: 16px;`} + {`    }`} + {`    .spinner-size-demo .sz-sm {`} + {`        width: 24px;`} + {`        height: 24px;`} + {`    }`} + {`    .spinner-size-demo .sz-md {`} + {`        width: 40px;`} + {`        height: 40px;`} + {`    }`} + {`    .spinner-size-demo .sz-lg {`} + {`        width: 64px;`} + {`        height: 64px;`} + {`    }`} + {`  [color=#116329][/color]`} + {`  [color=#116329]<[color=#0550ae]ui[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]widget[/color] [color=#0550ae]class[/color]=[color=#0a3069]"spinner-size-demo"[/color]>[/color]`} + {`      [color=#116329]<[color=#0550ae]spinner[/color] [color=#0550ae]class[/color]=[color=#0a3069]"sz-sm"[/color] />[/color]`} + {`      [color=#116329]<[color=#0550ae]spinner[/color] [color=#0550ae]class[/color]=[color=#0a3069]"sz-md"[/color] />[/color]`} + {`      [color=#116329]<[color=#0550ae]spinner[/color] [color=#0550ae]class[/color]=[color=#0a3069]"sz-lg"[/color] />[/color]`} + {`    [color=#116329][/color]`} + {`  [color=#116329][/color]`} + {`[color=#116329][/color]`} + + + + {`[color=#6e7781]#[color=#cf222e]include[/color] [color=#0a3069][/color][/color]`} + {` `} + {`[color=#953800]int[/color] [color=#8250df]main[/color][color=#0550ae]([color=#953800]void[/color])[/color]`} + {`{`} + {`        lcui_init();`} + {`        ui_load_xml_file([color=#0a3069]"ui.xml"[/color]);`} + {`        [color=#cf222e]return[/color] lcui_main();`} + {`}`} + + + + ); +} diff --git a/examples/doc-viewer/app/widget-fields/shimmer-content.ts b/examples/doc-viewer/app/widget-fields/shimmer-content.ts new file mode 100644 index 00000000..a1e3e369 --- /dev/null +++ b/examples/doc-viewer/app/widget-fields/shimmer-content.ts @@ -0,0 +1,5 @@ +import { widgetFields, FieldData } from "./widget"; + +// `shimmer-content` 继承公共字段;作为 Shimmer 的直属内容子树,其内部节点会被 +// 捕获为骨架遮罩,本身不暴露额外可配置属性。 +export const shimmerContentFields: FieldData[] = [...widgetFields]; diff --git a/examples/doc-viewer/app/widget-fields/shimmer.ts b/examples/doc-viewer/app/widget-fields/shimmer.ts new file mode 100644 index 00000000..c159fc5c --- /dev/null +++ b/examples/doc-viewer/app/widget-fields/shimmer.ts @@ -0,0 +1,59 @@ +import { widgetFields, FieldData } from "./widget"; + +export const shimmerFields: FieldData[] = [ + ...widgetFields, + { + name: "running", + type: "boolean", + default: "true", + description: { + en: "Whether the sheen animation is running. True by default; when off the content is composited with no highlight added.", + "zh-CN": "是否播放扫光动画。默认为 true;关闭时不再叠加高光,内容按原色显示。", + }, + }, + { + name: "color", + type: "string", + default: "rgba(255, 255, 255, 0.86)", + description: { + en: "Highlight colour overlaid on the content's opaque pixels, given as a CSS colour string; its alpha is the peak strength of the sheen.", + "zh-CN": "叠加到内容不透明像素上的高光颜色,采用 CSS 颜色字符串;其 alpha 即扫过处的峰值强度。", + }, + }, + { + name: "duration", + type: "number", + default: "2000", + description: { + en: "Time for one full sweep in milliseconds. Defaults to 2000.", + "zh-CN": "单次扫过的时长,单位为毫秒。默认为 2000。", + }, + }, + { + name: "spread", + type: "number", + default: "-", + description: { + en: "Width of the highlight band in logical pixels. When unset it defaults to about 45% of the box width and is recomputed on resize; setting it pins a fixed width.", + "zh-CN": "高光带宽度,单位为逻辑像素。未设置时默认为框宽的约 45% 并在尺寸变化时重新计算;显式设置后固定为该值。", + }, + }, + { + name: "angle", + type: "number", + default: "20", + description: { + en: "Tilt of the highlight band in degrees, shearing its center line across the height. Defaults to 20.", + "zh-CN": "高光带倾斜角,单位为度,使反光中心线沿高度方向错切。默认为 20。", + }, + }, + { + name: "phase", + type: "number", + default: "0", + description: { + en: "Sweep phase in [0, 1). Added for deterministic tests and manual control; while running the animation advances it by the duration.", + "zh-CN": "高光带相位,取值范围 [0, 1),用于确定性测试或手动控制;运行状态下由 duration 持续推进。", + }, + }, +]; diff --git a/examples/doc-viewer/app/widget-fields/spinner-content.ts b/examples/doc-viewer/app/widget-fields/spinner-content.ts new file mode 100644 index 00000000..c150af39 --- /dev/null +++ b/examples/doc-viewer/app/widget-fields/spinner-content.ts @@ -0,0 +1,3 @@ +import { widgetFields, FieldData } from "./widget"; + +export const spinnerContentFields: FieldData[] = [...widgetFields]; diff --git a/examples/doc-viewer/app/widget-fields/spinner.ts b/examples/doc-viewer/app/widget-fields/spinner.ts new file mode 100644 index 00000000..fcaa126f --- /dev/null +++ b/examples/doc-viewer/app/widget-fields/spinner.ts @@ -0,0 +1,32 @@ +import { widgetFields, FieldData } from "./widget"; + +export const spinnerFields: FieldData[] = [ + ...widgetFields, + { + name: "running", + type: "boolean", + default: "true", + description: { + en: "Whether the rotation animation is running. True by default.", + "zh-CN": "是否播放旋转动画。默认为 true。", + }, + }, + { + name: "speed", + type: "number", + default: "1", + description: { + en: "Rotation speed in revolutions per second.", + "zh-CN": "旋转速度,单位为每秒转数 (rev/s)。", + }, + }, + { + name: "angle", + type: "number", + default: "0", + description: { + en: "Current rotation angle in degrees. While running, the animation drives this value.", + "zh-CN": "当前旋转角度,单位为度。运行状态下由动画持续推进。", + }, + }, +]; diff --git a/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.c b/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.c new file mode 100644 index 00000000..2aca6fca --- /dev/null +++ b/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.c @@ -0,0 +1,57 @@ +#include "page.tsx.h" +#include "page.h" + +typedef struct { + zh_cn_widgets_shimmer_page_react_t base; + // Add additional states to your component here + // ... +} zh_cn_widgets_shimmer_page_t; + +static void zh_cn_widgets_shimmer_page_init(ui_widget_t *w) +{ + ui_widget_add_data(w, zh_cn_widgets_shimmer_page_proto, + sizeof(zh_cn_widgets_shimmer_page_t)); + + zh_cn_widgets_shimmer_page_react_init(w); + // Write the initialization code for your component here + // such as state initialization, event binding, etc + // ... + + zh_cn_widgets_shimmer_page_update(w); +} + +static zh_cn_widgets_shimmer_page_t *zh_cn_widgets_shimmer_page_get( + ui_widget_t *w) +{ + return ui_widget_get_data(w, zh_cn_widgets_shimmer_page_proto); +} + +static void zh_cn_widgets_shimmer_page_destroy(ui_widget_t *w) +{ + // Write code here to destroy the relevant resources of the component + // ... + + zh_cn_widgets_shimmer_page_react_destroy(w); +} + +void zh_cn_widgets_shimmer_page_update(ui_widget_t *w) +{ + zh_cn_widgets_shimmer_page_react_update(w); + // Write code here to update other content of your component + // ... +} + +ui_widget_t *ui_create_zh_cn_widgets_shimmer_page(void) +{ + return ui_create_widget_with_prototype( + zh_cn_widgets_shimmer_page_proto); +} + +void ui_register_zh_cn_widgets_shimmer_page(void) +{ + zh_cn_widgets_shimmer_page_init_prototype(); + zh_cn_widgets_shimmer_page_proto->init = + zh_cn_widgets_shimmer_page_init; + zh_cn_widgets_shimmer_page_proto->destroy = + zh_cn_widgets_shimmer_page_destroy; +} diff --git a/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.h b/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.h new file mode 100644 index 00000000..80d0c4a7 --- /dev/null +++ b/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.h @@ -0,0 +1,9 @@ +#include + +void ui_register_zh_cn_widgets_shimmer_page(void); + +ui_widget_t *ui_create_zh_cn_widgets_shimmer_page(void); + +void zh_cn_widgets_shimmer_page_update(ui_widget_t *w); + +void ui_load_zh_cn_widgets_shimmer_page_resources(void); diff --git a/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.tsx b/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.tsx new file mode 100644 index 00000000..7bf787d3 --- /dev/null +++ b/examples/doc-viewer/app/zh-CN/widgets/shimmer/page.tsx @@ -0,0 +1,105 @@ +import { Text, Widget } from "@lcui/react"; +import CodeBlockCopy from "../../../components/code-block-copy"; +import ShimmerBasicDemo from "../../../examples/shimmer-basic/index"; +import ShimmerColorDemo from "../../../examples/shimmer-color/index"; +import ShimmerDurationDemo from "../../../examples/shimmer-duration/index"; +import ShimmerSpreadDemo from "../../../examples/shimmer-spread/index"; +import ShimmerAngleDemo from "../../../examples/shimmer-angle/index"; +import ShimmerRunningDemo from "../../../examples/shimmer-running/index"; +import FieldTable from "../../../components/field-table"; +import { shimmerFields } from "../../../widget-fields/shimmer"; +import { shimmerContentFields } from "../../../widget-fields/shimmer-content"; + +export default function ShimmerDocPage() { + return ( + +

Shimmer

+

{`给文本、图标等内容叠加一道左右柔化、可倾斜的高光带,从左往右扫过;高光只落在内容的不透明像素上,内容本身保持原色。`}

+ + +

适用场景

+
    +
  • {`[b]适用[/b]:让一段状态文本(图标或纯文字)带着柔和高光扫过,表示“进行中/加载”`}
  • +
  • {`[b]适用[/b]:流式输出时给“正在生成”的提示文字加一层滑动高光`}
  • +
  • {`[b]不适用[/b]:纯灰块的骨架占位(Shimmer 叠加在真实内容上,不生成占位骨架)`}
  • +
  • {`[b]不适用[/b]:有明确百分比或已知时长的任务进度(用 [bgcolor=#eee] progress [/bgcolor])`}
  • +
  • {`[b]不适用[/b]:无固定形状的全局忙碌指示(用 [bgcolor=#eee] spinner [/bgcolor])`}
  • +
+
+ +

用法

+ + + TypeScript + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Shimmer[/color], [color=#8250df]ShimmerContent[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color]`} + + +

{`Shimmer 是 C 代码自绘部件:先按原色完整合成内容,再把一道带倾斜的高光带裁剪到内容的不透明像素上叠加。高光本身可配置颜色、周期、宽度与倾斜角,通过部件属性或对应的 [bgcolor=#eee] ui_shimmer_set_* [/bgcolor] 接口设置:`}

+ + + TypeScript + + + + {`<[color=#8250df]Shimmer[/color] running={[color=#0550ae]true[/color]} duration={[color=#0550ae]2000[/color]} spread={[color=#0550ae]120[/color]} angle={[color=#0550ae]20[/color]} color=[color=#0a3069]"rgba(255,255,255,0.86)"[/color]>`} + {`  [color=#116329]<[color=#0550ae]ShimmerContent[/color]>[/color]`} + {`    [color=#116329]<[color=#0550ae]Text[/color]>[/color]Generating response…[color=#116329][/color]`} + {`  [color=#116329][/color]`} + {``} + + +
+ +

组合

+

{`使用以下组合来构建 [bgcolor=#eee] Shimmer [/bgcolor]:`}

+ + + TEXT + + + + {`Shimmer`} + {`└── ShimmerContent`} + + +
+ +

颜色

+

{`高光带叠加的颜色取自 [bgcolor=#eee] color [/bgcolor],其 alpha 就是扫过处的峰值强度。同一段灰字上叠不同颜色,扫过时会被“染”向各自的色相。`}

+ +
+ +

时长

+

{`[bgcolor=#eee] duration [/bgcolor] 决定高光扫过一次所需的时间,单位为毫秒,默认 [bgcolor=#eee] 2000 [/bgcolor]。值越小扫得越快。`}

+ +
+ +

宽度

+

{`[bgcolor=#eee] spread [/bgcolor] 决定高光带的宽度(逻辑像素)。窄带像一道锐利反光,宽带则一次漫过整行,峰值强度不变、只是覆盖范围不同。`}

+ +
+ +

倾斜

+

{`[bgcolor=#eee] angle [/bgcolor] 让高光带相对竖直方向倾斜(度数)。0° 时各行在同一列被扫到,倾斜后反光沿高度错切,先从顶部掠过再到下方。`}

+ +
+ +

关闭高亮

+

{`把 [bgcolor=#eee] running [/bgcolor] 设为 [bgcolor=#eee] false [/bgcolor] 即可关闭动画:此时不再叠加高光,内容按原色正常显示。`}

+ +
+ +

API 参考

+

Shimmer

+

{`扫光容器,捕获 [bgcolor=#eee] ShimmerContent [/bgcolor] 子树;先按原色完整合成内容,再叠加一道可倾斜、左右渐隐的高光带并裁剪到内容不透明处。高光从左往右扫过,只在内容上显现。`}

+ +

ShimmerContent

+

{`被捕获的内容子树(如文本或图标加文字的状态行),作为 Shimmer 的直属子部件使用;省略时使用一个填满整行的默认占位块。Shimmer 默认块级、高度 20px,可用 [bgcolor=#eee] width [/bgcolor]/[bgcolor=#eee] height [/bgcolor] 覆盖。`}

+ +
+
+ ); +} diff --git a/examples/doc-viewer/app/zh-CN/widgets/spinner/page.c b/examples/doc-viewer/app/zh-CN/widgets/spinner/page.c new file mode 100644 index 00000000..0f402062 --- /dev/null +++ b/examples/doc-viewer/app/zh-CN/widgets/spinner/page.c @@ -0,0 +1,57 @@ +#include "page.tsx.h" +#include "page.h" + +typedef struct { + zh_cn_widgets_spinner_page_react_t base; + // Add additional states to your component here + // ... +} zh_cn_widgets_spinner_page_t; + +static void zh_cn_widgets_spinner_page_init(ui_widget_t *w) +{ + ui_widget_add_data(w, zh_cn_widgets_spinner_page_proto, + sizeof(zh_cn_widgets_spinner_page_t)); + + zh_cn_widgets_spinner_page_react_init(w); + // Write the initialization code for your component here + // such as state initialization, event binding, etc + // ... + + zh_cn_widgets_spinner_page_update(w); +} + +static zh_cn_widgets_spinner_page_t *zh_cn_widgets_spinner_page_get( + ui_widget_t *w) +{ + return ui_widget_get_data(w, zh_cn_widgets_spinner_page_proto); +} + +static void zh_cn_widgets_spinner_page_destroy(ui_widget_t *w) +{ + // Write code here to destroy the relevant resources of the component + // ... + + zh_cn_widgets_spinner_page_react_destroy(w); +} + +void zh_cn_widgets_spinner_page_update(ui_widget_t *w) +{ + zh_cn_widgets_spinner_page_react_update(w); + // Write code here to update other content of your component + // ... +} + +ui_widget_t *ui_create_zh_cn_widgets_spinner_page(void) +{ + return ui_create_widget_with_prototype( + zh_cn_widgets_spinner_page_proto); +} + +void ui_register_zh_cn_widgets_spinner_page(void) +{ + zh_cn_widgets_spinner_page_init_prototype(); + zh_cn_widgets_spinner_page_proto->init = + zh_cn_widgets_spinner_page_init; + zh_cn_widgets_spinner_page_proto->destroy = + zh_cn_widgets_spinner_page_destroy; +} diff --git a/examples/doc-viewer/app/zh-CN/widgets/spinner/page.h b/examples/doc-viewer/app/zh-CN/widgets/spinner/page.h new file mode 100644 index 00000000..23516ff9 --- /dev/null +++ b/examples/doc-viewer/app/zh-CN/widgets/spinner/page.h @@ -0,0 +1,9 @@ +#include + +void ui_register_zh_cn_widgets_spinner_page(void); + +ui_widget_t *ui_create_zh_cn_widgets_spinner_page(void); + +void zh_cn_widgets_spinner_page_update(ui_widget_t *w); + +void ui_load_zh_cn_widgets_spinner_page_resources(void); diff --git a/examples/doc-viewer/app/zh-CN/widgets/spinner/page.tsx b/examples/doc-viewer/app/zh-CN/widgets/spinner/page.tsx new file mode 100644 index 00000000..c9322a7e --- /dev/null +++ b/examples/doc-viewer/app/zh-CN/widgets/spinner/page.tsx @@ -0,0 +1,80 @@ +import { Text, Widget } from "@lcui/react"; +import CodeBlockCopy from "../../../components/code-block-copy"; +import SpinnerBasicDemo from "../../../examples/spinner-basic/index"; +import SpinnerCustomContentDemo from "../../../examples/spinner-custom-content/index"; +import SpinnerSizeDemo from "../../../examples/spinner-size/index"; +import SpinnerRunningDemo from "../../../examples/spinner-running/index"; +import FieldTable from "../../../components/field-table"; +import { spinnerFields } from "../../../widget-fields/spinner"; +import { spinnerContentFields } from "../../../widget-fields/spinner-content"; + +export default function SpinnerDocPage() { + return ( + +

Spinner

+

{`用于表达“加载中”的自绘旋转指示器部件。`}

+ + +

适用场景

+
    +
  • {`[b]适用[/b]:等待异步结果、无法预知进度的加载状态`}
  • +
  • {`[b]适用[/b]:按钮、卡片等局部区域的忙碌指示`}
  • +
  • {`[b]不适用[/b]:有明确百分比或已知时长的任务进度(用 [bgcolor=#eee] progress [/bgcolor])`}
  • +
+
+ +

用法

+ + + TypeScript + + + + {`[color=#cf222e]import[/color] { [color=#8250df]Spinner[/color], [color=#8250df]SpinnerContent[/color] } [color=#cf222e]from[/color] [color=#0a3069]"@lcui/react"[/color]`} + + + + + TypeScript + + + + {`<[color=#8250df]Spinner[/color] running={[color=#0550ae]true[/color]} speed={[color=#0550ae]1[/color]} />`} + + +
+ +

组合

+

{`使用以下组合来构建 [bgcolor=#eee] Spinner [/bgcolor]:`}

+ + + TEXT + + + + {`Spinner`} + {`└── SpinnerContent`} + + +
+ +

示例

+

自定义内容

+ +

不同尺寸

+ +

运行状态控制

+ +
+ +

API 参考

+

Spinner

+

{`旋转指示器容器,捕获 [bgcolor=#eee] SpinnerContent [/bgcolor] 子树并绕其中心旋转绘制。默认自带 16×16 尺寸,可用 [bgcolor=#eee] width [/bgcolor]/[bgcolor=#eee] height [/bgcolor] 覆盖。`}

+ +

SpinnerContent

+

{`被旋转捕获的内容子树,作为 Spinner 的直接子部件使用;省略时使用默认的圆环,其中一边透明形成随旋转扫过的缺口。`}

+ +
+
+ ); +} diff --git a/include/LCUI/widgets.h b/include/LCUI/widgets.h index 0f8cbe78..adb3faaf 100644 --- a/include/LCUI/widgets.h +++ b/include/LCUI/widgets.h @@ -13,6 +13,8 @@ #include "widgets/button.h" #include "widgets/anchor.h" #include "widgets/canvas.h" +#include "widgets/spinner.h" +#include "widgets/shimmer.h" #include "widgets/progress.h" #include "widgets/scrollarea.h" #include "widgets/scrollbar.h" diff --git a/include/LCUI/widgets/shimmer.h b/include/LCUI/widgets/shimmer.h new file mode 100644 index 00000000..0fe1afc2 --- /dev/null +++ b/include/LCUI/widgets/shimmer.h @@ -0,0 +1,73 @@ +/* + * include/LCUI/widgets/shimmer.h: -- LCUI's Shimmer widget + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#ifndef LCUI_INCLUDE_WIDGETS_SHIMMER_H +#define LCUI_INCLUDE_WIDGETS_SHIMMER_H + +#include +#include + +LCUI_BEGIN_HEADER + +/* + * A self-drawn effect that sweeps a soft highlight across real content. + * + * The shimmer captures its `shimmer-content` subtree into a reusable offscreen + * buffer (retaining its alpha as a mask), composites that content at full + * strength so it keeps its original colours, then adds a tilted, gradient + * sheen band on top. The band is masked to the content's alpha, so the + * highlight only lands on the content's opaque pixels and fades out at its + * left and right edges and across its tilt. `color` supplies the highlight + * hue and its alpha the peak strength; `duration` the time of one sweep; + * `spread` the band width; `angle` its tilt. With `running` set to false the + * content is shown as-is, with no sheen. + * + * The `shimmer-content` child is a real child of the shimmer but is styled + * offscreen (position: absolute; left/top: -100%) so the normal window render + * path culls it while the offscreen capture still records it. A shimmer created + * without an explicit `shimmer-content` child gets a default one so it is + * immediately visible; callers and TSX may declare their own. + */ + +/* Create a shimmer widget with a default `shimmer-content` subtree. */ +LCUI_API ui_widget_t *ui_create_shimmer(void); + +/* Start or stop the sweep animation. Running is on by default; when off the + * content is composited with no sheen added. */ +LCUI_API void ui_shimmer_set_running(ui_widget_t *w, bool running); + +/* Set the highlight colour that is overlaid on the content's opaque pixels; + * its alpha is the peak strength of the sheen. Defaults to a semi-transparent + * white. */ +LCUI_API void ui_shimmer_set_color(ui_widget_t *w, pd_color_t color); + +/* Set the time for one full sweep in milliseconds. Defaults to 2000. */ +LCUI_API void ui_shimmer_set_duration(ui_widget_t *w, unsigned ms); + +/* Set the highlight band width in logical pixels; when unset it defaults to + * about 45% of the box width, recomputed on resize. */ +LCUI_API void ui_shimmer_set_spread(ui_widget_t *w, float px); + +/* Set the tilt of the highlight band in degrees. Defaults to 20. */ +LCUI_API void ui_shimmer_set_angle(ui_widget_t *w, float degrees); + +/* Set the sweep phase in [0, 1); while running the animation overrides it. + * Added for deterministic tests and previews. */ +LCUI_API void ui_shimmer_set_phase(ui_widget_t *w, float phase); + +/* Replace the direct `shimmer-content` child with the given subtree. */ +LCUI_API void ui_shimmer_set_content(ui_widget_t *w, ui_widget_t *content); + +LCUI_API void ui_register_shimmer(void); + +LCUI_END_HEADER + +#endif /* LCUI_INCLUDE_WIDGETS_SHIMMER_H */ diff --git a/include/LCUI/widgets/spinner.h b/include/LCUI/widgets/spinner.h new file mode 100644 index 00000000..49794e80 --- /dev/null +++ b/include/LCUI/widgets/spinner.h @@ -0,0 +1,53 @@ +/* + * include/LCUI/widgets/spinner.h: -- LCUI's Spinner widget + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#ifndef LCUI_INCLUDE_WIDGETS_SPINNER_H +#define LCUI_INCLUDE_WIDGETS_SPINNER_H + +#include +#include + +LCUI_BEGIN_HEADER + +/* + * A self-drawn rotating loading indicator. + * + * The spinner captures its `spinner-content` subtree into a reusable offscreen + * buffer and composites it back to the window rotated around the content box + * center. The `spinner-content` child is a real child of the spinner but is + * styled offscreen (position: absolute; left/top: -100000px) so the normal + * window render path culls it while the offscreen capture still records it. + * + * A spinner created without an explicit `spinner-content` child gets a default + * one so it is immediately visible; callers and TSX may declare their own. + */ + +/* Create a spinner widget with a default `spinner-content` subtree. */ +LCUI_API ui_widget_t *ui_create_spinner(void); + +/* Start or stop the rotation animation. Running is on by default. */ +LCUI_API void ui_spinner_set_running(ui_widget_t *w, bool running); + +/* Set the rotation speed in revolutions per second (default 1.0). */ +LCUI_API void ui_spinner_set_speed(ui_widget_t *w, float revolutions_per_sec); + +/* Replace the direct `spinner-content` child with the given subtree. */ +LCUI_API void ui_spinner_set_content(ui_widget_t *w, ui_widget_t *content); + +/* Set the rotation angle in degrees; while running the animation overrides it. + */ +LCUI_API void ui_spinner_set_angle(ui_widget_t *w, double degrees); + +LCUI_API void ui_register_spinner(void); + +LCUI_END_HEADER + +#endif /* LCUI_INCLUDE_WIDGETS_SPINNER_H */ diff --git a/src/lcui_widgets.c b/src/lcui_widgets.c index 25913148..1d0cb025 100644 --- a/src/lcui_widgets.c +++ b/src/lcui_widgets.c @@ -17,6 +17,8 @@ void lcui_widgets_init(void) ui_register_portal(); ui_register_text(); ui_register_canvas(); + ui_register_spinner(); + ui_register_shimmer(); ui_register_anchor(); ui_register_button(); ui_register_icon(); diff --git a/src/widgets/draw_backend.h b/src/widgets/draw_backend.h new file mode 100644 index 00000000..40a8f8a5 --- /dev/null +++ b/src/widgets/draw_backend.h @@ -0,0 +1,182 @@ +/* + * src/widgets/draw_backend.h: internal drawing seam for self-drawn widgets + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#ifndef SRC_WIDGETS_DRAW_BACKEND_H +#define SRC_WIDGETS_DRAW_BACKEND_H + +#include +#include +#include + +/* An opaque offscreen surface. It owns a backend-specific pixel buffer (an + * ARGB canvas internally) and its own physical size derived from a logical + * size multiplied by the active device scale. Widget code never sees the + * pixel buffer or the scale: it only names surfaces and works in logical + * coordinates. The concrete type lives solely in the software backend. */ +typedef struct ui_draw_surface ui_draw_surface_t; + +/* Allocate an empty surface with no backing until resized. */ +ui_draw_surface_t *ui_draw_surface_create(void); + +/* Release a surface and its backing. Safe to call with NULL. */ +void ui_draw_surface_destroy(ui_draw_surface_t *s); + +/* Resize the surface so its backing covers ceil(logical * device_scale) in + * both axes. The backing is supersampled internally (an integer multiple of + * the device resolution) to improve the quality of rotation and compositing; + * callers still work purely in device pixels, and a supersampled surface is + * box-downsampled back to device resolution when it is blended onto a target. + * The buffer is (re)allocated only when the physical size changes; otherwise + * this is a no-op and returns 0. Content is not preserved across a resize. + * Returns 1 when the backing was rebuilt, 0 when unchanged. */ +int ui_draw_surface_resize(ui_draw_surface_t *s, float logical_w, + float logical_h); + +/* Report the logical size the surface was last resized to. Returns 0 and + * fills w/h when the surface has a positive physical size, else -1. */ +int ui_draw_surface_logical_size(const ui_draw_surface_t *s, float *w, + float *h); + +/* Capture a widget subtree into dst, retaining the subtree's alpha. dst must + * already be resized to the target size; this performs no allocation and does + * not touch the layout. The subtree is rasterised at the surface's internal + * supersampled resolution (the active device scale times the supersample + * factor) so the backing is filled at full quality. Returns 0 on success. */ +int ui_draw_capture(ui_widget_t *widget, ui_draw_surface_t *dst); + +/* Rotate src around its center by radians into dst. dst must already be + * resized to the same physical size as src; the existing backing is reused so + * this allocates nothing. src and dst must be different surfaces. Returns 0 on + * success. */ +int ui_draw_rotate_into(const ui_draw_surface_t *src, ui_draw_surface_t *dst, + double radians); + +/* Fill the entire surface with transparent pixels. Returns 0 on success. */ +int ui_draw_clear(ui_draw_surface_t *dst); + +/* Fill a logical-rectangle with an RGBA color, converting to physical pixels + * internally. dst must already be resized. Returns 0 on success. */ +int ui_draw_fill_rect(ui_draw_surface_t *dst, float x_logical, float y_logical, + float w_logical, float h_logical, unsigned char r, + unsigned char g, unsigned char b, unsigned char a); + +/* Fill dst with a tilted, gradient highlight "sheen": a soft-edged band of the + * given color whose alpha peaks at `color.a` along the band's center line and + * fades smoothly to 0 at both sides, so it reads as a sliding sheen rather than + * a solid slab. The band is centered horizontally at `center_x_logical` (its + * middle row), `spread_logical` wide, and rotated by `angle_deg` about that + * middle row so its center line is sheared across the height. The whole surface + * is written every call (transparent outside the band), evaluated per physical + * pixel with no allocation; dst must already be resized. Returns 0 on success. + */ +int ui_draw_fill_sheen(ui_draw_surface_t *dst, pd_color_t color, + float center_x_logical, float spread_logical, + float angle_deg); + +/* Copy src into dst (same physical size) into dst's existing backing. Returns + * 0 on success. */ +int ui_draw_copy(const ui_draw_surface_t *src, ui_draw_surface_t *dst); + +/* Multiply dst alpha by mask alpha in place (destination-in). Both surfaces + * must be valid; this allocates nothing. Returns 0 on success. */ +int ui_draw_mask_into(const ui_draw_surface_t *mask, ui_draw_surface_t *dst); + +/* Lighten dst toward the sheen's colour WITHOUT touching dst's alpha: for each + * pixel, t = sheen.alpha/255 and dst.rgb = dst.rgb*(1-t) + sheen.rgb*t, while + * dst.alpha is left exactly as-is. This is the highlight-composite for shimmer: + * it recolours the content's ink (including antialiased edges) but never + * rewrites their coverage, so text edges do not get a resampled halo. Both + * surfaces must be the same physical size and valid; allocates nothing. Returns + * 0 on success. */ +int ui_draw_sheen_over(ui_draw_surface_t *dst, const ui_draw_surface_t *sheen); + +/* Composite a surface onto a paint target at (x, y) in target pixel space, + * scaling the surface's source alpha by opacity (typically 1.0). Coordinates + * are device pixels: a supersampled surface is box-downsampled to device + * resolution as part of the blend. Returns 0 on success. */ +int ui_draw_blend_surface(pd_context_t *target, const ui_draw_surface_t *src, + int x, int y, bool with_alpha, float opacity); + +/* Composite a sub-rectangle [src_x, src_y, src_w, src_h] of a surface onto a + * paint target at the non-negative offset (dst_x, dst_y). The source rect is in + * device pixels and is box-downsampled from the backing when the surface is + * supersampled. This is the partial-repaint-safe form: pd_canvas_mix does not + * skip the source rows that a negative offset would push above the target, so + * blitting the whole box surface at a negative offset smears the top of the + * surface into the visible sub-region. Callers pass an already-clipped + * destination offset. No allocation. Returns 0 on success; a degenerate rect is + * a no-op returning 0. + */ +int ui_draw_blend_surface_rect(pd_context_t *target, + const ui_draw_surface_t *src, int src_x, + int src_y, int src_w, int src_h, int dst_x, + int dst_y, bool with_alpha, float opacity); + +/* Composite src onto dst at offset (0, 0) in dst's own pixel space, keeping + * dst's pixels under src's alpha (dst is typically a transparent display + * buffer being built up from scratch layers). src is clipped to dst's bounds; + * opacity scales src's source alpha like ui_draw_blend_surface. Both surfaces + * must be valid; this allocates nothing. Returns 0 on success. */ +int ui_draw_surface_composite(ui_draw_surface_t *dst, + const ui_draw_surface_t *src, bool with_alpha, + float opacity); + +/* A cached display layer: it owns one final-composition surface ("display") + * at the physical size for a logical box, plus a dirty flag. Self-drawn + * widgets use it as a render gate: run the full render pass into the display + * surface only when ui_draw_layer_needs_render() says so, then present the + * dirty sub-rectangle of the cache to the paint target. Presenting never + * re-renders, so a widget whose overlap with the dirty region changes (for + * example another window uncovers part of it) blits the cache instead of + * paying for a full redraw. Widget code never sees the backing or the scale. */ +typedef struct ui_draw_layer ui_draw_layer_t; + +/* Allocate an empty layer with no display backing and dirty set (so the first + * needs_render() is true). */ +ui_draw_layer_t *ui_draw_layer_create(void); + +/* Release a layer and its display surface. Safe to call with NULL. */ +void ui_draw_layer_destroy(ui_draw_layer_t *l); + +/* Ensure the display surface covers ceil(logical * device_scale) for the + * given logical box. When the physical size (or scale) changes the backing is + * rebuilt and the layer is marked dirty; a stable frame is a cheap no-op that + * allocates nothing. Returns 1 when the backing changed, 0 when not. */ +int ui_draw_layer_ensure(ui_draw_layer_t *l, float logical_w, float logical_h); + +/* Mark the cache stale: call whenever the widget's content, geometry, or + * animation state changes so the next paint runs the full render pass. */ +void ui_draw_layer_invalidate(ui_draw_layer_t *l); + +/* Whether the full render pass must run before the next present. */ +int ui_draw_layer_needs_render(ui_draw_layer_t *l); + +/* The display surface, as the widget's full-render target. The render writes + * the final composited result into it, including the widget's own alpha; + * transparent places must stay transparent. Valid after ensure() succeeded. */ +ui_draw_surface_t *ui_draw_layer_surface(ui_draw_layer_t *l); + +/* Clear the dirty flag once the full render pass has written the display + * surface. */ +void ui_draw_layer_mark_rendered(ui_draw_layer_t *l); + +/* Blit the intersection of the display surface with the paint target's dirty + * rect, at a non-negative target offset, onto paint. box_x/box_y/box_w/box_h + * are the widget's content box in physical pixels of its canvas coordinate + * space (the same origin as the widget's paint ox/oy/area). Nothing outside + * the box is drawn; an empty intersection draws nothing. The cache is never + * re-rendered here: if the widget skipped its full pass because + * needs_render() was false, this blits the cached pixels. Returns 1 when + * something was blitted, 0 otherwise. */ +int ui_draw_layer_present(ui_draw_layer_t *l, pd_context_t *paint, int box_x, + int box_y, int box_w, int box_h); + +#endif /* SRC_WIDGETS_DRAW_BACKEND_H */ diff --git a/src/widgets/draw_backend_software.c b/src/widgets/draw_backend_software.c new file mode 100644 index 00000000..76f789ae --- /dev/null +++ b/src/widgets/draw_backend_software.c @@ -0,0 +1,828 @@ +/* + * src/widgets/draw_backend_software.c: software implementation of the internal + * drawing seam. It is the only place that touches pd_canvas_t, the device + * scale, or PandaGL canvas operations, so that self-drawn widgets can work + * purely with opaque surfaces and logical coordinates. + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include +#include "draw_backend.h" + +/* M_PI is not portably declared (MSVC hides it behind _USE_MATH_DEFINES). */ +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +/* Supersample factor for the self-drawn surfaces. Content is rasterised into + * a backing that is this many times the device resolution, every pixel + * operation runs at that resolution, and the result is box-downsampled back to + * device resolution only when it is blended onto a paint target. A value of 1 + * disables supersampling entirely and reproduces the pre-supersample + * behaviour exactly. */ +#define UI_DRAW_SUPERSAMPLE 2 + +struct ui_draw_surface { + pd_canvas_t canvas; + /* Device-resolution scratch used only to downsample `canvas` (which is + * UI_DRAW_SUPERSAMPLE times larger) before it is blended onto a target. + * It is retained between calls so supersampling allocates nothing per + * frame; only a size change rebuilds it. Unused when ss == 1. */ + pd_canvas_t scratch; + int scratch_w; + int scratch_h; + float logical_w; + float logical_h; + /* phys_w/phys_h are the *device* pixel size (ceil(logical * scale)); + * the backing canvas is phys_w * ss by phys_h * ss. */ + int phys_w; + int phys_h; + int ss; +}; + +struct ui_draw_layer { + ui_draw_surface_t *display; + int dirty; +}; + +/* Convert a logical rectangle to a physical rectangle, rounding the outer + * edges outward so the intended coverage is fully represented. */ +static void logical_to_phys_rect(float x, float y, float w, float h, + double scale, pd_rect_t *out) +{ + float rx0 = x * (float)scale; + float rx1 = (x + w) * (float)scale; + float ry0 = y * (float)scale; + float ry1 = (y + h) * (float)scale; + int px = (int)floorf(rx0); + int py = (int)floorf(ry0); + + out->x = px; + out->y = py; + out->width = (int)ceilf(rx1) - px; + out->height = (int)ceilf(ry1) - py; +} + +// object 1: surface lifecycle + +ui_draw_surface_t *ui_draw_surface_create(void) +{ + ui_draw_surface_t *s = malloc(sizeof(ui_draw_surface_t)); + + if (!s) { + return NULL; + } + memset(s, 0, sizeof(*s)); + pd_canvas_init(&s->canvas); + pd_canvas_init(&s->scratch); + return s; +} + +void ui_draw_surface_destroy(ui_draw_surface_t *s) +{ + if (!s) { + return; + } + pd_canvas_destroy(&s->canvas); + pd_canvas_destroy(&s->scratch); + free(s); +} + +int ui_draw_surface_resize(ui_draw_surface_t *s, float logical_w, + float logical_h) +{ + double scale = ui_get_actual_scale(); + int ss = UI_DRAW_SUPERSAMPLE < 1 ? 1 : UI_DRAW_SUPERSAMPLE; + int phys_w = (int)ceil((double)logical_w * scale); + int phys_h = (int)ceil((double)logical_h * scale); + int back_w = phys_w * ss; + int back_h = phys_h * ss; + + if (!s) { + return -1; + } + if (phys_w < 1 || phys_h < 1) { + if (s->phys_w == phys_w && s->phys_h == phys_h) { + return 0; + } + pd_canvas_destroy(&s->canvas); + s->phys_w = phys_w; + s->phys_h = phys_h; + s->ss = ss; + s->logical_w = logical_w; + s->logical_h = logical_h; + return 1; + } + if (s->phys_w == phys_w && s->phys_h == phys_h && s->ss == ss && + pd_canvas_is_valid(&s->canvas)) { + s->logical_w = logical_w; + s->logical_h = logical_h; + return 0; + } + pd_canvas_destroy(&s->canvas); + s->canvas.color_type = PD_COLOR_TYPE_ARGB; + pd_canvas_create(&s->canvas, (unsigned)back_w, (unsigned)back_h); + s->phys_w = phys_w; + s->phys_h = phys_h; + s->ss = ss; + s->logical_w = logical_w; + s->logical_h = logical_h; + return 1; +} + +int ui_draw_surface_logical_size(const ui_draw_surface_t *s, float *w, float *h) +{ + if (!s || s->phys_w < 1 || s->phys_h < 1) { + return -1; + } + if (w) { + *w = s->logical_w; + } + if (h) { + *h = s->logical_h; + } + return 0; +} + +// object 2: pixel operations on surfaces + +int ui_draw_capture(ui_widget_t *widget, ui_draw_surface_t *dst) +{ + pd_context_t paint; + pd_rect_t rect; + float saved_scale; + int ss; + + if (!dst || !pd_canvas_is_valid(&dst->canvas)) { + return -1; + } + rect.x = 0; + rect.y = 0; + rect.width = (int)dst->canvas.width; + rect.height = (int)dst->canvas.height; + /* Clear before rendering: a self-drawn widget re-captures its subtree + * into the same persistent surface every animation frame, and the + * render composites source-over, so without this the partial-alpha + * edges of text/AA content accumulate toward full opacity across + * frames. */ + pd_canvas_fill(&dst->canvas, pd_argb(0, 0, 0, 0)); + pd_canvas_init(&paint.canvas); + paint.rect = rect; + paint.with_alpha = true; + pd_canvas_quote(&paint.canvas, &dst->canvas, &rect); + /* The surface backing is ss times the device resolution, so rasterise + * the subtree at ss times the active scale: ui_widget_render derives + * every device coordinate from ui_get_actual_scale(), and the paint + * target is the ss-sized backing, so the whole subtree fills it at the + * supersampled resolution. The global scale is restored immediately so + * nothing outside the capture observes the temporary boost. */ + ss = dst->ss > 0 ? dst->ss : 1; + saved_scale = ui_metrics.scale; + ui_metrics.scale = saved_scale * (float)ss; + ui_widget_render(widget, &paint); + ui_metrics.scale = saved_scale; + return 0; +} + +/* Bilinear fetch at a floating-point source coordinate, reading the ARGB + * backing directly. Out-of-bounds samples read as fully transparent so they can + * be averaged into a supersampled result without a separate coverage term. */ +static pd_color_t sample_bilinear(const pd_color_t *px, int stride, unsigned w, + unsigned h, float sx, float sy) +{ + pd_color_t a, b, c, e, t; + int ix, iy, ix1, iy1; + float fx, fy; + + if (sx < 0 || sy < 0 || sx > (float)(w - 1) || sy > (float)(h - 1)) { + return pd_argb(0, 0, 0, 0); + } + ix = (int)sx; + iy = (int)sy; + fx = sx - (float)ix; + fy = sy - (float)iy; + ix1 = ix + 1 < (int)w ? ix + 1 : ix; + iy1 = iy + 1 < (int)h ? iy + 1 : iy; + a = px[(size_t)iy * stride + ix]; + b = px[(size_t)iy * stride + ix1]; + c = px[(size_t)iy1 * stride + ix]; + e = px[(size_t)iy1 * stride + ix1]; + t.b = (uint8_t)(a.b * (1 - fx) * (1 - fy) + b.b * fx * (1 - fy) + + c.b * (1 - fx) * fy + e.b * fx * fy); + t.g = (uint8_t)(a.g * (1 - fx) * (1 - fy) + b.g * fx * (1 - fy) + + c.g * (1 - fx) * fy + e.g * fx * fy); + t.r = (uint8_t)(a.r * (1 - fx) * (1 - fy) + b.r * fx * (1 - fy) + + c.r * (1 - fx) * fy + e.r * fx * fy); + t.a = (uint8_t)(a.a * (1 - fx) * (1 - fy) + b.a * fx * (1 - fy) + + c.a * (1 - fx) * fy + e.a * fx * fy); + return t; +} + +/* Supersampling grid per destination pixel when rotating. A single bilinear + * tap leaves resampling stair-steps on curved, high-contrast edges; averaging + * SS x SS sub-samples spread across the pixel center turns those steps into a + * smooth ramp. It is purely per-pixel: no scratch buffer is allocated. */ +#define UI_ROTATE_SUPERSAMPLE 4 + +int ui_draw_rotate_into(const ui_draw_surface_t *src, ui_draw_surface_t *dst, + double radians) +{ + const pd_canvas_t *s; + pd_canvas_t *d; + const pd_color_t *sargb; + pd_color_t *dargb; + int sstride, dstride; + int x, y, i, j; + int taps; + unsigned w, h; + float cos_r, sin_r, cx, cy, ss; + + if (!src || !dst) { + return -1; + } + s = &src->canvas; + d = &dst->canvas; + if (!pd_canvas_is_valid(s) || !pd_canvas_is_valid(d)) { + return -1; + } + if (s->width != d->width || s->height != d->height) { + return -1; + } + /* Pixel access goes straight through the ARGB backing rather than the + * per-pixel helpers: this loop is the hottest path for animation, and + * the helpers add a call plus a modulo per sample. */ + sargb = s->argb; + dargb = d->argb; + sstride = (int)(s->bytes_per_row / sizeof(pd_color_t)); + dstride = (int)(d->bytes_per_row / sizeof(pd_color_t)); + /* When the source is already supersampled, the per-pixel grid is + * redundant: the box downsample at present time supplies the + * anti-aliasing, so a single bilinear tap per pixel is enough and + * roughly sixteen times cheaper than the 1x-quality grid. */ + taps = src->ss > 1 ? 1 : UI_ROTATE_SUPERSAMPLE; + ss = (float)taps; + w = s->width; + h = s->height; + /* Rotate about the same centre the border engine draws the circle on: + * CIRCLE_R = radius - 0.5 and the arc is centred at bound + radius - + * 0.5, which for a ring centred in the surface is the pixel-index + * midpoint (w-1)/2. The geometric box centre w/2 is a half pixel too + * far and makes the ring visibly orbit as it rotates. */ + cx = (float)(w - 1) / 2.0f; + cy = (float)(h - 1) / 2.0f; + cos_r = (float)cos(radians); + sin_r = (float)sin(radians); + + if (taps == 1) { + for (y = 0; y < (int)h; y++) { + pd_color_t *row = dargb + (size_t)y * dstride; + float dy = (float)y - cy; + + for (x = 0; x < (int)w; x++) { + float dx = (float)x - cx; + float sx = cos_r * dx + sin_r * dy + cx; + float sy = -sin_r * dx + cos_r * dy + cy; + + row[x] = sample_bilinear(sargb, sstride, w, h, + sx, sy); + } + } + return 0; + } + + for (y = 0; y < (int)h; y++) { + pd_color_t *row = dargb + (size_t)y * dstride; + + for (x = 0; x < (int)w; x++) { + pd_color_t t; + float acc_r = 0.f, acc_g = 0.f, acc_b = 0.f, + acc_a = 0.f; + float inv_ss = (float)(1.0 / (ss * ss)); + + for (j = 0; j < taps; j++) { + for (i = 0; i < taps; i++) { + /* Sub-sample at the center of the + * (i, j) cell of the pixel's grid. */ + float px = (float)x + + ((float)i + 0.5f) / ss - + 0.5f; + float py = (float)y + + ((float)j + 0.5f) / ss - + 0.5f; + float dx = px - cx; + float dy = py - cy; + float sx = cos_r * dx + sin_r * dy + cx; + float sy = + -sin_r * dx + cos_r * dy + cy; + pd_color_t c = sample_bilinear( + sargb, sstride, w, h, sx, sy); + + acc_r += (float)c.r; + acc_g += (float)c.g; + acc_b += (float)c.b; + acc_a += (float)c.a; + } + } + t.r = (uint8_t)(acc_r * inv_ss); + t.g = (uint8_t)(acc_g * inv_ss); + t.b = (uint8_t)(acc_b * inv_ss); + t.a = (uint8_t)(acc_a * inv_ss); + row[x] = t; + } + } + return 0; +} + +int ui_draw_clear(ui_draw_surface_t *dst) +{ + if (!dst || !pd_canvas_is_valid(&dst->canvas)) { + return -1; + } + pd_canvas_fill(&dst->canvas, pd_argb(0, 0, 0, 0)); + return 0; +} + +int ui_draw_fill_rect(ui_draw_surface_t *dst, float x_logical, float y_logical, + float w_logical, float h_logical, unsigned char r, + unsigned char g, unsigned char b, unsigned char a) +{ + pd_rect_t rect; + + if (!dst || !pd_canvas_is_valid(&dst->canvas)) { + return -1; + } + /* Rects are logical; the backing is ss times the device scale, so the + * logical-to-physical factor includes the supersample. */ + logical_to_phys_rect( + x_logical, y_logical, w_logical, h_logical, + ui_get_actual_scale() * (dst->ss > 0 ? dst->ss : 1), &rect); + pd_canvas_fill_rect(&dst->canvas, pd_argb(a, r, g, b), rect); + return 0; +} + +/* Symmetric soft falloff across the sheen: 1 at the band center line, 0 at + * either edge, with a zero slope across the whole span so the highlight fades + * smoothly into transparency instead of showing a hard edge. It is a raised + * cosine over the normalized distance d in [0, 1] from the center line. */ +static float sheen_falloff(float d) +{ + if (d <= 0.0f) { + return 1.0f; + } + if (d >= 1.0f) { + return 0.0f; + } + return 0.5f * (1.0f + cosf((float)M_PI * d)); +} + +int ui_draw_fill_sheen(ui_draw_surface_t *dst, pd_color_t color, + float center_x_logical, float spread_logical, + float angle_deg) +{ + pd_canvas_t *c; + float half, tan_a, midy, lx_scale, ly_scale; + int px, py; + + if (!dst || !pd_canvas_is_valid(&dst->canvas)) { + return -1; + } + c = &dst->canvas; + if (c->width < 1 || c->height < 1 || dst->logical_w < 1.0f || + dst->logical_h < 1.0f) { + return -1; + } + /* Map each physical pixel back to the surface's own logical space so + * the band is described in logical units regardless of the active + * scale. */ + lx_scale = dst->logical_w / (float)c->width; + ly_scale = dst->logical_h / (float)c->height; + half = spread_logical * 0.5f; + if (half < 0.5f) { + half = 0.5f; + } + tan_a = tanf(angle_deg * (float)(M_PI / 180.0)); + midy = dst->logical_h * 0.5f; + + for (py = 0; py < (int)c->height; py++) { + float ly = ((float)py + 0.5f) * ly_scale; + /* The center line is sheared by the tilt, proportional to the + * distance of this row from the surface's logical middle. */ + float shear = tan_a * (ly - midy); + for (px = 0; px < (int)c->width; px++) { + float lx = ((float)px + 0.5f) * lx_scale; + float u = (lx - center_x_logical) + shear; + float d = fabsf(u) / half; + float f = sheen_falloff(d); + pd_color_t col; + + col = pd_argb((unsigned char)(color.a * f + 0.5f), + color.r, color.g, color.b); + pd_canvas_set_pixel(c, (unsigned)px, (unsigned)py, col); + } + } + return 0; +} + +int ui_draw_copy(const ui_draw_surface_t *src, ui_draw_surface_t *dst) +{ + if (!src || !dst) { + return -1; + } + if (!pd_canvas_is_valid(&src->canvas) || + !pd_canvas_is_valid(&dst->canvas)) { + return -1; + } + return pd_canvas_replace(&dst->canvas, &src->canvas, 0, 0) < 0 ? -1 : 0; +} + +int ui_draw_mask_into(const ui_draw_surface_t *mask, ui_draw_surface_t *dst) +{ + if (!mask || !dst) { + return -1; + } + if (!pd_canvas_is_valid(&mask->canvas) || + !pd_canvas_is_valid(&dst->canvas)) { + return -1; + } + return pd_canvas_mask_composite(&dst->canvas, &mask->canvas) < 0 ? -1 + : 0; +} + +int ui_draw_sheen_over(ui_draw_surface_t *dst, const ui_draw_surface_t *sheen) +{ + pd_canvas_t *d; + const pd_canvas_t *s; + unsigned x, y, w, h; + + if (!dst || !sheen) { + return -1; + } + d = &dst->canvas; + s = &sheen->canvas; + if (!pd_canvas_is_valid(d) || !pd_canvas_is_valid(s)) { + return -1; + } + if (d->width != s->width || d->height != s->height) { + return -1; + } + w = d->width; + h = d->height; + for (y = 0; y < h; ++y) { + for (x = 0; x < w; ++x) { + pd_color_t sp = pd_canvas_get_pixel(s, x, y); + pd_color_t dp; + float t; + if (sp.a == 0) { + continue; + } + dp = pd_canvas_get_pixel(d, x, y); + t = sp.a / 255.0f; + dp.r = (uint8_t)(dp.r * (1.0f - t) + sp.r * t + 0.5f); + dp.g = (uint8_t)(dp.g * (1.0f - t) + sp.g * t + 0.5f); + dp.b = (uint8_t)(dp.b * (1.0f - t) + sp.b * t + 0.5f); + /* dp.a intentionally left unchanged: the sheen must not + * alter the coverage of the pixels it recolours. */ + pd_canvas_set_pixel(d, x, y, dp); + } + } + return 0; +} + +/* Return the surface's device-resolution downsample buffer, growing it on + * demand. The buffer is retained on the surface so a supersampled present + * allocates only when the required device size grows, never per frame. */ +static pd_canvas_t *surface_scratch(ui_draw_surface_t *s, int w, int h) +{ + int nw, nh; + + if (w < 1 || h < 1) { + return NULL; + } + if (pd_canvas_is_valid(&s->scratch) && w <= s->scratch_w && + h <= s->scratch_h) { + return &s->scratch; + } + nw = w > s->scratch_w ? w : s->scratch_w; + nh = h > s->scratch_h ? h : s->scratch_h; + pd_canvas_destroy(&s->scratch); + s->scratch.color_type = PD_COLOR_TYPE_ARGB; + pd_canvas_create(&s->scratch, (unsigned)nw, (unsigned)nh); + s->scratch_w = nw; + s->scratch_h = nh; + return &s->scratch; +} + +/* Box-downsample an ss-times supersampled region of src, starting at + * (src_x, src_y) and covering out_w * ss by out_h * ss pixels, into dst at + * (dst_x, dst_y) at device resolution. Channels are averaged in premultiplied + * space and then un-premultiplied so a partially covered pixel keeps its + * colour instead of darkening toward the transparent (0,0,0,0) samples beside + * it; sources outside src read as transparent. */ +static void downsample_box(pd_canvas_t *dst, int dst_x, int dst_y, + const pd_canvas_t *src, int src_x, int src_y, + int out_w, int out_h, int ss) +{ + const pd_color_t *spx = src->argb; + pd_color_t *dpx = dst->argb; + int sstride = (int)(src->bytes_per_row / sizeof(pd_color_t)); + int dstride = (int)(dst->bytes_per_row / sizeof(pd_color_t)); + int sw = (int)src->width; + int sh = (int)src->height; + int ox, oy, sx, sy; + int samples = ss * ss; + + for (oy = 0; oy < out_h; ++oy) { + pd_color_t *drow = dpx + (size_t)(dst_y + oy) * dstride + dst_x; + + for (ox = 0; ox < out_w; ++ox) { + unsigned sum_a = 0, sum_pr = 0, sum_pg = 0, sum_pb = 0; + pd_color_t col; + + for (sy = 0; sy < ss; ++sy) { + int py = src_y + oy * ss + sy; + const pd_color_t *srow; + + if (py < 0 || py >= sh) { + continue; + } + srow = spx + (size_t)py * sstride; + for (sx = 0; sx < ss; ++sx) { + int px = src_x + ox * ss + sx; + pd_color_t c; + + if (px < 0 || px >= sw) { + continue; + } + c = srow[px]; + sum_a += c.a; + sum_pr += (unsigned)c.r * c.a; + sum_pg += (unsigned)c.g * c.a; + sum_pb += (unsigned)c.b * c.a; + } + } + col.a = (uint8_t)(sum_a / samples); + if (sum_a > 0) { + col.r = (uint8_t)(sum_pr / sum_a); + col.g = (uint8_t)(sum_pg / sum_a); + col.b = (uint8_t)(sum_pb / sum_a); + } else { + col.r = col.g = col.b = 0; + } + drow[ox] = col; + } + } +} + +int ui_draw_blend_surface(pd_context_t *target, const ui_draw_surface_t *src, + int x, int y, bool with_alpha, float opacity) +{ + ui_draw_surface_t *s; + pd_canvas_t *scratch, sub; + pd_rect_t r; + int ret; + + if (!src || !target) { + return -1; + } + /* The surface owns a mutable backing buffer; the const qualifier only + * guards callers from naming it, so opacity is applied through the + * underlying blend by setting the buffer's transient opacity field. */ + s = (ui_draw_surface_t *)src; + if (s->ss > 1 && pd_canvas_is_valid(&s->canvas)) { + /* Downsample the supersampled backing to device resolution + * first; blending the raw ss-sized backing 1:1 would clip the + * bottom/right of the target. */ + scratch = surface_scratch(s, s->phys_w, s->phys_h); + if (!scratch) { + return -1; + } + downsample_box(scratch, 0, 0, &s->canvas, 0, 0, s->phys_w, + s->phys_h, s->ss); + r.x = 0; + r.y = 0; + r.width = s->phys_w; + r.height = s->phys_h; + pd_canvas_init(&sub); + if (pd_canvas_quote(&sub, scratch, &r) < 0) { + return -1; + } + sub.opacity = opacity; + ret = pd_canvas_mix(&target->canvas, &sub, x, y, with_alpha); + return ret < 0 ? ret : 0; + } + s->canvas.opacity = opacity; + ret = pd_canvas_mix(&target->canvas, &s->canvas, x, y, with_alpha); + s->canvas.opacity = 1.0; + return ret < 0 ? ret : 0; +} + +int ui_draw_blend_surface_rect(pd_context_t *target, + const ui_draw_surface_t *src, int src_x, + int src_y, int src_w, int src_h, int dst_x, + int dst_y, bool with_alpha, float opacity) +{ + ui_draw_surface_t *s; + pd_canvas_t *scratch, sub; + pd_rect_t r; + int ret; + + if (!src || !target || src_w <= 0 || src_h <= 0) { + return 0; + } + if (!pd_canvas_is_valid(&src->canvas)) { + return -1; + } + s = (ui_draw_surface_t *)src; + if (s->ss > 1) { + /* src_x/src_y/src_w/src_h are in the surface's device pixel + * space; downsample the matching ss-scaled region of the + * backing into the device-sized scratch, then blend that. */ + scratch = surface_scratch(s, src_w, src_h); + if (!scratch) { + return 0; + } + downsample_box(scratch, 0, 0, &s->canvas, src_x * s->ss, + src_y * s->ss, src_w, src_h, s->ss); + r.x = 0; + r.y = 0; + r.width = src_w; + r.height = src_h; + pd_canvas_init(&sub); + if (pd_canvas_quote(&sub, scratch, &r) < 0) { + return -1; + } + sub.opacity = opacity; + ret = pd_canvas_mix(&target->canvas, &sub, dst_x, dst_y, + with_alpha); + return ret < 0 ? ret : 0; + } + r.x = src_x; + r.y = src_y; + r.width = src_w; + r.height = src_h; + pd_canvas_init(&sub); + if (pd_canvas_quote(&sub, &s->canvas, &r) < 0) { + return -1; + } + sub.opacity = opacity; + ret = pd_canvas_mix(&target->canvas, &sub, dst_x, dst_y, with_alpha); + return ret < 0 ? ret : 0; +} + +int ui_draw_surface_composite(ui_draw_surface_t *dst, + const ui_draw_surface_t *src, bool with_alpha, + float opacity) +{ + pd_canvas_t *c; + int ret; + + if (!dst || !src) { + return -1; + } + if (!pd_canvas_is_valid(&dst->canvas) || + !pd_canvas_is_valid(&src->canvas)) { + return -1; + } + /* pd_canvas_mix crops the source to the destination bounds, so a + * larger src simply will not write past the display surface. */ + c = (pd_canvas_t *)&src->canvas; + c->opacity = opacity; + ret = pd_canvas_mix(&dst->canvas, c, 0, 0, with_alpha); + c->opacity = 1.0; + return ret < 0 ? ret : 0; +} + +// object 3: cached display layer + +ui_draw_layer_t *ui_draw_layer_create(void) +{ + ui_draw_layer_t *l = malloc(sizeof(ui_draw_layer_t)); + + if (!l) { + return NULL; + } + l->display = ui_draw_surface_create(); + l->dirty = 1; + if (!l->display) { + free(l); + return NULL; + } + return l; +} + +void ui_draw_layer_destroy(ui_draw_layer_t *l) +{ + if (!l) { + return; + } + ui_draw_surface_destroy(l->display); + free(l); +} + +int ui_draw_layer_ensure(ui_draw_layer_t *l, float logical_w, float logical_h) +{ + int changed; + + if (!l) { + return -1; + } + changed = ui_draw_surface_resize(l->display, logical_w, logical_h); + if (changed < 0) { + return -1; + } + if (changed > 0) { + l->dirty = 1; + } + return changed > 0 ? 1 : 0; +} + +void ui_draw_layer_invalidate(ui_draw_layer_t *l) +{ + if (l) { + l->dirty = 1; + } +} + +int ui_draw_layer_needs_render(ui_draw_layer_t *l) +{ + return l && l->dirty; +} + +ui_draw_surface_t *ui_draw_layer_surface(ui_draw_layer_t *l) +{ + return l ? l->display : NULL; +} + +void ui_draw_layer_mark_rendered(ui_draw_layer_t *l) +{ + if (l) { + l->dirty = 0; + } +} + +int ui_draw_layer_present(ui_draw_layer_t *l, pd_context_t *paint, int box_x, + int box_y, int box_w, int box_h) +{ + pd_rect_t box, overlap; + int src_x, src_y, src_w, src_h, dst_x, dst_y; + + if (!l || !paint || box_w <= 0 || box_h <= 0) { + return 0; + } + if (l->display->phys_w < 1 || l->display->phys_h < 1) { + return 0; + } + box.x = box_x; + box.y = box_y; + box.width = box_w; + box.height = box_h; + if (!pd_rect_overlap(&box, &paint->rect, &overlap)) { + return 0; + } + /* The box rect is the content box rounded from layout; the display + * surface is ceil(logical * scale), so the two can differ by a pixel. + * Clamp the source sub-rect to the surface bounds and keep the target + * offset non-negative by moving it with the clamped edge. */ + src_x = overlap.x - box_x; + src_y = overlap.y - box_y; + src_w = overlap.width; + src_h = overlap.height; + if (src_x < 0) { + src_w += src_x; + src_x = 0; + } + if (src_y < 0) { + src_h += src_y; + src_y = 0; + } + if (src_x + src_w > l->display->phys_w) { + src_w = l->display->phys_w - src_x; + } + if (src_y + src_h > l->display->phys_h) { + src_h = l->display->phys_h - src_y; + } + if (src_w <= 0 || src_h <= 0) { + return 0; + } + dst_x = box_x + src_x - paint->rect.x; + dst_y = box_y + src_y - paint->rect.y; + if (dst_x < 0 || dst_y < 0) { + return 0; + } + /* Blit only the intersecting sub-rectangle: mixing the whole display + * surface at a negative target offset smears the surface's top row + * into a partially dirty region (see pd_canvas_mix). */ + return ui_draw_blend_surface_rect(paint, l->display, src_x, src_y, + src_w, src_h, dst_x, dst_y, true, + 1.0f) == 0 + ? 1 + : 0; +} diff --git a/src/widgets/shimmer.c b/src/widgets/shimmer.c new file mode 100644 index 00000000..b3faaca3 --- /dev/null +++ b/src/widgets/shimmer.c @@ -0,0 +1,610 @@ +/* + * src/widgets/shimmer.c: -- LCUI's Shimmer widget + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "draw_backend.h" +#include "shimmer_priv.h" + +/* Fraction of the box width used as the highlight spread when it is not set + * explicitly. */ +#define UI_SHIMMER_DEFAULT_SPREAD_RATIO 0.45f +/* One full sweep duration in milliseconds when the caller does not set one. */ +#define UI_SHIMMER_DEFAULT_DURATION 2000 +/* The highlight tilt in degrees when the caller does not set one. */ +#define UI_SHIMMER_DEFAULT_ANGLE 20.0f + +typedef struct ui_shimmer { + bool running; + pd_color_t color; + unsigned duration; + float phase; + float spread; + bool spread_set; + float angle; + int frame_id; + uint64_t last_ts; + bool has_ts; + ui_draw_layer_t *layer; + ui_draw_surface_t *content_surface; + ui_draw_surface_t *band_surface; + ui_draw_surface_t *sheen_surface; + /* The captured content is expensive to rebuild (a full subtree render) + * but only changes when the content itself does; the travelling band + * changes every animation frame. Keep the capture cached until + * something marks the content dirty (resize, set_content, or a subtree + * mutation seen by the observer). */ + bool content_dirty; + ui_mutation_observer_t *observer; + ui_widget_t *content; + ui_widget_t *default_content; +} ui_shimmer_t; + +static ui_widget_prototype_t *ui_shimmer_prototype; +static ui_widget_prototype_t *ui_shimmer_content_prototype; + +static const char *ui_shimmer_css = "\ +shimmer {\ + display: block;\ + width: 100%;\ + height: 20px;\ +}\ +shimmer-content {\ + position: absolute;\ + box-sizing: border-box;\ + left: -100%;\ + top: -100%;\ + width: 100%;\ + height: 100%;\ +}\ +shimmer .shimmer-skeleton {\ + position: absolute;\ + box-sizing: border-box;\ + left: 0;\ + top: 0;\ + width: 100%;\ + height: 100%;\ + background-color: #b0b0b0;\ + border-radius: 4px;\ +}\ +"; + +static ui_shimmer_t *ui_shimmer_get_data(ui_widget_t *w); +static void ui_shimmer_on_frame(uint64_t timestamp, void *data); +static void ui_shimmer_size_content(ui_widget_t *content); +static void ui_shimmer_reconcile_frame(ui_widget_t *w); +static void ui_shimmer_attach_content(ui_widget_t *content); +static ui_widget_t *ui_shimmer_build_default_content(ui_widget_t *w); +static void ui_shimmer_fill_band(ui_shimmer_t *shimmer, float logical_w, + float logical_h); +static void ui_shimmer_invalidate_content(ui_widget_t *w); +static void ui_shimmer_observe(ui_widget_t *w); + +// object 1: content binding + +static void ui_shimmer_attach_content(ui_widget_t *content) +{ + ui_widget_t *owner = content->parent; + ui_shimmer_t *shimmer; + + while (owner && owner->proto != ui_shimmer_prototype) { + owner = owner->parent; + } + if (!owner) { + return; + } + shimmer = ui_shimmer_get_data(owner); + if (!shimmer || shimmer->content == content) { + return; + } + /* When a real content node links in, drop the auto-created default so + * the shimmer keeps exactly the one subtree it will capture. */ + if (shimmer->default_content == shimmer->content) { + ui_widget_destroy(shimmer->default_content); + shimmer->default_content = NULL; + } + shimmer->content = content; + ui_shimmer_invalidate_content(owner); +} + +static ui_widget_t *ui_shimmer_build_default_content(ui_widget_t *w) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + ui_widget_t *content, *block; + + content = ui_create_widget_with_prototype(ui_shimmer_content_prototype); + block = ui_create_widget(NULL); + ui_widget_add_class(block, "shimmer-skeleton"); + ui_widget_append(content, block); + /* Pre-set the pointers so the link callback fired by append() treats + * this content as already tracked and does not destroy it. */ + shimmer->content = content; + shimmer->default_content = content; + ui_widget_append(w, content); + return content; +} + +static void ui_shimmer_content_on_link(ui_widget_t *w, ui_event_t *e, void *arg) +{ + ui_shimmer_attach_content(w); +} + +static void ui_shimmer_content_on_init(ui_widget_t *w) +{ + ui_widget_on(w, "link", ui_shimmer_content_on_link, NULL); +} + +// object 2: surface and frame lifecycle + +/* Mark the cached content capture stale so the next full render rebinds the + * subtree. Every change that can alter what the capture looks like must go + * through here (or through a subtree mutation the observer sees), otherwise + * the shimmer keeps sweeping over a stale bitmap. */ +static void ui_shimmer_invalidate_content(ui_widget_t *w) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + if (!shimmer) { + return; + } + shimmer->content_dirty = true; + ui_draw_layer_invalidate(shimmer->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); +} + +static void ui_shimmer_on_mutation(ui_mutation_list_t *records, + ui_mutation_observer_t *observer, void *arg) +{ + ui_shimmer_invalidate_content((ui_widget_t *)arg); +} + +/* Watch the widget and its whole subtree so edits made to custom content + * (through the style, attribute or child-list APIs) invalidate the cache the + * same way the shimmer's own hooks do. The observation targets the shimmer + * itself, not the content node: widgets are destroyed children-first, so an + * observer connected to a child would still hold a dangling pointer when the + * shimmer tears it down (and connecting to an ancestor covers a replacement + * content too). */ +static void ui_shimmer_observe(ui_widget_t *w) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + ui_mutation_observer_init_t options = { + .child_list = true, + .subtree = true, + .properties = true, + .attributes = true, + }; + + if (!shimmer || shimmer->observer) { + return; + } + shimmer->observer = + ui_mutation_observer_create(ui_shimmer_on_mutation, w); + if (shimmer->observer) { + ui_mutation_observer_observe(shimmer->observer, w, options); + } +} + +static void ui_shimmer_size_content(ui_widget_t *content) +{ + if (!content) { + return; + } + /* The content fills the shimmer box and is pushed offscreen so the + * normal window render culls it while ui_draw_capture still records it. + * A percentage box is resolved from the parent on every layout pass, so + * it tracks resize and DPI changes without lagging a frame behind. */ + ui_widget_set_style_string(content, "position", "absolute"); + ui_widget_set_style_string(content, "box-sizing", "border-box"); + ui_widget_set_style_string(content, "left", "-100%"); + ui_widget_set_style_string(content, "top", "-100%"); + ui_widget_set_style_string(content, "width", "100%"); + ui_widget_set_style_string(content, "height", "100%"); +} + +/* Reconcile the one-shot frame request against the desired state: the + * animation runs only while the widget is both running and visible. on_frame + * re-arms the request itself while running, so this only boots the first + * frame or cancels a still-pending request when the widget hides. */ +static void ui_shimmer_reconcile_frame(ui_widget_t *w) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + bool want = shimmer->running && ui_widget_is_visible(w); + + if (want && shimmer->frame_id < 0) { + shimmer->has_ts = false; + shimmer->frame_id = lcui_request_frame(ui_shimmer_on_frame, w); + } else if (!want && shimmer->frame_id >= 0) { + lcui_cancel_frame(shimmer->frame_id); + shimmer->frame_id = -1; + } +} + +/* Write the travelling highlight band into the band surface in logical space. + * The band is a full-height column of the configured colour whose alpha ramps + * smoothly to 0 at both of its edges and is sheared across the height by the + * tilt angle, so it reads as a soft-edged sheen rather than a solid slab; the + * surface backend turns the logical range into device pixels. */ +static void ui_shimmer_fill_band(ui_shimmer_t *shimmer, float logical_w, + float logical_h) +{ + double phase, spread, center; + + /* Wrap the phase so set_phase(1.0) is equivalent to set_phase(0.0); the + * animation already keeps it in [0, 1) but a manual setter may not. */ + phase = fmod(shimmer->phase, 1.0); + if (phase < 0) { + phase += 1.0; + } + spread = shimmer->spread; + if (spread < 1.0) { + spread = 1.0; + } + /* Slide the band's center from just off the left edge to just off the + * right edge across the phase, so the highlight fully enters and exits. + */ + center = -spread + phase * (logical_w + 2.0 * spread); + + ui_draw_clear(shimmer->band_surface); + ui_draw_fill_sheen(shimmer->band_surface, shimmer->color, (float)center, + (float)spread, shimmer->angle); +} + +// object 3: widget callbacks + +static ui_shimmer_t *ui_shimmer_get_data(ui_widget_t *w) +{ + return ui_widget_get_data(w, ui_shimmer_prototype); +} + +static void ui_shimmer_on_frame(uint64_t timestamp, void *data) +{ + ui_widget_t *w = data; + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + bool want; + double ms; + + /* lcui_request_frame is one-shot: reaching this callback means the + * pending request has been consumed, so the stored id is no longer + * cancellable and resets to -1 before any new request is made. */ + shimmer->frame_id = -1; + want = shimmer->running && ui_widget_is_visible(w); + if (!want) { + return; + } + ms = shimmer->has_ts ? (double)(timestamp - shimmer->last_ts) : 0.0; + shimmer->last_ts = timestamp; + shimmer->has_ts = true; + shimmer->phase = + ui_shimmer_step_phase(shimmer->phase, ms, shimmer->duration); + ui_draw_layer_invalidate(shimmer->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); + /* Re-arm the one-shot subscription for the next frame; the id is -1 + * here, so reconcile() on a later update can never stack a second + * pending request on top of this one. */ + shimmer->frame_id = lcui_request_frame(ui_shimmer_on_frame, w); +} + +/* Advance the highlight phase by an elapsed time over a duration and wrap to + * [0, 1). This is the whole clock path so it can be exercised without a running + * app. */ +float ui_shimmer_step_phase(float cur, double ms_elapsed, unsigned duration_ms) +{ + float phase; + + if (duration_ms == 0) { + return cur; + } + phase = fmodf((float)((double)cur + ms_elapsed / (double)duration_ms), + 1.0f); + if (phase < 0.0f) { + phase += 1.0f; + } + return phase; +} + +static void ui_shimmer_on_init(ui_widget_t *w) +{ + ui_shimmer_t *shimmer; + + shimmer = ui_widget_add_data(w, ui_shimmer_prototype, sizeof(*shimmer)); + memset(shimmer, 0, sizeof(*shimmer)); + shimmer->running = true; + shimmer->color = pd_argb(220, 255, 255, 255); + shimmer->duration = UI_SHIMMER_DEFAULT_DURATION; + shimmer->phase = 0.0f; + shimmer->spread = 0.0f; + shimmer->spread_set = false; + shimmer->angle = UI_SHIMMER_DEFAULT_ANGLE; + shimmer->frame_id = -1; + shimmer->layer = ui_draw_layer_create(); + shimmer->content_surface = ui_draw_surface_create(); + shimmer->band_surface = ui_draw_surface_create(); + shimmer->sheen_surface = ui_draw_surface_create(); + /* Provide a default content up front (before children are parsed) so it + * participates in normal layout; an explicit `shimmer-content` that + * links in later replaces it via the link callback. */ + ui_shimmer_build_default_content(w); + shimmer->content_dirty = true; + ui_shimmer_observe(w); + /* The frame subscription is started by the first update reconcile + * rather than here, so a widget that is never shown never requests a + * frame. */ +} + +static void ui_shimmer_on_destroy(ui_widget_t *w) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + if (shimmer->frame_id >= 0) { + lcui_cancel_frame(shimmer->frame_id); + shimmer->frame_id = -1; + } + if (shimmer->observer) { + ui_mutation_observer_destroy(shimmer->observer); + shimmer->observer = NULL; + } + ui_draw_layer_destroy(shimmer->layer); + ui_draw_surface_destroy(shimmer->content_surface); + ui_draw_surface_destroy(shimmer->band_surface); + ui_draw_surface_destroy(shimmer->sheen_surface); + shimmer->layer = NULL; + shimmer->content_surface = NULL; + shimmer->band_surface = NULL; + shimmer->sheen_surface = NULL; +} + +static void ui_shimmer_on_update(ui_widget_t *w, ui_task_type_t task) +{ + if (task == UI_TASK_UPDATE_STYLE) { + /* A style refresh can change how the captured subtree looks + * (inherited properties), so drop the cached capture. */ + ui_shimmer_invalidate_content(w); + return; + } + if (task != UI_TASK_AFTER_UPDATE) { + return; + } + ui_shimmer_reconcile_frame(w); +} + +static void ui_shimmer_on_resize(ui_widget_t *w, float width, float height) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + if (!shimmer->spread_set) { + shimmer->spread = width * UI_SHIMMER_DEFAULT_SPREAD_RATIO; + } + ui_shimmer_size_content(shimmer->content); + /* Geometry (and the derived default spread) changed: force a full + * render on the next paint even if the physical box is unchanged. */ + ui_shimmer_invalidate_content(w); +} + +static void ui_shimmer_on_paint(ui_widget_t *w, pd_context_t *paint, + ui_widget_actual_style_t *style) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + pd_rect_t area, overlap; + float cw = w->content_box.width; + float ch = w->content_box.height; + int ox, oy; + + if (!shimmer->content || cw < 1.0f || ch < 1.0f) { + return; + } + ox = (int)(style->content_box.x - style->canvas_box.x); + oy = (int)(style->content_box.y - style->canvas_box.y); + area.x = ox; + area.y = oy; + area.width = style->content_box.width; + area.height = style->content_box.height; + if (!pd_rect_overlap(&area, &paint->rect, &overlap)) { + return; + } + /* Keep the cached display at the current box size; the backend only + * reallocates when the device size changes, so a stable animation + * allocates nothing per frame. */ + ui_draw_layer_ensure(shimmer->layer, cw, ch); + /* Full render only when the widget itself changed (animation tick, + * phase/colour/spread/angle/content/geometry). A paint that merely + * overlaps a newly dirty neighbour re-presents the cache untouched. */ + if (ui_draw_layer_needs_render(shimmer->layer)) { + ui_draw_surface_t *display = + ui_draw_layer_surface(shimmer->layer); + /* 1. Capture the content subtree, retaining its alpha. The + * capture is reused across frames because the content usually + * does not change; only the band below moves. */ + if (ui_draw_surface_resize(shimmer->content_surface, cw, ch) > + 0) { + shimmer->content_dirty = true; + } + ui_draw_surface_resize(shimmer->band_surface, cw, ch); + ui_draw_surface_resize(shimmer->sheen_surface, cw, ch); + if (shimmer->content_dirty) { + ui_draw_capture(shimmer->content, + shimmer->content_surface); + shimmer->content_dirty = false; + } + /* 2. Base: the content composited at full strength onto a + * clean display, keeping its own colours so nothing is + * dimmed. */ + ui_draw_clear(display); + ui_draw_surface_composite(display, shimmer->content_surface, + true, 1.0f); + /* 3. The travelling sheen: the tilted gradient band masked by + * the content alpha, so the highlight only lands on the ink + * and nowhere the content is transparent. Every step reuses + * the surfaces' existing backing, adding no allocation. */ + ui_shimmer_fill_band(shimmer, cw, ch); + ui_draw_copy(shimmer->band_surface, shimmer->sheen_surface); + ui_draw_mask_into(shimmer->content_surface, + shimmer->sheen_surface); + ui_draw_sheen_over(display, shimmer->sheen_surface); + ui_draw_layer_mark_rendered(shimmer->layer); + } + ui_draw_layer_present(shimmer->layer, paint, ox, oy, (int)area.width, + (int)area.height); +} + +static void ui_shimmer_on_setattr(ui_widget_t *w, const char *name, + const char *value) +{ + /* Declarative attributes route to the public setters. `running` uses + * the HTML boolean convention (a value of "false" disables it); + * `color` is parsed as a CSS colour string; `duration`, `spread`, + * `angle` and `phase` are parsed as numbers. Unknown names are ignored. + */ + if (strcmp(name, "running") == 0) { + ui_shimmer_set_running(w, + !(value && strcmp(value, "false") == 0)); + } else if (strcmp(name, "color") == 0 && value) { + css_style_value_t parsed; + + if (css_parse_color_value(&parsed, value) && + parsed.type == CSS_COLOR_VALUE) { + css_color_value_t cv = parsed.color_value; + ui_shimmer_set_color( + w, pd_argb((uint8_t)(cv >> 24), (uint8_t)(cv >> 16), + (uint8_t)(cv >> 8), (uint8_t)cv)); + } + } else if (strcmp(name, "duration") == 0 && value) { + ui_shimmer_set_duration(w, (unsigned)atoi(value)); + } else if (strcmp(name, "spread") == 0 && value) { + ui_shimmer_set_spread(w, (float)atof(value)); + } else if (strcmp(name, "angle") == 0 && value) { + ui_shimmer_set_angle(w, (float)atof(value)); + } else if (strcmp(name, "phase") == 0 && value) { + ui_shimmer_set_phase(w, (float)atof(value)); + } +} + +// object 4: public API + +void ui_shimmer_set_running(ui_widget_t *w, bool running) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + shimmer->running = running; + if (running) { + /* Restarting re-enters the clock path; refresh the cache on + * the next paint even if the first tick is still queued. */ + ui_draw_layer_invalidate(shimmer->layer); + } + ui_shimmer_reconcile_frame(w); +} + +void ui_shimmer_set_color(ui_widget_t *w, pd_color_t color) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + shimmer->color = color; + /* A different highlight colour changes the current frame's appearance. + */ + ui_draw_layer_invalidate(shimmer->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); +} + +void ui_shimmer_set_duration(ui_widget_t *w, unsigned ms) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + shimmer->duration = ms; + /* The sweep speed feeds the next phase step; refresh the cache so the + * next rendered frame uses the new timing even before a tick. */ + ui_draw_layer_invalidate(shimmer->layer); +} + +void ui_shimmer_set_spread(ui_widget_t *w, float px) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + shimmer->spread = px; + shimmer->spread_set = true; + /* A different spread changes the current frame's appearance. */ + ui_draw_layer_invalidate(shimmer->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); +} + +void ui_shimmer_set_angle(ui_widget_t *w, float degrees) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + shimmer->angle = degrees; + /* A different tilt changes the current frame's appearance. */ + ui_draw_layer_invalidate(shimmer->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); +} + +void ui_shimmer_set_phase(ui_widget_t *w, float phase) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + + shimmer->phase = phase; + /* Moving the band changes the current frame's appearance, so it must + * dirty the graph box for a repaint independent of the clock, and + * force the layer's full render pass on that repaint. */ + ui_draw_layer_invalidate(shimmer->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); +} + +void ui_shimmer_set_content(ui_widget_t *w, ui_widget_t *content) +{ + ui_shimmer_t *shimmer = ui_shimmer_get_data(w); + ui_widget_t *old = shimmer->content; + + if (old == content) { + return; + } + shimmer->content = content; + if (shimmer->default_content == old) { + shimmer->default_content = NULL; + } + if (content) { + if (content->parent != w) { + ui_widget_append(w, content); + } + ui_shimmer_size_content(content); + ui_widget_request_refresh_style(content); + } + if (old && old->parent == w) { + ui_widget_destroy(old); + } + /* A different captured subtree changes the widget's own appearance. */ + ui_shimmer_invalidate_content(w); +} + +ui_widget_t *ui_create_shimmer(void) +{ + return ui_create_widget_with_prototype(ui_shimmer_prototype); +} + +void ui_register_shimmer(void) +{ + ui_shimmer_prototype = ui_create_widget_prototype("shimmer", NULL); + ui_shimmer_prototype->init = ui_shimmer_on_init; + ui_shimmer_prototype->destroy = ui_shimmer_on_destroy; + ui_shimmer_prototype->update = ui_shimmer_on_update; + ui_shimmer_prototype->resize = ui_shimmer_on_resize; + ui_shimmer_prototype->paint = ui_shimmer_on_paint; + ui_shimmer_prototype->setattr = ui_shimmer_on_setattr; + ui_shimmer_content_prototype = + ui_create_widget_prototype("shimmer-content", NULL); + ui_shimmer_content_prototype->init = ui_shimmer_content_on_init; + ui_load_css_string(ui_shimmer_css, __FILE__); +} diff --git a/src/widgets/shimmer_priv.h b/src/widgets/shimmer_priv.h new file mode 100644 index 00000000..26dc3f8c --- /dev/null +++ b/src/widgets/shimmer_priv.h @@ -0,0 +1,24 @@ +/* + * src/widgets/shimmer_priv.h: internal frame-advance math for the shimmer + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#ifndef SRC_WIDGETS_SHIMMER_PRIV_H +#define SRC_WIDGETS_SHIMMER_PRIV_H + +/* Advance the highlight band's phase by an elapsed time. `ms_elapsed` is the + * milliseconds since the previous frame and `duration_ms` the time for one full + * sweep, so the band advances ms_elapsed / duration_ms of a cycle. This is the + * whole of the shimmer's clock path, extracted as a pure function so it can be + * unit-tested without a running app. It wraps the result to [0, 1), folding any + * negative value back into range so a rewound phase still reads as a valid + * position. A non-positive duration leaves the phase unchanged. */ +float ui_shimmer_step_phase(float cur, double ms_elapsed, unsigned duration_ms); + +#endif /* SRC_WIDGETS_SHIMMER_PRIV_H */ diff --git a/src/widgets/spinner.c b/src/widgets/spinner.c new file mode 100644 index 00000000..4b98b4db --- /dev/null +++ b/src/widgets/spinner.c @@ -0,0 +1,535 @@ +/* + * src/widgets/spinner.c: -- LCUI's Spinner widget + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "draw_backend.h" +#include "spinner_priv.h" + +#define UI_SPINNER_PI 3.14159265358979323846 +/* Alpha fraction of the ring's gap side relative to the other sides (0..1): a + * faint, not absent, segment so the mass stays balanced about the centre. */ +#define UI_SPINNER_GAP_ALPHA 0.2f + +typedef struct ui_spinner { + bool running; + float speed; + double angle; + int frame_id; + uint64_t last_ts; + bool has_ts; + bool ring_color_set; + css_color_value_t ring_color; + ui_draw_layer_t *layer; + ui_draw_surface_t *content_surface; + /* The captured content is expensive to rebuild (a full subtree render) + * but only changes when the content itself does; the rotation changes + * every animation frame. Keep the capture cached until something marks + * the content dirty (resize, set_content, ring colour, or a subtree + * mutation seen by the observer). */ + bool content_dirty; + ui_mutation_observer_t *observer; + ui_widget_t *content; + ui_widget_t *default_content; + ui_widget_t *ring; +} ui_spinner_t; + +static ui_widget_prototype_t *ui_spinner_prototype; +static ui_widget_prototype_t *ui_spinner_content_prototype; + +static const char *ui_spinner_css = "\ +spinner {\ + display: inline-block;\ + width: 16px;\ + height: 16px;\ + color: #6b7f8f;\ +}\ +spinner-content {\ + position: absolute;\ + box-sizing: border-box;\ + left: -100%;\ + top: -100%;\ + width: 100%;\ + height: 100%;\ +}\ +spinner .spinner-ring {\ + box-sizing: border-box;\ + position: absolute;\ + left: 2px;\ + top: 2px;\ + right: 2px;\ + bottom: 2px;\ + border-style: solid;\ + border-width: 2px;\ + border-color: #6b7f8f;\ + border-top-color: rgba(107, 127, 143, 0.2);\ + border-radius: 9999px;\ +}\ +"; + +static ui_spinner_t *ui_spinner_get_data(ui_widget_t *w); +static void ui_spinner_on_frame(uint64_t timestamp, void *data); +static void ui_spinner_size_content(ui_widget_t *content); +static void ui_spinner_reconcile_frame(ui_widget_t *w); +static void ui_spinner_attach_content(ui_widget_t *content); +static ui_widget_t *ui_spinner_build_default_content(ui_widget_t *w); +static void ui_spinner_invalidate_content(ui_widget_t *w); +static void ui_spinner_observe(ui_widget_t *w); + +// object 1: content binding + +static void ui_spinner_attach_content(ui_widget_t *content) +{ + ui_widget_t *owner = content->parent; + ui_spinner_t *spinner; + + while (owner && owner->proto != ui_spinner_prototype) { + owner = owner->parent; + } + if (!owner) { + return; + } + spinner = ui_spinner_get_data(owner); + if (!spinner || spinner->content == content) { + return; + } + /* When a real content node links in, drop the auto-created default so + * the spinner keeps exactly the one subtree it will capture. */ + if (spinner->default_content == spinner->content) { + ui_widget_destroy(spinner->default_content); + spinner->default_content = NULL; + spinner->ring = NULL; + } + spinner->content = content; + ui_spinner_invalidate_content(owner); +} + +static ui_widget_t *ui_spinner_build_default_content(ui_widget_t *w) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + ui_widget_t *content, *ring; + + content = ui_create_widget_with_prototype(ui_spinner_content_prototype); + ring = ui_create_widget(NULL); + ui_widget_add_class(ring, "spinner-ring"); + ui_widget_append(content, ring); + /* Pre-set the pointers so the link callback fired by append() treats + * this content as already tracked and does not destroy it. The ring + * pointer lets on_update write the inherited `color` into the stroke; + * custom content is not colour-managed. */ + spinner->content = content; + spinner->default_content = content; + spinner->ring = ring; + ui_widget_append(w, content); + return content; +} + +static void ui_spinner_content_on_link(ui_widget_t *w, ui_event_t *e, void *arg) +{ + ui_spinner_attach_content(w); +} + +static void ui_spinner_content_on_init(ui_widget_t *w) +{ + ui_widget_on(w, "link", ui_spinner_content_on_link, NULL); +} + +// object 2: surface and frame lifecycle + +/* Mark the cached content capture stale so the next full render rebinds the + * subtree. Every change that can alter what the capture looks like must go + * through here (or through a subtree mutation the observer sees), otherwise + * the spinner keeps rotating a stale bitmap. */ +static void ui_spinner_invalidate_content(ui_widget_t *w) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + + if (!spinner) { + return; + } + spinner->content_dirty = true; + ui_draw_layer_invalidate(spinner->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); +} + +static void ui_spinner_on_mutation(ui_mutation_list_t *records, + ui_mutation_observer_t *observer, void *arg) +{ + ui_spinner_invalidate_content((ui_widget_t *)arg); +} + +/* Watch the widget and its whole subtree so edits made to custom content + * (through the style, attribute or child-list APIs) invalidate the cache the + * same way the spinner's own hooks do. The observation targets the spinner + * itself, not the content node: widgets are destroyed children-first, so an + * observer connected to a child would still hold a dangling pointer when the + * spinner tears it down (and connecting to an ancestor covers a replacement + * content too). */ +static void ui_spinner_observe(ui_widget_t *w) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + ui_mutation_observer_init_t options = { + .child_list = true, + .subtree = true, + .properties = true, + .attributes = true, + }; + + if (!spinner || spinner->observer) { + return; + } + spinner->observer = + ui_mutation_observer_create(ui_spinner_on_mutation, w); + if (spinner->observer) { + ui_mutation_observer_observe(spinner->observer, w, options); + } +} + +static void ui_spinner_size_content(ui_widget_t *content) +{ + if (!content) { + return; + } + /* The content fills the spinner box and is pushed offscreen so the + * normal window render culls it while ui_draw_capture still records it. + * A percentage box is resolved from the parent on every layout pass, so + * it tracks resize and DPI changes without lagging a frame behind. */ + ui_widget_set_style_string(content, "position", "absolute"); + ui_widget_set_style_string(content, "box-sizing", "border-box"); + ui_widget_set_style_string(content, "left", "-100%"); + ui_widget_set_style_string(content, "top", "-100%"); + ui_widget_set_style_string(content, "width", "100%"); + ui_widget_set_style_string(content, "height", "100%"); +} + +/* Reconcile the one-shot frame request against the desired state: the + * animation runs only while the widget is both running and visible. on_frame + * re-arms the request itself while running, so this only boots the first + * frame or cancels a still-pending request when the widget hides. */ +static void ui_spinner_reconcile_frame(ui_widget_t *w) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + bool want = spinner->running && ui_widget_is_visible(w); + + if (want && spinner->frame_id < 0) { + spinner->has_ts = false; + spinner->frame_id = lcui_request_frame(ui_spinner_on_frame, w); + } else if (!want && spinner->frame_id >= 0) { + lcui_cancel_frame(spinner->frame_id); + spinner->frame_id = -1; + } +} + +// object 3: widget callbacks + +static ui_spinner_t *ui_spinner_get_data(ui_widget_t *w) +{ + return ui_widget_get_data(w, ui_spinner_prototype); +} + +static void ui_spinner_on_frame(uint64_t timestamp, void *data) +{ + ui_widget_t *w = data; + ui_spinner_t *spinner = ui_spinner_get_data(w); + bool want; + double dt; + + /* lcui_request_frame is one-shot: reaching this callback means the + * pending request has been consumed, so the stored id is no longer + * cancellable and resets to -1 before any new request is made. */ + spinner->frame_id = -1; + want = spinner->running && ui_widget_is_visible(w); + if (!want) { + return; + } + dt = spinner->has_ts ? (double)(timestamp - spinner->last_ts) / 1000.0 + : 0.0; + spinner->last_ts = timestamp; + spinner->has_ts = true; + spinner->angle = + ui_spinner_step_angle(spinner->angle, spinner->speed, dt); + ui_draw_layer_invalidate(spinner->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); + /* Re-arm the one-shot subscription for the next frame; the id is -1 + * here, so reconcile() on a later update can never stack a second + * pending request on top of this one. */ + spinner->frame_id = lcui_request_frame(ui_spinner_on_frame, w); +} + +/* Advance a rotation angle in degrees by a duration and wrap to [0, 360). This + * is the whole clock path so it can be exercised without a running app. */ +double ui_spinner_step_angle(double cur, double speed_rps, double dt_sec) +{ + return fmod(cur + speed_rps * 360.0 * dt_sec, 360.0); +} + +static void ui_spinner_on_init(ui_widget_t *w) +{ + ui_spinner_t *spinner; + + spinner = ui_widget_add_data(w, ui_spinner_prototype, sizeof(*spinner)); + memset(spinner, 0, sizeof(*spinner)); + spinner->running = true; + spinner->speed = 1.0f; + spinner->angle = 0.0; + spinner->frame_id = -1; + spinner->layer = ui_draw_layer_create(); + spinner->content_surface = ui_draw_surface_create(); + /* Provide a default content up front (before children are parsed) so it + * participates in normal layout; an explicit `spinner-content` that + * links in later replaces it via the link callback. */ + ui_spinner_build_default_content(w); + spinner->content_dirty = true; + ui_spinner_observe(w); + /* The frame subscription is started by the first update reconcile + * rather than here, so a widget that is never shown never requests a + * frame. */ +} + +static void ui_spinner_on_destroy(ui_widget_t *w) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + + if (spinner->frame_id >= 0) { + lcui_cancel_frame(spinner->frame_id); + spinner->frame_id = -1; + } + if (spinner->observer) { + ui_mutation_observer_destroy(spinner->observer); + spinner->observer = NULL; + } + ui_draw_layer_destroy(spinner->layer); + ui_draw_surface_destroy(spinner->content_surface); + spinner->layer = NULL; + spinner->content_surface = NULL; +} + +/* Paint reads the four border_X_color slots directly and CSS `current-color` + * is unimplemented, so a stroke that follows the inherited `color` property + * must be written here: copy the spinner's computed colour into the ring's + * left/right/bottom borders and leave the top border transparent as the gap. + * Only the auto-generated ring is managed; custom content keeps its own + * styling untouched. */ +static void ui_spinner_sync_ring_color(ui_widget_t *w) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + css_color_value_t color; + + if (!spinner->ring || !ui_widget_is_visible(w)) { + return; + } + color = w->computed_style.color; + if (spinner->ring_color_set && spinner->ring_color == color) { + return; + } + ui_widget_set_style_color_value(spinner->ring, + css_prop_border_left_color, color); + ui_widget_set_style_color_value(spinner->ring, + css_prop_border_right_color, color); + ui_widget_set_style_color_value(spinner->ring, + css_prop_border_bottom_color, color); + /* The gap side keeps the same hue at a low alpha instead of vanishing, + * so the ring carries ink all the way round and reads as centred, while + * the faint segment still sweeps as a fading tail. */ + ui_widget_set_style_color_value( + spinner->ring, css_prop_border_top_color, + css_color((uint8_t)((color >> 24) * UI_SPINNER_GAP_ALPHA), + (uint8_t)(color >> 16), (uint8_t)(color >> 8), + (uint8_t)color)); + spinner->ring_color = color; + spinner->ring_color_set = true; + ui_spinner_invalidate_content(w); +} + +static void ui_spinner_on_update(ui_widget_t *w, ui_task_type_t task) +{ + if (task == UI_TASK_UPDATE_STYLE) { + /* A style refresh can change how the captured subtree looks + * (inherited properties), so drop the cached capture. */ + ui_spinner_invalidate_content(w); + return; + } + if (task != UI_TASK_AFTER_UPDATE) { + return; + } + ui_spinner_reconcile_frame(w); + ui_spinner_sync_ring_color(w); +} + +static void ui_spinner_on_resize(ui_widget_t *w, float width, float height) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + + ui_spinner_size_content(spinner->content); + /* Geometry changed: the cached display was composed at the old size + * and the subtree relayouts, so run a full render on the next paint + * even if the physical box happens to come back the same. */ + ui_spinner_invalidate_content(w); +} + +static void ui_spinner_on_paint(ui_widget_t *w, pd_context_t *paint, + ui_widget_actual_style_t *style) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + pd_rect_t area, overlap; + float cw = w->content_box.width; + float ch = w->content_box.height; + int ox, oy; + + if (!spinner->content || cw < 1.0f || ch < 1.0f) { + return; + } + ox = (int)(style->content_box.x - style->canvas_box.x); + oy = (int)(style->content_box.y - style->canvas_box.y); + area.x = ox; + area.y = oy; + area.width = style->content_box.width; + area.height = style->content_box.height; + if (!pd_rect_overlap(&area, &paint->rect, &overlap)) { + return; + } + /* Keep the cached display at the current box size; the backend only + * reallocates when the physical size changes, so a stable frame + * allocates nothing. */ + ui_draw_layer_ensure(spinner->layer, cw, ch); + /* Full render only when the widget itself changed (animation tick, + * angle, content, colour, geometry). A paint that merely overlaps a + * newly dirty neighbour re-presents the cache untouched. */ + if (ui_draw_layer_needs_render(spinner->layer)) { + /* Rebuilding the capture re-runs the whole subtree render; the + * rotation below changes every frame but the content usually + * does not, so reuse the previous capture unless something + * marked it dirty (or the backing had to be rebuilt). */ + if (ui_draw_surface_resize(spinner->content_surface, cw, ch) > + 0) { + spinner->content_dirty = true; + } + if (spinner->content_dirty) { + ui_draw_capture(spinner->content, + spinner->content_surface); + spinner->content_dirty = false; + } + ui_draw_rotate_into(spinner->content_surface, + ui_draw_layer_surface(spinner->layer), + spinner->angle * UI_SPINNER_PI / 180.0); + ui_draw_layer_mark_rendered(spinner->layer); + } + ui_draw_layer_present(spinner->layer, paint, ox, oy, (int)area.width, + (int)area.height); +} + +static void ui_spinner_on_setattr(ui_widget_t *w, const char *name, + const char *value) +{ + /* Declarative attributes route to the public setters. `running` uses + * the HTML boolean convention (a value of "false" disables it); + * `speed` and `angle` are parsed as numbers. Unknown names are ignored. + */ + if (strcmp(name, "running") == 0) { + ui_spinner_set_running(w, + !(value && strcmp(value, "false") == 0)); + } else if (strcmp(name, "speed") == 0) { + if (value) { + ui_spinner_set_speed(w, (float)atof(value)); + } + } else if (strcmp(name, "angle") == 0) { + if (value) { + ui_spinner_set_angle(w, atof(value)); + } + } +} + +// object 4: public API + +void ui_spinner_set_running(ui_widget_t *w, bool running) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + + spinner->running = running; + if (running) { + /* Restarting re-enters the clock path; refresh the cache on + * the next paint even if the first tick is still queued. */ + ui_draw_layer_invalidate(spinner->layer); + } + ui_spinner_reconcile_frame(w); +} + +void ui_spinner_set_speed(ui_widget_t *w, float revolutions_per_sec) +{ + ui_spinner_get_data(w)->speed = revolutions_per_sec; +} + +void ui_spinner_set_angle(ui_widget_t *w, double degrees) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + + spinner->angle = degrees; + /* A static angle change alters the current frame's appearance, so it + * must dirty the graph box for a repaint independent of the clock, + * and force the layer's full render pass on that repaint. */ + ui_draw_layer_invalidate(spinner->layer); + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); +} + +void ui_spinner_set_content(ui_widget_t *w, ui_widget_t *content) +{ + ui_spinner_t *spinner = ui_spinner_get_data(w); + ui_widget_t *old = spinner->content; + + if (old == content) { + return; + } + spinner->content = content; + if (spinner->default_content == old) { + spinner->default_content = NULL; + spinner->ring = NULL; + } + if (content) { + if (content->parent != w) { + ui_widget_append(w, content); + } + ui_spinner_size_content(content); + ui_widget_request_refresh_style(content); + } + if (old && old->parent == w) { + ui_widget_destroy(old); + } + /* A different captured subtree changes the widget's own appearance. */ + ui_spinner_invalidate_content(w); +} + +ui_widget_t *ui_create_spinner(void) +{ + return ui_create_widget_with_prototype(ui_spinner_prototype); +} + +void ui_register_spinner(void) +{ + ui_spinner_prototype = ui_create_widget_prototype("spinner", NULL); + ui_spinner_prototype->init = ui_spinner_on_init; + ui_spinner_prototype->destroy = ui_spinner_on_destroy; + ui_spinner_prototype->update = ui_spinner_on_update; + ui_spinner_prototype->resize = ui_spinner_on_resize; + ui_spinner_prototype->paint = ui_spinner_on_paint; + ui_spinner_prototype->setattr = ui_spinner_on_setattr; + ui_spinner_content_prototype = + ui_create_widget_prototype("spinner-content", NULL); + ui_spinner_content_prototype->init = ui_spinner_content_on_init; + ui_load_css_string(ui_spinner_css, __FILE__); +} diff --git a/src/widgets/spinner_priv.h b/src/widgets/spinner_priv.h new file mode 100644 index 00000000..8b50b75f --- /dev/null +++ b/src/widgets/spinner_priv.h @@ -0,0 +1,21 @@ +/* + * src/widgets/spinner_priv.h: internal frame-advance math for the spinner + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#ifndef SRC_WIDGETS_SPINNER_PRIV_H +#define SRC_WIDGETS_SPINNER_PRIV_H + +/* Advance a rotation angle by a duration. This is the whole of the spinner's + * clock path, extracted as a pure function so it can be unit-tested without a + * running app. It wraps the result to [0, 360) so repeated frames never grow + * the angle unbounded. */ +double ui_spinner_step_angle(double cur, double speed_rps, double dt_sec); + +#endif /* SRC_WIDGETS_SPINNER_PRIV_H */ diff --git a/src/widgets/text.c b/src/widgets/text.c index cf2870f4..f66fd26f 100644 --- a/src/widgets/text.c +++ b/src/widgets/text.c @@ -30,6 +30,11 @@ typedef struct ui_text { ui_widget_t *widget; pd_text_t *layer; ui_text_style_t style; + /* Device scale the layer's glyph bitmaps were last rasterised at. A + * self-drawn widget captures its subtree at a boosted scale for + * supersampling, so on_paint rebuilds the layer when the active scale + * no longer matches, keeping the glyphs at their intended size. */ + float layer_scale; ui_text_task_t task; list_node_t node; } ui_text_t; @@ -96,6 +101,7 @@ static void ui_text_update_rendering(ui_widget_t *w) ui_widget_mark_dirty_rect(w, &rect, UI_BOX_TYPE_CONTENT_BOX); } pd_rects_clear(&rects); + txt->layer_scale = scale; } static void ui_text_on_update_content(ui_widget_t *w) @@ -206,6 +212,7 @@ static void ui_text_on_init(ui_widget_t *w) pd_text_set_multiline(txt->layer, true); pd_text_set_style_tag(txt->layer, true); ui_text_style_init(&txt->style); + txt->layer_scale = 0.0f; txt->node.data = txt; txt->node.prev = txt->node.next = NULL; list_append_node(&ui_text.list, &txt->node); @@ -295,6 +302,50 @@ static void ui_text_on_resize(ui_widget_t *w, float width, float height) // logger_debug("[ui-text] resize: %g, %g\n", width, height); } +/* The layer caches glyph bitmaps rasterised at the device scale active when it + * was last laid out. A self-drawn widget captures its subtree at a boosted + * scale so the result can be supersampled, which makes the active scale differ + * from the cached one during that capture; without a rebuild the cached 1x + * bitmaps would be composited into a 2x backing and the text would shrink by + * half. Rebuild the layer here in paint at the current scale, deliberately + * without the widget-level side effects (dirty rects, reflow) the update-time + * path performs: only the layer that paint is about to blit is refreshed. */ +static void ui_text_sync_layer_scale(ui_widget_t *w, + ui_widget_actual_style_t *style) +{ + ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); + ui_text_style_t fs; + pd_text_style_t ts; + list_t rects; + float scale = ui_get_actual_scale(); + + if (txt->layer_scale == scale) { + return; + } + ui_text_style_init(&fs); + ui_compute_text_style(&fs, &w->computed_style); + convert_font_style_to_text_style(&fs, &ts); + pd_text_set_align(txt->layer, fs.text_align); + pd_text_set_line_height(txt->layer, fs.line_height); + pd_text_set_autowrap(txt->layer, + fs.white_space != CSS_WHITE_SPACE_NOWRAP); + pd_text_set_word_break(txt->layer, fs.word_break); + pd_text_set_style(txt->layer, &ts); + pd_text_set_underline( + txt->layer, fs.text_decoration == CSS_TEXT_DECORATION_UNDERLINE, + fs.text_underline_offset, fs.text_decoration_thickness); + pd_text_set_fixed_size(txt->layer, style->content_box.width, + style->content_box.height); + pd_text_set_max_size(txt->layer, style->content_box.width, + style->content_box.height); + list_create(&rects); + pd_text_update(txt->layer, &rects); + pd_rects_clear(&rects); + ui_text_style_destroy(&fs); + pd_text_style_destroy(&ts); + txt->layer_scale = scale; +} + static void ui_text_on_paint(ui_widget_t *w, pd_context_t *paint, ui_widget_actual_style_t *style) { @@ -303,6 +354,7 @@ static void ui_text_on_paint(ui_widget_t *w, pd_context_t *paint, pd_rect_t content_rect, rect; ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); + ui_text_sync_layer_scale(w, style); content_rect.width = style->content_box.width; content_rect.height = style->content_box.height; content_rect.x = style->content_box.x - style->canvas_box.x; diff --git a/tests/fixtures/test_draw_backend.css b/tests/fixtures/test_draw_backend.css new file mode 100644 index 00000000..71af705c --- /dev/null +++ b/tests/fixtures/test_draw_backend.css @@ -0,0 +1,5 @@ +.bg { + width: 32px; + height: 32px; + background-color: #ff0000; +} diff --git a/tests/fixtures/test_draw_backend.xml b/tests/fixtures/test_draw_backend.xml new file mode 100644 index 00000000..121dcab4 --- /dev/null +++ b/tests/fixtures/test_draw_backend.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/fixtures/test_shimmer.css b/tests/fixtures/test_shimmer.css new file mode 100644 index 00000000..0b1fbe64 --- /dev/null +++ b/tests/fixtures/test_shimmer.css @@ -0,0 +1,33 @@ +/* + * All fill colors use CLASS-rank selectors so they beat the UA default + * `* { background-color: transparent }` (GENERAL rank) in the cascade; an + * inline background-color would be overridden and render transparent. + * + * The shimmer's box size is NOT set here: an ID-rank width/height would win + * over the inline size applied by ui_widget_resize, pinning the widget and + * defeating the resize tests. The size is established in C via + * ui_widget_resize instead. + */ + +/* Left-half opaque red over a transparent content box: the mask must reveal + * the highlight sweep only where the captured content alpha is present. */ +.half-left { + position: absolute; + left: 0; + top: 0; + width: 50%; + height: 100%; + background-color: #ff0000; +} + +/* A solid opaque blue block filling the whole content box: used by the phase, + * spread, angle, color, dpi and resize cases, which swap it in via + * set_content. */ +.content-bg { + position: absolute; + left: 0; + top: 0; + width: 100%; + height: 100%; + background-color: #0000ff; +} diff --git a/tests/fixtures/test_shimmer.xml b/tests/fixtures/test_shimmer.xml new file mode 100644 index 00000000..3b8b0fbe --- /dev/null +++ b/tests/fixtures/test_shimmer.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tests/fixtures/test_spinner.css b/tests/fixtures/test_spinner.css new file mode 100644 index 00000000..d031587e --- /dev/null +++ b/tests/fixtures/test_spinner.css @@ -0,0 +1,47 @@ +/* + * All fill colors use CLASS- or ID-rank selectors so they beat the UA default + * `* { background-color: transparent }` (GENERAL rank) in the cascade; an + * inline background-color would be overridden and render transparent. + * + * The spinner's box size is NOT set here: an ID-rank width/height would win + * over the inline size applied by ui_widget_resize (custom_style is merged + * before matched_style), pinning the widget and defeating the resize tests. + * The size is established in C via ui_widget_resize instead. + */ + +/* Opaque red background covering the content box: the identity sample point. */ +.content-bg { + background-color: #ff0000; +} + +/* A 10x10 green mark in the top-left corner: the feature rotation moves. */ +.mark { + position: absolute; + left: 0; + top: 0; + width: 10px; + height: 10px; + background-color: #00ff00; +} + +/* A blue block filling the whole content box: used to prove set_content swaps + * the captured subtree (the center sample changes to blue). */ +.mark-alt { + position: absolute; + left: 0; + top: 0; + width: 100%; + height: 100%; + background-color: #0000ff; +} + +/* An 80x80 red block that overflows a smaller spinner box: proves the capture + * is clipped to the content box so nothing spills onto the window target. */ +.bleed { + position: absolute; + left: 0; + top: 0; + width: 80px; + height: 80px; + background-color: #ff0000; +} diff --git a/tests/fixtures/test_spinner.xml b/tests/fixtures/test_spinner.xml new file mode 100644 index 00000000..07bc16c6 --- /dev/null +++ b/tests/fixtures/test_spinner.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tests/integration/test_draw_backend.c b/tests/integration/test_draw_backend.c new file mode 100644 index 00000000..7bed08ee --- /dev/null +++ b/tests/integration/test_draw_backend.c @@ -0,0 +1,302 @@ +/* + * tests/integration/test_draw_backend.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include "../../src/widgets/draw_backend.h" + +#define WIDGET_SIZE 32 +#define BUFFER_SIZE 64 +#define HALF_PI 1.57079632679489661923 + +static int is_red(pd_color_t c) +{ + return c.a != 0 && c.r >= 253 && c.g < 2 && c.b < 2; +} + +static void make_argb(pd_canvas_t *canvas, int width, int height) +{ + pd_canvas_init(canvas); + canvas->color_type = PD_COLOR_TYPE_ARGB; + pd_canvas_create(canvas, width, height); +} + +/* The surfaces are opaque to callers, so a test reads one back by compositing + * it, at 1:1 with the transparent device canvas it was rendered on (dpi 96), + * into a fresh ARGB canvas it can sample. */ +static void read_surface_into(ui_draw_surface_t *s, pd_canvas_t *out) +{ + pd_rect_t rect = { 0, 0, BUFFER_SIZE, BUFFER_SIZE }; + pd_context_t ctx; + + make_argb(out, BUFFER_SIZE, BUFFER_SIZE); + pd_canvas_init(&ctx.canvas); + ctx.rect = rect; + ctx.with_alpha = true; + pd_canvas_quote(&ctx.canvas, out, &rect); + ui_draw_blend_surface(&ctx, s, 0, 0, true, 1.0f); +} + +static ui_draw_surface_t *make_surface(void) +{ + ui_draw_surface_t *s = ui_draw_surface_create(); + + ui_draw_surface_resize(s, BUFFER_SIZE, BUFFER_SIZE); + return s; +} + +static void check_capture(void) +{ + pd_canvas_t out; + ui_draw_surface_t *s; + ui_widget_t *box; + pd_color_t color; + + ui_metrics.dpi = 96; + + /* Use a CSS class rule (.bg) for the background: the UA default `*` + * rule sets background-color: transparent at GENERAL rank, which an + * inline style cannot override through the cascade, but a CLASS-rank + * selector can. This guarantees the render pass paints an opaque + * background into the capture surface. */ + box = ui_load_xml_file("test_draw_backend.xml"); + ui_widget_append(ui_root(), box); + ui_widget_unwrap(box); + box = ui_get_widget("box"); + ui_widget_request_refresh_style(box); + ui_widget_update(ui_root()); + + s = make_surface(); + ui_draw_capture(box, s); + read_surface_into(s, &out); + + color = pd_canvas_get_pixel(&out, WIDGET_SIZE / 2, WIDGET_SIZE / 2); + ctest_equal_bool("capture: should paint widget background at center", + is_red(color), true); + + color = pd_canvas_get_pixel(&out, BUFFER_SIZE - 1, BUFFER_SIZE - 1); + ctest_equal_int("capture: should leave area outside widget transparent", + color.a, 0); + + ui_draw_surface_destroy(s); + pd_canvas_destroy(&out); + ui_widget_destroy(box); +} + +static void check_rotate(void) +{ + pd_canvas_t out; + ui_draw_surface_t *src, *dst; + pd_color_t color; + + ui_metrics.dpi = 96; + + /* Build a red square occupying the top-left quadrant so the feature + * has a location that rotation can visibly move. */ + src = make_surface(); + ui_draw_clear(src); + ui_draw_fill_rect(src, 0, 0, WIDGET_SIZE, WIDGET_SIZE, 255, 0, 0, 255); + + dst = make_surface(); + ui_draw_rotate_into(src, dst, HALF_PI); + read_surface_into(dst, &out); + + /* Rotated 90 degrees, the top-left square lands on the top-right. */ + color = pd_canvas_get_pixel(&out, BUFFER_SIZE - WIDGET_SIZE / 2, + WIDGET_SIZE / 2); + ctest_equal_bool("rotate: should move the feature to the top-right", + is_red(color), true); + + color = pd_canvas_get_pixel(&out, WIDGET_SIZE / 2, WIDGET_SIZE / 2); + ctest_equal_int("rotate: should clear the original feature location", + color.a, 0); + + ui_draw_surface_destroy(src); + ui_draw_surface_destroy(dst); + pd_canvas_destroy(&out); +} + +static void check_mask_into(void) +{ + pd_canvas_t out; + ui_draw_surface_t *dst, *mask; + pd_color_t color; + + ui_metrics.dpi = 96; + + /* Fully opaque red surface so both halves start with alpha 255. */ + dst = make_surface(); + ui_draw_fill_rect(dst, 0, 0, BUFFER_SIZE, BUFFER_SIZE, 255, 0, 0, 255); + + /* Mask is transparent on the left half, opaque on the right half. */ + mask = make_surface(); + ui_draw_clear(mask); + ui_draw_fill_rect(mask, BUFFER_SIZE / 2, 0, BUFFER_SIZE / 2, + BUFFER_SIZE, 255, 255, 255, 255); + + ui_draw_mask_into(mask, dst); + read_surface_into(dst, &out); + + color = pd_canvas_get_pixel(&out, BUFFER_SIZE / 4, BUFFER_SIZE / 2); + ctest_equal_int("mask: should zero alpha on the masked half", color.a, + 0); + + color = pd_canvas_get_pixel(&out, BUFFER_SIZE * 3 / 4, BUFFER_SIZE / 2); + ctest_equal_bool("mask: should keep the unmasked half opaque and red", + is_red(color), true); + + ui_draw_surface_destroy(dst); + ui_draw_surface_destroy(mask); + pd_canvas_destroy(&out); +} + +static void check_blend_surface(void) +{ + pd_canvas_t out; + ui_draw_surface_t *src; + pd_rect_t rect = { 0, 0, BUFFER_SIZE, BUFFER_SIZE }; + pd_context_t ctx; + pd_color_t color; + + ui_metrics.dpi = 96; + + /* An opaque blue target with a fully opaque red surface composited at a + * non-zero offset: the offset region shows red, the untouched margin + * keeps the base color, proving blend honors both position and alpha. + */ + make_argb(&out, BUFFER_SIZE, BUFFER_SIZE); + pd_canvas_fill(&out, pd_rgb(0, 0, 255)); + src = make_surface(); + ui_draw_fill_rect(src, 0, 0, BUFFER_SIZE / 2, BUFFER_SIZE / 2, 255, 0, + 0, 255); + + pd_canvas_init(&ctx.canvas); + ctx.rect = rect; + ctx.with_alpha = true; + pd_canvas_quote(&ctx.canvas, &out, &rect); + ui_draw_blend_surface(&ctx, src, WIDGET_SIZE / 2, WIDGET_SIZE / 2, true, + 1.0f); + + color = pd_canvas_get_pixel(&out, WIDGET_SIZE, WIDGET_SIZE); + ctest_equal_bool("blend: offset region should show the surface", + is_red(color), true); + + color = pd_canvas_get_pixel(&out, 2, 2); + ctest_equal_bool("blend: untouched margin should keep the base color", + color.b > 250 && color.r < 4, true); + + ui_draw_surface_destroy(src); + pd_canvas_destroy(&out); +} + +/* Cached display layer: ensure() gates the physical size, needs_render() + * starts true and follows invalidate/mark_rendered, and present() blits the + * cache's sub-rect of the target's dirty region without re-rendering it. */ +static void check_layer(void) +{ + pd_canvas_t out, before_canvas; + pd_rect_t rect = { 0, 0, BUFFER_SIZE, BUFFER_SIZE }; + pd_context_t ctx; + ui_draw_layer_t *l; + ui_draw_surface_t *display; + pd_color_t before, after; + int x, y, differs; + + ui_metrics.dpi = 96; + + l = ui_draw_layer_create(); + ctest_equal_bool("layer: create should return a layer", l != NULL, + true); + ctest_equal_bool("layer: a fresh layer needs a render", + ui_draw_layer_needs_render(l), true); + + ctest_equal_int( + "layer: first ensure builds the backing", + ui_draw_layer_ensure(l, BUFFER_SIZE / 2, BUFFER_SIZE / 2), 1); + ctest_equal_int( + "layer: ensure at the same size is a no-op", + ui_draw_layer_ensure(l, BUFFER_SIZE / 2, BUFFER_SIZE / 2), 0); + + /* Write an opaque red block into the display surface as the "full + * render" would, then close the gate. */ + display = ui_draw_layer_surface(l); + ui_draw_fill_rect(display, 0, 0, BUFFER_SIZE / 4, BUFFER_SIZE / 4, 255, + 0, 0, 255); + ui_draw_layer_mark_rendered(l); + ctest_equal_bool("layer: mark_rendered closes the gate", + ui_draw_layer_needs_render(l), false); + + make_argb(&out, BUFFER_SIZE, BUFFER_SIZE); + pd_canvas_fill(&out, pd_rgb(255, 255, 255)); + pd_canvas_init(&ctx.canvas); + ctx.rect = rect; + ctx.with_alpha = true; + pd_canvas_quote(&ctx.canvas, &out, &rect); + ctest_equal_int("layer: present blits the cache into the target", + ui_draw_layer_present(l, &ctx, 0, 0, BUFFER_SIZE / 2, + BUFFER_SIZE / 2), + 1); + + after = pd_canvas_get_pixel(&out, BUFFER_SIZE / 8, BUFFER_SIZE / 8); + ctest_equal_bool("layer: the rendered block should reach the target", + is_red(after), true); + after = pd_canvas_get_pixel(&out, BUFFER_SIZE - 2, BUFFER_SIZE - 2); + ctest_equal_bool("layer: pixels outside the box stay untouched", + after.r > 250 && after.g > 250 && after.b > 250, true); + + /* A second present over the same target re-blits the cache: present + * must neither re-render nor clear it, so the target is + * pixel-identical. */ + make_argb(&before_canvas, BUFFER_SIZE, BUFFER_SIZE); + pd_canvas_replace(&before_canvas, &out, 0, 0); + pd_canvas_init(&ctx.canvas); + ctx.rect = rect; + ctx.with_alpha = true; + pd_canvas_quote(&ctx.canvas, &out, &rect); + ctest_equal_int("layer: a second present still blits", + ui_draw_layer_present(l, &ctx, 0, 0, BUFFER_SIZE / 2, + BUFFER_SIZE / 2), + 1); + differs = 0; + for (y = 0; y < BUFFER_SIZE; ++y) { + for (x = 0; x < BUFFER_SIZE; ++x) { + before = pd_canvas_get_pixel(&before_canvas, x, y); + after = pd_canvas_get_pixel(&out, x, y); + if (before.value != after.value) { + differs += 1; + } + } + } + ctest_equal_int("layer: re-presenting must not disturb the cache", + differs, 0); + after = pd_canvas_get_pixel(&out, BUFFER_SIZE / 8, BUFFER_SIZE / 8); + ctest_equal_bool("layer: the cache still carries the block", + is_red(after), true); + + ui_draw_layer_invalidate(l); + ctest_equal_bool("layer: invalidate reopens the gate", + ui_draw_layer_needs_render(l), true); + + ui_draw_layer_destroy(l); + pd_canvas_destroy(&out); + pd_canvas_destroy(&before_canvas); +} + +void test_draw_backend(void) +{ + lcui_init(); + ctest_describe("draw backend capture", check_capture); + ctest_describe("draw backend rotate", check_rotate); + ctest_describe("draw backend mask into", check_mask_into); + ctest_describe("draw backend blend surface", check_blend_surface); + ctest_describe("draw backend layer", check_layer); + lcui_destroy(); +} diff --git a/tests/integration/test_shimmer.c b/tests/integration/test_shimmer.c new file mode 100644 index 00000000..7630dce2 --- /dev/null +++ b/tests/integration/test_shimmer.c @@ -0,0 +1,787 @@ +/* + * tests/integration/test_shimmer.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include +#include +#include +#include "../../src/widgets/draw_backend.h" +#include "../../src/widgets/shimmer_priv.h" +#include "../scenes/shimmer_scene.h" + +#define BOX 80 +/* The base canvas is filled with a dark grey and the content is composited at + * full strength (so opaque `.content-bg` blue / `.half-left` red columns read + * much brighter than the base), while the default highlight is a white sheen. + * A scanned opaque column is therefore lifted toward the highlight colour; a + * transparent column, or an opaque column outside the band, stays put. This + * makes every "lifted / not lifted" comparison a real measurement, never a + * tautology. + */ +#define BASE pd_rgb(16, 16, 16) +#define BASE_LUM 16 + +static shimmer_scene_t scene; + +static void make_argb(pd_canvas_t *canvas, int width, int height) +{ + pd_canvas_init(canvas); + canvas->color_type = PD_COLOR_TYPE_ARGB; + pd_canvas_create(canvas, (unsigned)width, (unsigned)height); +} + +/* Weighted luminance (Rec. 601). The default sheen is white, so lifting an + * opaque content pixel always raises its luminance toward 255. */ +static int lum_of(pd_color_t c) +{ + return (int)(0.299f * c.r + 0.587f * c.g + 0.114f * c.b); +} + +static int is_base(pd_color_t c) +{ + return c.r < 30 && c.g < 30 && c.b < 30; +} + +static void refresh_style_each(ui_widget_t *w, void *arg) +{ + ui_widget_request_refresh_style(w); +} + +/* Prepare a shimmer for a deterministic single-frame render: stop the + * animation so set_phase is authoritative, pin the spread, resize the box, + * then refresh the whole subtree and reflow so the offscreen content is + * measured at the current scale before we render. Three updates absorb the + * one-pass layout lag from the resize callback that sizes the offscreen + * content. */ +static void prime_shimmer(ui_widget_t *sh, float width, float height, + float phase, float spread) +{ + ui_shimmer_set_running(sh, false); + ui_shimmer_set_spread(sh, spread); + ui_shimmer_set_phase(sh, phase); + ui_widget_resize(sh, width, height); + ui_widget_request_refresh_style(sh); + ui_widget_each(sh, refresh_style_each, NULL); + ui_update(); + ui_update(); + ui_update(); +} + +static void render_into(ui_widget_t *sh, pd_canvas_t *out, int cw, int ch) +{ + pd_rect_t rect = { 0, 0, cw, ch }; + pd_context_t paint; + + make_argb(out, cw, ch); + pd_canvas_fill(out, BASE); + paint.with_alpha = true; + paint.rect = rect; + pd_canvas_init(&paint.canvas); + pd_canvas_quote(&paint.canvas, out, &rect); + ui_widget_render(sh, &paint); +} + +/* Build a fresh shimmer on the root and install a whole-box opaque blue + * content (`.content-bg`) or a left-half-only opaque red content + * (`.half-left`). An owned widget per case keeps the shared scene from + * drifting when one case swaps the captured subtree. */ +static ui_widget_t *make_shimmer(const char *child_class) +{ + ui_widget_t *sh = ui_create_shimmer(); + ui_widget_t *content = ui_create_widget("shimmer-content"); + ui_widget_t *child = ui_create_widget(NULL); + + ui_widget_append(ui_root(), sh); + ui_widget_add_class(child, child_class); + ui_widget_append(content, child); + ui_shimmer_set_content(sh, content); + return sh; +} + +static int col_lum(pd_canvas_t *out, int x, int row) +{ + return lum_of(pd_canvas_get_pixel(out, x, row)); +} + +static int peak_column(pd_canvas_t *out, int row, int width) +{ + int x, best = -1, bestv = -1; + + for (x = 0; x < width; ++x) { + int v = col_lum(out, x, row); + if (v > bestv) { + bestv = v; + best = x; + } + } + return best; +} + +static int count_bright(pd_canvas_t *out, int row, int width, int thresh) +{ + int x, n = 0; + + for (x = 0; x < width; ++x) { + if (col_lum(out, x, row) > thresh) { + n += 1; + } + } + return n; +} + +static double bright_centroid(pd_canvas_t *out, int row, int width, int thresh) +{ + int x; + double sw = 0.0, sx = 0.0; + + for (x = 0; x < width; ++x) { + int v = col_lum(out, x, row); + if (v > thresh) { + double w = (double)(v - thresh); + sw += w; + sx += w * x; + } + } + return sw > 0.0 ? sx / sw : -1.0; +} + +/* 1. Sheen is clipped to the content alpha: over the half-left content the + * band centered on the red/transparent boundary must brighten the opaque + * (left) columns it covers while the transparent (right) columns it covers + * stay at the bare base (the sheen is multiplied by the captured content + * alpha, so nothing lands on transparent pixels). phase 0.5 / spread 30 puts + * the band center at x=40 on an 80 px box, so both the opaque column 34 and + * the transparent column 46 sit inside the band's footprint. */ +static void check_mask_alpha(void) +{ + ui_widget_t *sh; + pd_canvas_t out; + int opaque_in_band, transparent_in_band, opaque_off_band; + + ui_metrics.dpi = 96; + sh = make_shimmer("half-left"); + prime_shimmer(sh, BOX, BOX, 0.5f, 30.0f); + render_into(sh, &out, BOX, BOX); + opaque_in_band = col_lum(&out, 34, BOX / 2); + transparent_in_band = col_lum(&out, 46, BOX / 2); + opaque_off_band = col_lum(&out, 10, BOX / 2); + + ctest_equal_bool("mask: band over an opaque column should brighten it", + opaque_in_band > opaque_off_band + 60, true); + ctest_equal_bool( + "mask: band over a transparent column should not lift it", + transparent_in_band - BASE_LUM < (opaque_in_band - BASE_LUM) / 3, + true); + ctest_equal_bool("mask: the transparent column should stay base-like", + is_base(pd_canvas_get_pixel(&out, 46, BOX / 2)), true); + + pd_canvas_destroy(&out); + ui_widget_destroy(sh); +} + +/* 2. A band is NOT a solid slab: over solid content the peak column must + * clear the base far more than columns toward the band's left and right + * edges. */ +static void check_gradient(void) +{ + ui_widget_t *sh; + pd_canvas_t out; + int p, row = BOX / 2, center, left_edge, right_edge; + + ui_metrics.dpi = 96; + sh = make_shimmer("content-bg"); + prime_shimmer(sh, BOX, BOX, 0.5f, 40.0f); + render_into(sh, &out, BOX, BOX); + + p = peak_column(&out, row, BOX); + center = col_lum(&out, p, row); + left_edge = col_lum(&out, p - 17, row); + right_edge = col_lum(&out, p + 17, row); + + ctest_equal_bool("gradient: the band center should be strongly lifted", + center - BASE_LUM > 120, true); + ctest_equal_bool("gradient: the left edge should be dimmer than center", + center - left_edge > 40, true); + ctest_equal_bool( + "gradient: the right edge should be dimmer than center", + center - right_edge > 40, true); + + pd_canvas_destroy(&out); + ui_widget_destroy(sh); +} + +/* 3. Phase: as the phase advances the sheen footprint's centroid travels + * left to right, and a phase past 1 wraps to the same spot (fmod). The new + * center model keeps the band fully off-screen at phase 0 (center = -spread), + * so the test uses in-flight phases rather than 0. */ +static void check_phase(void) +{ + ui_widget_t *sh; + pd_canvas_t c25, c75, c130, c30; + double q25, q75, q130, q30; + + ui_metrics.dpi = 96; + sh = make_shimmer("content-bg"); + + prime_shimmer(sh, BOX, BOX, 0.25f, 16.0f); + render_into(sh, &c25, BOX, BOX); + prime_shimmer(sh, BOX, BOX, 0.75f, 16.0f); + render_into(sh, &c75, BOX, BOX); + prime_shimmer(sh, BOX, BOX, 0.3f, 16.0f); + render_into(sh, &c30, BOX, BOX); + prime_shimmer(sh, BOX, BOX, 1.3f, 16.0f); + render_into(sh, &c130, BOX, BOX); + + /* Threshold just above the opaque base so only sheened columns count. + */ + q25 = bright_centroid(&c25, BOX / 2, BOX, BASE_LUM + 40); + q75 = bright_centroid(&c75, BOX / 2, BOX, BASE_LUM + 40); + q30 = bright_centroid(&c30, BOX / 2, BOX, BASE_LUM + 40); + q130 = bright_centroid(&c130, BOX / 2, BOX, BASE_LUM + 40); + + ctest_equal_bool("phase: the band sits left of center at phase 0.25", + q25 > 0.0 && q25 < 20.0, true); + ctest_equal_bool("phase: the band sits right of center at phase 0.75", + q75 > 60.0, true); + ctest_equal_bool("phase: the band travels right as phase grows", + q75 > q25 + 20.0, true); + ctest_equal_bool("phase: phase 1.3 wraps back to phase 0.3's spot", + fabs(q130 - q30) < 3.0, true); + + pd_canvas_destroy(&c25); + pd_canvas_destroy(&c75); + pd_canvas_destroy(&c30); + pd_canvas_destroy(&c130); + ui_widget_destroy(sh); +} + +/* 4. Spread: a wider band brightens strictly more columns and keeps the + * peak lift (the extra width spreads the sheen, it does not thin it). */ +static void check_spread(void) +{ + ui_widget_t *sh; + pd_canvas_t narrow, wide; + int narrow_count, wide_count, narrow_peak, wide_peak; + + ui_metrics.dpi = 96; + sh = make_shimmer("content-bg"); + + prime_shimmer(sh, BOX, BOX, 0.5f, 16.0f); + render_into(sh, &narrow, BOX, BOX); + narrow_count = count_bright(&narrow, BOX / 2, BOX, BASE_LUM + 40); + narrow_peak = + col_lum(&narrow, peak_column(&narrow, BOX / 2, BOX), BOX / 2); + + prime_shimmer(sh, BOX, BOX, 0.5f, 44.0f); + render_into(sh, &wide, BOX, BOX); + wide_count = count_bright(&wide, BOX / 2, BOX, BASE_LUM + 40); + wide_peak = col_lum(&wide, peak_column(&wide, BOX / 2, BOX), BOX / 2); + + ctest_equal_bool("spread: a wider band should lift more columns", + wide_count > narrow_count, true); + ctest_equal_bool("spread: the wider peak should not be dimmer", + wide_peak >= narrow_peak - 10, true); + + pd_canvas_destroy(&narrow); + pd_canvas_destroy(&wide); + ui_widget_destroy(sh); +} + +/* 5. Angle: with a tilt the band's center shears with the row, so the + * brightest column on the top row differs from that on the bottom row; with + * angle 0 the two agree. */ +static void check_angle(void) +{ + ui_widget_t *sh; + pd_canvas_t flat, tilted; + int flat_top, flat_bottom, tilt_top, tilt_bottom; + + ui_metrics.dpi = 96; + sh = make_shimmer("content-bg"); + + ui_shimmer_set_angle(sh, 0.0f); + prime_shimmer(sh, BOX, BOX, 0.5f, 30.0f); + render_into(sh, &flat, BOX, BOX); + flat_top = peak_column(&flat, 4, BOX); + flat_bottom = peak_column(&flat, BOX - 5, BOX); + + ui_shimmer_set_angle(sh, 45.0f); + prime_shimmer(sh, BOX, BOX, 0.5f, 30.0f); + render_into(sh, &tilted, BOX, BOX); + tilt_top = peak_column(&tilted, 4, BOX); + tilt_bottom = peak_column(&tilted, BOX - 5, BOX); + + ctest_equal_bool( + "angle: at angle 0 the top and bottom peaks should align", + abs(flat_top - flat_bottom) < 6, true); + ctest_equal_bool( + "angle: at angle 45 the top and bottom peaks should separate", + abs(tilt_top - tilt_bottom) > 18, true); + + pd_canvas_destroy(&flat); + pd_canvas_destroy(&tilted); + ui_widget_destroy(sh); +} + +/* 6. Color: the default white sheen lifts blue content toward white (all + * channels rise); setting a pure-red overlay instead drives the red channel + * up while leaving green low, proving the highlight follows the colour and + * is not a fixed white. */ +static void check_color(void) +{ + ui_widget_t *sh; + pd_canvas_t white_out, red_out; + int wp, rp; + pd_color_t wc, rc, offc; + + ui_metrics.dpi = 96; + + sh = make_shimmer("content-bg"); + ui_shimmer_set_color(sh, pd_argb(255, 255, 255, 255)); + prime_shimmer(sh, BOX, BOX, 0.5f, 40.0f); + render_into(sh, &white_out, BOX, BOX); + + ui_shimmer_set_color(sh, pd_argb(255, 255, 0, 0)); + prime_shimmer(sh, BOX, BOX, 0.5f, 40.0f); + render_into(sh, &red_out, BOX, BOX); + + wp = peak_column(&white_out, BOX / 2, BOX); + rp = peak_column(&red_out, BOX / 2, BOX); + wc = pd_canvas_get_pixel(&white_out, wp, BOX / 2); + rc = pd_canvas_get_pixel(&red_out, rp, BOX / 2); + offc = pd_canvas_get_pixel(&red_out, 4, BOX / 2); + + /* The white peak raises green as much as it raises red; the red peak + * raises red but leaves green near the untouched blue content. */ + ctest_equal_bool("color: the white peak should be bright in green", + wc.g > 180, true); + ctest_equal_bool("color: the red peak should be bright in red", + rc.r > 180, true); + ctest_equal_bool("color: the red peak should stay low in green", + rc.g < rc.r - 80, true); + ctest_equal_bool("color: an off-band column should not be red-shifted", + offc.r < 40, true); + + pd_canvas_destroy(&white_out); + pd_canvas_destroy(&red_out); + ui_widget_destroy(sh); +} + +/* 7. DPI parity: the capture buffer is rebuilt at the device scale, so a + * point inside the enlarged 192 buffer but outside the 96 buffer carries + * content only at 192. */ +static void check_dpi_parity(void) +{ + ui_widget_t *sh; + pd_canvas_t c96, c192; + int out_size = BOX * 2; + + ui_metrics.dpi = 96; + sh = make_shimmer("content-bg"); + prime_shimmer(sh, BOX, BOX, 0.0f, 16.0f); + render_into(sh, &c96, out_size, out_size); + + ui_metrics.dpi = 192; + prime_shimmer(sh, BOX, BOX, 0.0f, 16.0f); + render_into(sh, &c192, out_size, out_size); + + ctest_equal_bool("dpi: inside the box should carry content at 96", + col_lum(&c96, 10, 10) > BASE_LUM, true); + ctest_equal_bool("dpi: inside the box should carry content at 192", + col_lum(&c192, 10, 10) > BASE_LUM, true); + ctest_equal_bool( + "dpi: 96 buffer stays 80 (far corner is base color)", + is_base(pd_canvas_get_pixel(&c96, out_size - 2, out_size - 2)), + true); + ctest_equal_bool( + "dpi: 192 buffer grows to 160 (far corner shows content)", + col_lum(&c192, out_size - 2, out_size - 2) > BASE_LUM, true); + + ui_metrics.dpi = 96; + pd_canvas_destroy(&c96); + pd_canvas_destroy(&c192); + ui_widget_destroy(sh); +} + +/* 8. Resize parity: after growing the box the buffers rebuild to the new + * size, so fresh content appears in the enlarged area with no leftover. */ +static void check_resize_parity(void) +{ + ui_widget_t *sh; + pd_canvas_t small, big; + + ui_metrics.dpi = 96; + sh = make_shimmer("content-bg"); + + prime_shimmer(sh, 16, 16, 0.5f, 6.0f); + render_into(sh, &small, 48, 48); + + prime_shimmer(sh, 48, 48, 0.5f, 18.0f); + render_into(sh, &big, 48, 48); + + ctest_equal_bool( + "resize: a 16px box should leave the outer area as base color", + is_base(pd_canvas_get_pixel(&small, 40, 40)), true); + ctest_equal_bool("resize: growing to 48 should fill the new area", + col_lum(&big, 4, 4) > BASE_LUM, true); + ctest_equal_bool("resize: content should reach the 48px far corner", + col_lum(&big, 44, 44) > BASE_LUM, true); + + pd_canvas_destroy(&small); + pd_canvas_destroy(&big); + ui_widget_destroy(sh); +} + +/* 9. Lifecycle: the factory + default content path works, running can be + * toggled, set_content swaps the captured subtree, the new setters and + * declarative attributes (including a CSS colour string) are accepted, and + * destroy is safe. The default content is the whole-row gray skeleton block, + * which is opaque, so every middle row carries content. */ +static void check_lifecycle(void) +{ + ui_widget_t *sh, *alt; + pd_canvas_t out; + + ui_metrics.dpi = 96; + sh = ui_create_shimmer(); + ctest_equal_bool("lifecycle: factory should return a widget", + sh != NULL, true); + + ui_widget_append(ui_root(), sh); + prime_shimmer(sh, BOX, BOX, 0.5f, 30.0f); + render_into(sh, &out, BOX, BOX); + ctest_equal_bool("lifecycle: the default content should render opaque " + "across the row", + col_lum(&out, BOX / 2, BOX / 2) > BASE_LUM, true); + pd_canvas_destroy(&out); + + ui_shimmer_set_running(sh, true); + ui_shimmer_set_running(sh, false); + ui_shimmer_set_running(sh, true); + + alt = ui_create_widget("shimmer-content"); + ui_widget_add_class(alt, "content-bg"); + ui_shimmer_set_content(sh, alt); + + ui_shimmer_set_running(sh, false); + ui_shimmer_set_duration(sh, 1000); + ui_shimmer_set_angle(sh, 30.0f); + ui_shimmer_set_color(sh, pd_argb(255, 0, 255, 255)); + ui_widget_set_attr(sh, "phase", "0.3"); + ui_widget_set_attr(sh, "spread", "24"); + ui_widget_set_attr(sh, "angle", "15"); + ui_widget_set_attr(sh, "duration", "1500"); + ui_widget_set_attr(sh, "color", "#ff8000"); + ui_widget_set_attr(sh, "running", "false"); + ui_widget_destroy(sh); +} + +/* 10. Default full-row block (regression lock): a bare shimmer has an + * intrinsic size and its default content fills the whole box, so every middle + * row shows content edge to edge; the band column is brighter than an off-band + * column, proving the content is composited at full strength (not dimmed) with + * a sheen added on top. */ +static void check_default_full_row(void) +{ + ui_widget_t *sh, *box; + pd_canvas_t out; + int x, w = 40, h = 20, row = 10, present = 0; + + ui_metrics.dpi = 96; + box = ui_create_widget(NULL); + ui_widget_append(ui_root(), box); + ui_widget_resize(box, 120, 60); + sh = ui_create_shimmer(); + ui_widget_append(box, sh); + ui_widget_request_refresh_style(sh); + ui_widget_each(sh, refresh_style_each, NULL); + ui_update(); + ui_update(); + ui_update(); + ctest_equal_bool( + "default row: a bare shimmer should have intrinsic size", + sh->content_box.width >= 8.0f && sh->content_box.height >= 8.0f, + true); + + ui_shimmer_set_running(sh, false); + ui_shimmer_set_spread(sh, 12.0f); + ui_shimmer_set_phase(sh, 0.5f); + ui_widget_resize(sh, (float)w, (float)h); + ui_widget_request_refresh_style(sh); + ui_widget_each(sh, refresh_style_each, NULL); + ui_update(); + ui_update(); + ui_update(); + render_into(sh, &out, w, h); + + for (x = 2; x < w - 2; ++x) { + if (col_lum(&out, x, row) > BASE_LUM + 8) { + present += 1; + } + } + ctest_equal_int("default row: the whole row should carry content", + present, w - 4); + + { + int peak = peak_column(&out, row, w); + ctest_equal_bool( + "default row: the band peak should be brighter than " + "off-band", + col_lum(&out, peak, row) > col_lum(&out, 4, row) + 20, + true); + } + + ui_widget_destroy(box); + pd_canvas_destroy(&out); +} + +/* 11. step_phase: the extracted clock math advances by elapsed/duration, + * wraps to [0, 1), folds negatives, and leaves the phase put for a zero or + * unset duration. */ +static void check_step_phase(void) +{ + ctest_equal_float("step_phase: half of a 2000ms sweep after 1000ms", + ui_shimmer_step_phase(0.0f, 1000.0, 2000), 0.5f); + ctest_equal_float("step_phase: advancing past 1 wraps into range", + ui_shimmer_step_phase(0.8f, 600.0, 2000), 0.1f); + ctest_equal_float("step_phase: a negative elapsed wraps forward", + ui_shimmer_step_phase(0.1f, -300.0, 2000), 0.95f); + ctest_equal_float("step_phase: a zero duration leaves the phase", + ui_shimmer_step_phase(0.42f, 500.0, 0), 0.42f); +} + +/* Render only the sub-region [rx,ry,rw,rh] (widget-canvas coords) into a + * rw×rh canvas. Mirrors how the renderer hands a widget a partial dirty rect: + * the canvas quote covers the whole small buffer while paint->rect names the + * region in the widget's own canvas coordinates. */ +static void render_region(ui_widget_t *sh, pd_canvas_t *out, int rx, int ry, + int rw, int rh) +{ + pd_rect_t q = { 0, 0, rw, rh }; + pd_context_t paint; + + make_argb(out, rw, rh); + pd_canvas_fill(out, BASE); + pd_canvas_init(&paint.canvas); + paint.rect.x = rx; + paint.rect.y = ry; + paint.rect.width = rw; + paint.rect.height = rh; + paint.with_alpha = true; + pd_canvas_quote(&paint.canvas, out, &q); + ui_widget_render(sh, &paint); +} + +/* Compare the bottom half of a partial render against the same bottom half of + * a full render pixel by pixel (bit-exact); returns the number of differing + * pixels. */ +static int count_bottom_half_diff(pd_canvas_t *full, pd_canvas_t *part, + int phys, int half) +{ + int x, y, diff = 0; + + for (y = 0; y < half; ++y) { + for (x = 0; x < phys; ++x) { + pd_color_t a = pd_canvas_get_pixel(full, x, half + y); + pd_color_t b = pd_canvas_get_pixel(part, x, y); + if (abs((int)a.value - (int)b.value) != 0) { + diff += 1; + } + } + } + return diff; +} + +/* 12. Partial repaint (cached layer): rendering just the bottom half of the + * shimmer must equal the same half of a full render, at 96 dpi and at 144 dpi + * where the physical box differs from the logical one. The layer keeps the + * composed display between paints and presents only the dirty sub-rect, so a + * half-region repaint never re-renders or smears. */ +static void check_partial_repaint(void) +{ + ui_widget_t *sh; + pd_canvas_t full, part; + int phys96 = BOX; /* ceil(80 * 1.0) */ + int phys144 = BOX * 3 / 2; /* ceil(80 * 1.5) */ + + ui_metrics.dpi = 96; + sh = make_shimmer("content-bg"); + prime_shimmer(sh, BOX, BOX, 0.5f, 30.0f); + render_region(sh, &full, 0, 0, phys96, phys96); + render_region(sh, &part, 0, phys96 / 2, phys96, phys96 / 2); + ctest_equal_bool( + "partial repaint: bottom half must match the same region of a " + "full render", + count_bottom_half_diff(&full, &part, phys96, phys96 / 2) == 0, + true); + pd_canvas_destroy(&full); + pd_canvas_destroy(&part); + + ui_metrics.dpi = 144; + prime_shimmer(sh, BOX, BOX, 0.5f, 30.0f); + render_region(sh, &full, 0, 0, phys144, phys144); + render_region(sh, &part, 0, phys144 / 2, phys144, phys144 / 2); + ctest_equal_bool( + "partial repaint hidpi: bottom half must match the same region of " + "a full render at 144 dpi", + count_bottom_half_diff(&full, &part, phys144, phys144 / 2) == 0, + true); + pd_canvas_destroy(&full); + pd_canvas_destroy(&part); + ui_metrics.dpi = 96; + ui_widget_destroy(sh); +} + +/* 13. The sheen must only recolour the content's pixels, never change their + * alpha: it lightens what is already there (RGB toward the highlight) but the + * alpha the base content established must survive untouched -- otherwise text / + * antialiased edges get their coverage rewritten (aliasing). Render identical + * content with the band parked off-canvas (phase 0) and parked over the middle + * (phase 0.5) onto a TRANSPARENT target; the content carries partial alpha + * (opacity 0.5) so a wrong source-over would raise it. Every pixel's alpha must + * match between the two frames; some pixel's RGB must differ (non-vacuous). */ +static void render_transparent(ui_widget_t *sh, pd_canvas_t *out, int cw, + int ch) +{ + pd_rect_t rect = { 0, 0, cw, ch }; + pd_context_t paint; + + make_argb(out, cw, ch); + pd_canvas_fill(out, pd_argb(0, 0, 0, 0)); + pd_canvas_init(&paint.canvas); + paint.rect = rect; + paint.with_alpha = true; + pd_canvas_quote(&paint.canvas, out, &rect); + ui_widget_render(sh, &paint); +} + +static void check_sheen_preserves_alpha(void) +{ + ui_widget_t *sh, *content, *child; + pd_canvas_t a, b; + int x, y, alpha_changed = 0, rgb_changed = 0; + const int w = 40; + + ui_metrics.dpi = 96; + sh = ui_create_shimmer(); + content = ui_create_widget("shimmer-content"); + child = ui_create_widget(NULL); + ui_widget_add_class(child, "content-bg"); + ui_widget_set_style_string(child, "opacity", "0.5"); + ui_widget_append(content, child); + ui_shimmer_set_content(sh, content); + ui_widget_append(ui_root(), sh); + prime_shimmer(sh, 40.0f, 40.0f, 0.0f, 12.0f); + render_transparent(sh, &a, w, w); + ui_shimmer_set_phase(sh, 0.5f); + render_transparent(sh, &b, w, w); + for (y = 0; y < w; ++y) { + for (x = 0; x < w; ++x) { + pd_color_t pa = pd_canvas_get_pixel(&a, x, y); + pd_color_t pb = pd_canvas_get_pixel(&b, x, y); + if (pa.a != pb.a) { + alpha_changed += 1; + } + if (pa.r != pb.r || pa.g != pb.g || pa.b != pb.b) { + rgb_changed += 1; + } + } + } + ctest_equal_bool( + "sheen: band must not alter any pixel's alpha vs a no-band frame", + alpha_changed == 0, true); + ctest_equal_bool( + "sheen: at least one pixel's RGB is lifted (non-vacuous)", + rgb_changed > 0, true); + pd_canvas_destroy(&a); + pd_canvas_destroy(&b); + ui_widget_destroy(sh); +} + +/* 14. Capture-cache invalidation (regression): the content capture is reused + * between animation frames, so any change to the captured subtree must drop + * it or the sweep would run over a stale bitmap. Grow the subtree (a + * child-list mutation the observer must catch) and swap the whole content + * node; both must show up in the next render. */ +static void check_content_cache_invalidates(void) +{ + ui_widget_t *sh, *content, *child, *alt; + pd_canvas_t empty, appended, swapped; + int row = BOX / 2; + + ui_metrics.dpi = 96; + sh = ui_create_shimmer(); + content = ui_create_widget("shimmer-content"); + ui_shimmer_set_content(sh, content); + ui_widget_append(ui_root(), sh); + prime_shimmer(sh, BOX, BOX, 0.0f, 10.0f); + render_into(sh, &empty, BOX, BOX); + + /* Grow the subtree. No style refresh follows, so here only the + * mutation observer can drop the cached capture. */ + child = ui_create_widget(NULL); + ui_widget_add_class(child, "half-left"); + ui_widget_append(content, child); + ui_update(); + ui_update(); + render_into(sh, &appended, BOX, BOX); + + /* Replace the whole content node: a widget hook, independent of the + * observer. */ + alt = ui_create_widget("shimmer-content"); + child = ui_create_widget(NULL); + ui_widget_add_class(child, "content-bg"); + ui_widget_append(alt, child); + ui_shimmer_set_content(sh, alt); + ui_update(); + ui_update(); + render_into(sh, &swapped, BOX, BOX); + + ctest_equal_bool("cache: an empty content should show the base", + is_base(pd_canvas_get_pixel(&empty, 4, row)), true); + ctest_equal_bool("cache: appending a child must re-capture (observer)", + pd_canvas_get_pixel(&appended, 4, row).r > 200, true); + ctest_equal_bool("cache: swapping the content node must re-capture", + pd_canvas_get_pixel(&swapped, BOX - 6, row).b > 200, + true); + + pd_canvas_destroy(&empty); + pd_canvas_destroy(&appended); + pd_canvas_destroy(&swapped); + ui_widget_destroy(sh); +} + +void test_shimmer(void) +{ + lcui_init(); + shimmer_scene_build(&scene); + + ctest_describe("shimmer mask alpha", check_mask_alpha); + ctest_describe("shimmer gradient", check_gradient); + ctest_describe("shimmer phase", check_phase); + ctest_describe("shimmer spread", check_spread); + ctest_describe("shimmer angle", check_angle); + ctest_describe("shimmer color", check_color); + ctest_describe("shimmer dpi parity", check_dpi_parity); + ctest_describe("shimmer resize parity", check_resize_parity); + ctest_describe("shimmer lifecycle", check_lifecycle); + ctest_describe("shimmer default full row", check_default_full_row); + ctest_describe("shimmer step phase", check_step_phase); + ctest_describe("shimmer partial repaint", check_partial_repaint); + ctest_describe("shimmer sheen preserves alpha", + check_sheen_preserves_alpha); + ctest_describe("shimmer content cache", + check_content_cache_invalidates); + + ui_metrics.dpi = 96; + lcui_destroy(); +} diff --git a/tests/integration/test_spinner.c b/tests/integration/test_spinner.c new file mode 100644 index 00000000..6d0ffd83 --- /dev/null +++ b/tests/integration/test_spinner.c @@ -0,0 +1,1222 @@ +/* + * tests/integration/test_spinner.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include +#include +#include +#include "../../src/widgets/draw_backend.h" +#include "../../src/widgets/spinner_priv.h" +#include "../scenes/spinner_scene.h" + +#define SPIN 40 +#define RING 16 +#define BASE pd_rgb(255, 255, 255) + +/* The default ring stroke color (#6b7f8f). */ +#define RING_R 107 +#define RING_G 127 +#define RING_B 143 + +#define RED_R 255 +#define RED_G 0 +#define RED_B 0 +#define GREEN_R 0 +#define GREEN_G 255 +#define GREEN_B 0 +#define BLUE_R 0 +#define BLUE_G 0 +#define BLUE_B 255 + +static spinner_scene_t scene; + +static void make_argb(pd_canvas_t *canvas, int width, int height) +{ + pd_canvas_init(canvas); + canvas->color_type = PD_COLOR_TYPE_ARGB; + pd_canvas_create(canvas, (unsigned)width, (unsigned)height); +} + +static int is_color(pd_color_t c, int r, int g, int b) +{ + return c.a > 200 && abs(c.r - r) < 24 && abs(c.g - g) < 24 && + abs(c.b - b) < 24; +} + +static int is_base(pd_color_t c) +{ + return abs(c.r - 255) < 8 && abs(c.g - 255) < 8 && abs(c.b - 255) < 8; +} + +/* A pixel carrying the default ring stroke color. */ +static int is_ring(pd_color_t c) +{ + return c.a > 180 && abs(c.r - RING_R) < 30 && abs(c.g - RING_G) < 30 && + abs(c.b - RING_B) < 40; +} + +/* A pixel carrying the ring stroke when `color` is set to red. */ +static int is_redish(pd_color_t c) +{ + return c.a > 200 && c.r > 180 && c.g < 90 && c.b < 90; +} + +/* Count ring-stroke pixels inside the half-open box [x0, x1) x [y0, y1). The + * inset ring and supersampled rotation move exact sample points frame to + * frame, so the ring assertions count regions rather than pin single pixels. + */ +static int count_ring(pd_canvas_t *out, int x0, int y0, int x1, int y1) +{ + int x, y, n = 0; + + for (y = y0; y < y1; ++y) { + for (x = x0; x < x1; ++x) { + if (is_ring(pd_canvas_get_pixel(out, x, y))) { + n += 1; + } + } + } + return n; +} + +/* Mean x of every ring-stroke pixel; -1 when no stroke is present. Used to + * detect the horizontal direction the ring centroid shifts under rotation. */ +static double ring_centroid_x(pd_canvas_t *out, int w, int h) +{ + int x, y, n = 0; + double sx = 0.0; + + for (y = 0; y < h; ++y) { + for (x = 0; x < w; ++x) { + if (is_ring(pd_canvas_get_pixel(out, x, y))) { + sx += x; + n += 1; + } + } + } + return n > 0 ? sx / (double)n : -1.0; +} + +/* Distance from the nearest box edge to the closest ring-stroke column, taken + * over the full height so the top gap cannot short-circuit the measurement. */ +static int ring_left_margin(pd_canvas_t *out, int w, int h) +{ + int x, y; + + for (x = 0; x < w; ++x) { + for (y = 0; y < h; ++y) { + if (is_ring(pd_canvas_get_pixel(out, x, y))) { + return x; + } + } + } + return w; +} + +static int ring_right_margin(pd_canvas_t *out, int w, int h) +{ + int x, y; + + for (x = w - 1; x >= 0; --x) { + for (y = 0; y < h; ++y) { + if (is_ring(pd_canvas_get_pixel(out, x, y))) { + return (w - 1) - x; + } + } + } + return w; +} + +static void refresh_style_each(ui_widget_t *w, void *arg) +{ + ui_widget_request_refresh_style(w); +} + +/* Prepare a spinner for a deterministic single-frame render: stop the + * animation so set_angle is authoritative, resize the box, then refresh the + * whole subtree and reflow so the content (and its descendants) are measured + * at the current scale before we render. Three updates absorb the one-pass + * layout lag from the resize callback that sizes the offscreen content. */ +static void prime_spinner(ui_widget_t *sp, float width, float height, + double angle) +{ + ui_spinner_set_running(sp, false); + ui_spinner_set_angle(sp, angle); + ui_widget_resize(sp, width, height); + ui_widget_request_refresh_style(sp); + ui_widget_each(sp, refresh_style_each, NULL); + ui_update(); + ui_update(); + ui_update(); +} + +static void render_into(ui_widget_t *sp, pd_canvas_t *out, int cw, int ch) +{ + pd_rect_t rect = { 0, 0, cw, ch }; + pd_context_t paint; + + make_argb(out, cw, ch); + pd_canvas_fill(out, BASE); + paint.with_alpha = true; + paint.rect = rect; + pd_canvas_init(&paint.canvas); + pd_canvas_quote(&paint.canvas, out, &rect); + ui_widget_render(sp, &paint); +} + +/* 1. Identity: at angle 0 the spinner's composited center equals a direct + * offscreen capture of its content, which also proves the content appears only + * once (nothing painted at the offscreen -100000px location). */ +static void check_identity(void) +{ + pd_canvas_t out, cap; + ui_draw_surface_t *cs; + pd_rect_t cr = { 0, 0, SPIN, SPIN }; + pd_context_t ctx; + pd_color_t a, b; + + ui_metrics.dpi = 96; + prime_spinner(scene.spinner, SPIN, SPIN, 0); + + cs = ui_draw_surface_create(); + ui_draw_surface_resize(cs, SPIN, SPIN); + ui_draw_capture(scene.content, cs); + make_argb(&cap, SPIN, SPIN); + pd_canvas_init(&ctx.canvas); + ctx.rect = cr; + ctx.with_alpha = true; + pd_canvas_quote(&ctx.canvas, &cap, &cr); + ui_draw_blend_surface(&ctx, cs, 0, 0, true, 1.0f); + ui_draw_surface_destroy(cs); + render_into(scene.spinner, &out, SPIN, SPIN); + + a = pd_canvas_get_pixel(&out, SPIN / 2, SPIN / 2); + b = pd_canvas_get_pixel(&cap, SPIN / 2, SPIN / 2); + ctest_equal_bool("identity: composited center should match capture", + is_color(a, RED_R, RED_G, RED_B) && + abs(a.r - b.r) < 4 && abs(a.g - b.g) < 4 && + abs(a.b - b.b) < 4, + true); + + pd_canvas_destroy(&out); + pd_canvas_destroy(&cap); +} + +/* 2. Rotation reaches the widget layer: the top-left mark moves to the + * top-right under a 90 degree turn (direction verified in the draw backend). */ +static void check_rotate90(void) +{ + pd_canvas_t out; + + ui_metrics.dpi = 96; + prime_spinner(scene.spinner, SPIN, SPIN, 90); + render_into(scene.spinner, &out, SPIN, SPIN); + + ctest_equal_bool( + "rotate: mark should move to the top-right", + is_color(pd_canvas_get_pixel(&out, SPIN - SPIN / 8, SPIN / 8), + GREEN_R, GREEN_G, GREEN_B), + true); + ctest_equal_bool( + "rotate: original mark corner should show the background", + is_color(pd_canvas_get_pixel(&out, SPIN / 8, SPIN / 8), RED_R, + RED_G, RED_B), + true); + + pd_canvas_destroy(&out); +} + +/* 2b. Declarative attrs route through proto->setattr: an `angle` attribute must + * rotate the composited content, and `running`/`speed` must be accepted without + * corrupting the render. */ +static void check_setattr(void) +{ + pd_canvas_t out; + + ui_metrics.dpi = 96; + prime_spinner(scene.spinner, SPIN, SPIN, 0); + + ui_widget_set_attr(scene.spinner, "running", "false"); + ui_widget_set_attr(scene.spinner, "speed", "2.0"); + ui_widget_set_attr(scene.spinner, "angle", "90"); + render_into(scene.spinner, &out, SPIN, SPIN); + + ctest_equal_bool( + "setattr: angle attribute should rotate the mark to the top-right", + is_color(pd_canvas_get_pixel(&out, SPIN - SPIN / 8, SPIN / 8), + GREEN_R, GREEN_G, GREEN_B), + true); + ctest_equal_bool( + "setattr: running/speed attrs should leave the mark corner clear", + is_color(pd_canvas_get_pixel(&out, SPIN / 8, SPIN / 8), RED_R, + RED_G, RED_B), + true); + + pd_canvas_destroy(&out); +} + +/* 3. set_content swaps the captured subtree: the center feature changes color. + */ +static void check_set_content(void) +{ + pd_canvas_t out; + ui_widget_t *alt, *child; + + ui_metrics.dpi = 96; + prime_spinner(scene.spinner, SPIN, SPIN, 0); + + alt = ui_create_widget("spinner-content"); + child = ui_create_widget(NULL); + ui_widget_add_class(child, "mark-alt"); + ui_widget_append(alt, child); + ui_spinner_set_content(scene.spinner, alt); + prime_spinner(scene.spinner, SPIN, SPIN, 0); + + render_into(scene.spinner, &out, SPIN, SPIN); + ctest_equal_bool( + "set_content: swapped content should recolor the center", + is_color(pd_canvas_get_pixel(&out, SPIN / 2, SPIN / 2), BLUE_R, + BLUE_G, BLUE_B), + true); + + pd_canvas_destroy(&out); +} + +/* 4. Clip: a content block larger than the spinner box is clipped to the box, + * so the target area outside the box keeps the base color. */ +static void check_clip(void) +{ + pd_canvas_t out; + ui_widget_t *bleed, *child; + + ui_metrics.dpi = 96; + bleed = ui_create_widget("spinner-content"); + child = ui_create_widget(NULL); + ui_widget_add_class(child, "bleed"); + ui_widget_append(bleed, child); + ui_spinner_set_content(scene.spinner, bleed); + prime_spinner(scene.spinner, 24, 24, 0); + + render_into(scene.spinner, &out, 64, 64); + ctest_equal_bool( + "clip: inside the box should show the feature", + is_color(pd_canvas_get_pixel(&out, 12, 12), RED_R, RED_G, RED_B), + true); + ctest_equal_bool("clip: outside the box should keep the base color", + is_base(pd_canvas_get_pixel(&out, 50, 50)), true); + + pd_canvas_destroy(&out); +} + +/* 5. Resize parity: after growing the box the buffers rebuild to the new size + * with no leftover from the previous smaller capture. */ +static void check_resize_parity(void) +{ + pd_canvas_t small, big; + ui_widget_t *content, *child; + + ui_metrics.dpi = 96; + content = ui_create_widget("spinner-content"); + ui_widget_add_class(content, "content-bg"); + child = ui_create_widget(NULL); + ui_widget_add_class(child, "mark"); + ui_widget_append(content, child); + ui_spinner_set_content(scene.spinner, content); + + prime_spinner(scene.spinner, 16, 16, 0); + render_into(scene.spinner, &small, 16, 16); + + prime_spinner(scene.spinner, 48, 48, 0); + render_into(scene.spinner, &big, 48, 48); + + ctest_equal_bool( + "resize: small buffer center should be the background", + is_color(pd_canvas_get_pixel(&small, 8, 12), RED_R, RED_G, RED_B), + true); + ctest_equal_bool( + "resize: growing the box should reveal fresh content at 48", + is_color(pd_canvas_get_pixel(&big, 44, 44), RED_R, RED_G, RED_B), + true); + ctest_equal_bool("resize: the mark should scale with the new buffer", + is_color(pd_canvas_get_pixel(&big, 4, 4), GREEN_R, + GREEN_G, GREEN_B), + true); + + pd_canvas_destroy(&small); + pd_canvas_destroy(&big); +} + +/* 6. DPI parity: the spinner rebuilds its capture buffer at the device scale. + * Both renders target the same 80x80 canvas. With the box at 40 logical px the + * buffer is 40 at 96 dpi but 80 at 192 dpi, so a corner pixel is base color at + * 96 (outside the small buffer) and the background color at 192 (inside the + * enlarged buffer), proving the offscreen capture tracks the DPI. */ +static void check_dpi_parity(void) +{ + pd_canvas_t c96, c192; + ui_widget_t *content; + + ui_metrics.dpi = 96; + content = ui_create_widget("spinner-content"); + ui_widget_add_class(content, "content-bg"); + ui_spinner_set_content(scene.spinner, content); + + prime_spinner(scene.spinner, SPIN, SPIN, 0); + render_into(scene.spinner, &c96, SPIN * 2, SPIN * 2); + + ui_metrics.dpi = 192; + prime_spinner(scene.spinner, SPIN, SPIN, 0); + render_into(scene.spinner, &c192, SPIN * 2, SPIN * 2); + + ctest_equal_bool("dpi: near-corner is background color at 96", + is_color(pd_canvas_get_pixel(&c96, SPIN / 4, SPIN / 4), + RED_R, RED_G, RED_B), + true); + ctest_equal_bool( + "dpi: near-corner is background color at 192", + is_color(pd_canvas_get_pixel(&c192, SPIN / 4, SPIN / 4), RED_R, + RED_G, RED_B), + true); + /* (78,78) is inside the 80 buffer but outside the 40 buffer. */ + ctest_equal_bool( + "dpi: 96 buffer stays 40 (corner is base color)", + is_base(pd_canvas_get_pixel(&c96, SPIN * 2 - 2, SPIN * 2 - 2)), + true); + ctest_equal_bool( + "dpi: 192 buffer grows to 80 (corner shows the background)", + is_color(pd_canvas_get_pixel(&c192, SPIN * 2 - 2, SPIN * 2 - 2), + RED_R, RED_G, RED_B), + true); + + ui_metrics.dpi = 96; + pd_canvas_destroy(&c96); + pd_canvas_destroy(&c192); +} + +/* 7. Lifecycle: the factory + default content path works and toggling running + * or destroying the widget is safe. */ +static void check_lifecycle(void) +{ + ui_widget_t *sp, *probe_content; + pd_canvas_t out; + + ui_metrics.dpi = 96; + sp = ui_create_spinner(); + ctest_equal_bool("lifecycle: factory should return a widget", + sp != NULL, true); + + ui_widget_append(ui_root(), sp); + prime_spinner(sp, SPIN, SPIN, 0); + + render_into(sp, &out, SPIN, SPIN); + /* The default content is a ring: assert its presence by counting stroke + * pixels across the box instead of pinning one sample point, so the + * check holds for the scaled geometry at a 40px box. */ + { + int x, y, ring_count = 0; + for (y = 0; y < SPIN; ++y) { + for (x = 0; x < SPIN; ++x) { + if (is_ring(pd_canvas_get_pixel(&out, x, y))) { + ring_count += 1; + } + } + } + ctest_equal_bool( + "lifecycle: default content should render a visible ring", + ring_count >= 24, true); + } + pd_canvas_destroy(&out); + + ui_spinner_set_running(sp, true); + ui_spinner_set_running(sp, false); + ui_spinner_set_running(sp, true); + + probe_content = ui_create_widget("spinner-content"); + ui_spinner_set_content(sp, probe_content); + ui_widget_destroy(sp); +} + +/* 8. Default ring (regression lock): a bare spinner has an intrinsic size and + * renders an inset ring whose stroke is present on the left, right and bottom + * but broken by a transparent gap at the top center, with a hollow middle. The + * ring is now inset from the box edge and drawn through the supersampled + * rotation path, so exact pixels drift; assert over counted regions instead. */ +static void check_default_ring(void) +{ + ui_widget_t *sp; + pd_canvas_t out; + int left, right, bottom, top_center, center, bottom_center, total; + + ui_metrics.dpi = 96; + sp = ui_create_spinner(); + ui_widget_append(ui_root(), sp); + /* No explicit resize: prove the default CSS width/height give the bare + * spinner a non-zero box so it renders at all. */ + ui_spinner_set_running(sp, false); + ui_spinner_set_angle(sp, 0); + ui_widget_request_refresh_style(sp); + ui_widget_each(sp, refresh_style_each, NULL); + ui_update(); + ui_update(); + ui_update(); + ctest_equal_bool( + "default ring: a bare spinner should have intrinsic " + "size", + sp->content_box.width >= 8.0f && sp->content_box.height >= 8.0f, + true); + + render_into(sp, &out, RING, RING); + total = count_ring(&out, 0, 0, RING, RING); + left = count_ring(&out, 0, 0, RING / 2, RING); + right = count_ring(&out, RING / 2, 0, RING, RING); + bottom = count_ring(&out, 0, RING / 2, RING, RING); + /* The gap is the top quarter of the ring; sample a center-width band of + * the upper half so the (present) left/right arcs do not pollute it. */ + top_center = count_ring(&out, RING / 2 - 3, 0, RING / 2 + 3, RING / 2); + center = count_ring(&out, RING / 2 - 2, RING / 2 - 2, RING / 2 + 2, + RING / 2 + 2); + bottom_center = + count_ring(&out, RING / 2 - 3, RING / 2, RING / 2 + 3, RING); + + ctest_equal_bool("default ring: the stroke should be visible", + total >= 16, true); + ctest_equal_bool("default ring: the left half should carry the stroke", + left > 0, true); + ctest_equal_bool("default ring: the right half should carry the stroke", + right > 0, true); + ctest_equal_bool("default ring: the bottom should carry the stroke", + bottom > 0, true); + /* Left and right halves are mirror arcs, so their counts stay balanced: + * a rotation off the border center would skew one side. */ + ctest_equal_bool("default ring: the stroke should be roughly symmetric", + abs(left - right) <= 6, true); + /* Top center is the gap: the base shows through, so it carries no + * stroke, while the opposite (bottom center) does. */ + ctest_equal_bool("default ring: the top center should be the gap", + top_center == 0, true); + ctest_equal_bool("default ring: the bottom center should carry the " + "stroke", + bottom_center > top_center, true); + /* The center of a ring is empty, never the stroke. */ + ctest_equal_bool("default ring: the center should stay hollow", + center == 0, true); + + ui_widget_destroy(sp); + pd_canvas_destroy(&out); +} + +/* 8b. Ring centering: at angle 0 the ring is inset and drawn with no rotation, + * so its stroke bounding box must sit equidistant from the left and right + * edges. This is only true when the rotation pivot (now the pixel center) + * coincides with the border engine's rounded-corner center; a half-pixel or + * geometric-center mismatch would shift the whole ring toward one edge. The + * non-zero margins also prove the inset leaves headroom for the AA so the ring + * is not clipped by the capture bounds. Measured at a 40px box: left margin 3, + * right margin 4. */ +static void check_ring_centered(void) +{ + ui_widget_t *sp; + pd_canvas_t out; + int left_margin, right_margin; + + ui_metrics.dpi = 96; + sp = ui_create_spinner(); + ui_widget_append(ui_root(), sp); + prime_spinner(sp, SPIN, SPIN, 0); + render_into(sp, &out, SPIN, SPIN); + + left_margin = ring_left_margin(&out, SPIN, SPIN); + right_margin = ring_right_margin(&out, SPIN, SPIN); + ctest_equal_bool("centered: the ring should not touch an edge (inset)", + left_margin >= 1 && right_margin >= 1, true); + ctest_equal_bool( + "centered: the ring should be equidistant from the left and right", + abs(left_margin - right_margin) <= 2, true); + + ui_widget_destroy(sp); + pd_canvas_destroy(&out); +} + +/* 8c. Pivot correctness via a rotation-invariant probe. A full (gap-free) ring + * drawn by the border engine is unchanged by rotation about its drawn centre, + * so its ink centroid must be IDENTICAL at every angle iff the rotation pivot + * equals the border-drawn centre (the pixel midpoint (w-1)/2). If the pivot is + * off -- e.g. the geometric box centre w/2 -- the ring orbits and the centroid + * sweeps by ~twice the offset. This is what the earlier "spread <= 2px" check + * was too loose to catch. Content is a custom full ring so the gap's asymmetry + * is not a factor. + */ +static void check_pivot_invariant(void) +{ + static const int angles[] = { 0, 45, 90, 135, 180, 225, 270, 315 }; + ui_widget_t *sp, *content, *ring; + float cxs[8], cys[8], bboxcx[8], bboxcy[8]; + int i, x, y; + pd_canvas_t out; + + ui_metrics.dpi = 96; + ui_load_css_string( + ".pivot-full-ring { position: absolute; box-sizing: border-box; " + "left: 15%; top: 15%; width: 70%; height: 70%; " + "border-style: solid; border-width: 3px; border-color: #e5484d; " + "border-radius: 9999px; }", + "pivot-full-ring"); + sp = ui_create_spinner(); + content = ui_create_widget("spinner-content"); + ring = ui_create_widget(NULL); + ui_widget_add_class(ring, "pivot-full-ring"); + ui_widget_append(content, ring); + ui_spinner_set_content(sp, content); + ui_widget_append(ui_root(), sp); + + for (i = 0; i < 8; ++i) { + double sx = 0, sy = 0; + int cnt = 0; + int bx0 = 999, bx1 = -1, by0 = 999, by1 = -1; + prime_spinner(sp, SPIN, SPIN, angles[i]); + render_into(sp, &out, SPIN, SPIN); + for (y = 0; y < SPIN; ++y) { + for (x = 0; x < SPIN; ++x) { + pd_color_t c = pd_canvas_get_pixel(&out, x, y); + if (c.a > 200 && c.r > 180 && c.g < 90 && + c.b < 90) { + sx += x; + sy += y; + cnt += 1; + if (x < bx0) + bx0 = x; + if (x > bx1) + bx1 = x; + if (y < by0) + by0 = y; + if (y > by1) + by1 = y; + } + } + } + pd_canvas_destroy(&out); + if (cnt < 1) { + cnt = 1; + } + cxs[i] = (float)(sx / cnt); + cys[i] = (float)(sy / cnt); + /* ink bounding-box centre: the "does the ring look centred" + * measure -- each edge's outer ink, not the averaged centroid. + */ + bboxcx[i] = (float)(bx0 + bx1) / 2.0f; + bboxcy[i] = (float)(by0 + by1) / 2.0f; + } + { + double mx = 0, my = 0; + float ex = (float)(SPIN - 1) / 2.0f; + for (i = 0; i < 8; ++i) { + mx += cxs[i]; + my += cys[i]; + } + mx /= 8.0; + my /= 8.0; + /* Averaging over a full rotation cancels the orbit radius and + * leaves the pivot's offset from the ring's drawn centre, which + * is what "is the ring centred?" really asks. The old per-angle + * spread check (<=2px) was far too loose to see this. */ + ctest_equal_bool( + "pivot: mean ring centroid must sit on the drawn centre", + (float)fabs(mx - ex) <= 0.25f && + (float)fabs(my - ex) <= 0.25f, + true); + } + { + float ex = (float)(SPIN - 1) / 2.0f; + float nminx = bboxcx[0], nmaxx = bboxcx[0]; + float nminy = bboxcy[0], nmaxy = bboxcy[0]; + for (i = 1; i < 8; ++i) { + if (bboxcx[i] < nminx) + nminx = bboxcx[i]; + if (bboxcx[i] > nmaxx) + nmaxx = bboxcx[i]; + if (bboxcy[i] < nminy) + nminy = bboxcy[i]; + if (bboxcy[i] > nmaxy) + nmaxy = bboxcy[i]; + } + /* The rendered ring's outer silhouette (its bounding-box + * centre) must be equidistant from all four edges -- i.e. on + * (w-1)/2 -- and identical at every angle. This is the + * pixel-level stand-in for "it looks centred", which the + * centroid alone (an averaged point) can hide. */ + ctest_equal_bool( + "pivot: ring ink bbox centre sits on the box centre", + (float)fabs(nminx - ex) <= 0.6f && + (float)fabs(nmaxx - ex) <= 0.6f && + (float)fabs(nminy - ex) <= 0.6f && + (float)fabs(nmaxy - ex) <= 0.6f, + true); + ctest_equal_bool( + "pivot: ring ink bbox centre is rotation-invariant", + (nmaxx - nminx) <= 1.0f && (nmaxy - nminy) <= 1.0f, true); + } + + ui_widget_destroy(sp); +} + +/* 9. Rotation sensitivity at a non-right angle: rotating the inset ring 45 + * degrees sweeps the gap off the top center and moves the stroke centroid + * horizontally. Both are geometric counts, so they hold despite the inset and + * the supersampled rotation resampling the exact arc pixels. Measured at a 40px + * box: centroid x 20.1 -> 16.5 (shifts left as the gap opens toward the + * top-right), top-center stroke 0 -> non-zero. */ +static void check_rotate45(void) +{ + ui_widget_t *sp; + pd_canvas_t a0, a45; + double cx0, cx45; + int top0, top45; + + ui_metrics.dpi = 96; + sp = ui_create_spinner(); + ui_widget_append(ui_root(), sp); + prime_spinner(sp, SPIN, SPIN, 0); + render_into(sp, &a0, SPIN, SPIN); + + ui_spinner_set_angle(sp, 45); + ui_widget_request_refresh_style(sp); + ui_widget_each(sp, refresh_style_each, NULL); + ui_update(); + ui_update(); + ui_update(); + render_into(sp, &a45, SPIN, SPIN); + + /* The gap is the transparent top border. It starts centered on top and + * a real fractional rotation carries it off that column, so the top + * center band goes from empty to stroked. */ + top0 = count_ring(&a0, SPIN / 2 - 6, 0, SPIN / 2 + 6, SPIN / 2); + top45 = count_ring(&a45, SPIN / 2 - 6, 0, SPIN / 2 + 6, SPIN / 2); + ctest_equal_bool("rotate45: the top center is the gap at angle 0", + top0 == 0, true); + ctest_equal_bool("rotate45: the gap should sweep off the top center", + top45 > top0, true); + /* The gap opening on the upper-right removes ink there, pulling the + * stroke centroid to the left; a fixed ring would not move. */ + cx0 = ring_centroid_x(&a0, SPIN, SPIN); + cx45 = ring_centroid_x(&a45, SPIN, SPIN); + ctest_equal_bool("rotate45: the stroke centroid should shift left", + cx0 - cx45 >= 2.0, true); + + ui_widget_destroy(sp); + pd_canvas_destroy(&a0); + pd_canvas_destroy(&a45); +} + +/* 10. step_angle: the extracted clock math wraps to [0, 360). */ +static void check_step_angle(void) +{ + ctest_equal_float("step_angle: quarter turn at 1 rps", + (float)ui_spinner_step_angle(0, 1.0, 0.25), 90.0f); + ctest_equal_float("step_angle: advance adds speed*dt*360", + (float)ui_spinner_step_angle(90, 1.0, 0.1), 126.0f); + ctest_equal_float("step_angle: should wrap past 360 back into range", + (float)ui_spinner_step_angle(350, 1.0, 0.05), 8.0f); + ctest_equal_bool("step_angle: wrapped angle stays in [0, 360)", + ui_spinner_step_angle(350, 1.0, 0.05) >= 0.0 && + ui_spinner_step_angle(350, 1.0, 0.05) < 360.0, + true); +} + +/* 11. HiDPI capture (regression for ui_widget_render re-anchor): a bare default + * spinner must still capture its ring when device scale is 1.5 (Windows 150%). + * Before the fix, offscreen capture of the off-origin content came out blank at + * scale != 1, so the doc-viewer showed nothing. Count ring pixels rather than + * pin one point, since the scaled geometry shifts sample positions. */ +static void check_hidpi_capture(void) +{ + pd_canvas_t out; + ui_widget_t *sp; + int x, y, ring_count = 0; + + ui_metrics.dpi = 144; + sp = ui_create_spinner(); + ui_widget_append(ui_root(), sp); + prime_spinner(sp, RING, RING, 0); + render_into(sp, &out, 24, 24); + for (y = 0; y < 24; ++y) { + for (x = 0; x < 24; ++x) { + if (is_ring(pd_canvas_get_pixel(&out, x, y))) { + ring_count += 1; + } + } + } + ctest_equal_bool("hidpi: default ring should be captured at scale 1.5", + ring_count >= 12, true); + pd_canvas_destroy(&out); + ui_widget_destroy(sp); + ui_metrics.dpi = 96; +} + +/* 12. Ring colour follows CSS `color`: the CSS engine has no `current-color`, + * so the widget copies the spinner's inherited colour into the ring's three + * visible borders (top stays transparent as the gap). A red `color` must put + * red-ish stroke pixels on the ring where the default grey would not match. */ +static void check_color_stroke(void) +{ + ui_widget_t *sp; + pd_canvas_t out; + int x, y, red_count = 0, ring_count = 0; + + ui_metrics.dpi = 96; + sp = ui_create_spinner(); + ui_widget_set_style_string(sp, "color", "#ff0000"); + ui_widget_append(ui_root(), sp); + prime_spinner(sp, SPIN, SPIN, 0); + render_into(sp, &out, SPIN, SPIN); + for (y = 0; y < SPIN; ++y) { + for (x = 0; x < SPIN; ++x) { + pd_color_t c = pd_canvas_get_pixel(&out, x, y); + if (is_redish(c)) { + red_count += 1; + } + if (is_ring(c)) { + ring_count += 1; + } + } + } + /* Measured at 40px/2px stroke/96dpi: 158 red-ish pixels, so the + * threshold keeps a real stroke, not a stray edge. */ + ctest_equal_bool("color: ring should be tinted by `color`", + red_count >= 64, true); + ctest_equal_bool("color: the default grey stroke should be gone", + ring_count == 0, true); + pd_canvas_destroy(&out); + ui_widget_destroy(sp); +} + +/* Render only the sub-region [rx,ry,rw,rh] (widget-canvas coords) into a + * rw×rh canvas. Mirrors how the renderer hands a widget a partial dirty rect: + * the canvas quote covers the whole small buffer while paint->rect names the + * region in the widget's own canvas coordinates. */ +static void render_region(ui_widget_t *sp, pd_canvas_t *out, int rx, int ry, + int rw, int rh) +{ + pd_rect_t q = { 0, 0, rw, rh }; + pd_context_t paint; + + make_argb(out, rw, rh); + pd_canvas_fill(out, BASE); + pd_canvas_init(&paint.canvas); + paint.rect.x = rx; + paint.rect.y = ry; + paint.rect.width = rw; + paint.rect.height = rh; + paint.with_alpha = true; + pd_canvas_quote(&paint.canvas, out, &q); + ui_widget_render(sp, &paint); +} + +/* 12. Partial repaint (regression): rendering just the bottom half of the + * spinner must equal the same bottom half of a full render. The previous blit + * of the whole box surface at a negative target offset made the bottom half + * show the top half's content under a partial dirty rect. */ +static void check_partial_repaint(void) +{ + pd_canvas_t full, part; + int x, y, diff = 0; + + ui_metrics.dpi = 96; + prime_spinner(scene.spinner, SPIN, SPIN, 45); + render_region(scene.spinner, &full, 0, 0, SPIN, SPIN); + render_region(scene.spinner, &part, 0, SPIN / 2, SPIN, SPIN / 2); + for (y = 0; y < SPIN / 2; ++y) { + for (x = 0; x < SPIN; ++x) { + pd_color_t a = + pd_canvas_get_pixel(&full, x, SPIN / 2 + y); + pd_color_t b = pd_canvas_get_pixel(&part, x, y); + if (abs((int)a.value - (int)b.value) != 0) { + diff += 1; + } + } + } + ctest_equal_bool( + "partial repaint: bottom half must match the same region of a " + "full render", + diff == 0, true); + pd_canvas_destroy(&full); + pd_canvas_destroy(&part); +} + +/* 13. Partial repaint at 144 dpi (cached-layer regression): the same + * half-versus-full comparison as above must hold at scale 1.5, where the + * physical box (60 px) differs from the logical one (40 px) and the cache is + * presented from the display surface the layer keeps between paints. */ +static void check_partial_repaint_hidpi(void) +{ + pd_canvas_t full, part; + int x, y, diff = 0; + int phys = 60; /* ceil(40 * 1.5) */ + + ui_metrics.dpi = 144; + prime_spinner(scene.spinner, SPIN, SPIN, 45); + render_region(scene.spinner, &full, 0, 0, phys, phys); + render_region(scene.spinner, &part, 0, phys / 2, phys, phys / 2); + for (y = 0; y < phys / 2; ++y) { + for (x = 0; x < phys; ++x) { + pd_color_t a = + pd_canvas_get_pixel(&full, x, phys / 2 + y); + pd_color_t b = pd_canvas_get_pixel(&part, x, y); + if (abs((int)a.value - (int)b.value) != 0) { + diff += 1; + } + } + } + ctest_equal_bool( + "partial repaint hidpi: bottom half must match the same region of " + "a full render at 144 dpi", + diff == 0, true); + pd_canvas_destroy(&full); + pd_canvas_destroy(&part); + ui_metrics.dpi = 96; +} + +/* 14. Rotate centering (invariant test): a ring drawn by the border engine and + * rotated about the pixel center must maintain constant outer contour radius + * (rmax) and centroid offset from center (crad) across every angle. If the + * rotation pivot does not coincide with the ring center, these quantities would + * oscillate with angle. Tests both 96 and 144 dpi to catch half-pixel bias at + * non-unity scale. Uses a 64px logical box for sufficient ring pixels. */ +static void check_rotate_centering(void) +{ + ui_widget_t *sp; + pd_canvas_t out; + int angles[] = { 0, 45, 90, 135, 180, 225, 270, 315 }; + int nangles = 8; + int ai, x, y; + /* 96 dpi: logical 64, physical 64; center (31.5, 31.5) */ + int W96 = 64; + double cx96 = (W96 - 1) / 2.0; + double rmax96[8], crad96[8]; + int count96[8]; + /* 144 dpi: logical 64, physical 96; center (47.5, 47.5) */ + int W144 = 96; /* ceil(64 * 1.5) */ + double cx144 = (W144 - 1) / 2.0; + double rmax144[8], crad144[8]; + int count144[8]; + + double rmax_lo, rmax_hi, crad_lo, crad_hi; + double rmax_spread, crad_spread; + int cnt_lo, cnt_hi; + + sp = ui_create_spinner(); + ui_widget_append(ui_root(), sp); + + /* ---- 96 dpi ---- */ + ui_metrics.dpi = 96; + for (ai = 0; ai < nangles; ++ai) { + double sx = 0.0, sy = 0.0, rmax = 0.0; + int cnt = 0; + + prime_spinner(sp, (float)W96, (float)W96, (double)angles[ai]); + render_region(sp, &out, 0, 0, W96, W96); + for (y = 0; y < W96; ++y) { + for (x = 0; x < W96; ++x) { + if (is_ring(pd_canvas_get_pixel(&out, x, y))) { + double dx = x - cx96; + double dy = y - cx96; + double r = sqrt(dx * dx + dy * dy); + sx += x; + sy += y; + cnt += 1; + if (r > rmax) { + rmax = r; + } + } + } + } + rmax96[ai] = rmax; + crad96[ai] = cnt > 0 + ? sqrt((sx / cnt - cx96) * (sx / cnt - cx96) + + (sy / cnt - cx96) * (sy / cnt - cx96)) + : -1.0; + count96[ai] = cnt; + pd_canvas_destroy(&out); + } + + printf(" [rotate centering 96dpi] angle rmax crad count\n"); + for (ai = 0; ai < nangles; ++ai) { + printf(" %3d %8.3f %7.3f %5d\n", angles[ai], rmax96[ai], + crad96[ai], count96[ai]); + } + + /* Compute spreads for 96 dpi */ + rmax_lo = rmax_hi = rmax96[0]; + crad_lo = crad_hi = crad96[0]; + cnt_lo = cnt_hi = count96[0]; + for (ai = 1; ai < nangles; ++ai) { + if (rmax96[ai] < rmax_lo) + rmax_lo = rmax96[ai]; + if (rmax96[ai] > rmax_hi) + rmax_hi = rmax96[ai]; + if (crad96[ai] < crad_lo) + crad_lo = crad96[ai]; + if (crad96[ai] > crad_hi) + crad_hi = crad96[ai]; + if (count96[ai] < cnt_lo) + cnt_lo = count96[ai]; + if (count96[ai] > cnt_hi) + cnt_hi = count96[ai]; + } + rmax_spread = rmax_hi - rmax_lo; + crad_spread = crad_hi - crad_lo; + + ctest_equal_bool("centering 96: rmax spread should be <= 2 px", + rmax_spread <= 2.0, true); + ctest_equal_bool("centering 96: crad spread should be <= 2 px", + crad_spread <= 2.0, true); + ctest_equal_bool("centering 96: count spread should be <= 50%", + (double)(cnt_hi - cnt_lo) <= (double)cnt_hi * 0.50, + true); + + /* ---- 144 dpi ---- */ + ui_metrics.dpi = 144; + for (ai = 0; ai < nangles; ++ai) { + double sx = 0.0, sy = 0.0, rmax = 0.0; + int cnt = 0; + + prime_spinner(sp, 64.0f, 64.0f, (double)angles[ai]); + render_region(sp, &out, 0, 0, W144, W144); + for (y = 0; y < W144; ++y) { + for (x = 0; x < W144; ++x) { + if (is_ring(pd_canvas_get_pixel(&out, x, y))) { + double dx = x - cx144; + double dy = y - cx144; + double r = sqrt(dx * dx + dy * dy); + sx += x; + sy += y; + cnt += 1; + if (r > rmax) { + rmax = r; + } + } + } + } + rmax144[ai] = rmax; + crad144[ai] = + cnt > 0 ? sqrt((sx / cnt - cx144) * (sx / cnt - cx144) + + (sy / cnt - cx144) * (sy / cnt - cx144)) + : -1.0; + count144[ai] = cnt; + pd_canvas_destroy(&out); + } + + printf(" [rotate centering 144dpi] angle rmax crad count\n"); + for (ai = 0; ai < nangles; ++ai) { + printf(" %3d %8.3f %7.3f %5d\n", angles[ai], rmax144[ai], + crad144[ai], count144[ai]); + } + + /* Compute spreads for 144 dpi */ + rmax_lo = rmax_hi = rmax144[0]; + crad_lo = crad_hi = crad144[0]; + cnt_lo = cnt_hi = count144[0]; + for (ai = 1; ai < nangles; ++ai) { + if (rmax144[ai] < rmax_lo) + rmax_lo = rmax144[ai]; + if (rmax144[ai] > rmax_hi) + rmax_hi = rmax144[ai]; + if (crad144[ai] < crad_lo) + crad_lo = crad144[ai]; + if (crad144[ai] > crad_hi) + crad_hi = crad144[ai]; + if (count144[ai] < cnt_lo) + cnt_lo = count144[ai]; + if (count144[ai] > cnt_hi) + cnt_hi = count144[ai]; + } + rmax_spread = rmax_hi - rmax_lo; + crad_spread = crad_hi - crad_lo; + + ctest_equal_bool("centering 144: rmax spread should be <= 2 px", + rmax_spread <= 2.0, true); + ctest_equal_bool("centering 144: crad spread should be <= 2 px", + crad_spread <= 2.0, true); + ctest_equal_bool("centering 144: count spread should be <= 50%", + (double)(cnt_hi - cnt_lo) <= (double)cnt_hi * 0.50, + true); + + ui_metrics.dpi = 96; + ui_widget_destroy(sp); +} + +/* 15. Text size under supersampled capture (regression): a text layer is + * rasterised at the device scale active when it was laid out, so a self-drawn + * widget that captures its subtree at a boosted scale must rebuild the layer in + * paint. Otherwise the cached 1x glyph bitmaps are composited into a 2x backing + * and the text shrinks by half. Compare the text composited through the + * spinner's capture with the same text rendered on the normal path; their ink + * extents must agree. The comparison is between two render paths, so it holds + * regardless of the actual font metrics. */ +static void text_ink_bbox(pd_canvas_t *out, int w, int h, int *bx, int *by, + int *bw, int *bh) +{ + int x, y, minx = w, miny = h, maxx = -1, maxy = -1; + + for (y = 0; y < h; ++y) { + for (x = 0; x < w; ++x) { + if (!is_base(pd_canvas_get_pixel(out, x, y))) { + if (x < minx) + minx = x; + if (x > maxx) + maxx = x; + if (y < miny) + miny = y; + if (y > maxy) + maxy = y; + } + } + } + *bx = maxx >= 0 ? minx : -1; + *by = maxy >= 0 ? miny : -1; + *bw = maxx >= 0 ? maxx - minx + 1 : 0; + *bh = maxy >= 0 ? maxy - miny + 1 : 0; +} + +static void check_text_capture_size(void) +{ + static const char *css = + ".t-cap-demo spinner-content { display: flex; align-items: center; " + "justify-content: center; }" + ".cap-text { white-space: nowrap; font-size: 18px; " + "color: #e5484d; }" + ".t-cap-plain { display: flex; align-items: center; " + "justify-content: center; }"; + ui_widget_t *sp, *content, *txt, *plain, *ptxt; + pd_canvas_t cap, ref; + int cx, cy, cw, ch, rx, ry, rw, rh; + + ui_metrics.dpi = 96; + ui_load_css_string(css, "t-cap-demo"); + + /* Supersampled path: text inside a spinner's captured content. */ + sp = ui_create_spinner(); + ui_widget_add_class(sp, "t-cap-demo"); + content = ui_create_widget("spinner-content"); + txt = ui_create_widget("text"); + ui_text_set_content(txt, "Hello"); + ui_widget_add_class(txt, "cap-text"); + ui_widget_append(content, txt); + ui_spinner_set_content(sp, content); + ui_widget_append(ui_root(), sp); + prime_spinner(sp, 96, 32, 0); + render_into(sp, &cap, 96, 32); + + /* Reference path: the same text laid out and rendered normally. */ + plain = ui_create_widget(NULL); + ui_widget_add_class(plain, "t-cap-plain"); + ptxt = ui_create_widget("text"); + ui_text_set_content(ptxt, "Hello"); + ui_widget_add_class(ptxt, "cap-text"); + ui_widget_append(plain, ptxt); + ui_widget_append(ui_root(), plain); + ui_widget_resize(plain, 96, 32); + ui_widget_request_refresh_style(plain); + ui_widget_each(plain, refresh_style_each, NULL); + ui_update(); + ui_update(); + ui_update(); + render_into(plain, &ref, 96, 32); + + text_ink_bbox(&cap, 96, 32, &cx, &cy, &cw, &ch); + text_ink_bbox(&ref, 96, 32, &rx, &ry, &rw, &rh); + + ctest_equal_bool("text capture: the captured text should be visible", + cw > 0 && ch > 0, true); + ctest_equal_bool("text capture: width should match a normal render", + cw > 0 && abs(cw - rw) <= 2, true); + ctest_equal_bool("text capture: height should match a normal render", + ch > 0 && abs(ch - rh) <= 2, true); + ctest_equal_bool("text capture: left margin should match", + cw > 0 && abs(cx - rx) <= 2, true); + + pd_canvas_destroy(&cap); + pd_canvas_destroy(&ref); + ui_widget_destroy(sp); + ui_widget_destroy(plain); +} + +/* 16. Capture-cache invalidation (regression): growing the custom content + * subtree must drop the cached capture rather than keep rotating the stale + * bitmap, even though nothing about the spinner itself changed. */ +static void check_content_cache_invalidates(void) +{ + ui_widget_t *sp, *content, *mark; + pd_canvas_t empty, grown; + + ui_metrics.dpi = 96; + sp = ui_create_spinner(); + content = ui_create_widget("spinner-content"); + ui_spinner_set_content(sp, content); + ui_widget_append(ui_root(), sp); + prime_spinner(sp, SPIN, SPIN, 0); + render_into(sp, &empty, SPIN, SPIN); + + /* Child-list mutation: an opaque blue block fills the content box. No + * style refresh follows, so here only the mutation observer can drop + * the cached capture. */ + mark = ui_create_widget(NULL); + ui_widget_add_class(mark, "mark-alt"); + ui_widget_append(content, mark); + ui_update(); + ui_update(); + render_into(sp, &grown, SPIN, SPIN); + + ctest_equal_bool( + "cache: an empty content should show the base", + is_base(pd_canvas_get_pixel(&empty, SPIN / 2, SPIN / 2)), true); + ctest_equal_bool( + "cache: appending a child must re-capture", + is_color(pd_canvas_get_pixel(&grown, SPIN / 2, SPIN / 2), BLUE_R, + BLUE_G, BLUE_B), + true); + + pd_canvas_destroy(&empty); + pd_canvas_destroy(&grown); + ui_widget_destroy(sp); +} + +void test_spinner(void) +{ + lcui_init(); + spinner_scene_build(&scene); + + ctest_describe("spinner identity", check_identity); + ctest_describe("spinner rotate", check_rotate90); + ctest_describe("spinner setattr", check_setattr); + ctest_describe("spinner set content", check_set_content); + ctest_describe("spinner clip", check_clip); + ctest_describe("spinner resize parity", check_resize_parity); + ctest_describe("spinner dpi parity", check_dpi_parity); + ctest_describe("spinner lifecycle", check_lifecycle); + ctest_describe("spinner pivot invariant", check_pivot_invariant); + ctest_describe("spinner default ring", check_default_ring); + ctest_describe("spinner ring centered", check_ring_centered); + ctest_describe("spinner rotate 45", check_rotate45); + ctest_describe("spinner step angle", check_step_angle); + ctest_describe("spinner hidpi capture", check_hidpi_capture); + ctest_describe("spinner partial repaint", check_partial_repaint); + ctest_describe("spinner partial repaint hidpi", + check_partial_repaint_hidpi); + ctest_describe("spinner rotate centering", check_rotate_centering); + ctest_describe("spinner color stroke", check_color_stroke); + ctest_describe("spinner text capture", check_text_capture_size); + ctest_describe("spinner content cache", + check_content_cache_invalidates); + + ui_metrics.dpi = 96; + lcui_destroy(); +} diff --git a/tests/integration/test_spinner_perf.c b/tests/integration/test_spinner_perf.c new file mode 100644 index 00000000..4198cd7b --- /dev/null +++ b/tests/integration/test_spinner_perf.c @@ -0,0 +1,141 @@ +/* + * tests/integration/test_spinner_perf.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include +#include +#include "../scenes/spinner_perf_scene.h" + +/* The scene is deliberately heavy (200 spinners plus two 400x400 text + * spinners, all supersampled), so a single timed frame per scroll offset keeps + * the test bearable. The printed ms/frame is the point: it is the rendering + * cost the preview shows as stutter. */ +#define PERF_FRAMES 1 +#define PERF_OFFSETS 3 + +static spinner_perf_scene_t scene; +static pd_canvas_t canvas; + +static size_t render_box(void) +{ + pd_rect_t rect = { 0, 0, (int)SPINNER_PERF_BOX_W, + (int)SPINNER_PERF_BOX_H }; + pd_context_t paint; + + pd_canvas_fill(&canvas, pd_rgb(255, 255, 255)); + pd_canvas_init(&paint.canvas); + paint.rect = rect; + paint.with_alpha = true; + pd_canvas_quote(&paint.canvas, &canvas, &rect); + return ui_widget_render(scene.box, &paint); +} + +/* Count pixels the scene actually drew, to prove the frame is non-empty. */ +static int count_ink(void) +{ + int x, y, n = 0; + + for (y = 0; y < (int)SPINNER_PERF_BOX_H; ++y) { + for (x = 0; x < (int)SPINNER_PERF_BOX_W; ++x) { + pd_color_t p = pd_canvas_get_pixel(&canvas, (unsigned)x, + (unsigned)y); + if (p.r < 248 || p.g < 248 || p.b < 248) { + n += 1; + } + } + } + return n; +} + +/* How many widgets the frame actually rendered. This is the real measure of + * how much work the scroll offset removes: offscreen spinners are culled. */ +/* (counted via the return value of ui_widget_render below) */ + +/* Time one animated frame at the given scroll offset. */ +static double measure_frame_ms(float scroll_top, int *ink, size_t *widgets) +{ + clock_t t0, t1; + size_t rendered = 0; + int f; + + ui_scrollarea_set_scroll_top(scene.scrollarea, scroll_top); + ui_update(); + + t0 = clock(); + for (f = 0; f < PERF_FRAMES; ++f) { + spinner_perf_scene_advance(&scene, 37.0); + ui_update(); + rendered = render_box(); + } + t1 = clock(); + *ink = count_ink(); + *widgets = rendered; + return (double)(t1 - t0) * 1000.0 / + ((double)CLOCKS_PER_SEC * PERF_FRAMES); +} + +void test_spinner_perf(void) +{ + float max_scroll, offsets[PERF_OFFSETS]; + int i; + + lcui_init(); + spinner_perf_scene_build(&scene); + /* Drive the animation from the test instead of a live frame clock. */ + for (i = 0; i < scene.count; ++i) { + ui_spinner_set_running(scene.spinners[i], false); + } + for (i = 0; i < SPINNER_PERF_BIG_COUNT; ++i) { + ui_spinner_set_running(scene.big[i], false); + } + ui_widget_request_refresh_style(scene.box); + ui_update(); + ui_update(); + + pd_canvas_init(&canvas); + canvas.color_type = PD_COLOR_TYPE_ARGB; + pd_canvas_create(&canvas, (unsigned)SPINNER_PERF_BOX_W, + (unsigned)SPINNER_PERF_BOX_H); + + max_scroll = ui_scrollarea_get_scroll_height(scene.scrollarea) - + SPINNER_PERF_VIEW_H; + if (max_scroll < 0.0f) { + max_scroll = 0.0f; + } + offsets[0] = 0.0f; + offsets[1] = max_scroll * 0.5f; + offsets[2] = max_scroll; + + /* One untimed warm-up frame builds every spinner's cached layer. */ + render_box(); + + printf(" [spinner perf] %d small x 40x40, %d big x 400x400 text, " + "%d frame(s)/offset, scroll range %.0f px\n", + SPINNER_PERF_COUNT, SPINNER_PERF_BIG_COUNT, PERF_FRAMES, + max_scroll); + printf(" scroll_top rendered ink_px ms/frame\n"); + for (i = 0; i < PERF_OFFSETS; ++i) { + int ink = 0; + size_t widgets = 0; + double ms = measure_frame_ms(offsets[i], &ink, &widgets); + + printf(" %10.1f %8lu %6d %8.1f\n", offsets[i], + (unsigned long)widgets, ink, ms); + ctest_equal_bool( + "spinner perf: every offset should render something", + ink > 0, true); + } + + pd_canvas_destroy(&canvas); + ui_widget_destroy(scene.box); + lcui_destroy(); +} diff --git a/tests/main.c b/tests/main.c index f55ab97f..cfeea987 100644 --- a/tests/main.c +++ b/tests/main.c @@ -42,6 +42,10 @@ extern void test_badge(void); extern void test_alert(void); extern void test_item(void); extern void test_empty(void); +extern void test_draw_backend(void); +extern void test_spinner(void); +extern void test_spinner_perf(void); +extern void test_shimmer(void); static const ctest_suite_t suites[] = { { "settings", test_settings }, @@ -74,6 +78,10 @@ static const ctest_suite_t suites[] = { { "alert", test_alert }, { "item", test_item }, { "empty", test_empty }, + { "draw backend", test_draw_backend }, + { "spinner", test_spinner }, + { "spinner perf", test_spinner_perf }, + { "shimmer", test_shimmer }, { NULL, NULL } }; diff --git a/tests/previews/preview_shimmer.c b/tests/previews/preview_shimmer.c new file mode 100644 index 00000000..544aa622 --- /dev/null +++ b/tests/previews/preview_shimmer.c @@ -0,0 +1,24 @@ +/* + * tests/previews/preview_shimmer.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include "../scenes/shimmer_scene.h" + +int main(int argc, char **argv) +{ + shimmer_scene_t scene; + + lcui_init(); + shimmer_scene_build(&scene); + ui_widget_set_title(ui_root(), L"shimmer preview"); + return lcui_main(); +} diff --git a/tests/previews/preview_spinner.c b/tests/previews/preview_spinner.c new file mode 100644 index 00000000..46296588 --- /dev/null +++ b/tests/previews/preview_spinner.c @@ -0,0 +1,24 @@ +/* + * tests/previews/preview_spinner.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include "../scenes/spinner_scene.h" + +int main(int argc, char **argv) +{ + spinner_scene_t scene; + + lcui_init(); + spinner_scene_build(&scene); + ui_widget_set_title(ui_root(), L"spinner preview"); + return lcui_main(); +} diff --git a/tests/previews/preview_spinner_perf.c b/tests/previews/preview_spinner_perf.c new file mode 100644 index 00000000..1f1c94f1 --- /dev/null +++ b/tests/previews/preview_spinner_perf.c @@ -0,0 +1,46 @@ +/* + * tests/previews/preview_spinner_perf.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include +#include "../scenes/spinner_perf_scene.h" + +static spinner_perf_scene_t scene; +static uint64_t last_ts; +static float smoothed_ms; + +/* Sample the engine's FPS and the frame interval and show them in the status + * overlay, so scrolling to change the number of visible spinners makes the + * rendering cost visible. */ +static void on_frame(uint64_t timestamp, void *data) +{ + char buf[64]; + float dt = last_ts ? (float)(timestamp - last_ts) : 0.0f; + + last_ts = timestamp; + smoothed_ms = smoothed_ms * 0.85f + dt * 0.15f; + snprintf(buf, sizeof(buf), "FPS %u %.1f ms/frame", lcui_get_fps(), + smoothed_ms); + ui_text_set_content(scene.status, buf); + lcui_request_frame(on_frame, data); +} + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + lcui_init(); + ui_widget_set_title(ui_root(), L"spinner perf preview"); + spinner_perf_scene_build(&scene); + lcui_request_frame(on_frame, NULL); + return lcui_main(); +} diff --git a/tests/previews/xmake.lua b/tests/previews/xmake.lua index d74c72b7..383ad21b 100644 --- a/tests/previews/xmake.lua +++ b/tests/previews/xmake.lua @@ -2,6 +2,9 @@ local previews = { "flex_layout", "block_layout", "widget_opacity", + "spinner", + "spinner_perf", + "shimmer", "scrollbar", "cursor" } diff --git a/tests/scenes/shimmer_scene.c b/tests/scenes/shimmer_scene.c new file mode 100644 index 00000000..df228e27 --- /dev/null +++ b/tests/scenes/shimmer_scene.c @@ -0,0 +1,29 @@ +/* + * tests/scenes/shimmer_scene.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include "shimmer_scene.h" + +void shimmer_scene_build(shimmer_scene_t *scene) +{ + ui_widget_t *pack, *root; + + pack = ui_load_xml_file("test_shimmer.xml"); + root = ui_root(); + ui_widget_append(root, pack); + ui_widget_unwrap(pack); + scene->shimmer = ui_get_widget("sh"); + scene->content = ui_get_widget("sc"); + /* The shimmer box size is set in code (not via an ID CSS rule) so that + * tests can resize it with ui_widget_resize without cascade fights, + * and so the preview has a visible default box. */ + ui_widget_resize(scene->shimmer, 80, 80); +} diff --git a/tests/scenes/shimmer_scene.h b/tests/scenes/shimmer_scene.h new file mode 100644 index 00000000..2c5b82c2 --- /dev/null +++ b/tests/scenes/shimmer_scene.h @@ -0,0 +1,24 @@ +/* + * tests/scenes/shimmer_scene.h + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#ifndef LCUI_TESTS_SCENES_SHIMMER_SCENE_H +#define LCUI_TESTS_SCENES_SHIMMER_SCENE_H + +#include + +typedef struct shimmer_scene { + ui_widget_t *shimmer; + ui_widget_t *content; +} shimmer_scene_t; + +void shimmer_scene_build(shimmer_scene_t *scene); + +#endif /* LCUI_TESTS_SCENES_SHIMMER_SCENE_H */ diff --git a/tests/scenes/spinner_perf_scene.c b/tests/scenes/spinner_perf_scene.c new file mode 100644 index 00000000..26a57188 --- /dev/null +++ b/tests/scenes/spinner_perf_scene.c @@ -0,0 +1,138 @@ +/* + * tests/scenes/spinner_perf_scene.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include +#include +#include "spinner_perf_scene.h" + +static const char *spinner_perf_scene_css = "\ +.perf-box {\ + position: relative;\ + width: 848px;\ + height: 632px;\ +}\ +.perf-scrollarea {\ + position: absolute;\ + left: 0;\ + top: 0;\ + right: 0;\ + bottom: 0;\ +}\ +.perf-content {\ + display: flex;\ + flex-wrap: wrap;\ + align-content: flex-start;\ +}\ +.perf-small {\ + width: 40px;\ + height: 40px;\ + margin: 4px;\ +}\ +.perf-big {\ + width: 400px;\ + height: 400px;\ + margin: 4px;\ +}\ +.perf-big spinner-content {\ + display: flex;\ + align-items: center;\ + justify-content: center;\ +}\ +.perf-big spinner-content text {\ + width: 340px;\ + white-space: normal;\ + text-align: center;\ + font-size: 20px;\ + color: #2f6fb3;\ +}\ +.perf-status {\ + position: absolute;\ + right: 8px;\ + top: 8px;\ + padding: 4px 8px;\ + background-color: rgba(0, 0, 0, 0.65);\ + color: #ffffff;\ + font-size: 14px;\ +}\ +"; + +static const char *spinner_perf_big_text = + "Generating response...\n" + "Please wait, this may take a moment.\n" + "We are preparing your results."; + +static ui_widget_t *create_big_spinner(void) +{ + ui_widget_t *spinner = ui_create_spinner(); + ui_widget_t *content = ui_create_widget("spinner-content"); + ui_widget_t *text = ui_create_widget("text"); + + ui_text_set_content(text, spinner_perf_big_text); + ui_widget_append(content, text); + ui_widget_add_class(spinner, "perf-big"); + ui_spinner_set_content(spinner, content); + return spinner; +} + +void spinner_perf_scene_build(spinner_perf_scene_t *scene) +{ + ui_widget_t *vscrollbar; + int i; + + memset(scene, 0, sizeof(*scene)); + ui_widget_resize(ui_root(), SPINNER_PERF_BOX_W, SPINNER_PERF_BOX_H); + ui_load_css_string(spinner_perf_scene_css, __FILE__); + + scene->box = ui_create_widget(NULL); + ui_widget_add_class(scene->box, "perf-box"); + ui_root_append(scene->box); + + scene->scrollarea = ui_create_scrollarea(); + ui_widget_add_class(scene->scrollarea, "perf-scrollarea"); + scene->content = ui_create_scrollarea_content(); + ui_widget_add_class(scene->content, "perf-content"); + + for (i = 0; i < SPINNER_PERF_COUNT; ++i) { + scene->spinners[i] = ui_create_spinner(); + ui_widget_add_class(scene->spinners[i], "perf-small"); + ui_widget_append(scene->content, scene->spinners[i]); + } + scene->count = SPINNER_PERF_COUNT; + + for (i = 0; i < SPINNER_PERF_BIG_COUNT; ++i) { + scene->big[i] = create_big_spinner(); + ui_widget_append(scene->content, scene->big[i]); + } + + vscrollbar = ui_create_scrollbar(); + ui_widget_append(scene->scrollarea, scene->content); + ui_widget_append(scene->scrollarea, vscrollbar); + ui_widget_append(scene->box, scene->scrollarea); + + scene->status = ui_create_widget("text"); + ui_widget_add_class(scene->status, "perf-status"); + ui_text_set_content(scene->status, "FPS --"); + ui_widget_append(scene->box, scene->status); +} + +void spinner_perf_scene_advance(spinner_perf_scene_t *scene, double degrees) +{ + int i; + + scene->angle = fmod(scene->angle + degrees, 360.0); + for (i = 0; i < scene->count; ++i) { + ui_spinner_set_angle(scene->spinners[i], scene->angle); + } + for (i = 0; i < SPINNER_PERF_BIG_COUNT; ++i) { + ui_spinner_set_angle(scene->big[i], scene->angle); + } +} diff --git a/tests/scenes/spinner_perf_scene.h b/tests/scenes/spinner_perf_scene.h new file mode 100644 index 00000000..6946d0e0 --- /dev/null +++ b/tests/scenes/spinner_perf_scene.h @@ -0,0 +1,49 @@ +/* + * tests/scenes/spinner_perf_scene.h + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found + * in the LICENSE.TXT file in the root directory of this source tree. + */ + +#ifndef LCUI_TESTS_SCENES_SPINNER_PERF_SCENE_H +#define LCUI_TESTS_SCENES_SPINNER_PERF_SCENE_H + +#include + +/* A rendering stress scene: many small spinners plus a couple of large + * text-rotating spinners inside a scrollarea, so the number of visible + * self-drawn widgets can be varied by scrolling. Shared by the perf test and + * the interactive preview. */ +#define SPINNER_PERF_COUNT 200 +#define SPINNER_PERF_BIG_COUNT 2 + +/* The outer box is deliberately larger than the 800x600 viewport so the + * vertical scrollbar and the frame do not eat into the visible area. */ +#define SPINNER_PERF_BOX_W 848.0f +#define SPINNER_PERF_BOX_H 632.0f +#define SPINNER_PERF_VIEW_W 800.0f +#define SPINNER_PERF_VIEW_H 600.0f + +typedef struct spinner_perf_scene { + ui_widget_t *box; /* outer container appended to the root */ + ui_widget_t *scrollarea; /* holds the scrollable content */ + ui_widget_t *content; /* scrollarea-content */ + ui_widget_t *status; /* text overlay for preview readouts */ + ui_widget_t *spinners[SPINNER_PERF_COUNT]; + ui_widget_t *big[SPINNER_PERF_BIG_COUNT]; + int count; + double angle; +} spinner_perf_scene_t; + +/* Build the scene into the root and size the root to the outer box. */ +void spinner_perf_scene_build(spinner_perf_scene_t *scene); + +/* Advance every spinner by `degrees` so a frame re-renders all of them. The + * perf test drives frames with this instead of a running frame clock. */ +void spinner_perf_scene_advance(spinner_perf_scene_t *scene, double degrees); + +#endif /* LCUI_TESTS_SCENES_SPINNER_PERF_SCENE_H */ diff --git a/tests/scenes/spinner_scene.c b/tests/scenes/spinner_scene.c new file mode 100644 index 00000000..847d3ba5 --- /dev/null +++ b/tests/scenes/spinner_scene.c @@ -0,0 +1,29 @@ +/* + * tests/scenes/spinner_scene.c + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#include +#include "spinner_scene.h" + +void spinner_scene_build(spinner_scene_t *scene) +{ + ui_widget_t *pack, *root; + + pack = ui_load_xml_file("test_spinner.xml"); + root = ui_root(); + ui_widget_append(root, pack); + ui_widget_unwrap(pack); + scene->spinner = ui_get_widget("sp"); + scene->content = ui_get_widget("sc"); + /* The spinner box size is set in code (not via an ID CSS rule) so that + * tests can resize it with ui_widget_resize without cascade fights, + * and so the preview has a visible default box. */ + ui_widget_resize(scene->spinner, 40, 40); +} diff --git a/tests/scenes/spinner_scene.h b/tests/scenes/spinner_scene.h new file mode 100644 index 00000000..77e64b27 --- /dev/null +++ b/tests/scenes/spinner_scene.h @@ -0,0 +1,24 @@ +/* + * tests/scenes/spinner_scene.h + * + * Copyright (c) 2026, Liu Chao All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * This file is part of LCUI, distributed under the MIT License found in the + * LICENSE.TXT file in the root directory of this source tree. + */ + +#ifndef LCUI_TESTS_SCENES_SPINNER_SCENE_H +#define LCUI_TESTS_SCENES_SPINNER_SCENE_H + +#include + +typedef struct spinner_scene { + ui_widget_t *spinner; + ui_widget_t *content; +} spinner_scene_t; + +void spinner_scene_build(spinner_scene_t *scene); + +#endif /* LCUI_TESTS_SCENES_SPINNER_SCENE_H */