dear_imgui_rs/widget/misc/disabled.rs
1use crate::Ui;
2use crate::sys;
3
4// ============================================================================
5// Disabled scope (RAII)
6// ============================================================================
7
8/// Tracks a disabled scope begun with [`Ui::begin_disabled`] and ended on drop.
9#[must_use]
10#[doc(alias = "EndDisabled")]
11pub struct DisabledToken<'ui> {
12 _ui: &'ui Ui,
13}
14
15impl<'ui> DisabledToken<'ui> {
16 fn new(ui: &'ui Ui) -> Self {
17 DisabledToken { _ui: ui }
18 }
19
20 /// Ends the disabled scope explicitly.
21 pub fn end(self) {
22 // Drop will call EndDisabled
23 }
24}
25
26impl<'ui> Drop for DisabledToken<'ui> {
27 fn drop(&mut self) {
28 self._ui
29 .run_with_bound_context(|| unsafe { sys::igEndDisabled() });
30 }
31}
32
33impl Ui {
34 /// Begin a disabled scope for subsequent items.
35 ///
36 /// All following widgets will be disabled (grayed out and non-interactive)
37 /// until the returned token is dropped.
38 #[doc(alias = "BeginDisabled")]
39 pub fn begin_disabled(&self) -> DisabledToken<'_> {
40 self.run_with_bound_context(|| unsafe { sys::igBeginDisabled(true) });
41 DisabledToken::new(self)
42 }
43
44 /// Begin a conditionally disabled scope for subsequent items.
45 ///
46 /// If `disabled` is false, this still needs to be paired with the returned
47 /// token being dropped to correctly balance the internal stack.
48 #[doc(alias = "BeginDisabled")]
49 pub fn begin_disabled_with_cond(&self, disabled: bool) -> DisabledToken<'_> {
50 self.run_with_bound_context(|| unsafe { sys::igBeginDisabled(disabled) });
51 DisabledToken::new(self)
52 }
53}