Skip to content

Commit

Permalink
file-chooser: Allow to add filters
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
agx authored and bilelmoussaoui committed Nov 29, 2024
1 parent 699dd7d commit 828759d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
20 changes: 20 additions & 0 deletions ashpd-demo/data/resources/ui/file_chooser.ui
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<property name="title" translatable="yes">Directory</property>
</object>
</child>
<child>
<object class="AdwComboRow" id="open_filter_combo">
<property name="title" translatable="yes">Filters</property>
<property name="model">filters</property>
</object>
</child>
</object>
</child>
<child>
Expand Down Expand Up @@ -93,6 +99,12 @@
<property name="title" translatable="yes">Current Folder</property>
</object>
</child>
<child>
<object class="AdwComboRow" id="save_file_filter_combo">
<property name="title" translatable="yes">Filters</property>
<property name="model">filters</property>
</object>
</child>
</object>
</child>
<child>
Expand Down Expand Up @@ -173,4 +185,12 @@
</object>
</child>
</template>
<object class="GtkStringList" id="filters">
<items>
<item translatable="yes">No filter</item>
<item translatable="yes">Text files</item>
<item translatable="yes">Text files and images</item>
<item translatable="yes">Text files, images and videos</item>
</items>
</object>
</interface>
30 changes: 29 additions & 1 deletion ashpd-demo/src/portals/desktop/file_chooser.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,6 +27,8 @@ mod imp {
#[template_child]
pub open_directory_switch: TemplateChild<adw::SwitchRow>,
#[template_child]
pub open_filter_combo: TemplateChild<adw::ComboRow>,
#[template_child]
pub open_response_group: TemplateChild<adw::PreferencesGroup>,

#[template_child]
Expand All @@ -42,6 +44,8 @@ mod imp {
#[template_child]
pub save_file_modal_switch: TemplateChild<adw::SwitchRow>,
#[template_child]
pub save_file_filter_combo: TemplateChild<adw::ComboRow>,
#[template_child]
pub save_file_response_group: TemplateChild<adw::PreferencesGroup>,

#[template_child]
Expand Down Expand Up @@ -98,6 +102,22 @@ glib::wrapper! {
}

impl FileChooserPage {

fn filters(&self, pos: u32) -> Vec<FileFilter> {
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;
Expand All @@ -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) => {
Expand Down Expand Up @@ -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::<String>(current_folder)?
Expand Down

0 comments on commit 828759d

Please sign in to comment.