Sometimes you may want to dispatch a task when your application starts up. This is useful when you need to fetch some data to display on the screen, or to perform some initialization logic.
To accomplish that, you can't just run the application with iced::run, because this function doesn't allow you to customize the initialization logic of your application. Instead, you should use iced::application. This function is called the same way as iced::run. But instead of running the application straightaway, it will return an Application instance.
With an instance of Application, you can customize styles, and use the run_with function to dispatch a task.
The run_with function expects a function that returns a tuple with an initial state, and a task.
To keep things aligned with the previous example, in the example below, we dispatch a task that focuses the input field. Of course, tasks allow us to perform many more complex operations.
use iced::{
Task, Theme,
widget::{column, text_input},
};
fn main() -> iced::Result {
iced::application("My App", MyApp::update, MyApp::view)
.theme(|_| Theme::TokyoNight)
.run_with(MyApp::new)
}
const MY_TEXT_ID: &str = "my_text";
#[derive(Debug, Clone)]
enum Message {
UpdateText(String),
}
#[derive(Default)]
struct MyApp {
some_text: String,
}
impl MyApp {
fn new() -> (Self, Task<Message>) {
let app = Self::default();
(app, text_input::focus(text_input::Id::new(MY_TEXT_ID)))
// Try returning (app, Task::none()) instead and see what happens
// (app, Task::none())
}
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::UpdateText(s) => self.some_text = s,
}
Task::none()
}
fn view(&self) -> iced::Element<Message> {
column![
text_input("", &self.some_text)
.id(text_input::Id::new(MY_TEXT_ID))
.on_input(Message::UpdateText),
]
.into()
}
}➡️ Next: Batch Tasks
📘 Back: Table of contents
