mitsuami_widgets/
containers.rs1use std::rc::Rc;
4
5use std::path::PathBuf;
6
7use mitsuami_core::services::Shortcut;
8use mitsuami_core::{
9 Align, Children, Display, Element, ElementBuilder, FileDrop, FlexDirection, Justify, Length, NodeId, Prop, Track,
10 Tweak, Ui, UiEvent, View, WidgetKind,
11};
12use mitsuami_reactive::{IntoValue, Value};
13
14pub 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 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 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 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 pub fn on_hover(mut self, handler: impl Fn(bool) + 'static) -> Container {
80 self.0.on_hover(handler);
81 self
82 }
83
84 pub fn on_double_click(mut self, handler: impl Fn() + 'static) -> Container {
90 self.0.on_double_click(handler);
91 self
92 }
93
94 pub fn on_key(mut self, key: impl Into<Shortcut>, handler: impl Fn() + 'static) -> Container {
106 self.0.on_key(key.into(), handler);
107 self
108 }
109
110 pub fn flex_direction(mut self, direction: impl IntoValue<FlexDirection>) -> Container {
111 self.0.style_prop(direction.into_value(), |s, v| s.flex_direction = v);
112 self
113 }
114
115 pub fn gap(mut self, gap: impl IntoValue<Length>) -> Container {
117 self.0.style_prop(gap.into_value(), |s, v| {
118 s.row_gap = v;
119 s.column_gap = v;
120 });
121 self
122 }
123
124 pub fn row_gap(mut self, gap: impl IntoValue<Length>) -> Container {
125 self.0.style_prop(gap.into_value(), |s, v| s.row_gap = v);
126 self
127 }
128
129 pub fn column_gap(mut self, gap: impl IntoValue<Length>) -> Container {
130 self.0.style_prop(gap.into_value(), |s, v| s.column_gap = v);
131 self
132 }
133
134 pub fn align(mut self, align: impl IntoValue<Align>) -> Container {
136 self.0.style_prop(align.into_value(), |s, v| s.align_items = Some(v));
137 self
138 }
139
140 pub fn justify(mut self, justify: impl IntoValue<Justify>) -> Container {
142 self.0.style_prop(justify.into_value(), |s, v| s.justify_content = Some(v));
143 self
144 }
145
146 pub fn wrap(self) -> Container {
147 self.style(|s| s.flex_wrap = true)
148 }
149
150 pub fn columns<T: Into<Track>>(self, tracks: impl IntoIterator<Item = T>) -> Container {
152 let tracks: Vec<Track> = tracks.into_iter().map(Into::into).collect();
153 self.style(|s| {
154 s.display = Display::Grid;
155 s.grid_template_columns = tracks;
156 })
157 }
158
159 pub fn rows<T: Into<Track>>(self, tracks: impl IntoIterator<Item = T>) -> Container {
161 let tracks: Vec<Track> = tracks.into_iter().map(Into::into).collect();
162 self.style(|s| {
163 s.display = Display::Grid;
164 s.grid_template_rows = tracks;
165 })
166 }
167}
168
169pub struct Column;
172
173impl Column {
174 #[allow(clippy::new_ret_no_self)]
175 pub fn new() -> Container {
176 Container::new().flex_direction(FlexDirection::Column)
177 }
178}
179
180pub struct Row;
183
184impl Row {
185 #[allow(clippy::new_ret_no_self)]
186 pub fn new() -> Container {
187 Container::new().flex_direction(FlexDirection::Row)
188 }
189}
190
191pub struct Grid;
194
195impl Grid {
196 #[allow(clippy::new_ret_no_self)]
197 pub fn new() -> Container {
198 Container::new().style(|s| s.display = Display::Grid)
199 }
200}
201
202pub struct Group(Element);
217widget!(Group);
218
219impl Default for Group {
220 fn default() -> Group {
221 Group::new()
222 }
223}
224
225impl Group {
226 pub fn new() -> Group {
227 Group(Element::new(WidgetKind::Group)).style(|s| {
228 s.display = Display::Flex;
229 s.flex_direction = FlexDirection::Column;
230 })
231 }
232
233 pub fn title(mut self, title: impl IntoValue<String>) -> Group {
235 self.0.prop(title.into_value(), Prop::Title);
236 self
237 }
238
239 pub fn children(mut self, children: impl Children) -> Group {
240 self.0.add_children(children);
241 self
242 }
243
244 pub fn child(self, child: impl View) -> Group {
245 self.children(child)
246 }
247
248 pub fn file_drop(mut self, drop: impl IntoValue<FileDrop>) -> Group {
255 let drop = match drop.into_value() {
256 Value::Static(d) => Value::Static(Some(d)),
257 dynamic => Value::Dynamic(Rc::new(move || Some(dynamic.get()))),
258 };
259 self.0.prop(drop, Prop::FileDrop);
260 self
261 }
262
263 pub fn on_drop(mut self, handler: impl Fn(Vec<PathBuf>) + 'static) -> Group {
265 self.0.on(move |event| {
266 if let UiEvent::FilesDropped(paths) = event {
267 handler(paths.clone());
268 }
269 });
270 self
271 }
272
273 pub fn on_drop_hover(mut self, handler: impl Fn(bool) + 'static) -> Group {
275 self.0.on(move |event| {
276 if let UiEvent::DropHover(over) = event {
277 handler(*over);
278 }
279 });
280 self
281 }
282
283 pub fn on_hover(mut self, handler: impl Fn(bool) + 'static) -> Group {
290 self.0.on_hover(handler);
291 self
292 }
293
294 pub fn on_double_click(mut self, handler: impl Fn() + 'static) -> Group {
300 self.0.on_double_click(handler);
301 self
302 }
303
304 pub fn on_key(mut self, key: impl Into<Shortcut>, handler: impl Fn() + 'static) -> Group {
316 self.0.on_key(key.into(), handler);
317 self
318 }
319
320 pub fn gap(mut self, gap: impl IntoValue<Length>) -> Group {
322 self.0.style_prop(gap.into_value(), |s, v| {
323 s.row_gap = v;
324 s.column_gap = v;
325 });
326 self
327 }
328
329 pub fn native(mut self, tweak: Tweak<Group>) -> Group {
331 tweak.apply(&mut self.0);
332 self
333 }
334}
335
336impl Group {
337 #[doc(hidden)]
339 pub fn __tag() -> Group {
340 Group::new()
341 }
342
343 #[doc(hidden)]
344 pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Group {
345 self.children(children())
346 }
347}
348
349impl Container {
350 #[doc(hidden)]
351 pub fn __tag() -> Container {
352 Container::new()
353 }
354
355 #[doc(hidden)]
356 pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Container {
357 self.children(children())
358 }
359}
360
361impl Column {
362 #[doc(hidden)]
363 pub fn __tag() -> Container {
364 Column::new()
365 }
366}
367
368impl Row {
369 #[doc(hidden)]
370 pub fn __tag() -> Container {
371 Row::new()
372 }
373}
374
375impl Grid {
376 #[doc(hidden)]
377 pub fn __tag() -> Container {
378 Grid::new()
379 }
380}