Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace stateful widgets with the new iced_pure API #1393

Merged
merged 20 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 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,10 +55,10 @@ members = [
"glutin",
"lazy",
"native",
"pure",
"style",
"wgpu",
"winit",
"examples/arc",
"examples/bezier_tool",
"examples/clock",
"examples/color_palette",
Expand Down Expand Up @@ -90,16 +88,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 +98,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
39 changes: 14 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,9 @@ that can be incremented and decremented using two buttons.
We start by modelling the __state__ of our application:

```rust
use iced::button;

struct Counter {
// The counter value
value: i32,

// The local state of the two buttons
increment_button: button::State,
decrement_button: button::State,
}
```

Expand All @@ -125,28 +119,23 @@ Now, let's show the actual counter by putting it all together in our
__view logic__:

```rust
use iced::{Button, Column, Text};
use iced::widget::{button, column, text, Column};

impl Counter {
pub fn view(&mut self) -> Column<Message> {
pub fn view(&self) -> Column<Message> {
// We use a column: a simple vertical layout
Column::new()
.push(
// The increment button. We tell it to produce an
// `IncrementPressed` message when pressed
Button::new(&mut self.increment_button, Text::new("+"))
.on_press(Message::IncrementPressed),
)
.push(
// We show the value of the counter here
Text::new(self.value.to_string()).size(50),
)
.push(
// The decrement button. We tell it to produce a
// `DecrementPressed` message when pressed
Button::new(&mut self.decrement_button, Text::new("-"))
.on_press(Message::DecrementPressed),
)
column![
// The increment button. We tell it to produce an
// `IncrementPressed` message when pressed
button("+").on_press(Message::IncrementPressed),

// We show the value of the counter here
text(self.value).size(50),

// The decrement button. We tell it to produce a
// `DecrementPressed` message when pressed
button("-").on_press(Message::DecrementPressed),
]
}
}
```
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.
8 changes: 5 additions & 3 deletions examples/pure/arc/src/main.rs → examples/arc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{f32::consts::PI, time::Instant};

use iced::executor;
use iced::pure::widget::canvas::{
use iced::widget::canvas::{
self, Cache, Canvas, Cursor, Geometry, Path, Stroke,
};
use iced::pure::{Application, Element};
use iced::{Command, Length, Point, Rectangle, Settings, Subscription, Theme};
use iced::{
Application, Command, Element, Length, Point, Rectangle, Settings,
Subscription, Theme,
};

pub fn main() -> iced::Result {
Arc::run(Settings {
Expand Down
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
```
Loading