From 4e7599274d5eab5a328b3f122eb0bd2ef12d5083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 10 Sep 2026 01:11:39 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(app):=20closing=20with=20unsav?= =?UTF-8?q?ed=20work=20asks=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A close request — alt+F4, the titlebar button, a session logout — is answered instead of obeyed when the timeline holds a clip and the project file does not hold the latest edit (the same `autosave_dirty` the ledger's `unsaved` state word reads). The window stays up and raises DESIGN §8's decision notice above the ledger: red spine, `UNSAVED — save ↵ · discard d · stay esc`, and while it is up it owns the keyboard. An empty timeline, or a saved one, closes exactly as before. --- crates/app/src/main.rs | 25 +++++++++++++ crates/app/src/player/library.rs | 40 +++++++++++++++++++++ crates/app/src/tests/layout.rs | 19 ++++++++++ crates/app/src/ui/stance.rs | 62 ++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index eae9b79..984370d 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -463,6 +463,13 @@ struct Player { /// `project_path` is never touched by either path, only a manual save /// writes the real file. recovery_sidecar: Option, + /// Whether the unsaved-work question is on screen: raised by the window's + /// own close request when [`Player::close_needs_answer`] says the timeline + /// holds work the project file does not (user 2026-09-10, "if timeline is + /// not empty and user does alt+f4 or exit somehow, ask user to save"). + /// While it is up it owns the keyboard -- three answers and nothing else + /// ([`ui::stance`]'s root `on_key_down`). + quit_ask: bool, /// Whether `project_path` names a file this window itself put there -- /// loaded from a `.edith` ([`Player::install_project`]) or written by a /// manual save ([`Player::save_project`]) -- rather than one merely @@ -952,6 +959,7 @@ fn main() { autosave_last_edit: None, autosave_last_run: None, recovery_sidecar: None, + quit_ask: false, autosave_armed: false, keymap: keymap.clone(), keys_open: false, @@ -1042,6 +1050,23 @@ fn main() { // Nothing else takes focus, and without it the key listener // above is never reached. window.focus(&player.read(cx).focus); + // The compositor's close request -- alt+F4, the titlebar's + // button, `swaymsg kill`, a session logout -- is the one door + // out of this window (there is no Quit action and nothing in + // the tree calls `cx.quit()`). Answering `false` keeps the + // window up, which is what turns "closing" into a question + // when the timeline holds work no file has. + let asking = player.clone(); + window.on_window_should_close(cx, move |_, cx| { + asking.update(cx, |player, cx| { + if !player.close_needs_answer() { + return true; + } + player.quit_ask = true; + cx.notify(); + false + }) + }); player.update(cx, |player, cx| player.start_autosave(cx)); player }, diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs index 11c671a..94d5184 100644 --- a/crates/app/src/player/library.rs +++ b/crates/app/src/player/library.rs @@ -1561,6 +1561,29 @@ impl Player { cx.notify(); } + /// Whether a close has to be answered instead of obeyed: the timeline + /// holds at least one clip and the project file does not hold the latest + /// edit -- the same `autosave_dirty` the ledger's `unsaved` ghost reads, + /// so what the strip says and what the close asks can never disagree. + /// An empty timeline, or a saved one, closes the way it always did. + pub(crate) fn close_needs_answer(&self) -> bool { + let clips = self.session.as_ref().map_or(0, |session| { + session + .lanes() + .into_iter() + .map(|lane| session.lane_clips(lane).len()) + .sum() + }); + Self::close_asks(clips, self.autosave_dirty) + } + + /// The close question as a value: clips on the timeline and an edit the + /// file does not have. Split out of [`Player::close_needs_answer`] so the + /// rule can be read and tested without a window or a session. + pub(crate) fn close_asks(clips: usize, dirty: bool) -> bool { + clips > 0 && dirty + } + /// Writes the timeline back to its project file. Overwrites silently, like /// an export: the path was chosen once and the notice is the confirmation. pub(crate) fn save_project(&mut self, cx: &mut Context) { @@ -1907,6 +1930,23 @@ impl Player { mod autosave_tests { use super::*; + /// The three cases the user named: an empty timeline closes, a saved one + /// closes, and only work that is both there and unsaved asks first -- + /// the rule the window's close request routes through. + #[test] + fn only_unsaved_clips_ask_before_the_window_closes() { + assert!(!Player::close_asks(0, false)); + assert!( + !Player::close_asks(0, true), + "an empty timeline has nothing to lose" + ); + assert!( + !Player::close_asks(3, false), + "saved work closes without a question" + ); + assert!(Player::close_asks(1, true)); + } + /// The three answers [`Player::recovery_offer`] gives, off nothing but a /// project file and a sidecar's mtimes: younger sidecar offers it, an /// older one or none at all offers nothing. Nowhere near a `Player` or a diff --git a/crates/app/src/tests/layout.rs b/crates/app/src/tests/layout.rs index e0a32d8..70a5e82 100644 --- a/crates/app/src/tests/layout.rs +++ b/crates/app/src/tests/layout.rs @@ -3401,6 +3401,25 @@ fn one_name_per_source_one_stem_helper_and_one_save_door() { stance.contains(r#"room_ghost(player, "unsaved", ActionId::Save"#), "the ledger's unsaved state is not Save's door -- it is a word again" ); + // The close request is intercepted and routed through the predicate, and + // the plate it raises is answered: this repo can build no window in a + // test ([`Player`] needs a `TestAppContext`), so the wiring itself is + // what is guarded -- the hook, the rule it asks, and the three answers. + let main = src_text("main.rs"); + assert!( + main.contains("on_window_should_close") && main.contains("close_needs_answer()"), + "alt+F4 closes the window without asking about unsaved work" + ); + assert!( + stance.contains("if this.quit_ask {") + && stance.contains("this.save_project(cx);") + && stance.contains("window.remove_window()"), + "the unsaved plate is up with no key answering it" + ); + assert!( + stance.contains("UNSAVED — save ↵ · discard d · stay esc"), + "the decision plate speaks prose instead of state + verb·chord" + ); assert!( !src_text("ui/timeband_stance.rs").contains("ActionId::Save"), "Save has two doors: the ledger's state word and the time band's ghost" diff --git a/crates/app/src/ui/stance.rs b/crates/app/src/ui/stance.rs index 859421c..c1ebaea 100644 --- a/crates/app/src/ui/stance.rs +++ b/crates/app/src/ui/stance.rs @@ -620,6 +620,38 @@ fn bench( .child(bench_stance::render(player, bench_h - BENCH_CHROME_H, cx)) } +/// DESIGN §8's "needs a decision" notice, and the only one the room raises: +/// a red 3px spine, the state word, and the verbs that answer it -- state +/// plus verb·chord, never a sentence. It rises where notices rise, directly +/// above the ledger, so it is never over the picture. The keys behind it live +/// in the root `on_key_down` above. +fn decision_plate() -> impl IntoElement { + div() + .id("stance-quit-ask") + .flex_none() + .h(px(LEDGER_H)) + .bg(rgb(DARK_PANEL())) + .border_t_1() + .border_color(rgba(DARK_SEAM())) + .flex() + .items_center() + // The spine is a child, not a border: a border colours all four sides + // at once and the seam above this strip is the panel's, not the + // notice's severity (the clip spine in `bench_stance` is drawn the + // same way for the same reason). + .child(div().flex_none().w(px(3.)).h_full().bg(rgb(STATUS_ERROR()))) + .child( + div() + .pl(px(12.)) + .type_style(type_scale::mono( + type_scale::CHORD_METADATA_MIN_PX, + gpui::FontWeight::MEDIUM, + )) + .text_color(rgb(INK1())) + .child("UNSAVED — save ↵ · discard d · stay esc"), + ) +} + /// Thin strip at the bottom of the centre column: project identity, last /// action, export progress, position. Notices rise from here (DESIGN §5, §8). fn ledger(player: &Player, position: f64, cx: &mut Context) -> impl IntoElement { @@ -856,6 +888,35 @@ pub(crate) fn render( if event.is_held && !repeats(this.repeat_scope(), key, this.keymap.lookup(key, ctrl)) { return; } + // The unsaved-work question (user 2026-09-10: "if timeline is not + // empty and user does alt+f4 or exit somehow, ask user to save the + // file if it's not already saved"). Asked ahead of every branch + // below because while the plate is up it owns the keyboard: three + // answers and nothing else -- `↵` saves and goes, `d` goes without + // saving, `esc` stays, any other key does nothing at all. + if this.quit_ask { + match key { + "enter" => { + // The Save action's own door, so the notice, the + // sidecar cleanup and the dirty flag are the ones a + // ctrl+s writes. corner-cut: a project with no path + // yet says "NOTHING TO SAVE — no file open" and the + // window still goes; this app has no save-as dialog + // anywhere to route to (no file picker exists in the + // tree), and every timeline with a clip on it got a + // path from its first import (`project_path`). + this.save_project(cx); + window.remove_window(); + } + "d" => window.remove_window(), + "escape" => { + this.quit_ask = false; + cx.notify(); + } + _ => {} + } + return; + } // The recovery notice reads a key as more than "answered" (legacy // `render.rs`'s own rule): `enter` loads the sidecar it named, any // other key declines it. Asked before the generic dismiss below @@ -1055,6 +1116,7 @@ pub(crate) fn render( .child(time_band(player, position, cx)) .child(divider(Split::Bench, player.split_drag == Some(Split::Bench), cx)) .child(bench(player, bench_h, window, cx)) + .when(player.quit_ask, |el| el.child(decision_plate())) .child(ledger(player, position, cx)) .when(player.settings_open, |el| { el.child(settings_stance::render(player, window_size, cx))