diff options
| author | 2022-11-03 18:57:09 +0100 | |
|---|---|---|
| committer | 2022-11-03 18:57:09 +0100 | |
| commit | d222b5c8b0befab665c20ba0112b28199df0ae44 (patch) | |
| tree | 0ac3a59f04e1c1ca89ff43800efbefd825b015ea /examples/clock | |
| parent | a8f510c39917b2ac42fcc854f0a7eff13aee9838 (diff) | |
| parent | f31c8f2504ea7c004c5caed8913e5da28d2e50e2 (diff) | |
| download | iced-d222b5c8b0befab665c20ba0112b28199df0ae44.tar.gz iced-d222b5c8b0befab665c20ba0112b28199df0ae44.tar.bz2 iced-d222b5c8b0befab665c20ba0112b28199df0ae44.zip | |
Merge pull request #1448 from bungoboingo/fear/linear-gradients
Add linear gradient support to canvas widget
Diffstat (limited to 'examples/clock')
| -rw-r--r-- | examples/clock/src/main.rs | 54 | 
1 files changed, 32 insertions, 22 deletions
| diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 8818fb54..a389c54f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,5 +1,7 @@  use iced::executor; -use iced::widget::canvas::{Cache, Cursor, Geometry, LineCap, Path, Stroke}; +use iced::widget::canvas::{ +    stroke, Cache, Cursor, Geometry, LineCap, Path, Stroke, +};  use iced::widget::{canvas, container};  use iced::{      Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -24,9 +26,9 @@ enum Message {  }  impl Application for Clock { +    type Executor = executor::Default;      type Message = Message;      type Theme = Theme; -    type Executor = executor::Default;      type Flags = ();      fn new(_flags: ()) -> (Self, Command<Message>) { @@ -59,15 +61,6 @@ impl Application for Clock {          Command::none()      } -    fn subscription(&self) -> Subscription<Message> { -        iced::time::every(std::time::Duration::from_millis(500)).map(|_| { -            Message::Tick( -                time::OffsetDateTime::now_local() -                    .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), -            ) -        }) -    } -      fn view(&self) -> Element<Message> {          let canvas = canvas(self as &Self)              .width(Length::Fill) @@ -79,6 +72,15 @@ impl Application for Clock {              .padding(20)              .into()      } + +    fn subscription(&self) -> Subscription<Message> { +        iced::time::every(std::time::Duration::from_millis(500)).map(|_| { +            Message::Tick( +                time::OffsetDateTime::now_local() +                    .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), +            ) +        }) +    }  }  impl<Message> canvas::Program<Message> for Clock { @@ -104,33 +106,41 @@ impl<Message> canvas::Program<Message> for Clock {              let long_hand =                  Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius)); -            let thin_stroke = Stroke { -                width: radius / 100.0, -                color: Color::WHITE, -                line_cap: LineCap::Round, -                ..Stroke::default() +            let width = radius / 100.0; + +            let thin_stroke = || -> Stroke { +                Stroke { +                    width, +                    style: stroke::Style::Solid(Color::WHITE), +                    line_cap: LineCap::Round, +                    ..Stroke::default() +                }              }; -            let wide_stroke = Stroke { -                width: thin_stroke.width * 3.0, -                ..thin_stroke +            let wide_stroke = || -> Stroke { +                Stroke { +                    width: width * 3.0, +                    style: stroke::Style::Solid(Color::WHITE), +                    line_cap: LineCap::Round, +                    ..Stroke::default() +                }              };              frame.translate(Vector::new(center.x, center.y));              frame.with_save(|frame| {                  frame.rotate(hand_rotation(self.now.hour(), 12)); -                frame.stroke(&short_hand, wide_stroke); +                frame.stroke(&short_hand, wide_stroke());              });              frame.with_save(|frame| {                  frame.rotate(hand_rotation(self.now.minute(), 60)); -                frame.stroke(&long_hand, wide_stroke); +                frame.stroke(&long_hand, wide_stroke());              });              frame.with_save(|frame| {                  frame.rotate(hand_rotation(self.now.second(), 60)); -                frame.stroke(&long_hand, thin_stroke); +                frame.stroke(&long_hand, thin_stroke());              })          }); | 
