Skip to content

Commit

Permalink
Replace stateful widgets with new iced_pure API
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 27, 2022
1 parent c44267b commit ff2519b
Show file tree
Hide file tree
Showing 142 changed files with 3,585 additions and 14,448 deletions.
14 changes: 0 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ async-std = ["iced_futures/async-std"]
smol = ["iced_futures/smol"]
# Enables advanced color conversion via `palette`
palette = ["iced_core/palette"]
# Enables pure, virtual widgets in the `pure` module
pure = ["iced_pure", "iced_graphics/pure"]
# Enables querying system information
system = ["iced_winit/system"]

Expand All @@ -57,7 +55,6 @@ members = [
"glutin",
"lazy",
"native",
"pure",
"style",
"wgpu",
"winit",
Expand Down Expand Up @@ -90,16 +87,6 @@ members = [
"examples/tour",
"examples/url_handler",
"examples/websocket",
"examples/pure/arc",
"examples/pure/color_palette",
"examples/pure/component",
"examples/pure/counter",
"examples/pure/game_of_life",
"examples/pure/pane_grid",
"examples/pure/pick_list",
"examples/pure/todos",
"examples/pure/tooltip",
"examples/pure/tour",
]

[dependencies]
Expand All @@ -110,7 +97,6 @@ iced_graphics = { version = "0.3", path = "graphics" }
iced_winit = { version = "0.4", path = "winit" }
iced_glutin = { version = "0.3", path = "glutin", optional = true }
iced_glow = { version = "0.3", path = "glow", optional = true }
iced_pure = { version = "0.2", path = "pure", optional = true }
thiserror = "1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/pure/arc/Cargo.toml → examples/arc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
publish = false

[dependencies]
iced = { path = "../../..", features = ["pure", "canvas", "tokio", "debug"] }
iced = { path = "../../..", features = ["canvas", "tokio", "debug"] }
File renamed without changes.
File renamed without changes.
69 changes: 31 additions & 38 deletions examples/bezier_tool/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)]
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -122,24 +113,24 @@ 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,
});

