diff --git a/imgui/src/lib.rs b/imgui/src/lib.rs index dc126d5..34a5ede 100644 --- a/imgui/src/lib.rs +++ b/imgui/src/lib.rs @@ -340,7 +340,7 @@ impl Ui { /// This, like many objects in the library, uses the builder /// pattern to set optional arguments (like window size, flags, /// etc). Once all desired options are set, you must call either - /// [`Window::build`] or [`Window::begin`] must be called to + /// [`Window::build`] or [`Window::begin`] to /// actually create the window. /// /// # Examples @@ -355,28 +355,44 @@ impl Ui { /// ui.text("An example"); /// }); /// ``` + /// + /// Same as [`Ui::window`] but using the "token based" `.begin()` approach. + /// + /// ```no_run + /// # let mut ctx = imgui::Context::create(); + /// # let ui = ctx.frame(); + /// if let Some(wt) = ui + /// .window("Example Window") + /// .size([100.0, 50.0], imgui::Condition::FirstUseEver) + /// .begin() + /// { + /// ui.text("Window is visible"); + /// // Window ends where where wt is dropped, + /// // or you could call + /// // if you want to let it drop on its own, name it `_wt`. + /// // never name it `_`, as this will drop it *immediately*. + /// wt.unwrap().end(); + /// } + /// ``` pub fn window>(&self, name: Label) -> Window<'_, '_, Label> { Window::new(self, name) } - /// Same as [`Ui::window`] but using the "token based" `.begin()` approach. + /// Begins constructing a child window with the given name. /// - /// ``` - /// fn example(ui: &imgui::Ui) { - /// let wt = ui.window("Example Window") - /// .size([100.0, 50.0], imgui::Condition::FirstUseEver) - /// .begin(); - /// if wt.is_some() { - /// ui.text("Window is visible"); - /// } - /// // Window ends where where wt is dropped, - /// // or you could call - /// wt.unwrap().end() - /// } - /// ``` - pub fn child_window>(&self, name: Label) -> ChildWindow<'_, Label> { + /// Use child windows to begin into a self-contained independent scrolling/clipping + /// regions within a host window. Child windows can embed their own child. + pub fn child_window>(&self, name: Label) -> ChildWindow<'_> { ChildWindow::new(self, name) } + + /// Begins constructing a child window with the given name. + /// + /// Use child windows to begin into a self-contained independent scrolling/clipping + /// regions within a host window. Child windows can embed their own child. + pub fn child_window_id(&self, id: Id<'_>) -> ChildWindow<'_> { + ChildWindow::new_id(self, id) + } } // Widgets: Input diff --git a/imgui/src/window/child_window.rs b/imgui/src/window/child_window.rs index 5694305..2f5b378 100644 --- a/imgui/src/window/child_window.rs +++ b/imgui/src/window/child_window.rs @@ -1,16 +1,16 @@ use std::f32; use crate::math::MintVec2; -use crate::sys; use crate::window::WindowFlags; use crate::Ui; +use crate::{sys, Id}; /// Builder for a child window #[derive(Copy, Clone, Debug)] #[must_use] -pub struct ChildWindow<'ui, Label> { +pub struct ChildWindow<'ui> { ui: &'ui Ui, - name: Label, + id: u32, flags: WindowFlags, size: [f32; 2], content_size: [f32; 2], @@ -19,13 +19,20 @@ pub struct ChildWindow<'ui, Label> { border: bool, } -impl<'ui, Label: AsRef> ChildWindow<'ui, Label> { - /// Creates a new child window builder with the given ID +impl<'ui> ChildWindow<'ui> { + /// Creates a new child window builder with the str. #[doc(alias = "BeginChildID")] - pub fn new(ui: &'ui Ui, name: Label) -> ChildWindow<'ui, Label> { - ChildWindow { + pub fn new(ui: &'ui Ui, name: impl AsRef) -> Self { + let id = Id::Str(name.as_ref()); + Self::new_id(ui, id) + } + + /// Creates a new child window builder with the given imgui id. + #[doc(alias = "BeginChildID")] + pub fn new_id(ui: &'ui Ui, id: Id<'_>) -> Self { + Self { ui, - name, + id: id.as_imgui_id(), flags: WindowFlags::empty(), size: [0.0, 0.0], content_size: [0.0, 0.0], @@ -34,6 +41,7 @@ impl<'ui, Label: AsRef> ChildWindow<'ui, Label> { border: false, } } + /// Replace current window flags with the given value #[inline] pub fn flags(mut self, flags: WindowFlags) -> Self { @@ -258,8 +266,8 @@ impl<'ui, Label: AsRef> ChildWindow<'ui, Label> { unsafe { sys::igSetNextWindowBgAlpha(self.bg_alpha) }; } let should_render = unsafe { - sys::igBeginChild_Str( - self.ui.scratch_txt(self.name), + sys::igBeginChild_ID( + self.id, self.size.into(), self.border, self.flags.bits() as i32,