1use crate::component::{Component, EventCx, LayoutCx, MeasureCx};
2use crate::event::Event;
3use crate::geom::{Rect, Size};
4use crate::layout::{layout_horizontal, Constraint, LayoutItem};
5use crate::node::Node;
6use crate::render::RenderCx;
7use crate::style::{Layout, Length, Style};
8
9pub struct Row {
11 children: Vec<Node>,
12 style: Style,
13}
14
15impl Row {
16 pub fn new() -> Self {
17 Self {
18 children: Vec::new(),
19 style: Style::default().layout(Layout::Horizontal),
20 }
21 }
22
23 pub fn child(mut self, component: impl Component + 'static) -> Self {
24 self.children.push(Node::new(component));
25 self
26 }
27
28 pub fn padding(mut self, value: u16) -> Self {
29 self.style = self.style.padding(value);
30 self
31 }
32
33 pub fn gap(mut self, value: u16) -> Self {
34 self.style = self.style.gap(value);
35 self
36 }
37
38 pub fn style(mut self, style: Style) -> Self {
39 self.style = style;
40 self
41 }
42}
43
44impl Component for Row {
45 fn render(&self, cx: &mut RenderCx) {
46 for child in &self.children {
47 child.render_with_parent(cx.buffer, cx.focused_id, cx.clip_rect, cx.wrap, cx.truncate, cx.align, Some(&cx.style));
48 }
49 }
50
51 fn for_each_child(&self, f: &mut dyn FnMut(&Node)) {
52 for child in &self.children {
53 f(child);
54 }
55 }
56
57 fn for_each_child_mut(&mut self, f: &mut dyn FnMut(&mut Node)) {
58 for child in &mut self.children {
59 f(child);
60 }
61 }
62
63 fn measure(&self, constraint: Constraint, _cx: &mut MeasureCx) -> Size {
64 let mut width = self.style.padding.left + self.style.padding.right;
65 let mut max_height: u16 = 0;
66
67 for (i, child) in self.children.iter().enumerate() {
68 let child_size = child.measure(constraint);
69 width = width.saturating_add(child_size.width);
70 max_height = max_height.max(child_size.height);
71 if i < self.children.len().saturating_sub(1) {
72 width = width.saturating_add(self.style.gap);
73 }
74 }
75
76 let height = max_height
77 .saturating_add(self.style.padding.top)
78 .saturating_add(self.style.padding.bottom)
79 .min(constraint.max.height);
80
81 Size { width, height }
82 }
83
84 fn focusable(&self) -> bool {
85 false
86 }
87
88
89 fn event(&mut self, event: &Event, cx: &mut EventCx) {
90 if matches!(event, Event::Focus | Event::Blur | Event::Tick) { return; }
91
92 for child in &mut self.children {
93 let mut child_cx = EventCx::new(
94 &mut child.dirty,
95 cx.global_dirty,
96 cx.quit,
97 cx.phase,
98 cx.propagation_stopped,
99 );
100 child.component.event(event, &mut child_cx);
101 }
102 }
103
104 fn layout(&mut self, rect: Rect, _cx: &mut LayoutCx) {
105 let inner = rect.inner(self.style.padding);
106
107 let child_constraint = Constraint::loose(inner.width, inner.height);
108
109 let items: Vec<LayoutItem> = self
110 .children
111 .iter()
112 .map(|child| {
113 let s = child.component.style();
114 LayoutItem {
115 width: match s.width {
116 Length::Auto => {
117 let measured = child.measure(child_constraint);
118 Length::Fixed(measured.width)
119 }
120 other => other,
121 },
122 height: s.height,
123 margin: s.margin,
124 flex_grow: s.flex_grow,
125 flex_shrink: s.flex_shrink,
126 }
127 })
128 .collect();
129
130 let child_rects = layout_horizontal(inner, &items, self.style.gap);
131
132 for (child, child_rect) in self.children.iter_mut().zip(child_rects.iter()) {
133 child.layout(*child_rect);
134 }
135 }
136
137 fn style(&self) -> Style {
138 self.style.clone()
139 }
140}