-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
System tray #22
System tray #22
Changes from all commits
f7db1ae
db452e8
54f4221
9067660
b4d7b98
ae05c5e
8ffceb0
bca8f47
cb8edc4
4222e5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
use log::debug; | ||
use tauri::Manager; | ||
|
||
use crate::{commands::system_tray::SystemTrayManager, SYSTEMTRAY_MANAGER}; | ||
|
||
#[tauri::command] | ||
pub async fn close_splashscreen(window: tauri::Window) { | ||
debug!("[Core] Closing splash screen"); | ||
// Close splashscreen | ||
if let Some(splashscreen) = window.get_window("splashscreen") { | ||
splashscreen.close().unwrap(); | ||
} | ||
// Show main window | ||
window.get_window("main").unwrap().show().unwrap(); | ||
|
||
// Show the window if the "Start with system tray" config is set to false. | ||
// Otherwise, keep the window hidden until the user clicks the system tray icon to show it. | ||
let manager_guard = SYSTEMTRAY_MANAGER.lock().unwrap(); | ||
let manager: &SystemTrayManager = manager_guard.as_ref().unwrap(); | ||
|
||
if !manager.start_in_tray { | ||
window.get_window("main").unwrap().show().unwrap(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use tauri::Manager; | ||
use tauri::{ | ||
AppHandle, CustomMenuItem, Runtime, SystemTray, SystemTrayEvent, SystemTrayMenu, | ||
}; | ||
|
||
use crate::SYSTEMTRAY_MANAGER; | ||
|
||
const QUIT: &'static str = "quit"; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SystemTrayManager { | ||
pub exit_in_tray: bool, | ||
pub start_in_tray: bool, | ||
} | ||
|
||
impl SystemTrayManager { | ||
pub fn new() -> SystemTrayManager { | ||
SystemTrayManager { exit_in_tray: false, start_in_tray: false, } | ||
} | ||
|
||
// pub fn handle_window_exit<R: Runtime>(&self) -> impl Fn(GlobalWindowEvent<R>) + Send + Sync + 'static { | ||
// return |event| match event.event() { | ||
// tauri::WindowEvent::CloseRequested { api, .. } => { | ||
// event.window().hide().unwrap(); | ||
// api.prevent_close(); | ||
// } | ||
// _ => {} | ||
// } | ||
// } | ||
Comment on lines
+21
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it's commented out, I assume this can be removed? |
||
} | ||
|
||
// Initializes the system tray with menus. | ||
pub fn init_system_tray() -> SystemTray { | ||
// Menus | ||
let menu_quit = CustomMenuItem::new(QUIT, "Quit"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably find a way of localizing this as well, but I understand there's currently not yet a nice way of getting the translations to the rust side. I think doing that might be a bit outside of this PR's scope, especially when you consider the translations should change when the user switches language on the front, so that's probably something to look into after getting this merged. tl;dr: this is fine as is, feel free to ignore. I've opened a separate issue for this in #23. |
||
|
||
let tray_menu = SystemTrayMenu::new() | ||
//.add_native_item(SystemTrayMenuItem::Separator) | ||
.add_item(menu_quit); | ||
|
||
let tray = SystemTray::new().with_menu(tray_menu); | ||
|
||
return tray; | ||
} | ||
|
||
pub fn handle_events<R: Runtime>() -> impl Fn(&AppHandle<R>, SystemTrayEvent) + Send + Sync + 'static | ||
{ | ||
return |app, event| { | ||
let manager_guard = SYSTEMTRAY_MANAGER.lock().unwrap(); | ||
let manager = manager_guard.as_ref().unwrap(); | ||
|
||
match event { | ||
SystemTrayEvent::MenuItemClick { id, .. } => { | ||
match id.as_str() { | ||
QUIT => std::process::exit(0), | ||
_ => {} | ||
} | ||
}, | ||
|
||
// When clicking the tray icon, restore and focus window. | ||
SystemTrayEvent::LeftClick { tray_id, .. } => { | ||
let window = app.get_window("main").unwrap(); | ||
window.show().unwrap(); | ||
window.set_focus().unwrap(); | ||
} | ||
_ => {} | ||
}; | ||
} | ||
} | ||
|
||
#[tauri::command] | ||
pub fn set_exit_in_system_tray(app_handle: AppHandle, status: bool) { | ||
let mut manager_guard = SYSTEMTRAY_MANAGER.lock().unwrap(); | ||
let mut manager: &mut SystemTrayManager = manager_guard.as_mut().unwrap(); | ||
manager.exit_in_tray = status; | ||
} | ||
|
||
#[tauri::command] | ||
pub fn set_start_in_system_tray(app_handle: AppHandle, status: bool) { | ||
let mut manager_guard = SYSTEMTRAY_MANAGER.lock().unwrap(); | ||
let mut manager: &mut SystemTrayManager = manager_guard.as_mut().unwrap(); | ||
manager.start_in_tray = status; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ export interface AppSettings { | |
oscReceivingPort: number; | ||
enableDesktopNotifications: boolean; | ||
enableXSOverlayNotifications: boolean; | ||
exitInSystemTray: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can you refactor this to |
||
startInSystemTray: boolean; | ||
} | ||
Comment on lines
9
to
14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So these interfaces have a In this case, two new fields got added, which means the |
||
|
||
export const APP_SETTINGS_DEFAULT: AppSettings = { | ||
|
@@ -23,6 +25,8 @@ export const APP_SETTINGS_DEFAULT: AppSettings = { | |
oscReceivingPort: 9001, | ||
enableXSOverlayNotifications: false, | ||
enableDesktopNotifications: false, | ||
exitInSystemTray: false, | ||
startInSystemTray: false, | ||
}; | ||
|
||
export type ExecutableReferenceStatus = | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,48 @@ | ||||||
import { Injectable } from '@angular/core'; | ||||||
import { AppSettingsService } from './app-settings.service'; | ||||||
import { debounceTime, filter, first, last, map, pairwise, take } from 'rxjs'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: there's still a few unused imports here :) |
||||||
import { invoke } from '@tauri-apps/api'; | ||||||
|
||||||
const SYSTEM_TRAY_EXIT_COMMAND = 'set_exit_in_system_tray'; | ||||||
const START_WITH_SYSTEM_TRAY_COMMAND = 'set_start_in_system_tray'; | ||||||
|
||||||
@Injectable({ | ||||||
providedIn: 'root', | ||||||
}) | ||||||
export class SystemTrayService { | ||||||
constructor(private readonly _appSettingsService: AppSettingsService) { | ||||||
} | ||||||
|
||||||
async init() { | ||||||
// Update exit in system tray behaviour following the setting. | ||||||
this._appSettingsService.settings | ||||||
.pipe( | ||||||
map(settings => settings.exitInSystemTray), | ||||||
pairwise(), | ||||||
filter(([oldVal, newVal]) => oldVal !== newVal), | ||||||
map(([_, newVal]) => newVal), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Nit: in the case of unused array parameters, they can be left empty! |
||||||
debounceTime(100) | ||||||
) | ||||||
.subscribe(exitInSystemTray => this.updateSystemTrayExit(exitInSystemTray)); | ||||||
|
||||||
// Update start in system tray behaviour following the settings. | ||||||
// Send command only upon loading setings, in order to hide or show the window upon startup. | ||||||
this._appSettingsService.settings | ||||||
.pipe( | ||||||
map(settings => settings.startInSystemTray), | ||||||
pairwise(), | ||||||
filter(([oldVal, newVal]) => oldVal !== newVal), | ||||||
map(([_, newVal]) => newVal), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
debounceTime(100) | ||||||
) | ||||||
.subscribe(startInSystemTray => this.updateSystemTrayStart(startInSystemTray)); | ||||||
} | ||||||
|
||||||
private async updateSystemTrayExit(exitInSystemTray: boolean) { | ||||||
await invoke(SYSTEM_TRAY_EXIT_COMMAND, { status: exitInSystemTray }); | ||||||
} | ||||||
|
||||||
private async updateSystemTrayStart(startInSystemTray: boolean) { | ||||||
await invoke(START_WITH_SYSTEM_TRAY_COMMAND, { status: startInSystemTray }); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here I guess