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 = unsafe { sys::igBeginTabBar(id_ptr, options.raw()) };
30
31        if should_render {
32            Some(TabBarToken::new(self))
33        } else {
34            None
35        }
36    }
37
38    /// Creates a new tab item and returns a token if its contents are visible.
39    ///
40    /// By default, this doesn't pass an opened bool nor any flags. See [tab_item_with_opened]
41    /// and [tab_item_with_flags] for more.
42    ///
43    /// [tab_item_with_opened]: Self::tab_item_with_opened
44    /// [tab_item_with_flags]: Self::tab_item_with_flags
45    #[doc(alias = "BeginTabItem")]
46    pub fn tab_item(&self, label: impl AsRef<str>) -> Option<TabItemToken<'_>> {
47        self.tab_item_with_flags(label, None, TabItemOptions::new())
48    }
49
50    /// Creates a new tab item and returns a token if its contents are visible.
51    ///
52    /// By default, this doesn't pass any flags. See [tab_item_with_flags] for more.
53    #[doc(alias = "BeginTabItem")]
54    pub fn tab_item_with_opened(
55        &self,
56        label: impl AsRef<str>,
57        opened: &mut bool,
58    ) -> Option<TabItemToken<'_>> {
59        self.tab_item_with_flags(label, Some(opened), TabItemOptions::new())
60    }
61
62    /// Creates a new tab item and returns a token if its contents are visible.
63    #[doc(alias = "BeginTabItem")]
64    pub fn tab_item_with_flags(
65        &self,
66        label: impl AsRef<str>,
67        opened: Option<&mut bool>,
68        flags: impl Into<TabItemOptions>,
69    ) -> Option<TabItemToken<'_>> {
70        let options = flags.into();
71        options.validate_for_tab_item("Ui::tab_item_with_flags()");
72        let label_ptr = self.scratch_txt(label);
73        let opened_ptr = opened.map(|x| x as *mut bool).unwrap_or(ptr::null_mut());
74
75        let should_render = unsafe { sys::igBeginTabItem(label_ptr, opened_ptr, options.raw()) };
76
77        if should_render {
78            Some(TabItemToken::new(self))
79        } else {
80            None
81        }
82    }
83
84    /// Creates a button on the current tab bar (e.g. to append a `+` new-tab button).
85    #[doc(alias = "TabItemButton")]
86    pub fn tab_item_button(&self, label: impl AsRef<str>) -> bool {
87        self.tab_item_button_with_flags(label, TabItemOptions::new())
88    }
89
90    /// Creates a button on the current tab bar with explicit flags.
91    #[doc(alias = "TabItemButton")]
92    pub fn tab_item_button_with_flags(
93        &self,
94        label: impl AsRef<str>,
95        flags: impl Into<TabItemOptions>,
96    ) -> bool {
97        let options = flags.into();
98        options.validate_for_tab_button("Ui::tab_item_button_with_flags()");
99        unsafe { sys::igTabItemButton(self.scratch_txt(label), options.raw()) }
100    }
101
102    /// Notifies Dear ImGui that a tab (or docked window) has been closed.
103    #[doc(alias = "SetTabItemClosed")]
104    pub fn set_tab_item_closed(&self, tab_or_docked_window_label: impl AsRef<str>) {
105        unsafe { sys::igSetTabItemClosed(self.scratch_txt(tab_or_docked_window_label)) }
106    }
107}