feat(core): allow listening to event loop events & prevent window close (#2131)

This commit is contained in:
Lucas Fernandes Nogueira
2021-07-06 13:36:37 -03:00
committed by GitHub
parent d69b1cf6d7
commit 8157a68af1
12 changed files with 169 additions and 56 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -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();
}
})
}

View File

@@ -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 = [