Skip to content

Commit

Permalink
Merge pull request #2331 from iced-rs/program-api
Browse files Browse the repository at this point in the history
`Program` API
  • Loading branch information
hecrj authored Mar 16, 2024
2 parents 0524e9b + cfc0383 commit 503a48e
Show file tree
Hide file tree
Showing 47 changed files with 1,300 additions and 894 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ that can be incremented and decremented using two buttons.
We start by modelling the __state__ of our application:

```rust
#[derive(Default)]
struct Counter {
// The counter value
value: i32,
}
```
Expand All @@ -110,8 +110,8 @@ the button presses. These interactions are our __messages__:
```rust
#[derive(Debug, Clone, Copy)]
pub enum Message {
IncrementPressed,
DecrementPressed,
Increment,
Decrement,
}
```

Expand All @@ -126,15 +126,15 @@ impl Counter {
// We use a column: a simple vertical layout
column![
// The increment button. We tell it to produce an
// `IncrementPressed` message when pressed
button("+").on_press(Message::IncrementPressed),
// `Increment` message when pressed
button("+").on_press(Message::Increment),

// 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),
// `Decrement` message when pressed
button("-").on_press(Message::Decrement),
]
}
}
Expand All @@ -160,8 +160,15 @@ impl Counter {
}
```

And that's everything! We just wrote a whole user interface. Iced is now able
to:
And that's everything! We just wrote a whole user interface. Let's run it:

```rust
fn main() -> iced::Result {
iced::run("A cool counter", Counter::update, Counter::view)
}
```

Iced will automatically:

1. Take the result of our __view logic__ and layout its widgets.
1. Process events from our system and produce __messages__ for our
Expand Down
24 changes: 24 additions & 0 deletions core/src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Sub, SubAssign};
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Degrees(pub f32);

impl PartialEq<f32> for Degrees {
fn eq(&self, other: &f32) -> bool {
self.0.eq(other)
}
}

impl PartialOrd<f32> for Degrees {
fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(other)
}
}

/// Radians
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Radians(pub f32);
Expand Down Expand Up @@ -140,3 +152,15 @@ impl Div for Radians {
Self(self.0 / rhs.0)
}
}

impl PartialEq<f32> for Radians {
fn eq(&self, other: &f32) -> bool {
self.0.eq(other)
}
}

impl PartialOrd<f32> for Radians {
fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(other)
}
}
53 changes: 17 additions & 36 deletions examples/arc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use std::{f32::consts::PI, time::Instant};

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

pub fn main() -> iced::Result {
Arc::run(Settings {
antialiasing: true,
..Settings::default()
})
iced::sandbox("Arc - Iced", Arc::update, Arc::view)
.subscription(Arc::subscription)
.theme(|_| Theme::Dark)
.antialiased()
.run()
}

struct Arc {
Expand All @@ -27,30 +24,9 @@ enum Message {
Tick,
}

impl Application for Arc {
type Executor = executor::Default;
type Message = Message;
type Theme = Theme;
type Flags = ();

fn new(_flags: ()) -> (Self, Command<Message>) {
(
Arc {
start: Instant::now(),
cache: Cache::default(),
},
Command::none(),
)
}

fn title(&self) -> String {
String::from("Arc - Iced")
}

fn update(&mut self, _: Message) -> Command<Message> {
impl Arc {
fn update(&mut self, _: Message) {
self.cache.clear();

Command::none()
}

fn view(&self) -> Element<Message> {
Expand All @@ -60,16 +36,21 @@ impl Application for Arc {
.into()
}

fn theme(&self) -> Theme {
Theme::Dark
}

fn subscription(&self) -> Subscription<Message> {
iced::time::every(std::time::Duration::from_millis(10))
.map(|_| Message::Tick)
}
}

impl Default for Arc {
fn default() -> Self {
Arc {
start: Instant::now(),
cache: Cache::default(),
}
}
}

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

Expand Down
21 changes: 5 additions & 16 deletions examples/bezier_tool/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! This example showcases an interactive `Canvas` for drawing Bézier curves.
use iced::widget::{button, column, text};
use iced::{Alignment, Element, Length, Sandbox, Settings};
use iced::{Alignment, Element, Length};

pub fn main() -> iced::Result {
Example::run(Settings {
antialiasing: true,
..Settings::default()
})
iced::sandbox("Bezier Tool - Iced", Example::update, Example::view)
.antialiased()
.run()
}

#[derive(Default)]
Expand All @@ -21,17 +20,7 @@ enum Message {
Clear,
}

impl Sandbox for Example {
type Message = Message;

fn new() -> Self {
Example::default()
}

fn title(&self) -> String {
String::from("Bezier tool - Iced")
}

impl Example {
fn update(&mut self, message: Message) {
match message {
Message::AddCurve(curve) => {
Expand Down
33 changes: 6 additions & 27 deletions examples/checkbox/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use iced::executor;
use iced::font::{self, Font};
use iced::widget::{checkbox, column, container, row, text};
use iced::{Application, Command, Element, Length, Settings, Theme};
use iced::{Element, Font, Length};

const ICON_FONT: Font = Font::with_name("icons");

pub fn main() -> iced::Result {
Example::run(Settings::default())
iced::sandbox("Checkbox - Iced", Example::update, Example::view)
.font(include_bytes!("../fonts/icons.ttf").as_slice())
.run()
}

#[derive(Default)]
Expand All @@ -21,28 +21,10 @@ enum Message {
DefaultToggled(bool),
CustomToggled(bool),
StyledToggled(bool),
FontLoaded(Result<(), font::Error>),
}

impl Application for Example {
type Message = Message;
type Flags = ();
type Executor = executor::Default;
type Theme = Theme;

fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
(
Self::default(),
font::load(include_bytes!("../fonts/icons.ttf").as_slice())
.map(Message::FontLoaded),
)
}

fn title(&self) -> String {
String::from("Checkbox - Iced")
}

fn update(&mut self, message: Message) -> Command<Message> {
impl Example {
fn update(&mut self, message: Message) {
match message {
Message::DefaultToggled(default) => {
self.default = default;
Expand All @@ -53,10 +35,7 @@ impl Application for Example {
Message::CustomToggled(custom) => {
self.custom = custom;
}
Message::FontLoaded(_) => (),
}

Command::none()
}

fn view(&self) -> Element<Message> {
Expand Down
Loading

0 comments on commit 503a48e

Please sign in to comment.