Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tooltip function to accept a generic element as a tooltip #2228

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Dracula, Nord, Solarized, and Gruvbox variants for `Theme`. [#2170](https://github.com/iced-rs/iced/pull/2170)
- `From<T> where T: Into<PathBuf>` for `svg::Handle`. [#2235](https://github.com/iced-rs/iced/pull/2235)
- `on_open` and `on_close` handlers for `PickList`. [#2174](https://github.com/iced-rs/iced/pull/2174)
- Support for generic `Element` in `Tooltip`. [#2228](https://github.com/iced-rs/iced/pull/2228)

### Changed
- Enable WebGPU backend in `wgpu` by default instead of WebGL. [#2068](https://github.com/iced-rs/iced/pull/2068)
Expand Down
7 changes: 4 additions & 3 deletions widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,21 @@ where
Button::new(content)
}

/// Creates a new [`Tooltip`] with the provided content, tooltip text, and [`tooltip::Position`].
/// Creates a new [`Tooltip`] for the provided content with the given
/// [`Element`] and [`tooltip::Position`].
///
/// [`Tooltip`]: crate::Tooltip
/// [`tooltip::Position`]: crate::tooltip::Position
pub fn tooltip<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
tooltip: impl ToString,
tooltip: impl Into<Element<'a, Message, Theme, Renderer>>,
position: tooltip::Position,
) -> crate::Tooltip<'a, Message, Theme, Renderer>
where
Theme: container::StyleSheet + text::StyleSheet,
Renderer: core::text::Renderer,
{
Tooltip::new(content, tooltip.to_string(), position)
Tooltip::new(content, tooltip, position)
}

/// Creates a new [`Text`] widget with the provided content.
Expand Down
53 changes: 16 additions & 37 deletions widget/src/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ use crate::core::{
Clipboard, Element, Length, Padding, Pixels, Point, Rectangle, Shell, Size,
Vector,
};
use crate::Text;

use std::borrow::Cow;

/// An element to display a widget over another.
#[allow(missing_debug_implementations)]
Expand All @@ -27,7 +24,7 @@ pub struct Tooltip<
Renderer: text::Renderer,
{
content: Element<'a, Message, Theme, Renderer>,
tooltip: Text<'a, Theme, Renderer>,
tooltip: Element<'a, Message, Theme, Renderer>,
position: Position,
gap: f32,
padding: f32,
Expand All @@ -48,12 +45,12 @@ where
/// [`Tooltip`]: struct.Tooltip.html
pub fn new(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
tooltip: impl Into<Cow<'a, str>>,
tooltip: impl Into<Element<'a, Message, Theme, Renderer>>,
position: Position,
) -> Self {
Tooltip {
content: content.into(),
tooltip: Text::new(tooltip),
tooltip: tooltip.into(),
position,
gap: 0.0,
padding: Self::DEFAULT_PADDING,
Expand All @@ -62,26 +59,6 @@ where
}
}

/// Sets the size of the text of the [`Tooltip`].
pub fn size(mut self, size: impl Into<Pixels>) -> Self {
self.tooltip = self.tooltip.size(size);
self
}

/// Sets the [`text::Shaping`] strategy of the [`Tooltip`].
pub fn text_shaping(mut self, shaping: text::Shaping) -> Self {
self.tooltip = self.tooltip.shaping(shaping);
self
}

/// Sets the font of the [`Tooltip`].
///
/// [`Font`]: Renderer::Font
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.tooltip = self.tooltip.font(font);
self
}

/// Sets the gap between the content and its [`Tooltip`].
pub fn gap(mut self, gap: impl Into<Pixels>) -> Self {
self.gap = gap.into().0;
Expand Down Expand Up @@ -119,12 +96,15 @@ where
fn children(&self) -> Vec<widget::Tree> {
vec![
widget::Tree::new(&self.content),
widget::Tree::new(&self.tooltip as &dyn Widget<Message, _, _>),
widget::Tree::new(&self.tooltip),
]
}

fn diff(&self, tree: &mut widget::Tree) {
tree.diff_children(&[self.content.as_widget(), &self.tooltip]);
tree.diff_children(&[
self.content.as_widget(),
self.tooltip.as_widget(),
]);
}

fn state(&self) -> widget::tree::State {
Expand Down Expand Up @@ -312,13 +292,13 @@ enum State {
},
}

struct Overlay<'a, 'b, Theme, Renderer>
struct Overlay<'a, 'b, Message, Theme, Renderer>
where
Theme: container::StyleSheet + widget::text::StyleSheet,
Renderer: text::Renderer,
{
position: Point,
tooltip: &'b Text<'a, Theme, Renderer>,
tooltip: &'b Element<'a, Message, Theme, Renderer>,
state: &'b mut widget::Tree,
cursor_position: Point,
content_bounds: Rectangle,
Expand All @@ -331,16 +311,15 @@ where

impl<'a, 'b, Message, Theme, Renderer>
overlay::Overlay<Message, Theme, Renderer>
for Overlay<'a, 'b, Theme, Renderer>
for Overlay<'a, 'b, Message, Theme, Renderer>
where
Theme: container::StyleSheet + widget::text::StyleSheet,
Renderer: text::Renderer,
{
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node {
let viewport = Rectangle::with_size(bounds);

let text_layout = Widget::<(), Theme, Renderer>::layout(
self.tooltip,
let tooltip_layout = self.tooltip.as_widget().layout(
self.state,
renderer,
&layout::Limits::new(
Expand All @@ -352,7 +331,7 @@ where
.shrink(Padding::new(self.padding)),
);

let text_bounds = text_layout.bounds();
let text_bounds = tooltip_layout.bounds();
let x_center = self.position.x
+ (self.content_bounds.width - text_bounds.width) / 2.0;
let y_center = self.position.y
Expand Down Expand Up @@ -429,7 +408,8 @@ where

layout::Node::with_children(
tooltip_bounds.size(),
vec![text_layout.translate(Vector::new(self.padding, self.padding))],
vec![tooltip_layout
.translate(Vector::new(self.padding, self.padding))],
)
.translate(Vector::new(tooltip_bounds.x, tooltip_bounds.y))
}
Expand All @@ -450,8 +430,7 @@ where
text_color: style.text_color.unwrap_or(inherited_style.text_color),
};

Widget::<(), Theme, Renderer>::draw(
self.tooltip,
self.tooltip.as_widget().draw(
self.state,
renderer,
theme,
Expand Down
Loading