Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update tray icon and add on right click event for linux #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified src-tauri/icons/tray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
183 changes: 85 additions & 98 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

use tauri::Manager;
use tauri::{CustomMenuItem, SystemTray, SystemTrayEvent, SystemTrayMenu, PhysicalPosition};
use tauri::{CustomMenuItem, PhysicalPosition, SystemTray, SystemTrayEvent, SystemTrayMenu};
use tauri::{Manager, PhysicalSize};

mod auto_start;

Expand Down Expand Up @@ -42,7 +42,8 @@ impl<R: Runtime> WindowExt for Window<R> {
if remove_tool_bar {
let close_button = id.standardWindowButton_(NSWindowButton::NSWindowCloseButton);
let _: () = msg_send![close_button, setHidden: YES];
let min_button = id.standardWindowButton_(NSWindowButton::NSWindowMiniaturizeButton);
let min_button =
id.standardWindowButton_(NSWindowButton::NSWindowMiniaturizeButton);
let _: () = msg_send![min_button, setHidden: YES];
let zoom_button = id.standardWindowButton_(NSWindowButton::NSWindowZoomButton);
let _: () = msg_send![zoom_button, setHidden: YES];
Expand All @@ -65,100 +66,86 @@ impl<R: Runtime> WindowExt for Window<R> {

#[tauri::command]
fn set_review_count(app_handle: tauri::AppHandle, count: &str) {
let mut rev_count = count.to_string();
rev_count.insert_str(0, " ");
#[cfg(target_os = "macos")]
app_handle
.tray_handle()
.set_title(&rev_count)
.unwrap();
let mut rev_count = count.to_string();
rev_count.insert_str(0, " ");
#[cfg(target_os = "macos")]
app_handle.tray_handle().set_title(&rev_count).unwrap();
}

fn main() {
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let tray_menu = SystemTrayMenu::new()
.add_item(quit);
let system_tray = SystemTray::new()
.with_menu(tray_menu);

tauri::Builder::default()
.system_tray(system_tray)
.on_system_tray_event(move |app, event| match event {
SystemTrayEvent::LeftClick {
position,
size,
..
} => {
let w = app.get_window("main").unwrap();
let visible = w.is_visible().unwrap();
if visible {
w.hide().unwrap();
} else {
let window_size = w.outer_size().unwrap();
let physical_pos = PhysicalPosition {
fn open_window(
app_handle: &tauri::AppHandle,
position: PhysicalPosition<f64>,
size: PhysicalSize<f64>,
) {
let w = app_handle.get_window("main").unwrap();
let visible = w.is_visible().unwrap();
if visible {
w.hide().unwrap();
} else {
let window_size = w.outer_size().unwrap();
let physical_pos = PhysicalPosition {
x: position.x as i32 + (size.width as i32 / 2) - (window_size.width as i32 / 2),
y: position.y as i32 - window_size.height as i32
};

let _ = w.set_position(tauri::Position::Physical(physical_pos));
w.show().unwrap();
w.set_focus().unwrap();
}
}
SystemTrayEvent::RightClick {
position: _,
size: _,
..
} => {
println!("system tray received a right click");
}
SystemTrayEvent::DoubleClick {
position: _,
size: _,
..
} => {
println!("system tray received a double click");
}
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"quit" => {
std::process::exit(0);
}
_ => {}
},
_ => {}
})
.on_window_event(|event| match event.event() {
tauri::WindowEvent::CloseRequested { api, .. } => {
// don't kill the app when the user clicks close. this is important
event.window().hide().unwrap();
api.prevent_close();
}
tauri::WindowEvent::Focused(false) => {
// hide the window automatically when the user
// clicks out. this is for a matter of taste.
event.window().hide().unwrap();
}
_ => {}
})
.invoke_handler(tauri::generate_handler![set_review_count])
.plugin(auto_start::init(
None,
))
.setup(|app| {
#[cfg(target_os = "macos")]
// don't show on the taskbar/springboard
app.set_activation_policy(tauri::ActivationPolicy::Accessory);

let window = app.get_window("main").unwrap();
#[cfg(target_os = "macos")]
window.set_transparent_titlebar(true, true);

// this is a workaround for the window to always show in current workspace.
// see https://github.com/tauri-apps/tauri/issues/2801
window.set_always_on_top(true).unwrap();

Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
y: position.y as i32 - window_size.height as i32,
};

let _ = w.set_position(tauri::Position::Physical(physical_pos));
w.show().unwrap();
w.set_focus().unwrap();
}
}

fn main() {
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let tray_menu = SystemTrayMenu::new().add_item(quit);
let system_tray = SystemTray::new().with_menu(tray_menu);

tauri::Builder::default()
.system_tray(system_tray)
.on_system_tray_event(move |app, event| match event {
SystemTrayEvent::LeftClick { position, size, .. } => open_window(app, position, size),
#[cfg(target_os = "linux")]
SystemTrayEvent::RightClick { position, size, .. } =>
{
open_window(app, position, size)
}
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"quit" => {
std::process::exit(0);
}
_ => {}
},
_ => {}
})
.on_window_event(|event| match event.event() {
tauri::WindowEvent::CloseRequested { api, .. } => {
// don't kill the app when the user clicks close. this is important
event.window().hide().unwrap();
api.prevent_close();
}
tauri::WindowEvent::Focused(false) => {
// hide the window automatically when the user
// clicks out. this is for a matter of taste.
event.window().hide().unwrap();
}
_ => {}
})
.invoke_handler(tauri::generate_handler![set_review_count])
.plugin(auto_start::init(None))
.setup(|app| {
// don't show on the taskbar/springboard
#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Accessory);

let window = app.get_window("main").unwrap();
#[cfg(target_os = "macos")]
window.set_transparent_titlebar(true, true);

// this is a workaround for the window to always show in current workspace.
// see https://github.com/tauri-apps/tauri/issues/2801
window.set_always_on_top(true).unwrap();

Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
27 changes: 27 additions & 0 deletions tray.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.