pub trait Application: Sized
where Self::Theme: DefaultStyle,
{ type Executor: Executor; type Message: Debug + Send; type Theme: Default; type Flags; // Required methods fn new(flags: Self::Flags) -> (Self, Command<Self::Message>); fn title(&self, window: Id) -> String; fn update(&mut self, message: Self::Message) -> Command<Self::Message>; fn view( &self, window: Id ) -> Element<'_, Self::Message, Self::Theme, Renderer>; // Provided methods fn theme(&self, window: Id) -> Self::Theme { ... } fn style(&self, theme: &Self::Theme) -> Appearance { ... } fn subscription(&self) -> Subscription<Self::Message> { ... } fn scale_factor(&self, window: Id) -> f64 { ... } fn run(settings: Settings<Self::Flags>) -> Result where Self: 'static { ... } }
Available on crate feature multi-window only.
Expand description

An interactive cross-platform multi-window application.

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

  • On native platforms, it will run in its own windows.
  • On the web, it will take control of the <title> and the <body> of the document and display only the contents of the window::Id::MAIN window.

An Application can execute asynchronous actions by returning a Command in some of its methods.

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

Examples

See the examples/multi-window example to see this multi-window Application trait in action.

A simple “Hello, world!”

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

use iced::{executor, window};
use iced::{Command, Element, Settings, Theme};
use iced::multi_window::{self, Application};

pub fn main() -> iced::Result {
    Hello::run(Settings::default())
}

struct Hello;

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

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

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

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

    fn view(&self, _window: window::Id) -> Element<Self::Message> {
        "Hello, world!".into()
    }
}

Required Associated Types§

source

type Executor: Executor

The Executor that will run commands and subscriptions.

The default executor can be a good starting point!

source

type Message: Debug + Send

The type of messages your Application will produce.

source

type Theme: Default

The theme of your Application.

source

type Flags

The data needed to initialize your Application.

Required Methods§

source

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.

source

fn title(&self, window: Id) -> String

Returns the current title of the window of the Application.

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

source

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.

source

fn view(&self, window: Id) -> Element<'_, Self::Message, Self::Theme, Renderer>

Returns the widgets to display in the window of the Application.

These widgets can produce messages based on user interaction.

Provided Methods§

source

fn theme(&self, window: Id) -> Self::Theme

Returns the current Theme of the window of the Application.

source

fn style(&self, theme: &Self::Theme) -> Appearance

Returns the current Style of the Theme.

source

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.

source

fn scale_factor(&self, window: Id) -> f64

Returns the scale factor of the window 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.

source

fn run(settings: Settings<Self::Flags>) -> Result
where Self: 'static,

Runs the multi-window Application.

On native platforms, this method will take control of the current thread until the Application exits.

On the web platform, this method will NOT return unless there is an Error during startup.

Object Safety§

This trait is not object safe.

Implementors§