Simplify the QueryInformation Action

This commit is contained in:
Richard 2022-04-27 16:18:27 -03:00
parent 6e167675d6
commit 5eefa5d4ea
7 changed files with 15 additions and 35 deletions

View file

@ -42,8 +42,6 @@ smol = ["iced_futures/smol"]
palette = ["iced_core/palette"] palette = ["iced_core/palette"]
# Enables pure, virtual widgets in the `pure` module # Enables pure, virtual widgets in the `pure` module
pure = ["iced_pure", "iced_graphics/pure"] pure = ["iced_pure", "iced_graphics/pure"]
# Enables system information querying `Action`
sysinfo = ["iced_winit/sysinfo"]
[badges] [badges]
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }

View file

@ -6,5 +6,5 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
iced = { path = "../..", features = ["sysinfo"] } iced = { path = "../.." }
bytesize = { version = "1.1.0" } bytesize = { version = "1.1.0" }

View file

@ -15,12 +15,11 @@ enum Example {
information: system::Information, information: system::Information,
refresh_button: button::State, refresh_button: button::State,
}, },
Unsupported,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
enum Message { enum Message {
InformationReceived(Option<system::Information>), InformationReceived(system::Information),
Refresh, Refresh,
} }
@ -46,15 +45,11 @@ impl Application for Example {
return system::fetch_information(Message::InformationReceived); return system::fetch_information(Message::InformationReceived);
} }
Message::InformationReceived(information) => { Message::InformationReceived(information) => {
if let Some(information) = information { let refresh_button = button::State::new();
let refresh_button = button::State::new(); *self = Self::Loaded {
*self = Self::Loaded { information,
information, refresh_button,
refresh_button, };
};
} else {
*self = Self::Unsupported;
}
} }
} }
@ -156,7 +151,6 @@ impl Application for Example {
.spacing(10) .spacing(10)
.into() .into()
} }
Example::Unsupported => Text::new("Unsupported!").size(20).into(),
}; };
Container::new(content) Container::new(content)

View file

@ -7,7 +7,7 @@ use std::fmt;
/// An operation to be performed on the system. /// An operation to be performed on the system.
pub enum Action<T> { pub enum Action<T> {
/// Query system information and produce `T` with the result. /// Query system information and produce `T` with the result.
QueryInformation(Box<dyn Fn(Option<system::Information>) -> T>), QueryInformation(Box<dyn Fn(system::Information) -> T>),
} }
impl<T> Action<T> { impl<T> Action<T> {

View file

@ -17,7 +17,7 @@ debug = ["iced_native/debug"]
window_clipboard = "0.2" window_clipboard = "0.2"
log = "0.4" log = "0.4"
thiserror = "1.0" thiserror = "1.0"
sysinfo = { version = "0.23", optional = true } sysinfo = "0.23"
[dependencies.winit] [dependencies.winit]
version = "0.26" version = "0.26"

View file

@ -582,7 +582,7 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
command::Action::System(action) => match action { command::Action::System(action) => match action {
system::Action::QueryInformation(tag) => { system::Action::QueryInformation(tag) => {
let information = let information =
crate::system::get_information(graphics_info()); crate::system::information(graphics_info());
let message = tag(information); let message = tag(information);

View file

@ -5,20 +5,17 @@ pub use iced_native::system::*;
use iced_graphics::compositor; use iced_graphics::compositor;
/// Query for available system information. /// Query for available system information.
///
/// Returns `None` if not using the `sysinfo` feature flag.
pub fn fetch_information<Message>( pub fn fetch_information<Message>(
f: impl Fn(Option<Information>) -> Message + 'static, f: impl Fn(Information) -> Message + 'static,
) -> Command<Message> { ) -> Command<Message> {
Command::single(command::Action::System(Action::QueryInformation( Command::single(command::Action::System(Action::QueryInformation(
Box::new(f), Box::new(f),
))) )))
} }
#[cfg(feature = "sysinfo")] pub(crate) fn information(
pub(crate) fn get_information(
graphics_info: compositor::Information, graphics_info: compositor::Information,
) -> Option<Information> { ) -> Information {
use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt}; use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt};
let mut system = System::new_all(); let mut system = System::new_all();
system.refresh_all(); system.refresh_all();
@ -30,7 +27,7 @@ pub(crate) fn get_information(
.and_then(|process| Ok(process.memory())) .and_then(|process| Ok(process.memory()))
.ok(); .ok();
let information = Information { Information {
system_name: system.name(), system_name: system.name(),
system_kernel: system.kernel_version(), system_kernel: system.kernel_version(),
system_version: system.long_os_version(), system_version: system.long_os_version(),
@ -40,14 +37,5 @@ pub(crate) fn get_information(
memory_used, memory_used,
graphics_adapter: graphics_info.adapter, graphics_adapter: graphics_info.adapter,
graphics_backend: graphics_info.backend, graphics_backend: graphics_info.backend,
}; }
Some(information)
}
#[cfg(not(feature = "sysinfo"))]
pub(crate) fn get_information(
_graphics_info: compositor::Information,
) -> Option<Information> {
None
} }