refactor: replace load method with reload, returning Command

This commit is contained in:
Michael Aaron Murphy 2024-01-16 17:04:55 +01:00 committed by Michael Murphy
parent c6bf1525d8
commit 3787bf8cbc
7 changed files with 25 additions and 38 deletions

View file

@ -589,7 +589,6 @@ impl SettingsApp {
let page_command = self let page_command = self
.pages .pages
.page_reload(page) .page_reload(page)
.unwrap_or(iced::Command::none())
.map(Message::PageMessage) .map(Message::PageMessage)
.map(cosmic::app::Message::App); .map(cosmic::app::Message::App);

View file

@ -6,7 +6,7 @@ use std::sync::Arc;
use apply::Apply; use apply::Apply;
use ashpd::desktop::file_chooser::{FileFilter, SelectedFiles}; use ashpd::desktop::file_chooser::{FileFilter, SelectedFiles};
use cosmic::cosmic_config::{Config, ConfigSet, CosmicConfigEntry}; use cosmic::cosmic_config::{Config, ConfigSet, CosmicConfigEntry};
use cosmic::cosmic_theme::palette::{self, FromColor, Hsv, Srgb, Srgba}; use cosmic::cosmic_theme::palette::{FromColor, Hsv, Srgb, Srgba};
use cosmic::cosmic_theme::{CornerRadii, Theme, ThemeBuilder, ThemeMode}; use cosmic::cosmic_theme::{CornerRadii, Theme, ThemeBuilder, ThemeMode};
use cosmic::iced::wayland::actions::window::SctkWindowSettings; use cosmic::iced::wayland::actions::window::SctkWindowSettings;
use cosmic::iced::widget::{column, row}; use cosmic::iced::widget::{column, row};
@ -19,7 +19,7 @@ use cosmic::widget::{
button, color_picker::ColorPickerUpdate, container, header_bar, horizontal_space, settings, button, color_picker::ColorPickerUpdate, container, header_bar, horizontal_space, settings,
spin_button, text, ColorPickerModel, spin_button, text, ColorPickerModel,
}; };
use cosmic::{Command, Element}; use cosmic::{command, Command, Element};
use cosmic_settings_desktop::wallpaper; use cosmic_settings_desktop::wallpaper;
use cosmic_settings_page::Section; use cosmic_settings_page::Section;
use cosmic_settings_page::{self as page, section}; use cosmic_settings_page::{self as page, section};
@ -936,10 +936,8 @@ impl page::Page<crate::pages::Message> for Page {
.description(fl!("appearance", "desc")) .description(fl!("appearance", "desc"))
} }
fn load(&self, _: page::Entity) -> Option<page::Task<crate::pages::Message>> { fn reload(&mut self, _: page::Entity) -> Command<crate::pages::Message> {
Some(Box::pin(async move { command::future(async move { crate::pages::Message::Appearance(Message::Entered) })
crate::pages::Message::Appearance(Message::Entered)
}))
} }
fn on_leave(&mut self) -> Command<crate::pages::Message> { fn on_leave(&mut self) -> Command<crate::pages::Message> {

View file

@ -13,6 +13,12 @@ use std::{
}; };
use apply::Apply; use apply::Apply;
use cosmic::widget::{
button, dropdown, list_column, row,
segmented_button::{self, SingleSelectModel},
segmented_selection, settings, text, toggler,
};
use cosmic::{command, Command};
use cosmic::{ use cosmic::{
iced::{wayland::actions::window::SctkWindowSettings, window, Color, Length}, iced::{wayland::actions::window::SctkWindowSettings, window, Color, Length},
prelude::CollectionWidget, prelude::CollectionWidget,
@ -26,14 +32,6 @@ use cosmic::{
iced_core::{alignment, layout}, iced_core::{alignment, layout},
iced_runtime::core::image::Handle as ImageHandle, iced_runtime::core::image::Handle as ImageHandle,
}; };
use cosmic::{
widget::{
button, dropdown, list_column, row,
segmented_button::{self, SingleSelectModel},
segmented_selection, settings, text, toggler,
},
Command,
};
use cosmic::{ use cosmic::{
widget::{color_picker::ColorPickerUpdate, ColorPickerModel}, widget::{color_picker::ColorPickerUpdate, ColorPickerModel},
Element, Element,
@ -250,10 +248,10 @@ impl page::Page<crate::pages::Message> for Page {
Command::none() Command::none()
} }
fn load(&self, _page: page::Entity) -> Option<page::Task<crate::pages::Message>> { fn reload(&mut self, _page: page::Entity) -> Command<crate::pages::Message> {
let current_folder = self.config.current_folder().to_owned(); let current_folder = self.config.current_folder().to_owned();
Some(Box::pin(async move { command::future(async move {
let (wallpaper_service_config, outputs) = wallpaper::config(); let (wallpaper_service_config, outputs) = wallpaper::config();
let update = change_folder(current_folder).await; let update = change_folder(current_folder).await;
@ -263,7 +261,7 @@ impl page::Page<crate::pages::Message> for Page {
outputs, outputs,
update, update,
)))) ))))
})) })
} }
} }

