Introduce Tooltip::gap to control spacing

This commit is contained in:
Héctor Ramón Jiménez 2021-02-23 03:16:37 +01:00
parent 9f60a256fc
commit 2f766b7341
3 changed files with 21 additions and 6 deletions

View File

@ -113,5 +113,6 @@ fn tooltip<'a>(
Text::new("Tooltip"),
position,
)
.gap(10)
.into()
}

View File

@ -30,6 +30,7 @@ where
content: &Element<'_, Message, Self>,
tooltip: &Text<Self>,
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,

View File

@ -15,6 +15,7 @@ pub struct Tooltip<'a, Message, Renderer: self::Renderer + text::Renderer> {
content: Element<'a, Message, Renderer>,
tooltip: Text<Renderer>,
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<Self>,
position: Position,
gap: u16,
) -> Self::Output;
}