From 2f766b73413fe60cd881e139fa0e84a0f0134d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 23 Feb 2021 03:16:37 +0100 Subject: [PATCH] Introduce `Tooltip::gap` to control spacing --- examples/tooltip/src/main.rs | 1 + graphics/src/widget/tooltip.rs | 16 ++++++++++------ native/src/widget/tooltip.rs | 10 ++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index a49caa70..3677dc75 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -113,5 +113,6 @@ fn tooltip<'a>( Text::new("Tooltip"), position, ) + .gap(10) .into() } diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index 0b4d1d2f..26b18507 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -30,6 +30,7 @@ where content: &Element<'_, Message, Self>, tooltip: &Text, position: Position, + gap: u16, ) -> Self::Output { let bounds = content_layout.bounds(); @@ -58,18 +59,21 @@ where let y_center = bounds.y + (bounds.height - tooltip_bounds.height) / 2.0; + let gap = f32::from(gap); + let offset = match position { - Position::Top => { - Vector::new(x_center, bounds.y - tooltip_bounds.height) - } + Position::Top => Vector::new( + x_center, + bounds.y - tooltip_bounds.height - gap, + ), Position::Bottom => { - Vector::new(x_center, bounds.y + bounds.height) + Vector::new(x_center, bounds.y + bounds.height + gap) } Position::Left => { - Vector::new(bounds.x - tooltip_bounds.width, y_center) + Vector::new(bounds.x - tooltip_bounds.width - gap, y_center) } Position::Right => { - Vector::new(bounds.x + bounds.width, y_center) + Vector::new(bounds.x + bounds.width + gap, y_center) } Position::FollowCursor => Vector::new( cursor_position.x, diff --git a/native/src/widget/tooltip.rs b/native/src/widget/tooltip.rs index 72d03c1a..6da7b0ca 100644 --- a/native/src/widget/tooltip.rs +++ b/native/src/widget/tooltip.rs @@ -15,6 +15,7 @@ pub struct Tooltip<'a, Message, Renderer: self::Renderer + text::Renderer> { content: Element<'a, Message, Renderer>, tooltip: Text, position: Position, + gap: u16, } impl<'a, Message, Renderer> Tooltip<'a, Message, Renderer> @@ -33,8 +34,15 @@ where content: content.into(), tooltip, position, + gap: 0, } } + + /// Sets the gap between the content and its [`Tooltip`]. + pub fn gap(mut self, gap: u16) -> Self { + self.gap = gap; + self + } } /// The position of the tooltip. Defaults to following the cursor. @@ -109,6 +117,7 @@ where &self.content, &self.tooltip, self.position, + self.gap, ) } @@ -143,6 +152,7 @@ pub trait Renderer: crate::Renderer + text::Renderer { content: &Element<'_, Message, Self>, tooltip: &Text, position: Position, + gap: u16, ) -> Self::Output; }