mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-01 10:01:07 +02:00
feat(core): allow listening to event loop events & prevent window close (#2131)
This commit is contained in:
committed by
GitHub
parent
d69b1cf6d7
commit
8157a68af1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -16,7 +16,8 @@ mod menu;
|
||||
|
||||
use serde::Serialize;
|
||||
use tauri::{
|
||||
CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowBuilder, WindowUrl,
|
||||
CustomMenuItem, Event, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowBuilder,
|
||||
WindowUrl,
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -98,6 +99,13 @@ fn main() {
|
||||
cmd::perform_request,
|
||||
menu_toggle,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building tauri application")
|
||||
.run(|app_handle, e| {
|
||||
if let Event::CloseRequested { label, api, .. } = e {
|
||||
api.prevent_close();
|
||||
let window = app_handle.get_window(&label).unwrap();
|
||||
window.emit("close-requested", ()).unwrap();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import hotkeys from "hotkeys-js";
|
||||
import { open } from "@tauri-apps/api/shell";
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
import { appWindow, getCurrent } from "@tauri-apps/api/window";
|
||||
|
||||
import Welcome from "./components/Welcome.svelte";
|
||||
import Cli from "./components/Cli.svelte";
|
||||
@@ -24,6 +25,12 @@
|
||||
hotkeys(MENU_TOGGLE_HOTKEY, () => {
|
||||
invoke('menu_toggle');
|
||||
});
|
||||
|
||||
getCurrent().listen('close-requested', async () => {
|
||||
if (await confirm('Are you sure?')) {
|
||||
await appWindow.close()
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const views = [
|
||||
|
||||
Reference in New Issue
Block a user