summaryrefslogtreecommitdiffstats
path: root/examples/clock
diff options
context:
space:
mode:
authorLibravatar shan <shankern@protonmail.com>2022-09-29 10:52:58 -0700
committerLibravatar shan <shankern@protonmail.com>2022-09-29 11:15:35 -0700
commit40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb (patch)
tree38ffc5dd6bae5da4da3b93664dfe27e024dfa261 /examples/clock
parent97f385e093711c269df315b28f76e66e0220e22a (diff)
downloadiced-40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb.tar.gz
iced-40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb.tar.bz2
iced-40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb.zip
Adds linear gradient support to 2D meshes in the canvas widget.
Diffstat (limited to 'examples/clock')
-rw-r--r--examples/clock/src/main.rs54
1 files changed, 32 insertions, 22 deletions
diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs
index 8818fb54..51f25a3f 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::{
+ Cache, Cursor, Geometry, LineCap, Path, Stroke, StrokeStyle,
+};
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: StrokeStyle::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: StrokeStyle::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());
})
});