summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-02-23 03:09:16 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-02-23 03:09:16 +0100
commit81c75c15249b608dd8a6d47e25f96feb10ca68da (patch)
treecd069025bcda644c2cc5d00750981c927f15950f /graphics
parenta19f89d3a6af2804f2ac4e30f6d639b56a9bebfd (diff)
downloadiced-81c75c15249b608dd8a6d47e25f96feb10ca68da.tar.gz
iced-81c75c15249b608dd8a6d47e25f96feb10ca68da.tar.bz2
iced-81c75c15249b608dd8a6d47e25f96feb10ca68da.zip
Change `Tooltip` to support `Text` only for now
Diffstat (limited to 'graphics')
-rw-r--r--graphics/src/widget/tooltip.rs76
1 files changed, 71 insertions, 5 deletions
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<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)
+ }
}
}