[][src]Trait iced::Sandbox

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

A sandboxed Application.

If you are a just getting started with the library, this trait offers a simpler interface than Application.

Unlike an Application, a Sandbox cannot run any asynchronous actions or be initialized with some external flags. However, both traits are very similar and upgrading from a Sandbox is very straightforward.

Therefore, it is recommended to always start by implementing this trait and upgrade only once necessary.

Examples

The repository has a bunch of examples that use the Sandbox trait:

A simple "Hello, world!"

If you just want to get started, here is a simple Sandbox that says "Hello, world!":

use iced::{Element, Sandbox, Settings, Text};

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

struct Hello;

impl Sandbox for Hello {
    type Message = ();

    fn new() -> Hello {
        Hello
    }

    fn title(&self) -> String {
        String::from("A cool application")
    }

    fn update(&mut self, _message: Self::Message) {
        // This application has no interactions
    }

    fn view(&mut self) -> Element<Self::Message> {
        Text::new("Hello, world!").into()
    }
}

Associated Types

type Message: Debug + Send

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 background_color(&self) -> Color

Returns the background color of the Sandbox.

By default, it returns Color::WHITE.

fn scale_factor(&self) -> f64

Returns the scale factor of the Sandbox.

It can be used to dynamically control the size of the UI at runtime (i.e. zooming).

For instance, a scale factor of 2.0 will make widgets twice as big, while a scale factor of 0.5 will shrink them to half their size.

By default, it returns 1.0.

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

Runs the Sandbox.

On native platforms, 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...