Skip to main content

imgui_inspect/default/
default_string.rs

1use super::*;
2
3impl InspectRenderDefault<String> for String {
4    fn render(
5        data: &[&String],
6        label: &'static str,
7        ui: &imgui::Ui,
8        _args: &InspectArgsDefault,
9    ) {
10        if data.is_empty() {
11            // Values are inconsistent
12            let style_token = ui.push_style_color(imgui::StyleColor::Text, [1.0, 0.0, 0.0, 1.0]);
13            ui.text(&imgui::im_str!("{}: ", label));
14            style_token.pop(ui);
15            return;
16        }
17
18        match get_same_or_none(data) {
19            Some(_v) => {
20                // Values are consistent
21                ui.text(&imgui::im_str!("{}: {}", label, data[0]))
22            }
23            None => {
24                // Values are inconsistent
25                let style_token =
26                    ui.push_style_color(imgui::StyleColor::Text, [1.0, 1.0, 0.0, 1.0]);
27                ui.text(&imgui::im_str!("{}: ", label));
28                style_token.pop(ui);
29            }
30        }
31    }
32
33    fn render_mut(
34        data: &mut [&mut String],
35        label: &'static str,
36        ui: &imgui::Ui,
37        _args: &InspectArgsDefault,
38    ) -> bool {
39        let same_or_none_value = get_same_or_none_mut(data);
40
41        let style_token = if same_or_none_value.is_none() {
42            // If values are inconsistent, push a style
43            Some(ui.push_style_color(imgui::StyleColor::Text, [1.0, 1.0, 0.0, 1.0]))
44        } else {
45            None
46        };
47
48        let value = match same_or_none_value {
49            Some(v) => v,
50            None => "".to_string(), // Some reasonable default
51        };
52
53        let mut changed = false;
54        let mut value = imgui::im_str!("{}", value);
55        if ui
56            .input_text(&imgui::im_str!("{}", label), &mut value)
57            .resize_buffer(true)
58            .build()
59        {
60            for d in data {
61                **d = value.to_string();
62                changed = true;
63            }
64        }
65
66        if let Some(style_token) = style_token {
67            style_token.pop(ui);
68        }
69
70        changed
71    }
72}