use std::rc::Rc;
use std::path::PathBuf;
use mitsuami_core::services::Shortcut;
use mitsuami_core::{
Align, Children, Display, Element, ElementBuilder, FileDrop, FlexDirection, Justify, Length, NodeId, Prop, Track,
Tweak, Ui, UiEvent, View, WidgetKind,
};
use mitsuami_reactive::{IntoValue, Value};
pub struct Container(pub(crate) Element);
widget!(Container);
impl Default for Container {
fn default() -> Container {
Container::new()
}
}
impl Container {
pub fn new() -> Container {
Container(Element::new(WidgetKind::Container))
}
pub fn children(mut self, children: impl Children) -> Container {
self.0.add_children(children);
self
}
pub fn child(self, child: impl View) -> Container {
self.children(child)
}
pub fn file_drop(mut self, drop: impl IntoValue<FileDrop>) -> Container {
let drop = match drop.into_value() {
Value::Static(d) => Value::Static(Some(d)),
dynamic => Value::Dynamic(Rc::new(move || Some(dynamic.get()))),
};
self.0.prop(drop, Prop::FileDrop);
self
}
pub fn on_drop(mut self, handler: impl Fn(Vec<PathBuf>) + 'static) -> Container {
self.0.on(move |event| {
if let UiEvent::FilesDropped(paths) = event {
handler(paths.clone());
}
});
self
}
pub fn on_drop_hover(mut self, handler: impl Fn(bool) + 'static) -> Container {
self.0.on(move |event| {
if let UiEvent::DropHover(over) = event {
handler(*over);
}
});
self
}
pub fn on_hover(mut self, handler: impl Fn(bool) + 'static) -> Container {
self.0.on_hover(handler);
self
}
pub fn on_double_click(mut self, handler: impl Fn() + 'static) -> Container {
self.0.on_double_click(handler);
self
}
pub fn on_key(mut self, key: impl Into<Shortcut>, handler: impl Fn() + 'static) -> Container {
self.0.on_key(key.into(), handler);
self
}
pub fn flex_direction(mut self, direction: impl IntoValue<FlexDirection>) -> Container {
self.0.style_prop(direction.into_value(), |s, v| s.flex_direction = v);
self
}
pub fn gap(mut self, gap: impl IntoValue<Length>) -> Container {
self.0.style_prop(gap.into_value(), |s, v| {
s.row_gap = v;
s.column_gap = v;
});
self
}
pub fn row_gap(mut self, gap: impl IntoValue<Length>) -> Container {
self.0.style_prop(gap.into_value(), |s, v| s.row_gap = v);
self
}
pub fn column_gap(mut self, gap: impl IntoValue<Length>) -> Container {
self.0.style_prop(gap.into_value(), |s, v| s.column_gap = v);
self
}
pub fn align(mut self, align: impl IntoValue<Align>) -> Container {
self.0.style_prop(align.into_value(), |s, v| s.align_items = Some(v));
self
}
pub fn justify(mut self, justify: impl IntoValue<Justify>) -> Container {
self.0.style_prop(justify.into_value(), |s, v| s.justify_content = Some(v));
self
}
pub fn wrap(self) -> Container {
self.style(|s| s.flex_wrap = true)
}
pub fn columns<T: Into<Track>>(self, tracks: impl IntoIterator<Item = T>) -> Container {
let tracks: Vec<Track> = tracks.into_iter().map(Into::into).collect();
self.style(|s| {
s.display = Display::Grid;
s.grid_template_columns = tracks;
})
}
pub fn rows<T: Into<Track>>(self, tracks: impl IntoIterator<Item = T>) -> Container {
let tracks: Vec<Track> = tracks.into_iter().map(Into::into).collect();
self.style(|s| {
s.display = Display::Grid;
s.grid_template_rows = tracks;
})
}
}
pub struct Column;
impl Column {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> Container {
Container::new().flex_direction(FlexDirection::Column)
}
}
pub struct Row;
impl Row {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> Container {
Container::new().flex_direction(FlexDirection::Row)
}
}
pub struct Grid;
impl Grid {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> Container {
Container::new().style(|s| s.display = Display::Grid)
}
}
pub struct Group(Element);
widget!(Group);
impl Default for Group {
fn default() -> Group {
Group::new()
}
}
impl Group {
pub fn new() -> Group {
Group(Element::new(WidgetKind::Group)).style(|s| {
s.display = Display::Flex;
s.flex_direction = FlexDirection::Column;
})
}
pub fn title(mut self, title: impl IntoValue<String>) -> Group {
self.0.prop(title.into_value(), Prop::Title);
self
}
pub fn children(mut self, children: impl Children) -> Group {
self.0.add_children(children);
self
}
pub fn child(self, child: impl View) -> Group {
self.children(child)
}
pub fn file_drop(mut self, drop: impl IntoValue<FileDrop>) -> Group {
let drop = match drop.into_value() {
Value::Static(d) => Value::Static(Some(d)),
dynamic => Value::Dynamic(Rc::new(move || Some(dynamic.get()))),
};
self.0.prop(drop, Prop::FileDrop);
self
}
pub fn on_drop(mut self, handler: impl Fn(Vec<PathBuf>) + 'static) -> Group {
self.0.on(move |event| {
if let UiEvent::FilesDropped(paths) = event {
handler(paths.clone());
}
});
self
}
pub fn on_drop_hover(mut self, handler: impl Fn(bool) + 'static) -> Group {
self.0.on(move |event| {
if let UiEvent::DropHover(over) = event {
handler(*over);
}
});
self
}
pub fn on_hover(mut self, handler: impl Fn(bool) + 'static) -> Group {
self.0.on_hover(handler);
self
}
pub fn on_double_click(mut self, handler: impl Fn() + 'static) -> Group {
self.0.on_double_click(handler);
self
}
pub fn on_key(mut self, key: impl Into<Shortcut>, handler: impl Fn() + 'static) -> Group {
self.0.on_key(key.into(), handler);
self
}
pub fn gap(mut self, gap: impl IntoValue<Length>) -> Group {
self.0.style_prop(gap.into_value(), |s, v| {
s.row_gap = v;
s.column_gap = v;
});
self
}
pub fn native(mut self, tweak: Tweak<Group>) -> Group {
tweak.apply(&mut self.0);
self
}
}
impl Group {
#[doc(hidden)]
pub fn __tag() -> Group {
Group::new()
}
#[doc(hidden)]
pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Group {
self.children(children())
}
}
impl Container {
#[doc(hidden)]
pub fn __tag() -> Container {
Container::new()
}
#[doc(hidden)]
pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Container {
self.children(children())
}
}
impl Column {
#[doc(hidden)]
pub fn __tag() -> Container {
Column::new()
}
}
impl Row {
#[doc(hidden)]
pub fn __tag() -> Container {
Row::new()
}
}
impl Grid {
#[doc(hidden)]
pub fn __tag() -> Container {
Grid::new()
}
}