Passing initial values to application state

I’m currently creating a small application which is creating a window from a function which takes a few arguments. It is currently using the following code:

        let _ = iced::application(
                "title",
                ApplicationState::update,
                ApplicationState::view,
        )
        .centered()
        .theme(ApplicationState::theme);

I want to pass multiple arguments from the function to the application state, which has the needed fields, but I can’t find a way to pass the needed argument during construction.

I already tried to implement the Application trait to the application state (similar to how it was done here), but the compiler can’t find a trait called Application; it tells me the Application I imported using use iced::Application; was a struct, which is correct, but I can’t find a trait with this name in the Iced crate.

Can anyone tell me, how I can solve this issue?

I’m guessing this is 0.13. You can use Application::run_with and pass e.g. move || State::new(arg1, arg2) where that method is defined like

impl State {
  fn new(arg1: String, arg2: PathBuf) -> (Self, Task<Message>) {
    (
      Self { .. },
      Task::none()
    )
  }
}
1 Like

Thank you, this is working for me… And it’s much easier than expected.

Is this a new feature in the crate, which replaced the previously mentioned manual implimentation of the Application trait?

Kinda, they were introduced with 0.13 (for reference, we’re kinda close to getting a 0.14 release). Application and Sandbox (the traits) were replaced with iced::application and iced::run, respectively. I’m guessing there were multiple reasons for the change, imo it’s much simpler and saner.

1 Like