Add snap within viewport builder
This commit is contained in:
parent
3fe2e14a42
commit
1ae3a94de2
1 changed files with 27 additions and 15 deletions
|
|
@ -25,6 +25,7 @@ where
|
||||||
position: Position,
|
position: Position,
|
||||||
gap: u16,
|
gap: u16,
|
||||||
padding: u16,
|
padding: u16,
|
||||||
|
snap_within_viewport: bool,
|
||||||
style: <Renderer::Theme as container::StyleSheet>::Style,
|
style: <Renderer::Theme as container::StyleSheet>::Style,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,6 +51,7 @@ where
|
||||||
position,
|
position,
|
||||||
gap: 0,
|
gap: 0,
|
||||||
padding: Self::DEFAULT_PADDING,
|
padding: Self::DEFAULT_PADDING,
|
||||||
|
snap_within_viewport: true,
|
||||||
style: Default::default(),
|
style: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -80,6 +82,12 @@ where
|
||||||
self
|
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`].
|
/// Sets the style of the [`Tooltip`].
|
||||||
pub fn style(
|
pub fn style(
|
||||||
mut self,
|
mut self,
|
||||||
|
|
@ -190,6 +198,7 @@ where
|
||||||
self.position,
|
self.position,
|
||||||
self.gap,
|
self.gap,
|
||||||
self.padding,
|
self.padding,
|
||||||
|
self.snap_within_viewport,
|
||||||
self.style,
|
self.style,
|
||||||
|renderer, limits| {
|
|renderer, limits| {
|
||||||
Widget::<(), Renderer>::layout(tooltip, renderer, limits)
|
Widget::<(), Renderer>::layout(tooltip, renderer, limits)
|
||||||
|
|
@ -263,6 +272,7 @@ pub fn draw<Renderer>(
|
||||||
position: Position,
|
position: Position,
|
||||||
gap: u16,
|
gap: u16,
|
||||||
padding: u16,
|
padding: u16,
|
||||||
|
snap_within_viewport: bool,
|
||||||
style: <Renderer::Theme as container::StyleSheet>::Style,
|
style: <Renderer::Theme as container::StyleSheet>::Style,
|
||||||
layout_text: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
|
layout_text: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
|
||||||
draw_text: impl FnOnce(
|
draw_text: impl FnOnce(
|
||||||
|
|
@ -331,22 +341,24 @@ pub fn draw<Renderer>(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if tooltip_bounds.x < viewport.x {
|
if snap_within_viewport {
|
||||||
tooltip_bounds.x = viewport.x;
|
if tooltip_bounds.x < viewport.x {
|
||||||
} else if viewport.x + viewport.width
|
tooltip_bounds.x = viewport.x;
|
||||||
< tooltip_bounds.x + tooltip_bounds.width
|
} else if viewport.x + viewport.width
|
||||||
{
|
< tooltip_bounds.x + tooltip_bounds.width
|
||||||
tooltip_bounds.x =
|
{
|
||||||
viewport.x + viewport.width - tooltip_bounds.width;
|
tooltip_bounds.x =
|
||||||
}
|
viewport.x + viewport.width - tooltip_bounds.width;
|
||||||
|
}
|
||||||
|
|
||||||
if tooltip_bounds.y < viewport.y {
|
if tooltip_bounds.y < viewport.y {
|
||||||
tooltip_bounds.y = viewport.y;
|
tooltip_bounds.y = viewport.y;
|
||||||
} else if viewport.y + viewport.height
|
} else if viewport.y + viewport.height
|
||||||
< tooltip_bounds.y + tooltip_bounds.height
|
< tooltip_bounds.y + tooltip_bounds.height
|
||||||
{
|
{
|
||||||
tooltip_bounds.y =
|
tooltip_bounds.y =
|
||||||
viewport.y + viewport.height - tooltip_bounds.height;
|
viewport.y + viewport.height - tooltip_bounds.height;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer.with_layer(Rectangle::with_size(Size::INFINITY), |renderer| {
|
renderer.with_layer(Rectangle::with_size(Size::INFINITY), |renderer| {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue