summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml4
-rw-r--r--core/src/color.rs9
-rw-r--r--core/src/lib.rs2
-rw-r--r--core/src/point.rs7
-rw-r--r--core/src/size.rs (renamed from native/src/size.rs)0
-rw-r--r--examples/bezier_tool/src/main.rs54
-rw-r--r--examples/clock/Cargo.toml15
-rw-r--r--examples/clock/src/main.rs190
-rw-r--r--examples/geometry/src/main.rs123
-rw-r--r--examples/solar_system/Cargo.toml15
-rw-r--r--examples/solar_system/src/main.rs244
-rw-r--r--native/src/lib.rs4
-rw-r--r--src/lib.rs2
-rw-r--r--web/src/lib.rs4
-rw-r--r--wgpu/Cargo.toml16
-rw-r--r--wgpu/src/lib.rs2
-rw-r--r--wgpu/src/primitive.rs10
-rw-r--r--wgpu/src/renderer.rs12
-rw-r--r--wgpu/src/shader/triangle.vert4
-rw-r--r--wgpu/src/shader/triangle.vert.spvbin1468 -> 1256 bytes
-rw-r--r--wgpu/src/triangle.rs84
-rw-r--r--wgpu/src/widget.rs7
-rw-r--r--wgpu/src/widget/canvas.rs118
-rw-r--r--wgpu/src/widget/canvas/fill.rs12
-rw-r--r--wgpu/src/widget/canvas/frame.rs206
-rw-r--r--wgpu/src/widget/canvas/layer.rs16
-rw-r--r--wgpu/src/widget/canvas/layer/cached.rs82
-rw-r--r--wgpu/src/widget/canvas/path.rs183
-rw-r--r--wgpu/src/widget/canvas/stroke.rs66
29 files changed, 1334 insertions, 157 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 11cca8b3..01231b70 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,6 +16,8 @@ categories = ["gui"]
image = ["iced_wgpu/image"]
# Enables the `Svg` widget
svg = ["iced_wgpu/svg"]
+# Enables the `Canvas` widget
+canvas = ["iced_wgpu/canvas"]
# Enables a debug view in native platforms (press F12)
debug = ["iced_winit/debug"]
# Enables `tokio` as the `executor::Default` on native platforms
@@ -36,6 +38,7 @@ members = [
"wgpu",
"winit",
"examples/bezier_tool",
+ "examples/clock",
"examples/counter",
"examples/custom_widget",
"examples/events",
@@ -43,6 +46,7 @@ members = [
"examples/integration",
"examples/pokedex",
"examples/progress_bar",
+ "examples/solar_system",
"examples/stopwatch",
"examples/styling",
"examples/svg",
diff --git a/core/src/color.rs b/core/src/color.rs
index d6bdd365..db509b88 100644
--- a/core/src/color.rs
+++ b/core/src/color.rs
@@ -44,11 +44,18 @@ impl Color {
///
/// [`Color`]: struct.Color.html
pub fn from_rgb8(r: u8, g: u8, b: u8) -> Color {
+ Color::from_rgba8(r, g, b, 1.0)
+ }
+
+ /// Creates a [`Color`] from its RGB8 components and an alpha value.
+ ///
+ /// [`Color`]: struct.Color.html
+ pub fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
Color {
r: f32::from(r) / 255.0,
g: f32::from(g) / 255.0,
b: f32::from(b) / 255.0,
- a: 1.0,
+ a,
}
}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 3cbce743..ea5e8b43 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -22,6 +22,7 @@ mod font;
mod length;
mod point;
mod rectangle;
+mod size;
mod vector;
pub use align::{Align, HorizontalAlignment, VerticalAlignment};
@@ -31,4 +32,5 @@ pub use font::Font;
pub use length::Length;
pub use point::Point;
pub use rectangle::Rectangle;
+pub use size::Size;
pub use vector::Vector;
diff --git a/core/src/point.rs b/core/src/point.rs
index 47c8b142..b9a8149c 100644
--- a/core/src/point.rs
+++ b/core/src/point.rs
@@ -11,10 +11,15 @@ pub struct Point {
}
impl Point {
+ /// The origin (i.e. a [`Point`] with both X=0 and Y=0).
+ ///
+ /// [`Point`]: struct.Point.html
+ pub const ORIGIN: Point = Point::new(0.0, 0.0);
+
/// Creates a new [`Point`] with the given coordinates.
///
/// [`Point`]: struct.Point.html
- pub fn new(x: f32, y: f32) -> Self {
+ pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
diff --git a/native/src/size.rs b/core/src/size.rs
index 389b3247..389b3247 100644
--- a/native/src/size.rs
+++ b/core/src/size.rs
diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs
index 043d265c..efdb3924 100644
--- a/examples/bezier_tool/src/main.rs
+++ b/examples/bezier_tool/src/main.rs
@@ -94,7 +94,7 @@ mod bezier {
layout: Layout<'_>,
cursor_position: Point,
) -> (Primitive, MouseCursor) {
- let mut buffer: VertexBuffers<Vertex2D, u16> = VertexBuffers::new();
+ let mut buffer: VertexBuffers<Vertex2D, u32> = VertexBuffers::new();
let mut path_builder = lyon::path::Path::builder();
let bounds = layout.bounds();
@@ -102,7 +102,7 @@ mod bezier {
// Draw rectangle border with lyon.
basic_shapes::stroke_rectangle(
&lyon::math::Rect::new(
- lyon::math::Point::new(bounds.x + 0.5, bounds.y + 0.5),
+ lyon::math::Point::new(0.5, 0.5),
lyon::math::Size::new(
bounds.width - 1.0,
bounds.height - 1.0,
@@ -121,48 +121,35 @@ mod bezier {
for curve in self.curves {
path_builder.move_to(lyon::math::Point::new(
- curve.from.x + bounds.x,
- curve.from.y + bounds.y,
+ curve.from.x,
+ curve.from.y,
));
path_builder.quadratic_bezier_to(
- lyon::math::Point::new(
- curve.control.x + bounds.x,
- curve.control.y + bounds.y,
- ),
- lyon::math::Point::new(
- curve.to.x + bounds.x,
- curve.to.y + bounds.y,
- ),
+ lyon::math::Point::new(curve.control.x, curve.control.y),
+ lyon::math::Point::new(curve.to.x, curve.to.y),
);
}
match self.state.pending {
None => {}
Some(Pending::One { from }) => {
- path_builder.move_to(lyon::math::Point::new(
- from.x + bounds.x,
- from.y + bounds.y,
- ));
+ path_builder
+ .move_to(lyon::math::Point::new(from.x, from.y));
path_builder.line_to(lyon::math::Point::new(
- cursor_position.x,
- cursor_position.y,
+ cursor_position.x - bounds.x,
+ cursor_position.y - bounds.y,
));
}
Some(Pending::Two { from, to }) => {
- path_builder.move_to(lyon::math::Point::new(
- from.x + bounds.x,
- from.y + bounds.y,
- ));
+ path_builder
+ .move_to(lyon::math::Point::new(from.x, from.y));
path_builder.quadratic_bezier_to(
lyon::math::Point::new(
- cursor_position.x,
- cursor_position.y,
- ),
- lyon::math::Point::new(
- to.x + bounds.x,
- to.y + bounds.y,
+ cursor_position.x - bounds.x,
+ cursor_position.y - bounds.y,
),
+ lyon::math::Point::new(to.x, to.y),
);
}
}
@@ -186,10 +173,13 @@ mod bezier {
)
.unwrap();
- let mesh = Primitive::Mesh2D(Arc::new(Mesh2D {
- vertices: buffer.vertices,
- indices: buffer.indices,
- }));
+ let mesh = Primitive::Mesh2D {
+ origin: Point::new(bounds.x, bounds.y),
+ buffers: Arc::new(Mesh2D {
+ vertices: buffer.vertices,
+ indices: buffer.indices,
+ }),
+ };
(
Primitive::Clip {
diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml
new file mode 100644
index 00000000..308cbfbb
--- /dev/null
+++ b/examples/clock/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "clock"
+version = "0.1.0"
+authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
+edition = "2018"
+publish = false
+
+[features]
+canvas = []
+
+[dependencies]
+iced = { path = "../..", features = ["canvas", "async-std", "debug"] }
+iced_native = { path = "../../native" }
+chrono = "0.4"
+async-std = { version = "1.0", features = ["unstable"] }
diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs
new file mode 100644
index 00000000..0a70709f
--- /dev/null
+++ b/examples/clock/src/main.rs
@@ -0,0 +1,190 @@
+use iced::{
+ canvas, executor, Application, Canvas, Color, Command, Container, Element,
+ Length, Point, Settings, Subscription, Vector,
+};
+
+pub fn main() {
+ Clock::run(Settings::default())
+}
+
+struct Clock {
+ now: LocalTime,
+ clock: canvas::layer::Cached<LocalTime>,
+}
+
+#[derive(Debug, Clone, Copy)]
+enum Message {
+ Tick(chrono::DateTime<chrono::Local>),
+}
+
+impl Application for Clock {
+ type Executor = executor::Default;
+ type Message = Message;
+
+ fn new() -> (Self, Command<Message>) {
+ (
+ Clock {
+ now: chrono::Local::now().into(),
+ clock: canvas::layer::Cached::new(),
+ },
+ Command::none(),
+ )
+ }
+
+ fn title(&self) -> String {
+ String::from("Clock - Iced")
+ }
+
+ fn update(&mut self, message: Message) -> Command<Message> {
+ match message {
+ Message::Tick(local_time) => {
+ let now = local_time.into();
+
+ if now != self.now {
+ self.now = now;
+ self.clock.clear();
+ }
+ }
+ }
+
+ Command::none()
+ }
+
+ fn subscription(&self) -> Subscription<Message> {
+ time::every(std::time::Duration::from_millis(500)).map(Message::Tick)
+ }
+
+ fn view(&mut self) -> Element<Message> {
+ let canvas = Canvas::new()
+ .width(Length::Units(400))
+ .height(Length::Units(400))
+ .push(self.clock.with(&self.now));
+
+ Container::new(canvas)
+ .width(Length::Fill)
+ .height(Length::Fill)
+ .center_x()
+ .center_y()
+ .into()
+ }
+}
+
+#[derive(Debug, PartialEq, Eq)]
+struct LocalTime {
+ hour: u32,
+ minute: u32,
+ second: u32,
+}
+
+impl From<chrono::DateTime<chrono::Local>> for LocalTime {
+ fn from(date_time: chrono::DateTime<chrono::Local>) -> LocalTime {
+ use chrono::Timelike;
+
+ LocalTime {
+ hour: date_time.hour(),
+ minute: date_time.minute(),
+ second: date_time.second(),
+ }
+ }
+}
+
+impl canvas::layer::Drawable for LocalTime {
+ fn draw(&self, frame: &mut canvas::Frame) {
+ let center = frame.center();
+ let radius = frame.width().min(frame.height()) / 2.0;
+ let offset = Vector::new(center.x, center.y);
+
+ let clock = canvas::Path::new(|path| path.circle(center, radius));
+
+ frame.fill(
+ &clock,
+ canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)),
+ );
+
+ fn draw_hand(
+ n: u32,
+ total: u32,
+ length: f32,
+ offset: Vector,
+ path: &mut canvas::path::Builder,
+ ) {
+ let turns = n as f32 / total as f32;
+ let t = 2.0 * std::f32::consts::PI * (turns - 0.25);
+
+ let x = length * t.cos();
+ let y = length * t.sin();
+
+ path.line_to(Point::new(x, y) + offset);
+ }
+
+ let hour_and_minute_hands = canvas::Path::new(|path| {
+ path.move_to(center);
+ draw_hand(self.hour, 12, 0.5 * radius, offset, path);
+
+ path.move_to(center);
+ draw_hand(self.minute, 60, 0.8 * radius, offset, path)
+ });
+
+ frame.stroke(
+ &hour_and_minute_hands,
+ canvas::Stroke {
+ width: 6.0,
+ color: Color::WHITE,
+ line_cap: canvas::LineCap::Round,
+ ..canvas::Stroke::default()
+ },
+ );
+
+ let second_hand = canvas::Path::new(|path| {
+ path.move_to(center);
+ draw_hand(self.second, 60, 0.8 * radius, offset, path)
+ });
+
+ frame.stroke(
+ &second_hand,
+ canvas::Stroke {
+ width: 3.0,
+ color: Color::WHITE,
+ line_cap: canvas::LineCap::Round,
+ ..canvas::Stroke::default()
+ },
+ );
+ }
+}
+
+mod time {
+ use iced::futures;
+
+ pub fn every(
+ duration: std::time::Duration,
+ ) -> iced::Subscription<chrono::DateTime<chrono::Local>> {
+ iced::Subscription::from_recipe(Every(duration))
+ }
+
+ struct Every(std::time::Duration);
+
+ impl<H, I> iced_native::subscription::Recipe<H, I> for Every
+ where
+ H: std::hash::Hasher,
+ {
+ type Output = chrono::DateTime<chrono::Local>;
+
+ fn hash(&self, state: &mut H) {
+ use std::hash::Hash;
+
+ std::any::TypeId::of::<Self>().hash(state);
+ self.0.hash(state);
+ }
+
+ fn stream(
+ self: Box<Self>,
+ _input: futures::stream::BoxStream<'static, I>,
+ ) -> futures::stream::BoxStream<'static, Self::Output> {
+ use futures::stream::StreamExt;
+
+ async_std::stream::interval(self.0)
+ .map(|_| chrono::Local::now())
+ .boxed()
+ }
+ }
+}
diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs
index 9d5fd611..795c6a71 100644
--- a/examples/geometry/src/main.rs
+++ b/examples/geometry/src/main.rs
@@ -69,72 +69,75 @@ mod rainbow {
let posn_center = {
if b.contains(cursor_position) {
- [cursor_position.x, cursor_position.y]
+ [cursor_position.x - b.x, cursor_position.y - b.y]
} else {
- [b.x + (b.width / 2.0), b.y + (b.height / 2.0)]
+ [b.width / 2.0, b.height / 2.0]
}
};
- let posn_tl = [b.x, b.y];
- let posn_t = [b.x + (b.width / 2.0), b.y];
- let posn_tr = [b.x + b.width, b.y];
- let posn_r = [b.x + b.width, b.y + (b.height / 2.0)];
- let posn_br = [b.x + b.width, b.y + b.height];
- let posn_b = [b.x + (b.width / 2.0), b.y + b.height];
- let posn_bl = [b.x, b.y + b.height];
- let posn_l = [b.x, b.y + (b.height / 2.0)];
+ let posn_tl = [0.0, 0.0];
+ let posn_t = [b.width / 2.0, 0.0];
+ let posn_tr = [b.width, 0.0];
+ let posn_r = [b.width, b.height / 2.0];
+ let posn_br = [b.width, b.height];
+ let posn_b = [(b.width / 2.0), b.height];
+ let posn_bl = [0.0, b.height];
+ let posn_l = [0.0, b.height / 2.0];
(
- Primitive::Mesh2D(std::sync::Arc::new(Mesh2D {
- vertices: vec![
- Vertex2D {
- position: posn_center,
- color: [1.0, 1.0, 1.0, 1.0],
- },
- Vertex2D {
- position: posn_tl,
- color: color_r,
- },
- Vertex2D {
- position: posn_t,
- color: color_o,
- },
- Vertex2D {
- position: posn_tr,
- color: color_y,
- },
- Vertex2D {
- position: posn_r,
- color: color_g,
- },
- Vertex2D {
- position: posn_br,
- color: color_gb,
- },
- Vertex2D {
- position: posn_b,
- color: color_b,
- },
- Vertex2D {
- position: posn_bl,
- color: color_i,
- },
- Vertex2D {
- position: posn_l,
- color: color_v,
- },
- ],
- indices: vec![
- 0, 1, 2, // TL
- 0, 2, 3, // T
- 0, 3, 4, // TR
- 0, 4, 5, // R
- 0, 5, 6, // BR
- 0, 6, 7, // B
- 0, 7, 8, // BL
- 0, 8, 1, // L
- ],
- })),
+ Primitive::Mesh2D {
+ origin: Point::new(b.x, b.y),
+ buffers: std::sync::Arc::new(Mesh2D {
+ vertices: vec![
+ Vertex2D {
+ position: posn_center,
+ color: [1.0, 1.0, 1.0, 1.0],
+ },
+ Vertex2D {
+ position: posn_tl,
+ color: color_r,
+ },
+ Vertex2D {
+ position: posn_t,
+ color: color_o,
+ },
+ Vertex2D {
+ position: posn_tr,
+ color: color_y,
+ },
+ Vertex2D {
+ position: posn_r,
+ color: color_g,
+ },
+ Vertex2D {
+ position: posn_br,
+ color: color_gb,
+ },
+ Vertex2D {
+ position: posn_b,
+ color: color_b,
+ },
+ Vertex2D {
+ position: posn_bl,
+ color: color_i,
+ },
+ Vertex2D {
+ position: posn_l,
+ color: color_v,
+ },
+ ],
+ indices: vec![
+ 0, 1, 2, // TL
+ 0, 2, 3, // T
+ 0, 3, 4, // TR
+ 0, 4, 5, // R
+ 0, 5, 6, // BR
+ 0, 6, 7, // B
+ 0, 7, 8, // BL
+ 0, 8, 1, // L
+ ],
+ }),
+ },
MouseCursor::OutOfBounds,
)
}
diff --git a/examples/solar_system/Cargo.toml b/examples/solar_system/Cargo.toml
new file mode 100644
index 00000000..c88cda50
--- /dev/null
+++ b/examples/solar_system/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "solar_system"
+version = "0.1.0"
+authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
+edition = "2018"
+publish = false
+
+[features]
+canvas = []
+
+[dependencies]
+iced = { path = "../..", features = ["canvas", "async-std", "debug"] }
+iced_native = { path = "../../native" }
+async-std = { version = "1.0", features = ["unstable"] }
+rand = "0.7"
diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs
new file mode 100644
index 00000000..d05acf84
--- /dev/null
+++ b/examples/solar_system/src/main.rs
@@ -0,0 +1,244 @@
+//! An animated solar system.
+//!
+//! This example showcases how to use a `Canvas` widget with transforms to draw
+//! using different coordinate systems.
+//!
+//! Inspired by the example found in the MDN docs[1].
+//!
+//! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system
+use iced::{
+ canvas, executor, Application, Canvas, Color, Command, Container, Element,
+ Length, Point, Settings, Size, Subscription, Vector,
+};
+
+use std::time::Instant;
+
+pub fn main() {
+ SolarSystem::run(Settings::default())
+}
+
+struct SolarSystem {
+ state: State,
+ solar_system: canvas::layer::Cached<State>,
+}
+
+#[derive(Debug, Clone, Copy)]
+enum Message {
+ Tick(Instant),
+}
+
+impl Application for SolarSystem {
+ type Executor = executor::Default;
+ type Message = Message;
+
+ fn new() -> (Self, Command<Message>) {
+ (
+ SolarSystem {
+ state: State::new(),
+ solar_system: canvas::layer::Cached::new(),
+ },
+ Command::none(),
+ )
+ }
+
+ fn title(&self) -> String {
+ String::from("Solar system - Iced")
+ }
+
+ fn update(&mut self, message: Message) -> Command<Message> {
+ match message {
+ Message::Tick(instant) => {
+ self.state.update(instant);
+ self.solar_system.clear();
+ }
+ }
+
+ Command::none()
+ }
+
+ fn subscription(&self) -> Subscription<Message> {
+ time::every(std::time::Duration::from_millis(10))
+ .map(|instant| Message::Tick(instant))
+ }
+
+ fn view(&mut self) -> Element<Message> {
+ let canvas = Canvas::new()
+ .width(Length::Fill)
+ .height(Length::Fill)
+ .push(self.solar_system.with(&self.state));
+
+ Container::new(canvas)
+ .width(Length::Fill)
+ .height(Length::Fill)
+ .center_x()
+ .center_y()
+ .into()
+ }
+}
+
+#[derive(Debug)]
+struct State {
+ start: Instant,
+ current: Instant,
+ stars: Vec<(Point, f32)>,
+}
+
+impl State {
+ const SUN_RADIUS: f32 = 70.0;
+ const ORBIT_RADIUS: f32 = 150.0;
+ const EARTH_RADIUS: f32 = 12.0;
+ const MOON_RADIUS: f32 = 4.0;
+ const MOON_DISTANCE: f32 = 28.0;
+
+ pub fn new() -> State {
+ let now = Instant::now();
+ let (width, height) = Settings::default().window.size;
+
+ State {
+ start: now,
+ current: now,
+ stars: {
+ use rand::Rng;
+
+ let mut rng = rand::thread_rng();
+
+ (0..100)
+ .map(|_| {
+ (
+ Point::new(
+ rng.gen_range(0.0, width as f32),
+ rng.gen_range(0.0, height as f32),
+ ),
+ rng.gen_range(0.5, 1.0),
+ )
+ })
+ .collect()
+ },
+ }
+ }
+
+ pub fn update(&mut self, now: Instant) {
+ self.current = now;
+ }
+}
+
+impl canvas::layer::Drawable for State {
+ fn draw(&self, frame: &mut canvas::Frame) {
+ use canvas::{Fill, Path, Stroke};
+ use std::f32::consts::PI;
+
+ let center = frame.center();
+
+ let space = Path::new(|path| {
+ path.rectangle(Point::new(0.0, 0.0), frame.size())
+ });
+
+ let stars = Path::new(|path| {
+ for (p, size) in &self.stars {
+ path.rectangle(*p, Size::new(*size, *size));
+ }
+ });
+
+ let sun = Path::new(|path| path.circle(center, Self::SUN_RADIUS));
+ let orbit = Path::new(|path| path.circle(center, Self::ORBIT_RADIUS));
+
+ frame.fill(&space, Fill::Color(Color::BLACK));
+ frame.fill(&stars, Fill::Color(Color::WHITE));
+ frame.fill(&sun, Fill::Color(Color::from_rgb8(0xF9, 0xD7, 0x1C)));
+ frame.stroke(
+ &orbit,
+ Stroke {
+ width: 1.0,
+ color: Color::from_rgba8(0, 153, 255, 0.1),
+ ..Stroke::default()
+ },
+ );
+
+ let elapsed = self.current - self.start;
+ let elapsed_seconds = elapsed.as_secs() as f32;
+ let elapsed_millis = elapsed.subsec_millis() as f32;
+
+ frame.with_save(|frame| {
+ frame.translate(Vector::new(center.x, center.y));
+ frame.rotate(
+ (2.0 * PI / 60.0) * elapsed_seconds
+ + (2.0 * PI / 60_000.0) * elapsed_millis,
+ );
+ frame.translate(Vector::new(Self::ORBIT_RADIUS, 0.0));
+
+ let earth = Path::new(|path| {
+ path.circle(Point::ORIGIN, Self::EARTH_RADIUS)
+ });
+
+ let shadow = Path::new(|path| {
+ path.rectangle(
+ Point::new(0.0, -Self::EARTH_RADIUS),
+ Size::new(
+ Self::EARTH_RADIUS * 4.0,
+ Self::EARTH_RADIUS * 2.0,
+ ),
+ )
+ });
+
+ frame.fill(&earth, Fill::Color(Color::from_rgb8(0x6B, 0x93, 0xD6)));
+
+ frame.with_save(|frame| {
+ frame.rotate(
+ ((2.0 * PI) / 6.0) * elapsed_seconds
+ + ((2.0 * PI) / 6_000.0) * elapsed_millis,
+ );
+ frame.translate(Vector::new(0.0, Self::MOON_DISTANCE));
+
+ let moon = Path::new(|path| {
+ path.circle(Point::ORIGIN, Self::MOON_RADIUS)
+ });
+
+ frame.fill(&moon, Fill::Color(Color::WHITE));
+ });
+
+ frame.fill(
+ &shadow,
+ Fill::Color(Color {
+ a: 0.7,
+ ..Color::BLACK
+ }),
+ );
+ });
+ }
+}
+
+mod time {
+ use iced::futures;
+ use std::time::Instant;
+
+ pub fn every(duration: std::time::Duration) -> iced::Subscription<Instant> {
+ iced::Subscription::from_recipe(Every(duration))
+ }
+
+ struct Every(std::time::Duration);
+
+ impl<H, I> iced_native::subscription::Recipe<H, I> for Every
+ where
+ H: std::hash::Hasher,
+ {
+ type Output = Instant;
+
+ fn hash(&self, state: &mut H) {
+ use std::hash::Hash;
+
+ std::any::TypeId::of::<Self>().hash(state);
+ self.0.hash(state);
+ }
+
+ fn stream(
+ self: Box<Self>,
+ _input: futures::stream::BoxStream<'static, I>,
+ ) -> futures::stream::BoxStream<'static, Self::Output> {
+ use futures::stream::StreamExt;
+
+ async_std::stream::interval(self.0)
+ .map(|_| Instant::now())
+ .boxed()
+ }
+ }
+}
diff --git a/native/src/lib.rs b/native/src/lib.rs
index 3b81ef71..e4e7baee 100644
--- a/native/src/lib.rs
+++ b/native/src/lib.rs
@@ -52,12 +52,11 @@ mod event;
mod hasher;
mod mouse_cursor;
mod runtime;
-mod size;
mod user_interface;
pub use iced_core::{
Align, Background, Color, Font, HorizontalAlignment, Length, Point,
- Rectangle, Vector, VerticalAlignment,
+ Rectangle, Size, Vector, VerticalAlignment,
};
pub use iced_futures::{executor, futures, Command};
@@ -72,7 +71,6 @@ pub use layout::Layout;
pub use mouse_cursor::MouseCursor;
pub use renderer::Renderer;
pub use runtime::Runtime;
-pub use size::Size;
pub use subscription::Subscription;
pub use user_interface::{Cache, UserInterface};
pub use widget::*;
diff --git a/src/lib.rs b/src/lib.rs
index 7e658ca2..d492db02 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -204,5 +204,5 @@ use iced_web as common;
pub use common::{
futures, Align, Background, Color, Command, Font, HorizontalAlignment,
- Length, Space, Subscription, Vector, VerticalAlignment,
+ Length, Point, Size, Space, Subscription, Vector, VerticalAlignment,
};
diff --git a/web/src/lib.rs b/web/src/lib.rs
index 7b54a07a..258ad9e7 100644
--- a/web/src/lib.rs
+++ b/web/src/lib.rs
@@ -73,8 +73,8 @@ pub use dodrio;
pub use element::Element;
pub use hasher::Hasher;
pub use iced_core::{
- Align, Background, Color, Font, HorizontalAlignment, Length, Vector,
- VerticalAlignment,
+ Align, Background, Color, Font, HorizontalAlignment, Length, Point, Size,
+ Vector, VerticalAlignment,
};
pub use iced_futures::{executor, futures, Command};
pub use subscription::Subscription;
diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml
index 60b98b40..887c2d21 100644
--- a/wgpu/Cargo.toml
+++ b/wgpu/Cargo.toml
@@ -9,6 +9,7 @@ repository = "https://github.com/hecrj/iced"
[features]
svg = ["resvg"]
+canvas = ["lyon"]
[dependencies]
iced_native = { version = "0.1.0", path = "../native" }
@@ -20,5 +21,16 @@ raw-window-handle = "0.3"
glam = "0.8"
font-kit = "0.4"
log = "0.4"
-resvg = { version = "0.8", features = ["raqote-backend"], optional = true }
-image = { version = "0.22", optional = true }
+
+[dependencies.image]
+version = "0.22"
+optional = true
+
+[dependencies.resvg]
+version = "0.8"
+features = ["raqote-backend"]
+optional = true
+
+[dependencies.lyon]
+version = "0.15"
+optional = true
diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs
index e4834818..d38e2a31 100644
--- a/wgpu/src/lib.rs
+++ b/wgpu/src/lib.rs
@@ -19,7 +19,7 @@
//! [`wgpu`]: https://github.com/gfx-rs/wgpu-rs
//! [WebGPU API]: https://gpuweb.github.io/gpuweb/
//! [`wgpu_glyph`]: https://github.com/hecrj/wgpu_glyph
-#![deny(missing_docs)]
+//#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(unused_results)]
#![forbid(unsafe_code)]
diff --git a/wgpu/src/primitive.rs b/wgpu/src/primitive.rs
index 481252ef..823b4b72 100644
--- a/wgpu/src/primitive.rs
+++ b/wgpu/src/primitive.rs
@@ -1,5 +1,5 @@
use iced_native::{
- image, svg, Background, Color, Font, HorizontalAlignment, Rectangle,
+ image, svg, Background, Color, Font, HorizontalAlignment, Point, Rectangle,
Vector, VerticalAlignment,
};
@@ -73,7 +73,13 @@ pub enum Primitive {
/// A low-level primitive to render a mesh of triangles.
///
/// It can be used to render many kinds of geometry freely.
- Mesh2D(Arc<triangle::Mesh2D>),
+ Mesh2D {
+ /// The top-left coordinate of the mesh
+ origin: Point,
+
+ /// The vertex and index buffers of the mesh
+ buffers: Arc<triangle::Mesh2D>,
+ },
}
impl Default for Primitive {
diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs
index e93090b8..25b2e99a 100644
--- a/wgpu/src/renderer.rs
+++ b/wgpu/src/renderer.rs
@@ -26,7 +26,7 @@ struct Layer<'a> {
offset: Vector<u32>,
quads: Vec<Quad>,
images: Vec<Image>,
- meshes: Vec<Arc<triangle::Mesh2D>>,
+ meshes: Vec<(Point, Arc<triangle::Mesh2D>)>,
text: Vec<wgpu_glyph::Section<'a>>,
}
@@ -229,8 +229,8 @@ impl Renderer {
scale: [bounds.width, bounds.height],
});
}
- Primitive::Mesh2D(mesh) => {
- layer.meshes.push(mesh.clone());
+ Primitive::Mesh2D { origin, buffers } => {
+ layer.meshes.push((*origin, buffers.clone()));
}
Primitive::Clip {
bounds,
@@ -313,9 +313,10 @@ impl Renderer {
if layer.meshes.len() > 0 {
let translated = transformation
+ * Transformation::scale(scale_factor, scale_factor)
* Transformation::translate(
- -(layer.offset.x as f32) * scale_factor,
- -(layer.offset.y as f32) * scale_factor,
+ -(layer.offset.x as f32),
+ -(layer.offset.y as f32),
);
self.triangle_pipeline.draw(
@@ -323,7 +324,6 @@ impl Renderer {
encoder,
target,
translated,
- scale_factor,
&layer.meshes,
bounds,
);
diff --git a/wgpu/src/shader/triangle.vert b/wgpu/src/shader/triangle.vert
index fd86ecd6..1f2c009b 100644
--- a/wgpu/src/shader/triangle.vert
+++ b/wgpu/src/shader/triangle.vert
@@ -7,11 +7,9 @@ layout(location = 0) out vec4 o_Color;
layout (set = 0, binding = 0) uniform Globals {
mat4 u_Transform;
- float u_Scale;
};
void main() {
- vec2 p_Position = i_Position * u_Scale;
- gl_Position = u_Transform * vec4(p_Position, 0.0, 1.0);
+ gl_Position = u_Transform * vec4(i_Position, 0.0, 1.0);
o_Color = i_Color;
}
diff --git a/wgpu/src/shader/triangle.vert.spv b/wgpu/src/shader/triangle.vert.spv
index bc39c451..871f4f55 100644
--- a/wgpu/src/shader/triangle.vert.spv
+++ b/wgpu/src/shader/triangle.vert.spv
Binary files differ
diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs
index 38157d00..3cc1d3fb 100644
--- a/wgpu/src/triangle.rs
+++ b/wgpu/src/triangle.rs
@@ -1,6 +1,6 @@
//! Draw meshes of triangles.
use crate::Transformation;
-use iced_native::Rectangle;
+use iced_native::{Point, Rectangle};
use std::{mem, sync::Arc};
#[derive(Debug)]
@@ -91,7 +91,7 @@ impl Pipeline {
write_mask: wgpu::ColorWrite::ALL,
}],
depth_stencil_state: None,
- index_format: wgpu::IndexFormat::Uint16,
+ index_format: wgpu::IndexFormat::Uint32,
vertex_buffers: &[wgpu::VertexBufferDescriptor {
stride: mem::size_of::<Vertex2D>() as u64,
step_mode: wgpu::InputStepMode::Vertex,
@@ -128,47 +128,28 @@ impl Pipeline {
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
transformation: Transformation,
- scale: f32,
- meshes: &Vec<Arc<Mesh2D>>,
+ meshes: &Vec<(Point, Arc<Mesh2D>)>,
bounds: Rectangle<u32>,
) {
- let uniforms = Uniforms {
- transform: transformation.into(),
- scale,
- };
-
- let constants_buffer = device
- .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
- .fill_from_slice(&[uniforms]);
-
- encoder.copy_buffer_to_buffer(
- &constants_buffer,
- 0,
- &self.constants_buffer,
- 0,
- std::mem::size_of::<Uniforms>() as u64,
- );
-
- let mut render_pass =
- encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
- color_attachments: &[
- wgpu::RenderPassColorAttachmentDescriptor {
- attachment: target,
- resolve_target: None,
- load_op: wgpu::LoadOp::Load,
- store_op: wgpu::StoreOp::Store,
- clear_color: wgpu::Color {
- r: 0.0,
- g: 0.0,
- b: 0.0,
- a: 0.0,
- },
- },
- ],
- depth_stencil_attachment: None,
- });
+ for (origin, mesh) in meshes {
+ let uniforms = Uniforms {
+ transform: (transformation
+ * Transformation::translate(origin.x, origin.y))
+ .into(),
+ };
+
+ let constants_buffer = device
+ .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
+ .fill_from_slice(&[uniforms]);
+
+ encoder.copy_buffer_to_buffer(
+ &constants_buffer,
+ 0,
+ &self.constants_buffer,
+ 0,
+ std::mem::size_of::<Uniforms>() as u64,
+ );
- for mesh in meshes {
let vertices_buffer = device
.create_buffer_mapped(
mesh.vertices.len(),
@@ -183,6 +164,25 @@ impl Pipeline {
)
.fill_from_slice(&mesh.indices);
+ let mut render_pass =
+ encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ color_attachments: &[
+ wgpu::RenderPassColorAttachmentDescriptor {
+ attachment: target,
+ resolve_target: None,
+ load_op: wgpu::LoadOp::Load,
+ store_op: wgpu::StoreOp::Store,
+ clear_color: wgpu::Color {
+ r: 0.0,
+ g: 0.0,
+ b: 0.0,
+ a: 0.0,
+ },
+ },
+ ],
+ depth_stencil_attachment: None,
+ });
+
render_pass.set_pipeline(&self.pipeline);
render_pass.set_bind_group(0, &self.constants, &[]);
render_pass.set_index_buffer(&indices_buffer, 0);
@@ -203,14 +203,12 @@ impl Pipeline {
#[derive(Debug, Clone, Copy)]
struct Uniforms {
transform: [f32; 16],
- scale: f32,
}
impl Default for Uniforms {
fn default() -> Self {
Self {
transform: *Transformation::identity().as_ref(),
- scale: 1.0,
}
}
}
@@ -235,5 +233,5 @@ pub struct Mesh2D {
/// The list of vertex indices that defines the triangles of the mesh.
///
/// Therefore, this list should always have a length that is a multiple of 3.
- pub indices: Vec<u16>,
+ pub indices: Vec<u32>,
}
diff --git a/wgpu/src/widget.rs b/wgpu/src/widget.rs
index e3edda0b..73cce7e2 100644
--- a/wgpu/src/widget.rs
+++ b/wgpu/src/widget.rs
@@ -32,3 +32,10 @@ pub use scrollable::Scrollable;
pub use slider::Slider;
#[doc(no_inline)]
pub use text_input::TextInput;
+
+#[cfg(feature = "canvas")]
+pub mod canvas;
+
+#[cfg(feature = "canvas")]
+#[doc(no_inline)]
+pub use canvas::Canvas;
diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs
new file mode 100644
index 00000000..e8fdc1e8
--- /dev/null
+++ b/wgpu/src/widget/canvas.rs
@@ -0,0 +1,118 @@
+//! Draw freely in 2D.
+use crate::{Defaults, Primitive, Renderer};
+
+use iced_native::{
+ layout, Element, Hasher, Layout, Length, MouseCursor, Point, Size, Widget,
+};
+use std::hash::Hash;
+
+pub mod layer;
+pub mod path;
+
+mod fill;
+mod frame;
+mod stroke;
+
+pub use fill::Fill;
+pub use frame::Frame;
+pub use layer::Layer;
+pub use path::Path;
+pub use stroke::{LineCap, LineJoin, Stroke};
+
+/// A 2D drawable region.
+#[derive(Debug)]
+pub struct Canvas<'a> {
+ width: Length,
+ height: Length,
+ layers: Vec<Box<dyn Layer + 'a>>,
+}
+
+impl<'a> Canvas<'a> {
+ const DEFAULT_SIZE: u16 = 100;
+
+ pub fn new() -> Self {
+ Canvas {
+ width: Length::Units(Self::DEFAULT_SIZE),
+ height: Length::Units(Self::DEFAULT_SIZE),
+ layers: Vec::new(),
+ }
+ }
+
+ pub fn width(mut self, width: Length) -> Self {
+ self.width = width;
+ self
+ }
+
+ pub fn height(mut self, height: Length) -> Self {
+ self.height = height;
+ self
+ }
+
+ pub fn push(mut self, layer: impl Layer + 'a) -> Self {
+ self.layers.push(Box::new(layer));
+ self
+ }
+}
+
+impl<'a, Message> Widget<Message, Renderer> for Canvas<'a> {
+ fn width(&self) -> Length {
+ self.width
+ }
+
+ fn height(&self) -> Length {
+ self.height
+ }
+
+ fn layout(
+ &self,
+ _renderer: &Renderer,
+ limits: &layout::Limits,
+ ) -> layout::Node {
+ let limits = limits.width(self.width).height(self.height);
+ let size = limits.resolve(Size::ZERO);
+
+ layout::Node::new(size)
+ }
+
+ fn draw(
+ &self,
+ _renderer: &mut Renderer,
+ _defaults: &Defaults,
+ layout: Layout<'_>,
+ _cursor_position: Point,
+ ) -> (Primitive, MouseCursor) {
+ let bounds = layout.bounds();
+ let origin = Point::new(bounds.x, bounds.y);
+ let size = Size::new(bounds.width, bounds.height);
+
+ (
+ Primitive::Group {
+ primitives: self
+ .layers
+ .iter()
+ .map(|layer| Primitive::Mesh2D {
+ origin,
+ buffers: layer.draw(size),
+ })
+ .collect(),
+ },
+ MouseCursor::Idle,
+ )
+ }
+
+ fn hash_layout(&self, state: &mut Hasher) {
+ std::any::TypeId::of::<Canvas<'static>>().hash(state);
+
+ self.width.hash(state);
+ self.height.hash(state);
+ }
+}
+
+impl<'a, Message> From<Canvas<'a>> for Element<'a, Message, Renderer>
+where
+ Message: 'static,
+{
+ fn from(canvas: Canvas<'a>) -> Element<'a, Message, Renderer> {
+ Element::new(canvas)
+ }
+}
diff --git a/wgpu/src/widget/canvas/fill.rs b/wgpu/src/widget/canvas/fill.rs
new file mode 100644
index 00000000..9c23f997
--- /dev/null
+++ b/wgpu/src/widget/canvas/fill.rs
@@ -0,0 +1,12 @@
+use iced_native::Color;
+
+#[derive(Debug, Clone, Copy)]
+pub enum Fill {
+ Color(Color),
+}
+
+impl Default for Fill {
+ fn default() -> Fill {
+ Fill::Color(Color::BLACK)
+ }
+}
diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs
new file mode 100644
index 00000000..27d676d6
--- /dev/null
+++ b/wgpu/src/widget/canvas/frame.rs
@@ -0,0 +1,206 @@
+use iced_native::{Point, Size, Vector};
+
+use crate::{
+ canvas::{Fill, Path, Stroke},
+ triangle,
+};
+
+#[derive(Debug)]
+pub struct Frame {
+ width: f32,
+ height: f32,
+ buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u32>,
+
+ transforms: Transforms,
+}
+
+#[derive(Debug)]
+struct Transforms {
+ previous: Vec<Transform>,
+ current: Transform,
+}
+
+#[derive(Debug, Clone, Copy)]
+struct Transform {
+ raw: lyon::math::Transform,
+ is_identity: bool,
+}
+
+impl Frame {
+ pub fn new(width: f32, height: f32) -> Frame {
+ Frame {
+ width,
+ height,
+ buffers: lyon::tessellation::VertexBuffers::new(),
+ transforms: Transforms {
+ previous: Vec::new(),
+ current: Transform {
+ raw: lyon::math::Transform::identity(),
+ is_identity: true,
+ },
+ },
+ }
+ }
+
+ #[inline]
+ pub fn width(&self) -> f32 {
+ self.width
+ }
+
+ #[inline]
+ pub fn height(&self) -> f32 {
+ self.height
+ }
+
+ #[inline]
+ pub fn size(&self) -> Size {
+ Size::new(self.width, self.height)
+ }
+
+ #[inline]
+ pub fn center(&self) -> Point {
+ Point::new(self.width / 2.0, self.height / 2.0)
+ }
+
+ pub fn fill(&mut self, path: &Path, fill: Fill) {
+ use lyon::tessellation::{
+ BuffersBuilder, FillOptions, FillTessellator,
+ };
+
+ let mut buffers = BuffersBuilder::new(
+ &mut self.buffers,
+ FillVertex(match fill {
+ Fill::Color(color) => color.into_linear(),
+ }),
+ );
+
+ let mut tessellator = FillTessellator::new();
+
+ let result = if self.transforms.current.is_identity {
+ tessellator.tessellate_path(
+ path.raw(),
+ &FillOptions::default(),
+ &mut buffers,
+ )
+ } else {
+ let path = path.transformed(&self.transforms.current.raw);
+
+ tessellator.tessellate_path(
+ path.raw(),
+ &FillOptions::default(),
+ &mut buffers,
+ )
+ };
+
+ let _ = result.expect("Tessellate path");
+ }
+
+ pub fn stroke(&mut self, path: &Path, stroke: Stroke) {
+ use lyon::tessellation::{
+ BuffersBuilder, StrokeOptions, StrokeTessellator,
+ };
+
+ let mut buffers = BuffersBuilder::new(
+ &mut self.buffers,
+ StrokeVertex(stroke.color.into_linear()),
+ );
+
+ let mut tessellator = StrokeTessellator::new();
+
+ let mut options = StrokeOptions::default();
+ options.line_width = stroke.width;
+ options.start_cap = stroke.line_cap.into();
+ options.end_cap = stroke.line_cap.into();
+ options.line_join = stroke.line_join.into();
+
+ let result = if self.transforms.current.is_identity {
+ tessellator.tessellate_path(path.raw(), &options, &mut buffers)
+ } else {
+ let path = path.transformed(&self.transforms.current.raw);
+
+ tessellator.tessellate_path(path.raw(), &options, &mut buffers)
+ };
+
+ let _ = result.expect("Stroke path");
+ }
+
+ #[inline]
+ pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) {
+ self.transforms.previous.push(self.transforms.current);
+
+ f(self);
+
+ self.transforms.current = self.transforms.previous.pop().unwrap();
+ }
+
+ #[inline]
+ pub fn translate(&mut self, translation: Vector) {
+ self.transforms.current.raw = self
+ .transforms
+ .current
+ .raw
+ .pre_translate(lyon::math::Vector::new(
+ translation.x,
+ translation.y,
+ ));
+ self.transforms.current.is_identity = false;
+ }
+
+ #[inline]
+ pub fn rotate(&mut self, angle: f32) {
+ self.transforms.current.raw = self
+ .transforms
+ .current
+ .raw
+ .pre_rotate(lyon::math::Angle::radians(-angle));
+ self.transforms.current.is_identity = false;
+ }
+
+ #[inline]
+ pub fn scale(&mut self, scale: f32) {
+ self.transforms.current.raw =
+ self.transforms.current.raw.pre_scale(scale, scale);
+ self.transforms.current.is_identity = false;
+ }
+
+ pub fn into_mesh(self) -> triangle::Mesh2D {
+ triangle::Mesh2D {
+ vertices: self.buffers.vertices,
+ indices: self.buffers.indices,
+ }
+ }
+}
+
+struct FillVertex([f32; 4]);
+
+impl lyon::tessellation::FillVertexConstructor<triangle::Vertex2D>
+ for FillVertex
+{
+ fn new_vertex(
+ &mut self,
+ position: lyon::math::Point,
+ _attributes: lyon::tessellation::FillAttributes<'_>,
+ ) -> triangle::Vertex2D {
+ triangle::Vertex2D {
+ position: [position.x, position.y],
+ color: self.0,
+ }
+ }
+}
+
+struct StrokeVertex([f32; 4]);
+
+impl lyon::tessellation::StrokeVertexConstructor<triangle::Vertex2D>
+ for StrokeVertex
+{
+ fn new_vertex(
+ &mut self,
+ position: lyon::math::Point,
+ _attributes: lyon::tessellation::StrokeAttributes<'_, '_>,
+ ) -> triangle::Vertex2D {
+ triangle::Vertex2D {
+ position: [position.x, position.y],
+ color: self.0,
+ }
+ }
+}
diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs
new file mode 100644
index 00000000..8c069f18
--- /dev/null
+++ b/wgpu/src/widget/canvas/layer.rs
@@ -0,0 +1,16 @@
+mod cached;
+
+pub use cached::Cached;
+
+use crate::{canvas::Frame, triangle};
+
+use iced_native::Size;
+use std::sync::Arc;
+
+pub trait Layer: std::fmt::Debug {
+ fn draw(&self, bounds: Size) -> Arc<triangle::Mesh2D>;
+}
+
+pub trait Drawable {
+ fn draw(&self, frame: &mut Frame);
+}
diff --git a/wgpu/src/widget/canvas/layer/cached.rs b/wgpu/src/widget/canvas/layer/cached.rs
new file mode 100644
index 00000000..c6741372
--- /dev/null
+++ b/wgpu/src/widget/canvas/layer/cached.rs
@@ -0,0 +1,82 @@
+use crate::{
+ canvas::{layer::Drawable, Frame, Layer},
+ triangle,
+};
+
+use iced_native::Size;
+use std::cell::RefCell;
+use std::marker::PhantomData;
+use std::sync::Arc;
+
+#[derive(Debug)]
+pub struct Cached<T: Drawable> {
+ input: PhantomData<T>,
+ cache: RefCell<Cache>,
+}
+
+#[derive(Debug)]
+enum Cache {
+ Empty,
+ Filled {
+ mesh: Arc<triangle::Mesh2D>,
+ bounds: Size,
+ },
+}
+
+impl<T> Cached<T>
+where
+ T: Drawable + std::fmt::Debug,
+{
+ pub fn new() -> Self {
+ Cached {
+ input: PhantomData,
+ cache: RefCell::new(Cache::Empty),
+ }
+ }
+
+ pub fn clear(&mut self) {
+ *self.cache.borrow_mut() = Cache::Empty;
+ }
+
+ pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a {
+ Bind {
+ layer: self,
+ input: input,
+ }
+ }
+}
+
+#[derive(Debug)]
+struct Bind<'a, T: Drawable> {
+ layer: &'a Cached<T>,
+ input: &'a T,
+}
+
+impl<'a, T> Layer for Bind<'a, T>
+where
+ T: Drawable + std::fmt::Debug,
+{
+ fn draw(&self, current_bounds: Size) -> Arc<triangle::Mesh2D> {
+ use std::ops::Deref;
+
+ if let Cache::Filled { mesh, bounds } =
+ self.layer.cache.borrow().deref()
+ {
+ if *bounds == current_bounds {
+ return mesh.clone();
+ }
+ }
+
+ let mut frame = Frame::new(current_bounds.width, current_bounds.height);
+ self.input.draw(&mut frame);
+
+ let mesh = Arc::new(frame.into_mesh());
+
+ *self.layer.cache.borrow_mut() = Cache::Filled {
+ mesh: mesh.clone(),
+ bounds: current_bounds,
+ };
+
+ mesh
+ }
+}
diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs
new file mode 100644
index 00000000..b70d0aef
--- /dev/null
+++ b/wgpu/src/widget/canvas/path.rs
@@ -0,0 +1,183 @@
+use iced_native::{Point, Size, Vector};
+
+use lyon::path::builder::{Build, FlatPathBuilder, PathBuilder, SvgBuilder};
+
+#[derive(Debug, Clone)]
+pub struct Path {
+ raw: lyon::path::Path,
+}
+
+impl Path {
+ pub fn new(f: impl FnOnce(&mut Builder)) -> Self {
+ let mut builder = Builder::new();
+
+ // TODO: Make it pure instead of side-effect-based (?)
+ f(&mut builder);
+
+ builder.build()
+ }
+
+ #[inline]
+ pub(crate) fn raw(&self) -> &lyon::path::Path {
+ &self.raw
+ }
+
+ #[inline]
+ pub(crate) fn transformed(
+ &self,
+ transform: &lyon::math::Transform,
+ ) -> Path {
+ Path {
+ raw: self.raw.transformed(transform),
+ }
+ }
+}
+
+#[allow(missing_debug_implementations)]
+pub struct Builder {
+ raw: lyon::path::builder::SvgPathBuilder<lyon::path::Builder>,
+}
+
+impl Builder {
+ pub fn new() -> Builder {
+ Builder {
+ raw: lyon::path::Path::builder().with_svg(),
+ }
+ }
+
+ #[inline]
+ pub fn move_to(&mut self, point: Point) {
+ let _ = self.raw.move_to(lyon::math::Point::new(point.x, point.y));
+ }
+
+ #[inline]
+ pub fn line_to(&mut self, point: Point) {
+ let _ = self.raw.line_to(lyon::math::Point::new(point.x, point.y));
+ }
+
+ #[inline]
+ pub fn arc(&mut self, arc: Arc) {
+ self.ellipse(arc.into());
+ }
+
+ pub fn arc_to(&mut self, a: Point, b: Point, radius: f32) {
+ use lyon::{math, path};
+
+ let a = math::Point::new(a.x, a.y);
+
+ if self.raw.current_position() != a {
+ let _ = self.raw.line_to(a);
+ }
+
+ let _ = self.raw.arc_to(
+ math::Vector::new(radius, radius),
+ math::Angle::radians(0.0),
+ path::ArcFlags::default(),
+ math::Point::new(b.x, b.y),
+ );
+ }
+
+ pub fn ellipse(&mut self, ellipse: Ellipse) {
+ use lyon::{geom, math};
+
+ let arc = geom::Arc {
+ center: math::Point::new(ellipse.center.x, ellipse.center.y),
+ radii: math::Vector::new(ellipse.radii.x, ellipse.radii.y),
+ x_rotation: math::Angle::radians(ellipse.rotation),
+ start_angle: math::Angle::radians(ellipse.start_angle),
+ sweep_angle: math::Angle::radians(ellipse.end_angle),
+ };
+
+ let _ = self.raw.move_to(arc.sample(0.0));
+
+ arc.for_each_quadratic_bezier(&mut |curve| {
+ let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to);
+ });
+ }
+
+ #[inline]
+ pub fn bezier_curve_to(
+ &mut self,
+ control_a: Point,
+ control_b: Point,
+ to: Point,
+ ) {
+ use lyon::math;
+
+ let _ = self.raw.cubic_bezier_to(
+ math::Point::new(control_a.x, control_a.y),
+ math::Point::new(control_b.x, control_b.y),
+ math::Point::new(to.x, to.y),
+ );
+ }
+
+ #[inline]
+ pub fn quadratic_curve_to(&mut self, control: Point, to: Point) {
+ use lyon::math;
+
+ let _ = self.raw.quadratic_bezier_to(
+ math::Point::new(control.x, control.y),
+ math::Point::new(to.x, to.y),
+ );
+ }
+
+ #[inline]
+ pub fn rectangle(&mut self, p: Point, size: Size) {
+ self.move_to(p);
+ self.line_to(Point::new(p.x + size.width, p.y));
+ self.line_to(Point::new(p.x + size.width, p.y + size.height));
+ self.line_to(Point::new(p.x, p.y + size.height));
+ self.close();
+ }
+
+ #[inline]
+ pub fn circle(&mut self, center: Point, radius: f32) {
+ self.arc(Arc {
+ center,
+ radius,
+ start_angle: 0.0,
+ end_angle: 2.0 * std::f32::consts::PI,
+ });
+ }
+
+ #[inline]
+ pub fn close(&mut self) {
+ self.raw.close()
+ }
+
+ #[inline]
+ pub fn build(self) -> Path {
+ Path {
+ raw: self.raw.build(),
+ }
+ }
+}
+
+#[derive(Debug, Clone, Copy)]
+pub struct Arc {
+ pub center: Point,
+ pub radius: f32,
+ pub start_angle: f32,
+ pub end_angle: f32,
+}
+
+#[derive(Debug, Clone, Copy)]
+pub struct Ellipse {
+ pub center: Point,
+ pub radii: Vector,
+ pub rotation: f32,
+ pub start_angle: f32,
+ pub end_angle: f32,
+}
+
+impl From<Arc> for Ellipse {
+ fn from(arc: Arc) -> Ellipse {
+ Ellipse {
+ center: arc.center,
+ radii: Vector::new(arc.radius, arc.radius),
+ rotation: 0.0,
+ start_angle: arc.start_angle,
+ end_angle: arc.end_angle,
+ }
+ }
+}
diff --git a/wgpu/src/widget/canvas/stroke.rs b/wgpu/src/widget/canvas/stroke.rs
new file mode 100644
index 00000000..9bb260b2
--- /dev/null
+++ b/wgpu/src/widget/canvas/stroke.rs
@@ -0,0 +1,66 @@
+use iced_native::Color;
+
+#[derive(Debug, Clone, Copy)]
+pub struct Stroke {
+ pub color: Color,
+ pub width: f32,
+ pub line_cap: LineCap,
+ pub line_join: LineJoin,
+}
+
+impl Default for Stroke {
+ fn default() -> Stroke {
+ Stroke {
+ color: Color::BLACK,
+ width: 1.0,
+ line_cap: LineCap::default(),
+ line_join: LineJoin::default(),
+ }
+ }
+}
+
+#[derive(Debug, Clone, Copy)]
+pub enum LineCap {
+ Butt,
+ Square,
+ Round,
+}
+
+impl Default for LineCap {
+ fn default() -> LineCap {
+ LineCap::Butt
+ }
+}
+
+impl From<LineCap> for lyon::tessellation::LineCap {
+ fn from(line_cap: LineCap) -> lyon::tessellation::LineCap {
+ match line_cap {
+ LineCap::Butt => lyon::tessellation::LineCap::Butt,
+ LineCap::Square => lyon::tessellation::LineCap::Square,
+ LineCap::Round => lyon::tessellation::LineCap::Round,
+ }
+ }
+}
+
+#[derive(Debug, Clone, Copy)]
+pub enum LineJoin {
+ Miter,
+ Round,
+ Bevel,
+}
+
+impl Default for LineJoin {
+ fn default() -> LineJoin {
+ LineJoin::Miter
+ }
+}
+
+impl From<LineJoin> for lyon::tessellation::LineJoin {
+ fn from(line_join: LineJoin) -> lyon::tessellation::LineJoin {
+ match line_join {
+ LineJoin::Miter => lyon::tessellation::LineJoin::Miter,
+ LineJoin::Round => lyon::tessellation::LineJoin::Round,
+ LineJoin::Bevel => lyon::tessellation::LineJoin::Bevel,
+ }
+ }
+}