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.

fix(runtime): keep the unread badge steady while a background page flashes

Tw9342bf1fbcd103
diff --git a/src-tauri/src/app/invoke.rs b/src-tauri/src/app/invoke.rs
index 02184481..39df6c8d 100644
--- a/src-tauri/src/app/invoke.rs
+++ b/src-tauri/src/app/invoke.rs
@@ -86,10 +86,32 @@ pub fn parse_title_unread_count(title: &str) -> Option<i64> {
Some(parsed.min(MAX_BADGE_COUNT))
}
+/// The main window's last document title, read again when the window takes focus.
+static LAST_TITLE: Mutex<String> = Mutex::new(String::new());
+
+/// The title count to keep. A page in the background often flashes its title,
+/// swapping `(1) Inbox` for `New message from ...` every second; a title
+/// without a count clears the badge only while the window is in front, where
+/// the user is reading, so the badge does not blink with the flash.
+fn next_title_count(parsed: i64, previous: i64, focused: bool) -> i64 {
+ if parsed == 0 && !focused {
+ previous
+ } else {
+ parsed
+ }
+}
+
/// Updates the app unread badge when the main window's document title changes,
/// unless the page has taken explicit control via `navigator.setAppBadge`.
-pub fn sync_title_unread_badge(app: &AppHandle, title: &str) {
- let parsed = parse_title_unread_count(title).unwrap_or(0);
+pub fn sync_title_unread_badge(app: &AppHandle, title: &str, focused: bool) {
+ if let Ok(mut last) = LAST_TITLE.lock() {
+ title.clone_into(&mut last);
+ }
+ let parsed = next_title_count(
+ parse_title_unread_count(title).unwrap_or(0),
+ TITLE_BADGE_COUNT.load(Ordering::SeqCst),
+ focused,
+ );
let previous = TITLE_BADGE_COUNT.swap(parsed, Ordering::SeqCst);
if EXPLICIT_BADGE_ACTIVE.load(Ordering::SeqCst) {
return;
@@ -101,6 +123,13 @@ pub fn sync_title_unread_badge(app: &AppHandle, title: &str) {
}
}
+/// Re-reads the main window's last title once it is in front, so a count the
+/// page dropped while in the background clears when the user comes back.
+pub fn resync_title_unread_badge(app: &AppHandle) {
+ let title = LAST_TITLE.lock().map(|t| t.clone()).unwrap_or_default();
+ sync_title_unread_badge(app, &title, true);
+}
+
fn apply_badge(app: &AppHandle, count: Option<i64>) -> Result<(), String> {
let label = normalize_badge_count(count).map(|n| n.to_string());
apply_badge_label(app, label.as_deref())
@@ -1807,6 +1836,17 @@ mod tests {
}
}
+ #[test]
+ fn a_title_without_a_count_clears_the_badge_only_in_front() {
+ // A background page flashing between "(1) Inbox" and a message keeps 1.
+ assert_eq!(next_title_count(0, 1, false), 1);
+ // In front, or once focused again, the count follows the title.
+ assert_eq!(next_title_count(0, 1, true), 0);
+ // New counts always apply, in front or not.
+ assert_eq!(next_title_count(3, 1, false), 3);
+ assert_eq!(next_title_count(2, 5, true), 2);
+ }
+
#[test]
fn windows_badge_renderer_formats_labels_and_paints_opaque_glyph_pixels() {
assert_eq!(badge_overlay_text("7"), "7");
diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs
index 950fc17e..427b04ca 100644
--- a/src-tauri/src/app/window.rs
+++ b/src-tauri/src/app/window.rs
@@ -1189,7 +1189,11 @@ fn build_window(
let sync_tab_title = use_native_window_tabbing;
window_builder = window_builder.on_document_title_changed(move |window, title| {
if window.label() == "pake" {
- crate::app::invoke::sync_title_unread_badge(window.app_handle(), &title);
+ crate::app::invoke::sync_title_unread_badge(
+ window.app_handle(),
+ &title,
+ window.is_focused().unwrap_or(true),
+ );
}
#[cfg(target_os = "macos")]
if sync_tab_title && !title.trim().is_empty() {
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 35e2bebd..c1e990ea 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -542,8 +542,11 @@ pub fn run_app() {
Ok(())
})
.on_window_event(move |_window, _event| {
- #[cfg(target_os = "macos")]
if let tauri::WindowEvent::Focused(true) = _event {
+ if _window.label() == "pake" {
+ app::invoke::resync_title_unread_badge(_window.app_handle());
+ }
+ #[cfg(target_os = "macos")]
if let Some(webview) = _window.app_handle().get_webview_window(_window.label()) {
app::menu::sync_always_on_top_check(&webview);
}