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        unsafe { sys::igEndDisabled() }
28    }
29}
30
31impl Ui {
32    /// Begin a disabled scope for subsequent items.
33    ///
34    /// All following widgets will be disabled (grayed out and non-interactive)
35    /// until the returned token is dropped.
36    #[doc(alias = "BeginDisabled")]
37    pub fn begin_disabled(&self) -> DisabledToken<'_> {
38        unsafe { sys::igBeginDisabled(true) }
39        DisabledToken::new(self)
40    }
41
42    /// Begin a conditionally disabled scope for subsequent items.
43    ///
44    /// If `disabled` is false, this still needs to be paired with the returned
45    /// token being dropped to correctly balance the internal stack.
46    #[doc(alias = "BeginDisabled")]
47    pub fn begin_disabled_with_cond(&self, disabled: bool) -> DisabledToken<'_> {
48        unsafe { sys::igBeginDisabled(disabled) }
49        DisabledToken::new(self)
50    }
51}