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 9 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
11 changes: 11 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 All @@ -35,3 +45,4 @@ Adw.StatusPage {
}
}
}

24 changes: 24 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,11 @@ button_single.connect("clicked", () => {
openFile().catch(console.error);
});

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

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

20 changes: 20 additions & 0 deletions demos/Open File/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
import workbench

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

image_filter = Gtk.FileFilter(name="Images")
image_filter.add_mime_type("image/*")
file_dialog = Gtk.FileDialog(
default_filter=image_filter,
)


def on_file_opened(dialog, result):
file = dialog.open_finish(result)
Expand All @@ -23,6 +30,18 @@ def open_file():
dialog_for_file.open(workbench.window, None, on_file_opened)


def on_image_opened(dialog, result):
file = file_dialog.open_finish(result)
info = file.query_info("standard::name", Gio.FileQueryInfoFlags.NONE, None)

print(f"Selected File: {info.get_name()}")


def open_image():
dialog_for_image = file_dialog
dialog_for_image.open(workbench.window, None, on_image_opened)


def on_multiple_files_opened(dialog, result):
files = dialog.open_multiple_finish(result)
selected_items_count = files.get_n_items()
Expand All @@ -37,4 +56,5 @@ def open_multiple_files():


button_single.connect("clicked", lambda *_: open_file())
button_image.connect("clicked", lambda *_: open_image())
button_multiple.connect("clicked", lambda *_: open_multiple_files())
22 changes: 22 additions & 0 deletions demos/Open File/main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

public void main () {
var button_single = (Gtk.Button) workbench.builder.get_object ("button_single");
var button_image = (Gtk.Button) workbench.builder.get_object ("button_image");
var button_multiple = (Gtk.Button) workbench.builder.get_object ("button_multiple");

button_single.clicked.connect (open_single.begin);
button_image.clicked.connect (open_image.begin);
button_multiple.clicked.connect (open_multiple.begin);
}

Expand All @@ -23,6 +25,25 @@ private async void open_single () {
}
}

private async void open_image () {
var image_filter = new Gtk.FileFilter ();

image_filter.add_mime_type ("image/*");

var file_dialog = new Gtk.FileDialog () {
default_filter = image_filter
};

try {
File file = yield file_dialog.open (workbench.window, null);

FileInfo info = file.query_info ("standard::name", NONE, null);
message (@"Selected file: $(info.get_name ())");
} catch (Error e) {
critical (e.message);
}
}

private async void open_multiple () {
var file_dialog = new Gtk.FileDialog ();
try {
Expand All @@ -33,3 +54,4 @@ private async void open_multiple () {
critical (e.message);
}
}

Loading