Skip to content

Commit

Permalink
Add support for set_parent in XDG portals
Browse files Browse the repository at this point in the history
  • Loading branch information
kjarosh committed Aug 7, 2024
1 parent 42f4125 commit 685fa8b
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Add `TDF_SIZE_TO_CONTENT` to `TaskDialogIndirect` config so that it can display longer text without truncating/wrapping (80 characters instead of 55) (#202)
- Fix `xdg-portal` backend not accepting special characters in message dialogs
- Make `set_parent` require `HasWindowHandle + HasDisplayHandle`
- Add support for `set_parent` in XDG Portals

## 0.14.0
- i18n for GTK and XDG Portal
Expand Down
104 changes: 104 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ windows-sys = { version = "0.48", features = [

[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
# XDG Desktop Portal
ashpd = { version = "0.8", optional = true, default-features = false }
ashpd = { version = "0.8", optional = true, default-features = false, features = ["raw_handle"] }
urlencoding = { version = "2.1.0", optional = true }
pollster = { version = "0.3", optional = true }
# GTK
Expand Down
20 changes: 18 additions & 2 deletions src/backend/xdg_desktop_portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ use crate::message_dialog::MessageDialog;
use crate::{FileDialog, FileHandle, MessageButtons, MessageDialogResult};

use ashpd::desktop::file_chooser::{FileFilter, OpenFileRequest, SaveFileRequest};
// TODO: convert raw_window_handle::RawWindowHandle to ashpd::WindowIdentifier
// https://github.com/bilelmoussaoui/ashpd/issues/40
use ashpd::WindowIdentifier;

use log::error;
use pollster::block_on;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};

fn to_window_identifier(
window: Option<RawWindowHandle>,
display: Option<RawDisplayHandle>,
) -> Option<WindowIdentifier> {
window.map(|window| {
block_on(Box::pin(async move {
WindowIdentifier::from_raw_handle(&window, display.as_ref()).await
}))
})
}

impl From<&Filter> for FileFilter {
fn from(filter: &Filter) -> Self {
Expand Down Expand Up @@ -44,6 +55,7 @@ impl AsyncFilePickerDialogImpl for FileDialog {
fn pick_file_async(self) -> DialogFutureType<Option<FileHandle>> {
Box::pin(async move {
let res = OpenFileRequest::default()
.identifier(to_window_identifier(self.parent, self.parent_display))
.multiple(false)
.title(self.title.as_deref().or(None))
.filters(self.filters.iter().map(From::from))
Expand Down Expand Up @@ -77,6 +89,7 @@ impl AsyncFilePickerDialogImpl for FileDialog {
fn pick_files_async(self) -> DialogFutureType<Option<Vec<FileHandle>>> {
Box::pin(async move {
let res = OpenFileRequest::default()
.identifier(to_window_identifier(self.parent, self.parent_display))
.multiple(true)
.title(self.title.as_deref().or(None))
.filters(self.filters.iter().map(From::from))
Expand Down Expand Up @@ -130,6 +143,7 @@ impl AsyncFolderPickerDialogImpl for FileDialog {
fn pick_folder_async(self) -> DialogFutureType<Option<FileHandle>> {
Box::pin(async move {
let res = OpenFileRequest::default()
.identifier(to_window_identifier(self.parent, self.parent_display))
.multiple(false)
.directory(true)
.title(self.title.as_deref().or(None))
Expand Down Expand Up @@ -164,6 +178,7 @@ impl AsyncFolderPickerDialogImpl for FileDialog {
fn pick_folders_async(self) -> DialogFutureType<Option<Vec<FileHandle>>> {
Box::pin(async move {
let res = OpenFileRequest::default()
.identifier(to_window_identifier(self.parent, self.parent_display))
.multiple(true)
.directory(true)
.title(self.title.as_deref().or(None))
Expand Down Expand Up @@ -213,6 +228,7 @@ impl AsyncFileSaveDialogImpl for FileDialog {
fn save_file_async(self) -> DialogFutureType<Option<FileHandle>> {
Box::pin(async move {
let res = SaveFileRequest::default()
.identifier(to_window_identifier(self.parent, self.parent_display))
.title(self.title.as_deref().or(None))
.current_name(self.file_name.as_deref())
.filters(self.filters.iter().map(From::from))
Expand Down
14 changes: 10 additions & 4 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ impl FileDialog {
self
}

/// Set parent windows explicitly (optional)
/// Suported in: `macos` and `windows`
/// Set parent windows explicitly (optional).
/// Supported platforms:
/// * Windows
/// * Mac
/// * Linux (XDG only)
pub fn set_parent<W: HasWindowHandle + HasDisplayHandle>(mut self, parent: &W) -> Self {
self.parent = parent.window_handle().ok().map(|x| x.as_raw());
self.parent_display = parent.display_handle().ok().map(|x| x.as_raw());
Expand Down Expand Up @@ -207,8 +210,11 @@ impl AsyncFileDialog {
self
}

/// Set parent windows explicitly (optional)
/// Suported in: `macos` and `windows`
/// Set parent windows explicitly (optional).
/// Supported platforms:
/// * Windows
/// * Mac
/// * Linux (XDG only)
pub fn set_parent<W: HasWindowHandle + HasDisplayHandle>(mut self, parent: &W) -> Self {
self.file_dialog = self.file_dialog.set_parent(parent);
self
Expand Down
14 changes: 10 additions & 4 deletions src/message_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ impl MessageDialog {
self
}

/// Set parent windows explicitly (optional)
/// Suported in: `macos` and `windows`
/// Set parent windows explicitly (optional).
/// Supported platforms:
/// * Windows
/// * Mac
/// * Linux (XDG only)
pub fn set_parent<W: HasWindowHandle + HasDisplayHandle>(mut self, parent: &W) -> Self {
self.parent = parent.window_handle().ok().map(|x| x.as_raw());
self.parent_display = parent.display_handle().ok().map(|x| x.as_raw());
Expand Down Expand Up @@ -125,8 +128,11 @@ impl AsyncMessageDialog {
self
}

/// Set parent windows explicitly (optional)
/// Suported in: `macos` and `windows`
/// Set parent windows explicitly (optional).
/// Supported platforms:
/// * Windows
/// * Mac
/// * Linux (XDG only)
pub fn set_parent<W: HasWindowHandle + HasDisplayHandle>(mut self, parent: &W) -> Self {
self.0 = self.0.set_parent(parent);
self
Expand Down

0 comments on commit 685fa8b

Please sign in to comment.