Skip to content

Commit

Permalink
disallow creating tooltip's without hover_content, change the example…
Browse files Browse the repository at this point in the history
… accordingly
  • Loading branch information
yusdacra committed Aug 18, 2020
1 parent b37deb2 commit 19fb8ca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
6 changes: 4 additions & 2 deletions examples/tooltip/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ 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))
.hover_content(Text::new("hello but smaller").size(30));
let tooltip = Tooltip::new(
Text::new("hello").size(60),
Text::new("hello but smaller").size(30),
);

Container::new(tooltip)
.width(Length::Fill)
Expand Down
19 changes: 4 additions & 15 deletions native/src/widget/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
pub struct Tooltip<'a, Message, Renderer: self::Renderer> {
state: State,
content: Element<'a, Message, Renderer>,
hover_content: Option<Element<'a, Message, Renderer>>,
hover_content: Element<'a, Message, Renderer>,
}

impl<'a, Message, Renderer> Tooltip<'a, Message, Renderer>
Expand All @@ -21,27 +21,16 @@ where
/// Creates an empty [`Tooltip`].
///
/// [`Tooltip`]: struct.Tooltip.html
pub fn new<T>(content: T) -> Self
pub fn new<T>(content: T, hover_content: T) -> Self
where
T: Into<Element<'a, Message, Renderer>>,
{
Tooltip {
state: Default::default(),
content: content.into(),
hover_content: None,
hover_content: hover_content.into(),
}
}

/// Sets the hover_content of the [`Tooltip`].
///
/// [`Tooltip`]: struct.Tooltip.html
pub fn hover_content<T>(mut self, hover_content: T) -> Self
where
T: Into<Element<'a, Message, Renderer>>,
{
self.hover_content = Some(hover_content.into());
self
}
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -126,7 +115,7 @@ where
if layout.bounds().contains(self.state.cursor_position) {
Some(overlay::Element::new(
self.state.cursor_position,
Box::new(Overlay::new(self.hover_content.as_ref()?)),
Box::new(Overlay::new(&self.hover_content)),
))
} else {
None
Expand Down

0 comments on commit 19fb8ca

Please sign in to comment.