Skip to content

Commit

Permalink
Declare promisified Gtk.FileDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Sep 7, 2024
1 parent 699817a commit 0b9648f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/lib/fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Only use type imports here, as this module is used in prefs as well as extension code!
import type GLib from "gi://GLib";
import type Gio from "gi://Gio";
import type Soup from "gi://Soup";
import type Gtk from "gi://Gtk";

/**
* This module provides workarounds and fixes for various incomplete type
Expand Down Expand Up @@ -82,3 +84,15 @@ export interface PromisifiedSoupSession extends Soup.Session {
cancellable?: Gio.Cancellable | null,
): Promise<ReturnType<Soup.Session["send_and_read_finish"]>>;
}

/**
* @see https://github.com/gjsify/ts-for-gir/issues/200
* @see https://github.com/gjsify/ts-for-gir/issues/140
*/
export interface PromisifiedGtkFileDialog extends Gtk.FileDialog {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
select_folder(
parent?: Gtk.Window | null,
cancellable?: Gio.Cancellable | null,
): Promise<ReturnType<Gtk.FileDialog["select_folder_finish"]>>;
}
15 changes: 9 additions & 6 deletions src/prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import SOURCES from "./lib/sources/metadata/all.js";
import { SourceMetadata } from "./lib/source/source.js";

import type { ExtensionMetadata } from "@girs/gnome-shell/extensions/extension";
import { PromisifiedGtkFileDialog } from "./lib/fixes.js";

Gio._promisify(Gtk.FileDialog.prototype, "select_folder");

Expand Down Expand Up @@ -132,13 +133,15 @@ const SourcesPage = GObject.registerClass(
if (picturesDirectory) {
dialog.initialFolder = Gio.file_new_for_path(picturesDirectory);
}
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
const file = await (dialog.select_folder(
const file = await (dialog as PromisifiedGtkFileDialog).select_folder(
this.root as Gtk.Window,
null,
) as unknown as Promise<Gio.File>);
const value = new GLib.Variant("ms", file.get_uri());
this.settings.extension.set_value("image-download-folder", value);
);
if (file) {
const value = new GLib.Variant("ms", file.get_uri());
this.settings.extension.set_value("image-download-folder", value);
} else {
console.warn("No folder selected; dialog cancelled?");
}
}

private initialize(
Expand Down

0 comments on commit 0b9648f

Please sign in to comment.