feat: add switch tray icon (#75)
|
Before Width: | Height: | Size: 589 B After Width: | Height: | Size: 561 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 597 B After Width: | Height: | Size: 601 B |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 1.9 KiB |
@@ -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)]
|
||||
|
||||
@@ -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]);
|
||||
|
||||