Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

feat: Allow Tauri window to follow system theme by default and simplify injected theme detection logic.

Tw93f1a6bbd3f419
diff --git a/package.json b/package.json
index a0ee76e6..6bc50b77 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "pake-cli",
- "version": "3.7.3",
+ "version": "3.7.4",
"description": "🤱🏻 Turn any webpage into a desktop app with one command. 🤱🏻 一键打包网页生成轻量桌面应用。",
"engines": {
"node": ">=18.0.0"
diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs
index 4cb2fd1d..17b8fb9f 100644
--- a/src-tauri/src/app/window.rs
+++ b/src-tauri/src/app/window.rs
@@ -153,9 +153,13 @@ pub fn set_window(app: &mut App, config: &PakeConfig, tauri_config: &Config) ->
};
window_builder = window_builder.title_bar_style(title_bar_style);
- if window_config.dark_mode {
- window_builder = window_builder.theme(Some(Theme::Dark));
- }
+ // Default to following system theme (None), only force dark when explicitly set
+ let theme = if window_config.dark_mode {
+ Some(Theme::Dark)
+ } else {
+ None // Follow system theme
+ };
+ window_builder = window_builder.theme(theme);
}
// Windows and Linux: set data_directory before proxy_url
diff --git a/src-tauri/src/inject/theme_refresh.js b/src-tauri/src/inject/theme_refresh.js
index b86d7dcc..4e21a9a7 100644
--- a/src-tauri/src/inject/theme_refresh.js
+++ b/src-tauri/src/inject/theme_refresh.js
@@ -1,124 +1,57 @@
document.addEventListener("DOMContentLoaded", () => {
- // Helper: Calculate brightness from RGB color
- const isDarkColor = (color) => {
- if (!color) return false;
- const match = color.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
- if (!match) return false;
- const r = parseInt(match[1]);
- const g = parseInt(match[2]);
- const b = parseInt(match[3]);
- // Standard luminance formula
- const luminance = 0.299 * r + 0.587 * g + 0.114 * b;
- return luminance < 128;
- };
-
- // Debounce helper
const debounce = (func, wait) => {
let timeout;
- return function executedFunction(...args) {
- const later = () => {
- clearTimeout(timeout);
- func(...args);
- };
+ return (...args) => {
clearTimeout(timeout);
- timeout = setTimeout(later, wait);
+ timeout = setTimeout(() => func(...args), wait);
};
};
- // Function to detect and send theme to Rust
const updateTheme = () => {
- let mode = "light";
- let detected = false;
-
- // Strategy 1: Explicit DOM Class/Attribute (High Priority)
- // Many apps use specific classes for hard-coded themes
const doc = document.documentElement;
const body = document.body;
+ let mode = null;
- const isExplicitDark =
+ // Check for explicit theme classes or attributes
+ const isDark =
doc.classList.contains("dark") ||
body.classList.contains("dark") ||
doc.getAttribute("data-theme") === "dark" ||
body.getAttribute("data-theme") === "dark" ||
doc.style.colorScheme === "dark";
- const isExplicitLight =
+ const isLight =
doc.classList.contains("light") ||
body.classList.contains("light") ||
doc.getAttribute("data-theme") === "light" ||
body.getAttribute("data-theme") === "light" ||
doc.style.colorScheme === "light";
- if (isExplicitDark) {
- mode = "dark";
- detected = true;
- } else if (isExplicitLight) {
- mode = "light";
- detected = true;
- }
-
- // Strategy 2: Computed Background Color (Fallback & Verification)
- // If no explicit class is found, or to double-check, look at the actual background color.
- // This is useful when the site relies purely on CSS media queries without classes.
- if (!detected) {
- const bodyBg = window.getComputedStyle(document.body).backgroundColor;
- const htmlBg = window.getComputedStyle(
- document.documentElement,
- ).backgroundColor;
-
- // Check body first, then html
- if (bodyBg && bodyBg !== "rgba(0, 0, 0, 0)" && bodyBg !== "transparent") {
- mode = isDarkColor(bodyBg) ? "dark" : "light";
- } else if (
- htmlBg &&
- htmlBg !== "rgba(0, 0, 0, 0)" &&
- htmlBg !== "transparent"
- ) {
- mode = isDarkColor(htmlBg) ? "dark" : "light";
- } else {
- // Strategy 3: System Preference (Last Resort)
- if (
- window.matchMedia &&
- window.matchMedia("(prefers-color-scheme: dark)").matches
- ) {
- mode = "dark";
- }
- }
- }
+ if (isDark) mode = "dark";
+ else if (isLight) mode = "light";
- // Send to Rust
- if (window.__TAURI__ && window.__TAURI__.core) {
+ // Only invoke Rust command if an explicit theme override is detected
+ if (mode && window.__TAURI__?.core) {
window.__TAURI__.core.invoke("update_theme_mode", { mode });
}
};
- // Debounced version of updateTheme
const debouncedUpdateTheme = debounce(updateTheme, 200);
- // Initial check
- // Delay slightly to ensure styles are applied
- setTimeout(updateTheme, 100);
-
- // Watch for system theme changes
- window
- .matchMedia("(prefers-color-scheme: dark)")
- .addEventListener("change", updateTheme);
+ // Initial check with delay to allow site to render
+ setTimeout(updateTheme, 500);
// Watch for DOM changes
- // We observe attributes for class changes, and also style changes just in case
- const observer = new MutationObserver((mutations) => {
- debouncedUpdateTheme();
- });
-
- observer.observe(document.documentElement, {
+ const observer = new MutationObserver(debouncedUpdateTheme);
+ const config = {
attributes: true,
attributeFilter: ["class", "data-theme", "style"],
subtree: false,
- });
+ };
- observer.observe(document.body, {
- attributes: true,
- attributeFilter: ["class", "data-theme", "style"],
- subtree: false,
- });
+ observer.observe(document.documentElement, config);
+ observer.observe(document.body, config);
+
+ // Watch for system theme changes (though window should handle this natively now)
+ window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", updateTheme);
});