From 828759df80f14ffb2009c5f71f8e6d1282920e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Mon, 25 Nov 2024 20:16:15 +0100 Subject: [PATCH] file-chooser: Allow to add filters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a combox box that allows select different filters to the OpenFile and SaveFile dialogs. Adding different mime types allows for some flexibility when testing e.g. file choser implementations. When a filter set of filters is selected we pick the first filter as the `current_filter`. The default is no filters. Signed-off-by: Guido Günther --- ashpd-demo/data/resources/ui/file_chooser.ui | 20 +++++++++++++ .../src/portals/desktop/file_chooser.rs | 30 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/ashpd-demo/data/resources/ui/file_chooser.ui b/ashpd-demo/data/resources/ui/file_chooser.ui index 2f559eaf1..2c3d6ebc4 100644 --- a/ashpd-demo/data/resources/ui/file_chooser.ui +++ b/ashpd-demo/data/resources/ui/file_chooser.ui @@ -37,6 +37,12 @@ Directory + + + Filters + filters + + @@ -93,6 +99,12 @@ Current Folder + + + Filters + filters + + @@ -173,4 +185,12 @@ + + + No filter + Text files + Text files and images + Text files, images and videos + + diff --git a/ashpd-demo/src/portals/desktop/file_chooser.rs b/ashpd-demo/src/portals/desktop/file_chooser.rs index b01704725..cee7a0228 100644 --- a/ashpd-demo/src/portals/desktop/file_chooser.rs +++ b/ashpd-demo/src/portals/desktop/file_chooser.rs @@ -1,6 +1,6 @@ use adw::{prelude::*, subclass::prelude::*}; use ashpd::{ - desktop::file_chooser::{OpenFileRequest, SaveFileRequest, SaveFilesRequest}, + desktop::file_chooser::{OpenFileRequest, SaveFileRequest, SaveFilesRequest, FileFilter}, WindowIdentifier, }; use gtk::glib; @@ -27,6 +27,8 @@ mod imp { #[template_child] pub open_directory_switch: TemplateChild, #[template_child] + pub open_filter_combo: TemplateChild, + #[template_child] pub open_response_group: TemplateChild, #[template_child] @@ -42,6 +44,8 @@ mod imp { #[template_child] pub save_file_modal_switch: TemplateChild, #[template_child] + pub save_file_filter_combo: TemplateChild, + #[template_child] pub save_file_response_group: TemplateChild, #[template_child] @@ -98,6 +102,22 @@ glib::wrapper! { } impl FileChooserPage { + + fn filters(&self, pos: u32) -> Vec { + let mut filters = Vec::new(); + + if pos > 0 { + filters.push(FileFilter::new("Text files").mimetype("text/*").glob("*.txt")); + }; + if pos > 1 { + filters.push(FileFilter::new("Images").mimetype("image/*")); + }; + if pos > 2 { + filters.push(FileFilter::new("Videos").mimetype("video/*")); + }; + filters + } + async fn open_file(&self) { let root = self.native().unwrap(); let identifier = WindowIdentifier::from_native(&root).await; @@ -108,12 +128,16 @@ impl FileChooserPage { let multiple = imp.open_multiple_switch.is_active(); let accept_label = is_empty(imp.open_accept_label_entry.text()); + let filters = self.filters(imp.open_filter_combo.selected()); + let current_filter = filters.first().cloned(); let request = OpenFileRequest::default() .directory(directory) .identifier(identifier) .modal(modal) .title(&*title) .multiple(multiple) + .filters(filters) + .current_filter(current_filter) .accept_label(accept_label.as_deref()); match request.send().await.and_then(|r| r.response()) { Ok(files) => { @@ -146,10 +170,14 @@ impl FileChooserPage { let current_name = is_empty(imp.save_file_current_name_entry.text()); let current_folder = is_empty(imp.save_file_current_folder_entry.text()); let current_file = is_empty(imp.save_file_current_file_entry.text()); + let filters = self.filters(imp.save_file_filter_combo.selected()); + let current_filter = filters.first().cloned(); let request = SaveFileRequest::default() .identifier(identifier) .modal(modal) .title(&*title) + .filters(filters) + .current_filter(current_filter) .accept_label(accept_label.as_deref()) .current_name(current_name.as_deref()) .current_folder::(current_folder)?