Ctrl+Q closes the window it is pressed in, after asking about that window's files

The runtime clears egui's quit shortcut, which closed the window the key was pressed in, and its own Quit asks the main window to close. From another window that closed the main window's files, and the main window took over the other window with its unsaved file instead of asking about it. The framework's Quit is unbound and each window reads Ctrl+Q last in its frame.
This commit is contained in:
aiongg
2026-11-25 15:23:17 +00:00
parent d6af244d61
commit f9c0537b36
4 changed files with 136 additions and 6 deletions
+1
View File
@@ -174,6 +174,7 @@ impl FigApp {
windows::begin(self, &ctx);
let (files, prefs) = (self.session.docs.len(), self.session.prefs.clone());
self.frame(ui);
windows::quit_key(&ctx);
windows::settle(self, &ctx, files);
if self.session.prefs != prefs {
// This window too: a preference set during the frame (the interface size, the theme)
+5 -2
View File
@@ -16,7 +16,7 @@ use craft_host::target::ShellInfo;
use craft_host::{AppInfo, Host};
use craft_keymap::{Chord, Mods, Overrides};
use craft_ui::fonts::{I18nScripts, SystemFonts, UiFonts};
use craft_ui::runtime::commands::{CommandInput, CommandSet as _, NavCommand};
use craft_ui::runtime::commands::{CommandInput, CommandSet as _, NavCommand, QuitCommand};
use craft_ui::runtime::env::{RootEnv, Scale};
use craft_ui::runtime::focus::{EditSet, KeepKeys, KeyUse};
use craft_ui::runtime::frame::{FrameInput, FrameOutput};
@@ -81,7 +81,9 @@ pub(crate) fn host() -> Arc<Host> {
}
/// F6 and Shift+F6 are FigCraft's own region moves (`view.nextRegion`, `view.previousRegion`), so the framework's pane
/// moves are unbound. Overrides are keyed by command id, which the command set gives.
/// moves are unbound. So is the framework's Quit, which asks the main window to close: FigCraft's Ctrl+Q closes the
/// window it is pressed in ([`crate::windows::quit_key`]). Overrides are keyed by command id, which the command set
/// gives.
fn command_overrides() -> Arc<Overrides> {
static OVERRIDES: OnceLock<Arc<Overrides>> = OnceLock::new();
OVERRIDES
@@ -89,6 +91,7 @@ fn command_overrides() -> Arc<Overrides> {
let mut overrides = Overrides::new();
overrides.insert(NavCommand::NextPane.id(), Vec::new());
overrides.insert(NavCommand::PreviousPane.id(), Vec::new());
overrides.insert(QuitCommand::Quit.id(), Vec::new());
Arc::new(overrides)
})
.clone()
+15 -4
View File
@@ -11,7 +11,7 @@
use std::cell::RefCell;
use craft_ui::shell::embed::CloseDecision;
use egui::{Context, Pos2, Vec2, ViewportBuilder, ViewportCommand, ViewportId};
use egui::{Context, Key, KeyboardShortcut, Modifiers, Pos2, Vec2, ViewportBuilder, ViewportCommand, ViewportId};
use figcraft_engine::{DocState, Session};
use serde_json::{Value, json};
@@ -377,9 +377,19 @@ pub fn list(app: &FigApp) -> Value {
)
}
/// Another window drawing now was asked to close (its close button, the system's): its files
/// close with it, after asking when one has unsaved changes. The main window's close goes through
/// the runtime's close path ([`close_decision`]).
/// Ctrl+Q asks the window it is pressed in to close, as its close button does: only that window's
/// files are asked about and close. Last in the window's frame, so its widgets and shortcuts read
/// the key first. (On macOS Cmd+Q is the app menu's Quit, which asks the main window to close.)
pub(crate) fn quit_key(ctx: &Context) {
const QUIT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Q);
if ctx.input_mut(|i| i.consume_shortcut(&QUIT)) {
ctx.send_viewport_cmd(ViewportCommand::Close);
}
}
/// Another window drawing now was asked to close (its close button, Ctrl+Q, the system's): its
/// files close with it, after asking when one has unsaved changes. The main window's close goes
/// through the runtime's close path ([`close_decision`]).
pub fn on_close_request(app: &mut FigApp, ctx: &Context) {
if app.ui.quit || !ctx.input(|i| i.viewport().close_requested()) {
return;
@@ -844,6 +854,7 @@ fn draw(app: &mut FigApp, id: u64, ui: &mut egui::Ui) {
app.viewport_setup(&ctx);
app.frame(ui)
});
quit_key(&ctx);
app.end_input_frame(&ctx);
adopt(app, &e);
settle(app, &ctx, files_before);
+115
View File
@@ -558,3 +558,118 @@ fn quitting_twice_before_the_window_draws_closes_it_once() {
assert!(app.session.docs.iter().any(|d| d.doc.name == "A" && d.is_dirty()), "A keeps its changes: {:?}", names(&app));
assert!(!app.ui.quit, "the app didn't quit over A's unsaved changes");
}
/// The main window and another one, driven as eframe drives them: a pass of one at a time, and a
/// close a pass asked a window for reaches that window in its next pass. The app is installed,
/// because the other window draws itself from there.
struct Desk {
ctx: egui::Context,
other: ViewportId,
draw_other: Option<std::sync::Arc<egui::DeferredViewportUiCallback>>,
closing: Vec<ViewportId>,
/// The unsaved-changes question was on screen in the last pass.
asks: bool,
}
impl Desk {
/// File A in another window, with unsaved changes when `dirty`, and file B in the main window
/// with none.
fn new(dirty: bool) -> (Desk, u64) {
let mut s = Session::empty();
for n in ["A", "B"] {
s.execute("file.new", &json!({"name": n})).unwrap();
}
let ctx = crate::framework::headless_context();
ctx.set_embed_viewports(false);
let mut app = FigApp::new(s, Box::new(NoServices));
frame(&ctx, &mut app, false);
let w = crate::control::handle(&mut app, &ctx, "window.new", &json!({"index": 0})).unwrap()["window"].as_u64().unwrap();
if dirty {
let drawn = crate::windows::as_window(&mut app, w, |app| {
app.session.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).is_ok()
});
assert_eq!(drawn, Some(true));
}
crate::windows::install(app);
let mut desk = Desk { ctx, other: crate::windows::viewport(w), draw_other: None, closing: vec![], asks: false };
desk.frames(2);
(desk, w)
}
fn pass(&mut self, id: ViewportId, events: Vec<egui::Event>) {
let mut input = RawInput { viewport_id: id, events, ..Default::default() };
for window in [ViewportId::ROOT, self.other] {
let info = input.viewports.entry(window).or_default();
info.focused = Some(window == id);
if window == id && self.closing.contains(&id) {
info.events.push(ViewportEvent::Close);
}
}
self.closing.retain(|closing| *closing != id);
let draw = if id == self.other { Some(self.draw_other.clone().expect("the main window shows the other one")) } else { None };
with_app(|app| app.before_input(&self.ctx));
let mut out = self.ctx.run_ui(input, |ui| {
match &draw {
Some(draw) => draw(ui),
None => with_app(|app| {
app.close_path(ui.ctx());
app.ui(ui);
}),
}
let question = egui::Id::unique(Some(crate::i18n::tr("Unsaved changes")));
self.asks = ui.ctx().memory(|memory| memory.area_rect(question).is_some());
});
out.textures_delta.clear();
let closing = out.viewport_output.iter().filter(|(_, window)| window.commands.contains(&egui::ViewportCommand::Close));
self.closing.extend(closing.map(|(window, _)| *window));
if id == ViewportId::ROOT {
self.draw_other = out.viewport_output.get(&self.other).and_then(|window| window.viewport_ui_cb.clone());
}
}
/// `n` passes of the main window, each followed by one of the other window while it is open.
fn frames(&mut self, n: usize) {
for _ in 0..n {
self.pass(ViewportId::ROOT, vec![]);
if self.draw_other.is_some() {
self.pass(self.other, vec![]);
}
}
}
/// Ctrl+Q pressed in the other window, and the frames it takes to act.
fn quit_in_other(&mut self) {
let key = |pressed| egui::Event::Key { key: egui::Key::Q, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::COMMAND };
self.pass(self.other, vec![key(true), key(false)]);
self.frames(3);
}
}
fn with_app<R>(f: impl FnOnce(&mut FigApp) -> R) -> R {
crate::windows::with_app(f).expect("the app is installed and idle")
}
/// Ctrl+Q closes the window it is pressed in: one whose file has unsaved changes asks and stays, and
/// the main window, with nothing unsaved, neither closes nor takes the file over. One with nothing
/// unsaved closes with its file.
#[test]
fn ctrl_q_in_another_window_asks_about_its_own_files_and_closes_only_it() {
let (mut desk, w) = Desk::new(true);
desk.quit_in_other();
assert!(desk.asks, "the question is on screen in the window the key was pressed in");
with_app(|app| {
assert_eq!(crate::windows::as_window(app, w, |app| app.ui.confirm_close), Some(true));
assert!(!app.ui.confirm_close && !app.ui.quit, "the main window is not asked to close");
let list = crate::windows::list(app);
assert_eq!((&list[0]["files"], &list[1]["files"]), (&json!(["B"]), &json!(["A"])), "no file closed or moved");
assert!(app.session.docs.iter().any(|d| d.doc.name == "A" && d.is_dirty()), "A keeps its changes");
});
let (mut desk, w) = Desk::new(false);
desk.quit_in_other();
with_app(|app| {
assert!(!app.windows.exists(w), "nothing to ask about: the window closes");
assert_eq!(names(app), ["B"], "with its file, and the main window keeps its own");
assert!(!app.ui.quit);
});
}