From 1e62fdf069db5687be510e1cc375260bbff318a7 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 27 Jan 2022 04:00:53 -0300 Subject: [PATCH 01/37] Introduce `Error::ContextCreationFailed` --- glutin/src/application.rs | 15 ++++++++++++++- graphics/src/error.rs | 18 +++++++++++++++--- src/error.rs | 10 +++++----- wgpu/src/window/compositor.rs | 2 +- winit/src/error.rs | 12 ++++-------- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 27a932fc5d..146b234e10 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -80,12 +80,25 @@ where .or_else(|_| second_builder.build_windowed(builder, &event_loop)) .map_err(|error| { use glutin::CreationError; + use iced_graphics::Error as ContextError; match error { CreationError::Window(error) => { Error::WindowCreationFailed(error) } - _ => Error::GraphicsAdapterNotFound, + CreationError::OpenGlVersionNotSupported => { + Error::ContextCreationFailed( + ContextError::VersionNotSupported, + ) + } + CreationError::NoAvailablePixelFormat => { + Error::ContextCreationFailed( + ContextError::NoAvailablePixelFormat, + ) + } + error => Error::ContextCreationFailed( + ContextError::BackendError(error.to_string()), + ), } })?; diff --git a/graphics/src/error.rs b/graphics/src/error.rs index c86e326a4e..77758f5412 100644 --- a/graphics/src/error.rs +++ b/graphics/src/error.rs @@ -1,7 +1,19 @@ -/// A graphical error that occurred while running an application. +/// An error that occurred while creating an application's graphical context. #[derive(Debug, thiserror::Error)] pub enum Error { - /// A suitable graphics adapter or device could not be found + /// The requested backend version is not supported. + #[error("the requested backend version is not supported")] + VersionNotSupported, + + /// Failed to find any pixel format that matches the criteria. + #[error("failed to find any pixel format that matches the criteria")] + NoAvailablePixelFormat, + + /// A suitable graphics adapter or device could not be found. #[error("a suitable graphics adapter or device could not be found")] - AdapterNotFound, + GraphicsAdapterNotFound, + + /// An error occured in the context's internal backend + #[error("an error occured in the context's internal backend")] + BackendError(String), } diff --git a/src/error.rs b/src/error.rs index 17479c6099..83c9b1b643 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,9 +11,9 @@ pub enum Error { #[error("the application window could not be created")] WindowCreationFailed(Box), - /// A suitable graphics adapter or device could not be found. - #[error("a suitable graphics adapter or device could not be found")] - GraphicsAdapterNotFound, + /// The application context could not be created. + #[error("the application context could not be created")] + ContextCreationFailed(iced_graphics::Error), } impl From for Error { @@ -25,8 +25,8 @@ impl From for Error { iced_winit::Error::WindowCreationFailed(error) => { Error::WindowCreationFailed(Box::new(error)) } - iced_winit::Error::GraphicsAdapterNotFound => { - Error::GraphicsAdapterNotFound + iced_winit::Error::ContextCreationFailed(error) => { + Error::ContextCreationFailed(error) } } } diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 64c53607b6..0b368cbf03 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -105,7 +105,7 @@ impl iced_graphics::window::Compositor for Compositor { settings, compatible_window, )) - .ok_or(Error::AdapterNotFound)?; + .ok_or(Error::GraphicsAdapterNotFound)?; let backend = compositor.create_backend(); diff --git a/winit/src/error.rs b/winit/src/error.rs index 8e1d20e879..2dd045d29d 100644 --- a/winit/src/error.rs +++ b/winit/src/error.rs @@ -11,17 +11,13 @@ pub enum Error { #[error("the application window could not be created")] WindowCreationFailed(winit::error::OsError), - /// A suitable graphics adapter or device could not be found. - #[error("a suitable graphics adapter or device could not be found")] - GraphicsAdapterNotFound, + /// The application context could not be created. + #[error("the application context could not be created")] + ContextCreationFailed(iced_graphics::Error), } impl From for Error { fn from(error: iced_graphics::Error) -> Error { - match error { - iced_graphics::Error::AdapterNotFound => { - Error::GraphicsAdapterNotFound - } - } + Error::ContextCreationFailed(error) } } From 764b424dfc2b2163f21483a6d1f05ea1da62c561 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 27 Jan 2022 06:02:19 -0300 Subject: [PATCH 02/37] Add logging to window and context creation --- glow/src/window/compositor.rs | 2 ++ glutin/Cargo.toml | 3 +++ glutin/src/application.rs | 9 ++++++++- wgpu/src/window/compositor.rs | 15 +++++++++++++++ winit/src/application.rs | 18 ++++++++++-------- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/glow/src/window/compositor.rs b/glow/src/window/compositor.rs index 44019fb26f..d1896a9705 100644 --- a/glow/src/window/compositor.rs +++ b/glow/src/window/compositor.rs @@ -20,6 +20,8 @@ impl iced_graphics::window::GLCompositor for Compositor { ) -> Result<(Self, Self::Renderer), Error> { let gl = glow::Context::from_loader_function(loader_function); + log::info!("{:#?}", settings); + let version = gl.version(); log::info!("Version: {:?}", version); log::info!("Embedded: {}", version.is_embedded); diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index d1b0468dd5..cd5f2a7a30 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -13,6 +13,9 @@ categories = ["gui"] [features] debug = ["iced_winit/debug"] +[dependencies.log] +version = "0.4" + [dependencies.glutin] version = "0.28" git = "https://github.com/iced-rs/glutin" diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 146b234e10..148a8e5084 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -61,6 +61,8 @@ where settings.id, ); + log::info!("Window builder: {:#?}", builder); + let opengl_builder = ContextBuilder::new() .with_vsync(true) .with_multisampling(C::sample_count(&compositor_settings) as u16); @@ -75,9 +77,14 @@ where (opengl_builder, opengles_builder) }; + log::info!("Trying first builder: {:#?}", first_builder); + let context = first_builder .build_windowed(builder.clone(), &event_loop) - .or_else(|_| second_builder.build_windowed(builder, &event_loop)) + .or_else(|_| { + log::info!("Trying second builder: {:#?}", second_builder); + second_builder.build_windowed(builder, &event_loop) + }) .map_err(|error| { use glutin::CreationError; use iced_graphics::Error as ContextError; diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 0b368cbf03..ca0ce51b59 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -28,6 +28,17 @@ impl Compositor { ) -> Option { let instance = wgpu::Instance::new(settings.internal_backend); + log::info!("{:#?}", settings); + + #[cfg(not(target_arch = "wasm32"))] + { + let available_adapters: Vec<_> = instance + .enumerate_adapters(settings.internal_backend) + .map(|adapter| adapter.get_info()) + .collect(); + log::info!("Available adapters: {:#?}", available_adapters); + } + #[allow(unsafe_code)] let compatible_surface = compatible_window .map(|window| unsafe { instance.create_surface(window) }); @@ -44,10 +55,14 @@ impl Compositor { }) .await?; + log::info!("Selected: {:#?}", adapter.get_info()); + let format = compatible_surface .as_ref() .and_then(|surface| surface.get_preferred_format(&adapter))?; + log::info!("Selected format: {:?}", format); + #[cfg(target_arch = "wasm32")] let limits = wgpu::Limits::downlevel_webgl2_defaults() .using_resolution(adapter.limits()); diff --git a/winit/src/application.rs b/winit/src/application.rs index ed0775072a..6f2ea1a50c 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -137,14 +137,16 @@ where let subscription = application.subscription(); - let window = settings - .window - .into_builder( - &application.title(), - application.mode(), - event_loop.primary_monitor(), - settings.id, - ) + let builder = settings.window.into_builder( + &application.title(), + application.mode(), + event_loop.primary_monitor(), + settings.id, + ); + + log::info!("Window builder: {:#?}", builder); + + let window = builder .build(&event_loop) .map_err(Error::WindowCreationFailed)?; From 6e937131970d1e7b32204096e5f6c67de3fe6dfb Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 16 Feb 2022 19:11:42 -0300 Subject: [PATCH 03/37] Add `sysinfo` crate through feature flag --- Cargo.toml | 2 ++ winit/Cargo.toml | 1 + 2 files changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index c6ccc5df5d..6c69706a4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,8 @@ smol = ["iced_futures/smol"] palette = ["iced_core/palette"] # Enables pure, virtual widgets in the `pure` module pure = ["iced_pure", "iced_graphics/pure"] +# Enables system information querying `Action` +sysinfo = ["iced_winit/sysinfo"] [badges] maintenance = { status = "actively-developed" } diff --git a/winit/Cargo.toml b/winit/Cargo.toml index f723224820..b75831490e 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -17,6 +17,7 @@ debug = ["iced_native/debug"] window_clipboard = "0.2" log = "0.4" thiserror = "1.0" +sysinfo = { version = "0.23", optional = true } [dependencies.winit] version = "0.26" From fed8da1c9078638a96ae57a9dd6edacb0a1936b3 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 16 Feb 2022 19:20:26 -0300 Subject: [PATCH 04/37] Add new `System` variant to `Action` --- native/src/command/action.rs | 6 ++++++ native/src/lib.rs | 1 + native/src/system.rs | 3 +++ native/src/system/action.rs | 3 +++ winit/src/application.rs | 1 + 5 files changed, 14 insertions(+) create mode 100644 native/src/system.rs create mode 100644 native/src/system/action.rs diff --git a/native/src/command/action.rs b/native/src/command/action.rs index 5c7509c861..39536f7db7 100644 --- a/native/src/command/action.rs +++ b/native/src/command/action.rs @@ -1,4 +1,5 @@ use crate::clipboard; +use crate::system; use crate::window; use iced_futures::MaybeSend; @@ -17,6 +18,9 @@ pub enum Action { /// Run a window action. Window(window::Action), + + /// Run a system action. + System(system::Action), } impl Action { @@ -34,6 +38,7 @@ impl Action { Self::Future(future) => Action::Future(Box::pin(future.map(f))), Self::Clipboard(action) => Action::Clipboard(action.map(f)), Self::Window(window) => Action::Window(window), + Self::System(system) => Action::System(system), } } } @@ -46,6 +51,7 @@ impl fmt::Debug for Action { write!(f, "Action::Clipboard({:?})", action) } Self::Window(action) => write!(f, "Action::Window({:?})", action), + Self::System(action) => write!(f, "Action::System({:?})", action), } } } diff --git a/native/src/lib.rs b/native/src/lib.rs index 5c9c24c9cb..b80541697c 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -48,6 +48,7 @@ pub mod program; pub mod renderer; pub mod subscription; pub mod svg; +pub mod system; pub mod text; pub mod touch; pub mod user_interface; diff --git a/native/src/system.rs b/native/src/system.rs new file mode 100644 index 0000000000..e1a790bcd5 --- /dev/null +++ b/native/src/system.rs @@ -0,0 +1,3 @@ +//! Access the native system. +mod action; +pub use action::Action; diff --git a/native/src/system/action.rs b/native/src/system/action.rs new file mode 100644 index 0000000000..0d484957be --- /dev/null +++ b/native/src/system/action.rs @@ -0,0 +1,3 @@ +/// An operation to be performed on the system. +#[derive(Debug)] +pub enum Action {} diff --git a/winit/src/application.rs b/winit/src/application.rs index 6f2ea1a50c..33f53315ef 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -573,6 +573,7 @@ pub fn run_command( }); } }, + command::Action::System(_action) => {} } } } From 69781499cb070535bfc4e968d9ed3102ea722fb3 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 16 Feb 2022 19:37:38 -0300 Subject: [PATCH 05/37] Introduce `QueryInformation` to `system::Action` --- native/src/command/action.rs | 4 ++-- native/src/system.rs | 3 +++ native/src/system/action.rs | 37 ++++++++++++++++++++++++++++++-- native/src/system/information.rs | 12 +++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 native/src/system/information.rs diff --git a/native/src/command/action.rs b/native/src/command/action.rs index 39536f7db7..342d0cfe29 100644 --- a/native/src/command/action.rs +++ b/native/src/command/action.rs @@ -20,7 +20,7 @@ pub enum Action { Window(window::Action), /// Run a system action. - System(system::Action), + System(system::Action), } impl Action { @@ -38,7 +38,7 @@ impl Action { Self::Future(future) => Action::Future(Box::pin(future.map(f))), Self::Clipboard(action) => Action::Clipboard(action.map(f)), Self::Window(window) => Action::Window(window), - Self::System(system) => Action::System(system), + Self::System(system) => Action::System(system.map(f)), } } } diff --git a/native/src/system.rs b/native/src/system.rs index e1a790bcd5..61c8ff297f 100644 --- a/native/src/system.rs +++ b/native/src/system.rs @@ -1,3 +1,6 @@ //! Access the native system. mod action; +mod information; + pub use action::Action; +pub use information::Information; diff --git a/native/src/system/action.rs b/native/src/system/action.rs index 0d484957be..3bece0bb26 100644 --- a/native/src/system/action.rs +++ b/native/src/system/action.rs @@ -1,3 +1,36 @@ +use crate::system; + +use iced_futures::MaybeSend; + +use std::fmt; + /// An operation to be performed on the system. -#[derive(Debug)] -pub enum Action {} +pub enum Action { + /// Query system information and produce `T` with the result. + QueryInformation(Box) -> T>), +} + +impl Action { + /// Maps the output of a system [`Action`] using the provided closure. + pub fn map( + self, + f: impl Fn(T) -> A + 'static + MaybeSend + Sync, + ) -> Action + where + T: 'static, + { + match self { + Self::QueryInformation(o) => { + Action::QueryInformation(Box::new(move |s| f(o(s)))) + } + } + } +} + +impl fmt::Debug for Action { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::QueryInformation(_) => write!(f, "Action::QueryInformation"), + } + } +} diff --git a/native/src/system/information.rs b/native/src/system/information.rs new file mode 100644 index 0000000000..2100d110a9 --- /dev/null +++ b/native/src/system/information.rs @@ -0,0 +1,12 @@ +/// Contains informations about the system (e.g. system name, processor, memory, graphics adapter). +#[derive(Debug)] +pub struct Information { + system_name: String, + system_kernel: String, + system_version: String, + cpu_brand: String, + cpu_vendor: String, + cpu_name: String, + cpu_cores: String, + memory_total: String, +} From 6295a72aa66381af9567b0b25e22d960ac672998 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 8 Mar 2022 19:49:03 -0300 Subject: [PATCH 06/37] Implement `QueryInformation` for `iced_winit` --- winit/src/application.rs | 45 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/winit/src/application.rs b/winit/src/application.rs index 33f53315ef..d184c0c36b 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -540,6 +540,7 @@ pub fn run_command( window: &winit::window::Window, ) { use iced_native::command; + use iced_native::system; use iced_native::window; for action in command.actions() { @@ -573,7 +574,49 @@ pub fn run_command( }); } }, - command::Action::System(_action) => {} + command::Action::System(action) => match action { + system::Action::QueryInformation(tag) => { + #[cfg(feature = "sysinfo")] + let information = { + use sysinfo::{ProcessorExt, System, SystemExt}; + let mut system = System::new_all(); + system.refresh_all(); + + let cpu = system.global_processor_info(); + let unknown = String::from("unknown"); + + let information = system::Information { + system_name: system + .name() + .unwrap_or(unknown.clone()), + system_kernel: system + .kernel_version() + .unwrap_or(unknown.clone()), + system_version: system + .long_os_version() + .unwrap_or(unknown.clone()), + cpu_brand: cpu.brand().into(), + cpu_vendor: cpu.vendor_id().into(), + cpu_name: cpu.name().into(), + cpu_cores: system + .physical_core_count() + .map_or(unknown, |cores| cores.to_string()), + memory_total: system.total_memory().to_string(), + }; + + Some(information) + }; + + #[cfg(not(feature = "sysinfo"))] + let information = None; + + let message = tag(information); + + proxy + .send_event(message) + .expect("Send message to event loop"); + } + }, } } } From c2f45a192fdeba5eec79158dda8640a99a36fdb1 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 8 Mar 2022 19:58:34 -0300 Subject: [PATCH 07/37] Turn `Information` fields `pub` --- native/src/system/information.rs | 24 ++++++++++++++++-------- winit/src/application.rs | 19 +++++-------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/native/src/system/information.rs b/native/src/system/information.rs index 2100d110a9..a2abec9330 100644 --- a/native/src/system/information.rs +++ b/native/src/system/information.rs @@ -1,12 +1,20 @@ /// Contains informations about the system (e.g. system name, processor, memory, graphics adapter). #[derive(Debug)] pub struct Information { - system_name: String, - system_kernel: String, - system_version: String, - cpu_brand: String, - cpu_vendor: String, - cpu_name: String, - cpu_cores: String, - memory_total: String, + /// Contains the system name. + pub system_name: Option, + /// Contains the kernel version. + pub system_kernel: Option, + /// Contains the systme version. + pub system_version: Option, + /// Contains the processor brand. + pub cpu_brand: String, + /// Contains the processor vendor id. + pub cpu_vendor: String, + /// Contains the processor name. + pub cpu_name: String, + /// Contains the number of physical cores on the processor. + pub cpu_cores: Option, + /// Contains the total RAM size in KB. + pub memory_total: u64, } diff --git a/winit/src/application.rs b/winit/src/application.rs index d184c0c36b..b2f9be2958 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -583,25 +583,16 @@ pub fn run_command( system.refresh_all(); let cpu = system.global_processor_info(); - let unknown = String::from("unknown"); let information = system::Information { - system_name: system - .name() - .unwrap_or(unknown.clone()), - system_kernel: system - .kernel_version() - .unwrap_or(unknown.clone()), - system_version: system - .long_os_version() - .unwrap_or(unknown.clone()), + system_name: system.name(), + system_kernel: system.kernel_version(), + system_version: system.long_os_version(), cpu_brand: cpu.brand().into(), cpu_vendor: cpu.vendor_id().into(), cpu_name: cpu.name().into(), - cpu_cores: system - .physical_core_count() - .map_or(unknown, |cores| cores.to_string()), - memory_total: system.total_memory().to_string(), + cpu_cores: system.physical_core_count(), + memory_total: system.total_memory(), }; Some(information) From 0b36a55196300371343a1614cec61ac041f160f4 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 10 Mar 2022 01:56:25 -0300 Subject: [PATCH 08/37] Add function helper in `iced_winit::system` --- winit/src/lib.rs | 1 + winit/src/system.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 winit/src/system.rs diff --git a/winit/src/lib.rs b/winit/src/lib.rs index b31adf6e62..2b5bb2ff2b 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -31,6 +31,7 @@ pub mod application; pub mod clipboard; pub mod conversion; pub mod settings; +pub mod system; pub mod window; mod error; diff --git a/winit/src/system.rs b/winit/src/system.rs new file mode 100644 index 0000000000..cd3ba07578 --- /dev/null +++ b/winit/src/system.rs @@ -0,0 +1,14 @@ +//! Access the native system. +use crate::command::{self, Command}; +use iced_native::system; + +/// Query for available system information. +/// +/// Returns `None` if not using the `sysinfo` feature flag. +pub fn information( + f: impl Fn(Option) -> Message + 'static, +) -> Command { + Command::single(command::Action::System(system::Action::QueryInformation( + Box::new(f), + ))) +} From c8ed318e17c8a7673e4260e5d4e12c44c9faa987 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 10 Mar 2022 03:01:12 -0300 Subject: [PATCH 09/37] Export new `system` module --- src/lib.rs | 1 + winit/src/system.rs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 84e872c731..c9a735e57d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -224,6 +224,7 @@ pub use settings::Settings; pub use runtime::alignment; pub use runtime::futures; +pub use runtime::system; pub use runtime::{ Alignment, Background, Color, Command, ContentFit, Font, Length, Point, Rectangle, Size, Subscription, Vector, diff --git a/winit/src/system.rs b/winit/src/system.rs index cd3ba07578..3d1b40220a 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -1,14 +1,14 @@ //! Access the native system. use crate::command::{self, Command}; -use iced_native::system; +pub use iced_native::system::*; /// Query for available system information. /// /// Returns `None` if not using the `sysinfo` feature flag. pub fn information( - f: impl Fn(Option) -> Message + 'static, + f: impl Fn(Option) -> Message + 'static, ) -> Command { - Command::single(command::Action::System(system::Action::QueryInformation( + Command::single(command::Action::System(Action::QueryInformation( Box::new(f), ))) } From 53538b65b1c557015c2900fc28b8916cf719a10b Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 10 Mar 2022 03:02:17 -0300 Subject: [PATCH 10/37] Add `system_information` example --- Cargo.toml | 1 + examples/system_information/Cargo.toml | 10 ++ examples/system_information/src/main.rs | 119 ++++++++++++++++++++++++ native/src/system/information.rs | 4 - winit/src/application.rs | 2 - 5 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 examples/system_information/Cargo.toml create mode 100644 examples/system_information/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 6c69706a4f..500fa06ee4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,6 +96,7 @@ members = [ "examples/pure/todos", "examples/pure/tour", "examples/websocket", + "examples/system_information" ] [dependencies] diff --git a/examples/system_information/Cargo.toml b/examples/system_information/Cargo.toml new file mode 100644 index 0000000000..d3d7618225 --- /dev/null +++ b/examples/system_information/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "system_information" +version = "0.1.0" +authors = ["Richard "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["sysinfo"] } +bytesize = { version = "1.1.0" } \ No newline at end of file diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs new file mode 100644 index 0000000000..f8ae1aafe6 --- /dev/null +++ b/examples/system_information/src/main.rs @@ -0,0 +1,119 @@ +use iced::{ + executor, system, Application, Column, Command, Container, Element, Length, + Settings, Text, +}; + +use bytesize::ByteSize; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +enum Example { + Loading, + Loaded { information: system::Information }, + Unsupported, +} + +#[derive(Debug)] +enum Message { + InformationReceived(Option), +} + +impl Application for Example { + type Message = Message; + type Executor = executor::Default; + type Flags = (); + + fn new(_flags: ()) -> (Self, Command) { + ( + Self::Loading, + system::information(Message::InformationReceived), + ) + } + + fn title(&self) -> String { + String::from("System Information - Iced") + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::InformationReceived(information) => { + if let Some(information) = information { + *self = Self::Loaded { information }; + } else { + *self = Self::Unsupported; + } + } + } + + Command::none() + } + + fn view(&mut self) -> Element { + let content: Element = match self { + Example::Loading => Text::new("Loading...").size(40).into(), + Example::Loaded { information } => { + let system_name = Text::new(format!( + "System name: {}", + information + .system_name + .as_ref() + .unwrap_or(&"unknown".to_string()) + )); + + let system_kernel = Text::new(format!( + "System kernel: {}", + information + .system_kernel + .as_ref() + .unwrap_or(&"unknown".to_string()) + )); + + let system_version = Text::new(format!( + "System version: {}", + information + .system_version + .as_ref() + .unwrap_or(&"unknown".to_string()) + )); + + let cpu_brand = Text::new(format!( + "Processor brand: {}", + information.cpu_brand + )); + + let cpu_cores = Text::new(format!( + "Processor cores: {}", + information + .cpu_cores + .map_or("unknown".to_string(), |cores| cores + .to_string()) + )); + + let memory_total = Text::new(format!( + "Memory (total): {}", + ByteSize::kb(information.memory_total).to_string() + )); + + Column::with_children(vec![ + system_name.into(), + system_kernel.into(), + system_version.into(), + cpu_brand.into(), + cpu_cores.into(), + memory_total.into(), + ]) + .into() + } + Example::Unsupported => Text::new("Unsupported!").size(20).into(), + }; + + Container::new(content) + .center_x() + .center_y() + .width(Length::Fill) + .height(Length::Fill) + .into() + } +} diff --git a/native/src/system/information.rs b/native/src/system/information.rs index a2abec9330..e614f11e82 100644 --- a/native/src/system/information.rs +++ b/native/src/system/information.rs @@ -9,10 +9,6 @@ pub struct Information { pub system_version: Option, /// Contains the processor brand. pub cpu_brand: String, - /// Contains the processor vendor id. - pub cpu_vendor: String, - /// Contains the processor name. - pub cpu_name: String, /// Contains the number of physical cores on the processor. pub cpu_cores: Option, /// Contains the total RAM size in KB. diff --git a/winit/src/application.rs b/winit/src/application.rs index b2f9be2958..f5a58f2fda 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -589,8 +589,6 @@ pub fn run_command( system_kernel: system.kernel_version(), system_version: system.long_os_version(), cpu_brand: cpu.brand().into(), - cpu_vendor: cpu.vendor_id().into(), - cpu_name: cpu.name().into(), cpu_cores: system.physical_core_count(), memory_total: system.total_memory(), }; From d9853165e78bffc1d2d4b1fc4ef71fe91b1bd38d Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 16 Mar 2022 19:42:40 -0300 Subject: [PATCH 11/37] Add unformated memory total to example --- examples/system_information/src/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index f8ae1aafe6..95f57ac02c 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -91,9 +91,15 @@ impl Application for Example { .to_string()) )); + let memory_readable = + ByteSize::kb(information.memory_total).to_string(); + let memory_total = Text::new(format!( "Memory (total): {}", - ByteSize::kb(information.memory_total).to_string() + format!( + "{} kb ({})", + information.memory_total, memory_readable + ) )); Column::with_children(vec![ From ced5f5075f0debe7fee0b86b064c64f46055a000 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 00:22:27 -0300 Subject: [PATCH 12/37] Export `system` module in `iced_glutin` --- glutin/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/glutin/src/lib.rs b/glutin/src/lib.rs index 723977916a..9948603428 100644 --- a/glutin/src/lib.rs +++ b/glutin/src/lib.rs @@ -22,6 +22,7 @@ pub mod application; pub use iced_winit::clipboard; pub use iced_winit::conversion; pub use iced_winit::settings; +pub use iced_winit::system; pub use iced_winit::window; pub use iced_winit::{Error, Mode}; From e23e4b8db20f3910bb6617a6931c9a8906791f8e Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 00:25:00 -0300 Subject: [PATCH 13/37] Introduce `GraphicsInformation` to `iced_graphics` --- graphics/src/window.rs | 2 +- graphics/src/window/compositor.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/graphics/src/window.rs b/graphics/src/window.rs index 67ec33224c..c73c8c5332 100644 --- a/graphics/src/window.rs +++ b/graphics/src/window.rs @@ -4,7 +4,7 @@ mod compositor; #[cfg(feature = "opengl")] mod gl_compositor; -pub use compositor::{Compositor, SurfaceError}; +pub use compositor::{Compositor, GraphicsInformation, SurfaceError}; #[cfg(feature = "opengl")] pub use gl_compositor::GLCompositor; diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 9ea040cd1b..5987b11862 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -71,3 +71,12 @@ pub enum SurfaceError { #[error("There is no more memory left to allocate a new frame")] OutOfMemory, } + +/// Contains informations about the graphics (e.g. graphics adapter, graphics backend). +#[derive(Debug)] +pub struct GraphicsInformation { + /// Contains the graphics adapter. + pub adapter: String, + /// Contains the graphics backend. + pub backend: String, +} From 83fec2f5f6231f3871ecfccf594a555253f8286c Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 00:34:42 -0300 Subject: [PATCH 14/37] Implement `GraphicsInformation` for `iced_wgpu` --- graphics/src/window/compositor.rs | 3 +++ wgpu/src/window/compositor.rs | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 5987b11862..035101c9de 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -38,6 +38,9 @@ pub trait Compositor: Sized { height: u32, ); + /// Returns [`GraphicsInformation`] used by this [`Compositor`]. + fn get_information(&self) -> GraphicsInformation; + /// Presents the [`Renderer`] primitives to the next frame of the given [`Surface`]. /// /// [`SwapChain`]: Self::SwapChain diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index ca0ce51b59..e283c1e2a1 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -9,6 +9,7 @@ use raw_window_handle::HasRawWindowHandle; pub struct Compositor { settings: Settings, instance: wgpu::Instance, + adapter: wgpu::Adapter, device: wgpu::Device, queue: wgpu::Queue, staging_belt: wgpu::util::StagingBelt, @@ -93,6 +94,7 @@ impl Compositor { Some(Compositor { instance, settings, + adapter, device, queue, staging_belt, @@ -155,6 +157,15 @@ impl iced_graphics::window::Compositor for Compositor { ); } + fn get_information(&self) -> iced_graphics::window::GraphicsInformation { + let information = self.adapter.get_info(); + + iced_graphics::window::GraphicsInformation { + adapter: information.name, + backend: format!("{:?}", information.backend), + } + } + fn present>( &mut self, renderer: &mut Self::Renderer, From 2b4d8a7b2653f51ae83009257c4ec1dc4201d5ff Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 00:41:43 -0300 Subject: [PATCH 15/37] Implement `GraphicsInformation` for `iced_glow` --- glow/src/window/compositor.rs | 9 +++++++++ graphics/src/window/gl_compositor.rs | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/glow/src/window/compositor.rs b/glow/src/window/compositor.rs index d1896a9705..f2740f6c83 100644 --- a/glow/src/window/compositor.rs +++ b/glow/src/window/compositor.rs @@ -67,6 +67,15 @@ impl iced_graphics::window::GLCompositor for Compositor { } } + fn get_information(&self) -> iced_graphics::window::GraphicsInformation { + let adapter = unsafe { self.gl.get_parameter_string(glow::RENDERER) }; + + iced_graphics::window::GraphicsInformation { + backend: format!("{:?}", self.gl.version()), + adapter, + } + } + fn present>( &mut self, renderer: &mut Self::Renderer, diff --git a/graphics/src/window/gl_compositor.rs b/graphics/src/window/gl_compositor.rs index b1b995f1f5..23c6d3f8be 100644 --- a/graphics/src/window/gl_compositor.rs +++ b/graphics/src/window/gl_compositor.rs @@ -1,3 +1,4 @@ +use crate::window::GraphicsInformation; use crate::{Color, Error, Size, Viewport}; use core::ffi::c_void; @@ -48,6 +49,9 @@ pub trait GLCompositor: Sized { /// Resizes the viewport of the [`GLCompositor`]. fn resize_viewport(&mut self, physical_size: Size); + /// Returns [`GraphicsInformation`] used by this [`Compositor`]. + fn get_information(&self) -> GraphicsInformation; + /// Presents the primitives of the [`Renderer`] to the next frame of the /// [`GLCompositor`]. /// From 5356bb9bdb0074683722be194f36169836824af8 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 00:58:28 -0300 Subject: [PATCH 16/37] Add graphics information to `system::Information` --- native/src/system/information.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/native/src/system/information.rs b/native/src/system/information.rs index e614f11e82..bf0b100cd6 100644 --- a/native/src/system/information.rs +++ b/native/src/system/information.rs @@ -13,4 +13,8 @@ pub struct Information { pub cpu_cores: Option, /// Contains the total RAM size in KB. pub memory_total: u64, + /// Contains the graphics backend. + pub graphics_backend: String, + /// Contains the graphics adapter. + pub graphics_adapter: String, } From 83ab27b5cbb20de79e532687a342a5f4036e3046 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 00:59:35 -0300 Subject: [PATCH 17/37] Add graphics information to `iced_winit` --- winit/src/application.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/winit/src/application.rs b/winit/src/application.rs index f5a58f2fda..9d65d76974 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -167,17 +167,18 @@ where let mut clipboard = Clipboard::connect(&window); + let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; + run_command( init_command, &mut runtime, &mut clipboard, &mut proxy, &window, + &compositor.get_information(), ); runtime.track(subscription); - let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; - let (mut sender, receiver) = mpsc::unbounded(); let mut instance = Box::pin(run_instance::( @@ -262,6 +263,8 @@ async fn run_instance( physical_size.height, ); + let graphics_info = compositor.get_information(); + let mut user_interface = ManuallyDrop::new(build_user_interface( &mut application, user_interface::Cache::default(), @@ -317,6 +320,7 @@ async fn run_instance( &mut debug, &mut messages, &window, + &graphics_info, ); // Update window @@ -516,6 +520,7 @@ pub fn update( debug: &mut Debug, messages: &mut Vec, window: &winit::window::Window, + graphics_info: &window::GraphicsInformation, ) { for message in messages.drain(..) { debug.log_message(&message); @@ -524,7 +529,7 @@ pub fn update( let command = runtime.enter(|| application.update(message)); debug.update_finished(); - run_command(command, runtime, clipboard, proxy, window); + run_command(command, runtime, clipboard, proxy, window, graphics_info); } let subscription = application.subscription(); @@ -538,6 +543,7 @@ pub fn run_command( clipboard: &mut Clipboard, proxy: &mut winit::event_loop::EventLoopProxy, window: &winit::window::Window, + graphics_info: &window::GraphicsInformation, ) { use iced_native::command; use iced_native::system; @@ -591,6 +597,8 @@ pub fn run_command( cpu_brand: cpu.brand().into(), cpu_cores: system.physical_core_count(), memory_total: system.total_memory(), + graphics_adapter: graphics_info.adapter.clone(), + graphics_backend: graphics_info.backend.clone(), }; Some(information) From 75281d2b8574ffdab6573bd37b8ac501b59f046d Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 00:59:50 -0300 Subject: [PATCH 18/37] Add graphics information to `iced_glutin` --- glutin/src/application.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 148a8e5084..5e29ddc849 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -130,6 +130,7 @@ where &mut clipboard, &mut proxy, context.window(), + &compositor.get_information(), ); runtime.track(subscription); @@ -208,6 +209,7 @@ async fn run_instance( let mut state = application::State::new(&application, context.window()); let mut viewport_version = state.viewport_version(); + let graphics_info = compositor.get_information(); let mut user_interface = ManuallyDrop::new(application::build_user_interface( &mut application, @@ -264,6 +266,7 @@ async fn run_instance( &mut debug, &mut messages, context.window(), + &graphics_info, ); // Update window From 56065fe95968459c9b507715e87971c4c13ce2d5 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 01:02:31 -0300 Subject: [PATCH 19/37] Add graphics information to example --- examples/system_information/src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 95f57ac02c..eac762129e 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -102,6 +102,16 @@ impl Application for Example { ) )); + let graphics_adapter = Text::new(format!( + "Graphics adapter: {}", + information.graphics_adapter + )); + + let graphics_backend = Text::new(format!( + "Graphics backend: {}", + information.graphics_backend + )); + Column::with_children(vec![ system_name.into(), system_kernel.into(), @@ -109,6 +119,8 @@ impl Application for Example { cpu_brand.into(), cpu_cores.into(), memory_total.into(), + graphics_adapter.into(), + graphics_backend.into(), ]) .into() } From 5bfe887e3d4acb2c48cca22c73aa52f4cc4856ad Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 02:47:50 -0300 Subject: [PATCH 20/37] Improve example readability --- examples/system_information/src/main.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index eac762129e..e0e7db09a3 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -113,15 +113,16 @@ impl Application for Example { )); Column::with_children(vec![ - system_name.into(), - system_kernel.into(), - system_version.into(), - cpu_brand.into(), - cpu_cores.into(), - memory_total.into(), - graphics_adapter.into(), - graphics_backend.into(), + system_name.size(30).into(), + system_kernel.size(30).into(), + system_version.size(30).into(), + cpu_brand.size(30).into(), + cpu_cores.size(30).into(), + memory_total.size(30).into(), + graphics_adapter.size(30).into(), + graphics_backend.size(30).into(), ]) + .spacing(10) .into() } Example::Unsupported => Text::new("Unsupported!").size(20).into(), From c9ea1f11dec96df04ade463ea9f33062a85c9219 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 14 Apr 2022 02:11:43 -0300 Subject: [PATCH 21/37] Add memory usage to `Information` struct --- examples/system_information/src/main.rs | 43 +++++++++++++++++++++---- native/src/system/information.rs | 4 ++- winit/src/application.rs | 12 ++++++- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index e0e7db09a3..79033e4c72 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - executor, system, Application, Column, Command, Container, Element, Length, - Settings, Text, + button, executor, system, Application, Button, Column, Command, Container, + Element, Length, Settings, Text, }; use bytesize::ByteSize; @@ -11,13 +11,17 @@ pub fn main() -> iced::Result { enum Example { Loading, - Loaded { information: system::Information }, + Loaded { + information: system::Information, + refresh_button: button::State, + }, Unsupported, } -#[derive(Debug)] +#[derive(Clone, Debug)] enum Message { InformationReceived(Option), + Refresh, } impl Application for Example { @@ -38,9 +42,16 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { match message { + Message::Refresh => { + return system::information(Message::InformationReceived); + } Message::InformationReceived(information) => { if let Some(information) = information { - *self = Self::Loaded { information }; + let refresh_button = button::State::new(); + *self = Self::Loaded { + information, + refresh_button, + }; } else { *self = Self::Unsupported; } @@ -53,7 +64,10 @@ impl Application for Example { fn view(&mut self) -> Element { let content: Element = match self { Example::Loading => Text::new("Loading...").size(40).into(), - Example::Loaded { information } => { + Example::Loaded { + information, + refresh_button, + } => { let system_name = Text::new(format!( "System name: {}", information @@ -102,6 +116,19 @@ impl Application for Example { ) )); + let memory_text = if let Some(memory_used) = + information.memory_used + { + let memory_readable = ByteSize::kb(memory_used).to_string(); + + format!("{} kb ({})", memory_used, memory_readable) + } else { + String::from("None") + }; + + let memory_used = + Text::new(format!("Memory (used): {}", memory_text)); + let graphics_adapter = Text::new(format!( "Graphics adapter: {}", information.graphics_adapter @@ -119,8 +146,12 @@ impl Application for Example { cpu_brand.size(30).into(), cpu_cores.size(30).into(), memory_total.size(30).into(), + memory_used.size(30).into(), graphics_adapter.size(30).into(), graphics_backend.size(30).into(), + Button::new(refresh_button, Text::new("Refresh")) + .on_press(Message::Refresh) + .into(), ]) .spacing(10) .into() diff --git a/native/src/system/information.rs b/native/src/system/information.rs index bf0b100cd6..fa4a835b23 100644 --- a/native/src/system/information.rs +++ b/native/src/system/information.rs @@ -1,5 +1,5 @@ /// Contains informations about the system (e.g. system name, processor, memory, graphics adapter). -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct Information { /// Contains the system name. pub system_name: Option, @@ -13,6 +13,8 @@ pub struct Information { pub cpu_cores: Option, /// Contains the total RAM size in KB. pub memory_total: u64, + /// Contains the system used RAM size in KB. + pub memory_used: Option, /// Contains the graphics backend. pub graphics_backend: String, /// Contains the graphics adapter. diff --git a/winit/src/application.rs b/winit/src/application.rs index 9d65d76974..1865f34427 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -584,12 +584,21 @@ pub fn run_command( system::Action::QueryInformation(tag) => { #[cfg(feature = "sysinfo")] let information = { - use sysinfo::{ProcessorExt, System, SystemExt}; + use sysinfo::{ + ProcessExt, ProcessorExt, System, SystemExt, + }; let mut system = System::new_all(); system.refresh_all(); let cpu = system.global_processor_info(); + let memory_used = sysinfo::get_current_pid() + .and_then(|pid| { + system.process(pid).ok_or("Process not found") + }) + .and_then(|process| Ok(process.memory())) + .ok(); + let information = system::Information { system_name: system.name(), system_kernel: system.kernel_version(), @@ -597,6 +606,7 @@ pub fn run_command( cpu_brand: cpu.brand().into(), cpu_cores: system.physical_core_count(), memory_total: system.total_memory(), + memory_used, graphics_adapter: graphics_info.adapter.clone(), graphics_backend: graphics_info.backend.clone(), }; From 053f352f68863e31a4576b8462a54b4e65f629d9 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 14 Apr 2022 11:15:54 -0300 Subject: [PATCH 22/37] Introduce `get_information` --- winit/src/application.rs | 36 ++---------------------------------- winit/src/system.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/winit/src/application.rs b/winit/src/application.rs index 1865f34427..37a0d58409 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -582,40 +582,8 @@ pub fn run_command( }, command::Action::System(action) => match action { system::Action::QueryInformation(tag) => { - #[cfg(feature = "sysinfo")] - let information = { - use sysinfo::{ - ProcessExt, ProcessorExt, System, SystemExt, - }; - let mut system = System::new_all(); - system.refresh_all(); - - let cpu = system.global_processor_info(); - - let memory_used = sysinfo::get_current_pid() - .and_then(|pid| { - system.process(pid).ok_or("Process not found") - }) - .and_then(|process| Ok(process.memory())) - .ok(); - - let information = system::Information { - system_name: system.name(), - system_kernel: system.kernel_version(), - system_version: system.long_os_version(), - cpu_brand: cpu.brand().into(), - cpu_cores: system.physical_core_count(), - memory_total: system.total_memory(), - memory_used, - graphics_adapter: graphics_info.adapter.clone(), - graphics_backend: graphics_info.backend.clone(), - }; - - Some(information) - }; - - #[cfg(not(feature = "sysinfo"))] - let information = None; + let information = + crate::system::get_information(graphics_info); let message = tag(information); diff --git a/winit/src/system.rs b/winit/src/system.rs index 3d1b40220a..7e1c17c8bf 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -2,6 +2,8 @@ use crate::command::{self, Command}; pub use iced_native::system::*; +use iced_graphics::window; + /// Query for available system information. /// /// Returns `None` if not using the `sysinfo` feature flag. @@ -12,3 +14,40 @@ pub fn information( Box::new(f), ))) } + +#[cfg(feature = "sysinfo")] +pub(crate) fn get_information( + graphics_info: &window::GraphicsInformation, +) -> Option { + use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt}; + let mut system = System::new_all(); + system.refresh_all(); + + let cpu = system.global_processor_info(); + + let memory_used = sysinfo::get_current_pid() + .and_then(|pid| system.process(pid).ok_or("Process not found")) + .and_then(|process| Ok(process.memory())) + .ok(); + + let information = Information { + system_name: system.name(), + system_kernel: system.kernel_version(), + system_version: system.long_os_version(), + cpu_brand: cpu.brand().into(), + cpu_cores: system.physical_core_count(), + memory_total: system.total_memory(), + memory_used, + graphics_adapter: graphics_info.adapter.clone(), + graphics_backend: graphics_info.backend.clone(), + }; + + Some(information) +} + +#[cfg(not(feature = "sysinfo"))] +pub(crate) fn get_information( + _graphics_info: &window::GraphicsInformation, +) -> Option { + None +} From 7a13928130803629b15999923da3a9747e110e7a Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Apr 2022 19:02:31 -0300 Subject: [PATCH 23/37] Enumerate adapters based on log level --- wgpu/src/window/compositor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index e283c1e2a1..d7decccde2 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -32,7 +32,7 @@ impl Compositor { log::info!("{:#?}", settings); #[cfg(not(target_arch = "wasm32"))] - { + if log::max_level() >= log::LevelFilter::Info { let available_adapters: Vec<_> = instance .enumerate_adapters(settings.internal_backend) .map(|adapter| adapter.get_info()) From 5be1ac18fe1757d31386f98774d823bd1137eea4 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Apr 2022 19:09:09 -0300 Subject: [PATCH 24/37] Rename `GraphicsInformation` to `Information` --- glow/src/window/compositor.rs | 4 ++-- graphics/src/window.rs | 2 +- graphics/src/window/compositor.rs | 4 ++-- graphics/src/window/gl_compositor.rs | 4 ++-- wgpu/src/window/compositor.rs | 4 ++-- winit/src/application.rs | 4 ++-- winit/src/system.rs | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/glow/src/window/compositor.rs b/glow/src/window/compositor.rs index f2740f6c83..402461bbca 100644 --- a/glow/src/window/compositor.rs +++ b/glow/src/window/compositor.rs @@ -67,10 +67,10 @@ impl iced_graphics::window::GLCompositor for Compositor { } } - fn get_information(&self) -> iced_graphics::window::GraphicsInformation { + fn get_information(&self) -> iced_graphics::window::Information { let adapter = unsafe { self.gl.get_parameter_string(glow::RENDERER) }; - iced_graphics::window::GraphicsInformation { + iced_graphics::window::Information { backend: format!("{:?}", self.gl.version()), adapter, } diff --git a/graphics/src/window.rs b/graphics/src/window.rs index c73c8c5332..35a9f06ac9 100644 --- a/graphics/src/window.rs +++ b/graphics/src/window.rs @@ -4,7 +4,7 @@ mod compositor; #[cfg(feature = "opengl")] mod gl_compositor; -pub use compositor::{Compositor, GraphicsInformation, SurfaceError}; +pub use compositor::{Compositor, Information, SurfaceError}; #[cfg(feature = "opengl")] pub use gl_compositor::GLCompositor; diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 035101c9de..8a851773bb 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -39,7 +39,7 @@ pub trait Compositor: Sized { ); /// Returns [`GraphicsInformation`] used by this [`Compositor`]. - fn get_information(&self) -> GraphicsInformation; + fn get_information(&self) -> Information; /// Presents the [`Renderer`] primitives to the next frame of the given [`Surface`]. /// @@ -77,7 +77,7 @@ pub enum SurfaceError { /// Contains informations about the graphics (e.g. graphics adapter, graphics backend). #[derive(Debug)] -pub struct GraphicsInformation { +pub struct Information { /// Contains the graphics adapter. pub adapter: String, /// Contains the graphics backend. diff --git a/graphics/src/window/gl_compositor.rs b/graphics/src/window/gl_compositor.rs index 23c6d3f8be..65761fe8a5 100644 --- a/graphics/src/window/gl_compositor.rs +++ b/graphics/src/window/gl_compositor.rs @@ -1,4 +1,4 @@ -use crate::window::GraphicsInformation; +use crate::window::Information; use crate::{Color, Error, Size, Viewport}; use core::ffi::c_void; @@ -50,7 +50,7 @@ pub trait GLCompositor: Sized { fn resize_viewport(&mut self, physical_size: Size); /// Returns [`GraphicsInformation`] used by this [`Compositor`]. - fn get_information(&self) -> GraphicsInformation; + fn get_information(&self) -> Information; /// Presents the primitives of the [`Renderer`] to the next frame of the /// [`GLCompositor`]. diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index d7decccde2..01e5216250 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -157,10 +157,10 @@ impl iced_graphics::window::Compositor for Compositor { ); } - fn get_information(&self) -> iced_graphics::window::GraphicsInformation { + fn get_information(&self) -> iced_graphics::window::Information { let information = self.adapter.get_info(); - iced_graphics::window::GraphicsInformation { + iced_graphics::window::Information { adapter: information.name, backend: format!("{:?}", information.backend), } diff --git a/winit/src/application.rs b/winit/src/application.rs index 37a0d58409..f5e4c66de7 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -520,7 +520,7 @@ pub fn update( debug: &mut Debug, messages: &mut Vec, window: &winit::window::Window, - graphics_info: &window::GraphicsInformation, + graphics_info: &window::Information, ) { for message in messages.drain(..) { debug.log_message(&message); @@ -543,7 +543,7 @@ pub fn run_command( clipboard: &mut Clipboard, proxy: &mut winit::event_loop::EventLoopProxy, window: &winit::window::Window, - graphics_info: &window::GraphicsInformation, + graphics_info: &window::Information, ) { use iced_native::command; use iced_native::system; diff --git a/winit/src/system.rs b/winit/src/system.rs index 7e1c17c8bf..1305ba5bff 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -17,7 +17,7 @@ pub fn information( #[cfg(feature = "sysinfo")] pub(crate) fn get_information( - graphics_info: &window::GraphicsInformation, + graphics_info: &window::Information, ) -> Option { use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt}; let mut system = System::new_all(); @@ -47,7 +47,7 @@ pub(crate) fn get_information( #[cfg(not(feature = "sysinfo"))] pub(crate) fn get_information( - _graphics_info: &window::GraphicsInformation, + _graphics_info: &window::Information, ) -> Option { None } From 984d1f375ecec301dd42b049eecd1b88e3bca32a Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Apr 2022 19:18:18 -0300 Subject: [PATCH 25/37] Move `compositor` module access from `window` to `crate` --- glow/src/window/compositor.rs | 6 +++--- graphics/src/lib.rs | 1 + graphics/src/window.rs | 6 +++--- graphics/src/window/compositor.rs | 2 ++ graphics/src/window/gl_compositor.rs | 4 +++- wgpu/src/window/compositor.rs | 17 ++++++++--------- winit/src/application.rs | 7 ++++--- winit/src/system.rs | 6 +++--- 8 files changed, 27 insertions(+), 22 deletions(-) diff --git a/glow/src/window/compositor.rs b/glow/src/window/compositor.rs index 402461bbca..68d27d3672 100644 --- a/glow/src/window/compositor.rs +++ b/glow/src/window/compositor.rs @@ -2,7 +2,7 @@ use crate::{Backend, Color, Error, Renderer, Settings, Viewport}; use core::ffi::c_void; use glow::HasContext; -use iced_graphics::{Antialiasing, Size}; +use iced_graphics::{compositor, Antialiasing, Size}; /// A window graphics backend for iced powered by `glow`. #[allow(missing_debug_implementations)] @@ -67,10 +67,10 @@ impl iced_graphics::window::GLCompositor for Compositor { } } - fn get_information(&self) -> iced_graphics::window::Information { + fn get_information(&self) -> compositor::Information { let adapter = unsafe { self.gl.get_parameter_string(glow::RENDERER) }; - iced_graphics::window::Information { + compositor::Information { backend: format!("{:?}", self.gl.version()), adapter, } diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index b3be62afab..4364695f49 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -39,6 +39,7 @@ pub use primitive::Primitive; pub use renderer::Renderer; pub use transformation::Transformation; pub use viewport::Viewport; +pub use window::compositor; pub use iced_native::alignment; pub use iced_native::{ diff --git a/graphics/src/window.rs b/graphics/src/window.rs index 35a9f06ac9..a38b81f36a 100644 --- a/graphics/src/window.rs +++ b/graphics/src/window.rs @@ -1,10 +1,10 @@ //! Draw graphics to window surfaces. -mod compositor; +pub mod compositor; #[cfg(feature = "opengl")] -mod gl_compositor; +pub mod gl_compositor; -pub use compositor::{Compositor, Information, SurfaceError}; +pub use compositor::Compositor; #[cfg(feature = "opengl")] pub use gl_compositor::GLCompositor; diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 8a851773bb..3f1165d41d 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -1,3 +1,5 @@ +//! A compositor is responsible for initializing a renderer and managing window +//! surfaces. use crate::{Color, Error, Viewport}; use raw_window_handle::HasRawWindowHandle; diff --git a/graphics/src/window/gl_compositor.rs b/graphics/src/window/gl_compositor.rs index 65761fe8a5..3adde8e610 100644 --- a/graphics/src/window/gl_compositor.rs +++ b/graphics/src/window/gl_compositor.rs @@ -1,4 +1,6 @@ -use crate::window::Information; +//! A compositor is responsible for initializing a renderer and managing window +//! surfaces. +use crate::compositor::Information; use crate::{Color, Error, Size, Viewport}; use core::ffi::c_void; diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 01e5216250..04393fe5f1 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -1,6 +1,7 @@ use crate::{Backend, Color, Error, Renderer, Settings, Viewport}; use futures::task::SpawnExt; +use iced_graphics::compositor; use iced_native::futures; use raw_window_handle::HasRawWindowHandle; @@ -157,10 +158,10 @@ impl iced_graphics::window::Compositor for Compositor { ); } - fn get_information(&self) -> iced_graphics::window::Information { + fn get_information(&self) -> compositor::Information { let information = self.adapter.get_info(); - iced_graphics::window::Information { + compositor::Information { adapter: information.name, backend: format!("{:?}", information.backend), } @@ -173,7 +174,7 @@ impl iced_graphics::window::Compositor for Compositor { viewport: &Viewport, background_color: Color, overlay: &[T], - ) -> Result<(), iced_graphics::window::SurfaceError> { + ) -> Result<(), compositor::SurfaceError> { match surface.get_current_texture() { Ok(frame) => { let mut encoder = self.device.create_command_encoder( @@ -241,16 +242,14 @@ impl iced_graphics::window::Compositor for Compositor { } Err(error) => match error { wgpu::SurfaceError::Timeout => { - Err(iced_graphics::window::SurfaceError::Timeout) + Err(compositor::SurfaceError::Timeout) } wgpu::SurfaceError::Outdated => { - Err(iced_graphics::window::SurfaceError::Outdated) - } - wgpu::SurfaceError::Lost => { - Err(iced_graphics::window::SurfaceError::Lost) + Err(compositor::SurfaceError::Outdated) } + wgpu::SurfaceError::Lost => Err(compositor::SurfaceError::Lost), wgpu::SurfaceError::OutOfMemory => { - Err(iced_graphics::window::SurfaceError::OutOfMemory) + Err(compositor::SurfaceError::OutOfMemory) } }, } diff --git a/winit/src/application.rs b/winit/src/application.rs index f5e4c66de7..853ede0c46 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -13,6 +13,7 @@ use crate::{ use iced_futures::futures; use iced_futures::futures::channel::mpsc; +use iced_graphics::compositor; use iced_graphics::window; use iced_native::program::Program; use iced_native::user_interface::{self, UserInterface}; @@ -426,7 +427,7 @@ async fn run_instance( } Err(error) => match error { // This is an unrecoverable error. - window::SurfaceError::OutOfMemory => { + compositor::SurfaceError::OutOfMemory => { panic!("{:?}", error); } _ => { @@ -520,7 +521,7 @@ pub fn update( debug: &mut Debug, messages: &mut Vec, window: &winit::window::Window, - graphics_info: &window::Information, + graphics_info: &compositor::Information, ) { for message in messages.drain(..) { debug.log_message(&message); @@ -543,7 +544,7 @@ pub fn run_command( clipboard: &mut Clipboard, proxy: &mut winit::event_loop::EventLoopProxy, window: &winit::window::Window, - graphics_info: &window::Information, + graphics_info: &compositor::Information, ) { use iced_native::command; use iced_native::system; diff --git a/winit/src/system.rs b/winit/src/system.rs index 1305ba5bff..a961c5b4ab 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -2,7 +2,7 @@ use crate::command::{self, Command}; pub use iced_native::system::*; -use iced_graphics::window; +use iced_graphics::compositor; /// Query for available system information. /// @@ -17,7 +17,7 @@ pub fn information( #[cfg(feature = "sysinfo")] pub(crate) fn get_information( - graphics_info: &window::Information, + graphics_info: &compositor::Information, ) -> Option { use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt}; let mut system = System::new_all(); @@ -47,7 +47,7 @@ pub(crate) fn get_information( #[cfg(not(feature = "sysinfo"))] pub(crate) fn get_information( - _graphics_info: &window::Information, + _graphics_info: &compositor::Information, ) -> Option { None } From 005e516b5e1e8bb22f2da8524ffe4529f3b60ba1 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Apr 2022 19:20:38 -0300 Subject: [PATCH 26/37] Rename `get_information` to `fetch_information` --- glow/src/window/compositor.rs | 2 +- glutin/src/application.rs | 4 ++-- graphics/src/window/compositor.rs | 2 +- graphics/src/window/gl_compositor.rs | 2 +- wgpu/src/window/compositor.rs | 2 +- winit/src/application.rs | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/glow/src/window/compositor.rs b/glow/src/window/compositor.rs index 68d27d3672..622d7fc0bb 100644 --- a/glow/src/window/compositor.rs +++ b/glow/src/window/compositor.rs @@ -67,7 +67,7 @@ impl iced_graphics::window::GLCompositor for Compositor { } } - fn get_information(&self) -> compositor::Information { + fn fetch_information(&self) -> compositor::Information { let adapter = unsafe { self.gl.get_parameter_string(glow::RENDERER) }; compositor::Information { diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 5e29ddc849..1400dcce2f 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -130,7 +130,7 @@ where &mut clipboard, &mut proxy, context.window(), - &compositor.get_information(), + &compositor.fetch_information(), ); runtime.track(subscription); @@ -209,7 +209,7 @@ async fn run_instance( let mut state = application::State::new(&application, context.window()); let mut viewport_version = state.viewport_version(); - let graphics_info = compositor.get_information(); + let graphics_info = compositor.fetch_information(); let mut user_interface = ManuallyDrop::new(application::build_user_interface( &mut application, diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 3f1165d41d..7525de1d61 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -41,7 +41,7 @@ pub trait Compositor: Sized { ); /// Returns [`GraphicsInformation`] used by this [`Compositor`]. - fn get_information(&self) -> Information; + fn fetch_information(&self) -> Information; /// Presents the [`Renderer`] primitives to the next frame of the given [`Surface`]. /// diff --git a/graphics/src/window/gl_compositor.rs b/graphics/src/window/gl_compositor.rs index 3adde8e610..4ff17366a3 100644 --- a/graphics/src/window/gl_compositor.rs +++ b/graphics/src/window/gl_compositor.rs @@ -52,7 +52,7 @@ pub trait GLCompositor: Sized { fn resize_viewport(&mut self, physical_size: Size); /// Returns [`GraphicsInformation`] used by this [`Compositor`]. - fn get_information(&self) -> Information; + fn fetch_information(&self) -> Information; /// Presents the primitives of the [`Renderer`] to the next frame of the /// [`GLCompositor`]. diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 04393fe5f1..54ea82474b 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -158,7 +158,7 @@ impl iced_graphics::window::Compositor for Compositor { ); } - fn get_information(&self) -> compositor::Information { + fn fetch_information(&self) -> compositor::Information { let information = self.adapter.get_info(); compositor::Information { diff --git a/winit/src/application.rs b/winit/src/application.rs index 853ede0c46..59f0624c4a 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -176,7 +176,7 @@ where &mut clipboard, &mut proxy, &window, - &compositor.get_information(), + &compositor.fetch_information(), ); runtime.track(subscription); @@ -264,7 +264,7 @@ async fn run_instance( physical_size.height, ); - let graphics_info = compositor.get_information(); + let graphics_info = compositor.fetch_information(); let mut user_interface = ManuallyDrop::new(build_user_interface( &mut application, From 18ecec2bbd9478151b8c65df0644278a174d0847 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Apr 2022 19:28:04 -0300 Subject: [PATCH 27/37] Change `ContextCreationFailed` to `GraphicsCreationFailed` --- glutin/src/application.rs | 6 +++--- src/error.rs | 10 +++++----- winit/src/error.rs | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 1400dcce2f..61f3ecd57e 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -94,16 +94,16 @@ where Error::WindowCreationFailed(error) } CreationError::OpenGlVersionNotSupported => { - Error::ContextCreationFailed( + Error::GraphicsCreationFailed( ContextError::VersionNotSupported, ) } CreationError::NoAvailablePixelFormat => { - Error::ContextCreationFailed( + Error::GraphicsCreationFailed( ContextError::NoAvailablePixelFormat, ) } - error => Error::ContextCreationFailed( + error => Error::GraphicsCreationFailed( ContextError::BackendError(error.to_string()), ), } diff --git a/src/error.rs b/src/error.rs index 83c9b1b643..0bfa3ff1c4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,9 +11,9 @@ pub enum Error { #[error("the application window could not be created")] WindowCreationFailed(Box), - /// The application context could not be created. - #[error("the application context could not be created")] - ContextCreationFailed(iced_graphics::Error), + /// The application graphics context could not be created. + #[error("the application graphics context could not be created")] + GraphicsCreationFailed(iced_graphics::Error), } impl From for Error { @@ -25,8 +25,8 @@ impl From for Error { iced_winit::Error::WindowCreationFailed(error) => { Error::WindowCreationFailed(Box::new(error)) } - iced_winit::Error::ContextCreationFailed(error) => { - Error::ContextCreationFailed(error) + iced_winit::Error::GraphicsCreationFailed(error) => { + Error::GraphicsCreationFailed(error) } } } diff --git a/winit/src/error.rs b/winit/src/error.rs index 2dd045d29d..eaeafd51d4 100644 --- a/winit/src/error.rs +++ b/winit/src/error.rs @@ -11,13 +11,13 @@ pub enum Error { #[error("the application window could not be created")] WindowCreationFailed(winit::error::OsError), - /// The application context could not be created. - #[error("the application context could not be created")] - ContextCreationFailed(iced_graphics::Error), + /// The application graphics context could not be created. + #[error("the application graphics context could not be created")] + GraphicsCreationFailed(iced_graphics::Error), } impl From for Error { fn from(error: iced_graphics::Error) -> Error { - Error::ContextCreationFailed(error) + Error::GraphicsCreationFailed(error) } } From 8643fbef90fc16371e27ae0142eb7e4c6b432d29 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Apr 2022 19:35:43 -0300 Subject: [PATCH 28/37] Rename `system::information` to `fetch_information` --- examples/system_information/src/main.rs | 4 ++-- winit/src/system.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 79033e4c72..1e61480f87 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -32,7 +32,7 @@ impl Application for Example { fn new(_flags: ()) -> (Self, Command) { ( Self::Loading, - system::information(Message::InformationReceived), + system::fetch_information(Message::InformationReceived), ) } @@ -43,7 +43,7 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { match message { Message::Refresh => { - return system::information(Message::InformationReceived); + return system::fetch_information(Message::InformationReceived); } Message::InformationReceived(information) => { if let Some(information) = information { diff --git a/winit/src/system.rs b/winit/src/system.rs index a961c5b4ab..f5f6859323 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -7,7 +7,7 @@ use iced_graphics::compositor; /// Query for available system information. /// /// Returns `None` if not using the `sysinfo` feature flag. -pub fn information( +pub fn fetch_information( f: impl Fn(Option) -> Message + 'static, ) -> Command { Command::single(command::Action::System(Action::QueryInformation( From 6e167675d6a51a8a78d93439719ebffe35dcfdef Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 27 Apr 2022 15:40:29 -0300 Subject: [PATCH 29/37] Use closure internally to fetch `graphics_info` --- glutin/src/application.rs | 5 ++--- winit/src/application.rs | 12 +++++------- winit/src/system.rs | 8 ++++---- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 61f3ecd57e..dbc9b58003 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -130,7 +130,7 @@ where &mut clipboard, &mut proxy, context.window(), - &compositor.fetch_information(), + || compositor.fetch_information(), ); runtime.track(subscription); @@ -209,7 +209,6 @@ async fn run_instance( let mut state = application::State::new(&application, context.window()); let mut viewport_version = state.viewport_version(); - let graphics_info = compositor.fetch_information(); let mut user_interface = ManuallyDrop::new(application::build_user_interface( &mut application, @@ -266,7 +265,7 @@ async fn run_instance( &mut debug, &mut messages, context.window(), - &graphics_info, + || compositor.fetch_information(), ); // Update window diff --git a/winit/src/application.rs b/winit/src/application.rs index 59f0624c4a..9d881475ce 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -176,7 +176,7 @@ where &mut clipboard, &mut proxy, &window, - &compositor.fetch_information(), + || compositor.fetch_information(), ); runtime.track(subscription); @@ -264,8 +264,6 @@ async fn run_instance( physical_size.height, ); - let graphics_info = compositor.fetch_information(); - let mut user_interface = ManuallyDrop::new(build_user_interface( &mut application, user_interface::Cache::default(), @@ -321,7 +319,7 @@ async fn run_instance( &mut debug, &mut messages, &window, - &graphics_info, + || compositor.fetch_information(), ); // Update window @@ -521,7 +519,7 @@ pub fn update( debug: &mut Debug, messages: &mut Vec, window: &winit::window::Window, - graphics_info: &compositor::Information, + graphics_info: impl FnOnce() -> compositor::Information + Copy, ) { for message in messages.drain(..) { debug.log_message(&message); @@ -544,7 +542,7 @@ pub fn run_command( clipboard: &mut Clipboard, proxy: &mut winit::event_loop::EventLoopProxy, window: &winit::window::Window, - graphics_info: &compositor::Information, + graphics_info: impl FnOnce() -> compositor::Information + Copy, ) { use iced_native::command; use iced_native::system; @@ -584,7 +582,7 @@ pub fn run_command( command::Action::System(action) => match action { system::Action::QueryInformation(tag) => { let information = - crate::system::get_information(graphics_info); + crate::system::get_information(graphics_info()); let message = tag(information); diff --git a/winit/src/system.rs b/winit/src/system.rs index f5f6859323..beda2bdd5c 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -17,7 +17,7 @@ pub fn fetch_information( #[cfg(feature = "sysinfo")] pub(crate) fn get_information( - graphics_info: &compositor::Information, + graphics_info: compositor::Information, ) -> Option { use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt}; let mut system = System::new_all(); @@ -38,8 +38,8 @@ pub(crate) fn get_information( cpu_cores: system.physical_core_count(), memory_total: system.total_memory(), memory_used, - graphics_adapter: graphics_info.adapter.clone(), - graphics_backend: graphics_info.backend.clone(), + graphics_adapter: graphics_info.adapter, + graphics_backend: graphics_info.backend, }; Some(information) @@ -47,7 +47,7 @@ pub(crate) fn get_information( #[cfg(not(feature = "sysinfo"))] pub(crate) fn get_information( - _graphics_info: &compositor::Information, + _graphics_info: compositor::Information, ) -> Option { None } From 5eefa5d4ead9ebfac7dab1db9aebf9797d2dad38 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 27 Apr 2022 16:18:27 -0300 Subject: [PATCH 30/37] Simplify the `QueryInformation` Action --- Cargo.toml | 2 -- examples/system_information/Cargo.toml | 2 +- examples/system_information/src/main.rs | 18 ++++++------------ native/src/system/action.rs | 2 +- winit/Cargo.toml | 2 +- winit/src/application.rs | 2 +- winit/src/system.rs | 22 +++++----------------- 7 files changed, 15 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 500fa06ee4..21bb6dd294 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,8 +42,6 @@ smol = ["iced_futures/smol"] palette = ["iced_core/palette"] # Enables pure, virtual widgets in the `pure` module pure = ["iced_pure", "iced_graphics/pure"] -# Enables system information querying `Action` -sysinfo = ["iced_winit/sysinfo"] [badges] maintenance = { status = "actively-developed" } diff --git a/examples/system_information/Cargo.toml b/examples/system_information/Cargo.toml index d3d7618225..13a59d5ef6 100644 --- a/examples/system_information/Cargo.toml +++ b/examples/system_information/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["sysinfo"] } +iced = { path = "../.." } bytesize = { version = "1.1.0" } \ No newline at end of file diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 1e61480f87..704f5f4d82 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -15,12 +15,11 @@ enum Example { information: system::Information, refresh_button: button::State, }, - Unsupported, } #[derive(Clone, Debug)] enum Message { - InformationReceived(Option), + InformationReceived(system::Information), Refresh, } @@ -46,15 +45,11 @@ impl Application for Example { return system::fetch_information(Message::InformationReceived); } Message::InformationReceived(information) => { - if let Some(information) = information { - let refresh_button = button::State::new(); - *self = Self::Loaded { - information, - refresh_button, - }; - } else { - *self = Self::Unsupported; - } + let refresh_button = button::State::new(); + *self = Self::Loaded { + information, + refresh_button, + }; } } @@ -156,7 +151,6 @@ impl Application for Example { .spacing(10) .into() } - Example::Unsupported => Text::new("Unsupported!").size(20).into(), }; Container::new(content) diff --git a/native/src/system/action.rs b/native/src/system/action.rs index 3bece0bb26..6dab20a699 100644 --- a/native/src/system/action.rs +++ b/native/src/system/action.rs @@ -7,7 +7,7 @@ use std::fmt; /// An operation to be performed on the system. pub enum Action { /// Query system information and produce `T` with the result. - QueryInformation(Box) -> T>), + QueryInformation(Box T>), } impl Action { diff --git a/winit/Cargo.toml b/winit/Cargo.toml index b75831490e..f5478e7334 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -17,7 +17,7 @@ debug = ["iced_native/debug"] window_clipboard = "0.2" log = "0.4" thiserror = "1.0" -sysinfo = { version = "0.23", optional = true } +sysinfo = "0.23" [dependencies.winit] version = "0.26" diff --git a/winit/src/application.rs b/winit/src/application.rs index 9d881475ce..04dd55f171 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -582,7 +582,7 @@ pub fn run_command( command::Action::System(action) => match action { system::Action::QueryInformation(tag) => { let information = - crate::system::get_information(graphics_info()); + crate::system::information(graphics_info()); let message = tag(information); diff --git a/winit/src/system.rs b/winit/src/system.rs index beda2bdd5c..fa4ad1a350 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -5,20 +5,17 @@ pub use iced_native::system::*; use iced_graphics::compositor; /// Query for available system information. -/// -/// Returns `None` if not using the `sysinfo` feature flag. pub fn fetch_information( - f: impl Fn(Option) -> Message + 'static, + f: impl Fn(Information) -> Message + 'static, ) -> Command { Command::single(command::Action::System(Action::QueryInformation( Box::new(f), ))) } -#[cfg(feature = "sysinfo")] -pub(crate) fn get_information( +pub(crate) fn information( graphics_info: compositor::Information, -) -> Option { +) -> Information { use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt}; let mut system = System::new_all(); system.refresh_all(); @@ -30,7 +27,7 @@ pub(crate) fn get_information( .and_then(|process| Ok(process.memory())) .ok(); - let information = Information { + Information { system_name: system.name(), system_kernel: system.kernel_version(), system_version: system.long_os_version(), @@ -40,14 +37,5 @@ pub(crate) fn get_information( memory_used, graphics_adapter: graphics_info.adapter, graphics_backend: graphics_info.backend, - }; - - Some(information) -} - -#[cfg(not(feature = "sysinfo"))] -pub(crate) fn get_information( - _graphics_info: compositor::Information, -) -> Option { - None + } } From 93bfe2c75ec97ef78f993926c703f040dde4a5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 30 Apr 2022 13:37:57 +0200 Subject: [PATCH 31/37] Expose `system` module through feature flag --- Cargo.toml | 2 ++ examples/system_information/Cargo.toml | 4 ++-- glutin/Cargo.toml | 1 + glutin/src/lib.rs | 4 +++- src/lib.rs | 4 +++- winit/Cargo.toml | 6 +++++- winit/src/application.rs | 19 +++++++++++-------- winit/src/lib.rs | 4 +++- 8 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 21bb6dd294..75e573527e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,8 @@ smol = ["iced_futures/smol"] palette = ["iced_core/palette"] # Enables pure, virtual widgets in the `pure` module pure = ["iced_pure", "iced_graphics/pure"] +# Enables querying system information +system = ["iced_winit/system"] [badges] maintenance = { status = "actively-developed" } diff --git a/examples/system_information/Cargo.toml b/examples/system_information/Cargo.toml index 13a59d5ef6..7d1e4b94eb 100644 --- a/examples/system_information/Cargo.toml +++ b/examples/system_information/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } -bytesize = { version = "1.1.0" } \ No newline at end of file +iced = { path = "../..", features = ["system"] } +bytesize = { version = "1.1.0" } diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index cd5f2a7a30..2d4625da82 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -12,6 +12,7 @@ categories = ["gui"] [features] debug = ["iced_winit/debug"] +system = ["iced_winit/system"] [dependencies.log] version = "0.4" diff --git a/glutin/src/lib.rs b/glutin/src/lib.rs index 9948603428..fb24b688bf 100644 --- a/glutin/src/lib.rs +++ b/glutin/src/lib.rs @@ -22,7 +22,6 @@ pub mod application; pub use iced_winit::clipboard; pub use iced_winit::conversion; pub use iced_winit::settings; -pub use iced_winit::system; pub use iced_winit::window; pub use iced_winit::{Error, Mode}; @@ -32,3 +31,6 @@ pub use application::Application; pub use clipboard::Clipboard; #[doc(no_inline)] pub use settings::Settings; + +#[cfg(feature = "system")] +pub use iced_winit::system; diff --git a/src/lib.rs b/src/lib.rs index c9a735e57d..cbda3c8786 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -224,8 +224,10 @@ pub use settings::Settings; pub use runtime::alignment; pub use runtime::futures; -pub use runtime::system; pub use runtime::{ Alignment, Background, Color, Command, ContentFit, Font, Length, Point, Rectangle, Size, Subscription, Vector, }; + +#[cfg(feature = "system")] +pub use runtime::system; diff --git a/winit/Cargo.toml b/winit/Cargo.toml index f5478e7334..7758899ce7 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -12,12 +12,12 @@ categories = ["gui"] [features] debug = ["iced_native/debug"] +system = ["sysinfo"] [dependencies] window_clipboard = "0.2" log = "0.4" thiserror = "1.0" -sysinfo = "0.23" [dependencies.winit] version = "0.26" @@ -42,3 +42,7 @@ version = "0.3.6" [target.'cfg(target_arch = "wasm32")'.dependencies.web-sys] version = "0.3" features = ["Document", "Window"] + +[dependencies.sysinfo] +version = "0.23" +optional = true diff --git a/winit/src/application.rs b/winit/src/application.rs index 04dd55f171..1b7a4c8d9c 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -542,7 +542,7 @@ pub fn run_command( clipboard: &mut Clipboard, proxy: &mut winit::event_loop::EventLoopProxy, window: &winit::window::Window, - graphics_info: impl FnOnce() -> compositor::Information + Copy, + _graphics_info: impl FnOnce() -> compositor::Information + Copy, ) { use iced_native::command; use iced_native::system; @@ -580,15 +580,18 @@ pub fn run_command( } }, command::Action::System(action) => match action { - system::Action::QueryInformation(tag) => { - let information = - crate::system::information(graphics_info()); + system::Action::QueryInformation(_tag) => { + #[cfg(feature = "sysinfo")] + { + let information = + crate::system::information(_graphics_info()); - let message = tag(information); + let message = _tag(information); - proxy - .send_event(message) - .expect("Send message to event loop"); + proxy + .send_event(message) + .expect("Send message to event loop"); + } } }, } diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 2b5bb2ff2b..4e5cb63711 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -31,9 +31,11 @@ pub mod application; pub mod clipboard; pub mod conversion; pub mod settings; -pub mod system; pub mod window; +#[cfg(feature = "system")] +pub mod system; + mod error; mod mode; mod position; From a4477723d44c35effc8f4bb0ff0cfce93fdf4695 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 27 Jan 2022 06:02:19 -0300 Subject: [PATCH 32/37] Add logging to window and context creation --- glow/src/settings.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/glow/src/settings.rs b/glow/src/settings.rs index f3dddfaf92..3691747bb8 100644 --- a/glow/src/settings.rs +++ b/glow/src/settings.rs @@ -4,7 +4,7 @@ pub use iced_graphics::Antialiasing; /// The settings of a [`Backend`]. /// /// [`Backend`]: crate::Backend -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct Settings { /// The bytes of the font that will be used by default. /// @@ -39,6 +39,18 @@ impl Default for Settings { } } +impl std::fmt::Debug for Settings { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Settings") + // Instead of printing the font bytes, we simply show a `bool` indicating if using a default font or not. + .field("default_font", &self.default_font.is_none()) + .field("default_text_size", &self.default_text_size) + .field("text_multithreading", &self.text_multithreading) + .field("antialiasing", &self.antialiasing) + .finish() + } +} + impl Settings { /// Creates new [`Settings`] using environment configuration. /// From e24f26c28f09916f04f536a52368dcd1a4f34ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 4 May 2022 14:02:54 +0200 Subject: [PATCH 33/37] Fix feature name in `cfg` block in `iced_winit` --- winit/src/application.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/winit/src/application.rs b/winit/src/application.rs index 1b7a4c8d9c..421ae398da 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -581,7 +581,7 @@ pub fn run_command( }, command::Action::System(action) => match action { system::Action::QueryInformation(_tag) => { - #[cfg(feature = "sysinfo")] + #[cfg(feature = "system")] { let information = crate::system::information(_graphics_info()); From f1ec0af5070fe2752967cdc38ed66b8b70882366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 4 May 2022 14:25:04 +0200 Subject: [PATCH 34/37] Run `system::information` in a different thread ... since it seems it can block for a couple of seconds. --- native/src/system/action.rs | 7 +++++-- winit/src/application.rs | 17 +++++++++++------ winit/src/system.rs | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/native/src/system/action.rs b/native/src/system/action.rs index 6dab20a699..dea9536f42 100644 --- a/native/src/system/action.rs +++ b/native/src/system/action.rs @@ -1,15 +1,18 @@ use crate::system; use iced_futures::MaybeSend; - use std::fmt; /// An operation to be performed on the system. pub enum Action { /// Query system information and produce `T` with the result. - QueryInformation(Box T>), + QueryInformation(Box>), } +pub trait Closure: Fn(system::Information) -> T + MaybeSend {} + +impl Closure for T where T: Fn(system::Information) -> O + MaybeSend {} + impl Action { /// Maps the output of a system [`Action`] using the provided closure. pub fn map( diff --git a/winit/src/application.rs b/winit/src/application.rs index 421ae398da..90b03d569b 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -583,14 +583,19 @@ pub fn run_command( system::Action::QueryInformation(_tag) => { #[cfg(feature = "system")] { - let information = - crate::system::information(_graphics_info()); + let graphics_info = _graphics_info(); + let proxy = proxy.clone(); - let message = _tag(information); + let _ = std::thread::spawn(move || { + let information = + crate::system::information(graphics_info); - proxy - .send_event(message) - .expect("Send message to event loop"); + let message = _tag(information); + + proxy + .send_event(message) + .expect("Send message to event loop") + }); } } }, diff --git a/winit/src/system.rs b/winit/src/system.rs index fa4ad1a350..0ed61dc965 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -6,7 +6,7 @@ use iced_graphics::compositor; /// Query for available system information. pub fn fetch_information( - f: impl Fn(Information) -> Message + 'static, + f: impl Fn(Information) -> Message + Send + 'static, ) -> Command { Command::single(command::Action::System(Action::QueryInformation( Box::new(f), From 1aeb8b8340400e28219c17d7d2ee2a441ae1cd71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 4 May 2022 14:25:44 +0200 Subject: [PATCH 35/37] Show `Loading` screen when refreshing in `system_information` example --- examples/system_information/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 704f5f4d82..560220b82e 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -42,6 +42,8 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { match message { Message::Refresh => { + *self = Self::Loading; + return system::fetch_information(Message::InformationReceived); } Message::InformationReceived(information) => { From 3e735f14803a3c7e4b37e48e684e584393ad9e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 4 May 2022 17:15:13 +0200 Subject: [PATCH 36/37] Re-export `iced_winit` in `iced_glutin` This fixes the `system` module not being exported in `iced_glutin` when only setting the `iced_winit/system` flag. --- glutin/src/lib.rs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/glutin/src/lib.rs b/glutin/src/lib.rs index fb24b688bf..146dfc4d7d 100644 --- a/glutin/src/lib.rs +++ b/glutin/src/lib.rs @@ -14,23 +14,11 @@ #![forbid(rust_2018_idioms)] pub use glutin; + #[doc(no_inline)] -pub use iced_native::*; +pub use iced_winit::*; pub mod application; -pub use iced_winit::clipboard; -pub use iced_winit::conversion; -pub use iced_winit::settings; -pub use iced_winit::window; -pub use iced_winit::{Error, Mode}; - #[doc(no_inline)] pub use application::Application; -#[doc(no_inline)] -pub use clipboard::Clipboard; -#[doc(no_inline)] -pub use settings::Settings; - -#[cfg(feature = "system")] -pub use iced_winit::system; From a97c520c814a6d3cc538537791be39e0c3182d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 4 May 2022 17:17:07 +0200 Subject: [PATCH 37/37] Sort workspace members in `Cargo.toml` --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 75e573527e..632c66fc22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,10 +84,12 @@ members = [ "examples/stopwatch", "examples/styling", "examples/svg", + "examples/system_information", "examples/todos", "examples/tooltip", "examples/tour", "examples/url_handler", + "examples/websocket", "examples/pure/component", "examples/pure/counter", "examples/pure/game_of_life", @@ -95,8 +97,6 @@ members = [ "examples/pure/pick_list", "examples/pure/todos", "examples/pure/tour", - "examples/websocket", - "examples/system_information" ] [dependencies]