iced_runtime/
system.rs

1//! Access the native system.
2use crate::core::theme;
3use crate::futures::futures::channel::oneshot;
4use crate::futures::subscription::{self, Subscription};
5use crate::task::{self, Task};
6
7/// An operation to be performed on the system.
8#[derive(Debug)]
9pub enum Action {
10    /// Send available system information.
11    GetInformation(oneshot::Sender<Information>),
12
13    /// Send the current system theme mode.
14    GetTheme(oneshot::Sender<theme::Mode>),
15
16    /// Notify to the runtime that the system theme has changed.
17    NotifyTheme(theme::Mode),
18}
19
20/// Contains information about the system (e.g. system name, processor, memory, graphics adapter).
21#[derive(Clone, Debug)]
22pub struct Information {
23    /// The operating system name
24    pub system_name: Option<String>,
25    /// Operating system kernel version
26    pub system_kernel: Option<String>,
27    /// Long operating system version
28    ///
29    /// Examples:
30    /// - MacOS 10.15 Catalina
31    /// - Windows 10 Pro
32    /// - Ubuntu 20.04 LTS (Focal Fossa)
33    pub system_version: Option<String>,
34    /// Short operating system version number
35    pub system_short_version: Option<String>,
36    /// Detailed processor model information
37    pub cpu_brand: String,
38    /// The number of physical cores on the processor
39    pub cpu_cores: Option<usize>,
40    /// Total RAM size, in bytes
41    pub memory_total: u64,
42    /// Memory used by this process, in bytes
43    pub memory_used: Option<u64>,
44    /// Underlying graphics backend for rendering
45    pub graphics_backend: String,
46    /// Model information for the active graphics adapter
47    pub graphics_adapter: String,
48}
49
50/// Returns available system information.
51pub fn information() -> Task<Information> {
52    task::oneshot(|channel| {
53        crate::Action::System(Action::GetInformation(channel))
54    })
55}
56
57/// Returns the current system theme.
58pub fn theme() -> Task<theme::Mode> {
59    task::oneshot(|sender| crate::Action::System(Action::GetTheme(sender)))
60}
61
62/// Subscribes to system theme changes.
63pub fn theme_changes() -> Subscription<theme::Mode> {
64    #[derive(Hash)]
65    struct ThemeChanges;
66
67    subscription::filter_map(ThemeChanges, |event| {
68        let subscription::Event::SystemThemeChanged(mode) = event else {
69            return None;
70        };
71
72        Some(mode)
73    })
74}