Skip to main content

dear_imgui_rs/stacks/
layout.rs

1use crate::{Ui, sys};
2
3/// # Parameter stacks (current window)
4impl Ui {
5    /// Changes the item width by pushing a change to the item width stack.
6    ///
7    /// Returns an `ItemWidthStackToken`. The pushed width item is popped when either
8    /// `ItemWidthStackToken` goes out of scope, or `.end()` is called.
9    ///
10    /// - `> 0.0`: width is `item_width` pixels
11    /// - `= 0.0`: default to ~2/3 of window width
12    /// - `< 0.0`: `item_width` pixels relative to the right of window (-1.0 always aligns width to
13    ///   the right side)
14    #[doc(alias = "PushItemWidth")]
15    pub fn push_item_width(&self, item_width: f32) -> ItemWidthStackToken<'_> {
16        unsafe { sys::igPushItemWidth(item_width) };
17        ItemWidthStackToken::new(self)
18    }
19
20    /// Sets the width of the next item(s) to be the same as the width of the given text.
21    ///
22    /// Returns an `ItemWidthStackToken`. The pushed width item is popped when either
23    /// `ItemWidthStackToken` goes out of scope, or `.end()` is called.
24    #[doc(alias = "PushItemWidth")]
25    pub fn push_item_width_text(&self, text: impl AsRef<str>) -> ItemWidthStackToken<'_> {
26        let text_width = unsafe {
27            let text_ptr = self.scratch_txt(text);
28            let out = sys::igCalcTextSize(text_ptr, std::ptr::null(), false, -1.0);
29            out.x
30        };
31        self.push_item_width(text_width)
32    }
33
34    /// Sets the position where text will wrap around.
35    ///
36    /// Returns a `TextWrapPosStackToken`. The pushed wrap position is popped when either
37    /// `TextWrapPosStackToken` goes out of scope, or `.end()` is called.
38    ///
39    /// - `wrap_pos_x < 0.0`: no wrapping
40    /// - `wrap_pos_x = 0.0`: wrap to end of window (or column)
41    /// - `wrap_pos_x > 0.0`: wrap at `wrap_pos_x` position in window local space
42    #[doc(alias = "PushTextWrapPos")]
43    pub fn push_text_wrap_pos(&self, wrap_pos_x: f32) -> TextWrapPosStackToken<'_> {
44        unsafe { sys::igPushTextWrapPos(wrap_pos_x) };
45        TextWrapPosStackToken::new(self)
46    }
47}
48
49create_token!(
50    /// Tracks a change made with [`Ui::push_item_width`] that can be popped
51    /// by calling [`ItemWidthStackToken::end`] or dropping.
52    pub struct ItemWidthStackToken<'ui>;
53
54    /// Pops an item width change made with [`Ui::push_item_width`].
55    #[doc(alias = "PopItemWidth")]
56    drop { unsafe { sys::igPopItemWidth() } }
57);
58
59create_token!(
60    /// Tracks a change made with [`Ui::push_text_wrap_pos`] that can be popped
61    /// by calling [`TextWrapPosStackToken::end`] or dropping.
62    pub struct TextWrapPosStackToken<'ui>;
63
64    /// Pops a text wrap position change made with [`Ui::push_text_wrap_pos`].
65    #[doc(alias = "PopTextWrapPos")]
66    drop { unsafe { sys::igPopTextWrapPos() } }
67);