Skip to content

Latest commit

 

History

History
59 lines (45 loc) · 1.65 KB

File metadata and controls

59 lines (45 loc) · 1.65 KB

Application Lifecycle

A standard Iced application lifecycle is very similar to how Elm does it.

Here's a break down:

  • The view renders widgets with the current state
  • The user interacts with these widgets (e.g. a button)
  • The button dispatches a message
  • The message is processed by update and mutates the state
  • Repeat...

In practical terms, considering our application below:

  • The view is rendering a text and a button in a column layout
  • The user clicks the button, which listens for an event (on_press)
  • The button dispatches the message (Message::ButtonPressed)
  • The message is matched on the update function, and the counter is incremented by 1
  • Repeat...
use iced::widget::{button, column, text};

fn main() -> iced::Result {
    iced::run("My App", MyApp::update, MyApp::view)
}

#[derive(Debug, Clone)]
enum Message {
    ButtonPressed,
}

#[derive(Default)]
struct MyApp {
    counter: usize,
}

impl MyApp {
    fn update(&mut self, message: Message) {
        match message {
            Message::ButtonPressed => self.counter += 1,
        }
    }

    fn view(&self) -> iced::Element<Message> {
        column![
            text(self.counter),
            button("Increase").on_press(Message::ButtonPressed),
        ]
        .into()
    }
}

Producing and receiving messages

➡️ Next: Adding Widgets

📘 Back: Table of contents