1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//! Layout and child-list modifiers for [`El`].
use crate::layout::{LayoutCtx, LayoutFn, VirtualAnchorPolicy};
use crate::metrics::{ComponentSize, MetricsRole};
use super::geometry::{Rect, Sides};
use super::layout_types::{Align, Axis, Justify, Size};
use super::node::El;
impl El {
// ---- Sizing ----
pub fn width(mut self, w: Size) -> Self {
self.width = w;
self.explicit_width = true;
self
}
pub fn height(mut self, h: Size) -> Self {
self.height = h;
self.explicit_height = true;
self
}
pub fn hug(mut self) -> Self {
self.width = Size::Hug;
self.height = Size::Hug;
self.explicit_width = true;
self.explicit_height = true;
self
}
pub fn fill_size(mut self) -> Self {
self.width = Size::Fill(1.0);
self.height = Size::Fill(1.0);
self.explicit_width = true;
self.explicit_height = true;
self
}
/// Set the t-shirt size for stock controls.
pub fn size(mut self, size: ComponentSize) -> Self {
self.component_size = Some(size);
self
}
pub fn medium(self) -> Self {
self.size(ComponentSize::Md)
}
pub fn large(self) -> Self {
self.size(ComponentSize::Lg)
}
/// Set the theme-facing stock metrics role for this widget.
pub fn metrics_role(mut self, role: MetricsRole) -> Self {
self.metrics_role = Some(role);
self
}
// ---- Layout (container) ----
pub fn padding(mut self, p: impl Into<Sides>) -> Self {
self.padding = p.into();
self.explicit_padding = true;
self
}
/// Override only the top padding side, preserving the other three
/// sides at their current value (whether from a constructor's
/// `default_padding` or a previous explicit `.padding(...)`).
/// Mirrors Tailwind's `pt-N`. Marks the padding as explicit, so
/// the metrics pass will not stamp a density-driven value over it.
pub fn pt(mut self, v: f32) -> Self {
self.padding.top = v;
self.explicit_padding = true;
self
}
/// Override only the bottom padding side. Mirrors Tailwind's `pb-N`.
/// See [`Self::pt`] for composition semantics.
pub fn pb(mut self, v: f32) -> Self {
self.padding.bottom = v;
self.explicit_padding = true;
self
}
/// Override only the left padding side. Mirrors Tailwind's `pl-N`.
/// See [`Self::pt`] for composition semantics.
pub fn pl(mut self, v: f32) -> Self {
self.padding.left = v;
self.explicit_padding = true;
self
}
/// Override only the right padding side. Mirrors Tailwind's `pr-N`.
/// See [`Self::pt`] for composition semantics.
pub fn pr(mut self, v: f32) -> Self {
self.padding.right = v;
self.explicit_padding = true;
self
}
/// Override the horizontal padding sides (left + right), preserving
/// `top` and `bottom`. Mirrors Tailwind's `px-N`.
/// See [`Self::pt`] for composition semantics.
pub fn px(mut self, v: f32) -> Self {
self.padding.left = v;
self.padding.right = v;
self.explicit_padding = true;
self
}
/// Override the vertical padding sides (top + bottom), preserving
/// `left` and `right`. Mirrors Tailwind's `py-N`.
/// See [`Self::pt`] for composition semantics.
pub fn py(mut self, v: f32) -> Self {
self.padding.top = v;
self.padding.bottom = v;
self.explicit_padding = true;
self
}
pub fn gap(mut self, g: f32) -> Self {
self.gap = g;
self.explicit_gap = true;
self
}
pub fn align(mut self, a: Align) -> Self {
self.align = a;
self
}
pub fn justify(mut self, j: Justify) -> Self {
self.justify = j;
self
}
pub fn clip(mut self) -> Self {
self.clip = true;
self
}
pub fn scrollable(mut self) -> Self {
self.scrollable = true;
self
}
/// Show a draggable vertical scrollbar thumb when this scrollable
/// node's content overflows.
pub fn scrollbar(mut self) -> Self {
self.scrollbar = true;
self
}
/// Suppress the default scrollbar thumb on this scrollable node.
pub fn no_scrollbar(mut self) -> Self {
self.scrollbar = false;
self
}
/// Stick this scroll viewport's offset to the tail of its content
/// the way chat logs and activity feeds do — when new children land
/// below the current bottom, the offset follows them; when the user
/// scrolls up, the pin releases; when the user scrolls back to the
/// bottom, it re-engages. Mirrors `egui::ScrollArea::stick_to_bottom`.
///
/// On first layout the offset starts at `max_offset`, so a freshly
/// mounted `scroll([...]).pin_end()` paints with its tail visible
/// rather than its head. Programmatic
/// [`crate::scroll::ScrollRequest::EnsureVisible`] requests that
/// resolve away from the tail also release the pin, so a
/// "jump-to-message N" action behaves as the user expects.
pub fn pin_end(mut self) -> Self {
self.pin_end = true;
self
}
/// Override how a dynamic virtual list chooses the in-viewport row
/// point that anchors the next frame.
pub fn virtual_anchor_policy(mut self, policy: VirtualAnchorPolicy) -> Self {
if let Some(items) = self.virtual_items.take() {
self.virtual_items = Some(items.anchor_policy(policy));
}
self
}
/// Treat this element's focusable children as a single
/// arrow-navigable group.
pub fn arrow_nav_siblings(mut self) -> Self {
self.arrow_nav_siblings = true;
self
}
/// Replace the column/row/overlay distribution for this node with
/// a custom child layout function.
pub fn layout<F>(mut self, f: F) -> Self
where
F: Fn(LayoutCtx) -> Vec<Rect> + Send + Sync + 'static,
{
self.layout_override = Some(LayoutFn::new(f));
self
}
// ---- Children ----
pub fn child(mut self, c: impl Into<El>) -> Self {
self.children.push(c.into());
self
}
pub fn children<I, E>(mut self, cs: I) -> Self
where
I: IntoIterator<Item = E>,
E: Into<El>,
{
self.children.extend(cs.into_iter().map(Into::into));
self
}
/// Set the layout axis directly.
pub fn axis(mut self, a: Axis) -> Self {
self.axis = a;
self
}
// ---- Internal stock defaults ----
pub(crate) fn default_width(mut self, w: Size) -> Self {
self.width = w;
self.explicit_width = false;
self
}
pub(crate) fn default_height(mut self, h: Size) -> Self {
self.height = h;
self.explicit_height = false;
self
}
pub(crate) fn default_padding(mut self, p: impl Into<Sides>) -> Self {
self.padding = p.into();
self.explicit_padding = false;
self
}
pub(crate) fn default_gap(mut self, g: f32) -> Self {
self.gap = g;
self.explicit_gap = false;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tree::Kind;
fn fresh() -> El {
El::new(Kind::Group)
}
#[test]
fn pt_sets_only_top_and_marks_explicit() {
let el = fresh().pt(7.0);
assert_eq!(
el.padding,
Sides {
left: 0.0,
right: 0.0,
top: 7.0,
bottom: 0.0
}
);
assert!(el.explicit_padding);
}
#[test]
fn px_py_set_only_their_axis() {
let el = fresh().px(4.0).py(2.0);
assert_eq!(
el.padding,
Sides {
left: 4.0,
right: 4.0,
top: 2.0,
bottom: 2.0
}
);
assert!(el.explicit_padding);
}
#[test]
fn pt_overrides_only_top_when_following_padding() {
// Tailwind shape: `p-4 pt-0` keeps left/right/bottom at 4 and zeros only top.
let el = fresh().padding(4.0).pt(0.0);
assert_eq!(
el.padding,
Sides {
left: 4.0,
right: 4.0,
top: 0.0,
bottom: 4.0
}
);
assert!(el.explicit_padding);
}
#[test]
fn pt_after_default_padding_preserves_other_sides_and_marks_explicit() {
// Constructor default of all-4, then author overrides just the top to 0.
// Other sides keep the default's value; explicit_padding flips so the
// metrics pass cannot stamp over the override.
let el = fresh().default_padding(4.0).pt(0.0);
assert_eq!(
el.padding,
Sides {
left: 4.0,
right: 4.0,
top: 0.0,
bottom: 4.0
}
);
assert!(el.explicit_padding);
}
#[test]
fn per_side_chainables_compose() {
let el = fresh().pl(1.0).pr(2.0).pt(3.0).pb(4.0);
assert_eq!(
el.padding,
Sides {
left: 1.0,
right: 2.0,
top: 3.0,
bottom: 4.0
}
);
assert!(el.explicit_padding);
}
#[test]
fn sides_x_and_y_constructors_only_populate_one_axis() {
assert_eq!(
Sides::x(5.0),
Sides {
left: 5.0,
right: 5.0,
top: 0.0,
bottom: 0.0
}
);
assert_eq!(
Sides::y(5.0),
Sides {
left: 0.0,
right: 0.0,
top: 5.0,
bottom: 5.0
}
);
}
}