mitsuami_widgets/
color_well.rs1use mitsuami_core::{Color, Element, ElementBuilder, EventValue, NodeId, Prop, Tweak, Ui, UiEvent, View, WidgetKind};
4use mitsuami_reactive::{IntoValue, Signal};
5
6pub struct ColorWell(Element);
16widget!(ColorWell);
17
18impl ColorWell {
19 pub fn new(label: impl IntoValue<String>) -> ColorWell {
21 let mut element = Element::new(WidgetKind::ColorWell);
22 element.prop(label.into_value(), Prop::Label);
23 ColorWell(element)
24 }
25
26 pub fn label(mut self, label: impl IntoValue<String>) -> ColorWell {
29 self.0.prop(label.into_value(), Prop::Label);
30 self
31 }
32
33 pub fn color(mut self, color: impl IntoValue<Color>) -> ColorWell {
35 let opaque = |color: Color| match color {
36 Color::Rgba(r, g, b, _) => Color::rgb(r, g, b),
37 other => other,
38 };
39 self.0.prop(color.into_value(), move |c| Prop::ColorValue(opaque(c)));
40 self
41 }
42
43 pub fn bind(self, signal: Signal<Color>) -> ColorWell {
45 self.color(signal).on_change(move |color| signal.set(color))
46 }
47
48 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> ColorWell {
49 self.0.prop(enabled.into_value(), Prop::Enabled);
50 self
51 }
52
53 pub fn on_change(mut self, handler: impl Fn(Color) + 'static) -> ColorWell {
57 self.0.on(move |event| {
58 if let UiEvent::Changed(EventValue::Color(color)) = event {
59 handler(*color);
60 }
61 });
62 self
63 }
64
65 pub fn native(mut self, tweak: Tweak<ColorWell>) -> ColorWell {
67 tweak.apply(&mut self.0);
68 self
69 }
70
71 #[doc(hidden)]
73 pub fn __tag() -> ColorWell {
74 ColorWell(Element::new(WidgetKind::ColorWell))
75 }
76}