|
@@ -7,6 +7,8 @@
|
|
|
#[cfg(any(dialog_open, dialog_save))]
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
+use crate::{Runtime, Window};
|
|
|
+
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
|
macro_rules! run_dialog {
|
|
|
($e:expr, $h: ident) => {{
|
|
@@ -30,6 +32,48 @@ macro_rules! run_dialog {
|
|
|
}};
|
|
|
}
|
|
|
|
|
|
+/// Window parent definition.
|
|
|
+#[cfg(any(windows, target_os = "macos"))]
|
|
|
+#[cfg_attr(doc_cfg, doc(cfg(any(windows, target_os = "macos"))))]
|
|
|
+pub struct WindowParent {
|
|
|
+ #[cfg(windows)]
|
|
|
+ hwnd: *mut std::ffi::c_void,
|
|
|
+ #[cfg(target_os = "macos")]
|
|
|
+ ns_window: *mut std::ffi::c_void,
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(any(windows, target_os = "macos"))]
|
|
|
+unsafe impl raw_window_handle::HasRawWindowHandle for WindowParent {
|
|
|
+ #[cfg(windows)]
|
|
|
+ fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
|
|
|
+ let mut handle = raw_window_handle::windows::WindowsHandle::empty();
|
|
|
+ handle.hwnd = self.hwnd;
|
|
|
+ raw_window_handle::RawWindowHandle::Windows(handle)
|
|
|
+ }
|
|
|
+
|
|
|
+ #[cfg(target_os = "macos")]
|
|
|
+ fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
|
|
|
+ let mut handle = raw_window_handle::macos::MacOSHandle::empty();
|
|
|
+ handle.ns_window = self.ns_window;
|
|
|
+ raw_window_handle::RawWindowHandle::MacOS(handle)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(any(windows, target_os = "macos"))]
|
|
|
+#[cfg_attr(doc_cfg, doc(cfg(any(windows, target_os = "macos"))))]
|
|
|
+#[doc(hidden)]
|
|
|
+pub fn window_parent<R: Runtime>(window: &Window<R>) -> crate::Result<WindowParent> {
|
|
|
+ #[cfg(windows)]
|
|
|
+ let w = WindowParent {
|
|
|
+ hwnd: window.hwnd()?,
|
|
|
+ };
|
|
|
+ #[cfg(target_os = "macos")]
|
|
|
+ let w = WindowParent {
|
|
|
+ ns_window: window.ns_window()?,
|
|
|
+ };
|
|
|
+ Ok(w)
|
|
|
+}
|
|
|
+
|
|
|
/// The file dialog builder.
|
|
|
///
|
|
|
/// Constructs file picker dialogs that can select single/multiple files or directories.
|
|
@@ -62,7 +106,6 @@ impl FileDialogBuilder {
|
|
|
self
|
|
|
}
|
|
|
|
|
|
- #[cfg(windows)]
|
|
|
/// Sets the parent window of the dialog.
|
|
|
pub fn set_parent<W: raw_window_handle::HasRawWindowHandle>(mut self, parent: &W) -> Self {
|
|
|
self.0 = self.0.set_parent(parent);
|
|
@@ -91,36 +134,60 @@ impl FileDialogBuilder {
|
|
|
}
|
|
|
|
|
|
/// Displays a dialog with a message and an optional title with a "yes" and a "no" button.
|
|
|
-pub fn ask<F: FnOnce(bool) + Send + 'static>(
|
|
|
+#[allow(unused_variables)]
|
|
|
+pub fn ask<R: Runtime, F: FnOnce(bool) + Send + 'static>(
|
|
|
+ parent_window: Option<&Window<R>>,
|
|
|
title: impl AsRef<str>,
|
|
|
message: impl AsRef<str>,
|
|
|
f: F,
|
|
|
) {
|
|
|
let title = title.as_ref().to_string();
|
|
|
let message = message.as_ref().to_string();
|
|
|
- run_dialog!(
|
|
|
- rfd::MessageDialog::new()
|
|
|
- .set_title(&title)
|
|
|
- .set_description(&message)
|
|
|
- .set_buttons(rfd::MessageButtons::YesNo)
|
|
|
- .set_level(rfd::MessageLevel::Info)
|
|
|
- .show(),
|
|
|
- f
|
|
|
- )
|
|
|
+ #[allow(unused_mut)]
|
|
|
+ let mut builder = rfd::MessageDialog::new()
|
|
|
+ .set_title(&title)
|
|
|
+ .set_description(&message)
|
|
|
+ .set_buttons(rfd::MessageButtons::YesNo)
|
|
|
+ .set_level(rfd::MessageLevel::Info);
|
|
|
+
|
|
|
+ #[cfg(any(windows, target_os = "macos"))]
|
|
|
+ {
|
|
|
+ if let Some(window) = parent_window {
|
|
|
+ if let Ok(parent) = window_parent(window) {
|
|
|
+ builder = builder.set_parent(&parent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ run_dialog!(builder.show(), f)
|
|
|
}
|
|
|
|
|
|
/// Displays a message dialog.
|
|
|
-pub fn message(title: impl AsRef<str>, message: impl AsRef<str>) {
|
|
|
+#[allow(unused_variables)]
|
|
|
+pub fn message<R: Runtime>(
|
|
|
+ parent_window: Option<&Window<R>>,
|
|
|
+ title: impl AsRef<str>,
|
|
|
+ message: impl AsRef<str>,
|
|
|
+) {
|
|
|
let title = title.as_ref().to_string();
|
|
|
let message = message.as_ref().to_string();
|
|
|
let cb = |_| {};
|
|
|
- run_dialog!(
|
|
|
- rfd::MessageDialog::new()
|
|
|
- .set_title(&title)
|
|
|
- .set_description(&message)
|
|
|
- .set_buttons(rfd::MessageButtons::Ok)
|
|
|
- .set_level(rfd::MessageLevel::Info)
|
|
|
- .show(),
|
|
|
- cb
|
|
|
- )
|
|
|
+
|
|
|
+ #[allow(unused_mut)]
|
|
|
+ let mut builder = rfd::MessageDialog::new()
|
|
|
+ .set_title(&title)
|
|
|
+ .set_description(&message)
|
|
|
+ .set_buttons(rfd::MessageButtons::Ok)
|
|
|
+ .set_level(rfd::MessageLevel::Info);
|
|
|
+
|
|
|
+ #[cfg(any(windows, target_os = "macos"))]
|
|
|
+ {
|
|
|
+ if let Some(window) = parent_window {
|
|
|
+ if let Ok(parent) = window_parent(window) {
|
|
|
+ builder = builder.set_parent(&parent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ run_dialog!(builder.show(), cb)
|
|
|
}
|