Change Tooltip to support Text only for now

This commit is contained in:
Héctor Ramón Jiménez 2021-02-23 03:09:16 +01:00
parent a19f89d3a6
commit 81c75c1524
15 changed files with 171 additions and 302 deletions

View File

@ -1,6 +1,7 @@
use iced::tooltip::{self, Tooltip};
use iced::{
button, tooltip::TooltipPosition, Button, Column, Container, Element,
Length, Row, Sandbox, Settings, Text, Tooltip,
button, Button, Column, Container, Element, HorizontalAlignment, Length,
Row, Sandbox, Settings, Text, VerticalAlignment,
};
pub fn main() {
@ -9,11 +10,11 @@ pub fn main() {
#[derive(Default)]
struct Example {
tooltip_top_button_state: button::State,
tooltip_bottom_button_state: button::State,
tooltip_right_button_state: button::State,
tooltip_left_button_state: button::State,
tooltip_cursor_button_state: button::State,
top: button::State,
bottom: button::State,
right: button::State,
left: button::State,
follow_cursor: button::State,
}
#[derive(Debug, Clone, Copy)]
@ -33,52 +34,39 @@ impl Sandbox for Example {
fn update(&mut self, _message: Message) {}
fn view(&mut self) -> Element<Message> {
let tooltip_top = tooltip_builder(
"Tooltip at top",
&mut self.tooltip_top_button_state,
TooltipPosition::Top,
);
let tooltip_bottom = tooltip_builder(
let top =
tooltip("Tooltip at top", &mut self.top, tooltip::Position::Top);
let bottom = tooltip(
"Tooltip at bottom",
&mut self.tooltip_bottom_button_state,
TooltipPosition::Bottom,
&mut self.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",
&mut self.tooltip_right_button_state,
TooltipPosition::Right,
);
let tooltip_left = tooltip_builder(
"Tooltip at left",
&mut self.tooltip_left_button_state,
TooltipPosition::Left,
&mut self.right,
tooltip::Position::Right,
);
let fixed_tooltips = Row::with_children(vec![
tooltip_top.into(),
tooltip_bottom.into(),
tooltip_left.into(),
tooltip_right.into(),
top.into(),
bottom.into(),
left.into(),
right.into(),
])
.width(Length::Fill)
.height(Length::Fill)
.align_items(iced::Align::Center)
.spacing(120);
.spacing(50);
let cursor_tooltip_area = Tooltip::new(
Button::new(
&mut self.tooltip_cursor_button_state,
Container::new(Text::new("Tooltip follows cursor").size(40))
.center_y()
.center_x()
.width(Length::Fill)
.height(Length::Fill),
)
.on_press(Message)
.width(Length::Fill)
.height(Length::Fill),
tooltip(),
TooltipPosition::FollowCursor,
let follow_cursor = tooltip(
"Tooltip follows cursor",
&mut self.follow_cursor,
tooltip::Position::FollowCursor,
);
let content = Column::with_children(vec![
@ -88,36 +76,42 @@ impl Sandbox for Example {
.center_x()
.center_y()
.into(),
cursor_tooltip_area.into(),
follow_cursor.into(),
])
.width(Length::Fill)
.height(Length::Fill);
.height(Length::Fill)
.spacing(50);
Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.padding(50)
.into()
}
}
fn tooltip_builder<'a>(
fn tooltip<'a>(
label: &str,
button_state: &'a mut button::State,
position: TooltipPosition,
) -> Container<'a, Message> {
Container::new(Tooltip::new(
Button::new(button_state, Text::new(label).size(40)).on_press(Message),
tooltip(),
position: tooltip::Position,
) -> Element<'a, Message> {
Tooltip::new(
Button::new(
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,
))
.center_x()
.center_y()
.width(Length::Fill)
.height(Length::Fill)
}
fn tooltip() -> Text {
Text::new("Tooltip").size(20)
)
.into()
}

View File

@ -1,7 +1,10 @@
//! Decorate content and apply alignment.
use crate::backend::{self, Backend};
use crate::defaults::Defaults;
use crate::{Backend, Renderer};
use iced_native::{Element, Layout, Point, Rectangle};
use crate::{Primitive, Renderer, Vector};
use iced_native::layout::{self, Layout};
use iced_native::{Element, Point, Rectangle, Size, Text};
/// An element decorating some content.
///
@ -10,9 +13,11 @@ use iced_native::{Element, Layout, Point, Rectangle};
pub type Tooltip<'a, Message, Backend> =
iced_native::Tooltip<'a, Message, Renderer<Backend>>;
pub use iced_native::tooltip::Position;
impl<B> iced_native::tooltip::Renderer for Renderer<B>
where
B: Backend,
B: Backend + backend::Text,
{
type Style = ();
@ -20,10 +25,14 @@ where
&mut self,
defaults: &Defaults,
cursor_position: Point,
content: &Element<'_, Message, Self>,
content_layout: Layout<'_>,
viewport: &Rectangle,
content: &Element<'_, Message, Self>,
tooltip: &Text<Self>,
position: Position,
) -> Self::Output {
let bounds = content_layout.bounds();
let (content, mouse_interaction) = content.draw(
self,
&defaults,
@ -32,6 +41,63 @@ where
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)
}
}
}

View File

@ -259,11 +259,8 @@ where
pub fn overlay<'b>(
&'b mut self,
layout: Layout<'_>,
overlay_content_bounds: Option<Rectangle>,
cursor_position: Point,
) -> Option<overlay::Element<'b, Message, Renderer>> {
self.widget
.overlay(layout, overlay_content_bounds, cursor_position)
self.widget.overlay(layout)
}
}
@ -355,13 +352,11 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
overlay_content_bounds: Option<Rectangle>,
cursor_position: Point,
) -> Option<overlay::Element<'_, B, Renderer>> {
let mapper = &self.mapper;
self.widget
.overlay(layout, overlay_content_bounds, cursor_position)
.overlay(layout)
.map(move |overlay| overlay.map(mapper))
}
}
@ -445,10 +440,7 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
overlay_content_bounds: Option<Rectangle>,
cursor_position: Point,
) -> Option<overlay::Element<'_, Message, Renderer>> {
self.element
.overlay(layout, overlay_content_bounds, cursor_position)
self.element.overlay(layout)
}
}

