summaryrefslogtreecommitdiffstats
path: root/examples/tooltip
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-07-27 06:49:20 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-07-27 06:49:20 +0200
commitff2519b1d43d481987351a83b6dd7237524c21f0 (patch)
tree5731eeb7eb1247d4a8951de0d5bc5d8102640559 /examples/tooltip
parentc44267b85f7aaa2997e3caf1323b837d95818c22 (diff)
downloadiced-ff2519b1d43d481987351a83b6dd7237524c21f0.tar.gz
iced-ff2519b1d43d481987351a83b6dd7237524c21f0.tar.bz2
iced-ff2519b1d43d481987351a83b6dd7237524c21f0.zip
Replace stateful widgets with new `iced_pure` API
Diffstat (limited to 'examples/tooltip')
-rw-r--r--examples/tooltip/src/main.rs134
1 files changed, 46 insertions, 88 deletions
diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs
index 7c800112..35b862a8 100644
--- a/examples/tooltip/src/main.rs
+++ b/examples/tooltip/src/main.rs
@@ -1,117 +1,75 @@
-use iced::alignment::{self, Alignment};
-use iced::button;
use iced::theme;
-use iced::tooltip::{self, Tooltip};
-use iced::{
- Button, Column, Container, Element, Length, Row, Sandbox, Settings, Text,
-};
+use iced::widget::tooltip::Position;
+use iced::widget::{button, container, tooltip};
+use iced::{Element, Length, Sandbox, Settings};
-pub fn main() {
- Example::run(Settings::default()).unwrap()
+pub fn main() -> iced::Result {
+ Example::run(Settings::default())
}
-#[derive(Default)]
struct Example {
- top: button::State,
- bottom: button::State,
- right: button::State,
- left: button::State,
- follow_cursor: button::State,
+ position: Position,
}
-#[derive(Debug, Clone, Copy)]
-struct Message;
+#[derive(Debug, Clone)]
+enum Message {
+ ChangePosition,
+}
impl Sandbox for Example {
type Message = Message;
fn new() -> Self {
- Self::default()
+ Self {
+ position: Position::Bottom,
+ }
}
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, bottom, left, right])
- .width(Length::Fill)
- .height(Length::Fill)
- .align_items(Alignment::Center)
- .spacing(50);
-
- let follow_cursor = tooltip(
- "Tooltip follows cursor",
- &mut self.follow_cursor,
- tooltip::Position::FollowCursor,
- );
+ fn update(&mut self, message: Message) {
+ match message {
+ Message::ChangePosition => {
+ let position = match &self.position {
+ Position::FollowCursor => Position::Top,
+ Position::Top => Position::Bottom,
+ Position::Bottom => Position::Left,
+ Position::Left => Position::Right,
+ Position::Right => Position::FollowCursor,
+ };
+
+ self.position = position
+ }
+ }
+ }
- let content = Column::with_children(vec![
- Container::new(fixed_tooltips)
- .width(Length::Fill)
- .height(Length::Fill)
- .center_x()
- .center_y()
- .into(),
- follow_cursor,
- ])
- .width(Length::Fill)
- .height(Length::Fill)
- .spacing(50);
+ fn view(&self) -> Element<Message> {
+ let tooltip = tooltip(
+ button("Press to change position")
+ .on_press(Message::ChangePosition),
+ position_to_text(self.position),
+ self.position,
+ )
+ .gap(10)
+ .style(theme::Container::Box);
- Container::new(content)
+ container(tooltip)
.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(alignment::Horizontal::Center)
- .vertical_alignment(alignment::Vertical::Center),
- )
- .on_press(Message)
- .width(Length::Fill)
- .height(Length::Fill),
- "Tooltip",
- position,
- )
- .gap(5)
- .padding(10)
- .style(theme::Container::Box)
- .into()
+fn position_to_text<'a>(position: Position) -> &'a str {
+ match position {
+ Position::FollowCursor => "Follow Cursor",
+ Position::Top => "Top",
+ Position::Bottom => "Bottom",
+ Position::Left => "Left",
+ Position::Right => "Right",
+ }
}