-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tauri): split system_tray to another file
- Loading branch information
Showing
9 changed files
with
116 additions
and
43 deletions.
There are no files selected for viewing
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
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
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,68 @@ | ||
use std::path::PathBuf; | ||
use tauri::{Builder, Icon, Manager, Wry}; | ||
use tauri::{ | ||
menu::{MenuBuilder, MenuItemBuilder}, | ||
tray::{ClickType, TrayIconBuilder}, | ||
}; | ||
use tauri::path::BaseDirectory; | ||
|
||
/** | ||
* We could do this by passing the object into a custom function that adds the commands but I wanted | ||
* to practice more with rust. Plus it makes the setup cleaner. | ||
* Switch to a function that takes and returns tauri::Builder<Wry> or uses a reference if we need to | ||
* switch it. | ||
*/ | ||
pub trait PomatezTray { | ||
fn set_pomatez_system_tray(self) -> tauri::Builder<Wry>; | ||
} | ||
|
||
impl PomatezTray for Builder<Wry> { | ||
|
||
/* | ||
* The icon is updated after rendering on the frontend so that is handled in the commands file. | ||
* However the initial setup and behavior is handled here. | ||
*/ | ||
fn set_pomatez_system_tray(self) -> tauri::Builder<Wry> { | ||
println!("Setting system tray"); | ||
self.setup(|app| { | ||
// Was defined in tauri.config.json to start in v1 | ||
// That was created with an id of 1 though this gives more control | ||
|
||
let show = MenuItemBuilder::with_id("show", "Show").build(app); | ||
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app); | ||
let menu = MenuBuilder::new(app).items(&[&show, &quit]).build()?; | ||
|
||
let icon_path = app.path().resolve::<PathBuf>("icons/icon.png".into(), BaseDirectory::Resource) | ||
.expect("failed to resolve icon path, this should not happen as it is an internal file"); | ||
|
||
println!("Icon path: {:?}", icon_path); | ||
|
||
let _ = TrayIconBuilder::new() | ||
.menu(&menu) | ||
.tooltip("Pomatez") | ||
.on_menu_event(move |app, event| match event.id().as_ref() { | ||
"show" => { | ||
let window = app.get_window("main").unwrap(); | ||
window.show().unwrap(); | ||
window.set_focus().unwrap(); | ||
} | ||
"quit" => { | ||
app.exit(0); | ||
} | ||
_ => {} | ||
}) | ||
.on_tray_event(|tray, event| { | ||
if event.click_type == ClickType::Left { | ||
let app = tray.app_handle(); | ||
let window = app.get_window("main").unwrap(); | ||
window.show().unwrap(); | ||
window.set_focus().unwrap(); | ||
} | ||
}) | ||
.icon(Icon::File(icon_path)) | ||
.build(app)?; | ||
Ok(()) | ||
}) | ||
} | ||
} |
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