Skip to main content

dear_imgui_rs/widget/tab/
ui.rs

1use std::ptr;
2
3use crate::sys;
4use crate::ui::Ui;
5
6use super::{TabBarFlags, TabBarOptions, TabBarToken, TabItemOptions, TabItemToken};
7
8/// # Tab Widgets
9impl Ui {
10    /// Creates a tab bar and returns a tab bar token, allowing you to append
11    /// Tab items afterwards. This passes no flags. To pass flags explicitly,
12    /// use [tab_bar_with_flags](Self::tab_bar_with_flags).
13    #[doc(alias = "BeginTabBar")]
14    pub fn tab_bar(&self, id: impl AsRef<str>) -> Option<TabBarToken<'_>> {
15        self.tab_bar_with_flags(id, TabBarFlags::NONE)
16    }
17
18    /// Creates a tab bar and returns a tab bar token, allowing you to append
19    /// Tab items afterwards.
20    #[doc(alias = "BeginTabBar")]
21    pub fn tab_bar_with_flags(
22        &self,
23        id: impl AsRef<str>,
24        flags: impl Into<TabBarOptions>,
25    ) -> Option<TabBarToken<'_>> {
26        let options = flags.into();
27        options.validate("Ui::tab_bar_with_flags()");
28        let id_ptr = self.scratch_txt(id);
29        let should_render =
30            self.run_with_bound_context(|| unsafe { sys::igBeginTabBar(id_ptr, options.raw()) });
31
32        if should_render {
33            Some(TabBarToken::new(self))
34        } else {
35            None
36        }
37    }
38
39    /// Creates a new tab item and returns a token if its contents are visible.
40    ///
41    /// By default, this doesn't pass an opened bool nor any flags. See [tab_item_with_opened]
42    /// and [tab_item_with_flags] for more.
43    ///
44    /// [tab_item_with_opened]: Self::tab_item_with_opened
45    /// [tab_item_with_flags]: Self::tab_item_with_flags
46    #[doc(alias = "BeginTabItem")]
47    pub fn tab_item(&self, label: impl AsRef<str>) -> Option<TabItemToken<'_>> {
48        self.tab_item_with_flags(label, None, TabItemOptions::new())
49    }
50
51    /// Creates a new tab item and returns a token if its contents are visible.
52    ///
53    /// By default, this doesn't pass any flags. See [tab_item_with_flags] for more.
54    #[doc(alias = "BeginTabItem")]
55    pub fn tab_item_with_opened(
56        &self,
57        label: impl AsRef<str>,
58        opened: &mut bool,
59    ) -> Option<TabItemToken<'_>> {
60        self.tab_item_with_flags(label, Some(opened), TabItemOptions::new())
61    }
62
63    /// Creates a new tab item and returns a token if its contents are visible.
64    #[doc(alias = "BeginTabItem")]
65    pub fn tab_item_with_flags(
66        &self,
67        label: impl AsRef<str>,
68        opened: Option<&mut bool>,
69        flags: impl Into<TabItemOptions>,
70    ) -> Option<TabItemToken<'_>> {
71        let options = flags.into();
72        options.validate_for_tab_item("Ui::tab_item_with_flags()");
73        let label_ptr = self.scratch_txt(label);
74        let opened_ptr = opened.map(|x| x as *mut bool).unwrap_or(ptr::null_mut());
75
76        let should_render = self.run_with_bound_context(|| unsafe {
77            sys::igBeginTabItem(label_ptr, opened_ptr, options.raw())
78        });
79
80        if should_render {
81            Some(TabItemToken::new(self))
82        } else {
83            None
84        }
85    }
86
87    /// Creates a button on the current tab bar (e.g. to append a `+` new-tab button).
88    #[doc(alias = "TabItemButton")]
89    pub fn tab_item_button(&self, label: impl AsRef<str>) -> bool {
90        self.tab_item_button_with_flags(label, TabItemOptions::new())
91    }
92
93    /// Creates a button on the current tab bar with explicit flags.
94    #[doc(alias = "TabItemButton")]
95    pub fn tab_item_button_with_flags(
96        &self,
97        label: impl AsRef<str>,
98        flags: impl Into<TabItemOptions>,
99    ) -> bool {
100        let options = flags.into();
101        options.validate_for_tab_button("Ui::tab_item_button_with_flags()");
102        let label_ptr = self.scratch_txt(label);
103        self.run_with_bound_context(|| unsafe { sys::igTabItemButton(label_ptr, options.raw()) })
104    }
105
106    /// Notifies Dear ImGui that a tab (or docked window) has been closed.
107    #[doc(alias = "SetTabItemClosed")]
108    pub fn set_tab_item_closed(&self, tab_or_docked_window_label: impl AsRef<str>) {
109        let label_ptr = self.scratch_txt(tab_or_docked_window_label);
110        self.run_with_bound_context(|| unsafe { sys::igSetTabItemClosed(label_ptr) });
111    }
112}