Add theme and theme_changes functions to system

This commit is contained in:
Héctor Ramón Jiménez 2025-09-08 14:32:24 +02:00
parent 9518573fce
commit 09c604c92d
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
24 changed files with 186 additions and 102 deletions

View file

@ -25,7 +25,6 @@ pub use iced_futures as futures;
pub use task::Task;
pub use user_interface::UserInterface;
use crate::core::theme;
use crate::core::widget;
use crate::futures::futures::channel::oneshot;
@ -57,9 +56,6 @@ pub enum Action<T> {
/// Run a system action.
System(system::Action),
/// Change the default system theme mode of all windows.
ChangeTheme(theme::Mode),
/// Recreate all user interfaces and redraw all windows.
Reload,
@ -86,7 +82,6 @@ impl<T> Action<T> {
Action::Clipboard(action) => Err(Action::Clipboard(action)),
Action::Window(action) => Err(Action::Window(action)),
Action::System(action) => Err(Action::System(action)),
Action::ChangeTheme(mode) => Err(Action::ChangeTheme(mode)),
Action::Reload => Err(Action::Reload),
Action::Exit => Err(Action::Exit),
}
@ -111,9 +106,6 @@ where
}
Action::Window(_) => write!(f, "Action::Window"),
Action::System(action) => write!(f, "Action::System({action:?})"),
Action::ChangeTheme(mode) => {
write!(f, "Action::ChangeTheme({mode:?})")
}
Action::Reload => write!(f, "Action::Reload"),
Action::Exit => write!(f, "Action::Exit"),
}

View file

@ -1,11 +1,20 @@
//! Access the native system.
use crate::core::theme;
use crate::futures::futures::channel::oneshot;
use crate::futures::subscription::{self, Subscription};
use crate::task::{self, Task};
/// An operation to be performed on the system.
#[derive(Debug)]
pub enum Action {
/// Query system information and produce `T` with the result.
QueryInformation(oneshot::Sender<Information>),
/// Send available system information.
GetInformation(oneshot::Sender<Information>),
/// Send the current system theme mode.
GetTheme(oneshot::Sender<theme::Mode>),
/// Notify to the runtime that the system theme has changed.
NotifyTheme(theme::Mode),
}
/// Contains information about the system (e.g. system name, processor, memory, graphics adapter).
@ -37,3 +46,29 @@ pub struct Information {
/// Model information for the active graphics adapter
pub graphics_adapter: String,
}
/// Returns available system information.
pub fn information() -> Task<Information> {
task::oneshot(|channel| {
crate::Action::System(Action::GetInformation(channel))
})
}
/// Returns the current system theme.
pub fn theme() -> Task<theme::Mode> {
task::oneshot(|sender| crate::Action::System(Action::GetTheme(sender)))
}
/// Subscribes to system theme changes.
pub fn theme_changes() -> Subscription<theme::Mode> {
#[derive(Hash)]
struct ThemeChanges;
subscription::filter_map(ThemeChanges, |event| {
let subscription::Event::SystemThemeChanged(mode) = event else {
return None;
};
Some(mode)
})
}

View file

@ -266,12 +266,12 @@ pub fn close<T>(id: Id) -> Task<T> {
}
/// Gets the window [`Id`] of the oldest window.
pub fn get_oldest() -> Task<Option<Id>> {
pub fn oldest() -> Task<Option<Id>> {
task::oneshot(|channel| crate::Action::Window(Action::GetOldest(channel)))
}
/// Gets the window [`Id`] of the latest window.
pub fn get_latest() -> Task<Option<Id>> {
pub fn latest() -> Task<Option<Id>> {
task::oneshot(|channel| crate::Action::Window(Action::GetLatest(channel)))
}
@ -315,14 +315,14 @@ pub fn set_resize_increments<T>(id: Id, increments: Option<Size>) -> Task<T> {
}
/// Get the window's size in logical dimensions.
pub fn get_size(id: Id) -> Task<Size> {
pub fn size(id: Id) -> Task<Size> {
task::oneshot(move |channel| {
crate::Action::Window(Action::GetSize(id, channel))
})
}
/// Gets the maximized state of the window with the given [`Id`].
pub fn get_maximized(id: Id) -> Task<bool> {
pub fn is_maximized(id: Id) -> Task<bool> {
task::oneshot(move |channel| {
crate::Action::Window(Action::GetMaximized(id, channel))
})
@ -334,7 +334,7 @@ pub fn maximize<T>(id: Id, maximized: bool) -> Task<T> {
}
/// Gets the minimized state of the window with the given [`Id`].
pub fn get_minimized(id: Id) -> Task<Option<bool>> {
pub fn is_minimized(id: Id) -> Task<Option<bool>> {
task::oneshot(move |channel| {
crate::Action::Window(Action::GetMinimized(id, channel))
})
@ -346,14 +346,14 @@ pub fn minimize<T>(id: Id, minimized: bool) -> Task<T> {
}
/// Gets the position in logical coordinates of the window with the given [`Id`].
pub fn get_position(id: Id) -> Task<Option<Point>> {
pub fn position(id: Id) -> Task<Option<Point>> {
task::oneshot(move |channel| {
crate::Action::Window(Action::GetPosition(id, channel))
})
}
/// Gets the scale factor of the window with the given [`Id`].
pub fn get_scale_factor(id: Id) -> Task<f32> {
pub fn scale_factor(id: Id) -> Task<f32> {
task::oneshot(move |channel| {
crate::Action::Window(Action::GetScaleFactor(id, channel))
})
@ -365,7 +365,7 @@ pub fn move_to<T>(id: Id, position: Point) -> Task<T> {
}
/// Gets the current [`Mode`] of the window.
pub fn get_mode(id: Id) -> Task<Mode> {
pub fn mode(id: Id) -> Task<Mode> {
task::oneshot(move |channel| {
crate::Action::Window(Action::GetMode(id, channel))
})
@ -426,7 +426,7 @@ pub fn show_system_menu<T>(id: Id) -> Task<T> {
/// Gets an identifier unique to the window, provided by the underlying windowing system. This is
/// not to be confused with [`Id`].
pub fn get_raw_id<Message>(id: Id) -> Task<u64> {
pub fn raw_id<Message>(id: Id) -> Task<u64> {
task::oneshot(|channel| {
crate::Action::Window(Action::GetRawId(id, channel))
})