Skip to content
Development

Common patterns ​

Self-contained examples covering reactive thresholds, flex sizing, spring-driven selection rings, custom vector views, curve text, and scrollable viewports.


Reactive state & threshold styling ​

Drive a numeric readout, a percentage bar width, and a threshold color from one observed integer field without manual callbacks.

Model
612#[model]
613pub struct Gauge {
614 #[observe]
615 pub level: i32,
616}
617
618impl Default for Gauge {
619 fn default() -> Self {
620 Self { level: 42 }
621 }
622}
623
624#[model]
625impl Gauge {
626 pub fn step(&mut self, delta: i32) {
627 self.level = (self.level + delta).clamp(0, 100);
628 }
629}
630
631#[compose]
632fn reactive_scene(gauge: model!(Gauge)) {
633 ui! {
634 Column (
635 grow: 1.0,
636 padding: Padding::all(20),
637 justify: JustifyContent::SpaceBetween,
638 text_color: ColorToken::OnSurface
639 ) {
640 Row (height: 28, justify: JustifyContent::SpaceBetween, align: AlignItems::Center) {
641 Text (
642 "Signal binding",
643 grow: 1.0,
644 height: 20,
645 font_size: 14,
646 text_color: ColorToken::OnSurfaceVariant,
647 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
648 )
649 Text (
650 text: ${ format_args!("{} %", gauge.level()) },
651 capacity: 12,
652 grow: 1.0,
653 font_size: 22,
654 height: 28,
655 paragraph: ParagraphStyle::label().with_align(TextAlign::End)
656 )
657 }
658 View (height: 14, bg_color: ColorToken::SurfaceVariant, border_radius: 7, clip_children: true) {
659 View (
660 width: ${ Dimension::percent(gauge.level()) },
661 height: Dimension::percent(100),
662 bg_color: ${ if gauge.level() >= 80 { ColorToken::Primary } else { ColorToken::Secondary } },
663 border_radius: 7
664 )
665 }
666 Row (height: 40, column_gap: 8) {
667 Button (
668 "- 10",
669 grow: 1.0,
670 height: 40,
671 border_radius: 9,
672 normal_color: ColorToken::SurfaceVariant,
673 pressed_color: ColorToken::Outline,
674 text_color: ColorToken::OnSurface
675 ) on Tap { gauge.step(-10); }
676 Button (
677 "42",
678 width: 64,
679 height: 40,
680 border_radius: 9,
681 normal_color: ColorToken::SurfaceVariant,
682 pressed_color: ColorToken::Outline,
683 text_color: ColorToken::OnSurfaceVariant
684 ) on Tap { gauge.step(42 - gauge.level()); }
685 Button (
686 "+ 10",
687 grow: 1.0,
688 height: 40,
689 border_radius: 9,
690 normal_color: ColorToken::Primary,
691 pressed_color: ColorToken::Secondary,
692 text_color: ColorToken::OnPrimary
693 ) on Tap { gauge.step(10); }
694 }
695 }
696 };
697}
Loading preview…

Tap + 10 past 80 % to watch the fill color switch automatically, or tap 42 to reset.

Marking level with #[observe] generates the gauge.level() getter. Both width: ${ Dimension::percent(gauge.level()) } and bg_color: ${ ... } subscribe to that field and recompute only when gauge.step(...) mutates it.


Flex sizing & space distribution ​

Toggle a primary card between compact and expanded widths while its sibling claims the remaining row space with grow: 1.0.

