mitsuami_widgets/containers.rs
1//! Layout containers: flexbox, grid and titled groups.
2
3use std::rc::Rc;
4
5use std::path::PathBuf;
6
7use mitsuami_core::services::Shortcut;
8use mitsuami_core::{
9 Align, Children, Display, Element, ElementBuilder, EventValue, FileDrop, FlexDirection, HeaderEdge, Justify,
10 Length, NodeId, Orientation, Prop, Track, Tweak, Ui, UiEvent, View, WidgetKind,
11};
12use mitsuami_reactive::{IntoValue, Signal, Value};
13
14/// A layout host: flexbox (column by default) or grid.
15pub struct Container(pub(crate) Element);
16widget!(Container);
17
18impl Default for Container {
19 fn default() -> Container {
20 Container::new()
21 }
22}
23
24impl Container {
25 pub fn new() -> Container {
26 Container(Element::new(WidgetKind::Container))
27 }
28
29 pub fn children(mut self, children: impl Children) -> Container {
30 self.0.add_children(children);
31 self
32 }
33
34 pub fn child(self, child: impl View) -> Container {
35 self.children(child)
36 }
37
38 /// Takes the files and folders `drop` says when they're dropped on it
39 /// from the platform's file manager, and gives them to `on_drop`. The
40 /// platform shows it'll copy them while they're over it; `on_drop_hover`
41 /// says when, for the app's own highlight. Dragging isn't reachable
42 /// from the keyboard or assistive technology: offer another way (an
43 /// open dialog) too.
44 pub fn file_drop(mut self, drop: impl IntoValue<FileDrop>) -> Container {
45 let drop = match drop.into_value() {
46 Value::Static(d) => Value::Static(Some(d)),
47 dynamic => Value::Dynamic(Rc::new(move || Some(dynamic.get()))),
48 };
49 self.0.prop(drop, Prop::FileDrop);
50 self
51 }
52
53 /// The files and folders dropped on it that it takes (`file_drop`).
54 pub fn on_drop(mut self, handler: impl Fn(Vec<PathBuf>) + 'static) -> Container {
55 self.0.on(move |event| {
56 if let UiEvent::FilesDropped(paths) = event {
57 handler(paths.clone());
58 }
59 });
60 self
61 }
62
63 /// Whether files it takes are over it (`file_drop`).
64 pub fn on_drop_hover(mut self, handler: impl Fn(bool) + 'static) -> Container {
65 self.0.on(move |event| {
66 if let UiEvent::DropHover(over) = event {
67 handler(*over);
68 }
69 });
70 self
71 }
72
73 /// Called with `true` when the pointer comes over it, or over anything
74 /// inside it, and `false` when it leaves, as the platform tracks it:
75 /// to show a row's buttons while it's hovered. Keyboards and
76 /// assistive technology never hover, so whatever it shows must be
77 /// reachable another way too (a context menu, a selected row's
78 /// toolbar).
79 pub fn on_hover(mut self, handler: impl Fn(bool) + 'static) -> Container {
80 self.0.on_hover(handler);
81 self
82 }
83
84 /// Called when it's double-clicked, or anything inside it that isn't
85 /// a control, as the platform counts a double click: to rename what a
86 /// row shows in place. Keyboards and assistive technology don't
87 /// double-click, so what it does must be reachable another way too (a
88 /// context menu, a button).
89 pub fn on_double_click(mut self, handler: impl Fn() + 'static) -> Container {
90 self.0.on_double_click(handler);
91 self
92 }
93
94 /// Called when it's clicked, or anything inside it that isn't a
95 /// control (a label, an icon): a header that opens its section, as
96 /// GTK's and WinUI's expanders do. Pointers only, as for
97 /// `on_double_click`: keyboards and assistive technology need a
98 /// control inside (the header's disclosure button).
99 pub fn on_click(mut self, handler: impl Fn() + 'static) -> Container {
100 self.0.on_click(handler);
101 self
102 }
103
104 /// Runs `handler` when `key` is pressed while it, or a control
105 /// inside it, has keyboard focus, and the focused control doesn't use
106 /// the key itself: Space for a preview, Delete for Move to Trash.
107 /// A key goes to the nearest node around the focused control that
108 /// takes it. Nothing shows keys taken this way, so give the command a
109 /// menu item or a button too, and pick the keys the platform's own apps
110 /// use (`platform!`).
111 ///
112 /// ```ignore
113 /// Column::new().on_key(Shortcut::primary(Key::Backspace), trash)
114 /// ```
115 pub fn on_key(mut self, key: impl Into<Shortcut>, handler: impl Fn() + 'static) -> Container {
116 self.0.on_key(key.into(), handler);
117 self
118 }
119
120 /// Makes it its window's header, which stays while what's under it
121 /// scrolls: put it at the top of the window's content, over the scroll
122 /// view it stays above. On macOS it goes in the title bar, in the
123 /// bar's glass, and the scroll view runs up under it, as Safari's and
124 /// Mail's bars do; elsewhere it stays at the content's top as it is.
125 ///
126 /// ```ignore
127 /// Column::new().children((
128 /// Row::new().header(true).children((Text::new("Library"), add_button())),
129 /// ScrollView::new(rows()).grow(1.0),
130 /// ))
131 /// ```
132 pub fn header(mut self, header: impl IntoValue<bool>) -> Container {
133 self.0.prop(header.into_value(), Prop::Header);
134 self
135 }
136
137 /// How its window's header meets what scrolls under it (`header`):
138 /// on macOS 26.1 and later, a soft fade or a hard line. Without, the
139 /// platform's own; elsewhere it has no such edge.
140 pub fn header_edge(mut self, edge: impl IntoValue<HeaderEdge>) -> Container {
141 self.0.prop(edge.into_value(), Prop::HeaderEdge);
142 self
143 }
144
145 /// Makes it a pane down its window's side that navigates, one the app
146 /// builds itself (the rail that picks a Git client's pages): on WinUI under tabs in the
147 /// title bar, it's on the same layer as the chosen tab and a header,
148 /// as WinUI's guidance has for navigation. Elsewhere it's left as is.
149 pub fn sidebar_pane(mut self, pane: impl IntoValue<bool>) -> Container {
150 self.0.prop(pane.into_value(), Prop::SidebarPane);
151 self
152 }
153
154 /// Lets the pointer through it and everything in it to what's under
155 /// it, as a hint laid over a text area lets clicks reach the text.
156 pub fn click_through(mut self, through: impl IntoValue<bool>) -> Container {
157 self.0.prop(through.into_value(), Prop::ClickThrough);
158 self
159 }
160
161 pub fn flex_direction(mut self, direction: impl IntoValue<FlexDirection>) -> Container {
162 self.0.style_prop(direction.into_value(), |s, v| s.flex_direction = v);
163 self
164 }
165
166 /// Gap between rows and columns.
167 pub fn gap(mut self, gap: impl IntoValue<Length>) -> Container {
168 self.0.style_prop(gap.into_value(), |s, v| {
169 s.row_gap = v;
170 s.column_gap = v;
171 });
172 self
173 }
174
175 pub fn row_gap(mut self, gap: impl IntoValue<Length>) -> Container {
176 self.0.style_prop(gap.into_value(), |s, v| s.row_gap = v);
177 self
178 }
179
180 pub fn column_gap(mut self, gap: impl IntoValue<Length>) -> Container {
181 self.0.style_prop(gap.into_value(), |s, v| s.column_gap = v);
182 self
183 }
184
185 /// Cross-axis alignment of children (`align-items`).
186 pub fn align(mut self, align: impl IntoValue<Align>) -> Container {
187 self.0.style_prop(align.into_value(), |s, v| s.align_items = Some(v));
188 self
189 }
190
191 /// Main-axis distribution of children (`justify-content`).
192 pub fn justify(mut self, justify: impl IntoValue<Justify>) -> Container {
193 self.0.style_prop(justify.into_value(), |s, v| s.justify_content = Some(v));
194 self
195 }
196
197 pub fn wrap(self) -> Container {
198 self.style(|s| s.flex_wrap = true)
199 }
200
201 /// Switches to grid layout with these column tracks.
202 pub fn columns<T: Into<Track>>(self, tracks: impl IntoIterator<Item = T>) -> Container {
203 let tracks: Vec<Track> = tracks.into_iter().map(Into::into).collect();
204 self.style(|s| {
205 s.display = Display::Grid;
206 s.grid_template_columns = tracks;
207 })
208 }
209
210 /// Switches to grid layout with these row tracks.
211 pub fn rows<T: Into<Track>>(self, tracks: impl IntoIterator<Item = T>) -> Container {
212 let tracks: Vec<Track> = tracks.into_iter().map(Into::into).collect();
213 self.style(|s| {
214 s.display = Display::Grid;
215 s.grid_template_rows = tracks;
216 })
217 }
218}
219
220/// Vertical flex container. `new()` returns a [`Container`]: chain its
221/// methods, and the style setters of [`ElementBuilder`].
222pub struct Column;
223
224impl Column {
225 #[allow(clippy::new_ret_no_self)]
226 pub fn new() -> Container {
227 Container::new().flex_direction(FlexDirection::Column)
228 }
229}
230
231/// Horizontal flex container. `new()` returns a [`Container`]: chain its
232/// methods, and the style setters of [`ElementBuilder`].
233pub struct Row;
234
235impl Row {
236 #[allow(clippy::new_ret_no_self)]
237 pub fn new() -> Container {
238 Container::new().flex_direction(FlexDirection::Row)
239 }
240}
241
242/// Grid container. `new()` returns a [`Container`]: chain its methods
243/// (`columns`, `rows`), and the style setters of [`ElementBuilder`].
244pub struct Grid;
245
246impl Grid {
247 #[allow(clippy::new_ret_no_self)]
248 pub fn new() -> Container {
249 Container::new().style(|s| s.display = Display::Grid)
250 }
251}
252
253/// A box around related content, as the platform groups it: an `NSBox`
254/// (its heading inside, at the top), libadwaita's card under a heading,
255/// a Fluent card under a heading on Windows, a `QQC2.GroupBox`. The
256/// heading is optional; it names the group to assistive technology.
257///
258/// Its children are laid out as a column's (`gap`, and any style), inside
259/// the platform's border and margins; `padding` adds to those. Put a `Row`
260/// or `Grid` in it for other layouts.
261///
262/// ```ignore
263/// Group::new().title("CD drive").child(
264/// Row::new().gap(Spacing::Md).children((Icon::new(disc), Text::new(title), eject)),
265/// )
266/// ```
267pub struct Group(Element);
268widget!(Group);
269
270impl Default for Group {
271 fn default() -> Group {
272 Group::new()
273 }
274}
275
276impl Group {
277 pub fn new() -> Group {
278 Group(Element::new(WidgetKind::Group)).style(|s| {
279 s.display = Display::Flex;
280 s.flex_direction = FlexDirection::Column;
281 })
282 }
283
284 /// Its heading; empty: none.
285 pub fn title(mut self, title: impl IntoValue<String>) -> Group {
286 self.0.prop(title.into_value(), Prop::Title);
287 self
288 }
289
290 pub fn children(mut self, children: impl Children) -> Group {
291 self.0.add_children(children);
292 self
293 }
294
295 pub fn child(self, child: impl View) -> Group {
296 self.children(child)
297 }
298
299 /// Takes the files and folders `drop` says when they're dropped on it
300 /// from the platform's file manager, and gives them to `on_drop`. The
301 /// platform shows it'll copy them while they're over it; `on_drop_hover`
302 /// says when, for the app's own highlight. Dragging isn't reachable
303 /// from the keyboard or assistive technology: offer another way (an
304 /// open dialog) too.
305 pub fn file_drop(mut self, drop: impl IntoValue<FileDrop>) -> Group {
306 let drop = match drop.into_value() {
307 Value::Static(d) => Value::Static(Some(d)),
308 dynamic => Value::Dynamic(Rc::new(move || Some(dynamic.get()))),
309 };
310 self.0.prop(drop, Prop::FileDrop);
311 self
312 }
313
314 /// The files and folders dropped on it that it takes (`file_drop`).
315 pub fn on_drop(mut self, handler: impl Fn(Vec<PathBuf>) + 'static) -> Group {
316 self.0.on(move |event| {
317 if let UiEvent::FilesDropped(paths) = event {
318 handler(paths.clone());
319 }
320 });
321 self
322 }
323
324 /// Whether files it takes are over it (`file_drop`).
325 pub fn on_drop_hover(mut self, handler: impl Fn(bool) + 'static) -> Group {
326 self.0.on(move |event| {
327 if let UiEvent::DropHover(over) = event {
328 handler(*over);
329 }
330 });
331 self
332 }
333
334 /// Called with `true` when the pointer comes over it, or over anything
335 /// inside it, and `false` when it leaves, as the platform tracks it:
336 /// to show a row's buttons while it's hovered. Keyboards and
337 /// assistive technology never hover, so whatever it shows must be
338 /// reachable another way too (a context menu, a selected row's
339 /// toolbar).
340 pub fn on_hover(mut self, handler: impl Fn(bool) + 'static) -> Group {
341 self.0.on_hover(handler);
342 self
343 }
344
345 /// Called when it's double-clicked, or anything inside it that isn't
346 /// a control, as the platform counts a double click: to rename what a
347 /// row shows in place. Keyboards and assistive technology don't
348 /// double-click, so what it does must be reachable another way too (a
349 /// context menu, a button).
350 pub fn on_double_click(mut self, handler: impl Fn() + 'static) -> Group {
351 self.0.on_double_click(handler);
352 self
353 }
354
355 /// Called when it's clicked, or anything inside it that isn't a
356 /// control (a label, an icon): a header that opens its section, as
357 /// GTK's and WinUI's expanders do. Pointers only, as for
358 /// `on_double_click`: keyboards and assistive technology need a
359 /// control inside (the header's disclosure button).
360 pub fn on_click(mut self, handler: impl Fn() + 'static) -> Group {
361 self.0.on_click(handler);
362 self
363 }
364
365 /// Runs `handler` when `key` is pressed while it, or a control
366 /// inside it, has keyboard focus, and the focused control doesn't use
367 /// the key itself: Space for a preview, Delete for Move to Trash.
368 /// A key goes to the nearest node around the focused control that
369 /// takes it. Nothing shows keys taken this way, so give the command a
370 /// menu item or a button too, and pick the keys the platform's own apps
371 /// use (`platform!`).
372 ///
373 /// ```ignore
374 /// Group::new().on_key(Shortcut::primary(Key::Backspace), trash)
375 /// ```
376 pub fn on_key(mut self, key: impl Into<Shortcut>, handler: impl Fn() + 'static) -> Group {
377 self.0.on_key(key.into(), handler);
378 self
379 }
380
381 /// Space between its children.
382 pub fn gap(mut self, gap: impl IntoValue<Length>) -> Group {
383 self.0.style_prop(gap.into_value(), |s, v| {
384 s.row_gap = v;
385 s.column_gap = v;
386 });
387 self
388 }
389
390 /// Raw platform settings, past the semantic ones: see [`Tweak`].
391 pub fn native(mut self, tweak: Tweak<Group>) -> Group {
392 tweak.apply(&mut self.0);
393 self
394 }
395}
396
397/// Two panes with a divider between them that the user drags, as the
398/// platform splits a window: `NSSplitView`, `GtkPaned`, Qt's `SplitView`,
399/// and on Windows a grip on the panes' edge, as File Explorer's navigation
400/// pane has (WinUI has no splitter). Side by side, or one over the other
401/// with `Orientation::Vertical`.
402///
403/// Its two children are the panes. The first is as long along the split as
404/// its position (its content's, until there's one), and keeps it as the
405/// split grows; the second takes the rest. A pane's least width (or height)
406/// in its style is as far as the divider goes.
407///
408/// ```ignore
409/// let sidebar = signal(250.0);
410/// Split::new().bind(sidebar).children((branches.min_width(200), history.min_width(100)))
411/// ```
412pub struct Split(Element);
413widget!(Split);
414
415impl Default for Split {
416 fn default() -> Split {
417 Split::new()
418 }
419}
420
421impl Split {
422 pub fn new() -> Split {
423 Split(Element::new(WidgetKind::Split))
424 }
425
426 /// Its panes, first then second.
427 pub fn children(mut self, children: impl Children) -> Split {
428 self.0.add_children(children);
429 self
430 }
431
432 /// The first pane's length along the split, in points.
433 pub fn position(mut self, position: impl IntoValue<f64>) -> Split {
434 self.0.prop(position.into_value(), Prop::Number);
435 self
436 }
437
438 /// Two-way binding, Vue's `v-model`.
439 pub fn bind(self, signal: Signal<f64>) -> Split {
440 self.position(signal).on_change(move |position| signal.set(position))
441 }
442
443 /// Horizontal (the default) puts the panes side by side.
444 pub fn orientation(mut self, orientation: impl IntoValue<Orientation>) -> Split {
445 self.0.prop(orientation.into_value(), Prop::Orientation);
446 self
447 }
448
449 /// Called with the first pane's new length as the user drags the
450 /// divider.
451 pub fn on_change(mut self, handler: impl Fn(f64) + 'static) -> Split {
452 self.0.on(move |event| {
453 if let UiEvent::Changed(EventValue::Number(position)) = event {
454 handler(*position);
455 }
456 });
457 self
458 }
459
460 /// Raw platform settings, past the semantic ones: see [`Tweak`].
461 pub fn native(mut self, tweak: Tweak<Split>) -> Split {
462 tweak.apply(&mut self.0);
463 self
464 }
465}
466
467impl Split {
468 /// `<Split position=sidebar>…</Split>`
469 #[doc(hidden)]
470 pub fn __tag() -> Split {
471 Split::new()
472 }
473
474 #[doc(hidden)]
475 pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Split {
476 self.children(children())
477 }
478}
479
480impl Group {
481 /// `<Group title="CD drive">…</Group>`
482 #[doc(hidden)]
483 pub fn __tag() -> Group {
484 Group::new()
485 }
486
487 #[doc(hidden)]
488 pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Group {
489 self.children(children())
490 }
491}
492
493impl Container {
494 #[doc(hidden)]
495 pub fn __tag() -> Container {
496 Container::new()
497 }
498
499 #[doc(hidden)]
500 pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Container {
501 self.children(children())
502 }
503}
504
505impl Column {
506 #[doc(hidden)]
507 pub fn __tag() -> Container {
508 Column::new()
509 }
510}
511
512impl Row {
513 #[doc(hidden)]
514 pub fn __tag() -> Container {
515 Row::new()
516 }
517}
518
519impl Grid {
520 #[doc(hidden)]
521 pub fn __tag() -> Container {
522 Grid::new()
523 }
524}