dear_imgui_rs/widget/misc/button_repeat.rs
1use crate::{Ui, 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`].
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 #[doc(alias = "PushButtonRepeat")]
28 pub fn push_button_repeat(&self, repeat: bool) -> ButtonRepeatToken<'_> {
29 self.run_with_bound_context(|| unsafe {
30 sys::igPushItemFlag(sys::ImGuiItemFlags_ButtonRepeat as i32, repeat)
31 });
32 ButtonRepeatToken::new(self)
33 }
34
35 /// Push a button repeat item flag, run `f`, then pop the flag.
36 ///
37 /// The flag is popped during unwinding if `f` panics.
38 #[doc(alias = "PushButtonRepeat", alias = "PopButtonRepeat")]
39 pub fn with_button_repeat<R>(&self, repeat: bool, f: impl FnOnce() -> R) -> R {
40 let _repeat = self.push_button_repeat(repeat);
41 f()
42 }
43}