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"
|
||||
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]]
|
||||
name = "bit-set"
|
||||
version = "0.5.3"
|
||||
|
|
@ -435,18 +425,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "const_panic"
|
||||
version = "0.2.7"
|
||||
|
|
@ -512,7 +490,6 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"apply",
|
||||
"async-channel",
|
||||
"better-panic",
|
||||
"bytecheck",
|
||||
"color-eyre",
|
||||
"derive_setters",
|
||||
|
|
@ -526,6 +503,7 @@ dependencies = [
|
|||
"rkyv",
|
||||
"rust-embed",
|
||||
"slotmap",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -922,12 +900,6 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
|
||||
|
||||
[[package]]
|
||||
name = "error-code"
|
||||
version = "2.3.1"
|
||||
|
|
@ -1625,6 +1597,7 @@ source = "git+https://github.com/pop-os/libcosmic?rev=843919e44f0a00c33c29358359
|
|||
dependencies = [
|
||||
"futures",
|
||||
"log",
|
||||
"tokio",
|
||||
"wasm-bindgen-futures",
|
||||
"wasm-timer",
|
||||
]
|
||||
|
|
@ -1925,6 +1898,7 @@ dependencies = [
|
|||
"lazy_static",
|
||||
"palette",
|
||||
"slotmap",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -3538,6 +3512,18 @@ dependencies = [
|
|||
"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]]
|
||||
name = "toml"
|
||||
version = "0.5.11"
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ rust-version = "1.65.0"
|
|||
[dependencies]
|
||||
apply = "0.3.0"
|
||||
async-channel = "1.8.0"
|
||||
better-panic = "0.3.0"
|
||||
bytecheck = "0.6.9"
|
||||
color-eyre = "0.6.2"
|
||||
derive_setters = "0.1.5"
|
||||
|
|
@ -20,6 +19,7 @@ regex = "1.7.1"
|
|||
rkyv = { version = "0.7.39", features = ["validation"]}
|
||||
rust-embed = "6.4.2"
|
||||
slotmap = "1.0.6"
|
||||
tokio = "1.25.0"
|
||||
|
||||
[dependencies.i18n-embed]
|
||||
version = "0.13.8"
|
||||
|
|
@ -29,14 +29,14 @@ features = ["fluent-system", "desktop-requester"]
|
|||
git = "https://github.com/pop-os/libcosmic"
|
||||
rev = "843919e44f0a00c33c29358359be5b4bfa41ab00"
|
||||
default-features = false
|
||||
features = ["debug", "winit", "dyrend"]
|
||||
features = ["debug", "winit", "dyrend", "tokio"]
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 3
|
||||
opt-level = 2
|
||||
incremental = true
|
||||
|
||||
[profile.release]
|
||||
opt-level = "s"
|
||||
overflow-checks = true
|
||||
lto = "thin"
|
||||
incremental = false
|
||||
incremental = false
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ pub enum Message {
|
|||
}
|
||||
|
||||
impl Application for SettingsApp {
|
||||
type Executor = iced::executor::Default;
|
||||
type Executor = cosmic::executor::single::Executor;
|
||||
type Flags = ();
|
||||
type Message = Message;
|
||||
type Theme = Theme;
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ use i18n_embed::DesktopLanguageRequester;
|
|||
///
|
||||
/// Returns error if iced fails to run the application.
|
||||
pub fn main() -> color_eyre::Result<()> {
|
||||
better_panic::install();
|
||||
color_eyre::install()?;
|
||||
|
||||
if std::env::var("RUST_SPANTRACE").is_err() {
|
||||
|
|
|
|||
|
|
@ -5,13 +5,14 @@ pub mod desktop;
|
|||
pub mod networking;
|
||||
pub mod section;
|
||||
pub mod time;
|
||||
|
||||
pub use section::Section;
|
||||
pub mod sound;
|
||||
pub mod system;
|
||||
|
||||
mod model;
|
||||
|
||||
pub use model::{Insert, Model};
|
||||
pub use model::{Insert, Model, PageTask};
|
||||
|
||||
use derive_setters::Setters;
|
||||
use slotmap::SlotMap;
|
||||
|
|
@ -39,8 +40,8 @@ pub trait Page {
|
|||
|
||||
#[must_use]
|
||||
#[allow(unused)]
|
||||
fn load(page: Entity) -> crate::Message {
|
||||
crate::Message::None
|
||||
fn load(page: Entity) -> PageTask {
|
||||
Box::pin(async move { crate::Message::None })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,15 +4,20 @@
|
|||
use std::{
|
||||
any::{Any, TypeId},
|
||||
collections::HashMap,
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
};
|
||||
|
||||
use crate::page::{self, section, Content, Meta, Page, Section};
|
||||
use cosmic::iced_native::command::{Action, Command};
|
||||
use regex::Regex;
|
||||
use slotmap::{SecondaryMap, SlotMap, SparseSecondaryMap};
|
||||
|
||||
pub type PageTask = Pin<Box<dyn Future<Output = crate::Message> + Send>>;
|
||||
|
||||
pub struct Model {
|
||||
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 storage: HashMap<TypeId, SecondaryMap<page::Entity, Box<dyn Any>>>,
|
||||
pub sub_pages: SparseSecondaryMap<page::Entity, Vec<page::Entity>>,
|
||||
|
|
@ -81,19 +86,9 @@ impl Model {
|
|||
.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() {
|
||||
let (tx, rx) = async_channel::bounded(1);
|
||||
|
||||
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)),
|
||||
));
|
||||
return Some(Command::single(Action::Future(func(id))));
|
||||
}
|
||||
|
||||
None
|
||||
|
|
@ -102,6 +97,7 @@ impl Model {
|
|||
// Registers a new page in the settings panel.
|
||||
pub fn register<P: Page>(&mut self) -> Insert {
|
||||
let id = self.pages.insert(P::page());
|
||||
|
||||
self.page_load.insert(id, P::load);
|
||||
|
||||
if let Some(content) = P::content(&mut self.sections) {
|
||||
|
|
@ -184,6 +180,8 @@ impl<'a> Insert<'a> {
|
|||
..P::page()
|
||||
});
|
||||
|
||||
self.model.page_load.insert(page, P::load);
|
||||
|
||||
if let Some(content) = P::content(&mut self.model.sections) {
|
||||
self.model.content.insert(page, content);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright 2023 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use std::future::Future;
|
||||
|
||||
use crate::page::{self, Content, Section};
|
||||
use cosmic::{
|
||||
iced::{
|
||||
|
|
@ -38,7 +40,7 @@ pub struct Info {
|
|||
impl Model {
|
||||
pub fn update(&mut self, message: 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 {
|
||||
let info = Info {
|
||||
windowing_system: std::env::var("XDG_SESSION_TYPE")
|
||||
.ok()
|
||||
.unwrap_or_else(|| fl!("unknown")),
|
||||
..Info::default()
|
||||
};
|
||||
fn load(_page: page::Entity) -> crate::page::PageTask {
|
||||
Box::pin(async move {
|
||||
let info = Info {
|
||||
windowing_system: std::env::var("XDG_SESSION_TYPE")
|
||||
.ok()
|
||||
.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