mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-25 04:44:12 +02:00
Follow the OS appearance when set to System without a new window (#540)
This commit is contained in:
@@ -160,19 +160,14 @@ fn setup_window_menu<R: Runtime>(win: &WebviewWindow<R>) -> Result<()> {
|
||||
}
|
||||
|
||||
fn initial_appearance_script<R: Runtime>(app_handle: &AppHandle<R>) -> Option<String> {
|
||||
use yaak_system_appearance::{Appearance, InitialAppearanceSource};
|
||||
|
||||
let settings = app_handle.db().get_settings();
|
||||
let (appearance, source) = match settings.appearance.as_str() {
|
||||
"dark" => (Appearance::Dark, InitialAppearanceSource::Settings),
|
||||
"light" => (Appearance::Light, InitialAppearanceSource::Settings),
|
||||
_ => (
|
||||
yaak_system_appearance::system_appearance()?,
|
||||
InitialAppearanceSource::LinuxSystem,
|
||||
),
|
||||
};
|
||||
|
||||
Some(yaak_system_appearance::initialization_script(appearance, source))
|
||||
// Only report the appearance the OS prefers. The frontend needs it to resolve the
|
||||
// "automatic" setting, so the configured appearance is never a substitute for it.
|
||||
//
|
||||
// NOTE: The value comes from the watcher state, not a fresh detection, so the frontend
|
||||
// only ever sees a snapshot when the change events that keep it fresh are also flowing.
|
||||
let state = app_handle.try_state::<yaak_system_appearance::SystemAppearanceState>()?;
|
||||
let appearance = state.last_appearance()?;
|
||||
Some(yaak_system_appearance::initialization_script(appearance))
|
||||
}
|
||||
|
||||
/// Extension trait for easily creating a PluginContext from a WebviewWindow
|
||||
@@ -1783,7 +1778,7 @@ pub fn run() {
|
||||
app.state::<yaak_models::query_manager::QueryManager>().inner().clone();
|
||||
let app_id = app.config().identifier.to_string();
|
||||
app.manage(yaak_crypto::manager::EncryptionManager::new(query_manager, app_id));
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
if let Some(state) = yaak_system_appearance::watch(app.app_handle().clone()) {
|
||||
app.manage(state);
|
||||
}
|
||||
@@ -2002,7 +1997,7 @@ pub fn run() {
|
||||
});
|
||||
}
|
||||
RunEvent::WindowEvent { event: WindowEvent::Focused(true), label, .. } => {
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
if let Some(state) =
|
||||
app_handle.try_state::<yaak_system_appearance::SystemAppearanceState>()
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
|
||||
dark-light = "2.0.0"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
use std::time::Duration;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
use log::{debug, warn};
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
use tauri::Emitter;
|
||||
use tauri::{AppHandle, Runtime};
|
||||
|
||||
pub const INITIAL_APPEARANCE_GLOBAL: &str = "__YAAK_INITIAL_APPEARANCE__";
|
||||
pub const INITIAL_APPEARANCE_SOURCE_GLOBAL: &str = "__YAAK_INITIAL_APPEARANCE_SOURCE__";
|
||||
pub const SYSTEM_APPEARANCE_CHANGE_EVENT: &str = "system_appearance_change";
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
const SYSTEM_APPEARANCE_POLL_INTERVAL: Duration = Duration::from_secs(1);
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
@@ -30,64 +29,53 @@ impl Appearance {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum InitialAppearanceSource {
|
||||
Settings,
|
||||
LinuxSystem,
|
||||
}
|
||||
|
||||
impl InitialAppearanceSource {
|
||||
fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Settings => "settings",
|
||||
Self::LinuxSystem => "linux-system",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SystemAppearanceState {
|
||||
// Only read by the Linux polling thread
|
||||
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
|
||||
last_appearance: Arc<Mutex<Option<Appearance>>>,
|
||||
}
|
||||
|
||||
pub fn initialization_script(appearance: Appearance, source: InitialAppearanceSource) -> String {
|
||||
let appearance = appearance.as_str();
|
||||
let source = source.as_str();
|
||||
format!(
|
||||
"window.{INITIAL_APPEARANCE_GLOBAL} = {appearance:?};\
|
||||
window.{INITIAL_APPEARANCE_SOURCE_GLOBAL} = {source:?};"
|
||||
)
|
||||
impl SystemAppearanceState {
|
||||
pub fn last_appearance(&self) -> Option<Appearance> {
|
||||
*self.last_appearance.lock().expect("system appearance lock poisoned")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn initialization_script(appearance: Appearance) -> String {
|
||||
let appearance = appearance.as_str();
|
||||
format!("window.{INITIAL_APPEARANCE_GLOBAL} = {appearance:?};")
|
||||
}
|
||||
|
||||
/// Detect the appearance the OS prefers, independent of any appearance that has
|
||||
/// been forced onto app windows (which is what the webview itself reports).
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
pub fn system_appearance() -> Option<Appearance> {
|
||||
#[cfg(target_os = "linux")]
|
||||
if let Some(appearance) = gsettings_system_appearance() {
|
||||
return Some(appearance);
|
||||
}
|
||||
|
||||
// On macOS this reads AppleInterfaceStyle from the global user defaults
|
||||
match dark_light::detect() {
|
||||
Ok(dark_light::Mode::Dark) => Some(Appearance::Dark),
|
||||
Ok(dark_light::Mode::Light) => Some(Appearance::Light),
|
||||
Ok(dark_light::Mode::Unspecified) => None,
|
||||
Err(err) => {
|
||||
debug!("Failed to detect Linux system appearance: {err:?}");
|
||||
debug!("Failed to detect system appearance: {err:?}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
|
||||
pub fn system_appearance() -> Option<Appearance> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
pub fn watch<R: Runtime>(app_handle: AppHandle<R>) -> Option<SystemAppearanceState> {
|
||||
let last_appearance = system_appearance();
|
||||
if last_appearance.is_none() {
|
||||
debug!("Linux system appearance detection unavailable");
|
||||
debug!("System appearance detection unavailable");
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -103,12 +91,12 @@ pub fn watch<R: Runtime>(app_handle: AppHandle<R>) -> Option<SystemAppearanceSta
|
||||
Some(state)
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
|
||||
pub fn watch<R: Runtime>(_app_handle: AppHandle<R>) -> Option<SystemAppearanceState> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
pub fn emit_change<R: Runtime>(app_handle: &AppHandle<R>, state: &SystemAppearanceState) {
|
||||
let appearance = system_appearance();
|
||||
let mut last_appearance =
|
||||
|
||||
Reference in New Issue
Block a user