Flexbox
699#[model]
700#[derive(Default)]
701pub struct FlexDeck {
702 #[observe]
703 pub compact: bool,
704}
705
706#[model]
707impl FlexDeck {
708 pub fn toggle(&mut self) {
709 self.compact = !self.compact;
710 }
711}
712
713#[compose]
714fn layout_scene(deck: model!(FlexDeck)) {
715 ui! {
716 Column (
717 grow: 1.0,
718 padding: Padding::all(18),
719 row_gap: 12,
720 text_color: ColorToken::OnSurface
721 ) {
722 Row (height: 34, column_gap: 8, align: AlignItems::Center) {
723 Text (
724 text: ${ if deck.compact() { "Compact · 96 px card" } else { "Expanded · 156 px card" } },
725 capacity: 28,
726 grow: 1.0,
727 min_width: 0,
728 height: 22,
729 font_size: 13,
730 text_color: ColorToken::OnSurfaceVariant,
731 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
732 )
733 Button (
734 "Toggle width",
735 width: 112,
736 height: 32,
737 font_size: 12,
738 border_radius: 8,
739 normal_color: ColorToken::Primary,
740 pressed_color: ColorToken::Secondary,
741 text_color: ColorToken::OnPrimary
742 ) on Tap { deck.toggle(); }
743 }
744 Row (
745 grow: 1.0,
746 padding: Padding::all(14),
747 column_gap: 10,
748 align: AlignItems::Center,
749 justify: JustifyContent::SpaceBetween,
750 bg_color: ColorToken::SurfaceVariant,
751 border_radius: 12
752 ) {
753 Column (
754 width: ${ Dimension::px(if deck.compact() { 96 } else { 156 }) },
755 height: 76,
756 padding: Padding::all(10),
757 justify: JustifyContent::SpaceBetween,
758 bg_color: ColorToken::Surface,
759 border_radius: 9
760 ) {
761 View (width: 24, height: 6, bg_color: ColorToken::Primary, border_radius: 3)
762 Text ("Primary", font_size: 12, height: 18)
763 }
764 Column (
765 grow: 1.0,
766 height: 76,
767 padding: Padding::all(10),
768 justify: JustifyContent::SpaceBetween,
769 bg_color: ColorToken::Surface,
770 border_radius: 9
771 ) {
772 View (width: 36, height: 6, bg_color: ColorToken::Secondary, border_radius: 3)
773 Text ("Flex grow: 1.0", font_size: 12, height: 18, text_color: ColorToken::OnSurfaceVariant)
774 }
775 }
776 }
777 };
778}
Loading preview…

Tap Toggle width, or drag the circular grip at the bottom-right corner to resize the viewport and see grow: 1.0 in action.

Explicit dimensions (Dimension::px) coexist with flex distribution (grow: 1.0). When the reactive width changes, layout invalidates only the affected row.


Spring selection rings (FollowBounds) ​

Float a selection frame above a row of variable-width buttons so rapid taps retarget mid-flight while preserving velocity.

FollowBounds
recipes.rs105 lines
780#[model]
781#[derive(Default)]
782pub struct SpringTabs {
783 #[observe]
784 pub active: u8,
785}
786
787#[event]
788fn pick_tab(
789 index: u8,
790 tabs: model!(SpringTabs),
791 target: entity!(),
792 followers: query!(mut FollowBounds),
793) {
794 tabs.set_active(index);
795 for follower in followers {
796 follower.target = target;
797 }
798}
799
800#[model]
801impl SpringTabs {
802 pub fn set_active(&mut self, index: u8) {
803 self.active = index;
804 }
805}
806
807#[compose]
808fn spring_scene(tabs: model!(SpringTabs), reduced: bool) {
809 ui! {
810 Column (
811 grow: 1.0,
812 padding: Padding::all(20),
813 justify: JustifyContent::SpaceBetween,
814 text_color: ColorToken::OnSurface
815 ) {
816 Text (
817 "Tap any pill — FollowBounds retargets mid-flight",
818 height: 22,
819 font_size: 13,
820 text_color: ColorToken::OnSurfaceVariant,
821 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
822 )
823 Row (
824 height: 52,
825 padding: Padding::all(5),
826 column_gap: 6,
827 align: AlignItems::Center,
828 bg_color: ColorToken::SurfaceVariant,
829 border_radius: 12
830 ) {
831 Button (
832 "MCU",
833 id: "spring_first",
834 grow: 1.0,
835 height: 38,
836 border_radius: 8,
837 normal_color: ColorToken::Surface,
838 pressed_color: ColorToken::SurfaceVariant,
839 text_color: ColorToken::OnSurface
840 ) on Tap(pick_tab::callback(0, tabs))
841 Button (
842 "Desktop",
843 grow: 1.4,
844 height: 38,
845 border_radius: 8,
846 normal_color: ColorToken::Surface,
847 pressed_color: ColorToken::SurfaceVariant,
848 text_color: ColorToken::OnSurface
849 ) on Tap(pick_tab::callback(1, tabs))
850 Button (
851 "Web",
852 grow: 1.0,
853 height: 38,
854 border_radius: 8,
855 normal_color: ColorToken::Surface,
856 pressed_color: ColorToken::SurfaceVariant,
857 text_color: ColorToken::OnSurface
858 ) on Tap(pick_tab::callback(2, tabs))
859 View (
860 id: "spring_ring",
861 border_width: 2,
862 border_color: ColorToken::Primary,
863 border_radius: 9
864 ) [
865 FollowBounds::new(id("spring_first"))
866 .with_transition(indicator_transition(reduced)),
867 ]
868 }
869 Text (
870 text: ${
871 match tabs.active() {
872 0 => "Target 01 · SPI framebuffer with dirty-rect clips",
873 1 => "Target 02 · Batched GPU draws with retained textures",
874 _ => "Target 03 · Browser canvas driven by requestAnimationFrame",
875 }
876 },
877 height: 20,
878 font_size: 12,
879 text_color: ColorToken::OnSurfaceVariant,
880 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
881 )
882 }
883 };
884}
Loading preview…

