dear_imgui_rs/stacks/
layout.rs1use crate::{Ui, sys};
2
3impl Ui {
5 #[doc(alias = "PushItemWidth")]
15 pub fn push_item_width(&self, item_width: f32) -> ItemWidthStackToken<'_> {
16 self.run_with_bound_context(|| unsafe { sys::igPushItemWidth(item_width) });
17 ItemWidthStackToken::new(self)
18 }
19
20 #[doc(alias = "PushItemWidth")]
27 pub fn push_item_width_text(&self, text: impl AsRef<str>) -> ItemWidthStackToken<'_> {
28 let text = text.as_ref();
29 self.run_with_bound_context(|| unsafe {
30 let text_width = self.calc_text_size_bound(text, false, -1.0)[0];
31 sys::igPushItemWidth(text_width);
32 });
33 ItemWidthStackToken::new(self)
34 }
35
36 #[doc(alias = "PushTextWrapPos")]
45 pub fn push_text_wrap_pos(&self, wrap_pos_x: f32) -> TextWrapPosStackToken<'_> {
46 self.run_with_bound_context(|| unsafe { sys::igPushTextWrapPos(wrap_pos_x) });
47 TextWrapPosStackToken::new(self)
48 }
49}
50
51create_token!(
52 #[doc(alias = "PopItemWidth")]
55 pub struct ItemWidthStackToken<'ui>;
56
57 pop crate::scope::NativeScopePop::PopItemWidth;
58
59 #[doc(alias = "PopItemWidth")]
61 drop { unsafe { sys::igPopItemWidth() } }
62);
63
64create_token!(
65 #[doc(alias = "PopTextWrapPos")]
68 pub struct TextWrapPosStackToken<'ui>;
69
70 pop crate::scope::NativeScopePop::PopTextWrap;
71
72 #[doc(alias = "PopTextWrapPos")]
74 drop { unsafe { sys::igPopTextWrapPos() } }
75);
76
77#[cfg(test)]
78mod tests {
79 fn setup_context() -> crate::Context {
80 let mut ctx = crate::Context::create();
81 ctx.io_mut().set_display_size([128.0, 128.0]);
82 ctx.io_mut().set_delta_time(1.0 / 60.0);
83 ctx.font_atlas()
84 .try_claim_legacy_renderer()
85 .expect("legacy renderer font atlas should be available")
86 .build();
87 ctx
88 }
89
90 #[test]
91 fn text_item_width_scope_matches_measurement_and_restores_previous_width() {
92 let mut ctx = setup_context();
93 let ui = ctx.frame();
94
95 ui.window("text_item_width").build(|| {
96 let original_width = ui.calc_item_width();
97 let text_width = ui.calc_text_size("measured width")[0];
98
99 {
100 let _width = ui.push_item_width_text("measured width");
101 assert_eq!(ui.calc_item_width(), text_width);
102 }
103
104 assert_eq!(ui.calc_item_width(), original_width);
105 });
106 }
107}