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
//! ## Optional params
//!
//! * `label`
//! * `flags` Name of a local function that returns the input [flags].
//! * `size` Name of a local function that returns the size of the field (multi-line text).
//! * `catch`
//!
//! [flags]: https://docs.rs/imgui/0.0/imgui/struct.ImGuiInputTextFlags.html
//!
//! ## Example
//!
//! ```
//! use imgui::ImString;
//! use imgui_ext::ImGuiExt;
//!
//! #[derive(ImGuiExt)]
//! struct Form {
//!     #[imgui(text)]
//!     name: ImString,
//!     #[imgui(text)]
//!     email: ImString,
//!     #[imgui(
//!         text(size = "size"),
//!         button(label = "submit")
//!     )]
//!     comment: ImString,
//! }
//!
//! fn size() -> (f32, f32) {
//!     (200.0, 100.0)
//! }
//! ```
//!
//! ### Result
//!
//! ![][result]
//!
//! [result]: https://i.imgur.com/8pEKoPn.png
use imgui::{ImGuiInputTextFlags, ImStr, ImString, ImVec2, InputText, InputTextMultiline, Ui};

pub struct TextParams<'ui> {
    pub label: &'ui ImStr,
    pub flags: Option<ImGuiInputTextFlags>,
    pub size: Option<ImVec2>,
}

pub trait Text {
    fn build(ui: &Ui, elem: &mut Self, params: TextParams) -> bool;
}

impl Text for ImString {
    fn build(ui: &Ui, elem: &mut Self, params: TextParams) -> bool {
        if let Some(size) = params.size {
            let mut input = InputTextMultiline::new(ui, params.label, elem, size);
            if let Some(flags) = params.flags {
                input = input.flags(flags);
            }
            input.build()
        } else {
            let mut input = InputText::new(ui, params.label, elem);
            if let Some(flags) = params.flags {
                input = input.flags(flags);
            }
            input.build()
        }
    }
}

impl Text for Option<ImString> {
    fn build(ui: &Ui, elem: &mut Self, params: TextParams) -> bool {
        if let Some(ref mut elem) = elem {
            Text::build(ui, elem, params)
        } else {
            false
        }
    }
}