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

@ -319,9 +319,6 @@ pub fn window_event(
Some(Event::Window(window::Event::Moved(Point::new(x, y))))
}
WindowEvent::ThemeChanged(theme) => Some(Event::Window(
window::Event::ThemeModeChanged(theme_mode(theme)),
)),
_ => None,
}
}

View file

@ -29,9 +29,6 @@ pub use winit;
pub mod clipboard;
pub mod conversion;
#[cfg(feature = "system")]
pub mod system;
mod error;
mod proxy;
mod window;
@ -53,6 +50,7 @@ use crate::futures::futures::{Future, StreamExt};
use crate::futures::subscription;
use crate::futures::{Executor, Runtime};
use crate::graphics::{Compositor, compositor};
use crate::runtime::system;
use crate::runtime::user_interface::{self, UserInterface};
use crate::runtime::{Action, Task};
@ -126,6 +124,7 @@ where
let (event_sender, event_receiver) = mpsc::unbounded();
let (control_sender, control_receiver) = mpsc::unbounded();
let (system_theme_sender, system_theme_receiver) = oneshot::channel();
let instance = Box::pin(run_instance::<P>(
program,
@ -136,6 +135,7 @@ where
is_daemon,
graphics_settings,
settings.fonts,
system_theme_receiver,
));
let context = task::Context::from_waker(task::noop_waker_ref());
@ -147,6 +147,7 @@ where
sender: mpsc::UnboundedSender<Event<Action<Message>>>,
receiver: mpsc::UnboundedReceiver<Control>,
error: Option<Error>,
system_theme: Option<oneshot::Sender<theme::Mode>>,
#[cfg(target_arch = "wasm32")]
canvas: Option<web_sys::HtmlCanvasElement>,
@ -159,6 +160,7 @@ where
sender: event_sender,
receiver: control_receiver,
error: None,
system_theme: Some(system_theme_sender),
#[cfg(target_arch = "wasm32")]
canvas: None,
@ -172,10 +174,15 @@ where
Message: std::fmt::Debug,
F: Future<Output = ()>,
{
fn resumed(
&mut self,
_event_loop: &winit::event_loop::ActiveEventLoop,
) {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
if let Some(sender) = self.system_theme.take() {
let _ = sender.send(
event_loop
.system_theme()
.map(conversion::theme_mode)
.unwrap_or_default(),
);
}
}
fn new_events(
@ -498,6 +505,7 @@ async fn run_instance<P>(
is_daemon: bool,
graphics_settings: graphics::Settings,
default_fonts: Vec<Cow<'static, [u8]>>,
mut _system_theme: oneshot::Receiver<theme::Mode>,
) where
P: Program + 'static,
P::Theme: theme::Base,
@ -518,7 +526,7 @@ async fn run_instance<P>(
let mut clipboard = Clipboard::unconnected();
#[cfg(all(feature = "linux-theme-detection", target_os = "linux"))]
let system_theme = {
let mut system_theme = {
let to_mode = |color_scheme| match color_scheme {
mundy::ColorScheme::NoPreference => theme::Mode::None,
mundy::ColorScheme::Light => theme::Mode::Light,
@ -528,7 +536,9 @@ async fn run_instance<P>(
runtime.run(
mundy::Preferences::stream(mundy::Interest::ColorScheme)
.map(move |preferences| {
Action::ChangeTheme(to_mode(preferences.color_scheme))
Action::System(system::Action::NotifyTheme(to_mode(
preferences.color_scheme,
)))
})
.boxed(),
);
@ -542,7 +552,10 @@ async fn run_instance<P>(
};
#[cfg(not(all(feature = "linux-theme-detection", target_os = "linux")))]
let system_theme = theme::Mode::None;
let mut system_theme =
_system_theme.try_recv().ok().flatten().unwrap_or_default();
log::info!("System theme: {system_theme:?}");
loop {
// Empty the queue if possible
@ -728,6 +741,7 @@ async fn run_instance<P>(
run_action(
action,
&program,
&mut runtime,
&mut compositor,
&mut events,
&mut messages,
@ -737,6 +751,7 @@ async fn run_instance<P>(
&mut window_manager,
&mut ui_caches,
&mut is_window_opening,
&mut system_theme,
);
actions += 1;
}
@ -895,11 +910,24 @@ async fn run_instance<P>(
continue;
};
if matches!(
window_event,
winit::event::WindowEvent::Resized(_)
) {
window.raw.request_redraw();
match window_event {
winit::event::WindowEvent::Resized(_) => {
window.raw.request_redraw();
}
winit::event::WindowEvent::ThemeChanged(theme) => {
let mode = conversion::theme_mode(theme);
if mode != system_theme {
system_theme = mode;
runtime.broadcast(
subscription::Event::SystemThemeChanged(
mode,
),
);
}
}
_ => {}
}
if matches!(
@ -912,6 +940,7 @@ async fn run_instance<P>(
id,
)),
&program,
&mut runtime,
&mut compositor,
&mut events,
&mut messages,
@ -921,6 +950,7 @@ async fn run_instance<P>(
&mut window_manager,
&mut ui_caches,
&mut is_window_opening,
&mut system_theme,
);
} else {
window.state.update(
@ -1132,6 +1162,7 @@ fn update<P: Program, E: Executor>(
fn run_action<'a, P, C>(
action: Action<P::Message>,
program: &'a program::Instance<P>,
runtime: &mut Runtime<P::Executor, Proxy<P::Message>, Action<P::Message>>,
compositor: &mut Option<C>,
events: &mut Vec<(window::Id, core::Event)>,
messages: &mut Vec<P::Message>,
@ -1144,13 +1175,13 @@ fn run_action<'a, P, C>(
window_manager: &mut WindowManager<P, C>,
ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
is_window_opening: &mut bool,
system_theme: &mut theme::Mode,
) where
P: Program,
C: Compositor<Renderer = P::Renderer> + 'static,
P::Theme: theme::Base,
{
use crate::runtime::clipboard;
use crate::runtime::system;
use crate::runtime::window;
match action {
@ -1458,21 +1489,44 @@ fn run_action<'a, P, C>(
}
},
Action::System(action) => match action {
system::Action::QueryInformation(_channel) => {
#[cfg(feature = "system")]
system::Action::GetInformation(_channel) => {
#[cfg(feature = "sysinfo")]
{
if let Some(compositor) = compositor {
let graphics_info = compositor.fetch_information();
let graphics_info = compositor.information();
let _ = std::thread::spawn(move || {
let information =
crate::system::information(graphics_info);
let information = system_information(graphics_info);
let _ = _channel.send(information);
});
}
}
}
system::Action::GetTheme(channel) => {
let _ = channel.send(*system_theme);
}
system::Action::NotifyTheme(mode) => {
if mode != *system_theme {
*system_theme = mode;
runtime.broadcast(subscription::Event::SystemThemeChanged(
mode,
));
}
let Some(theme) = conversion::window_theme(mode) else {
return;
};
for (_id, window) in window_manager.iter_mut() {
window.state.update(
program,
&window.raw,
&winit::event::WindowEvent::ThemeChanged(theme),
);
}
}
},
Action::Widget(operation) => {
let mut current_operation = Some(operation);
@ -1501,27 +1555,6 @@ fn run_action<'a, P, C>(
let _ = channel.send(Ok(()));
}
}
Action::ChangeTheme(mode) => {
let Some(theme) = conversion::window_theme(mode) else {
return;
};
for (id, window) in window_manager.iter_mut() {
window.raw.set_theme(Some(theme));
window.state.update(
program,
&window.raw,
&winit::event::WindowEvent::ThemeChanged(theme),
);
events.push((
id,
core::Event::Window(core::window::Event::ThemeModeChanged(
mode,
)),
));
}
}
Action::Reload => {
for (id, window) in window_manager.iter_mut() {
let Some(ui) = interfaces.remove(&id) else {
@ -1602,3 +1635,37 @@ pub fn user_force_quit(
_ => false,
}
}
#[cfg(feature = "sysinfo")]
fn system_information(
graphics: compositor::Information,
) -> system::Information {
use sysinfo::{Process, System};
let mut system = System::new_all();
system.refresh_all();
let cpu_brand = system
.cpus()
.first()
.map(|cpu| cpu.brand().to_string())
.unwrap_or_default();
let memory_used = sysinfo::get_current_pid()
.and_then(|pid| system.process(pid).ok_or("Process not found"))
.map(Process::memory)
.ok();
system::Information {
system_name: System::name(),
system_kernel: System::kernel_version(),
system_version: System::long_os_version(),
system_short_version: System::os_version(),
cpu_brand,
cpu_cores: system.physical_core_count(),
memory_total: system.total_memory(),
memory_used,
graphics_adapter: graphics.adapter,
graphics_backend: graphics.backend,
}
}

View file

@ -3,13 +3,6 @@ use crate::graphics::compositor;
use crate::runtime::system::{Action, Information};
use crate::runtime::{self, Task};
/// Query for available system information.
pub fn fetch_information() -> Task<Information> {
runtime::task::oneshot(|channel| {
runtime::Action::System(Action::QueryInformation(channel))
})
}
pub(crate) fn information(
graphics_info: compositor::Information,
) -> Information {

View file

@ -53,11 +53,6 @@ where
window: &Window,
system_theme: theme::Mode,
) -> Self {
let system_theme = window
.theme()
.map(conversion::theme_mode)
.unwrap_or(system_theme);
let title = program.title(window_id);
let scale_factor = program.scale_factor(window_id);
let theme = program.theme(window_id);