diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index 6e2c4dd4..a49caa70 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -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 { - 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() } diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index b5b0c558..0b4d1d2f 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -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>; +pub use iced_native::tooltip::Position; + impl iced_native::tooltip::Renderer for Renderer 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, + 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) + } } } diff --git a/native/src/element.rs b/native/src/element.rs index 5e906524..d6e9639a 100644 --- a/native/src/element.rs +++ b/native/src/element.rs @@ -259,11 +259,8 @@ where pub fn overlay<'b>( &'b mut self, layout: Layout<'_>, - overlay_content_bounds: Option, - cursor_position: Point, ) -> Option> { - 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, - cursor_position: Point, ) -> Option> { 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, - cursor_position: Point, ) -> Option> { - self.element - .overlay(layout, overlay_content_bounds, cursor_position) + self.element.overlay(layout) } } diff --git a/native/src/layout.rs b/native/src/layout.rs index 6d144902..b4b4a021 100644 --- a/native/src/layout.rs +++ b/native/src/layout.rs @@ -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 { diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 996bdd30..d1835b65 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -198,11 +198,8 @@ where messages: &mut Vec, ) -> Vec { 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, diff --git a/native/src/widget.rs b/native/src/widget.rs index 1309d6af..d5c353df 100644 --- a/native/src/widget.rs +++ b/native/src/widget.rs @@ -175,8 +175,6 @@ where fn overlay( &mut self, _layout: Layout<'_>, - _overlay_content_bounds: Option, - _cursor_position: Point, ) -> Option> { None } diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 9ee60627..e0e88d31 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -198,19 +198,11 @@ where fn overlay( &mut self, layout: Layout<'_>, - overlay_content_bounds: Option, - cursor_position: Point, ) -> Option> { 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() } } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 2fc6707e..65764148 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -200,14 +200,8 @@ where fn overlay( &mut self, layout: Layout<'_>, - overlay_content_bounds: Option, - cursor_position: Point, ) -> Option> { - self.content.overlay( - layout.children().next().unwrap(), - overlay_content_bounds, - cursor_position, - ) + self.content.overlay(layout.children().next().unwrap()) } } diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 0a7d818d..c6fe4b60 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -558,15 +558,11 @@ where fn overlay( &mut self, layout: Layout<'_>, - overlay_content_bounds: Option, - cursor_position: Point, ) -> Option> { 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() } } diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs index 28515624..913cfe96 100644 --- a/native/src/widget/pane_grid/content.rs +++ b/native/src/widget/pane_grid/content.rs @@ -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, - cursor_position: Point, ) -> Option> { 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) } } diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 6c424d28..74f4508e 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -274,8 +274,6 @@ where fn overlay( &mut self, layout: Layout<'_>, - _overlay_content_bounds: Option, - _cursor_position: Point, ) -> Option> { if *self.is_open { let bounds = layout.bounds(); diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index c542aedc..b71663bd 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -197,19 +197,11 @@ where fn overlay( &mut self, layout: Layout<'_>, - overlay_content_bounds: Option, - cursor_position: Point, ) -> Option> { 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() } } diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 86a68f22..18cdf169 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -401,17 +401,11 @@ where fn overlay( &mut self, layout: Layout<'_>, - overlay_content_bounds: Option, - cursor_position: Point, ) -> Option> { 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(); diff --git a/native/src/widget/tooltip.rs b/native/src/widget/tooltip.rs index cae38d46..72d03c1a 100644 --- a/native/src/widget/tooltip.rs +++ b/native/src/widget/tooltip.rs @@ -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, + 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( - content: T, - tooltip: H, - tooltip_position: TooltipPosition, - ) -> Self - where - T: Into>, - H: Into>, - { + pub fn new( + content: impl Into>, + tooltip: Text, + 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 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, - cursor_position: Point, - ) -> Option> { - 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 - 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::().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, + position: Position, ) -> Self::Output; } impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Renderer: 'a + self::Renderer, + Renderer: 'a + self::Renderer + text::Renderer, Message: 'a, { fn from( diff --git a/wgpu/src/widget/tooltip.rs b/wgpu/src/widget/tooltip.rs index b7d4f11e..89ab3a15 100644 --- a/wgpu/src/widget/tooltip.rs +++ b/wgpu/src/widget/tooltip.rs @@ -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;