feat(linux): add tray badge support

This commit is contained in:
mortebrume committed 2026-07-19 22:26:09 +02:00
1 parent c973b3917c
commit 08eeef2658
8 files changed
+160 -9

No files matched your search

+2
View File
@@ -2,6 +2,8 @@ pub mod download;
pub mod runtime_state;
pub mod settings;
pub mod tray;
#[cfg(target_os = "linux")]
pub mod tray_badge;
#[cfg(windows)]
pub mod windows;
+5 -1
View File
@@ -17,7 +17,7 @@ use tauri_plugin_store::StoreExt;
#[cfg(not(target_os = "linux"))]
use tauri::tray::{MouseButton, TrayIconEvent};
const MAIN_TRAY_ID: &str = "main";
pub(crate) const MAIN_TRAY_ID: &str = "main";
const TRAY_MENU_SHOW_ID: &str = "tray_show";
const TRAY_MENU_QUIT_ID: &str = "tray_quit";
@@ -270,6 +270,10 @@ pub fn create_system_tray(app: &AppHandle<crate::BrowserEngine>) -> tauri::Resul
}
tray_builder.build(app)?;
#[cfg(target_os = "linux")]
crate::desktop::tray_badge::reset_badge_state();
Ok(())
}
+128
View File
@@ -0,0 +1,128 @@
//! Unread dot for the Linux system tray icon. Linux trays have no native
//! badge API and the CEF webview never exposes the web Badging API, so the
//! unread indicator is painted onto the icon bitmap and swapped in via
//! `TrayIcon::set_icon`.
use std::sync::atomic::{AtomicBool, Ordering};
use tauri::image::Image;
use tauri::AppHandle;
use crate::desktop::tray::MAIN_TRAY_ID;
static BADGE_SHOWN: AtomicBool = AtomicBool::new(false);
/// Clears the tracked dot state after the tray icon is (re)built.
pub fn reset_badge_state() {
BADGE_SHOWN.store(false, Ordering::Relaxed);
}
fn render_badged_icon(base: &Image<'_>) -> Image<'static> {
let width = base.width();
let height = base.height();
let mut rgba = base.rgba().to_vec();
if width == 0 || height == 0 {
return Image::new_owned(rgba, width, height);
}
let w = width as f32;
let h = height as f32;
let diameter = w.min(h) * 0.4;
let radius = diameter / 2.0;
let cx = w - radius - w * 0.04;
let cy = h - radius - h * 0.04;
let red: [f32; 3] = [0.878, 0.176, 0.176];
let x0 = (cx - radius - 1.0).floor().max(0.0) as u32;
let y0 = (cy - radius - 1.0).floor().max(0.0) as u32;
let x1 = ((cx + radius + 1.0).ceil() as u32).min(width);
let y1 = ((cy + radius + 1.0).ceil() as u32).min(height);
for py in y0..y1 {
for px in x0..x1 {
let dx = px as f32 + 0.5 - cx;
let dy = py as f32 + 0.5 - cy;
let dist = (dx * dx + dy * dy).sqrt();
// Hard edge: antialiased pixels turn into a visible ring once the
// tray premultiplies the pixmap, so keep every dot pixel opaque.
if dist > radius {
continue;
}
let idx = ((py * width + px) * 4) as usize;
rgba[idx] = (red[0] * 255.0).round() as u8;
rgba[idx + 1] = (red[1] * 255.0).round() as u8;
rgba[idx + 2] = (red[2] * 255.0).round() as u8;
rgba[idx + 3] = 255;
}
}
Image::new_owned(rgba, width, height)
}
/// Redraws the tray icon with or without the unread dot. `count` of `None` or
/// `Some(0)` restores the plain icon.
pub fn apply_tray_badge(app: &AppHandle<crate::BrowserEngine>, count: Option<u32>) {
let want_dot = matches!(count, Some(count) if count > 0);
// Swapping the pixmap makes the icon blink, so only touch it on a change.
if BADGE_SHOWN.swap(want_dot, Ordering::Relaxed) == want_dot {
return;
}
let Some(tray) = app.tray_by_id(MAIN_TRAY_ID) else {
BADGE_SHOWN.store(false, Ordering::Relaxed);
return;
};
let Some(base) = app.default_window_icon() else {
BADGE_SHOWN.store(false, Ordering::Relaxed);
return;
};
let result = if want_dot {
tray.set_icon(Some(render_badged_icon(base)))
} else {
tray.set_icon(Some(base.to_owned()))
};
if let Err(error) = result {
log::warn!("Failed to update tray badge: {error}");
BADGE_SHOWN.store(false, Ordering::Relaxed);
}
}
#[tauri::command]
pub fn set_tray_badge(
app: AppHandle<crate::BrowserEngine>,
count: Option<u32>,
) -> Result<(), String> {
apply_tray_badge(&app, count);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn badge_modifies_bottom_right_pixels() {
let width = 32;
let height = 32;
let base = Image::new_owned(vec![0u8; (width * height * 4) as usize], width, height);
let out = render_badged_icon(&base);
let idx = (((height - 7) * width + (width - 7)) * 4) as usize;
assert!(out.rgba()[idx + 3] > 0);
assert_eq!(&out.rgba()[0..4], &[0, 0, 0, 0]);
}
#[test]
fn dimensions_are_preserved() {
let base = Image::new_owned([10u8, 20, 30, 255].repeat(16), 4, 4);
let out = render_badged_icon(&base);
assert_eq!((out.width(), out.height()), (4, 4));
}
}
+2
View File
@@ -313,6 +313,8 @@ pub fn run() {
desktop::tray::get_desktop_runtime_state,
#[cfg(desktop)]
desktop::tray::sync_desktop_settings,
#[cfg(target_os = "linux")]
desktop::tray_badge::set_tray_badge,
#[cfg(windows)]
desktop::windows::snap_overlay::show_snap_overlay,
#[cfg(windows)]
+5 -1
View File
@@ -1,7 +1,7 @@
/**
* Auto-generated TypeScript bindings for Tauri commands
* Generated by tauri-typegen v0.5.0
* Generated at: 2026-07-18T21:46:21.964297+00:00
* Generated at: 2026-07-19T19:29:48.202533519+00:00
* Generator: none
*
* Do not edit manually - regenerate using: cargo tauri-typegen generate
@@ -50,6 +50,10 @@ export async function setStatusBarColor(params: types.SetStatusBarColorParams):
return invoke('set_status_bar_color', params);
}
export async function setTrayBadge(params: types.SetTrayBadgeParams): Promise<void> {
return invoke('set_tray_badge', params);
}
export async function showSnapOverlay(): Promise<void> {
return invoke('show_snap_overlay');
}
+1 -1
View File
@@ -1,7 +1,7 @@
/**
* Auto-generated TypeScript bindings for Tauri commands
* Generated by tauri-typegen v0.5.0
* Generated at: 2026-07-18T21:46:21.964597+00:00
* Generated at: 2026-07-19T19:29:48.202975583+00:00
* Generator: none
*
* Do not edit manually - regenerate using: cargo tauri-typegen generate
+6 -1
View File
@@ -1,7 +1,7 @@
/**
* Auto-generated TypeScript bindings for Tauri commands
* Generated by tauri-typegen v0.5.0
* Generated at: 2026-07-18T21:46:21.963724+00:00
* Generated at: 2026-07-19T19:29:48.201956318+00:00
* Generator: none
*
* Do not edit manually - regenerate using: cargo tauri-typegen generate
@@ -69,6 +69,11 @@ export interface SetStatusBarColorParams {
[key: string]: unknown;
}
export interface SetTrayBadgeParams {
count?: number | null;
[key: string]: unknown;
}
export interface StartWindowTrackingWithTargetParams {
target: WindowTarget;
[key: string]: unknown;
+11 -5
View File
@@ -1,5 +1,7 @@
import { useAtomValue, useSetAtom } from 'jotai';
import * as Sentry from '@sentry/react';
import { type as osType } from '@tauri-apps/plugin-os';
import { setTrayBadge } from '$generated/tauri/commands';
import type { ReactNode } from 'react';
import { useCallback, useEffect, useRef } from 'react';
import { useNavigate } from 'react-router-dom';
@@ -172,11 +174,15 @@ function FaviconUpdater() {
// Only badge with highlight (mention) counts — total unread is too noisy
// for an OS-level app badge.
if (isNativeNotificationTauri()) {
import('@tauri-apps/api/window')
.then(({ getCurrentWindow }) =>
getCurrentWindow().setBadgeCount(highlightTotal > 0 ? highlightTotal : undefined)
)
.catch(() => {});
const badgeCount = highlightTotal > 0 ? highlightTotal : undefined;
if (osType() === 'linux') {
// Linux has no taskbar badge; the count goes on the tray icon instead.
setTrayBadge({ count: badgeCount ?? null }).catch(() => {});
} else {
import('@tauri-apps/api/window')
.then(({ getCurrentWindow }) => getCurrentWindow().setBadgeCount(badgeCount))
.catch(() => {});
}
} else if (highlightTotal > 0) {
navigator.setAppBadge(highlightTotal);
} else {