feat: load page data asynchronously with tokio
This commit is contained in:
parent
b2cb2b2e70
commit
d4c83c6de7
7 changed files with 48 additions and 60 deletions
44
Cargo.lock
generated
44
Cargo.lock
generated
|
|
@ -166,16 +166,6 @@ version = "0.13.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "better-panic"
|
|
||||||
version = "0.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6fa9e1d11a268684cbd90ed36370d7577afb6c62d912ddff5c15fc34343e5036"
|
|
||||||
dependencies = [
|
|
||||||
"backtrace",
|
|
||||||
"console",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bit-set"
|
name = "bit-set"
|
||||||
version = "0.5.3"
|
version = "0.5.3"
|
||||||
|
|
@ -435,18 +425,6 @@ dependencies = [
|
||||||
"crossbeam-utils",
|
"crossbeam-utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "console"
|
|
||||||
version = "0.15.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c3d79fbe8970a77e3e34151cc13d3b3e248aa0faaecb9f6091fa07ebefe5ad60"
|
|
||||||
dependencies = [
|
|
||||||
"encode_unicode",
|
|
||||||
"lazy_static",
|
|
||||||
"libc",
|
|
||||||
"windows-sys 0.42.0",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "const_panic"
|
name = "const_panic"
|
||||||
version = "0.2.7"
|
version = "0.2.7"
|
||||||
|
|
@ -512,7 +490,6 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"apply",
|
"apply",
|
||||||
"async-channel",
|
"async-channel",
|
||||||
"better-panic",
|
|
||||||
"bytecheck",
|
"bytecheck",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"derive_setters",
|
"derive_setters",
|
||||||
|
|
@ -526,6 +503,7 @@ dependencies = [
|
||||||
"rkyv",
|
"rkyv",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
"slotmap",
|
"slotmap",
|
||||||
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -922,12 +900,6 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "encode_unicode"
|
|
||||||
version = "0.3.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "error-code"
|
name = "error-code"
|
||||||
version = "2.3.1"
|
version = "2.3.1"
|
||||||
|
|
@ -1625,6 +1597,7 @@ source = "git+https://github.com/pop-os/libcosmic?rev=843919e44f0a00c33c29358359
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"log",
|
"log",
|
||||||
|
"tokio",
|
||||||
"wasm-bindgen-futures",
|
"wasm-bindgen-futures",
|
||||||
"wasm-timer",
|
"wasm-timer",
|
||||||
]
|
]
|
||||||
|
|
@ -1925,6 +1898,7 @@ dependencies = [
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"palette",
|
"palette",
|
||||||
"slotmap",
|
"slotmap",
|
||||||
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -3538,6 +3512,18 @@ dependencies = [
|
||||||
"displaydoc",
|
"displaydoc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tokio"
|
||||||
|
version = "1.25.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"num_cpus",
|
||||||
|
"pin-project-lite",
|
||||||
|
"windows-sys 0.42.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "0.5.11"
|
version = "0.5.11"
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ rust-version = "1.65.0"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
apply = "0.3.0"
|
apply = "0.3.0"
|
||||||
async-channel = "1.8.0"
|
async-channel = "1.8.0"
|
||||||
better-panic = "0.3.0"
|
|
||||||
bytecheck = "0.6.9"
|
bytecheck = "0.6.9"
|
||||||
color-eyre = "0.6.2"
|
color-eyre = "0.6.2"
|
||||||
derive_setters = "0.1.5"
|
derive_setters = "0.1.5"
|
||||||
|
|
@ -20,6 +19,7 @@ regex = "1.7.1"
|
||||||
rkyv = { version = "0.7.39", features = ["validation"]}
|
rkyv = { version = "0.7.39", features = ["validation"]}
|
||||||
rust-embed = "6.4.2"
|
rust-embed = "6.4.2"
|
||||||
slotmap = "1.0.6"
|
slotmap = "1.0.6"
|
||||||
|
tokio = "1.25.0"
|
||||||
|
|
||||||
[dependencies.i18n-embed]
|
[dependencies.i18n-embed]
|
||||||
version = "0.13.8"
|
version = "0.13.8"
|
||||||
|
|
@ -29,10 +29,10 @@ features = ["fluent-system", "desktop-requester"]
|
||||||
git = "https://github.com/pop-os/libcosmic"
|
git = "https://github.com/pop-os/libcosmic"
|
||||||
rev = "843919e44f0a00c33c29358359be5b4bfa41ab00"
|
rev = "843919e44f0a00c33c29358359be5b4bfa41ab00"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["debug", "winit", "dyrend"]
|
features = ["debug", "winit", "dyrend", "tokio"]
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 3
|
opt-level = 2
|
||||||
incremental = true
|
incremental = true
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ pub enum Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Application for SettingsApp {
|
impl Application for SettingsApp {
|
||||||
type Executor = iced::executor::Default;
|
type Executor = cosmic::executor::single::Executor;
|
||||||
type Flags = ();
|
type Flags = ();
|
||||||
type Message = Message;
|
type Message = Message;
|
||||||
type Theme = Theme;
|
type Theme = Theme;
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,6 @@ use i18n_embed::DesktopLanguageRequester;
|
||||||
///
|
///
|
||||||
/// Returns error if iced fails to run the application.
|
/// Returns error if iced fails to run the application.
|
||||||
pub fn main() -> color_eyre::Result<()> {
|
pub fn main() -> color_eyre::Result<()> {
|
||||||
better_panic::install();
|
|
||||||
color_eyre::install()?;
|
color_eyre::install()?;
|
||||||
|
|
||||||
if std::env::var("RUST_SPANTRACE").is_err() {
|
if std::env::var("RUST_SPANTRACE").is_err() {
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,14 @@ pub mod desktop;
|
||||||
pub mod networking;
|
pub mod networking;
|
||||||
pub mod section;
|
pub mod section;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
|
|
||||||
pub use section::Section;
|
pub use section::Section;
|
||||||
pub mod sound;
|
pub mod sound;
|
||||||
pub mod system;
|
pub mod system;
|
||||||
|
|
||||||
mod model;
|
mod model;
|
||||||
|
|
||||||
pub use model::{Insert, Model};
|
pub use model::{Insert, Model, PageTask};
|
||||||
|
|
||||||
use derive_setters::Setters;
|
use derive_setters::Setters;
|
||||||
use slotmap::SlotMap;
|
use slotmap::SlotMap;
|
||||||
|
|
@ -39,8 +40,8 @@ pub trait Page {
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
fn load(page: Entity) -> crate::Message {
|
fn load(page: Entity) -> PageTask {
|
||||||
crate::Message::None
|
Box::pin(async move { crate::Message::None })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,20 @@
|
||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
any::{Any, TypeId},
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
|
future::Future,
|
||||||
|
pin::Pin,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::page::{self, section, Content, Meta, Page, Section};
|
use crate::page::{self, section, Content, Meta, Page, Section};
|
||||||
|
use cosmic::iced_native::command::{Action, Command};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use slotmap::{SecondaryMap, SlotMap, SparseSecondaryMap};
|
use slotmap::{SecondaryMap, SlotMap, SparseSecondaryMap};
|
||||||
|
|
||||||
|
pub type PageTask = Pin<Box<dyn Future<Output = crate::Message> + Send>>;
|
||||||
|
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
pub pages: SlotMap<page::Entity, Meta>,
|
pub pages: SlotMap<page::Entity, Meta>,
|
||||||
pub page_load: SecondaryMap<page::Entity, fn(page::Entity) -> crate::Message>,
|
pub page_load: SecondaryMap<page::Entity, fn(page::Entity) -> PageTask>,
|
||||||
pub resource: HashMap<TypeId, Box<dyn Any>>,
|
pub resource: HashMap<TypeId, Box<dyn Any>>,
|
||||||
pub storage: HashMap<TypeId, SecondaryMap<page::Entity, Box<dyn Any>>>,
|
pub storage: HashMap<TypeId, SecondaryMap<page::Entity, Box<dyn Any>>>,
|
||||||
pub sub_pages: SparseSecondaryMap<page::Entity, Vec<page::Entity>>,
|
pub sub_pages: SparseSecondaryMap<page::Entity, Vec<page::Entity>>,
|
||||||
|
|
@ -81,19 +86,9 @@ impl Model {
|
||||||
.and_then(|storage| storage.remove(id));
|
.and_then(|storage| storage.remove(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_page(&mut self, id: page::Entity) -> Option<cosmic::iced::Command<crate::Message>> {
|
pub fn init_page(&mut self, id: page::Entity) -> Option<Command<crate::Message>> {
|
||||||
if let Some(func) = self.page_load.get(id).copied() {
|
if let Some(func) = self.page_load.get(id).copied() {
|
||||||
let (tx, rx) = async_channel::bounded(1);
|
return Some(Command::single(Action::Future(func(id))));
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
|
||||||
let _res = tx.send_blocking(func(id));
|
|
||||||
});
|
|
||||||
|
|
||||||
let future = async move { dbg!(rx.recv().await.unwrap_or(crate::Message::None)) };
|
|
||||||
|
|
||||||
return Some(cosmic::iced::Command::single(
|
|
||||||
cosmic::iced_native::command::Action::Future(Box::pin(future)),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
|
@ -102,6 +97,7 @@ impl Model {
|
||||||
// Registers a new page in the settings panel.
|
// Registers a new page in the settings panel.
|
||||||
pub fn register<P: Page>(&mut self) -> Insert {
|
pub fn register<P: Page>(&mut self) -> Insert {
|
||||||
let id = self.pages.insert(P::page());
|
let id = self.pages.insert(P::page());
|
||||||
|
|
||||||
self.page_load.insert(id, P::load);
|
self.page_load.insert(id, P::load);
|
||||||
|
|
||||||
if let Some(content) = P::content(&mut self.sections) {
|
if let Some(content) = P::content(&mut self.sections) {
|
||||||
|
|
@ -184,6 +180,8 @@ impl<'a> Insert<'a> {
|
||||||
..P::page()
|
..P::page()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
self.model.page_load.insert(page, P::load);
|
||||||
|
|
||||||
if let Some(content) = P::content(&mut self.model.sections) {
|
if let Some(content) = P::content(&mut self.model.sections) {
|
||||||
self.model.content.insert(page, content);
|
self.model.content.insert(page, content);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
// Copyright 2023 System76 <info@system76.com>
|
// Copyright 2023 System76 <info@system76.com>
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
use crate::page::{self, Content, Section};
|
use crate::page::{self, Content, Section};
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
iced::{
|
iced::{
|
||||||
|
|
@ -38,7 +40,7 @@ pub struct Info {
|
||||||
impl Model {
|
impl Model {
|
||||||
pub fn update(&mut self, message: Message) {
|
pub fn update(&mut self, message: Message) {
|
||||||
match message {
|
match message {
|
||||||
Message::Info(info) => self.info = dbg!(info),
|
Message::Info(info) => self.info = info,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -64,15 +66,17 @@ impl page::Page for Page {
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load(page: page::Entity) -> crate::Message {
|
fn load(_page: page::Entity) -> crate::page::PageTask {
|
||||||
let info = Info {
|
Box::pin(async move {
|
||||||
windowing_system: std::env::var("XDG_SESSION_TYPE")
|
let info = Info {
|
||||||
.ok()
|
windowing_system: std::env::var("XDG_SESSION_TYPE")
|
||||||
.unwrap_or_else(|| fl!("unknown")),
|
.ok()
|
||||||
..Info::default()
|
.unwrap_or_else(|| fl!("unknown")),
|
||||||
};
|
..Info::default()
|
||||||
|
};
|
||||||
|
|
||||||
crate::Message::About(Message::Info(info))
|
crate::Message::About(Message::Info(info))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue