#[cfg(all(feature = "watch", feature = "kde", target_os = "linux"))]
mod kde;
#[cfg(all(feature = "watch", feature = "portal", target_os = "linux"))]
mod gnome;
#[cfg(all(feature = "watch", feature = "macos", target_os = "macos"))]
mod macos;
#[cfg(all(feature = "watch", feature = "windows", target_os = "windows"))]
mod windows;
use std::sync::mpsc;
use std::thread::JoinHandle;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ThemeChangeEvent {
Changed,
}
#[must_use]
pub struct ThemeSubscription {
shutdown_tx: Option<mpsc::Sender<()>>,
thread: Option<JoinHandle<()>>,
platform_shutdown: Option<Box<dyn FnOnce() + Send>>,
}
impl std::fmt::Debug for ThemeSubscription {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ThemeSubscription")
.field("shutdown_tx", &self.shutdown_tx)
.field("thread", &self.thread)
.field(
"platform_shutdown",
&self.platform_shutdown.as_ref().map(|_| "..."),
)
.finish()
}
}
impl ThemeSubscription {
#[cfg(any(
test,
all(target_os = "linux", any(feature = "kde", feature = "portal")),
all(target_os = "macos", feature = "macos"),
all(target_os = "windows", feature = "windows"),
))]
pub(crate) fn new(
shutdown_tx: mpsc::Sender<()>,
thread: JoinHandle<()>,
platform_shutdown: Option<Box<dyn FnOnce() + Send>>,
) -> Self {
Self {
shutdown_tx: Some(shutdown_tx),
thread: Some(thread),
platform_shutdown,
}
}
}
impl Drop for ThemeSubscription {
fn drop(&mut self) {
if let Some(shutdown_fn) = self.platform_shutdown.take() {
shutdown_fn();
}
drop(self.shutdown_tx.take());
if let Some(handle) = self.thread.take() {
let _ = handle.join();
}
}
}
pub fn on_theme_change(
callback: impl Fn(ThemeChangeEvent) + Send + 'static,
) -> crate::Result<ThemeSubscription> {
#[cfg(target_os = "linux")]
{
#[cfg(not(any(feature = "kde", feature = "portal")))]
let _ = callback;
let de = crate::detect_linux_desktop();
match de {
#[cfg(feature = "kde")]
crate::LinuxDesktop::Kde => kde::watch_kde(callback),
#[cfg(feature = "portal")]
crate::LinuxDesktop::Gnome | crate::LinuxDesktop::Budgie => {
gnome::watch_gnome(callback)
}
_ => Err(crate::Error::WatchUnavailable {
reason: "theme watching not supported for this desktop environment",
}),
}
}
#[cfg(target_os = "macos")]
{
#[cfg(feature = "macos")]
{
return macos::watch_macos(callback);
}
#[cfg(not(feature = "macos"))]
{
let _ = callback;
return Err(crate::Error::WatchUnavailable {
reason: "enable the 'macos' feature for macOS theme watching",
});
}
}
#[cfg(target_os = "windows")]
{
#[cfg(feature = "windows")]
{
return windows::watch_windows(callback);
}
#[cfg(not(feature = "windows"))]
{
let _ = callback;
return Err(crate::Error::WatchUnavailable {
reason: "enable the 'windows' feature for Windows theme watching",
});
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
{
let _ = callback;
Err(crate::Error::PlatformUnsupported {
platform: "unsupported",
})
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn theme_change_event_is_debug_clone_eq() {
let event = ThemeChangeEvent::Changed;
let cloned = event.clone();
assert_eq!(event, cloned);
let debug_str = format!("{:?}", event);
assert!(debug_str.contains("Changed"));
}
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
#[test]
fn on_theme_change_returns_unsupported() {
let result = on_theme_change(|_| {});
assert!(result.is_err());
let err = result.unwrap_err();
assert!(
matches!(&err, crate::Error::PlatformUnsupported { .. }),
"expected PlatformUnsupported, got: {err:?}"
);
}
#[cfg(target_os = "linux")]
#[test]
fn on_theme_change_dispatches_or_returns_error() {
let result = on_theme_change(|_| {});
assert!(
matches!(
&result,
Ok(_)
| Err(crate::Error::WatchUnavailable { .. })
| Err(crate::Error::PlatformUnsupported { .. })
| Err(crate::Error::ReaderFailed { .. })
),
"unexpected result: {result:?}"
);
}
#[test]
fn theme_subscription_drop_signals_shutdown() {
use std::sync::mpsc;
use std::thread;
let (tx, rx) = mpsc::channel();
let thread_handle = thread::spawn(move || {
let _ = rx.recv();
});
let watcher = ThemeSubscription::new(tx, thread_handle, None);
drop(watcher);
}
#[test]
fn theme_subscription_is_send() {
fn assert_send<T: Send>() {}
assert_send::<ThemeSubscription>();
}
}