Skip to content

Commit d222b5c

Browse files
authored
Merge pull request #1448 from bungoboingo/fear/linear-gradients
Add linear gradient support to canvas widget
2 parents a8f510c + f31c8f2 commit d222b5c

47 files changed

Lines changed: 2234 additions & 971 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ members = [
6969
"examples/events",
7070
"examples/exit",
7171
"examples/game_of_life",
72-
"examples/geometry",
7372
"examples/integration_opengl",
7473
"examples/integration_wgpu",
7574
"examples/lazy",
75+
"examples/modern_art",
7676
"examples/multitouch",
7777
"examples/pane_grid",
7878
"examples/pick_list",

examples/arc/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant};
22

33
use iced::executor;
44
use iced::widget::canvas::{
5-
self, Cache, Canvas, Cursor, Geometry, Path, Stroke,
5+
self, stroke, Cache, Canvas, Cursor, Geometry, Path, Stroke,
66
};
77
use iced::{
88
Application, Command, Element, Length, Point, Rectangle, Settings,
@@ -52,11 +52,6 @@ impl Application for Arc {
5252
Command::none()
5353
}
5454

55-
fn subscription(&self) -> Subscription<Message> {
56-
iced::time::every(std::time::Duration::from_millis(10))
57-
.map(|_| Message::Tick)
58-
}
59-
6055
fn view(&self) -> Element<Message> {
6156
Canvas::new(self)
6257
.width(Length::Fill)
@@ -67,6 +62,11 @@ impl Application for Arc {
6762
fn theme(&self) -> Theme {
6863
Theme::Dark
6964
}
65+
66+
fn subscription(&self) -> Subscription<Message> {
67+
iced::time::every(std::time::Duration::from_millis(10))
68+
.map(|_| Message::Tick)
69+
}
7070
}
7171

7272
impl<Message> canvas::Program<Message> for Arc {
@@ -114,7 +114,7 @@ impl<Message> canvas::Program<Message> for Arc {
114114
frame.stroke(
115115
&path,
116116
Stroke {
117-
color: palette.text,
117+
style: stroke::Style::Solid(palette.text),
118118
width: 10.0,
119119
..Stroke::default()
120120
},

examples/clock/src/main.rs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use iced::executor;
2-
use iced::widget::canvas::{Cache, Cursor, Geometry, LineCap, Path, Stroke};
2+
use iced::widget::canvas::{
3+
stroke, Cache, Cursor, Geometry, LineCap, Path, Stroke,
4+
};
35
use iced::widget::{canvas, container};
46
use iced::{
57
Application, Color, Command, Element, Length, Point, Rectangle, Settings,
@@ -24,9 +26,9 @@ enum Message {
2426
}
2527

2628
impl Application for Clock {
29+
type Executor = executor::Default;
2730
type Message = Message;
2831
type Theme = Theme;
29-
type Executor = executor::Default;
3032
type Flags = ();
3133

3234
fn new(_flags: ()) -> (Self, Command<Message>) {
@@ -59,15 +61,6 @@ impl Application for Clock {
5961
Command::none()
6062
}
6163

62-
fn subscription(&self) -> Subscription<Message> {
63-
iced::time::every(std::time::Duration::from_millis(500)).map(|_| {
64-
Message::Tick(
65-
time::OffsetDateTime::now_local()
66-
.unwrap_or_else(|_| time::OffsetDateTime::now_utc()),
67-
)
68-
})
69-
}
70-
7164
fn view(&self) -> Element<Message> {
7265
let canvas = canvas(self as &Self)
7366
.width(Length::Fill)
@@ -79,6 +72,15 @@ impl Application for Clock {
7972
.padding(20)
8073
.into()
8174
}
75+
76+
fn subscription(&self) -> Subscription<Message> {
77+
iced::time::every(std::time::Duration::from_millis(500)).map(|_| {
78+
Message::Tick(
79+
time::OffsetDateTime::now_local()
80+
.unwrap_or_else(|_| time::OffsetDateTime::now_utc()),
81+
)
82+
})
83+
}
8284
}
8385

8486
impl<Message> canvas::Program<Message> for Clock {
@@ -104,33 +106,41 @@ impl<Message> canvas::Program<Message> for Clock {
104106
let long_hand =
105107
Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius));
106108

107-
let thin_stroke = Stroke {
108-
width: radius / 100.0,
109-
color: Color::WHITE,
110-
line_cap: LineCap::Round,
111-
..Stroke::default()
109+
let width = radius / 100.0;
110+
111+
let thin_stroke = || -> Stroke {
112+
Stroke {
113+
width,
114+
style: stroke::Style::Solid(Color::WHITE),
115+
line_cap: LineCap::Round,
116+
..Stroke::default()
117+
}
112118
};
113119

114-
let wide_stroke = Stroke {
115-
width: thin_stroke.width * 3.0,
116-
..thin_stroke
120+
let wide_stroke = || -> Stroke {
121+
Stroke {
122+
width: width * 3.0,
123+
style: stroke::Style::Solid(Color::WHITE),
124+
line_cap: LineCap::Round,
125+
..Stroke::default()
126+
}
117127
};
118128

119129
frame.translate(Vector::new(center.x, center.y));
120130

121131
frame.with_save(|frame| {
122132
frame.rotate(hand_rotation(self.now.hour(), 12));
123-
frame.stroke(&short_hand, wide_stroke);
133+
frame.stroke(&short_hand, wide_stroke());
124134
});
125135

126136
frame.with_save(|frame| {
127137
frame.rotate(hand_rotation(self.now.minute(), 60));
128-
frame.stroke(&long_hand, wide_stroke);
138+
frame.stroke(&long_hand, wide_stroke());
129139
});
130140

131141
frame.with_save(|frame| {
132142
frame.rotate(hand_rotation(self.now.second(), 60));
133-
frame.stroke(&long_hand, thin_stroke);
143+
frame.stroke(&long_hand, thin_stroke());
134144
})
135145
});
136146

examples/geometry/Cargo.toml

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/geometry/README.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)