mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented Discard changes button
This commit is contained in:
1 parent
4e875958a7
commit
e4e98eda3f
17 files changed
+564
-43
No files matched your search
@@ -5,21 +5,94 @@
|
||||
// it to actually accept connections, then create the app window with an
|
||||
// initialization script that hands the frontend its API base
|
||||
// (`window.__KONDUKTOR_API__`). The sidecar is killed when the app exits.
|
||||
//
|
||||
// Quitting ASKS FIRST. Closing the window or Cmd/Ctrl+Q is prevented and
|
||||
// becomes a `konduktor://quit-requested` event; the frontend (QuitGuard.tsx)
|
||||
// acknowledges it, checks for unsaved changes and either quits (`quit_now`) or
|
||||
// asks Save / Discard / Cancel. The sidecar must outlive that question — a
|
||||
// Save or Discard runs in it — so it is killed only once the app really exits.
|
||||
// If the webview does not acknowledge within ~2 s (hung, or not loaded yet),
|
||||
// the app quits anyway: a broken page must never make the app unquittable.
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
|
||||
use tauri::{Manager, WebviewUrl, WebviewWindowBuilder};
|
||||
use tauri::{AppHandle, Emitter, Manager, RunEvent, WebviewUrl, WebviewWindowBuilder, WindowEvent};
|
||||
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
|
||||
/// Holds the running sidecar child so we can terminate it on exit.
|
||||
struct Sidecar(Mutex<Option<CommandChild>>);
|
||||
|
||||
/// Quit handshake state: `allowed` once the frontend (or the timeout) has said
|
||||
/// quit; `acked` once the frontend has seen the current request.
|
||||
struct Quit {
|
||||
allowed: AtomicBool,
|
||||
acked: AtomicBool,
|
||||
}
|
||||
|
||||
fn kill_sidecar(app: &AppHandle) {
|
||||
if let Some(child) = app.state::<Sidecar>().0.lock().unwrap().take() {
|
||||
let _ = child.kill();
|
||||
}
|
||||
}
|
||||
|
||||
fn exit_now(app: &AppHandle) {
|
||||
app.state::<Quit>().allowed.store(true, Ordering::SeqCst);
|
||||
kill_sidecar(app);
|
||||
app.exit(0);
|
||||
}
|
||||
|
||||
/// Hand the quit decision to the frontend, with a deadline for it to answer.
|
||||
fn request_quit(app: &AppHandle) {
|
||||
app.state::<Quit>().acked.store(false, Ordering::SeqCst);
|
||||
if app.emit("konduktor://quit-requested", ()).is_err() {
|
||||
exit_now(app);
|
||||
return;
|
||||
}
|
||||
let handle = app.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
if !handle.state::<Quit>().acked.load(Ordering::SeqCst) {
|
||||
exit_now(&handle);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The frontend has seen the quit request (it may now take as long as it likes).
|
||||
#[tauri::command]
|
||||
fn quit_ack(app: AppHandle) {
|
||||
app.state::<Quit>().acked.store(true, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
/// The user chose Cancel: forget the request.
|
||||
#[tauri::command]
|
||||
fn quit_cancel(app: AppHandle) {
|
||||
app.state::<Quit>().acked.store(false, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
/// Quit for real (nothing unsaved, or Save / Discard has finished).
|
||||
#[tauri::command]
|
||||
fn quit_now(app: AppHandle) {
|
||||
exit_now(&app);
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.manage(Sidecar(Mutex::new(None)))
|
||||
.manage(Quit { allowed: AtomicBool::new(false), acked: AtomicBool::new(false) })
|
||||
.invoke_handler(tauri::generate_handler![quit_ack, quit_cancel, quit_now])
|
||||
.on_window_event(|window, event| {
|
||||
if let WindowEvent::CloseRequested { api, .. } = event {
|
||||
let app = window.app_handle();
|
||||
if !app.state::<Quit>().allowed.load(Ordering::SeqCst) {
|
||||
api.prevent_close();
|
||||
request_quit(app);
|
||||
}
|
||||
}
|
||||
})
|
||||
.setup(|app| {
|
||||
if cfg!(debug_assertions) {
|
||||
app.handle().plugin(
|
||||
@@ -99,12 +172,17 @@ pub fn run() {
|
||||
})
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building the Tauri application")
|
||||
.run(|app_handle, event| {
|
||||
// Terminate the sidecar when the app is quitting.
|
||||
if let tauri::RunEvent::ExitRequested { .. } = event {
|
||||
if let Some(child) = app_handle.state::<Sidecar>().0.lock().unwrap().take() {
|
||||
let _ = child.kill();
|
||||
.run(|app_handle, event| match event {
|
||||
// Cmd/Ctrl+Q, the app menu's Quit, the last window closing: ask
|
||||
// first, unless the answer is already in.
|
||||
RunEvent::ExitRequested { api, .. } => {
|
||||
if !app_handle.state::<Quit>().allowed.load(Ordering::SeqCst) {
|
||||
api.prevent_exit();
|
||||
request_quit(app_handle);
|
||||
}
|
||||
}
|
||||
// Really exiting: only now may the backend go.
|
||||
RunEvent::Exit => kill_sidecar(app_handle),
|
||||
_ => {}
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user