Skip to content

Commit

Permalink
feat: add switch tray icon (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
RainyNight9 authored Dec 26, 2024
1 parent c1c3c85 commit 035b62d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 13 deletions.
Binary file modified src-tauri/icons/dark@1x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/dark@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/dark@3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/light@1x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/light@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/light@3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 26 additions & 13 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn run() {
get_current_shortcut,
change_autostart,
hide_coco,
switch_tray_icon,
])
.setup(|app| {
init(app.app_handle());
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions src/contexts/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand Down

0 comments on commit 035b62d

Please # to comment.