feat: add switch tray icon (#75)

This commit is contained in:
BiggerRain
2024-12-26 14:22:20 +08:00
committed by GitHub
parent c1c3c85f1b
commit 035b62d3ee
8 changed files with 32 additions and 13 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 589 B

After

Width:  |  Height:  |  Size: 561 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 597 B

After

Width:  |  Height:  |  Size: 601 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@@ -55,6 +55,7 @@ pub fn run() {
get_current_shortcut,
change_autostart,
hide_coco,
switch_tray_icon,
])
.setup(|app| {
init(app.app_handle());
@@ -244,20 +245,35 @@ fn handle_hide_coco(app: &AppHandle) {
}
}
fn switch_tray_icon(app_handle: &AppHandle, is_dark_mode: bool) {
let icon_path: Vec<u8> = if is_dark_mode {
include_bytes!("../icons/dark@2x.png").to_vec()
#[tauri::command]
fn switch_tray_icon(app: tauri::AppHandle, is_dark_mode: bool) {
let app_handle = app.app_handle();
println!("is_dark_mode: {}", is_dark_mode);
const DARK_ICON_PATH: &[u8] = include_bytes!("../icons/dark@2x.png");
const LIGHT_ICON_PATH: &[u8] = include_bytes!("../icons/light@2x.png");
let icon_path: &[u8] = if is_dark_mode {
DARK_ICON_PATH
} else {
include_bytes!("../icons/light@2x.png").to_vec()
LIGHT_ICON_PATH
};
let Some(tray) = app_handle.tray_by_id("tray") else {
return;
let tray = match app_handle.tray_by_id("tray") {
Some(tray) => tray,
None => {
eprintln!("Tray with ID 'tray' not found");
return;
}
};
tray.set_icon(Some(
tauri::image::Image::from_bytes(&icon_path).expect("Failed to load icon"),
))
.unwrap();
if let Err(e) = tray.set_icon(Some(
tauri::image::Image::from_bytes(icon_path)
.unwrap_or_else(|e| panic!("Failed to load icon from bytes: {}", e)),
)) {
eprintln!("Failed to set tray icon: {}", e);
}
}
fn enable_tray(app: &mut tauri::App) {
@@ -316,9 +332,6 @@ fn enable_tray(app: &mut tauri::App) {
})
.build(app)
.unwrap();
let app_handle = app.handle();
switch_tray_icon(&app_handle, false);
}
#[allow(dead_code)]

View File

@@ -13,6 +13,10 @@ const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
export function ThemeProvider({ children }: { children: React.ReactNode }) {
const { activeTheme: theme, setTheme } = useThemeStore();
async function switchTrayIcon(value: "dark" | "light") {
await invoke("switch_tray_icon", { isDarkMode: value === "dark" });
}
useEffect(() => {
// Apply theme class to document
const root = window.document.documentElement;
@@ -24,8 +28,10 @@ export function ThemeProvider({ children }: { children: React.ReactNode }) {
? "dark"
: "light";
root.classList.add(systemTheme);
switchTrayIcon(systemTheme);
} else {
root.classList.add(theme);
switchTrayIcon(theme);
}
if (isTauri()) getAppTheme();
}, [theme]);