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
//! `Container` — a rectangular box with optional background, border, and
//! padding that holds zero or more child widgets.
//!
//! Phase 4 child layout is a simple top-down vertical stack (bottom-most child
//! at `y = padding`, each subsequent child placed above the previous). Flex
//! layout arrives in Phase 5.
use crate::color::Color;
use crate::draw_ctx::DrawCtx;
use crate::event::{Event, EventResult};
use crate::geometry::{Rect, Size};
use crate::layout_props::{HAnchor, Insets, VAnchor, WidgetBase};
use crate::widget::Widget;
/// Inspector-visible properties of a [`Container`].
#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
#[derive(Clone, Debug)]
pub struct ContainerProps {
pub background: Color,
pub border_color: Option<Color>,
pub border_width: f64,
pub corner_radius: f64,
pub inner_padding: Insets,
/// When `true`, `layout` returns the content's natural height + vertical
/// padding instead of the full available height. Off by default for
/// backward compatibility (callers that used `Container` as a fill-
/// parent decoration still work). Match egui's `Frame` by opting in.
pub fit_height: bool,
}
impl Default for ContainerProps {
fn default() -> Self {
Self {
background: Color::rgba(0.0, 0.0, 0.0, 0.0),
border_color: None,
border_width: 1.0,
corner_radius: 0.0,
inner_padding: Insets::ZERO,
fit_height: false,
}
}
}
/// A rectangular container widget.
///
/// Paints a background rounded-rect (optional border), then lets the framework
/// recurse into its children. Children are stacked bottom-to-top inside the
/// padding area.
pub struct Container {
bounds: Rect,
children: Vec<Box<dyn Widget>>,
base: WidgetBase,
pub props: ContainerProps,
}
impl Container {
/// Create a transparent container with no border and default padding.
pub fn new() -> Self {
Self {
bounds: Rect::default(),
children: Vec::new(),
base: WidgetBase::new(),
props: ContainerProps::default(),
}
}
/// Opt into content-fit height — [`layout`] returns
/// `content_height + vertical_padding` instead of the full
/// available height. Required when this `Container` sits inside
/// an auto-sized ancestor (e.g. `Window::with_auto_size(true)`),
/// which would otherwise pick up the full available height as
/// the container's preferred size and inflate the window.
pub fn with_fit_height(mut self, fit: bool) -> Self {
self.props.fit_height = fit;
self
}
/// Append a child widget.
pub fn add(mut self, child: Box<dyn Widget>) -> Self {
self.children.push(child);
self
}
pub fn with_background(mut self, color: Color) -> Self {
self.props.background = color;
self
}
pub fn with_border(mut self, color: Color, width: f64) -> Self {
self.props.border_color = Some(color);
self.props.border_width = width;
self
}
pub fn with_corner_radius(mut self, r: f64) -> Self {
self.props.corner_radius = r;
self
}
pub fn with_padding(mut self, p: f64) -> Self {
self.props.inner_padding = Insets::all(p);
self
}
pub fn with_inner_padding(mut self, p: Insets) -> Self {
self.props.inner_padding = p;
self
}
pub fn with_margin(mut self, m: Insets) -> Self {
self.base.margin = m;
self
}
pub fn with_h_anchor(mut self, h: HAnchor) -> Self {
self.base.h_anchor = h;
self
}
pub fn with_v_anchor(mut self, v: VAnchor) -> Self {
self.base.v_anchor = v;
self
}
pub fn with_min_size(mut self, s: Size) -> Self {
self.base.min_size = s;
self
}
pub fn with_max_size(mut self, s: Size) -> Self {
self.base.max_size = s;
self
}
}
impl Default for Container {
fn default() -> Self {
Self::new()
}
}
impl Widget for Container {
fn type_name(&self) -> &'static str {
"Container"
}
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn children(&self) -> &[Box<dyn Widget>] {
&self.children
}
fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> {
&mut self.children
}
#[cfg(feature = "reflect")]
fn as_reflect(&self) -> Option<&dyn bevy_reflect::Reflect> {
Some(&self.props)
}
#[cfg(feature = "reflect")]
fn as_reflect_mut(&mut self) -> Option<&mut dyn bevy_reflect::Reflect> {
Some(&mut self.props)
}
fn margin(&self) -> Insets {
self.base.margin
}
fn widget_base(&self) -> Option<&WidgetBase> {
Some(&self.base)
}
fn widget_base_mut(&mut self) -> Option<&mut WidgetBase> {
Some(&mut self.base)
}
fn padding(&self) -> Insets {
self.props.inner_padding
}
fn h_anchor(&self) -> HAnchor {
self.base.h_anchor
}
fn v_anchor(&self) -> VAnchor {
self.base.v_anchor
}
fn min_size(&self) -> Size {
self.base.min_size
}
fn max_size(&self) -> Size {
self.base.max_size
}
fn layout(&mut self, available: Size) -> Size {
let pad_l = self.props.inner_padding.left;
let pad_r = self.props.inner_padding.right;
let pad_t = self.props.inner_padding.top;
let pad_b = self.props.inner_padding.bottom;
let inner_w = (available.width - pad_l - pad_r).max(0.0);
fn layout_children(
children: &mut [Box<dyn Widget>],
inner_w: f64,
pad_l: f64,
pad_t: f64,
pad_b: f64,
height: f64,
) -> f64 {
// Stack children top-to-bottom (first child = visually highest).
// In Y-up coordinates, "top" = higher Y values.
let start_cursor = height - pad_t;
let mut cursor_y = start_cursor;
for child in children.iter_mut() {
// Margins are logical units; DPI is applied at the App
// paint boundary, never during layout.
let m = child.margin();
let avail_w = (inner_w - m.left - m.right).max(0.0);
let avail_h = (cursor_y - pad_b - m.top - m.bottom).max(0.0);
let desired = child.layout(Size::new(avail_w, avail_h));
cursor_y -= m.top;
let child_y = cursor_y - desired.height;
child.set_bounds(Rect::new(
pad_l + m.left,
child_y,
desired.width.min(avail_w),
desired.height,
));
cursor_y = child_y - m.bottom;
}
(start_cursor - cursor_y).max(0.0)
}
let consumed_h = layout_children(
&mut self.children,
inner_w,
pad_l,
pad_t,
pad_b,
available.height,
);
// Default: fill the full available area (legacy — many demo
// sites use `Container` as a decorated wrapper around content
// that should stretch). Opt in to content-fit via
// `with_fit_height(true)` — matches egui `Frame` semantics.
if self.props.fit_height {
let natural_h = (consumed_h + pad_t + pad_b).min(available.height);
// The first pass measured content from the parent-provided height.
// A fit-height container paints at `natural_h`, so lay children out
// again in that tight height to keep their bounds inside the frame.
if (available.height - natural_h).abs() > 0.5 {
layout_children(&mut self.children, inner_w, pad_l, pad_t, pad_b, natural_h);
}
Size::new(available.width, natural_h)
} else {
Size::new(available.width, available.height)
}
}
fn paint(&mut self, ctx: &mut dyn DrawCtx) {
let w = self.bounds.width;
let h = self.bounds.height;
let r = self.props.corner_radius;
// Background
if self.props.background.a > 0.001 {
ctx.set_fill_color(self.props.background);
ctx.begin_path();
ctx.rounded_rect(0.0, 0.0, w, h, r);
ctx.fill();
}
// Border
if let Some(bc) = self.props.border_color {
ctx.set_stroke_color(bc);
ctx.set_line_width(self.props.border_width);
ctx.begin_path();
let inset = self.props.border_width * 0.5;
ctx.rounded_rect(
inset,
inset,
(w - self.props.border_width).max(0.0),
(h - self.props.border_width).max(0.0),
r,
);
ctx.stroke();
}
}
fn on_event(&mut self, _event: &Event) -> EventResult {
EventResult::Ignored
}
}