feat(native): add Tooltip widget
This commit is contained in:
parent
4de164dcc7
commit
a19f89d3a6
23 changed files with 580 additions and 18 deletions
|
|
@ -198,11 +198,19 @@ where
|
|||
fn overlay(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
overlay_content_bounds: Option<Rectangle>,
|
||||
cursor_position: Point,
|
||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||
self.children
|
||||
.iter_mut()
|
||||
.zip(layout.children())
|
||||
.filter_map(|(child, layout)| child.widget.overlay(layout))
|
||||
.filter_map(|(child, layout)| {
|
||||
child.widget.overlay(
|
||||
layout,
|
||||
overlay_content_bounds,
|
||||
cursor_position,
|
||||
)
|
||||
})
|
||||
.next()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -200,8 +200,14 @@ where
|
|||
fn overlay(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
overlay_content_bounds: Option<Rectangle>,
|
||||
cursor_position: Point,
|
||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||
self.content.overlay(layout.children().next().unwrap())
|
||||
self.content.overlay(
|
||||
layout.children().next().unwrap(),
|
||||
overlay_content_bounds,
|
||||
cursor_position,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -558,11 +558,15 @@ where
|
|||
fn overlay(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
overlay_content_bounds: Option<Rectangle>,
|
||||
cursor_position: Point,
|
||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||
self.elements
|
||||
.iter_mut()
|
||||
.zip(layout.children())
|
||||
.filter_map(|((_, pane), layout)| pane.overlay(layout))
|
||||
.filter_map(|((_, pane), layout)| {
|
||||
pane.overlay(layout, overlay_content_bounds, cursor_position)
|
||||
})
|
||||
.next()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use iced_core::Rectangle;
|
||||
|
||||
use crate::container;
|
||||
use crate::event::{self, Event};
|
||||
use crate::layout;
|
||||
|
|
@ -189,6 +191,8 @@ where
|
|||
pub(crate) fn overlay(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
overlay_content_bounds: Option<Rectangle>,
|
||||
cursor_position: Point,
|
||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||
let body_layout = if self.title_bar.is_some() {
|
||||
let mut children = layout.children();
|
||||
|
|
@ -201,7 +205,8 @@ where
|
|||
layout
|
||||
};
|
||||
|
||||
self.body.overlay(body_layout)
|
||||
self.body
|
||||
.overlay(body_layout, overlay_content_bounds, cursor_position)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -274,6 +274,8 @@ where
|
|||
fn overlay(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
_overlay_content_bounds: Option<Rectangle>,
|
||||
_cursor_position: Point,
|
||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||
if *self.is_open {
|
||||
let bounds = layout.bounds();
|
||||
|
|
|
|||
|
|
@ -197,11 +197,19 @@ where
|
|||
fn overlay(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
overlay_content_bounds: Option<Rectangle>,
|
||||
cursor_position: Point,
|
||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||
self.children
|
||||
.iter_mut()
|
||||
.zip(layout.children())
|
||||
.filter_map(|(child, layout)| child.widget.overlay(layout))
|
||||
.filter_map(|(child, layout)| {
|
||||
child.widget.overlay(
|
||||
layout,
|
||||
overlay_content_bounds,
|
||||
cursor_position,
|
||||
)
|
||||
})
|
||||
.next()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -401,11 +401,17 @@ where
|
|||
fn overlay(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
overlay_content_bounds: Option<Rectangle>,
|
||||
cursor_position: Point,
|
||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||
let Self { content, state, .. } = self;
|
||||
|
||||
content
|
||||
.overlay(layout.children().next().unwrap())
|
||||
.overlay(
|
||||
layout.children().next().unwrap(),
|
||||
overlay_content_bounds,
|
||||
cursor_position,
|
||||
)
|
||||
.map(|overlay| {
|
||||
let bounds = layout.bounds();
|
||||
let content_layout = layout.children().next().unwrap();
|
||||
|
|
|
|||
300
native/src/widget/tooltip.rs
Normal file
300
native/src/widget/tooltip.rs
Normal file
|
|
@ -0,0 +1,300 @@
|
|||
//! Display a widget over another.
|
||||
use std::hash::Hash;
|
||||
|
||||
use iced_core::Rectangle;
|
||||
|
||||
use crate::{
|
||||
event, layout, overlay, Clipboard, Element, Event, Hasher, Layout, Length,
|
||||
Point, Size, Vector, Widget,
|
||||
};
|
||||
|
||||
/// An element to display a widget over another.
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct Tooltip<'a, Message, Renderer: self::Renderer> {
|
||||
content: Element<'a, Message, Renderer>,
|
||||
tooltip: Element<'a, Message, Renderer>,
|
||||
tooltip_position: TooltipPosition,
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> Tooltip<'a, Message, Renderer>
|
||||
where
|
||||
Renderer: self::Renderer,
|
||||
{
|
||||
/// Creates an empty [`Tooltip`].
|
||||
///
|
||||
/// [`Tooltip`]: struct.Tooltip.html
|
||||
pub fn new<T, H>(
|
||||
content: T,
|
||||
tooltip: H,
|
||||
tooltip_position: TooltipPosition,
|
||||
) -> Self
|
||||
where
|
||||
T: Into<Element<'a, Message, Renderer>>,
|
||||
H: Into<Element<'a, Message, Renderer>>,
|
||||
{
|
||||
Tooltip {
|
||||
content: content.into(),
|
||||
tooltip: tooltip.into(),
|
||||
tooltip_position,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The position of the tooltip. Defaults to following the cursor.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum TooltipPosition {
|
||||
/// The tooltip will follow the cursor.
|
||||
FollowCursor,
|
||||
/// The tooltip will appear on the top of the widget.
|
||||
Top,
|
||||
/// The tooltip will appear on the bottom of the widget.
|
||||
Bottom,
|
||||
/// The tooltip will appear on the left of the widget.
|
||||
Left,
|
||||
/// The tooltip will appear on the right of the widget.
|
||||
Right,
|
||||
}
|
||||
|
||||
impl Default for TooltipPosition {
|
||||
fn default() -> Self {
|
||||
TooltipPosition::FollowCursor
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
||||
for Tooltip<'a, Message, Renderer>
|
||||
where
|
||||
Renderer: self::Renderer,
|
||||
{
|
||||
fn width(&self) -> Length {
|
||||
self.content.width()
|
||||
}
|
||||
|
||||
fn height(&self) -> Length {
|
||||
self.content.height()
|
||||
}
|
||||
|
||||
fn layout(
|
||||
&self,
|
||||
renderer: &Renderer,
|
||||
limits: &layout::Limits,
|
||||
) -> layout::Node {
|
||||
self.content.layout(renderer, limits)
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
&mut self,
|
||||
event: Event,
|
||||
layout: Layout<'_>,
|
||||
cursor_position: Point,
|
||||
messages: &mut Vec<Message>,
|
||||
renderer: &Renderer,
|
||||
clipboard: Option<&dyn Clipboard>,
|
||||
) -> event::Status {
|
||||
self.content.widget.on_event(
|
||||
event,
|
||||
layout,
|
||||
cursor_position,
|
||||
messages,
|
||||
renderer,
|
||||
clipboard,
|
||||
)
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
renderer: &mut Renderer,
|
||||
defaults: &Renderer::Defaults,
|
||||
layout: Layout<'_>,
|
||||
cursor_position: Point,
|
||||
viewport: &Rectangle,
|
||||
) -> Renderer::Output {
|
||||
renderer.draw(
|
||||
defaults,
|
||||
cursor_position,
|
||||
&self.content,
|
||||
layout,
|
||||
viewport,
|
||||
)
|
||||
}
|
||||
|
||||
fn hash_layout(&self, state: &mut Hasher) {
|
||||
struct Marker;
|
||||
std::any::TypeId::of::<Marker>().hash(state);
|
||||
|
||||
self.content.hash_layout(state);
|
||||
}
|
||||
|
||||
fn overlay(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
overlay_content_bounds: Option<Rectangle>,
|
||||
cursor_position: Point,
|
||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||
let bounds = layout.bounds();
|
||||
|
||||
if bounds.contains(cursor_position) {
|
||||
let mut position = cursor_position;
|
||||
|
||||
if let Some(content_bounds) = overlay_content_bounds {
|
||||
if TooltipPosition::FollowCursor != self.tooltip_position {
|
||||
match self.tooltip_position {
|
||||
TooltipPosition::Top | TooltipPosition::Bottom => {
|
||||
let x = bounds.x + bounds.width * 0.5
|
||||
- content_bounds.width * 0.5;
|
||||
|
||||
position = match self.tooltip_position {
|
||||
TooltipPosition::Top => Point::new(
|
||||
x,
|
||||
bounds.y - content_bounds.height,
|
||||
),
|
||||
TooltipPosition::Bottom => Point::new(
|
||||
x,
|
||||
bounds.y
|
||||
+ bounds.height
|
||||
+ content_bounds.height,
|
||||
),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
}
|
||||
TooltipPosition::Left | TooltipPosition::Right => {
|
||||
let y =
|
||||
bounds.center_y() + content_bounds.height * 0.5;
|
||||
|
||||
position = match self.tooltip_position {
|
||||
TooltipPosition::Left => Point::new(
|
||||
bounds.x - content_bounds.width,
|
||||
y,
|
||||
),
|
||||
TooltipPosition::Right => {
|
||||
Point::new(bounds.x + bounds.width, y)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(overlay::Element::new(
|
||||
position,
|
||||
Box::new(Overlay::new(&self.tooltip)),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Overlay<'a, Message, Renderer: self::Renderer> {
|
||||
content: &'a Element<'a, Message, Renderer>,
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer: self::Renderer> Overlay<'a, Message, Renderer>
|
||||
where
|
||||
Message: 'a,
|
||||
Renderer: 'a,
|
||||
{
|
||||
pub fn new(content: &'a Element<'a, Message, Renderer>) -> Self {
|
||||
Self { content }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> crate::Overlay<Message, Renderer>
|
||||
for Overlay<'a, Message, Renderer>
|
||||
where
|
||||
Renderer: self::Renderer,
|
||||
{
|
||||
fn layout(
|
||||
&self,
|
||||
renderer: &Renderer,
|
||||
bounds: Size,
|
||||
position: Point,
|
||||
) -> layout::Node {
|
||||
let space_below = bounds.height - position.y;
|
||||
let space_above = position.y;
|
||||
|
||||
let limits = layout::Limits::new(
|
||||
Size::ZERO,
|
||||
Size::new(
|
||||
bounds.width - position.x,
|
||||
if space_below > space_above {
|
||||
space_below
|
||||
} else {
|
||||
space_above
|
||||
},
|
||||
),
|
||||
)
|
||||
.width(self.content.width());
|
||||
|
||||
let mut node = self.content.layout(renderer, &limits);
|
||||
|
||||
node.move_to(position - Vector::new(0.0, node.size().height));
|
||||
|
||||
node
|
||||
}
|
||||
|
||||
fn hash_layout(&self, state: &mut Hasher, position: Point) {
|
||||
struct Marker;
|
||||
std::any::TypeId::of::<Marker>().hash(state);
|
||||
|
||||
(position.x as u32).hash(state);
|
||||
(position.y as u32).hash(state);
|
||||
self.content.hash_layout(state);
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
renderer: &mut Renderer,
|
||||
defaults: &Renderer::Defaults,
|
||||
layout: Layout<'_>,
|
||||
cursor_position: Point,
|
||||
viewport: &Rectangle,
|
||||
) -> Renderer::Output {
|
||||
renderer.draw(
|
||||
defaults,
|
||||
cursor_position,
|
||||
&self.content,
|
||||
layout,
|
||||
viewport,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The renderer of a [`Tooltip`].
|
||||
///
|
||||
/// Your [renderer] will need to implement this trait before being
|
||||
/// able to use a [`Tooltip`] in your user interface.
|
||||
///
|
||||
/// [`Tooltip`]: struct.Tooltip.html
|
||||
/// [renderer]: ../../renderer/index.html
|
||||
pub trait Renderer: crate::Renderer {
|
||||
/// The style supported by this renderer.
|
||||
type Style: Default;
|
||||
|
||||
/// Draws a [`Tooltip`].
|
||||
///
|
||||
/// [`Tooltip`]: struct.Tooltip.html
|
||||
fn draw<Message>(
|
||||
&mut self,
|
||||
defaults: &Self::Defaults,
|
||||
cursor_position: Point,
|
||||
content: &Element<'_, Message, Self>,
|
||||
content_layout: Layout<'_>,
|
||||
viewport: &Rectangle,
|
||||
) -> Self::Output;
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> From<Tooltip<'a, Message, Renderer>>
|
||||
for Element<'a, Message, Renderer>
|
||||
where
|
||||
Renderer: 'a + self::Renderer,
|
||||
Message: 'a,
|
||||
{
|
||||
fn from(
|
||||
column: Tooltip<'a, Message, Renderer>,
|
||||
) -> Element<'a, Message, Renderer> {
|
||||
Element::new(column)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue