1use egui::{Response, Ui};
8use egui_pop_core::binding::{self, Binding};
9use egui_pop_core::lock;
10use egui_pop_core::strings::{self, RESET};
11use egui_pop_core::ui::UiPop as _;
12use egui_pop_core::widgets;
13use egui_pop_model::commands::Origin;
14use egui_pop_model::ids::OwnerId;
15use egui_pop_model::values::{Change, Descriptor, Set, Type, Value};
16
17use crate::app::AppHandle;
18use crate::hooks::SettingEntry;
19use crate::strings as txt;
20
21pub fn form(ui: &mut Ui, app: &AppHandle) {
23 let entries = app.settings();
24 let mut sections: Vec<(String, Vec<SettingEntry>)> = Vec::new();
25 let ctx = ui.ctx().clone();
26 for e in entries {
27 match sections.iter_mut().find(|(s, _)| *s == e.section) {
28 Some((_, list)) => list.push(e),
29 None => sections.push((e.section.clone(), vec![e])),
30 }
31 }
32 for (section, list) in sections {
33 ui.add_space(ui.spacing().item_spacing.y * 2.0);
34 if let Some(first) = list.first() {
35 ui.strong(app.section_title(&ctx, &first.owner, §ion));
36 }
37 egui::Grid::new(("egui_pop.settings", §ion))
38 .num_columns(3)
39 .spacing([
40 ui.spacing().item_spacing.x * 2.0,
41 ui.spacing().item_spacing.y,
42 ])
43 .show(ui, |ui| {
44 for entry in list {
45 row(ui, app, &entry);
46 ui.end_row();
47 }
48 });
49 }
50}
51
52fn row(ui: &mut Ui, app: &AppHandle, entry: &SettingEntry) {
53 let store = app.store();
54 let d: &Descriptor = &entry.descriptor;
55 let path = entry.path.as_str();
56 let ctx = ui.ctx().clone();
57 let id = entry.id.as_str();
58 let text = strings::setting_label(&ctx, id, &d.label);
59 let label = ui.label(&text);
60 let label = if d.description.is_empty() {
61 label
62 } else {
63 label.on_hover_text(strings::setting_description(&ctx, id, &d.description))
64 };
65 let named = |r: Response| r.labelled_by(label.id);
66 ui.horizontal(|ui| match d.ty {
67 Type::Bool => {
68 binding::edit(ui, Binding::bound(&store, path), |ui, v: &mut bool| {
69 named(widgets::checkbox(ui, v, ""))
70 });
71 }
72 Type::Float => {
73 let (lo, hi) = d.soft.or(d.hard).unwrap_or((0.0, 1.0));
74 binding::edit(ui, Binding::bound(&store, path), |ui, v: &mut f64| {
75 let slider = named(
76 ui.add(
77 egui::Slider::new(v, lo..=hi)
78 .step_by(d.step)
79 .show_value(false),
80 ),
81 );
82 let mut value = egui::DragValue::new(v)
83 .range(lo..=hi)
84 .speed(d.step)
85 .max_decimals(d.precision as usize);
86 if let Some(u) = &d.unit {
87 value = value.suffix(format!(" {u}"));
88 }
89 slider | named(ui.add(value))
90 });
91 }
92 Type::Int => {
93 let (lo, hi) = d.hard.unwrap_or((0.0, 100.0));
94 let (lo, hi) = (lo as i64, hi as i64);
95 binding::edit(ui, Binding::bound(&store, path), |ui, v: &mut i64| {
96 let slider = named(ui.add(egui::Slider::new(v, lo..=hi).show_value(false)));
97 slider | named(ui.add(egui::DragValue::new(v).range(lo..=hi)))
98 });
99 }
100 Type::Text => {
101 binding::edit(ui, Binding::bound(&store, path), |ui, v: &mut String| {
102 let r = named(ui.text_edit_singleline(v));
103 egui_pop_core::focus_mode::follow_pointer(&r);
104 r
105 });
106 }
107 Type::Choice => {
108 let current = lock(&store)
109 .get(path)
110 .and_then(|e| e.value.as_choice().map(str::to_string))
111 .unwrap_or_default();
112 let mut picked = current.clone();
113 let combo =
114 widgets::combo_box(path).selected_text(strings::setting_choice(&ctx, id, ¤t));
115 let r = ui
116 .combo_box(combo, |ui| {
117 for c in &d.choices {
118 widgets::selectable_value(
119 ui,
120 &mut picked,
121 c.clone(),
122 strings::setting_choice(&ctx, id, c),
123 );
124 }
125 })
126 .response;
127 named(r);
128 if picked != current {
129 let _ = lock(&store).set(
130 Set::new(path, Change::To(Value::Choice(picked)), Origin::User)
131 .by(&OwnerId::FRAMEWORK),
132 );
133 }
134 }
135 Type::Floats | Type::Ints => {
136 let shown = lock(&store)
137 .get(path)
138 .map(|e| format!("{}", e.value.to_json()))
139 .unwrap_or_default();
140 ui.monospace(shown);
141 }
142 });
143 let changed = lock(&store)
144 .get(path)
145 .is_some_and(|e| e.value != e.descriptor.default);
146 let reset = strings::tr(ui.ctx(), &RESET);
147 let setting = [("setting", text.as_str())];
148 if ui
149 .add_enabled(changed, egui::Button::new(&reset).small())
150 .on_hover_text(strings::tr_args(
151 ui.ctx(),
152 &txt::SETTINGS_RESET_SETTING,
153 &setting,
154 ))
155 .on_disabled_hover_text(strings::tr_args(
156 ui.ctx(),
157 &txt::SETTINGS_AT_DEFAULT,
158 &setting,
159 ))
160 .clicked()
161 {
162 let _ =
163 lock(&store).set(Set::new(path, Change::Reset, Origin::User).by(&OwnerId::FRAMEWORK));
164 }
165}