View File

@ -19,11 +19,14 @@ pub struct 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)
}
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();
Self {

View File

@ -198,11 +198,8 @@ where
messages: &mut Vec<Message>,
) -> Vec<event::Status> {
let (base_cursor, overlay_statuses) = if let Some(mut overlay) =
self.root.overlay(
Layout::new(&self.base.layout),
self.overlay.as_ref().map(|l| l.layout.bounds()),
cursor_position,
) {
self.root.overlay(Layout::new(&self.base.layout))
{
let layer = Self::overlay_layer(
self.overlay.take(),
self.bounds,
@ -337,11 +334,9 @@ where
) -> Renderer::Output {
let viewport = Rectangle::with_size(self.bounds);
let overlay = if let Some(mut overlay) = self.root.overlay(
Layout::new(&self.base.layout),
self.overlay.as_ref().map(|l| l.layout.bounds()),
cursor_position,
) {
let overlay = if let Some(mut overlay) =
self.root.overlay(Layout::new(&self.base.layout))
{
let layer = Self::overlay_layer(
self.overlay.take(),
self.bounds,

View File

@ -175,8 +175,6 @@ where
fn overlay(
&mut self,
_layout: Layout<'_>,
_overlay_content_bounds: Option<Rectangle>,
_cursor_position: Point,
) -> Option<overlay::Element<'_, Message, Renderer>> {
None
}

View File

@ -198,19 +198,11 @@ 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,
overlay_content_bounds,
cursor_position,
)
})
.filter_map(|(child, layout)| child.widget.overlay(layout))
.next()
}
}

View File

@ -200,14 +200,8 @@ 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(),
overlay_content_bounds,
cursor_position,
)
self.content.overlay(layout.children().next().unwrap())
}
}

