diff options
Diffstat (limited to 'examples/clock/src')
| -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());              })          }); | 
