Skip to main content

dear_imgui_rs/widget/misc/
button_repeat.rs

1use crate::{Ui, create_token, sys};
2
3// ============================================================================
4// Button repeat (convenience over item flag)
5// ============================================================================
6
7create_token!(
8    /// Tracks a button repeat item flag pushed with [`Ui::push_button_repeat_token`].
9    pub struct ButtonRepeatToken<'ui>;
10
11    /// Pops the button repeat item flag.
12    #[doc(alias = "PopButtonRepeat")]
13    drop { unsafe { sys::igPopItemFlag() } }
14);
15
16impl ButtonRepeatToken<'_> {
17    /// Pops the button repeat item flag.
18    pub fn pop(self) {
19        self.end()
20    }
21}
22
23impl Ui {
24    /// Enable/disable repeating behavior for subsequent buttons.
25    ///
26    /// Internally uses `PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat)`.
27    ///
28    /// Prefer [`Self::push_button_repeat_token`] or [`Self::with_button_repeat`]
29    /// for scoped usage that remains balanced if a panic unwinds through the
30    /// scope. This manual API is kept for compatibility with existing
31    /// push/pop-style code.
32    #[doc(alias = "PushButtonRepeat")]
33    pub fn push_button_repeat(&self, repeat: bool) {
34        unsafe { sys::igPushItemFlag(sys::ImGuiItemFlags_ButtonRepeat as i32, repeat) }
35    }
36
37    /// Push a button repeat item flag and return an RAII token that pops it on drop.
38    #[doc(alias = "PushButtonRepeat")]
39    pub fn push_button_repeat_token(&self, repeat: bool) -> ButtonRepeatToken<'_> {
40        self.push_button_repeat(repeat);
41        ButtonRepeatToken::new(self)
42    }
43
44    /// Push a button repeat item flag, run `f`, then pop the flag.
45    ///
46    /// The flag is popped during unwinding if `f` panics.
47    #[doc(alias = "PushButtonRepeat", alias = "PopButtonRepeat")]
48    pub fn with_button_repeat<R>(&self, repeat: bool, f: impl FnOnce() -> R) -> R {
49        let _repeat = self.push_button_repeat_token(repeat);
50        f()
51    }
52
53    /// Pop the button repeat item flag.
54    #[doc(alias = "PopButtonRepeat")]
55    pub fn pop_button_repeat(&self) {
56        unsafe { sys::igPopItemFlag() }
57    }
58}