Remove load method from application and daemon

If you need to run a `Task` during boot, use
`run_with` instead!
This commit is contained in:
Héctor Ramón Jiménez 2024-07-09 00:28:40 +02:00
parent 3d99da805d
commit e86920be5b
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
10 changed files with 70 additions and 197 deletions

View file

@ -10,6 +10,6 @@ iced.workspace = true
iced.features = ["tokio"]
[dependencies.reqwest]
version = "0.11"
version = "0.12"
default-features = false
features = ["rustls-tls"]

View file

@ -13,12 +13,11 @@ use std::sync::Arc;
pub fn main() -> iced::Result {
iced::application("Editor - Iced", Editor::update, Editor::view)
.load(Editor::load)
.subscription(Editor::subscription)
.theme(Editor::theme)
.font(include_bytes!("../fonts/icons.ttf").as_slice())
.default_font(Font::MONOSPACE)
.run()
.run_with(Editor::new)
}
struct Editor {
@ -41,20 +40,22 @@ enum Message {
}
impl Editor {
fn new() -> Self {
Self {
file: None,
content: text_editor::Content::new(),
theme: highlighter::Theme::SolarizedDark,
is_loading: true,
is_dirty: false,
}
}
fn load() -> Task<Message> {
Task::perform(
load_file(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))),
Message::FileOpened,
fn new() -> (Self, Task<Message>) {
(
Self {
file: None,
content: text_editor::Content::new(),
theme: highlighter::Theme::SolarizedDark,
is_loading: true,
is_dirty: false,
},
Task::perform(
load_file(format!(
"{}/src/main.rs",
env!("CARGO_MANIFEST_DIR")
)),
Message::FileOpened,
),
)
}
@ -214,12 +215,6 @@ impl Editor {
}
}
impl Default for Editor {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub enum Error {
DialogClosed,

View file

@ -9,16 +9,12 @@ use std::collections::BTreeMap;
fn main() -> iced::Result {
iced::daemon(Example::title, Example::update, Example::view)
.load(|| {
window::open(window::Settings::default()).map(Message::WindowOpened)
})
.subscription(Example::subscription)
.theme(Example::theme)
.scale_factor(Example::scale_factor)
.run()
.run_with(Example::new)
}
#[derive(Default)]
struct Example {
windows: BTreeMap<window::Id, Window>,
}
@ -43,6 +39,16 @@ enum Message {
}
impl Example {
fn new() -> (Self, Task<Message>) {
(
Self {
windows: BTreeMap::new(),
},
window::open(window::Settings::default())
.map(Message::WindowOpened),
)
}
fn title(&self, window: window::Id) -> String {
self.windows
.get(&window)

View file

@ -16,7 +16,7 @@ version = "1.0"
features = ["derive"]
[dependencies.reqwest]
version = "0.11"
version = "0.12"
default-features = false
features = ["json", "rustls-tls"]

View file

@ -4,17 +4,13 @@ use iced::{Alignment, Element, Length, Task};
pub fn main() -> iced::Result {
iced::application(Pokedex::title, Pokedex::update, Pokedex::view)
.load(Pokedex::search)
.run()
.run_with(Pokedex::new)
}
#[derive(Debug, Default)]
#[derive(Debug)]
enum Pokedex {
#[default]
Loading,
Loaded {
pokemon: Pokemon,
},
Loaded { pokemon: Pokemon },
Errored,
}
@ -25,6 +21,10 @@ enum Message {
}
impl Pokedex {
fn new() -> (Self, Task<Message>) {
(Self::Loading, Self::search())
}
fn search() -> Task<Message> {
Task::perform(Pokemon::search(), Message::PokemonFound)
}

View file

@ -18,16 +18,14 @@ pub fn main() -> iced::Result {
tracing_subscriber::fmt::init();
iced::application(Todos::title, Todos::update, Todos::view)
.load(Todos::load)
.subscription(Todos::subscription)
.font(include_bytes!("../fonts/icons.ttf").as_slice())
.window_size((500.0, 800.0))
.run()
.run_with(Todos::new)
}
#[derive(Default, Debug)]
#[derive(Debug)]
enum Todos {
#[default]
Loading,
Loaded(State),
}
@ -54,8 +52,11 @@ enum Message {
}
impl Todos {
fn load() -> Command<Message> {
Command::perform(SavedState::load(), Message::Loaded)
fn new() -> (Self, Command<Message>) {
(
Self::Loading,
Command::perform(SavedState::load(), Message::Loaded),
)
}
fn title(&self) -> String {

View file

@ -9,12 +9,10 @@ use once_cell::sync::Lazy;
pub fn main() -> iced::Result {
iced::application("WebSocket - Iced", WebSocket::update, WebSocket::view)
.load(WebSocket::load)
.subscription(WebSocket::subscription)
.run()
.run_with(WebSocket::new)
}
#[derive(Default)]
struct WebSocket {
messages: Vec<echo::Message>,
new_message: String,
@ -30,11 +28,18 @@ enum Message {
}
impl WebSocket {
fn load() -> Task<Message> {
Task::batch([
Task::perform(echo::server::run(), |_| Message::Server),
widget::focus_next(),
])
fn new() -> (Self, Task<Message>) {
(
Self {
messages: Vec::new(),
new_message: String::new(),
state: State::Disconnected,
},
Task::batch([
Task::perform(echo::server::run(), |_| Message::Server),
widget::focus_next(),
]),
)
}
fn update(&mut self, message: Message) -> Task<Message> {
@ -140,10 +145,4 @@ enum State {
Connected(echo::Connection),
}
impl Default for State {
fn default() -> Self {
Self::Disconnected
}
}
static MESSAGE_LOG: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);