Tap quickly across MCU, Desktop, and Web—the outline reverses and resizes in mid-air without snapping.

FollowBounds maps the target entity's layout rectangle into the follower's parent space. Updating follower.target = target inside an #[event] handler hands the new destination to the INTERACTIVE spring.


Custom vector widgets (#[view]) ​

Author a custom widget tag for ui! that strokes static vector paths scaled to the widget's layout box.

#[view]
recipes.rs133 lines
886static APERTURE_BLADE: Path = path!(M 50 6 L 94 50 L 50 94 L 6 50 Z);
887static INNER_RING: Path = path!(M 50 24 L 76 50 L 50 76 L 24 50 Z);
888
889const ACCENTS: [Color; 3] = [
890 Color::rgb(52, 82, 255),
891 Color::rgb(207, 67, 43),
892 Color::rgb(42, 157, 143),
893];
894
895#[component]
896#[derive(Clone, Copy)]
897pub struct VectorGlyph {
898 pub accent: i32,
899 pub stroke: i32,
900}
901
902impl Default for VectorGlyph {
903 fn default() -> Self {
904 Self {
905 accent: 0,
906 stroke: 2,
907 }
908 }
909}
910
911impl VectorGlyph {
912 fn cycle(self) -> Self {
913 Self {
914 accent: (self.accent + 1) % 3,
915 stroke: if self.stroke >= 4 { 2 } else { self.stroke + 1 },
916 }
917 }
918}
919
920#[view(component = VectorGlyph, name = "VectorGlyph", priority = 60)]
921fn paint_glyph(glyph: component!(), paint: paint!()) {
922 let rect = paint.rect();
923 let transform = paint
924 .transform()
925 .compose(&Transform::translate(rect.x, rect.y))
926 .compose(&Transform::scale(
927 rect.w / Fixed::from_int(100),
928 rect.h / Fixed::from_int(100),
929 ));
930 let color = ACCENTS[(glyph.accent.rem_euclid(3)) as usize];
931 let stroke_paint = Paint::Color(color.into());
932 let dash: [Fixed; 0] = [];
933 paint.draw(&DrawCommand::StrokePath {
934 path: &APERTURE_BLADE,
935 transform,
936 paint: &stroke_paint,
937 width: Fixed::from_int(glyph.stroke),
938 opa: 255,
939 line_cap: LineCap::Round,
940 line_join: LineJoin::Round,
941 miter_limit: Fixed::from_int(4),
942 dash: &dash,
943 });
944 paint.draw(&DrawCommand::StrokePath {
945 path: &INNER_RING,
946 transform,
947 paint: &stroke_paint,
948 width: Fixed::from_int(1),
949 opa: 180,
950 line_cap: LineCap::Round,
951 line_join: LineJoin::Round,
952 miter_limit: Fixed::from_int(4),
953 dash: &dash,
954 });
955}
956
957#[compose]
958fn vector_scene() {
959 ui! {
960 Column (
961 grow: 1.0,
962 padding: Padding::all(18),
963 justify: JustifyContent::SpaceBetween,
964 text_color: ColorToken::OnSurface
965 ) {
966 Text (
967 "Custom #[view] — tap any tile to cycle stroke & pigment",
968 height: 20,
969 font_size: 13,
970 text_color: ColorToken::OnSurfaceVariant,
971 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
972 )
973 Row (
974 grow: 1.0,
975 justify: JustifyContent::SpaceEvenly,
976 align: AlignItems::Center,
977 column_gap: 12
978 ) {
979 VectorGlyph (
980 accent: 0,
981 stroke: 2,
982 width: 72,
983 height: 72,
984 bg_color: ColorToken::SurfaceVariant,
985 border_radius: 16
986 ) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
987 if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
988 cmd.insert(ev.target, next);
989 }
990 })
991 VectorGlyph (
992 accent: 1,
993 stroke: 3,
994 width: 72,
995 height: 72,
996 bg_color: ColorToken::SurfaceVariant,
997 border_radius: 16
998 ) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
999 if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
1000 cmd.insert(ev.target, next);
1001 }
1002 })
1003 VectorGlyph (
1004 accent: 2,
1005 stroke: 4,
1006 width: 72,
1007 height: 72,
1008 bg_color: ColorToken::SurfaceVariant,
1009 border_radius: 16
1010 ) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
1011 if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
1012 cmd.insert(ev.target, next);
1013 }
1014 })
1015 }
1016 }
1017 };
1018}
Loading preview…

