diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index b7cd6ab..704c6ef 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -2,6 +2,9 @@ ## [Unreleased] +- BREAKING: Modifies `build` style methods to allow the provide closure to return a value. The build call will then return Some(value) if the closure is called, and None if it isn't. + - The most likely breaking changes users will see is that they will need to add semicolons after calling `build`, because these function no longer return `()`. + - BREAKING: Created `with_x` variants for most functions which previously took multiple parameters where some had default arguments in the C++. This makes calling most functions simpler and more similar to the C++. - The most likely breaking changes users will see is `button` and `same_line` now take one fewer parameter -- if you were calling `button` with `[0.0, 0.0]`, simply delete that -- otherwise, call `button_with_size`. Similarly, for `same_line`, if you were passing in `0.0.` simply delete that parameter. Otherwise, call `same_line_with_pos`. diff --git a/imgui-examples/examples/test_window_impl.rs b/imgui-examples/examples/test_window_impl.rs index cef5823..e24f673 100644 --- a/imgui-examples/examples/test_window_impl.rs +++ b/imgui-examples/examples/test_window_impl.rs @@ -828,7 +828,7 @@ CTRL+click on individual component to input value.\n", }); }); } - }) + }); } fn show_example_app_main_menu_bar<'a>(ui: &Ui<'a>, state: &mut State) { @@ -942,7 +942,7 @@ output your content because that would create a feedback loop.", for i in 0..state.lines { ui.text(format!("{:2$}This is line {}", "", i, i as usize * 4)); } - }) + }); } fn show_example_app_fixed_overlay(ui: &Ui, opened: &mut bool) { diff --git a/imgui/src/popups.rs b/imgui/src/popups.rs index 401e2a5..701cc3a 100644 --- a/imgui/src/popups.rs +++ b/imgui/src/popups.rs @@ -125,11 +125,10 @@ impl<'p> PopupModal<'p> { } /// Consume and draw the PopupModal. + /// Returns the result of the closure, if it is called. #[doc(alias = "BeginPopupModal")] - pub fn build(self, ui: &Ui<'_>, f: F) { - if let Some(_popup) = self.begin_popup(ui) { - f(); - } + pub fn build T>(self, ui: &Ui<'_>, f: F) -> Option { + self.begin_popup(ui).map(|_popup| f()) } /// Consume and draw the PopupModal. diff --git a/imgui/src/widget/combo_box.rs b/imgui/src/widget/combo_box.rs index 6bc6616..cc70035 100644 --- a/imgui/src/widget/combo_box.rs +++ b/imgui/src/widget/combo_box.rs @@ -155,12 +155,11 @@ impl<'a> ComboBox<'a> { } } /// Creates a combo box and runs a closure to construct the popup contents. + /// Returns the result of the closure, if it is called. /// /// Note: the closure is not called if the combo box is not open. - pub fn build(self, ui: &Ui, f: F) { - if let Some(_combo) = self.begin(ui) { - f(); - } + pub fn build T>(self, ui: &Ui, f: F) -> Option { + self.begin(ui).map(|_combo| f()) } } diff --git a/imgui/src/widget/list_box.rs b/imgui/src/widget/list_box.rs index a95fc19..60482f9 100644 --- a/imgui/src/widget/list_box.rs +++ b/imgui/src/widget/list_box.rs @@ -49,12 +49,11 @@ impl<'a> ListBox<'a> { } } /// Creates a list box and runs a closure to construct the list contents. + /// Returns the result of the closure, if it is called. /// /// Note: the closure is not called if the list box is not open. - pub fn build(self, ui: &Ui, f: F) { - if let Some(_list) = self.begin(ui) { - f(); - } + pub fn build T>(self, ui: &Ui<'_>, f: F) -> Option { + self.begin(ui).map(|_list| f()) } } diff --git a/imgui/src/widget/tab.rs b/imgui/src/widget/tab.rs index 55e8c2f..20f6ed2 100644 --- a/imgui/src/widget/tab.rs +++ b/imgui/src/widget/tab.rs @@ -103,12 +103,11 @@ impl<'a> TabBar<'a> { } /// Creates a tab bar and runs a closure to construct the contents. + /// Returns the result of the closure, if it is called. /// /// Note: the closure is not called if no tabbar content is visible - pub fn build(self, ui: &Ui, f: F) { - if let Some(_tab) = self.begin(ui) { - f(); - } + pub fn build T>(self, ui: &Ui<'_>, f: F) -> Option { + self.begin(ui).map(|_tab| f()) } } @@ -175,12 +174,11 @@ impl<'a> TabItem<'a> { } /// Creates a tab item and runs a closure to construct the contents. + /// Returns the result of the closure, if it is called. /// /// Note: the closure is not called if the tab item is not selected - pub fn build(self, ui: &Ui, f: F) { - if let Some(_tab) = self.begin(ui) { - f(); - } + pub fn build T>(self, ui: &Ui<'_>, f: F) -> Option { + self.begin(ui).map(|_tab| f()) } } diff --git a/imgui/src/widget/tree.rs b/imgui/src/widget/tree.rs index c3a754c..4b93483 100644 --- a/imgui/src/widget/tree.rs +++ b/imgui/src/widget/tree.rs @@ -264,12 +264,11 @@ impl<'a> TreeNode<'a> { } } /// Creates a tree node and runs a closure to construct the contents. + /// Returns the result of the closure, if it is called. /// /// Note: the closure is not called if the tree node is not open. - pub fn build(self, ui: &Ui, f: F) { - if let Some(_node) = self.push(ui) { - f(); - } + pub fn build T>(self, ui: &Ui<'_>, f: F) -> Option { + self.push(ui).map(|_node| f()) } } diff --git a/imgui/src/window/child_window.rs b/imgui/src/window/child_window.rs index d3deb6c..df35ecc 100644 --- a/imgui/src/window/child_window.rs +++ b/imgui/src/window/child_window.rs @@ -277,13 +277,12 @@ impl<'a> ChildWindow<'a> { } } /// Creates a child window and runs a closure to construct the contents. + /// Returns the result of the closure, if it is called. /// /// Note: the closure is not called if no window content is visible (e.g. window is collapsed /// or fully clipped). - pub fn build(self, ui: &Ui, f: F) { - if let Some(_window) = self.begin(ui) { - f(); - } + pub fn build T>(self, ui: &Ui<'_>, f: F) -> Option { + self.begin(ui).map(|_window| f()) } } diff --git a/imgui/src/window/mod.rs b/imgui/src/window/mod.rs index 7b65f30..f5caafb 100644 --- a/imgui/src/window/mod.rs +++ b/imgui/src/window/mod.rs @@ -541,13 +541,12 @@ impl<'a> Window<'a> { } } /// Creates a window and runs a closure to construct the contents. + /// Returns the result of the closure, if it is called. /// /// Note: the closure is not called if no window content is visible (e.g. window is collapsed /// or fully clipped). - pub fn build(self, ui: &Ui, f: F) { - if let Some(_window) = self.begin(ui) { - f(); - } + pub fn build T>(self, ui: &Ui<'_>, f: F) -> Option { + self.begin(ui).map(|_window| f()) } }