View file

@ -3,6 +3,7 @@
use cosmic_settings_page::{self as page, section, Section}; use cosmic_settings_page::{self as page, section, Section};
use cosmic::{command, Command};
use cosmic::{ use cosmic::{
iced::{ iced::{
widget::{horizontal_space, row}, widget::{horizontal_space, row},
@ -46,10 +47,10 @@ impl page::Page<crate::pages::Message> for Page {
.description(fl!("about", "desc")) .description(fl!("about", "desc"))
} }
fn load(&self, _page: page::Entity) -> Option<page::Task<crate::pages::Message>> { fn reload(&mut self, _page: page::Entity) -> Command<crate::pages::Message> {
Some(Box::pin(async move { command::future(async move {
crate::pages::Message::About(Message::Info(Box::new(Info::load()))) crate::pages::Message::About(Message::Info(Box::new(Info::load())))
})) })
} }
} }

View file

@ -37,10 +37,6 @@ impl page::Page<crate::pages::Message> for Page {
.title(fl!("time-date")) .title(fl!("time-date"))
.description(fl!("time-date", "desc")) .description(fl!("time-date", "desc"))
} }
fn load(&self, _page: page::Entity) -> Option<page::Task<crate::pages::Message>> {
None
}
} }
impl Page { impl Page {

View file

@ -3,7 +3,7 @@
use crate::section::{self, Section}; use crate::section::{self, Section};
use crate::{Content, Info, Page}; use crate::{Content, Info, Page};
use cosmic::iced_runtime::command::{Action, Command}; use cosmic::iced_runtime::command::Command;
use cosmic::Element; use cosmic::Element;
use regex::Regex; use regex::Regex;
use slotmap::{SecondaryMap, SlotMap, SparseSecondaryMap}; use slotmap::{SecondaryMap, SlotMap, SparseSecondaryMap};
@ -159,14 +159,12 @@ impl<Message: 'static> Binder<Message> {
} }
/// Calls a page's load function to refresh its data. /// Calls a page's load function to refresh its data.
pub fn page_reload(&mut self, id: crate::Entity) -> Option<Command<Message>> { pub fn page_reload(&mut self, id: crate::Entity) -> Command<Message> {
if let Some(page) = self.page.get(id) { if let Some(page) = self.page.get_mut(id) {
if let Some(future) = page.load(id) { return page.reload(id);
return Some(Command::single(Action::Future(future)));
}
} }
None Command::none()
} }
#[must_use] #[must_use]

View file

@ -14,7 +14,7 @@ pub use section::Section;
use derive_setters::Setters; use derive_setters::Setters;
use slotmap::SlotMap; use slotmap::SlotMap;
use std::{borrow::Cow, future::Future, pin::Pin}; use std::borrow::Cow;
slotmap::new_key_type! { slotmap::new_key_type! {
/// The unique ID of a page. /// The unique ID of a page.
@ -24,9 +24,6 @@ slotmap::new_key_type! {
/// A collection of sections which a page may be comprised of. /// A collection of sections which a page may be comprised of.
pub type Content = Vec<section::Entity>; pub type Content = Vec<section::Entity>;
/// A request by a page to run a command in the background.
pub type Task<Message> = Pin<Box<dyn Future<Output = Message> + Send>>;
pub trait Page<Message: 'static>: Downcast { pub trait Page<Message: 'static>: Downcast {
/// Information about the page /// Information about the page
fn info(&self) -> Info; fn info(&self) -> Info;
@ -51,8 +48,8 @@ pub trait Page<Message: 'static>: Downcast {
#[must_use] #[must_use]
#[allow(unused)] #[allow(unused)]
fn load(&self, page: crate::Entity) -> Option<crate::Task<Message>> { fn reload(&mut self, page: crate::Entity) -> Command<Message> {
None Command::none()
} }
/// Emit a command when the page is left /// Emit a command when the page is left