summaryrefslogtreecommitdiffstats
path: root/examples/bezier_tool/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-07-27 06:49:20 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-07-27 06:49:20 +0200
commitff2519b1d43d481987351a83b6dd7237524c21f0 (patch)
tree5731eeb7eb1247d4a8951de0d5bc5d8102640559 /examples/bezier_tool/src
parentc44267b85f7aaa2997e3caf1323b837d95818c22 (diff)
downloadiced-ff2519b1d43d481987351a83b6dd7237524c21f0.tar.gz
iced-ff2519b1d43d481987351a83b6dd7237524c21f0.tar.bz2
iced-ff2519b1d43d481987351a83b6dd7237524c21f0.zip
Replace stateful widgets with new `iced_pure` API
Diffstat (limited to 'examples/bezier_tool/src')
-rw-r--r--examples/bezier_tool/src/main.rs69
1 files changed, 31 insertions, 38 deletions
diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs
index 11e4828e..7c3916d4 100644
--- a/examples/bezier_tool/src/main.rs
+++ b/examples/bezier_tool/src/main.rs
@@ -1,7 +1,6 @@
//! This example showcases an interactive `Canvas` for drawing Bézier curves.
-use iced::{
- button, Alignment, Button, Column, Element, Length, Sandbox, Settings, Text,
-};
+use iced::widget::{button, column, text};
+use iced::{Alignment, Element, Length, Sandbox, Settings};
pub fn main() -> iced::Result {
Example::run(Settings {
@@ -14,7 +13,6 @@ pub fn main() -> iced::Result {
struct Example {
bezier: bezier::State,
curves: Vec<bezier::Curve>,
- button_state: button::State,
}
#[derive(Debug, Clone, Copy)]
@@ -47,44 +45,34 @@ impl Sandbox for Example {
}
}
- fn view(&mut self) -> Element<Message> {
- Column::new()
- .padding(20)
- .spacing(20)
- .align_items(Alignment::Center)
- .push(
- Text::new("Bezier tool example")
- .width(Length::Shrink)
- .size(50),
- )
- .push(self.bezier.view(&self.curves).map(Message::AddCurve))
- .push(
- Button::new(&mut self.button_state, Text::new("Clear"))
- .padding(8)
- .on_press(Message::Clear),
- )
- .into()
+ fn view(&self) -> Element<Message> {
+ column![
+ text("Bezier tool example").width(Length::Shrink).size(50),
+ self.bezier.view(&self.curves).map(Message::AddCurve),
+ button("Clear").padding(8).on_press(Message::Clear),
+ ]
+ .padding(20)
+ .spacing(20)
+ .align_items(Alignment::Center)
+ .into()
}
}
mod bezier {
- use iced::{
- canvas::event::{self, Event},
- canvas::{self, Canvas, Cursor, Frame, Geometry, Path, Stroke},
- mouse, Element, Length, Point, Rectangle, Theme,
+ use iced::mouse;
+ use iced::widget::canvas::event::{self, Event};
+ use iced::widget::canvas::{
+ self, Canvas, Cursor, Frame, Geometry, Path, Stroke,
};
+ use iced::{Element, Length, Point, Rectangle, Theme};
#[derive(Default)]
pub struct State {
- pending: Option<Pending>,
cache: canvas::Cache,
}
impl State {
- pub fn view<'a>(
- &'a mut self,
- curves: &'a [Curve],
- ) -> Element<'a, Curve> {
+ pub fn view<'a>(&'a self, curves: &'a [Curve]) -> Element<'a, Curve> {
Canvas::new(Bezier {
state: self,
curves,
@@ -100,13 +88,16 @@ mod bezier {
}
struct Bezier<'a> {
- state: &'a mut State,
+ state: &'a State,
curves: &'a [Curve],
}
impl<'a> canvas::Program<Curve> for Bezier<'a> {
+ type State = Option<Pending>;
+
fn update(
- &mut self,
+ &self,
+ state: &mut Self::State,
event: Event,
bounds: Rectangle,
cursor: Cursor,
@@ -122,16 +113,16 @@ mod bezier {
Event::Mouse(mouse_event) => {
let message = match mouse_event {
mouse::Event::ButtonPressed(mouse::Button::Left) => {
- match self.state.pending {
+ match *state {
None => {
- self.state.pending = Some(Pending::One {
+ *state = Some(Pending::One {
from: cursor_position,
});
None
}
Some(Pending::One { from }) => {
- self.state.pending = Some(Pending::Two {
+ *state = Some(Pending::Two {
from,
to: cursor_position,
});
@@ -139,7 +130,7 @@ mod bezier {
None
}
Some(Pending::Two { from, to }) => {
- self.state.pending = None;
+ *state = None;
Some(Curve {
from,
@@ -160,6 +151,7 @@ mod bezier {
fn draw(
&self,
+ state: &Self::State,
_theme: &Theme,
bounds: Rectangle,
cursor: Cursor,
@@ -170,11 +162,11 @@ mod bezier {
frame.stroke(
&Path::rectangle(Point::ORIGIN, frame.size()),
- Stroke::default(),
+ Stroke::default().with_width(2.0),
);
});
- if let Some(pending) = &self.state.pending {
+ if let Some(pending) = state {
let pending_curve = pending.draw(bounds, cursor);
vec![content, pending_curve]
@@ -185,6 +177,7 @@ mod bezier {
fn mouse_interaction(
&self,
+ _state: &Self::State,
bounds: Rectangle,
cursor: Cursor,
) -> mouse::Interaction {