diff --git a/src/Library/demos/Dialogs/main.js b/src/Library/demos/Dialogs/main.js index c0733afd6..2cab5bd69 100644 --- a/src/Library/demos/Dialogs/main.js +++ b/src/Library/demos/Dialogs/main.js @@ -1,12 +1,15 @@ import Adw from "gi://Adw"; import Gtk from "gi://Gtk"; +import Gio from "gi://Gio"; + +Gio._promisify(Adw.MessageDialog.prototype, "choose", "choose_finish"); const button_confirmation = workbench.builder.get_object("button_confirmation"); const button_error = workbench.builder.get_object("button_error"); const button_advanced = workbench.builder.get_object("button_advanced"); const window = workbench.window; -function createConfirmationDialog() { +async function createConfirmationDialog() { const dialog = new Adw.MessageDialog({ heading: "Replace File?", body: "A file named `example.png` already exists. Do you want to replace it?", @@ -21,14 +24,11 @@ function createConfirmationDialog() { // Use DESTRUCTIVE appearance to draw attention to the potentially damaging consequences of this action dialog.set_response_appearance("replace", Adw.ResponseAppearance.DESTRUCTIVE); - dialog.connect("response", (_self, response) => { - console.log(`Selected "${response}" response.`); - }); - - dialog.present(); + const response = await dialog.choose(null); + console.log(`Selected "${response}" response.`); } -function createErrorDialog() { +async function createErrorDialog() { const dialog = new Adw.MessageDialog({ heading: "Critical Error", body: "You did something you should not have", @@ -39,15 +39,12 @@ function createErrorDialog() { dialog.add_response("okay", "Okay"); - dialog.connect("response", (_self, response) => { - console.log(`Selected "${response}" response.`); - }); - - dialog.present(); + const response = await dialog.choose(null); + console.log(`Selected "${response}" response.`); } //Creates a message dialog with an extra child -function createAdvancedDialog() { +async function createAdvancedDialog() { const dialog = new Adw.MessageDialog({ heading: "Login", body: "A valid password is needed to continue", @@ -68,19 +65,22 @@ function createAdvancedDialog() { dialog.set_extra_child(entry); - dialog.connect("response", (dialog, response) => { - if (dialog.get_response_label(response) === "Login") { - console.log( - `Selected "${response}" response with password "${entry.get_text()}"`, - ); - } else { - console.log(`Selected "${response}" response.`); - } - }); - - dialog.present(); + const response = await dialog.choose(null); + if (response === "login") { + console.log( + `Selected "${response}" response with password "${entry.get_text()}"`, + ); + } else { + console.log(`Selected "${response}" response.`); + } } -button_confirmation.connect("clicked", createConfirmationDialog); -button_error.connect("clicked", createErrorDialog); -button_advanced.connect("clicked", createAdvancedDialog); +button_confirmation.connect("clicked", () => { + createConfirmationDialog().catch(logError); +}); +button_error.connect("clicked", () => { + createErrorDialog().catch(logError); +}); +button_advanced.connect("clicked", () => { + createAdvancedDialog().catch(logError); +}); diff --git a/src/Library/demos/Welcome/main.js b/src/Library/demos/Welcome/main.js index c09ef7c82..dd4a27dbd 100644 --- a/src/Library/demos/Welcome/main.js +++ b/src/Library/demos/Welcome/main.js @@ -1,5 +1,8 @@ import Gtk from "gi://Gtk"; import Adw from "gi://Adw"; +import Gio from "gi://Gio"; + +Gio._promisify(Adw.MessageDialog.prototype, "choose", "choose_finish"); const box = workbench.builder.get_object("subtitle"); @@ -9,12 +12,14 @@ const button = new Gtk.Button({ margin_top: 6, css_classes: ["suggested-action"], }); -button.connect("clicked", greet); +button.connect("clicked", () => { + greet().catch(logError); +}); box.append(button); console.log("Welcome to Workbench!"); -function greet() { +async function greet() { // https://gjs-docs.gnome.org/adw1~1/adw.messagedialog const dialog = new Adw.MessageDialog({ body: "Hello World!", @@ -22,9 +27,7 @@ function greet() { }); dialog.add_response("ok", "OK"); - dialog.connect("response", (self, response) => { - console.log(response); - }); - dialog.present(); + const response = await dialog.choose(null); + console.log(response); } diff --git a/src/init.js b/src/init.js index 59ad3a71a..d433c97e7 100644 --- a/src/init.js +++ b/src/init.js @@ -8,10 +8,12 @@ import "gi://Pango?version=1.0"; import Gtk from "gi://Gtk"; import Gio from "gi://Gio"; +import Adw from "gi://Adw"; import Xdp from "gi://Xdp"; import Source from "gi://GtkSource"; import WebKit from "gi://WebKit"; +Gio._promisify(Adw.MessageDialog.prototype, "choose", "choose_finish"); Gio._promisify(Xdp.Portal.prototype, "trash_file", "trash_file_finish"); Gio._promisify(Xdp.Portal.prototype, "open_uri", "open_uri_finish"); Gio._promisify(Xdp.Portal.prototype, "open_file", "open_file_finish"); diff --git a/src/window.js b/src/window.js index 71a36b122..eaea0a7ba 100644 --- a/src/window.js +++ b/src/window.js @@ -445,10 +445,7 @@ async function onCloseSession({ session, window }) { updateSaveButton(); } - const response = await new Promise((resolve) => { - dialog.connect("response", (self, response) => resolve(response)); - }); - + const response = await dialog.choose(null); if (response === "cancel") return; if (response === "discard") {