Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Tooltip inside Scrollable #1405

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions native/src/widget/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ where
position: Position,
gap: u16,
padding: u16,
snap_within_viewport: bool,
style: <Renderer::Theme as container::StyleSheet>::Style,
}

Expand All @@ -50,6 +51,7 @@ where
position,
gap: 0,
padding: Self::DEFAULT_PADDING,
snap_within_viewport: true,
style: Default::default(),
}
}
Expand Down Expand Up @@ -80,6 +82,12 @@ where
self
}

/// Sets whether the [`Tooltip`] is snapped within the viewport.
pub fn snap_within_viewport(mut self, snap: bool) -> Self {
self.snap_within_viewport = snap;
self
}

/// Sets the style of the [`Tooltip`].
pub fn style(
mut self,
Expand Down Expand Up @@ -190,6 +198,7 @@ where
self.position,
self.gap,
self.padding,
self.snap_within_viewport,
self.style,
|renderer, limits| {
Widget::<(), Renderer>::layout(tooltip, renderer, limits)
Expand Down Expand Up @@ -263,6 +272,7 @@ pub fn draw<Renderer>(
position: Position,
gap: u16,
padding: u16,
snap_within_viewport: bool,
style: <Renderer::Theme as container::StyleSheet>::Style,
layout_text: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
draw_text: impl FnOnce(
Expand Down Expand Up @@ -331,25 +341,27 @@ pub fn draw<Renderer>(
}
};

if tooltip_bounds.x < viewport.x {
tooltip_bounds.x = viewport.x;
} else if viewport.x + viewport.width
< tooltip_bounds.x + tooltip_bounds.width
{
tooltip_bounds.x =
viewport.x + viewport.width - tooltip_bounds.width;
}
if snap_within_viewport {
if tooltip_bounds.x < viewport.x {
tooltip_bounds.x = viewport.x;
} else if viewport.x + viewport.width
< tooltip_bounds.x + tooltip_bounds.width
{
tooltip_bounds.x =
viewport.x + viewport.width - tooltip_bounds.width;
}

if tooltip_bounds.y < viewport.y {
tooltip_bounds.y = viewport.y;
} else if viewport.y + viewport.height
< tooltip_bounds.y + tooltip_bounds.height
{
tooltip_bounds.y =
viewport.y + viewport.height - tooltip_bounds.height;
if tooltip_bounds.y < viewport.y {
tooltip_bounds.y = viewport.y;
} else if viewport.y + viewport.height
< tooltip_bounds.y + tooltip_bounds.height
{
tooltip_bounds.y =
viewport.y + viewport.height - tooltip_bounds.height;
}
}

renderer.with_layer(*viewport, |renderer| {
renderer.with_layer(Rectangle::with_size(Size::INFINITY), |renderer| {
container::draw_background(renderer, &style, tooltip_bounds);

draw_text(
Expand Down