From 4203eaacc67799da6bff6bb385aba739bf91f466 Mon Sep 17 00:00:00 2001 From: barsoosayque Date: Mon, 25 May 2020 22:18:36 +0700 Subject: [PATCH] Add ability to create tooltip function-less --- src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index bc25d74..9907cf7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -327,6 +327,28 @@ impl<'ui> Ui<'ui> { } } +/// Tracks a layout tooltip that must be ended by calling `.end()` +#[must_use] +pub struct TooltipToken { + ctx: *const Context, +} + +impl TooltipToken { + /// Ends a layout tooltip + pub fn end(mut self, _: &Ui) { + self.ctx = ptr::null(); + unsafe { sys::igEndTooltip() }; + } +} + +impl Drop for TooltipToken { + fn drop(&mut self) { + if !self.ctx.is_null() && !thread::panicking() { + panic!("A TooltipToken was leaked. Did you call .end()?"); + } + } +} + /// # Tooltips impl<'ui> Ui<'ui> { /// Construct a tooltip window that can have any kind of content. @@ -351,6 +373,13 @@ impl<'ui> Ui<'ui> { f(); unsafe { sys::igEndTooltip() }; } + /// Construct a tooltip window that can have any kind of content. + /// + /// Returns a `TooltipToken` that must be ended by calling `.end()` + pub fn begin_tooltip(&self) -> TooltipToken { + unsafe { sys::igBeginTooltip() }; + TooltipToken { ctx: self.ctx } + } /// Construct a tooltip window with simple text content. /// /// Typically used with `Ui::is_item_hovered()` or some other conditional check.