View File

@ -558,15 +558,11 @@ 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, overlay_content_bounds, cursor_position)
})
.filter_map(|((_, pane), layout)| pane.overlay(layout))
.next()
}
}

View File

@ -1,5 +1,3 @@
use iced_core::Rectangle;
use crate::container;
use crate::event::{self, Event};
use crate::layout;
@ -191,8 +189,6 @@ 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();
@ -205,8 +201,7 @@ where
layout
};
self.body
.overlay(body_layout, overlay_content_bounds, cursor_position)
self.body.overlay(body_layout)
}
}

View File

@ -274,8 +274,6 @@ 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();

View File

@ -197,19 +197,11 @@ 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,
overlay_content_bounds,
cursor_position,
)
})
.filter_map(|(child, layout)| child.widget.overlay(layout))
.next()
}
}

View File

@ -401,17 +401,11 @@ 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_content_bounds,
cursor_position,
)
.overlay(layout.children().next().unwrap())
.map(|overlay| {
let bounds = layout.bounds();
let content_layout = layout.children().next().unwrap();

View File

@ -3,46 +3,43 @@ use std::hash::Hash;
use iced_core::Rectangle;
use crate::widget::text::{self, Text};
use crate::{
event, layout, overlay, Clipboard, Element, Event, Hasher, Layout, Length,
Point, Size, Vector, Widget,
event, layout, Clipboard, Element, Event, Hasher, Layout, Length, Point,
Widget,
};
/// An element to display a widget over another.
#[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>,
tooltip: Element<'a, Message, Renderer>,
tooltip_position: TooltipPosition,
tooltip: Text<Renderer>,
position: Position,
}
impl<'a, Message, Renderer> Tooltip<'a, Message, Renderer>
where
Renderer: self::Renderer,
Renderer: self::Renderer + text::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>>,
{
pub fn new(
content: impl Into<Element<'a, Message, Renderer>>,
tooltip: Text<Renderer>,
position: Position,
) -> Self {
Tooltip {
content: content.into(),
tooltip: tooltip.into(),
tooltip_position,
tooltip,
position,
}
}
}
/// The position of the tooltip. Defaults to following the cursor.
#[derive(Debug, PartialEq)]
pub enum TooltipPosition {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Position {
/// The tooltip will follow the cursor.
FollowCursor,
/// The tooltip will appear on the top of the widget.
@ -55,16 +52,10 @@ pub enum TooltipPosition {
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,
Renderer: self::Renderer + text::Renderer,
{
fn width(&self) -> Length {
self.content.width()
@ -109,12 +100,15 @@ where
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(
self::Renderer::draw(
renderer,
defaults,
cursor_position,
&self.content,
layout,
viewport,
&self.content,
&self.tooltip,
self.position,
)
}
@ -124,142 +118,6 @@ where
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`].
@ -269,7 +127,7 @@ where
///
/// [`Tooltip`]: struct.Tooltip.html
/// [renderer]: ../../renderer/index.html
pub trait Renderer: crate::Renderer {
pub trait Renderer: crate::Renderer + text::Renderer {
/// The style supported by this renderer.
type Style: Default;
@ -280,16 +138,18 @@ pub trait Renderer: crate::Renderer {
&mut self,
defaults: &Self::Defaults,
cursor_position: Point,
content: &Element<'_, Message, Self>,
content_layout: Layout<'_>,
viewport: &Rectangle,
content: &Element<'_, Message, Self>,
tooltip: &Text<Self>,
position: Position,
) -> Self::Output;
}
impl<'a, Message, Renderer> From<Tooltip<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: 'a + self::Renderer,
Renderer: 'a + self::Renderer + text::Renderer,
Message: 'a,
{
fn from(

View File

@ -3,4 +3,4 @@
pub type Tooltip<'a, Message> =
iced_native::Tooltip<'a, Message, crate::Renderer>;
pub use iced_native::tooltip::TooltipPosition;
pub use iced_native::tooltip::Position;