1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use core::hash::Hash;

use crate::{EguiProbe, Style};

#[derive(Clone, Copy)]
struct ProbeHeaderState {
    open: bool,
    body_height: f32,
}

struct ProbeHeader {
    id: egui::Id,
    state: ProbeHeaderState,
    dirty: bool,
    openness: f32,
}

impl ProbeHeader {
    fn load(cx: &egui::Context, id: egui::Id) -> ProbeHeader {
        let state = cx.data_mut(|d| {
            *d.get_temp_mut_or(
                id,
                ProbeHeaderState {
                    open: false,
                    body_height: 0.0,
                },
            )
        });

        let openness = cx.animate_bool(id, state.open);

        ProbeHeader {
            id,
            state,
            dirty: false,
            openness,
        }
    }

    fn store(self, cx: &egui::Context) {
        if self.dirty {
            cx.data_mut(|d| d.insert_temp(self.id, self.state));
            cx.request_repaint();
        }
    }

    fn toggle(&mut self) {
        self.state.open = !self.state.open;
        self.dirty = true;
    }

    // fn is_open(&self) -> bool {
    //     self.state.open
    // }

    fn set_body_height(&mut self, height: f32) {
        // TODO: Better approximation
        if (self.state.body_height - height).abs() > 0.001 {
            self.state.body_height = height;
            self.dirty = true;
        }
    }

    fn body_shift(&self) -> f32 {
        (1.0 - self.openness) * self.state.body_height
    }

    fn collapse_button(&mut self, ui: &mut egui::Ui) -> egui::Response {
        let desired_size = ui.spacing().icon_width_inner;
        let response =
            ui.allocate_response(egui::vec2(desired_size, desired_size), egui::Sense::click());

        if response.clicked() {
            self.toggle();
        }

        egui::collapsing_header::paint_default_icon(ui, self.openness, &response);
        response
    }
}

#[derive(Clone, Copy)]
struct ProbeLayoutState {
    labels_width: f32,
}

pub struct ProbeLayout {
    id: egui::Id,
    state: ProbeLayoutState,
    dirty: bool,
    min_labels_width: f32,
}

impl ProbeLayout {
    fn load(cx: &egui::Context, id: egui::Id) -> ProbeLayout {
        let state = cx.data_mut(|d| *d.get_temp_mut_or(id, ProbeLayoutState { labels_width: 0.0 }));
        ProbeLayout {
            id,
            state,
            dirty: false,
            min_labels_width: 0.0,
        }
    }

    fn store(mut self, cx: &egui::Context) {
        if self.dirty {
            self.state.labels_width = self.min_labels_width;
            cx.data_mut(|d| d.insert_temp(self.id, self.state));
            cx.request_repaint();
        }
    }

    fn bump_labels_width(&mut self, width: f32) {
        if self.min_labels_width < width {
            self.min_labels_width = width;
            self.dirty = true;
        }
    }

    pub fn inner_label_ui(
        &mut self,
        indent: usize,
        id_source: impl Hash,
        ui: &mut egui::Ui,
        add_content: impl FnOnce(&mut egui::Ui) -> egui::Response,
    ) -> egui::Response {
        let labels_width = self.state.labels_width;
        let cursor = ui.cursor();

        let max = egui::pos2(cursor.max.x.min(cursor.min.x + labels_width), cursor.max.y);
        let min = egui::pos2(cursor.min.x, cursor.min.y);
        let rect = egui::Rect::from_min_max(min, max);

        let mut label_ui =
            ui.child_ui_with_id_source(rect.intersect(ui.max_rect()), *ui.layout(), id_source);
        label_ui.set_clip_rect(
            ui.clip_rect()
                .intersect(egui::Rect::everything_left_of(max.x)),
        );

        for _ in 0..indent {
            label_ui.separator();
        }

        let label_response = add_content(&mut label_ui);
        let mut final_rect = label_ui.min_rect();

        self.bump_labels_width(final_rect.width());

        final_rect.max.x = final_rect.min.x + labels_width;

        ui.advance_cursor_after_rect(final_rect);
        label_response
    }

    pub fn inner_value_ui(
        &mut self,
        id_source: impl Hash,
        ui: &mut egui::Ui,
        add_content: impl FnOnce(&mut egui::Ui),
    ) {
        let mut value_ui = ui.child_ui_with_id_source(
            ui.cursor().intersect(ui.max_rect()),
            *ui.layout(),
            id_source,
        );

        add_content(&mut value_ui);
        let final_rect = value_ui.min_rect();
        ui.advance_cursor_after_rect(final_rect);
    }
}

