-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Incorporate feedback into system tray PR and merge it with new rust c…
…ore structure. Co-authored-by: Neuroblack <[email protected]>
- Loading branch information
Showing
18 changed files
with
523 additions
and
179 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use super::{SystemTrayManager, SYSTEMTRAY_MANAGER}; | ||
|
||
#[tauri::command] | ||
pub async fn set_close_to_system_tray(enabled: bool) { | ||
let mut manager_guard = SYSTEMTRAY_MANAGER.lock().await; | ||
let mut manager: &mut SystemTrayManager = manager_guard.as_mut().unwrap(); | ||
manager.close_to_tray = enabled; | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn set_start_in_system_tray(enabled: bool) { | ||
let mut manager_guard = SYSTEMTRAY_MANAGER.lock().await; | ||
let mut manager: &mut SystemTrayManager = manager_guard.as_mut().unwrap(); | ||
manager.start_in_tray = enabled; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
pub mod commands; | ||
|
||
use tauri::{AppHandle, CustomMenuItem, Runtime, SystemTray, SystemTrayEvent, SystemTrayMenu}; | ||
use tauri::{GlobalWindowEvent, Manager}; | ||
use tokio::sync::Mutex; | ||
|
||
const QUIT: &str = "quit"; | ||
|
||
lazy_static! { | ||
pub static ref SYSTEMTRAY_MANAGER: Mutex<Option<SystemTrayManager>> = Default::default(); | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SystemTrayManager { | ||
pub close_to_tray: bool, | ||
pub start_in_tray: bool, | ||
} | ||
|
||
impl SystemTrayManager { | ||
pub fn new() -> SystemTrayManager { | ||
SystemTrayManager { | ||
close_to_tray: false, | ||
start_in_tray: false, | ||
} | ||
} | ||
} | ||
|
||
pub async fn init() { | ||
// Initialize the system tray manager | ||
let system_tray_manager = SystemTrayManager::new(); | ||
*SYSTEMTRAY_MANAGER.lock().await = Some(system_tray_manager); | ||
} | ||
|
||
// Initializes the system tray with menus. | ||
pub fn init_system_tray() -> SystemTray { | ||
// Menus | ||
let menu_quit = CustomMenuItem::new(QUIT, "Quit"); | ||
|
||
let tray_menu = SystemTrayMenu::new() | ||
//.add_native_item(SystemTrayMenuItem::Separator) | ||
.add_item(menu_quit); | ||
|
||
SystemTray::new().with_menu(tray_menu) | ||
} | ||
|
||
pub fn handle_system_tray_events<R: Runtime>( | ||
) -> impl Fn(&AppHandle<R>, SystemTrayEvent) + Send + Sync + 'static { | ||
|app, event| { | ||
match event { | ||
SystemTrayEvent::MenuItemClick { id, .. } => { | ||
if id.as_str() == QUIT { | ||
std::process::exit(0); | ||
} | ||
} | ||
// When clicking the tray icon, restore and focus window. | ||
SystemTrayEvent::LeftClick { .. } => { | ||
let window = app.get_window("main").unwrap(); | ||
window.show().unwrap(); | ||
window.set_focus().unwrap(); | ||
} | ||
_ => {} | ||
}; | ||
} | ||
} | ||
|
||
pub fn handle_window_events<R: Runtime>() -> impl Fn(GlobalWindowEvent<R>) + Send + Sync + 'static { | ||
|event| { | ||
if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() { | ||
let manager_guard = tauri::async_runtime::block_on(SYSTEMTRAY_MANAGER.lock()); | ||
let manager = manager_guard.as_ref().unwrap(); | ||
if manager.close_to_tray { | ||
event.window().hide().unwrap(); | ||
api.prevent_close(); | ||
} else { | ||
std::process::exit(0); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.