Closing with unsaved work asks first
Browse files at this commit4e7599274d5eab5a328b3f122eb0bd2ef12d5083Parent: 2f9d285- feat
- closing with unsaved work asks first
4 changed files +146 -0
- crates/app/src/main.rs +25 -0
- crates/app/src/player/library.rs +40 -0
- crates/app/src/tests/layout.rs +19 -0
- crates/app/src/ui/stance.rs +62 -0
crates/app/src/main.rsmodified+25 -0
| @@ -463,6 +463,13 @@ struct Player { | |||
| 463 | /// `project_path` is never touched by either path, only a manual save | 463 | /// `project_path` is never touched by either path, only a manual save |
| 464 | /// writes the real file. | 464 | /// writes the real file. |
| 465 | recovery_sidecar: Option<PathBuf>, | 465 | 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, | ||
| 466 | /// Whether `project_path` names a file this window itself put there -- | 473 | /// Whether `project_path` names a file this window itself put there -- |
| 467 | /// loaded from a `.edith` ([`Player::install_project`]) or written by a | 474 | /// loaded from a `.edith` ([`Player::install_project`]) or written by a |
| 468 | /// manual save ([`Player::save_project`]) -- rather than one merely | 475 | /// manual save ([`Player::save_project`]) -- rather than one merely |
| @@ -952,6 +959,7 @@ fn main() { | |||
| 952 | autosave_last_edit: None, | 959 | autosave_last_edit: None, |
| 953 | autosave_last_run: None, | 960 | autosave_last_run: None, |
| 954 | recovery_sidecar: None, | 961 | recovery_sidecar: None, |
| 962 | + quit_ask: false, | ||
| 955 | autosave_armed: false, | 963 | autosave_armed: false, |
| 956 | keymap: keymap.clone(), | 964 | keymap: keymap.clone(), |
| 957 | keys_open: false, | 965 | keys_open: false, |
| @@ -1042,6 +1050,23 @@ fn main() { | |||
| 1042 | // Nothing else takes focus, and without it the key listener | 1050 | // Nothing else takes focus, and without it the key listener |
| 1043 | // above is never reached. | 1051 | // above is never reached. |
| 1044 | window.focus(&player.read(cx).focus); | 1052 | 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 | + }); | ||
| 1045 | player.update(cx, |player, cx| player.start_autosave(cx)); | 1070 | player.update(cx, |player, cx| player.start_autosave(cx)); |
| 1046 | player | 1071 | player |
| 1047 | }, | 1072 | }, |
crates/app/src/player/library.rsmodified+40 -0
| @@ -1561,6 +1561,29 @@ impl Player { | |||
| 1561 | cx.notify(); | 1561 | cx.notify(); |
| 1562 | } | 1562 | } |
| 1563 | 1563 | ||
| 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 | + | ||
| 1564 | /// Writes the timeline back to its project file. Overwrites silently, like | 1587 | /// Writes the timeline back to its project file. Overwrites silently, like |
| 1565 | /// an export: the path was chosen once and the notice is the confirmation. | 1588 | /// an export: the path was chosen once and the notice is the confirmation. |
| 1566 | pub(crate) fn save_project(&mut self, cx: &mut Context<Self>) { | 1589 | pub(crate) fn save_project(&mut self, cx: &mut Context<Self>) { |
| @@ -1907,6 +1930,23 @@ impl Player { | |||
| 1907 | mod autosave_tests { | 1930 | mod autosave_tests { |
| 1908 | use super::*; | 1931 | use super::*; |
| 1909 | 1932 | ||
| 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 | + | ||
| 1910 | /// The three answers [`Player::recovery_offer`] gives, off nothing but a | 1950 | /// The three answers [`Player::recovery_offer`] gives, off nothing but a |
| 1911 | /// project file and a sidecar's mtimes: younger sidecar offers it, an | 1951 | /// project file and a sidecar's mtimes: younger sidecar offers it, an |
| 1912 | /// older one or none at all offers nothing. Nowhere near a `Player` or a | 1952 | /// 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() { | |||
| 3401 | stance.contains(r#"room_ghost(player, "unsaved", ActionId::Save"#), | 3401 | stance.contains(r#"room_ghost(player, "unsaved", ActionId::Save"#), |
| 3402 | "the ledger's unsaved state is not Save's door -- it is a word again" | 3402 | "the ledger's unsaved state is not Save's door -- it is a word again" |
| 3403 | ); | 3403 | ); |
| 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 | + ); | ||
| 3404 | assert!( | 3423 | assert!( |
| 3405 | !src_text("ui/timeband_stance.rs").contains("ActionId::Save"), | 3424 | !src_text("ui/timeband_stance.rs").contains("ActionId::Save"), |
| 3406 | "Save has two doors: the ledger's state word and the time band's ghost" | 3425 | "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( | |||
| 620 | .child(bench_stance::render(player, bench_h - BENCH_CHROME_H, cx)) | 620 | .child(bench_stance::render(player, bench_h - BENCH_CHROME_H, cx)) |
| 621 | } | 621 | } |
| 622 | 622 | ||
| 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 | + | ||
| 623 | /// Thin strip at the bottom of the centre column: project identity, last | 655 | /// Thin strip at the bottom of the centre column: project identity, last |
| 624 | /// action, export progress, position. Notices rise from here (DESIGN §5, §8). | 656 | /// action, export progress, position. Notices rise from here (DESIGN §5, §8). |
| 625 | fn ledger(player: &Player, position: f64, cx: &mut Context<Player>) -> impl IntoElement { | 657 | fn ledger(player: &Player, position: f64, cx: &mut Context<Player>) -> impl IntoElement { |
| @@ -856,6 +888,35 @@ pub(crate) fn render( | |||
| 856 | if event.is_held && !repeats(this.repeat_scope(), key, this.keymap.lookup(key, ctrl)) { | 888 | if event.is_held && !repeats(this.repeat_scope(), key, this.keymap.lookup(key, ctrl)) { |
| 857 | return; | 889 | return; |
| 858 | } | 890 | } |
| 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 | + } | ||
| 859 | // The recovery notice reads a key as more than "answered" (legacy | 920 | // The recovery notice reads a key as more than "answered" (legacy |
| 860 | // `render.rs`'s own rule): `enter` loads the sidecar it named, any | 921 | // `render.rs`'s own rule): `enter` loads the sidecar it named, any |
| 861 | // other key declines it. Asked before the generic dismiss below | 922 | // other key declines it. Asked before the generic dismiss below |
| @@ -1055,6 +1116,7 @@ pub(crate) fn render( | |||
| 1055 | .child(time_band(player, position, cx)) | 1116 | .child(time_band(player, position, cx)) |
| 1056 | .child(divider(Split::Bench, player.split_drag == Some(Split::Bench), cx)) | 1117 | .child(divider(Split::Bench, player.split_drag == Some(Split::Bench), cx)) |
| 1057 | .child(bench(player, bench_h, window, cx)) | 1118 | .child(bench(player, bench_h, window, cx)) |
| 1119 | + .when(player.quit_ask, |el| el.child(decision_plate())) | ||
| 1058 | .child(ledger(player, position, cx)) | 1120 | .child(ledger(player, position, cx)) |
| 1059 | .when(player.settings_open, |el| { | 1121 | .when(player.settings_open, |el| { |
| 1060 | el.child(settings_stance::render(player, window_size, cx)) | 1122 | el.child(settings_stance::render(player, window_size, cx)) |