Tap any of the three tiles to cycle its stroke width (2 px → 3 px → 4 px) and color.

path!(...) builds a zero-allocation static Path. #[view(component = VectorGlyph)] exposes VectorGlyph directly inside ui! and scales its 100 × 100 local coordinates to paint.rect() during rendering.


Text along Bézier curves (TextPath) ​

Place shaped text along a static or reactive vector curve and slide its leading offset without copying path commands.

TextPath
recipes.rs107 lines
1020static WAVE_CREST: Path = path!(M 8 64 Q 60 18 112 64 Q 164 110 216 64);
1021static WAVE_VALLEY: Path = path!(M 8 50 Q 60 96 112 50 Q 164 4 216 50);
1022
1023#[model]
1024pub struct CurveRoute {
1025 crest: PathId,
1026 valley: PathId,
1027 #[observe]
1028 pub inverted: bool,
1029 #[observe]
1030 pub offset: i32,
1031}
1032
1033impl CurveRoute {
1034 pub fn new(crest: PathId, valley: PathId) -> Self {
1035 Self {
1036 crest,
1037 valley,
1038 inverted: false,
1039 offset: 4,
1040 }
1041 }
1042}
1043
1044#[model]
1045impl CurveRoute {
1046 #[observe]
1047 pub fn placement(&self) -> TextPath {
1048 let path = if self.inverted {
1049 self.valley
1050 } else {
1051 self.crest
1052 };
1053 TextPath::new(path).with_offset(Fixed::from_int(self.offset))
1054 }
1055
1056 pub fn flip(&mut self) {
1057 self.inverted = !self.inverted;
1058 }
1059
1060 pub fn advance(&mut self) {
1061 self.offset = (self.offset + 14) % 42;
1062 }
1063}
1064
1065#[compose]
1066fn curve_scene(route: model!(CurveRoute)) {
1067 ui! {
1068 Column (
1069 grow: 1.0,
1070 padding: Padding::all(16),
1071 row_gap: 10,
1072 text_color: ColorToken::OnSurface
1073 ) {
1074 Row (height: 30, column_gap: 6, align: AlignItems::Center) {
1075 Text (
1076 text: ${ format_args!("+{} px", route.offset()) },
1077 capacity: 16,
1078 grow: 1.0,
1079 min_width: 0,
1080 height: 22,
1081 font_size: 13,
1082 text_color: ColorToken::OnSurfaceVariant,
1083 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
1084 )
1085 Button (
1086 "Slide +14",
1087 width: 72,
1088 height: 28,
1089 font_size: 12,
1090 border_radius: 7,
1091 normal_color: ColorToken::SurfaceVariant,
1092 pressed_color: ColorToken::Outline,
1093 text_color: ColorToken::OnSurface
1094 ) on Tap { route.advance(); }
1095 Button (
1096 "Flip wave",
1097 width: 74,
1098 height: 28,
1099 font_size: 12,
1100 border_radius: 7,
1101 normal_color: ColorToken::Primary,
1102 pressed_color: ColorToken::Secondary,
1103 text_color: ColorToken::OnPrimary
1104 ) on Tap { route.flip(); }
1105 }
1106 View (
1107 grow: 1.0,
1108 bg_color: ColorToken::SurfaceVariant,
1109 border_radius: 12,
1110 clip_children: true
1111 ) {
1112 Text (
1113 "mirui rides the wave",
1114 path: ${ route.placement() },
1115 position: Position::Absolute,
1116 left: 6,
1117 top: 8,
1118 width: Dimension::percent(96),
1119 height: Dimension::percent(88),
1120 font_size: 14,
1121 text_color: ColorToken::Primary
1122 )
1123 }
1124 }
1125 };
1126}
Loading preview…

