Skip to content

Commit 369d965

Browse files
committed
Quit Workbench when the last open window is closed
Fixes #727
1 parent e02a1b6 commit 369d965

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

src/Library/Library.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
demos_dir,
66
getLanguage,
77
settings as global_settings,
8+
quitOnLastWindowClose,
89
} from "../util.js";
910
import Window from "../window.js";
1011

@@ -24,6 +25,8 @@ export default function Library({ application }) {
2425

2526
let last_selected;
2627

28+
window.connect("close-request", quitOnLastWindowClose);
29+
2730
const demos = getDemos();
2831
demos.forEach((demo) => {
2932
const widget = new EntryRow({ demo: demo });

src/shortcutsWindow.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export default function ShortcutsWindow({ application }) {
1515
if (!window) {
1616
({ window } = build(resource));
1717
window.set_transient_for(application.get_active_window());
18-
window.set_application(application);
1918
}
2019
window.present();
2120
});

src/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,17 @@ export function buildRuntimePath(...args) {
197197
...args,
198198
]);
199199
}
200+
201+
export function quitOnLastWindowClose(self) {
202+
const { application } = self;
203+
const application_windows = [...application.get_windows()];
204+
const is_last_window_open = application_windows
205+
.filter((w) => w !== self)
206+
.every((w) => !w.get_mapped());
207+
208+
if (is_last_window_open) {
209+
application.quit();
210+
}
211+
212+
return false;
213+
}

src/window.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Adw from "gi://Adw";
66
import Vte from "gi://Vte";
77

88
import * as xml from "./langs/xml/xml.js";
9-
import { buildRuntimePath, languages } from "./util.js";
9+
import { buildRuntimePath, languages, quitOnLastWindowClose } from "./util.js";
1010
import Document from "./Document.js";
1111
import PanelUI from "./PanelUI.js";
1212
import PanelCode from "./PanelCode.js";
@@ -508,18 +508,34 @@ async function setGtk4PreferDark(dark) {
508508
settings.save_to_file(settings_path);
509509
}
510510

511+
function close(window) {
512+
quitOnLastWindowClose(window);
513+
window.destroy();
514+
}
515+
511516
async function onCloseSession({ session, window }) {
512517
if (session.isProject()) {
513-
window.destroy();
514-
return;
518+
return close(window);
515519
}
516520

517521
if (!session.settings.get_boolean("edited")) {
518522
await deleteSession(session);
519-
window.destroy();
520-
return;
523+
return close(window);
521524
}
522525

526+
const [response, location] = await promptSessionClose({ window });
527+
if (response === "cancel") return;
528+
529+
if (response === "discard") {
530+
await deleteSession(session);
531+
} else if (response === "save") {
532+
await saveSessionAsProject(session, location);
533+
}
534+
535+
close(window);
536+
}
537+
538+
async function promptSessionClose({ window }) {
523539
const builder = Gtk.Builder.new_from_resource(resource);
524540
const dialog = builder.get_object("message_dialog_save_project");
525541

@@ -561,16 +577,8 @@ async function onCloseSession({ session, window }) {
561577
}
562578

563579
const response = await dialog.choose(null);
564-
if (response === "cancel") return;
565-
566-
if (response === "discard") {
567-
await deleteSession(session);
568-
} else if (response === "save") {
569-
const destination = location.get_child_for_display_name(
570-
row_project_name.text,
571-
);
572-
await saveSessionAsProject(session, destination);
573-
}
574-
575-
window.destroy();
580+
return [
581+
response,
582+
location?.get_child_for_display_name(row_project_name.text),
583+
];
576584
}

0 commit comments

Comments
 (0)