Skip to main content

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