A preview (the Theme Builder's, developer mode's, or one an automation left on through theme_preview) replaced every site tab's theme, pane themes included, also on reload; "Theme for this pane" then showed its choice checked while the page kept the preview. Reproduced live: a preview of Windows 98 turned a Paper pane into Windows 98 until the preview ended. Now a pane with its own theme keeps it; every other tab shows the preview, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
219 lines
8.7 KiB
Rust
219 lines
8.7 KiB
Rust
//! Site themes, native side, for every app that sets [`crate::app::AppConfig::themes`]: the
|
|
//! shell-only `theme_*` commands ([`crate::commands::THEME`]) over [`crate::theme`]'s state (the
|
|
//! owner's theme, the account and pane themes, the Theme Builder's preview). The app only names
|
|
//! its built-in themes ([`ThemeConfig`]) and the two files they are saved in.
|
|
//!
|
|
//! Site tabs gain no command: native code evaluates the theme script (and the cached Google font
|
|
//! bytes it needs) in every site tab and sets each tab's native background to its theme's page color.
|
|
//! The app's own writers (a backup import, developer mode's theme file) use [`set_theme`],
|
|
//! [`set_scopes`] and [`set_preview`], then [`apply`].
|
|
|
|
use crate::app::{config, tab_profile};
|
|
use crate::host::{data_file, read_json, shell_only, write_json};
|
|
use crate::theme::{ThemeChoice, ThemeScopes};
|
|
use std::sync::{Mutex, MutexGuard};
|
|
use tauri::{AppHandle, Manager, Webview};
|
|
|
|
pub use crate::theme::ThemeConfig;
|
|
|
|
#[derive(Default)]
|
|
struct Themes {
|
|
theme: Option<ThemeChoice>,
|
|
scopes: ThemeScopes,
|
|
/// The Theme Builder's unsaved draft, shown in every site tab until it ends.
|
|
preview: Option<ThemeChoice>,
|
|
}
|
|
|
|
struct ThemeState(Mutex<Themes>);
|
|
|
|
/// Loads the saved theme and scopes (anything that no longer validates is dropped). Does nothing
|
|
/// unless the app set [`crate::app::AppConfig::themes`].
|
|
pub(crate) fn init(app: &AppHandle) {
|
|
let Some(themes) = config(app).themes else { return };
|
|
let theme = read_json::<ThemeChoice>(app, themes.file).filter(|theme| theme.validate(themes.site).is_ok());
|
|
let scopes = read_json::<ThemeScopes>(app, themes.scopes_file).filter(|scopes| scopes.validate().is_ok()).unwrap_or_default();
|
|
app.manage(ThemeState(Mutex::new(Themes { theme, scopes, preview: None })));
|
|
}
|
|
|
|
fn themes(app: &AppHandle) -> Result<&'static ThemeConfig, String> {
|
|
config(app).themes.ok_or_else(|| "UNAVAILABLE: this app has no themes".to_owned())
|
|
}
|
|
|
|
fn state(app: &AppHandle) -> Result<MutexGuard<'_, Themes>, String> {
|
|
app.try_state::<ThemeState>()
|
|
.ok_or("DATABASE: theme state unavailable; restart safely")?
|
|
.inner()
|
|
.0
|
|
.lock()
|
|
.map_err(|_| "DATABASE: theme state unavailable; restart safely".into())
|
|
}
|
|
|
|
/// The theme the owner chose (the site's own look when none).
|
|
pub fn current(app: &AppHandle) -> Result<ThemeChoice, String> {
|
|
let themes = themes(app)?;
|
|
Ok(state(app)?.theme.clone().unwrap_or_else(|| ThemeChoice::default_for(themes.site)))
|
|
}
|
|
|
|
/// Account and pane themes and the chrome setting.
|
|
pub fn scopes(app: &AppHandle) -> Result<ThemeScopes, String> {
|
|
Ok(state(app)?.scopes.clone())
|
|
}
|
|
|
|
/// The theme for site tab webview `label`: its pane's own theme; else the Theme Builder's preview
|
|
/// while one is open; else its account's or the global theme.
|
|
fn tab_theme(app: &AppHandle, themes: &ThemeConfig, state: &Themes, label: &str) -> ThemeChoice {
|
|
let global = state.theme.clone().unwrap_or_else(|| ThemeChoice::default_for(themes.site));
|
|
let tab = crate::policy::tab_id(label).unwrap_or_default();
|
|
state.scopes.for_tab_with_preview(themes.site, &global, state.preview.as_ref(), tab, tab_profile(app, label).as_deref())
|
|
}
|
|
|
|
fn background(theme: &ThemeChoice, themes: &ThemeConfig) -> Option<tauri::window::Color> {
|
|
theme.page_color(themes.site).map(|(r, g, b)| tauri::window::Color(r, g, b, 255))
|
|
}
|
|
|
|
fn scripts_for(app: &AppHandle, themes: &ThemeConfig, theme: &ThemeChoice) -> Vec<String> {
|
|
let site = config(app).site;
|
|
let mut scripts = Vec::new();
|
|
#[cfg(feature = "net")]
|
|
if let Some(fonts) = crate::fonts::page_script_for(site, &theme.google_fonts()) {
|
|
scripts.push(fonts);
|
|
}
|
|
if let Ok(script) = theme.script(themes.site, site) {
|
|
scripts.push(script);
|
|
}
|
|
scripts
|
|
}
|
|
|
|
/// The native background of site tab `label` until its page paints: its theme's page color.
|
|
pub fn tab_background(app: &AppHandle, label: &str) -> Option<tauri::window::Color> {
|
|
let themes = config(app).themes?;
|
|
let state = state(app).ok()?;
|
|
background(&tab_theme(app, themes, &state, label), themes)
|
|
}
|
|
|
|
/// The scripts a freshly loaded site document in tab `label` gets for its theme (and the font
|
|
/// bytes it needs); none while the site keeps its own look.
|
|
pub fn page_scripts(app: &AppHandle, label: &str) -> Vec<String> {
|
|
let Some(themes) = config(app).themes else { return Vec::new() };
|
|
let Ok(state) = state(app) else { return Vec::new() };
|
|
if state.theme.is_none() && state.preview.is_none() && state.scopes.is_default() {
|
|
return Vec::new();
|
|
}
|
|
scripts_for(app, themes, &tab_theme(app, themes, &state, label))
|
|
}
|
|
|
|
/// Sends every site tab its theme again (after the theme, the scopes, the preview or the fonts
|
|
/// changed). In safe mode site pages keep the site's own look; a theme chosen meanwhile applies
|
|
/// when safe mode is left.
|
|
pub fn apply(app: &AppHandle) {
|
|
if crate::safe::active() {
|
|
return;
|
|
}
|
|
let Some(themes) = config(app).themes else { return };
|
|
let site = config(app).site;
|
|
let per_tab: Vec<(Webview, ThemeChoice)> = {
|
|
let Ok(state) = state(app) else { return };
|
|
app.webviews()
|
|
.into_iter()
|
|
.filter(|(label, tab)| crate::policy::tab_id(label).is_some() && tab.url().is_ok_and(|url| site.is_origin(&url)))
|
|
.map(|(label, tab)| (tab, tab_theme(app, themes, &state, &label)))
|
|
.collect()
|
|
};
|
|
for (tab, theme) in per_tab {
|
|
let _ = tab.set_background_color(background(&theme, themes));
|
|
for script in scripts_for(app, themes, &theme) {
|
|
let _ = tab.eval(&script);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Saves the owner's theme (checked against the built-in ones; `None` or an unchanged site look
|
|
/// removes the file). Does not apply it: call [`apply`].
|
|
pub fn set_theme(app: &AppHandle, theme: Option<ThemeChoice>) -> Result<(), String> {
|
|
let themes = themes(app)?;
|
|
let theme = theme.unwrap_or_else(|| ThemeChoice::default_for(themes.site));
|
|
theme.validate(themes.site)?;
|
|
if theme.is_empty(themes.site) {
|
|
if let Some(path) = data_file(app, themes.file) {
|
|
let _ = std::fs::remove_file(path);
|
|
}
|
|
state(app)?.theme = None;
|
|
} else {
|
|
write_json(app, themes.file, &theme)?;
|
|
state(app)?.theme = Some(theme);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Saves the account and pane themes (bounded, ids checked). Does not apply them: call [`apply`].
|
|
pub fn set_scopes(app: &AppHandle, scopes: ThemeScopes) -> Result<(), String> {
|
|
let themes = themes(app)?;
|
|
scopes.validate()?;
|
|
if scopes.is_default() {
|
|
if let Some(path) = data_file(app, themes.scopes_file) {
|
|
let _ = std::fs::remove_file(path);
|
|
}
|
|
} else {
|
|
write_json(app, themes.scopes_file, &scopes)?;
|
|
}
|
|
state(app)?.scopes = scopes;
|
|
Ok(())
|
|
}
|
|
|
|
/// Shows an unsaved theme in every site tab (not persisted); `None` ends the preview. Does not
|
|
/// apply it: call [`apply`].
|
|
pub fn set_preview(app: &AppHandle, theme: Option<ThemeChoice>) -> Result<(), String> {
|
|
let themes = themes(app)?;
|
|
if let Some(theme) = &theme {
|
|
theme.validate(themes.site)?;
|
|
}
|
|
state(app)?.preview = theme;
|
|
Ok(())
|
|
}
|
|
|
|
/// Applies a theme to every site tab and persists it. `None` (or the site's own preset without an
|
|
/// accent) restores the site's own look.
|
|
#[tauri::command]
|
|
pub(crate) async fn theme_set(webview: Webview, app: AppHandle, theme: Option<ThemeChoice>) -> Result<(), String> {
|
|
shell_only(&webview)?;
|
|
set_theme(&app, theme)?;
|
|
apply(&app);
|
|
Ok(())
|
|
}
|
|
|
|
/// The persisted theme (the site's own look when none).
|
|
#[tauri::command]
|
|
pub(crate) async fn theme_get(webview: Webview, app: AppHandle) -> Result<ThemeChoice, String> {
|
|
shell_only(&webview)?;
|
|
current(&app)
|
|
}
|
|
|
|
/// Account and pane themes and the chrome setting.
|
|
#[tauri::command]
|
|
pub(crate) async fn theme_scopes_get(webview: Webview, app: AppHandle) -> Result<ThemeScopes, String> {
|
|
shell_only(&webview)?;
|
|
scopes(&app)
|
|
}
|
|
|
|
/// Saves account and pane themes and re-themes every site tab.
|
|
#[tauri::command]
|
|
pub(crate) async fn theme_scopes_set(webview: Webview, app: AppHandle, scopes: ThemeScopes) -> Result<(), String> {
|
|
shell_only(&webview)?;
|
|
set_scopes(&app, scopes)?;
|
|
apply(&app);
|
|
Ok(())
|
|
}
|
|
|
|
/// The Theme Builder's live preview: an unsaved theme shown in every site tab. `None` ends the
|
|
/// preview and gives every tab its own theme back.
|
|
#[tauri::command]
|
|
pub(crate) async fn theme_preview(webview: Webview, app: AppHandle, theme: Option<ThemeChoice>) -> Result<(), String> {
|
|
shell_only(&webview)?;
|
|
set_preview(&app, theme)?;
|
|
apply(&app);
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn handler() -> impl Fn(tauri::ipc::Invoke<tauri::Wry>) -> bool + Send + Sync + 'static {
|
|
tauri::generate_handler![theme_get, theme_set, theme_preview, theme_scopes_get, theme_scopes_set]
|
|
}
|