Skip to content

Commit

Permalink
filechooser: Support current_folder with OpenFile
Browse files Browse the repository at this point in the history
  • Loading branch information
sophie-h committed Aug 29, 2022
1 parent 49aca6f commit c8594a7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ashpd-demo/data/resources/ui/file_chooser.ui
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@
</child>
</object>
</child>
<child>
<object class="AdwActionRow">
<property name="title" translatable="yes">Current Folder</property>
<property name="activatable-widget">open_current_folder_entry</property>
<child>
<object class="GtkEntry" id="open_current_folder_entry">
<property name="valign">center</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
Expand Down
11 changes: 11 additions & 0 deletions ashpd-demo/src/portals/desktop/file_chooser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ mod imp {
#[template_child]
pub open_directory_switch: TemplateChild<gtk::Switch>,
#[template_child]
pub open_current_folder_entry: TemplateChild<gtk::Entry>,
#[template_child]
pub open_response_group: TemplateChild<adw::PreferencesGroup>,

#[template_child]
Expand Down Expand Up @@ -133,6 +135,7 @@ impl FileChooserPage {
let directory = imp.open_directory_switch.is_active();
let modal = imp.open_modal_switch.is_active();
let multiple = imp.open_multiple_switch.is_active();
let current_folder = is_empty(imp.open_current_folder_entry.text());

match portal_open_file(
&identifier,
Expand All @@ -141,6 +144,7 @@ impl FileChooserPage {
directory,
modal,
multiple,
current_folder.as_deref(),
)
.await
{
Expand Down Expand Up @@ -249,6 +253,7 @@ async fn portal_open_file(
directory: bool,
modal: bool,
multiple: bool,
current_folder: Option<&str>,
) -> ashpd::Result<SelectedFiles> {
let proxy = FileChooserProxy::new().await?;
let options = OpenFileOptions::default()
Expand All @@ -261,6 +266,12 @@ async fn portal_open_file(
options
};

let options = if let Some(current_folder) = current_folder {
options.current_folder(current_folder)
} else {
options
};

let selected_files = proxy.open_file(identifier, title, options).await?;
Ok(selected_files)
}
Expand Down
11 changes: 11 additions & 0 deletions src/desktop/file_chooser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ pub struct OpenFileOptions {
current_filter: Option<FileFilter>,
/// List of serialized combo boxes to add to the file chooser
choices: Vec<Choice>,
/// Suggested folder to open the files in
current_folder: Option<Vec<u8>>,
}

impl OpenFileOptions {
Expand Down Expand Up @@ -266,6 +268,15 @@ impl OpenFileOptions {
self.choices.push(choice);
self
}

/// Specifies the current folder path.
#[must_use]
pub fn current_folder(mut self, current_folder: impl AsRef<Path>) -> Self {
let cstr = CString::new(current_folder.as_ref().as_os_str().as_bytes())
.expect("`current_folder` should not be null terminated");
self.current_folder = Some(cstr.into_bytes_with_nul());
self
}
}

#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
Expand Down

0 comments on commit c8594a7

Please sign in to comment.