Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PARITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Status of each Windows user-facing feature on Linux in this tree: `working`, `pa
| Always-on-top strip on Wayland | not possible | `src/gui/overlay.rs` — xdg-shell has no stacking request. A layer-shell surface would replace the strip. The compositor's keep-above rule is the equivalent |
| Strip hover hints kept inside the monitor work area | partial | `src/gui/overlay.rs` — `monitor_work_area` is `None` |
| Minimize to the strip, and always show the strip | working | `src/gui/overlay.rs`, `src/gui/settings_tab.rs` |
| System light or dark theme | partial | winit reports no system theme on X11 (`theme` is `None`) and on Wayland only a theme this process already set, so egui stays on its dark fallback |
| System light or dark theme | working | `src/system_theme.rs` — follows the desktop Settings portal live on X11 and Wayland; falls back to GTK color-scheme / gtk-theme via gsettings when unavailable or unspecified |
| Screen reader | working | `Cargo.toml` (eframe `accesskit`, `accesskit_unix`) |
| One running copy (lock) | working | `src/instance.rs` |
| Settings saved across launches | working | `src/config.rs` |
Expand Down
8 changes: 8 additions & 0 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ pub struct App {
window_placement: overlay::Placement,
/// The window was minimized last frame (to catch the moment it is).
was_minimized: bool,
#[cfg(target_os = "linux")]
system_theme: Option<crate::system_theme::Monitor>,
mic_test: mic_test::MicTest,
update_error: Option<String>,
}
Expand All @@ -288,6 +290,10 @@ impl App {
) -> Self {
widgets::configure_text_styles(ctx);
let mut app = Self::stopped(settings);
#[cfg(target_os = "linux")]
{
app.system_theme = Some(crate::system_theme::start(ctx));
}
app.updated_at = just_updated.then(Instant::now);
{
// Development builds never replace themselves on their own.
Expand Down Expand Up @@ -413,6 +419,8 @@ impl App {
overlay_on_top: overlay::OnTop::default(),
window_placement: overlay::Placement::default(),
was_minimized: false,
#[cfg(target_os = "linux")]
system_theme: None,
mic_test: mic_test::MicTest::default(),
update_error: None,
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ mod portal_shortcuts;
mod record;
mod resample;
mod rt;
#[cfg(target_os = "linux")]
mod system_theme;
mod tray;
mod update;
mod virtual_mic;
Expand Down
191 changes: 191 additions & 0 deletions src/system_theme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//! Linux desktop appearance, independent of winit's X11/Wayland theme support.
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use std::time::Duration;

use ashpd::desktop::settings::{ColorScheme, Settings};
use eframe::egui;
use futures_channel::mpsc;
use futures_util::{FutureExt, StreamExt, select};

/// Stops the worker when the application closes. All desktop I/O stays off the UI thread.
pub(crate) struct Monitor(Arc<AtomicBool>);

impl Drop for Monitor {
fn drop(&mut self) {
self.0.store(true, Ordering::Relaxed);
}
}

pub(crate) fn start(ctx: &egui::Context) -> Monitor {
let stopped = Arc::new(AtomicBool::new(false));
let stop = stopped.clone();
let (mut tx, mut ticks) = mpsc::unbounded();
// Also bounds portal discovery and lets an idle signal listener shut down.
std::thread::spawn(move || {
while !stop.load(Ordering::Relaxed) {
std::thread::sleep(Duration::from_secs(2));
if tx.start_send(()).is_err() {
break;
}
}
});
let ctx = ctx.clone();
std::thread::spawn(move || {
pollster::block_on(async move {
loop {
let portal = async {
let settings = Settings::new().await?;
// Subscribe before reading so a change during startup is not lost.
let changes = settings.receive_color_scheme_changed().await?;
let scheme = settings.color_scheme().await?;
Ok::<_, ashpd::Error>((settings, changes, scheme))
}
.fuse();
futures_util::pin_mut!(portal);
let result = select! {
result = portal => result.ok(),
tick = ticks.next().fuse() => {
if tick.is_none() { return; }
None
}
};
if let Some((_settings, changes, mut scheme)) = result {
futures_util::pin_mut!(changes);
loop {
apply(&ctx, portal_theme(scheme).or_else(gtk_theme));
select! {
next = changes.next().fuse() => match next {
Some(next) => scheme = next,
None => break,
},
tick = ticks.next().fuse() => if tick.is_none() { return; },
}
}
} else {
apply(&ctx, gtk_theme());
if ticks.next().await.is_none() {
return;
}
}
}
})
});
Monitor(stopped)
}

fn apply(ctx: &egui::Context, theme: Option<egui::Theme>) {
if let Some(theme) = theme
&& ctx.theme() != theme
{
// Explicit preference prevents winit's absent theme from resetting this next frame.
ctx.set_theme(theme);
ctx.request_repaint();
}
}

fn portal_theme(scheme: ColorScheme) -> Option<egui::Theme> {
match scheme {
ColorScheme::PreferDark => Some(egui::Theme::Dark),
ColorScheme::PreferLight => Some(egui::Theme::Light),
ColorScheme::NoPreference => None,
}
}

fn gsettings(key: &str) -> Option<String> {
let output = std::process::Command::new("gsettings")
.args(["get", "org.gnome.desktop.interface", key])
.output()
.ok()?;
output
.status
.success()
.then(|| String::from_utf8(output.stdout).ok())
.flatten()
}

fn gtk_theme() -> Option<egui::Theme> {
gtk_preference(gsettings("color-scheme").as_deref(), None)
.or_else(|| gtk_preference(None, gsettings("gtk-theme").as_deref()))
}

fn gtk_preference(scheme: Option<&str>, theme: Option<&str>) -> Option<egui::Theme> {
let unquote = |s: &str| s.trim().trim_matches(['\'', '"']).to_owned();
match scheme.map(unquote).as_deref() {
Some("prefer-dark") => Some(egui::Theme::Dark),
Some("prefer-light") => Some(egui::Theme::Light),
_ => theme
.map(unquote)
.filter(|name| !name.is_empty())
.map(|name| {
if name.to_ascii_lowercase().ends_with("-dark") {
egui::Theme::Dark
} else {
egui::Theme::Light
}
}),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn portal_preferences() {
assert_eq!(
portal_theme(ColorScheme::PreferDark),
Some(egui::Theme::Dark)
);
assert_eq!(
portal_theme(ColorScheme::PreferLight),
Some(egui::Theme::Light)
);
assert_eq!(portal_theme(ColorScheme::NoPreference), None);
}

#[test]
fn desktop_changes_survive_missing_winit_theme() {
let ctx = egui::Context::default();
for theme in [egui::Theme::Light, egui::Theme::Dark, egui::Theme::Light] {
apply(&ctx, Some(theme));
let _ = ctx.run_ui(egui::RawInput::default(), |ui| {
assert_eq!(ui.ctx().theme(), theme);
});
}
// Missing desktop settings do not erase the last usable preference.
apply(&ctx, None);
let _ = ctx.run_ui(egui::RawInput::default(), |ui| {
assert_eq!(ui.ctx().theme(), egui::Theme::Light);
});
}

#[test]
fn gtk_preferences() {
for (scheme, theme, expected) in [
(
Some("'prefer-dark'\n"),
Some("'Adwaita'"),
Some(egui::Theme::Dark),
),
(
Some("'prefer-light'"),
Some("'Adwaita-dark'"),
Some(egui::Theme::Light),
),
(
Some("'default'"),
Some("'Adwaita-dark'\n"),
Some(egui::Theme::Dark),
),
(None, Some("\"Breeze-Dark\""), Some(egui::Theme::Dark)),
(None, Some("'Adwaita'"), Some(egui::Theme::Light)),
(Some("unknown"), Some("''"), None),
(None, None, None),
] {
assert_eq!(gtk_preference(scheme, theme), expected);
}
}
}
Loading