[][src]Trait iced::Sandbox

pub trait Sandbox {
    type Message: Debug + Send + Clone;
    fn new() -> Self;
fn title(&self) -> String;
fn update(&mut self, message: Self::Message);
fn view(&mut self) -> Element<Self::Message>; fn run(settings: Settings)
    where
        Self: 'static + Sized
, { ... } }

A sandboxed Application.

A Sandbox is just an Application that cannot run any asynchronous actions.

If you do not need to leverage a Command, you can use a Sandbox instead of returning a Command::none everywhere.

Example

We can use a Sandbox to run the Counter example we implemented before, instead of an Application. We just need to remove the use of Command:

use iced::{button, Button, Column, Element, Sandbox, Settings, Text};

pub fn main() {
    Counter::run(Settings::default())
}

#[derive(Default)]
struct Counter {
    value: i32,
    increment_button: button::State,
    decrement_button: button::State,
}

#[derive(Debug, Clone, Copy)]
enum Message {
    IncrementPressed,
    DecrementPressed,
}

impl Sandbox for Counter {
    type Message = Message;

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

    fn title(&self) -> String {
        String::from("A simple counter")
    }

    fn update(&mut self, message: Message) {
        match message {
            Message::IncrementPressed => {
                self.value += 1;
            }
            Message::DecrementPressed => {
                self.value -= 1;
            }
        }
    }

    fn view(&mut self) -> Element<Message> {
        Column::new()
            .push(
                Button::new(&mut self.increment_button, Text::new("Increment"))
                    .on_press(Message::IncrementPressed),
            )
            .push(
                Text::new(self.value.to_string()).size(50),
            )
            .push(
                Button::new(&mut self.decrement_button, Text::new("Decrement"))
                    .on_press(Message::DecrementPressed),
            )
            .into()
    }
}

Associated Types

type Message: Debug + Send + Clone

The type of messages your Sandbox will produce.

Loading content...

Required methods

fn new() -> Self

Initializes the Sandbox.

Here is where you should return the initial state of your app.

fn title(&self) -> String

Returns the current title of the Sandbox.

This title can be dynamic! The runtime will automatically update the title of your application when necessary.

fn update(&mut self, message: Self::Message)

Handles a message and updates the state of the Sandbox.

This is where you define your update logic. All the messages, produced by user interactions, will be handled by this method.

fn view(&mut self) -> Element<Self::Message>

Returns the widgets to display in the Sandbox.

These widgets can produce messages based on user interaction.

Loading content...

Provided methods

fn run(settings: Settings) where
    Self: 'static + Sized

Runs the Sandbox.

This method will take control of the current thread and will NOT return.

It should probably be that last thing you call in your main function.

Loading content...

Implementors

Loading content...