Tap Slide +18 to advance the text along the wave, or tap Flip wave to switch between crest and valley paths.

app.paths().insert_static(...) registers a compile-time Path and returns a lightweight PathId. Binding path: ${ route.placement() } updates glyph placement whenever the model's TextPath offset or curve selection changes.


Animated scroll containers (ScrollOffset) ​

Drive a clipped scroll container with commands.scroll_to and ScrollConfig::with_transition while keeping direct pointer and wheel scrolling active.

ScrollOffset
1128#[compose]
1129fn scroll_item(title: &'static str) -> Entity {
1130 ui! {
1131 Row (
1132 height: 38,
1133 shrink: 0.0,
1134 padding: Padding::all(10),
1135 align: AlignItems::Center,
1136 bg_color: ColorToken::Surface,
1137 border_radius: 8
1138 ) {
1139 Text (title, height: 18, font_size: 13)
1140 }
1141 }
1142}
1143
1144#[compose]
1145fn scroll_button(label: &'static str, list: Entity, y: i32, primary: bool) -> Entity {
1146 ui! {
1147 Button (
1148 label,
1149 grow: 1.0,
1150 height: 30,
1151 font_size: 12,
1152 border_radius: 7,
1153 normal_color: if primary { ColorToken::Primary } else { ColorToken::SurfaceVariant },
1154 pressed_color: if primary { ColorToken::Secondary } else { ColorToken::Outline },
1155 text_color: if primary { ColorToken::OnPrimary } else { ColorToken::OnSurface }
1156 ) on Tap {
1157 commands!().scroll_to(
1158 list,
1159 ScrollOffset {
1160 x: Fixed::ZERO,
1161 y: Fixed::from_int(y),
1162 },
1163 );
1164 }
1165 }
1166}
1167
1168#[compose]
1169fn scroll_scene(reduced: bool) {
1170 ui! {
1171 Column (
1172 grow: 1.0,
1173 padding: Padding::all(16),
1174 row_gap: 10,
1175 text_color: ColorToken::OnSurface
1176 ) {
1177 Row (height: 32, column_gap: 6, align: AlignItems::Center) {
1178 scroll_button ("Top (#1)", id("scroll_list"), 0, false)
1179 scroll_button ("Middle (#4)", id("scroll_list"), 132, false)
1180 scroll_button ("Bottom (#7)", id("scroll_list"), 264, true)
1181 }
1182 Column (
1183 id: "scroll_list",
1184 grow: 1.0,
1185 min_height: 0,
1186 padding: Padding::all(8),
1187 row_gap: 6,
1188 bg_color: ColorToken::SurfaceVariant,
1189 border_radius: 10,
1190 clip_children: true
1191 ) [
1192 ScrollOffset::default(),
1193 ScrollConfig::default().with_transition(indicator_transition(reduced)),
1194 ] {
1195 scroll_item ("01 · FramebufSurface (MCU SPI)")
1196 scroll_item ("02 · SdlSurface (Desktop CPU)")
1197 scroll_item ("03 · SdlGpuSurface (OpenGL / Metal)")
1198 scroll_item ("04 · WgpuSurface (Vulkan / DX12)")
1199 scroll_item ("05 · WebCanvasSurface (Browser)")
1200 scroll_item ("06 · LinuxDrmSurface (KMS / GBM)")
1201 scroll_item ("07 · NuttxSurface (Embedded RTOS)")
1202 }
1203 }
1204 };
1205}
Loading preview…

Tap Top (#1), Middle (#4), or Bottom (#7) to smoothly scroll the container to that offset, or drag/wheel inside the list directly.

Attaching ScrollOffset::default() and ScrollConfig::default().with_transition(...) turns a Column(clip_children: true) into a scrollable viewport; commands.scroll_to(list, offset) animates the container along that transition and redirects mid-flight if tapped again.