Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kfiven committed Oct 21, 2023
2 parents 2a4d7da + 095b8fc commit ecb6984
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 12 deletions.
36 changes: 36 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ tauri-build = { version = "1.2.1", features = [] }

[dependencies]
serde_json = "1.0.91"
serde = { version = "1.0.152", features = ["derive"] }
tauri = { version = "1.2.3", features = ["api-all", "devtools", "updater"] }
serde = { version = "1.0.147", features = ["derive"] }
tauri = { version = "1.2.3", features = ["api-all", "devtools", "system-tray", "updater"] }

[features]
# by default Tauri runs in production mode
Expand Down
34 changes: 24 additions & 10 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

#[cfg(target_os = "macos")]
mod menu;
mod tray;

fn main() {
let builder = tauri::Builder::default();
let builder = tauri::Builder::default();

#[cfg(target_os = "macos")]
let builder = builder.menu(menu::menu());

let builder = builder
.system_tray(tray::system_tray())
.on_system_tray_event(tray::system_tray_handler);

#[cfg(target_os = "macos")]
let builder = builder.menu(menu::menu());
builder
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(run_event_handler)
}

builder
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
fn run_event_handler<R: tauri::Runtime>(app: &tauri::AppHandle<R>, event: tauri::RunEvent) {
match event {
tauri::RunEvent::WindowEvent { label, event, .. } => {
tray::window_event_handler(app, &label, &event);
}
_ => {}
}
}
87 changes: 87 additions & 0 deletions src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use tauri::{
CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu,
SystemTrayMenuItem, WindowEvent, SystemTrayHandle, Window,
};

const TRAY_LABEL: &'static str = "main-tray";

pub fn window_event_handler<R: tauri::Runtime>(
app: &tauri::AppHandle<R>,
label: &str,
event: &WindowEvent,
) {
match event {
// Prevent Cinny from closing, instead hide it and let it be
// reopened through the tray.
WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
app.get_window(&label).unwrap().hide().unwrap();
app.tray_handle_by_id(TRAY_LABEL)
.unwrap()
.get_item("toggle")
.set_title("Show Cinny")
.unwrap();
}
_ => {}
}
}

/// Build the system tray object
pub fn system_tray() -> SystemTray {
let toggle = CustomMenuItem::new("toggle".to_owned(), "Hide Cinny");
let quit = CustomMenuItem::new("quit".to_owned(), "Quit");
let menu = SystemTrayMenu::new()
.add_item(toggle)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(quit);

tauri::SystemTray::new()
.with_menu(menu)
.with_id(TRAY_LABEL.to_owned())
}

pub fn toggle_window_state<R: tauri::Runtime>(window: Window<R>, tray_handle: SystemTrayHandle<R>) {
// Hide the window if it's visible, show it if not
// `is_visible` returns true for minimized state for whatever reason
if window.is_visible().unwrap() {
window.hide().unwrap();
tray_handle
.get_item("toggle")
.set_title("Show Cinny")
.unwrap();
} else {
window.unminimize().unwrap();
window.show().unwrap();
window.set_focus().unwrap();
tray_handle
.get_item("toggle")
.set_title("Hide Cinny")
.unwrap();
};
}

pub fn system_tray_handler<R: tauri::Runtime>(app: &tauri::AppHandle<R>, event: SystemTrayEvent) {
let tray_handle = match app.tray_handle_by_id(TRAY_LABEL) {
Some(h) => h,
None => return,
};
let window = app.get_window("main").unwrap();

match event {
SystemTrayEvent::LeftClick { .. } => {
toggle_window_state(window, tray_handle);
}
SystemTrayEvent::MenuItemClick { id, .. } => {
match id.as_str() {
"quit" => {
app.exit(0);
}
"toggle" => {
toggle_window_state(window, tray_handle)
}
_ => {}
}
}
_ => {}
}
}
5 changes: 5 additions & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
],
"security": {
"csp": "script-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'"
},
"systemTray": {
"iconPath": "icons/32x32.png",
"iconAsTemplate": true,
"menuOnLeftClick": false
}
}
}

0 comments on commit ecb6984

Please sign in to comment.