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
use egui::{Align, Response, Ui};
use crate::{
containers::frame::StyledFrame,
style::shared_style::{Distribution, SharedStyle, distribute_row_horizontally},
};
/// A single child of a [`DistributedRow`], rendered once into the row's `Ui`.
type ItemFn<'a> = Box<dyn FnOnce(&mut Ui) + 'a>;
pub struct DistributedRow<'a> {
pub(crate) mode: Distribution,
pub(crate) min_gap: f32,
pub(crate) align: Option<Align>,
pub(crate) style: SharedStyle,
pub(crate) items: Vec<ItemFn<'a>>,
}
impl<'a> DistributedRow<'a> {
pub fn item(mut self, f: impl FnOnce(&mut Ui) + 'a) -> Self {
self.items.push(Box::new(f));
self
}
pub fn show(self, ui: &mut Ui) -> Response {
if self.style.visible == Some(false) {
ui.set_invisible();
}
let n = self.items.len();
let mode = self.mode;
let min_gap = self.min_gap;
let cross_align = self.align.unwrap_or(Align::Center);
let mut items = self.items;
// render is called inside the (optional) StyledFrame, so avail and id
// are captured inside the closure to reflect post-padding dimensions.
let mut render = move |ui: &mut Ui| {
let avail = ui.available_width();
// Per-position stable id for the cross-frame W cache. A constant
// source would collide between sibling distributed rows, so one row
// would read another's cached content width. `next_auto_id` advances
// per widget and is deterministic across frames (same pattern as the
// vertical-justify cache in `StyledFrame`).
let id = ui
.make_persistent_id(ui.next_auto_id())
.with("__distribute_w");
distribute_row_horizontally(ui, avail, mode, min_gap, n, cross_align, id, |ui| {
for item in items.drain(..) {
ui.scope(|ui| item(ui));
}
});
ui.response()
};
if self.style.has_frame_styles() {
StyledFrame {
style: self.style,
align: None,
justify: None,
gap: None,
fill_size: None,
}
.show(ui, |ui| render(ui))
.inner
} else {
render(ui)
}
}
}
#[cfg(test)]
mod tests {
use crate::containers::row::StyledRow;
use std::cell::RefCell;
use std::rc::Rc;
fn screen(w: f32, h: f32) -> egui::RawInput {
egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(
egui::Pos2::ZERO,
egui::vec2(w, h),
)),
..Default::default()
}
}
fn run_space_between(ctx: &egui::Context, screen_w: f32) -> Vec<egui::Rect> {
let out: Rc<RefCell<Vec<egui::Rect>>> = Rc::new(RefCell::new(Vec::new()));
{
let out_ref = Rc::clone(&out);
let _ = ctx.run_ui(screen(screen_w, 400.0), |ui| {
let o1 = Rc::clone(&out_ref);
let o2 = Rc::clone(&out_ref);
let o3 = Rc::clone(&out_ref);
out_ref.borrow_mut().clear();
StyledRow::new()
.full_width()
.space_between()
.item(move |ui| {
o1.borrow_mut().push(ui.label("Alpha").rect);
})
.item(move |ui| {
o2.borrow_mut().push(ui.label("Beta-longer").rect);
})
.item(move |ui| {
o3.borrow_mut().push(ui.label("Gamma").rect);
})
.show(ui);
});
} // out_ref dropped here
Rc::try_unwrap(out).unwrap().into_inner()
}
fn run_space_evenly(ctx: &egui::Context, screen_w: f32) -> Vec<egui::Rect> {
let out: Rc<RefCell<Vec<egui::Rect>>> = Rc::new(RefCell::new(Vec::new()));
{
let out_ref = Rc::clone(&out);
let _ = ctx.run_ui(screen(screen_w, 400.0), |ui| {
let o1 = Rc::clone(&out_ref);
let o2 = Rc::clone(&out_ref);
let o3 = Rc::clone(&out_ref);
out_ref.borrow_mut().clear();
StyledRow::new()
.full_width()
.space_evenly()
.item(move |ui| {
o1.borrow_mut().push(ui.label("A").rect);
})
.item(move |ui| {
o2.borrow_mut().push(ui.label("B").rect);
})
.item(move |ui| {
o3.borrow_mut().push(ui.label("C").rect);
})
.show(ui);
});
}
Rc::try_unwrap(out).unwrap().into_inner()
}
#[test]
fn space_between_pins_ends() {
let ctx = egui::Context::default();
let screen_w = 400.0f32;
// Frame 1: measure pass (items invisible, W cached).
let _ = run_space_between(&ctx, screen_w);
// Frame 2: layout applied with distribution.
let rects = run_space_between(&ctx, screen_w);
assert_eq!(rects.len(), 3, "all 3 items should render on frame 2");
let left = rects[0].left();
let right = rects[2].right();
assert!(
left < 10.0,
"space_between: first item should be near left edge, got {left}"
);
assert!(
right > screen_w - 10.0,
"space_between: last item right should be near {screen_w}, got {right}"
);
let mid = rects[1].center().x;
assert!(
(mid - screen_w / 2.0).abs() < 30.0,
"space_between: middle center ({mid:.1}) should be near screen center ({:.1})",
screen_w / 2.0
);
}
#[test]
fn space_evenly_leading_equals_gap() {
let ctx = egui::Context::default();
let screen_w = 400.0f32;
let _ = run_space_evenly(&ctx, screen_w);
let rects = run_space_evenly(&ctx, screen_w);
assert_eq!(rects.len(), 3);
let leading = rects[0].left();
let gap = rects[1].left() - rects[0].right();
// space_evenly: leading ≈ gap (both = slack/(n+1)).
assert!(
(leading - gap).abs() < 5.0,
"space_evenly: leading ({leading:.1}) should ≈ gap ({gap:.1})"
);
}
/// Regression: unlike a bare spacer (which pushes trailing content past the
/// edge), `space_between` measures content and must keep every item *inside*
/// the container — even a `full_width` row with bg + padding.
#[test]
fn space_between_does_not_overflow_container() {
let ctx = egui::Context::default();
let screen_w = 1140.0f32;
let run = |ctx: &egui::Context| -> f32 {
let right = Rc::new(RefCell::new(0.0f32));
{
let r = Rc::clone(&right);
let _ = ctx.run_ui(screen(screen_w, 600.0), |ui| {
let r1 = Rc::clone(&r);
let r2 = Rc::clone(&r);
StyledRow::new()
.full_width()
.bg(egui::Color32::from_rgb(40, 42, 58))
.padding(10.0)
.space_between()
.item(move |ui| {
let right = ui.label("Left").rect.right();
let mut cell = r1.borrow_mut();
*cell = cell.max(right);
})
.item(move |ui| {
let right = ui.label("Right").rect.right();
let mut cell = r2.borrow_mut();
*cell = cell.max(right);
})
.show(ui);
});
}
*right.borrow()
};
// Frame 1 measures, frame 2 lays out.
let _ = run(&ctx);
let max_right = run(&ctx);
assert!(
max_right <= screen_w,
"space_between content right={max_right} must stay within container width {screen_w}"
);
}
/// Regression: two distributed rows on the same page must not share a W
/// cache. With a constant cache id the second row read the first row's
/// (smaller) content width, computed too much slack, and overflowed. Each
/// row needs a per-position id so its own measured width is used.
#[test]
fn sibling_distributed_rows_do_not_share_w_cache() {
let ctx = egui::Context::default();
let screen_w = 1140.0f32;
// Row A has narrow content; row B has wide content. If B reads A's W it
// overshoots far past the edge.
let run = |ctx: &egui::Context| -> f32 {
let b_right = Rc::new(RefCell::new(0.0f32));
{
let br = Rc::clone(&b_right);
let _ = ctx.run_ui(screen(screen_w, 600.0), |ui| {
let br1 = Rc::clone(&br);
// Row A: tiny content.
StyledRow::new()
.full_width()
.space_between()
.item(|ui| {
ui.label("A");
})
.item(|ui| {
ui.label("B");
})
.show(ui);
// Row B: wide content (a long label + a button group).
StyledRow::new()
.full_width()
.space_between()
.item(|ui| {
ui.label("A fairly long title goes here");
})
.item(move |ui| {
let r = ui.button("Some Action Button").rect.right();
*br1.borrow_mut() = r;
})
.show(ui);
});
}
*b_right.borrow()
};
let _ = run(&ctx);
let b_right = run(&ctx);
assert!(
b_right <= screen_w + 1.0,
"second distributed row right={b_right} overflowed {screen_w} — W caches collided"
);
}
}