[−][src]Trait iced::Application
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
.
- On native platforms, it will run in its own window.
- On the web, it will take control of the
<title>
and the<body>
of the document.
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.
Example
Let's say we want to run the Counter
example we implemented
before. We just need to fill in the gaps:
use iced::{button, Application, Button, Column, Command, Element, 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 Application for Counter { type Message = Message; fn new() -> (Self, Command<Message>) { (Self::default(), Command::none()) } fn title(&self) -> String { String::from("A simple counter") } fn update(&mut self, message: Message) -> Command<Message> { match message { Message::IncrementPressed => { self.value += 1; } Message::DecrementPressed => { self.value -= 1; } } Command::none() } 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 Application
will produce.
Required methods
fn new() -> (Self, Command<Self::Message>)
Initializes the Application
.
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.
Provided methods
fn run(settings: Settings) where
Self: 'static,
Self: 'static,
Runs the Application
.
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.