1use egui::{Response, Ui};
23use egui_pop_model::values::{Descriptor, Type, Value};
24
25use crate::binding::{self, Bindable, Binding, Choice, SharedStore};
26use crate::number::NumberInput;
27use crate::slider::Slider;
28use crate::ui::UiPop as _;
29use crate::widgets;
30
31#[derive(Debug)]
33pub enum Mode<'a> {
34 Plain(&'a mut Value),
36 Bound(&'a SharedStore, &'a str),
38 Watch(&'a SharedStore, &'a str),
40}
41
42fn with<T: Bindable>(
43 ui: &mut Ui,
44 mode: Mode<'_>,
45 widget: impl FnOnce(&mut Ui, &mut T) -> Response,
46) -> Response {
47 match mode {
48 Mode::Plain(v) => {
49 let mut t = T::from_value(v).unwrap_or_default();
50 let before = t.clone();
51 let r = widget(ui, &mut t);
52 if t != before {
53 *v = t.to_value();
54 }
55 r
56 }
57 Mode::Bound(store, path) => binding::edit(ui, Binding::bound(store, path), widget),
58 Mode::Watch(store, path) => binding::edit(ui, Binding::watch(store, path), widget),
59 }
60}
61
62fn labelled(ui: &mut Ui, label: &str, add: impl FnOnce(&mut Ui) -> Response) -> Response {
64 ui.horizontal(|ui| {
65 let l = ui.label(label);
66 add(ui).labelled_by(l.id)
67 })
68 .inner
69}
70
71pub fn is_colour(d: &Descriptor) -> bool {
73 d.ty == Type::Floats
74 && matches!(d.count, Some(3 | 4))
75 && d.hard.is_some_and(|(lo, hi)| lo >= 0.0 && hi <= 1.0)
76}
77
78pub fn param(ui: &mut Ui, d: &Descriptor, mode: Mode<'_>) -> Response {
80 param_shown(ui, d, mode, &|c| c.to_string())
81}
82
83pub fn bound(ui: &mut Ui, store: &SharedStore, path: &str) -> Response {
102 let descriptor = crate::lock(store).get(path).map(|e| e.descriptor.clone());
103 match descriptor {
104 Some(d) => param(ui, &d, Mode::Bound(store, path)),
105 None => ui.label(format!("{path}: not declared")),
106 }
107}
108
109pub fn param_shown(
112 ui: &mut Ui,
113 d: &Descriptor,
114 mode: Mode<'_>,
115 shown: &dyn Fn(&str) -> String,
116) -> Response {
117 match d.ty {
118 Type::Float if crate::number::is_wide(d) => labelled(ui, &d.label, |ui| {
120 with(ui, mode, |ui, v: &mut f64| {
121 NumberInput::from_descriptor(v, d).show(ui)
122 })
123 }),
124 Type::Int if crate::number::is_wide(d) => labelled(ui, &d.label, |ui| {
125 with(ui, mode, |ui, v: &mut i64| {
126 let mut f = *v as f64;
127 let r = NumberInput::from_descriptor(&mut f, d).show(ui);
128 *v = f.round() as i64;
129 r
130 })
131 }),
132 Type::Float => with(ui, mode, |ui, v: &mut f64| {
133 ui.add(Slider::from_descriptor(v, d))
134 }),
135 Type::Int => with(ui, mode, |ui, v: &mut i64| {
136 let mut f = *v as f64;
137 let r = ui.add(Slider::from_descriptor(&mut f, d).integer());
138 *v = f.round() as i64;
139 r
140 }),
141 Type::Bool => with(ui, mode, |ui, v: &mut bool| {
142 widgets::checkbox(ui, v, &d.label)
143 }),
144 Type::Choice => labelled(ui, &d.label, |ui| {
145 with(ui, mode, |ui, v: &mut Choice| {
146 let mut picked = v.0.clone();
147 let combo =
148 widgets::combo_box(("egui_pop.param", &d.label)).selected_text(shown(&picked));
149 let r = ui
150 .combo_box(combo, |ui| {
151 for c in &d.choices {
152 widgets::selectable_value(ui, &mut picked, c.clone(), shown(c));
153 }
154 })
155 .response;
156 crate::a11y::input(ui.ctx(), r.id, &d.label);
157 if picked != v.0 {
158 v.0 = picked;
159 }
160 r
161 })
162 }),
163 Type::Floats if is_colour(d) => labelled(ui, &d.label, |ui| {
164 with(ui, mode, |ui, v: &mut Vec<f64>| {
165 v.resize(d.count.unwrap_or(3) as usize, 1.0);
166 let before = [v[0] as f32, v[1] as f32, v[2] as f32];
167 let mut rgb = before;
168 let r = ui.color_edit_button_rgb(&mut rgb);
169 if r.changed() && rgb != before {
172 for (i, c) in rgb.iter().enumerate() {
173 v[i] = f64::from(*c);
174 }
175 }
176 r
177 })
178 }),
179 Type::Text => labelled(ui, &d.label, |ui| {
180 with(ui, mode, |ui, v: &mut String| {
181 let r = ui.add(egui::TextEdit::singleline(v).password(d.sensitive));
184 crate::focus_mode::follow_pointer(&r);
185 r
186 })
187 }),
188 Type::Floats | Type::Ints => labelled(ui, &d.label, |ui| {
189 let shown = match mode {
190 Mode::Plain(v) => v.to_json().to_string(),
191 Mode::Bound(s, p) | Mode::Watch(s, p) => crate::lock(s)
192 .get(p)
193 .map(|e| e.value.to_json().to_string())
194 .unwrap_or_default(),
195 };
196 ui.monospace(shown)
197 }),
198 }
199}
200
201pub fn current(mode: &Mode<'_>) -> Option<Value> {
203 match mode {
204 Mode::Plain(v) => Some((**v).clone()),
205 Mode::Bound(s, p) | Mode::Watch(s, p) => crate::lock(s).get(p).map(|e| e.value.clone()),
206 }
207}