None
}
Some(Pending::Two { from, to }) => {
self.state.pending = None;
*state = None;

Some(Curve {
from,
Expand All @@ -160,6 +151,7 @@ mod bezier {

fn draw(
&self,
state: &Self::State,
_theme: &Theme,
bounds: Rectangle,
cursor: Cursor,
Expand All @@ -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]
Expand All @@ -185,6 +177,7 @@ mod bezier {

fn mouse_interaction(
&self,
_state: &Self::State,
bounds: Rectangle,
cursor: Cursor,
) -> mouse::Interaction {
Expand Down
20 changes: 12 additions & 8 deletions examples/clock/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use iced::canvas::{
self, Cache, Canvas, Cursor, Geometry, LineCap, Path, Stroke,
};
use iced::executor;
use iced::widget::canvas::{Cache, Cursor, Geometry, LineCap, Path, Stroke};
use iced::widget::{canvas, container};
use iced::{
Application, Color, Command, Container, Element, Length, Point, Rectangle,
Settings, Subscription, Theme, Vector,
Application, Color, Command, Element, Length, Point, Rectangle, Settings,
Subscription, Theme, Vector,
};

pub fn main() -> iced::Result {
Expand Down Expand Up @@ -69,10 +68,12 @@ impl Application for Clock {
})
}

fn view(&mut self) -> Element<Message> {
let canvas = Canvas::new(self).width(Length::Fill).height(Length::Fill);
fn view(&self) -> Element<Message> {
let canvas = canvas(self as &Self)
.width(Length::Fill)
.height(Length::Fill);

Container::new(canvas)
container(canvas)
.width(Length::Fill)
.height(Length::Fill)
.padding(20)
Expand All @@ -81,8 +82,11 @@ impl Application for Clock {
}

impl<Message> canvas::Program<Message> for Clock {
type State = ();

fn draw(
&self,
_state: &Self::State,
_theme: &Theme,
bounds: Rectangle,
_cursor: Cursor,
Expand Down
2 changes: 1 addition & 1 deletion examples/color_palette/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ A color palette generator, based on a user-defined root color.
You can run it with `cargo run`:

```
cargo run --package color_palette
cargo run --package pure_color_palette
```
79 changes: 38 additions & 41 deletions examples/color_palette/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use iced::canvas::{self, Cursor, Frame, Geometry, Path};
use iced::widget::canvas::{self, Canvas, Cursor, Frame, Geometry, Path};
use iced::widget::{column, row, text, Slider};
use iced::{
alignment, slider, Alignment, Canvas, Color, Column, Element, Length,
Point, Rectangle, Row, Sandbox, Settings, Size, Slider, Text, Vector,
alignment, Alignment, Color, Element, Length, Point, Rectangle, Sandbox,
Settings, Size, Vector,
};
use palette::{self, convert::FromColor, Hsl, Srgb};
use std::marker::PhantomData;
Expand Down Expand Up @@ -59,7 +60,7 @@ impl Sandbox for ColorPalette {
self.theme = Theme::new(srgb);
}

fn view(&mut self) -> Element<Message> {
fn view(&self) -> Element<Message> {
let base = self.theme.base;

let srgb = palette::Srgb::from(base);
Expand All @@ -69,17 +70,18 @@ impl Sandbox for ColorPalette {
let lab = palette::Lab::from_color(srgb);
let lch = palette::Lch::from_color(srgb);

Column::new()
.padding(10)
.spacing(10)
.push(self.rgb.view(base).map(Message::RgbColorChanged))
.push(self.hsl.view(hsl).map(Message::HslColorChanged))
.push(self.hsv.view(hsv).map(Message::HsvColorChanged))
.push(self.hwb.view(hwb).map(Message::HwbColorChanged))
.push(self.lab.view(lab).map(Message::LabColorChanged))
.push(self.lch.view(lch).map(Message::LchColorChanged))
.push(self.theme.view())
.into()
column![
self.rgb.view(base).map(Message::RgbColorChanged),
self.hsl.view(hsl).map(Message::HslColorChanged),
self.hsv.view(hsv).map(Message::HsvColorChanged),
self.hwb.view(hwb).map(Message::HwbColorChanged),
self.lab.view(lab).map(Message::LabColorChanged),
self.lch.view(lch).map(Message::LchColorChanged),
self.theme.view(),
]
.padding(10)
.spacing(10)
.into()
}
}

Expand Down Expand Up @@ -139,7 +141,7 @@ impl Theme {
.chain(self.higher.iter())
}

pub fn view(&mut self) -> Element<Message> {
pub fn view(&self) -> Element<Message> {
Canvas::new(self)
.width(Length::Fill)
.height(Length::Fill)
Expand Down Expand Up @@ -236,8 +238,11 @@ impl Theme {
}

impl<Message> canvas::Program<Message> for Theme {
type State = ();

fn draw(
&self,
_state: &Self::State,
_theme: &iced::Theme,
bounds: Rectangle,
_cursor: Cursor,
Expand Down Expand Up @@ -267,7 +272,6 @@ fn color_hex_string(color: &Color) -> String {

#[derive(Default)]
struct ColorPicker<C: ColorSpace> {
sliders: [slider::State; 3],
color_space: PhantomData<C>,
}

Expand All @@ -282,37 +286,30 @@ trait ColorSpace: Sized {
fn to_string(&self) -> String;
}

impl<C: 'static + ColorSpace + Copy> ColorPicker<C> {
fn view(&mut self, color: C) -> Element<C> {
impl<C: ColorSpace + Copy> ColorPicker<C> {
fn view(&self, color: C) -> Element<C> {
let [c1, c2, c3] = color.components();
let [s1, s2, s3] = &mut self.sliders;
let [cr1, cr2, cr3] = C::COMPONENT_RANGES;

fn slider<C: Clone>(
state: &mut slider::State,
fn slider<'a, C: Clone>(
range: RangeInclusive<f64>,
component: f32,
update: impl Fn(f32) -> C + 'static,
) -> Slider<f64, C, iced::Renderer> {
Slider::new(state, range, f64::from(component), move |v| {
update(v as f32)
})
.step(0.01)
update: impl Fn(f32) -> C + 'a,
) -> Slider<'a, f64, C, iced::Renderer> {
Slider::new(range, f64::from(component), move |v| update(v as f32))
.step(0.01)
}

Row::new()
.spacing(10)
.align_items(Alignment::Center)
.push(Text::new(C::LABEL).width(Length::Units(50)))
.push(slider(s1, cr1, c1, move |v| C::new(v, c2, c3)))
.push(slider(s2, cr2, c2, move |v| C::new(c1, v, c3)))
.push(slider(s3, cr3, c3, move |v| C::new(c1, c2, v)))
.push(
Text::new(color.to_string())
.width(Length::Units(185))
.size(14),
)
.into()
row![
text(C::LABEL).width(Length::Units(50)),
slider(cr1, c1, move |v| C::new(v, c2, c3)),
slider(cr2, c2, move |v| C::new(c1, v, c3)),
slider(cr3, c3, move |v| C::new(c1, c2, v)),
text(color.to_string()).width(Length::Units(185)).size(14),
]
.spacing(10)
.align_items(Alignment::Center)
.into()
}
}

Expand Down
Loading

0 comments on commit ff2519b

Please sign in to comment.