[][src]Trait iced::Application

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

An interactive cross-platform application.

This trait is the main entrypoint of Iced. Once implemented, you can run your GUI application by simply calling run.

An Application can execute asynchronous actions by returning a Command in some of its methods. If you do not intend to perform any background work in your program, the Sandbox trait offers a simplified interface.

When using an Application with the debug feature enabled, a debug view can be toggled by pressing F12.

Examples

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

A simple "Hello, world!"

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

use iced::{executor, Application, Command, Element, Settings, Text};

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

struct Hello;

impl Application for Hello {
    type Executor = executor::Null;
    type Message = ();
    type Flags = ();

    fn new(_flags: ()) -> (Hello, Command<Self::Message>) {
        (Hello, Command::none())
    }

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

    fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
        Command::none()
    }

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

Associated Types

type Executor: Executor

The Executor that will run commands and subscriptions.

The default executor can be a good starting point!

type Message: Debug + Send

The type of messages your Application will produce.

type Flags

The data needed to initialize your Application.

Loading content...

Required methods

fn new(flags: Self::Flags) -> (Self, Command<Self::Message>)

Initializes the Application with the flags provided to run as part of the Settings.

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

Additionally, you can return a Command if you need to perform some async action in the background on startup. This is useful if you want to load state from a file, perform an initial HTTP request, etc.

fn title(&self) -> String

Returns the current title of the Application.

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

fn update(&mut self, message: Self::Message) -> Command<Self::Message>

Handles a message and updates the state of the Application.

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

Any Command returned will be executed immediately in the background.

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

Returns the widgets to display in the Application.

These widgets can produce messages based on user interaction.

Loading content...

Provided methods

fn subscription(&self) -> Subscription<Self::Message>

Returns the event Subscription for the current state of the application.

A Subscription will be kept alive as long as you keep returning it, and the messages produced will be handled by update.

By default, this method returns an empty Subscription.

fn mode(&self) -> Mode

Returns the current Application mode.

The runtime will automatically transition your application if a new mode is returned.

Currently, the mode only has an effect in native platforms.

By default, an application will run in windowed mode.

fn background_color(&self) -> Color

Returns the background color of the Application.

By default, it returns Color::WHITE.

fn scale_factor(&self) -> f64

Returns the scale factor of the Application.

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<Self::Flags>) where
    Self: 'static, 

Runs the Application.

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

impl<T> Application for T where
    T: Sandbox
[src]

type Executor = Null

type Flags = ()

type Message = T::Message

Loading content...