forge
Explore
Log in

Don't you dare go Hollow

Closing with unsaved work asks first

Browse files at this commit

4e7599274d5eab5a328b3f122eb0bd2ef12d5083Ahmet Kaan GümüşParent: 2f9d285
  • feat
    • closing with unsaved work asks first
Show full message
✨ feat(app): closing with unsaved work asks first

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.rsmodified+25 -0
@@ -463,6 +463,13 @@ struct Player {
463463 /// `project_path` is never touched by either path, only a manual save
464464 /// writes the real file.
465465 recovery_sidecar: Option<PathBuf>,
466+ /// Whether the unsaved-work question is on screen: raised by the window's
467+ /// own close request when [`Player::close_needs_answer`] says the timeline
468+ /// holds work the project file does not (user 2026-09-10, "if timeline is
469+ /// not empty and user does alt+f4 or exit somehow, ask user to save").
470+ /// While it is up it owns the keyboard -- three answers and nothing else
471+ /// ([`ui::stance`]'s root `on_key_down`).
472+ quit_ask: bool,
466473 /// Whether `project_path` names a file this window itself put there --
467474 /// loaded from a `.edith` ([`Player::install_project`]) or written by a
468475 /// manual save ([`Player::save_project`]) -- rather than one merely
@@ -952,6 +959,7 @@ fn main() {
952959 autosave_last_edit: None,
953960 autosave_last_run: None,
954961 recovery_sidecar: None,
962+ quit_ask: false,
955963 autosave_armed: false,
956964 keymap: keymap.clone(),
957965 keys_open: false,
@@ -1042,6 +1050,23 @@ fn main() {
10421050 // Nothing else takes focus, and without it the key listener
10431051 // above is never reached.
10441052 window.focus(&player.read(cx).focus);
1053+ // The compositor's close request -- alt+F4, the titlebar's
1054+ // button, `swaymsg kill`, a session logout -- is the one door
1055+ // out of this window (there is no Quit action and nothing in
1056+ // the tree calls `cx.quit()`). Answering `false` keeps the
1057+ // window up, which is what turns "closing" into a question
1058+ // when the timeline holds work no file has.
1059+ let asking = player.clone();
1060+ window.on_window_should_close(cx, move |_, cx| {
1061+ asking.update(cx, |player, cx| {
1062+ if !player.close_needs_answer() {
1063+ return true;
1064+ }
1065+ player.quit_ask = true;
1066+ cx.notify();
1067+ false
1068+ })
1069+ });
10451070 player.update(cx, |player, cx| player.start_autosave(cx));
10461071 player
10471072 },
crates/app/src/player/library.rsmodified+40 -0
@@ -1561,6 +1561,29 @@ impl Player {
15611561 cx.notify();
15621562 }
15631563
1564+ /// Whether a close has to be answered instead of obeyed: the timeline
1565+ /// holds at least one clip and the project file does not hold the latest
1566+ /// edit -- the same `autosave_dirty` the ledger's `unsaved` ghost reads,
1567+ /// so what the strip says and what the close asks can never disagree.
1568+ /// An empty timeline, or a saved one, closes the way it always did.
1569+ pub(crate) fn close_needs_answer(&self) -> bool {
1570+ let clips = self.session.as_ref().map_or(0, |session| {
1571+ session
1572+ .lanes()
1573+ .into_iter()
1574+ .map(|lane| session.lane_clips(lane).len())
1575+ .sum()
1576+ });
1577+ Self::close_asks(clips, self.autosave_dirty)
1578+ }
1579+
1580+ /// The close question as a value: clips on the timeline and an edit the
1581+ /// file does not have. Split out of [`Player::close_needs_answer`] so the
1582+ /// rule can be read and tested without a window or a session.
1583+ pub(crate) fn close_asks(clips: usize, dirty: bool) -> bool {
1584+ clips > 0 && dirty
1585+ }
1586+
15641587 /// Writes the timeline back to its project file. Overwrites silently, like
15651588 /// an export: the path was chosen once and the notice is the confirmation.
15661589 pub(crate) fn save_project(&mut self, cx: &mut Context<Self>) {
@@ -1907,6 +1930,23 @@ impl Player {
19071930 mod autosave_tests {
19081931 use super::*;
19091932
1933+ /// The three cases the user named: an empty timeline closes, a saved one
1934+ /// closes, and only work that is both there and unsaved asks first --
1935+ /// the rule the window's close request routes through.
1936+ #[test]
1937+ fn only_unsaved_clips_ask_before_the_window_closes() {
1938+ assert!(!Player::close_asks(0, false));
1939+ assert!(
1940+ !Player::close_asks(0, true),
1941+ "an empty timeline has nothing to lose"
1942+ );
1943+ assert!(
1944+ !Player::close_asks(3, false),
1945+ "saved work closes without a question"
1946+ );
1947+ assert!(Player::close_asks(1, true));
1948+ }
1949+
19101950 /// The three answers [`Player::recovery_offer`] gives, off nothing but a
19111951 /// project file and a sidecar's mtimes: younger sidecar offers it, an
19121952 /// older one or none at all offers nothing. Nowhere near a `Player` or a
crates/app/src/tests/layout.rsmodified+19 -0
@@ -3401,6 +3401,25 @@ fn one_name_per_source_one_stem_helper_and_one_save_door() {
34013401 stance.contains(r#"room_ghost(player, "unsaved", ActionId::Save"#),
34023402 "the ledger's unsaved state is not Save's door -- it is a word again"
34033403 );
3404+ // The close request is intercepted and routed through the predicate, and
3405+ // the plate it raises is answered: this repo can build no window in a
3406+ // test ([`Player`] needs a `TestAppContext`), so the wiring itself is
3407+ // what is guarded -- the hook, the rule it asks, and the three answers.
3408+ let main = src_text("main.rs");
3409+ assert!(
3410+ main.contains("on_window_should_close") && main.contains("close_needs_answer()"),
3411+ "alt+F4 closes the window without asking about unsaved work"
3412+ );
3413+ assert!(
3414+ stance.contains("if this.quit_ask {")
3415+ && stance.contains("this.save_project(cx);")
3416+ && stance.contains("window.remove_window()"),
3417+ "the unsaved plate is up with no key answering it"
3418+ );
3419+ assert!(
3420+ stance.contains("UNSAVED — save ↵ · discard d · stay esc"),
3421+ "the decision plate speaks prose instead of state + verb·chord"
3422+ );
34043423 assert!(
34053424 !src_text("ui/timeband_stance.rs").contains("ActionId::Save"),
34063425 "Save has two doors: the ledger's state word and the time band's ghost"
crates/app/src/ui/stance.rsmodified+62 -0
@@ -620,6 +620,38 @@ fn bench(
620620 .child(bench_stance::render(player, bench_h - BENCH_CHROME_H, cx))
621621 }
622622
623+/// DESIGN §8's "needs a decision" notice, and the only one the room raises:
624+/// a red 3px spine, the state word, and the verbs that answer it -- state
625+/// plus verb·chord, never a sentence. It rises where notices rise, directly
626+/// above the ledger, so it is never over the picture. The keys behind it live
627+/// in the root `on_key_down` above.
628+fn decision_plate() -> impl IntoElement {
629+ div()
630+ .id("stance-quit-ask")
631+ .flex_none()
632+ .h(px(LEDGER_H))
633+ .bg(rgb(DARK_PANEL()))
634+ .border_t_1()
635+ .border_color(rgba(DARK_SEAM()))
636+ .flex()
637+ .items_center()
638+ // The spine is a child, not a border: a border colours all four sides
639+ // at once and the seam above this strip is the panel's, not the
640+ // notice's severity (the clip spine in `bench_stance` is drawn the
641+ // same way for the same reason).
642+ .child(div().flex_none().w(px(3.)).h_full().bg(rgb(STATUS_ERROR())))
643+ .child(
644+ div()
645+ .pl(px(12.))
646+ .type_style(type_scale::mono(
647+ type_scale::CHORD_METADATA_MIN_PX,
648+ gpui::FontWeight::MEDIUM,
649+ ))
650+ .text_color(rgb(INK1()))
651+ .child("UNSAVED — save ↵ · discard d · stay esc"),
652+ )
653+}
654+
623655 /// Thin strip at the bottom of the centre column: project identity, last
624656 /// action, export progress, position. Notices rise from here (DESIGN §5, §8).
625657 fn ledger(player: &Player, position: f64, cx: &mut Context<Player>) -> impl IntoElement {
@@ -856,6 +888,35 @@ pub(crate) fn render(
856888 if event.is_held && !repeats(this.repeat_scope(), key, this.keymap.lookup(key, ctrl)) {
857889 return;
858890 }
891+ // The unsaved-work question (user 2026-09-10: "if timeline is not
892+ // empty and user does alt+f4 or exit somehow, ask user to save the
893+ // file if it's not already saved"). Asked ahead of every branch
894+ // below because while the plate is up it owns the keyboard: three
895+ // answers and nothing else -- `↵` saves and goes, `d` goes without
896+ // saving, `esc` stays, any other key does nothing at all.
897+ if this.quit_ask {
898+ match key {
899+ "enter" => {
900+ // The Save action's own door, so the notice, the
901+ // sidecar cleanup and the dirty flag are the ones a
902+ // ctrl+s writes. corner-cut: a project with no path
903+ // yet says "NOTHING TO SAVE — no file open" and the
904+ // window still goes; this app has no save-as dialog
905+ // anywhere to route to (no file picker exists in the
906+ // tree), and every timeline with a clip on it got a
907+ // path from its first import (`project_path`).
908+ this.save_project(cx);
909+ window.remove_window();
910+ }
911+ "d" => window.remove_window(),
912+ "escape" => {
913+ this.quit_ask = false;
914+ cx.notify();
915+ }
916+ _ => {}
917+ }
918+ return;
919+ }
859920 // The recovery notice reads a key as more than "answered" (legacy
860921 // `render.rs`'s own rule): `enter` loads the sidecar it named, any
861922 // other key declines it. Asked before the generic dismiss below
@@ -1055,6 +1116,7 @@ pub(crate) fn render(
10551116 .child(time_band(player, position, cx))
10561117 .child(divider(Split::Bench, player.split_drag == Some(Split::Bench), cx))
10571118 .child(bench(player, bench_h, window, cx))
1119+ .when(player.quit_ask, |el| el.child(decision_plate()))
10581120 .child(ledger(player, position, cx))
10591121 .when(player.settings_open, |el| {
10601122 el.child(settings_stance::render(player, window_size, cx))