Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open File: Add filter example #60

Merged
merged 11 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions demos/Open File/main.blp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ Adw.StatusPage {
]
}

Button button_image {
halign: center;
label: _("Open Image");

styles [
"pill",
"suggested-action"
]
}

Button button_multiple {
halign: center;
label: _("Open multiple files");
Expand Down
23 changes: 23 additions & 0 deletions demos/Open File/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Gio._promisify(
);

const button_single = workbench.builder.get_object("button_single");
const button_image = workbench.builder.get_object("button_image");
const button_multiple = workbench.builder.get_object("button_multiple");

async function openFile() {
Expand All @@ -22,6 +23,24 @@ async function openFile() {
console.log(`Selected File: ${info.get_name()}`);
}

async function openImageFile() {
const filters = new Gio.ListStore();

const imageFilter = Gtk.FileFilter.new();
imageFilter.set_name("Image File");
imageFilter.add_mime_type("image/*");
filters.append(imageFilter);

const fileDialog = new Gtk.FileDialog({ filters });
const file = await fileDialog.open(workbench.window, null);
const info = file.query_info(
"standard::name",
Gio.FileQueryInfoFlags.NONE,
null,
);
console.log(`Selected file: ${info.get_name()}`);
}

async function openMultipleFiles() {
const dialog_for_multiple_files = new Gtk.FileDialog();
const files = await dialog_for_multiple_files.open_multiple(
Expand All @@ -36,6 +55,10 @@ button_single.connect("clicked", () => {
openFile().catch(console.error);
});

button_image.connect("clicked", () => {
openImageFile().catch(console.error);
});

button_multiple.connect("clicked", () => {
openMultipleFiles().catch(console.error);
});