-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Program
API
#2331
Program
API
#2331
Conversation
? Where can I find the migration guide? |
For those who needs a quick migration guide for this API update, I'll briefly introduce how to turn your code to fit into 0.13. It's quite straightforward (than I thought). If I'm missing something or saying something wrong, please let me know. I hope that it will help other people who are interested in migration!
-impl Application for App {
- type Executor = iced::executor::Default;
- type Flags = ();
- type Message = Message;
- type Theme = Theme;
-
- fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
+impl App {
+ fn new(_flags: ()) -> (Self, Task<Message>) {
fn main() -> iced::Result {
- App::run(Settings::default())
+ iced::application(App::title, App::update, App::view)
+ .run_with(|| App::new(()))
}
fn main() -> iced::Result {
iced::application(App::title, App::update, App::view)
+ .subscription(App::subscription)
+ .theme(App::Theme)
.run_with(|| App::new(()))
}
fn main() -> iced::Result {
- App::run(Settings {
- default_font: FONT,
- ..Default::default()
- })
+ iced::application(App::title, App::update, App::view)
+ .subscription(App::subscription)
+ .theme(App::theme)
+ .default_font(FONT) // < it's me!
+ .run_with(|| App::new(()))
} Note that window options are also flattened into a nice method call. fn main() -> iced::Result {
- App::run(Settings {
- window: window::Settings {
- exit_on_close_request: false,
- ..window::Settings::default()
- },
- ..Default::default()
- })
+ iced::application(App::title, App::update, App::view)
+ .subscription(App::subscription)
+ .exit_on_close_request(false)
+ .run_with(|| App::new(()))
} It should be quite obvious from the doc for the Optional refactoring
fn main() -> iced::Result {
iced::application(App::title, App::update, App::view)
- .run_with(|| App::new(()))
+ .run()
}
-impl App {
- fn new(_flags: ()) -> (Self, Task<Message>) {
+impl Default for App {
+ fn default() -> Self {
fn main() -> iced::Result {
- iced::application(App::title, App::update, App::view)
+ iced::application("Awesome app", App::update, App::view)
.run()
}
impl App {
- fn title(&self) -> String {
- "Awesome app".into()
- }
fn main() -> iced::Result {
- iced::application("Awesome app", App::update, App::view)
- .run()
+ iced::run("Awesome app", App::update, App::view)
} |
This PR introduces a new
Program
API as a more convenient—although less powerful—alternative to theSandbox
andApplication
traits.The main motivation is lowering the learning curve and entry barrier of the library. Specifically,
Application
has a lot of moving parts (e.g.Flags
,Executor
,Theme
,subscription
, etc.)The
run
FunctionThere is a new
run
function in the root module that can be used to easily run basic applications, like the classic counter:The
Program
APIThe
Program
API can be used to create and run iced applications step by step—without coupling your logic to a trait or a specific type.For instance, here is the new
main
function of theclock
example: