use mitsuami_core::services::{Menu, MenuEntries, install_button_menu, install_button_menu_on_open};
use mitsuami_core::{
ButtonRole, ButtonStyle, Color, Element, ElementBuilder, EventValue, NodeId, Prop, Tweak, Ui, UiEvent, VectorIcon,
View, WidgetKind,
};
use mitsuami_reactive::{IntoValue, Signal};
pub struct Button(Element);
widget!(Button);
impl Button {
pub fn new(label: impl IntoValue<String>) -> Button {
let mut element = Element::new(WidgetKind::Button);
element.prop(label.into_value(), Prop::Label);
Button(element)
}
pub fn role(mut self, role: impl IntoValue<ButtonRole>) -> Button {
self.0.prop(role.into_value(), Prop::ButtonRole);
self
}
pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> Button {
self.0.prop(style.into_value(), Prop::ButtonStyle);
self
}
pub fn vector_icon(mut self, icon: impl IntoValue<VectorIcon>) -> Button {
self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
self
}
pub fn icon(mut self, name: impl IntoValue<String>) -> Button {
self.0.prop(name.into_value(), Prop::Icon);
self
}
pub fn icon_only(mut self, only: impl IntoValue<bool>) -> Button {
self.0.prop(only.into_value(), Prop::IconOnly);
self
}
pub fn icon_color(mut self, color: impl IntoValue<Color>) -> Button {
self.0.prop(color.into_value(), Prop::TextColor);
self
}
pub fn native(mut self, tweak: Tweak<Button>) -> Button {
tweak.apply(&mut self.0);
self
}
pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> Button {
self.0.prop(enabled.into_value(), Prop::Enabled);
self
}
pub fn on_click(mut self, handler: impl Fn() + 'static) -> Button {
self.0.on(move |event| {
if *event == UiEvent::Click {
handler();
}
});
self
}
}
pub struct ToggleButton(Element);
widget!(ToggleButton);
impl ToggleButton {
pub fn new(label: impl IntoValue<String>) -> ToggleButton {
let mut element = Element::new(WidgetKind::ToggleButton);
element.prop(label.into_value(), Prop::Label);
ToggleButton(element)
}
pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> ToggleButton {
self.0.prop(style.into_value(), Prop::ButtonStyle);
self
}
pub fn vector_icon(mut self, icon: impl IntoValue<VectorIcon>) -> ToggleButton {
self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
self
}
pub fn icon(mut self, name: impl IntoValue<String>) -> ToggleButton {
self.0.prop(name.into_value(), Prop::Icon);
self
}
pub fn icon_only(mut self, only: impl IntoValue<bool>) -> ToggleButton {
self.0.prop(only.into_value(), Prop::IconOnly);
self
}
pub fn icon_color(mut self, color: impl IntoValue<Color>) -> ToggleButton {
self.0.prop(color.into_value(), Prop::TextColor);
self
}
pub fn native(mut self, tweak: Tweak<ToggleButton>) -> ToggleButton {
tweak.apply(&mut self.0);
self
}
}
pub struct MenuButton(Element);
widget!(MenuButton);
impl MenuButton {
pub fn new(label: impl IntoValue<String>) -> MenuButton {
let mut element = Element::new(WidgetKind::MenuButton);
element.prop(label.into_value(), Prop::Label);
MenuButton(element)
}
pub fn menu(mut self, entries: impl MenuEntries + 'static) -> MenuButton {
let menu = Menu::new(String::new()).children(entries);
self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
self
}
pub fn menu_with<E: MenuEntries>(mut self, entries: impl Fn() -> E + 'static) -> MenuButton {
let menu = Menu::new(String::new()).children_with(entries);
self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
self
}
pub fn menu_on_open<E: MenuEntries>(mut self, entries: impl Fn() -> E + 'static) -> MenuButton {
let build: std::rc::Rc<dyn Fn() -> Menu> =
std::rc::Rc::new(move || Menu::new(String::new()).children(entries()));
self.0.after_build(move |ui, id| install_button_menu_on_open(ui, id, build));
self
}
pub fn vector_icon(mut self, icon: impl IntoValue<VectorIcon>) -> MenuButton {
self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
self
}
pub fn icon(mut self, name: impl IntoValue<String>) -> MenuButton {
self.0.prop(name.into_value(), Prop::Icon);
self
}
pub fn icon_only(mut self, only: impl IntoValue<bool>) -> MenuButton {
self.0.prop(only.into_value(), Prop::IconOnly);
self
}
pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> MenuButton {
self.0.prop(style.into_value(), Prop::ButtonStyle);
self
}
pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> MenuButton {
self.0.prop(enabled.into_value(), Prop::Enabled);
self
}
pub fn native(mut self, tweak: Tweak<MenuButton>) -> MenuButton {
tweak.apply(&mut self.0);
self
}
}
pub struct SplitButton(Element);
widget!(SplitButton);
impl SplitButton {
pub fn new(label: impl IntoValue<String>) -> SplitButton {
let mut element = Element::new(WidgetKind::SplitButton);
element.prop(label.into_value(), Prop::Label);
SplitButton(element)
}
pub fn menu(mut self, entries: impl MenuEntries + 'static) -> SplitButton {
let menu = Menu::new(String::new()).children(entries);
self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
self
}
pub fn menu_with<E: MenuEntries>(mut self, entries: impl Fn() -> E + 'static) -> SplitButton {
let menu = Menu::new(String::new()).children_with(entries);
self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
self
}
pub fn vector_icon(mut self, icon: impl IntoValue<VectorIcon>) -> SplitButton {
self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
self
}
pub fn icon(mut self, name: impl IntoValue<String>) -> SplitButton {
self.0.prop(name.into_value(), Prop::Icon);
self
}
pub fn icon_only(mut self, only: impl IntoValue<bool>) -> SplitButton {
self.0.prop(only.into_value(), Prop::IconOnly);
self
}
pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> SplitButton {
self.0.prop(style.into_value(), Prop::ButtonStyle);
self
}
pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> SplitButton {
self.0.prop(enabled.into_value(), Prop::Enabled);
self
}
pub fn on_click(mut self, handler: impl Fn() + 'static) -> SplitButton {
self.0.on(move |event| {
if *event == UiEvent::Click {
handler();
}
});
self
}
pub fn native(mut self, tweak: Tweak<SplitButton>) -> SplitButton {
tweak.apply(&mut self.0);
self
}
}
toggle!(ToggleButton);
text_tag!(Button, Label);
text_tag!(ToggleButton, Label);
text_tag!(MenuButton, Label);
text_tag!(SplitButton, Label);