Skip to content

Commit

Permalink
feat: hide window on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi authored Sep 9, 2022
1 parent f32c5ba commit 7597d33
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
27 changes: 25 additions & 2 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ anyhow = "1.0"
dirs = "4.0.0"
open = "2.1.1"
log = "0.4.14"
ctrlc = "3.2.3"
dunce = "1.0.2"
log4rs = "1.0.0"
nanoid = "0.4.0"
Expand Down
45 changes: 34 additions & 11 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
};
use tauri::{
api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
WindowEvent,
};

fn main() -> std::io::Result<()> {
Expand Down Expand Up @@ -176,19 +177,41 @@ fn main() -> std::io::Result<()> {
builder = builder.menu(Menu::new().add_submenu(submenu_file));
}

builder
let app = builder
.build(context)
.expect("error while running tauri application")
.run(|app_handle, e| match e {
tauri::RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
}
tauri::RunEvent::Exit => {
resolve::resolve_reset(app_handle);
api::process::kill_children();
.expect("error while running tauri application");

let app_handle = app.app_handle();
ctrlc::set_handler(move || {
resolve::resolve_reset(&app_handle);
app_handle.exit(0);
})
.expect("error when exiting.");

app.run(|app_handle, e| match e {
tauri::RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
}
tauri::RunEvent::Exit => {
resolve::resolve_reset(app_handle);
api::process::kill_children();
}
#[cfg(target_os = "macos")]
tauri::RunEvent::WindowEvent { label, event, .. } => {
if label == "main" {
match event {
WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
app_handle.get_window("main").map(|win| {
let _ = win.hide();
});
}
_ => {}
}
}
_ => {}
});
}
_ => {}
});

Ok(())
}
5 changes: 4 additions & 1 deletion src/pages/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ const Layout = () => {

useEffect(() => {
window.addEventListener("keydown", (e) => {
if (e.key === "Escape") appWindow.close();
if (e.key === "Escape") {
if (OS === "macos") appWindow.hide();
else appWindow.close();
}
});

listen("verge://refresh-clash-config", async () => {
Expand Down

0 comments on commit 7597d33

Please sign in to comment.