Skip to content

Commit

Permalink
Add 'TooltipPosition' enum and 'tooltip_position' field to 'Tooltip' …
Browse files Browse the repository at this point in the history
…to be able to change the position of the tooltip, add 'cursor_position' and 'overlay_content_bounds' to overlay method in trait for calculating where to put the tooltip
  • Loading branch information
yusdacra committed Nov 3, 2020
1 parent f59ce87 commit b8c5ebc
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 42 deletions.
98 changes: 91 additions & 7 deletions examples/tooltip/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
use iced::{Container, Element, Length, Sandbox, Settings, Text, Tooltip};
use iced::{
button, tooltip::TooltipPosition, Button, Column, Container, Element,
Length, Row, Sandbox, Settings, Text, Tooltip,
};

pub fn main() {
Example::run(Settings::default()).unwrap()
}

#[derive(Default)]
struct Example {}
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,
}

#[derive(Debug, Clone, Copy)]
enum Message {}
struct Message;

impl Sandbox for Example {
type Message = Message;
Expand All @@ -24,16 +33,91 @@ impl Sandbox for Example {
fn update(&mut self, _message: Message) {}

fn view(&mut self) -> Element<Message> {
let tooltip = Tooltip::new(
Text::new("hello").size(60),
Text::new("hello but smaller").size(30),
let tooltip_top = tooltip_builder(
"Tooltip at top",
&mut self.tooltip_top_button_state,
TooltipPosition::Top,
);
let tooltip_bottom = tooltip_builder(
"Tooltip at bottom",
&mut self.tooltip_bottom_button_state,
TooltipPosition::Bottom,
);
let tooltip_right = tooltip_builder(
"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,
);

Container::new(tooltip)
let fixed_tooltips = Row::with_children(vec![
tooltip_top.into(),
tooltip_bottom.into(),
tooltip_left.into(),
tooltip_right.into(),
])
.width(Length::Fill)
.height(Length::Fill)
.align_items(iced::Align::Center)
.spacing(120);

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 content = Column::with_children(vec![
Container::new(fixed_tooltips)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into(),
cursor_tooltip_area.into(),
])
.width(Length::Fill)
.height(Length::Fill);

Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}

fn tooltip_builder<'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,
))
.center_x()
.center_y()
.width(Length::Fill)
.height(Length::Fill)
}

fn tooltip() -> Text {
Text::new("Tooltip").size(20)
}
14 changes: 11 additions & 3 deletions native/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,11 @@ 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)
self.widget
.overlay(layout, overlay_content_bounds, cursor_position)
}
}

Expand Down Expand Up @@ -371,11 +374,13 @@ 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(layout, overlay_content_bounds, cursor_position)
.map(move |overlay| overlay.map(mapper))
}
}
Expand Down Expand Up @@ -459,7 +464,10 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
overlay_content_bounds: Option<Rectangle>,
cursor_position: Point,
) -> Option<overlay::Element<'_, Message, Renderer>> {
self.element.overlay(layout)
self.element
.overlay(layout, overlay_content_bounds, cursor_position)
}
}
16 changes: 10 additions & 6 deletions native/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ where
) -> Vec<Message> {
let mut messages = Vec::new();

let base_cursor = if let Some(mut overlay) =
self.root.overlay(Layout::new(&self.base.layout))
{
let base_cursor = 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 layer = Self::overlay_layer(
self.overlay.take(),
self.bounds,
Expand Down Expand Up @@ -331,9 +333,11 @@ 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))
{
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 layer = Self::overlay_layer(
self.overlay.take(),
self.bounds,
Expand Down
2 changes: 2 additions & 0 deletions native/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ where
fn overlay(
&mut self,
_layout: Layout<'_>,
_overlay_content_bounds: Option<Rectangle>,
_cursor_position: Point,
) -> Option<overlay::Element<'_, Message, Renderer>> {
None
}
Expand Down
10 changes: 9 additions & 1 deletion native/src/widget/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,19 @@ 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))
.filter_map(|(child, layout)| {
child.widget.overlay(
layout,
overlay_content_bounds,
cursor_position,
)
})
.next()
}
}
Expand Down
8 changes: 7 additions & 1 deletion native/src/widget/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,14 @@ 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())
self.content.overlay(
layout.children().next().unwrap(),
overlay_content_bounds,
cursor_position,
)
}
}

Expand Down
6 changes: 5 additions & 1 deletion native/src/widget/pane_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,15 @@ 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))
.filter_map(|((_, pane), layout)| {
pane.overlay(layout, overlay_content_bounds, cursor_position)
})
.next()
}
}
Expand Down
7 changes: 6 additions & 1 deletion native/src/widget/pane_grid/content.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use iced_core::Rectangle;

use crate::container;
use crate::layout;
use crate::overlay;
Expand Down Expand Up @@ -193,6 +195,8 @@ 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();
Expand All @@ -205,7 +209,8 @@ where
layout
};

self.body.overlay(body_layout)
self.body
.overlay(body_layout, overlay_content_bounds, cursor_position)
}
}

Expand Down
2 changes: 2 additions & 0 deletions native/src/widget/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ 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();
Expand Down
10 changes: 9 additions & 1 deletion native/src/widget/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,19 @@ 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))
.filter_map(|(child, layout)| {
child.widget.overlay(
layout,
overlay_content_bounds,
cursor_position,
)
})
.next()
}
}
Expand Down
8 changes: 7 additions & 1 deletion native/src/widget/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,17 @@ 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(
layout.children().next().unwrap(),
overlay_content_bounds,
cursor_position,
)
.map(|overlay| {
let bounds = layout.bounds();
let content_layout = layout.children().next().unwrap();
Expand Down
Loading

0 comments on commit b8c5ebc

Please sign in to comment.