Skip to content

Commit

Permalink
Tauri: don't close window, hide instead
Browse files Browse the repository at this point in the history
  • Loading branch information
0rvar committed Nov 13, 2023
1 parent 8114bcb commit a686d12
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
22 changes: 15 additions & 7 deletions app/tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

#[cfg(debug_assertions)]
use tauri::{Manager};
use tauri::{RunEvent};
use tauri::Manager;
use tauri::RunEvent;
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_window;

#[macro_use]
mod commands;
mod system_tray;
mod global_shortcuts;
mod main_window;
mod system_tray;
mod updater;

use commands::PomatezCommands;
use global_shortcuts::{PomatezGlobalShortcutsRegister, PomatezGlobalShortcutsSetup};
use system_tray::PomatezTray;
use global_shortcuts::{PomatezGlobalShortcutsSetup, PomatezGlobalShortcutsRegister};

fn main() {
let app = tauri::Builder::default()
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, None))
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
None,
))
.plugin(tauri_plugin_window::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_notification::init())
Expand All @@ -30,6 +34,7 @@ fn main() {
.setup(|app| {
#[cfg(desktop)]
{
main_window::create_main_window(&app.handle());
app.setup_global_shortcuts();
app.set_pomatez_system_tray();
}
Expand All @@ -49,6 +54,9 @@ fn main() {

println!("Pomatez is ready");
}
RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
}
_ => {}
});
}
24 changes: 24 additions & 0 deletions app/tauri/src/main_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use tauri::{AppHandle, Manager, WindowBuilder};

pub static MAIN_WINDOW_ID: &str = "main";

pub fn create_main_window(handle: &AppHandle) {
if handle.get_window(MAIN_WINDOW_ID).is_some() {
return;
}
let window = WindowBuilder::new(handle, MAIN_WINDOW_ID, Default::default())
.inner_size(340f64, 502f64)
.resizable(true)
.title("pomatez")
.build()
.unwrap();

let window_clone = window.clone();
window.on_window_event(move |e| match e {
tauri::WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
window_clone.hide().unwrap();
}
_ => {}
})
}
29 changes: 19 additions & 10 deletions app/tauri/src/system_tray.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::path::PathBuf;
use tauri::{App, Icon, Manager, Runtime};
use tauri::path::BaseDirectory;
use tauri::{
menu::{MenuBuilder, MenuItemBuilder},
tray::{ClickType, TrayIconBuilder},
};
use tauri::path::BaseDirectory;
use tauri::{App, Icon, Manager, Runtime};

use base64;
use base64::Engine;
use base64::engine::general_purpose;
use base64::Engine;

use crate::main_window;

#[tauri::command]
pub fn tray_icon_update<R: Runtime>(data_url: String, window: tauri::Window<R>) {
Expand All @@ -33,7 +35,6 @@ pub fn tray_icon_update<R: Runtime>(data_url: String, window: tauri::Window<R>)
}
}


/**
* 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.
Expand All @@ -46,22 +47,28 @@ pub trait PomatezTray {
}

impl PomatezTray for App {

/*
* 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) {
println!("Setting system tray");
// 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(self);
let quit = MenuItemBuilder::with_id("quit", "Quit").build(self);
let menu = MenuBuilder::new(self).items(&[&show, &quit]).build().expect("failed to build menu");
let menu = MenuBuilder::new(self)
.items(&[&show, &quit])
.build()
.expect("failed to build menu");

let icon_path = self.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");
let icon_path = self
.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",
);

let _ = TrayIconBuilder::new()
.menu(&menu)
Expand All @@ -80,12 +87,14 @@ impl PomatezTray for App {
.on_tray_icon_event(|tray, event| {
if event.click_type == ClickType::Left {
let app = tray.app_handle();
main_window::create_main_window(app);
let window = app.get_window("main").unwrap();
window.show().unwrap();
window.set_focus().unwrap();
}
})
.icon(Icon::File(icon_path))
.build(self).expect("failed to build tray icon");
.build(self)
.expect("failed to build tray icon");
}
}

0 comments on commit a686d12

Please sign in to comment.