/// Widget for editing a value via `EguiProbe` trait.
///
/// For simple values it will show a probe UI for it.
/// For complex values it will header with collapsible body.
#[must_use = "You should call .show()"]
pub struct Probe<'a, T> {
    id_source: egui::Id,
    label: egui::WidgetText,
    style: Style,
    value: &'a mut T,
}

impl<'a, T> Probe<'a, T>
where
    T: EguiProbe,
{
    /// Creates a new `Probe` widget.
    pub fn new(label: impl Into<egui::WidgetText>, value: &'a mut T) -> Self {
        let label = label.into();
        Probe {
            id_source: egui::Id::new(label.text()),
            label,
            style: Style::default(),
            value,
        }
    }

    /// Show probbing UI to edit the value.
    pub fn show(self, ui: &mut egui::Ui) -> egui::Response {
        if !self.value.has_inner() {
            return self.value.probe(ui, &self.style);
        }

        ui.allocate_ui(ui.available_size(), |ui| {
            let ref mut child_ui = ui.child_ui_with_id_source(
                ui.max_rect(),
                egui::Layout::top_down(egui::Align::Min),
                self.id_source,
            );

            let mut header =
                ProbeHeader::load(child_ui.ctx(), child_ui.make_persistent_id("probe_header"));

            egui::Frame::none()
                .fill(child_ui.visuals().extreme_bg_color)
                .inner_margin(child_ui.spacing().item_spacing * 0.5)
                .show(child_ui, |child_ui| {
                    child_ui.horizontal(|child_ui| {
                        header.collapse_button(child_ui);
                        child_ui.label(self.label);
                    });
                });

            if header.openness > 0.0 && self.value.has_inner() {
                let mut layout =
                    ProbeLayout::load(child_ui.ctx(), child_ui.make_persistent_id("probe_layout"));

                show_table(
                    self.value,
                    &mut header,
                    &mut layout,
                    0,
                    child_ui,
                    &self.style,
                    "table",
                );

                layout.store(child_ui.ctx());
            }

            header.store(child_ui.ctx());

            let final_rect = child_ui.min_rect();
            ui.advance_cursor_after_rect(final_rect);

            // let response = ui.interact(final_rect, child_ui.id(), egui::Sense::hover());
            // response.widget_info(|| egui::WidgetInfo::new(egui::WidgetType::Other));

            // response
        })
        .response
    }
}

fn show_header(
    label: &str,
    value: &mut dyn EguiProbe,
    layout: &mut ProbeLayout,
    indent: usize,
    ui: &mut egui::Ui,
    style: &Style,
    id_source: impl Hash,
) -> Option<ProbeHeader> {
    let mut header = None;

    let id = ui.make_persistent_id(id_source);

    if value.has_inner() {
        header = Some(ProbeHeader::load(ui.ctx(), id));
    }

    ui.horizontal(|ui| {
        let label_response = layout.inner_label_ui(indent, id.with("label"), ui, |ui| {
            if let Some(header) = &mut header {
                header.collapse_button(ui);
            }
            ui.label(label)
        });

        layout.inner_value_ui(id.with("value"), ui, |ui| {
            value.probe(ui, style).labelled_by(label_response.id);
        });
    });

    header
}

fn show_table(
    value: &mut dyn EguiProbe,
    header: &mut ProbeHeader,
    layout: &mut ProbeLayout,
    indent: usize,
    ui: &mut egui::Ui,
    style: &Style,
    id_source: impl Hash,
) {
    let cursor = ui.cursor();

    let table_rect = egui::Rect::from_min_max(
        egui::pos2(cursor.min.x, cursor.min.y - header.body_shift()),
        ui.max_rect().max,
    );

    let mut table_ui = ui.child_ui_with_id_source(
        table_rect,
        egui::Layout::top_down(egui::Align::Min),
        id_source,
    );
    table_ui.set_clip_rect(
        ui.clip_rect()
            .intersect(egui::Rect::everything_below(ui.min_rect().max.y)),
    );

    let mut idx = 0;
    value.iterate_inner(&mut |label, value| {
        let header = show_header(label, value, layout, indent + 1, &mut table_ui, style, idx);

        if let Some(mut header) = header {
            if header.openness > 0.0 {
                show_table(
                    value,
                    &mut header,
                    layout,
                    indent + 1,
                    &mut table_ui,
                    style,
                    idx,
                );
            }
            header.store(table_ui.ctx());
        }

        idx += 1;
    });

    let final_table_rect = table_ui.min_rect();

    ui.advance_cursor_after_rect(final_table_rect);
    let table_height = ui.cursor().min.y - table_rect.min.y;
    header.set_body_height(table_height);
}