Change Tooltip
to support Text
only for now
This commit is contained in:
parent
a19f89d3a6
commit
81c75c1524
@ -1,6 +1,7 @@
|
|||||||
|
use iced::tooltip::{self, Tooltip};
|
||||||
use iced::{
|
use iced::{
|
||||||
button, tooltip::TooltipPosition, Button, Column, Container, Element,
|
button, Button, Column, Container, Element, HorizontalAlignment, Length,
|
||||||
Length, Row, Sandbox, Settings, Text, Tooltip,
|
Row, Sandbox, Settings, Text, VerticalAlignment,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
@ -9,11 +10,11 @@ pub fn main() {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Example {
|
struct Example {
|
||||||
tooltip_top_button_state: button::State,
|
top: button::State,
|
||||||
tooltip_bottom_button_state: button::State,
|
bottom: button::State,
|
||||||
tooltip_right_button_state: button::State,
|
right: button::State,
|
||||||
tooltip_left_button_state: button::State,
|
left: button::State,
|
||||||
tooltip_cursor_button_state: button::State,
|
follow_cursor: button::State,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@ -33,52 +34,39 @@ impl Sandbox for Example {
|
|||||||
fn update(&mut self, _message: Message) {}
|
fn update(&mut self, _message: Message) {}
|
||||||
|
|
||||||
fn view(&mut self) -> Element<Message> {
|
fn view(&mut self) -> Element<Message> {
|
||||||
let tooltip_top = tooltip_builder(
|
let top =
|
||||||
"Tooltip at top",
|
tooltip("Tooltip at top", &mut self.top, tooltip::Position::Top);
|
||||||
&mut self.tooltip_top_button_state,
|
|
||||||
TooltipPosition::Top,
|
let bottom = tooltip(
|
||||||
);
|
|
||||||
let tooltip_bottom = tooltip_builder(
|
|
||||||
"Tooltip at bottom",
|
"Tooltip at bottom",
|
||||||
&mut self.tooltip_bottom_button_state,
|
&mut self.bottom,
|
||||||
TooltipPosition::Bottom,
|
tooltip::Position::Bottom,
|
||||||
);
|
);
|
||||||
let tooltip_right = tooltip_builder(
|
|
||||||
|
let left =
|
||||||
|
tooltip("Tooltip at left", &mut self.left, tooltip::Position::Left);
|
||||||
|
|
||||||
|
let right = tooltip(
|
||||||
"Tooltip at right",
|
"Tooltip at right",
|
||||||
&mut self.tooltip_right_button_state,
|
&mut self.right,
|
||||||
TooltipPosition::Right,
|
tooltip::Position::Right,
|
||||||
);
|
|
||||||
let tooltip_left = tooltip_builder(
|
|
||||||
"Tooltip at left",
|
|
||||||
&mut self.tooltip_left_button_state,
|
|
||||||
TooltipPosition::Left,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let fixed_tooltips = Row::with_children(vec![
|
let fixed_tooltips = Row::with_children(vec![
|
||||||
tooltip_top.into(),
|
top.into(),
|
||||||
tooltip_bottom.into(),
|
bottom.into(),
|
||||||
tooltip_left.into(),
|
left.into(),
|
||||||
tooltip_right.into(),
|
right.into(),
|
||||||
])
|
])
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.align_items(iced::Align::Center)
|
.align_items(iced::Align::Center)
|
||||||
.spacing(120);
|
.spacing(50);
|
||||||
|
|
||||||
let cursor_tooltip_area = Tooltip::new(
|
let follow_cursor = tooltip(
|
||||||
Button::new(
|
"Tooltip follows cursor",
|
||||||
&mut self.tooltip_cursor_button_state,
|
&mut self.follow_cursor,
|
||||||
Container::new(Text::new("Tooltip follows cursor").size(40))
|
tooltip::Position::FollowCursor,
|
||||||
.center_y()
|
|
||||||
.center_x()
|
|
||||||
.width(Length::Fill)
|
|
||||||
.height(Length::Fill),
|
|
||||||
)
|
|
||||||
.on_press(Message)
|
|
||||||
.width(Length::Fill)
|
|
||||||
.height(Length::Fill),
|
|
||||||
tooltip(),
|
|
||||||
TooltipPosition::FollowCursor,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let content = Column::with_children(vec![
|
let content = Column::with_children(vec![
|
||||||
@ -88,36 +76,42 @@ impl Sandbox for Example {
|
|||||||
.center_x()
|
.center_x()
|
||||||
.center_y()
|
.center_y()
|
||||||
.into(),
|
.into(),
|
||||||
cursor_tooltip_area.into(),
|
follow_cursor.into(),
|
||||||
])
|
])
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill);
|
.height(Length::Fill)
|
||||||
|
.spacing(50);
|
||||||
|
|
||||||
Container::new(content)
|
Container::new(content)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.center_x()
|
.center_x()
|
||||||
.center_y()
|
.center_y()
|
||||||
|
.padding(50)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tooltip_builder<'a>(
|
fn tooltip<'a>(
|
||||||
label: &str,
|
label: &str,
|
||||||
button_state: &'a mut button::State,
|
button_state: &'a mut button::State,
|
||||||
position: TooltipPosition,
|
position: tooltip::Position,
|
||||||
) -> Container<'a, Message> {
|
) -> Element<'a, Message> {
|
||||||
Container::new(Tooltip::new(
|
Tooltip::new(
|
||||||
Button::new(button_state, Text::new(label).size(40)).on_press(Message),
|
Button::new(
|
||||||
tooltip(),
|
button_state,
|
||||||
|
Text::new(label)
|
||||||
|
.size(40)
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.horizontal_alignment(HorizontalAlignment::Center)
|
||||||
|
.vertical_alignment(VerticalAlignment::Center),
|
||||||
|
)
|
||||||
|
.on_press(Message)
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill),
|
||||||
|
Text::new("Tooltip"),
|
||||||
position,
|
position,
|
||||||
))
|
)
|
||||||
.center_x()
|
.into()
|
||||||
.center_y()
|
|
||||||
.width(Length::Fill)
|
|
||||||
.height(Length::Fill)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tooltip() -> Text {
|
|
||||||
Text::new("Tooltip").size(20)
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
//! Decorate content and apply alignment.
|
//! Decorate content and apply alignment.
|
||||||
|
use crate::backend::{self, Backend};
|
||||||
use crate::defaults::Defaults;
|
use crate::defaults::Defaults;
|
||||||
use crate::{Backend, Renderer};
|
use crate::{Primitive, Renderer, Vector};
|
||||||
use iced_native::{Element, Layout, Point, Rectangle};
|
|
||||||
|
use iced_native::layout::{self, Layout};
|
||||||
|
use iced_native::{Element, Point, Rectangle, Size, Text};
|
||||||
|
|
||||||
/// An element decorating some content.
|
/// An element decorating some content.
|
||||||
///
|
///
|
||||||
@ -10,9 +13,11 @@ use iced_native::{Element, Layout, Point, Rectangle};
|
|||||||
pub type Tooltip<'a, Message, Backend> =
|
pub type Tooltip<'a, Message, Backend> =
|
||||||
iced_native::Tooltip<'a, Message, Renderer<Backend>>;
|
iced_native::Tooltip<'a, Message, Renderer<Backend>>;
|
||||||
|
|
||||||
|
pub use iced_native::tooltip::Position;
|
||||||
|
|
||||||
impl<B> iced_native::tooltip::Renderer for Renderer<B>
|
impl<B> iced_native::tooltip::Renderer for Renderer<B>
|
||||||
where
|
where
|
||||||
B: Backend,
|
B: Backend + backend::Text,
|
||||||
{
|
{
|
||||||
type Style = ();
|
type Style = ();
|
||||||
|
|
||||||
@ -20,10 +25,14 @@ where
|
|||||||
&mut self,
|
&mut self,
|
||||||
defaults: &Defaults,
|
defaults: &Defaults,
|
||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
content: &Element<'_, Message, Self>,
|
|
||||||
content_layout: Layout<'_>,
|
content_layout: Layout<'_>,
|
||||||
viewport: &Rectangle,
|
viewport: &Rectangle,
|
||||||
|
content: &Element<'_, Message, Self>,
|
||||||
|
tooltip: &Text<Self>,
|
||||||
|
position: Position,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
|
let bounds = content_layout.bounds();
|
||||||
|
|
||||||
let (content, mouse_interaction) = content.draw(
|
let (content, mouse_interaction) = content.draw(
|
||||||
self,
|
self,
|
||||||
&defaults,
|
&defaults,
|
||||||
@ -32,6 +41,63 @@ where
|
|||||||
viewport,
|
viewport,
|
||||||
);
|
);
|
||||||
|
|
||||||
(content, mouse_interaction)
|
if bounds.contains(cursor_position) {
|
||||||
|
use iced_native::Widget;
|
||||||
|
|
||||||
|
let tooltip_layout = Widget::<(), Self>::layout(
|
||||||
|
tooltip,
|
||||||
|
self,
|
||||||
|
&layout::Limits::new(Size::ZERO, viewport.size()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let tooltip_bounds = tooltip_layout.bounds();
|
||||||
|
|
||||||
|
let x_center =
|
||||||
|
bounds.x + (bounds.width - tooltip_bounds.width) / 2.0;
|
||||||
|
|
||||||
|
let y_center =
|
||||||
|
bounds.y + (bounds.height - tooltip_bounds.height) / 2.0;
|
||||||
|
|
||||||
|
let offset = match position {
|
||||||
|
Position::Top => {
|
||||||
|
Vector::new(x_center, bounds.y - tooltip_bounds.height)
|
||||||
|
}
|
||||||
|
Position::Bottom => {
|
||||||
|
Vector::new(x_center, bounds.y + bounds.height)
|
||||||
|
}
|
||||||
|
Position::Left => {
|
||||||
|
Vector::new(bounds.x - tooltip_bounds.width, y_center)
|
||||||
|
}
|
||||||
|
Position::Right => {
|
||||||
|
Vector::new(bounds.x + bounds.width, y_center)
|
||||||
|
}
|
||||||
|
Position::FollowCursor => Vector::new(
|
||||||
|
cursor_position.x,
|
||||||
|
cursor_position.y - tooltip_bounds.height,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
let (tooltip, _) = Widget::<(), Self>::draw(
|
||||||
|
tooltip,
|
||||||
|
self,
|
||||||
|
defaults,
|
||||||
|
Layout::with_offset(offset, &tooltip_layout),
|
||||||
|
cursor_position,
|
||||||
|
viewport,
|
||||||
|
);
|
||||||
|
|
||||||
|
(
|
||||||
|
Primitive::Clip {
|
||||||
|
bounds: *viewport,
|
||||||
|
offset: Vector::new(0, 0),
|
||||||
|
content: Box::new(Primitive::Group {
|
||||||
|
primitives: vec![content, tooltip],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
mouse_interaction,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(content, mouse_interaction)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -259,11 +259,8 @@ where
|
|||||||
pub fn overlay<'b>(
|
pub fn overlay<'b>(
|
||||||
&'b mut self,
|
&'b mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'b, Message, Renderer>> {
|
) -> Option<overlay::Element<'b, Message, Renderer>> {
|
||||||
self.widget
|
self.widget.overlay(layout)
|
||||||
.overlay(layout, overlay_content_bounds, cursor_position)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -355,13 +352,11 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, B, Renderer>> {
|
) -> Option<overlay::Element<'_, B, Renderer>> {
|
||||||
let mapper = &self.mapper;
|
let mapper = &self.mapper;
|
||||||
|
|
||||||
self.widget
|
self.widget
|
||||||
.overlay(layout, overlay_content_bounds, cursor_position)
|
.overlay(layout)
|
||||||
.map(move |overlay| overlay.map(mapper))
|
.map(move |overlay| overlay.map(mapper))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -445,10 +440,7 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
self.element
|
self.element.overlay(layout)
|
||||||
.overlay(layout, overlay_content_bounds, cursor_position)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,11 +19,14 @@ pub struct Layout<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Layout<'a> {
|
impl<'a> Layout<'a> {
|
||||||
pub(crate) fn new(node: &'a Node) -> Self {
|
/// Creates a new [`Layout`] for the given [`Node`] at the origin.
|
||||||
|
pub fn new(node: &'a Node) -> Self {
|
||||||
Self::with_offset(Vector::new(0.0, 0.0), node)
|
Self::with_offset(Vector::new(0.0, 0.0), node)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn with_offset(offset: Vector, node: &'a Node) -> Self {
|
/// Creates a new [`Layout`] for the given [`Node`] with the provided offset
|
||||||
|
/// from the origin.
|
||||||
|
pub fn with_offset(offset: Vector, node: &'a Node) -> Self {
|
||||||
let bounds = node.bounds();
|
let bounds = node.bounds();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -198,11 +198,8 @@ where
|
|||||||
messages: &mut Vec<Message>,
|
messages: &mut Vec<Message>,
|
||||||
) -> Vec<event::Status> {
|
) -> Vec<event::Status> {
|
||||||
let (base_cursor, overlay_statuses) = if let Some(mut overlay) =
|
let (base_cursor, overlay_statuses) = if let Some(mut overlay) =
|
||||||
self.root.overlay(
|
self.root.overlay(Layout::new(&self.base.layout))
|
||||||
Layout::new(&self.base.layout),
|
{
|
||||||
self.overlay.as_ref().map(|l| l.layout.bounds()),
|
|
||||||
cursor_position,
|
|
||||||
) {
|
|
||||||
let layer = Self::overlay_layer(
|
let layer = Self::overlay_layer(
|
||||||
self.overlay.take(),
|
self.overlay.take(),
|
||||||
self.bounds,
|
self.bounds,
|
||||||
@ -337,11 +334,9 @@ where
|
|||||||
) -> Renderer::Output {
|
) -> Renderer::Output {
|
||||||
let viewport = Rectangle::with_size(self.bounds);
|
let viewport = Rectangle::with_size(self.bounds);
|
||||||
|
|
||||||
let overlay = if let Some(mut overlay) = self.root.overlay(
|
let overlay = if let Some(mut overlay) =
|
||||||
Layout::new(&self.base.layout),
|
self.root.overlay(Layout::new(&self.base.layout))
|
||||||
self.overlay.as_ref().map(|l| l.layout.bounds()),
|
{
|
||||||
cursor_position,
|
|
||||||
) {
|
|
||||||
let layer = Self::overlay_layer(
|
let layer = Self::overlay_layer(
|
||||||
self.overlay.take(),
|
self.overlay.take(),
|
||||||
self.bounds,
|
self.bounds,
|
||||||
|
@ -175,8 +175,6 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
_layout: Layout<'_>,
|
_layout: Layout<'_>,
|
||||||
_overlay_content_bounds: Option<Rectangle>,
|
|
||||||
_cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -198,19 +198,11 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
self.children
|
self.children
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.zip(layout.children())
|
.zip(layout.children())
|
||||||
.filter_map(|(child, layout)| {
|
.filter_map(|(child, layout)| child.widget.overlay(layout))
|
||||||
child.widget.overlay(
|
|
||||||
layout,
|
|
||||||
overlay_content_bounds,
|
|
||||||
cursor_position,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.next()
|
.next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,14 +200,8 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
self.content.overlay(
|
self.content.overlay(layout.children().next().unwrap())
|
||||||
layout.children().next().unwrap(),
|
|
||||||
overlay_content_bounds,
|
|
||||||
cursor_position,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -558,15 +558,11 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
self.elements
|
self.elements
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.zip(layout.children())
|
.zip(layout.children())
|
||||||
.filter_map(|((_, pane), layout)| {
|
.filter_map(|((_, pane), layout)| pane.overlay(layout))
|
||||||
pane.overlay(layout, overlay_content_bounds, cursor_position)
|
|
||||||
})
|
|
||||||
.next()
|
.next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
use iced_core::Rectangle;
|
|
||||||
|
|
||||||
use crate::container;
|
use crate::container;
|
||||||
use crate::event::{self, Event};
|
use crate::event::{self, Event};
|
||||||
use crate::layout;
|
use crate::layout;
|
||||||
@ -191,8 +189,6 @@ where
|
|||||||
pub(crate) fn overlay(
|
pub(crate) fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
let body_layout = if self.title_bar.is_some() {
|
let body_layout = if self.title_bar.is_some() {
|
||||||
let mut children = layout.children();
|
let mut children = layout.children();
|
||||||
@ -205,8 +201,7 @@ where
|
|||||||
layout
|
layout
|
||||||
};
|
};
|
||||||
|
|
||||||
self.body
|
self.body.overlay(body_layout)
|
||||||
.overlay(body_layout, overlay_content_bounds, cursor_position)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,8 +274,6 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
_overlay_content_bounds: Option<Rectangle>,
|
|
||||||
_cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
if *self.is_open {
|
if *self.is_open {
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
|
@ -197,19 +197,11 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
self.children
|
self.children
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.zip(layout.children())
|
.zip(layout.children())
|
||||||
.filter_map(|(child, layout)| {
|
.filter_map(|(child, layout)| child.widget.overlay(layout))
|
||||||
child.widget.overlay(
|
|
||||||
layout,
|
|
||||||
overlay_content_bounds,
|
|
||||||
cursor_position,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.next()
|
.next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -401,17 +401,11 @@ where
|
|||||||
fn overlay(
|
fn overlay(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
overlay_content_bounds: Option<Rectangle>,
|
|
||||||
cursor_position: Point,
|
|
||||||
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
) -> Option<overlay::Element<'_, Message, Renderer>> {
|
||||||
let Self { content, state, .. } = self;
|
let Self { content, state, .. } = self;
|
||||||
|
|
||||||
content
|
content
|
||||||
.overlay(
|
.overlay(layout.children().next().unwrap())
|
||||||
layout.children().next().unwrap(),
|
|
||||||
overlay_content_bounds,
|
|
||||||
cursor_position,
|
|
||||||
)
|
|
||||||
.map(|overlay| {
|
.map(|overlay| {
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
let content_layout = layout.children().next().unwrap();
|
let content_layout = layout.children().next().unwrap();
|
||||||
|
@ -3,46 +3,43 @@ use std::hash::Hash;
|
|||||||
|
|
||||||
use iced_core::Rectangle;
|
use iced_core::Rectangle;
|
||||||
|
|
||||||
|
use crate::widget::text::{self, Text};
|
||||||
use crate::{
|
use crate::{
|
||||||
event, layout, overlay, Clipboard, Element, Event, Hasher, Layout, Length,
|
event, layout, Clipboard, Element, Event, Hasher, Layout, Length, Point,
|
||||||
Point, Size, Vector, Widget,
|
Widget,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An element to display a widget over another.
|
/// An element to display a widget over another.
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Tooltip<'a, Message, Renderer: self::Renderer> {
|
pub struct Tooltip<'a, Message, Renderer: self::Renderer + text::Renderer> {
|
||||||
content: Element<'a, Message, Renderer>,
|
content: Element<'a, Message, Renderer>,
|
||||||
tooltip: Element<'a, Message, Renderer>,
|
tooltip: Text<Renderer>,
|
||||||
tooltip_position: TooltipPosition,
|
position: Position,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Renderer> Tooltip<'a, Message, Renderer>
|
impl<'a, Message, Renderer> Tooltip<'a, Message, Renderer>
|
||||||
where
|
where
|
||||||
Renderer: self::Renderer,
|
Renderer: self::Renderer + text::Renderer,
|
||||||
{
|
{
|
||||||
/// Creates an empty [`Tooltip`].
|
/// Creates an empty [`Tooltip`].
|
||||||
///
|
///
|
||||||
/// [`Tooltip`]: struct.Tooltip.html
|
/// [`Tooltip`]: struct.Tooltip.html
|
||||||
pub fn new<T, H>(
|
pub fn new(
|
||||||
content: T,
|
content: impl Into<Element<'a, Message, Renderer>>,
|
||||||
tooltip: H,
|
tooltip: Text<Renderer>,
|
||||||
tooltip_position: TooltipPosition,
|
position: Position,
|
||||||
) -> Self
|
) -> Self {
|
||||||
where
|
|
||||||
T: Into<Element<'a, Message, Renderer>>,
|
|
||||||
H: Into<Element<'a, Message, Renderer>>,
|
|
||||||
{
|
|
||||||
Tooltip {
|
Tooltip {
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
tooltip: tooltip.into(),
|
tooltip,
|
||||||
tooltip_position,
|
position,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The position of the tooltip. Defaults to following the cursor.
|
/// The position of the tooltip. Defaults to following the cursor.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum TooltipPosition {
|
pub enum Position {
|
||||||
/// The tooltip will follow the cursor.
|
/// The tooltip will follow the cursor.
|
||||||
FollowCursor,
|
FollowCursor,
|
||||||
/// The tooltip will appear on the top of the widget.
|
/// The tooltip will appear on the top of the widget.
|
||||||
@ -55,16 +52,10 @@ pub enum TooltipPosition {
|
|||||||
Right,
|
Right,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TooltipPosition {
|
|
||||||
fn default() -> Self {
|
|
||||||
TooltipPosition::FollowCursor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
||||||
for Tooltip<'a, Message, Renderer>
|
for Tooltip<'a, Message, Renderer>
|
||||||
where
|
where
|
||||||
Renderer: self::Renderer,
|
Renderer: self::Renderer + text::Renderer,
|
||||||
{
|
{
|
||||||
fn width(&self) -> Length {
|
fn width(&self) -> Length {
|
||||||
self.content.width()
|
self.content.width()
|
||||||
@ -109,12 +100,15 @@ where
|
|||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
viewport: &Rectangle,
|
viewport: &Rectangle,
|
||||||
) -> Renderer::Output {
|
) -> Renderer::Output {
|
||||||
renderer.draw(
|
self::Renderer::draw(
|
||||||
|
renderer,
|
||||||
defaults,
|
defaults,
|
||||||
cursor_position,
|
cursor_position,
|
||||||
&self.content,
|
|
||||||
layout,
|
layout,
|
||||||
viewport,
|
viewport,
|
||||||
|
&self.content,
|
||||||
|
&self.tooltip,
|
||||||
|
self.position,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,142 +118,6 @@ where
|
|||||||
|
|
||||||
self.content.hash_layout(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`].
|
/// The renderer of a [`Tooltip`].
|
||||||
@ -269,7 +127,7 @@ where
|
|||||||
///
|
///
|
||||||
/// [`Tooltip`]: struct.Tooltip.html
|
/// [`Tooltip`]: struct.Tooltip.html
|
||||||
/// [renderer]: ../../renderer/index.html
|
/// [renderer]: ../../renderer/index.html
|
||||||
pub trait Renderer: crate::Renderer {
|
pub trait Renderer: crate::Renderer + text::Renderer {
|
||||||
/// The style supported by this renderer.
|
/// The style supported by this renderer.
|
||||||
type Style: Default;
|
type Style: Default;
|
||||||
|
|
||||||
@ -280,16 +138,18 @@ pub trait Renderer: crate::Renderer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
defaults: &Self::Defaults,
|
defaults: &Self::Defaults,
|
||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
content: &Element<'_, Message, Self>,
|
|
||||||
content_layout: Layout<'_>,
|
content_layout: Layout<'_>,
|
||||||
viewport: &Rectangle,
|
viewport: &Rectangle,
|
||||||
|
content: &Element<'_, Message, Self>,
|
||||||
|
tooltip: &Text<Self>,
|
||||||
|
position: Position,
|
||||||
) -> Self::Output;
|
) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Renderer> From<Tooltip<'a, Message, Renderer>>
|
impl<'a, Message, Renderer> From<Tooltip<'a, Message, Renderer>>
|
||||||
for Element<'a, Message, Renderer>
|
for Element<'a, Message, Renderer>
|
||||||
where
|
where
|
||||||
Renderer: 'a + self::Renderer,
|
Renderer: 'a + self::Renderer + text::Renderer,
|
||||||
Message: 'a,
|
Message: 'a,
|
||||||
{
|
{
|
||||||
fn from(
|
fn from(
|
||||||
|
@ -3,4 +3,4 @@
|
|||||||
pub type Tooltip<'a, Message> =
|
pub type Tooltip<'a, Message> =
|
||||||
iced_native::Tooltip<'a, Message, crate::Renderer>;
|
iced_native::Tooltip<'a, Message, crate::Renderer>;
|
||||||
|
|
||||||
pub use iced_native::tooltip::TooltipPosition;
|
pub use iced_native::tooltip::Position;
|
||||||
|
Loading…
Reference in New Issue
Block a user