diff options
author | 2021-02-24 01:42:08 +0100 | |
---|---|---|
committer | 2021-02-24 01:42:08 +0100 | |
commit | a5fddf9ee648927b294ef34e8819855d5e117b26 (patch) | |
tree | 96f5454cdb07249beefdb0b804de3b10f5ff6cb6 /examples | |
parent | 6759a5c56fc53286d77698ac9a86812b6d7b03ff (diff) | |
parent | 2736e4ca35f17a92768f0be682acf6da3b574cb6 (diff) | |
download | iced-a5fddf9ee648927b294ef34e8819855d5e117b26.tar.gz iced-a5fddf9ee648927b294ef34e8819855d5e117b26.tar.bz2 iced-a5fddf9ee648927b294ef34e8819855d5e117b26.zip |
Merge pull request #465 from yusdacra/tooltip-widget
Tooltip widget
Diffstat (limited to 'examples')
-rw-r--r-- | examples/tooltip/Cargo.toml | 9 | ||||
-rw-r--r-- | examples/tooltip/README.md | 14 | ||||
-rw-r--r-- | examples/tooltip/src/main.rs | 138 |
3 files changed, 161 insertions, 0 deletions
diff --git a/examples/tooltip/Cargo.toml b/examples/tooltip/Cargo.toml new file mode 100644 index 00000000..1171de00 --- /dev/null +++ b/examples/tooltip/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "tooltip" +version = "0.1.0" +authors = ["Yusuf Bera Ertan <y.bera003.06@protonmail.com>"] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } diff --git a/examples/tooltip/README.md b/examples/tooltip/README.md new file mode 100644 index 00000000..4ccf6578 --- /dev/null +++ b/examples/tooltip/README.md @@ -0,0 +1,14 @@ +## Tooltip + +A tooltip. + +It displays and positions a widget on another based on cursor position. + +The __[`main`]__ file contains all the code of the example. + +You can run it with `cargo run`: +``` +cargo run --package tooltip +``` + +[`main`]: src/main.rs diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs new file mode 100644 index 00000000..d6c8b8e1 --- /dev/null +++ b/examples/tooltip/src/main.rs @@ -0,0 +1,138 @@ +use iced::tooltip::{self, Tooltip}; +use iced::{ + button, Button, Column, Container, Element, HorizontalAlignment, Length, + Row, Sandbox, Settings, Text, VerticalAlignment, +}; + +pub fn main() { + Example::run(Settings::default()).unwrap() +} + +#[derive(Default)] +struct Example { + top: button::State, + bottom: button::State, + right: button::State, + left: button::State, + follow_cursor: button::State, +} + +#[derive(Debug, Clone, Copy)] +struct Message; + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Tooltip - Iced") + } + + fn update(&mut self, _message: Message) {} + + fn view(&mut self) -> Element<Message> { + let top = + tooltip("Tooltip at top", &mut self.top, tooltip::Position::Top); + + let bottom = tooltip( + "Tooltip at bottom", + &mut self.bottom, + tooltip::Position::Bottom, + ); + + let left = + tooltip("Tooltip at left", &mut self.left, tooltip::Position::Left); + + let right = tooltip( + "Tooltip at right", + &mut self.right, + tooltip::Position::Right, + ); + + let fixed_tooltips = Row::with_children(vec![ + top.into(), + bottom.into(), + left.into(), + right.into(), + ]) + .width(Length::Fill) + .height(Length::Fill) + .align_items(iced::Align::Center) + .spacing(50); + + let follow_cursor = tooltip( + "Tooltip follows cursor", + &mut self.follow_cursor, + tooltip::Position::FollowCursor, + ); + + let content = Column::with_children(vec![ + Container::new(fixed_tooltips) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into(), + follow_cursor.into(), + ]) + .width(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<'a>( + label: &str, + button_state: &'a mut button::State, + 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), + "Tooltip", + position, + ) + .gap(5) + .padding(10) + .style(style::Tooltip) + .into() +} + +mod style { + use iced::container; + use iced::Color; + + pub struct Tooltip; + + impl container::StyleSheet for Tooltip { + fn style(&self) -> container::Style { + container::Style { + text_color: Some(Color::from_rgb8(0xEE, 0xEE, 0xEE)), + background: Some(Color::from_rgb(0.11, 0.42, 0.87).into()), + border_radius: 12.0, + ..container::Style::default() + } + } + } +} |