use std::rc::Rc;
use mitsuami_core::{Element, ElementBuilder, EventValue, NodeId, Prop, Tweak, Ui, UiEvent, View, WidgetKind};
use mitsuami_reactive::{IntoValue, Signal, Value};
pub struct Select {
element: Element,
options: Value<Vec<String>>,
selected: Value<usize>,
}
impl ElementBuilder for Select {
fn element(&mut self) -> &mut Element {
&mut self.element
}
}
impl View for Select {
fn build(mut self, ui: &Ui) -> NodeId {
let chosen = |index: usize, count: usize| (count > 0).then_some(if index < count { index } else { 0 });
let selected = match (self.selected, self.options.clone()) {
(Value::Static(index), Value::Static(options)) => Value::Static(chosen(index, options.len())),
(index, options) => Value::Dynamic(Rc::new(move || chosen(index.get(), options.get().len()))),
};
self.element.prop(self.options, Prop::Options);
self.element.prop(selected, Prop::SelectedIndex);
self.element.build(ui)
}
}
impl Select {
pub fn new(label: impl IntoValue<String>) -> Select {
let mut element = Element::new(WidgetKind::Select);
element.prop(label.into_value(), Prop::Label);
Select { element, options: Value::Static(Vec::new()), selected: Value::Static(0) }
}
pub fn label(mut self, label: impl IntoValue<String>) -> Select {
self.element.prop(label.into_value(), Prop::Label);
self
}
pub fn options(mut self, options: impl IntoValue<Vec<String>>) -> Select {
self.options = options.into_value();
self
}
pub fn selected(mut self, index: impl IntoValue<usize>) -> Select {
self.selected = index.into_value();
self
}
pub fn bind(self, signal: Signal<usize>) -> Select {
self.selected(signal).on_change(move |index| signal.set(index))
}
pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> Select {
self.element.prop(enabled.into_value(), Prop::Enabled);
self
}
pub fn native(mut self, tweak: Tweak<Select>) -> Select {
tweak.apply(&mut self.element);
self
}
pub fn on_change(mut self, handler: impl Fn(usize) + 'static) -> Select {
self.element.on(move |event| {
if let UiEvent::Changed(EventValue::Index(index)) = event {
handler(*index);
}
});
self
}
}
pub struct RadioGroup {
element: Element,
options: Value<Vec<String>>,
selected: Value<Option<usize>>,
}
impl ElementBuilder for RadioGroup {
fn element(&mut self) -> &mut Element {
&mut self.element
}
}
impl View for RadioGroup {
fn build(mut self, ui: &Ui) -> NodeId {
let chosen = |index: Option<usize>, count: usize| index.filter(|i| *i < count);
let selected = match (self.selected, self.options.clone()) {
(Value::Static(index), Value::Static(options)) => Value::Static(chosen(index, options.len())),
(index, options) => Value::Dynamic(Rc::new(move || chosen(index.get(), options.get().len()))),
};
self.element.prop(self.options, Prop::Options);
self.element.prop(selected, Prop::SelectedIndex);
self.element.build(ui)
}
}
impl RadioGroup {
pub fn new(label: impl IntoValue<String>) -> RadioGroup {
let mut element = Element::new(WidgetKind::RadioGroup);
element.prop(label.into_value(), Prop::Label);
RadioGroup { element, options: Value::Static(Vec::new()), selected: Value::Static(None) }
}
pub fn label(mut self, label: impl IntoValue<String>) -> RadioGroup {
self.element.prop(label.into_value(), Prop::Label);
self
}
pub fn options(mut self, options: impl IntoValue<Vec<String>>) -> RadioGroup {
self.options = options.into_value();
self
}
pub fn selected(mut self, index: impl IntoValue<Option<usize>>) -> RadioGroup {
self.selected = index.into_value();
self
}
pub fn bind(self, signal: Signal<Option<usize>>) -> RadioGroup {
self.selected(signal).on_change(move |index| signal.set(Some(index)))
}
pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> RadioGroup {
self.element.prop(enabled.into_value(), Prop::Enabled);
self
}
pub fn native(mut self, tweak: Tweak<RadioGroup>) -> RadioGroup {
tweak.apply(&mut self.element);
self
}
pub fn on_change(mut self, handler: impl Fn(usize) + 'static) -> RadioGroup {
self.element.on(move |event| {
if let UiEvent::Changed(EventValue::Index(index)) = event {
handler(*index);
}
});
self
}
}
impl Select {
#[doc(hidden)]
pub fn __tag() -> Select {
Select {
element: Element::new(WidgetKind::Select),
options: Value::Static(Vec::new()),
selected: Value::Static(0),
}
}
}
impl RadioGroup {
#[doc(hidden)]
pub fn __tag() -> RadioGroup {
RadioGroup {
element: Element::new(WidgetKind::RadioGroup),
options: Value::Static(Vec::new()),
selected: Value::Static(None),
}
}
}