Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Fail to dismiss the Message Dialog merely in MacOS #223

Merged
merged 5 commits into from
Nov 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -53,6 +53,12 @@ objc2-app-kit = { version = "0.2.0", features = [
"NSView",
"NSWindow",
] }
futures = { version = "0.3.12", features = [
"thread-pool"
]}
deferred-future = "0.1.5"
core-foundation = "0.10.0"
core-foundation-sys = "0.8.7"

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { version = "0.48", features = [
39 changes: 35 additions & 4 deletions examples/msg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use ::std::io::{ Read, self };
use ::futures::executor::block_on;
fn main() {
#[cfg(not(feature = "gtk3"))]
let res = "";

#[cfg(any(
target_os = "windows",
target_os = "macos",
@@ -20,7 +21,37 @@ fn main() {
.set_title("Msg!")
.set_description("Description!")
.set_buttons(rfd::MessageButtons::OkCancel)
.set_level(rfd::MessageLevel::Error)
.show();

println!("{}", res);
}
println!("被点击按钮是 {}。敲击 Ctrl+D 继续。", res);
let mut stdin = io::stdin();
let mut buffer: Vec<u8> = vec![];
stdin.read_to_end(&mut buffer).unwrap();
#[cfg(any(
target_os = "windows",
target_os = "macos",
all(
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
),
feature = "gtk3"
)
))]
block_on(async move {
let res = rfd::AsyncMessageDialog::new()
.set_title("Msg!")
.set_description("Description!")
.set_buttons(rfd::MessageButtons::OkCancel)
.show().await;
println!("被点击按钮是 {}", res);
});
println!("敲击 Ctrl+D 继续。");
let mut stdin = io::stdin();
let mut buffer: Vec<u8> = vec![];
stdin.read_to_end(&mut buffer).unwrap();
println!("结束");
}
26 changes: 17 additions & 9 deletions src/backend/macos/message_dialog.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use crate::message_dialog::{MessageButtons, MessageDialog, MessageDialogResult,
use super::modal_future::AsModal;
use super::{
modal_future::{InnerModal, ModalFuture},
utils::{run_on_main, FocusManager, PolicyManager},
utils::{run_on_main, FocusManager, PolicyManager, self},
};

use super::utils::window_from_raw_window_handle;
@@ -155,7 +155,13 @@ impl InnerModal for NSAlert {
use crate::backend::MessageDialogImpl;
impl MessageDialogImpl for MessageDialog {
fn show(self) -> MessageDialogResult {
autoreleasepool(move |_| run_on_main(move |mtm| Alert::new(self, mtm).run()))
autoreleasepool(move |_| run_on_main(move |mtm| {
if self.parent.is_none() {
utils::sync_pop_dialog(self, mtm)
} else {
Alert::new(self, mtm).run()
}
}))
}
}

@@ -164,12 +170,14 @@ use crate::backend::AsyncMessageDialogImpl;
impl AsyncMessageDialogImpl for MessageDialog {
fn show_async(self) -> DialogFutureType<MessageDialogResult> {
let win = self.parent.as_ref().map(window_from_raw_window_handle);

let future = ModalFuture::new(
win,
move |mtm| Alert::new(self, mtm),
|dialog, ret| dialog_result(&dialog.buttons, ret),
);
Box::pin(future)
if self.parent.is_none() {
utils::async_pop_dialog(self)
} else {
Box::pin(ModalFuture::new(
win,
move |mtm| Alert::new(self, mtm),
|dialog, ret| dialog_result(&dialog.buttons, ret),
))
}
}
}
6 changes: 4 additions & 2 deletions src/backend/macos/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod focus_manager;
mod policy_manager;
mod user_alert;

pub use self::focus_manager::FocusManager;
pub use self::policy_manager::PolicyManager;
pub use focus_manager::FocusManager;
pub use policy_manager::PolicyManager;
pub use user_alert::{ async_pop_dialog, sync_pop_dialog };

use objc2::rc::Id;
use objc2_app_kit::{NSApplication, NSView, NSWindow};
120 changes: 120 additions & 0 deletions src/backend/macos/utils/user_alert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use ::objc2::rc::autoreleasepool;
use ::objc2_foundation::MainThreadMarker;
use ::deferred_future::ThreadDeferredFuture;
use ::std::{ mem::MaybeUninit, ptr, sync::PoisonError };
use ::core_foundation::{ base::TCFType, string::CFString };
use ::futures::{ future, executor::ThreadPool, task::SpawnExt };
use ::core_foundation_sys::{ base::CFOptionFlags, date::CFTimeInterval, url::CFURLRef, user_notification::{ CFUserNotificationDisplayAlert, kCFUserNotificationStopAlertLevel, kCFUserNotificationCautionAlertLevel, kCFUserNotificationNoteAlertLevel, kCFUserNotificationDefaultResponse, kCFUserNotificationAlternateResponse, kCFUserNotificationOtherResponse } };
use crate::{
message_dialog::{ MessageButtons, MessageDialog, MessageDialogResult, MessageLevel },
backend::{ DialogFutureType, macos::utils::{ FocusManager, PolicyManager, run_on_main } }
};
struct UserAlert {
timeout: CFTimeInterval,
flags: CFOptionFlags,
icon_url: CFURLRef,
sound_url: CFURLRef,
localization_url: CFURLRef,
alert_header: String,
alert_message: String,
default_button_title: Option<String>,
alternate_button_title: Option<String>,
other_button_title: Option<String>,
buttons: MessageButtons,
_focus_manager: Option<FocusManager>,
_policy_manager: Option<PolicyManager>,
}
impl UserAlert {
fn new(opt: MessageDialog, mtm: Option<MainThreadMarker>) -> Self {
let mut buttons: [Option<String>; 3] = match &opt.buttons {
MessageButtons::Ok => [None, None, None],
MessageButtons::OkCancel => [None, Some("Cancel".to_string()), None],
MessageButtons::YesNo => [Some("Yes".to_string()), Some("No".to_string()), None],
MessageButtons::YesNoCancel => [Some("Yes".to_string()), Some("No".to_string()), Some("Cancel".to_string())],
MessageButtons::OkCustom(ok_text) => [Some(ok_text.to_string()), None, None],
MessageButtons::OkCancelCustom(ok_text, cancel_text) => [Some(ok_text.to_string()), Some(cancel_text.to_string()), None],
MessageButtons::YesNoCancelCustom(yes_text, no_text, cancel_text) => [Some(yes_text.to_string()), Some(no_text.to_string()), Some(cancel_text.to_string())]
};
UserAlert {
timeout: 0_f64,
icon_url: ptr::null(),
sound_url: ptr::null(),
localization_url: ptr::null(),
flags: match opt.level {
MessageLevel::Info => kCFUserNotificationNoteAlertLevel,
MessageLevel::Warning => kCFUserNotificationCautionAlertLevel,
MessageLevel::Error => kCFUserNotificationStopAlertLevel
},
alert_header: opt.title,
alert_message: opt.description,
default_button_title: buttons[0].take(),
alternate_button_title: buttons[1].take(),
other_button_title: buttons[2].take(),
buttons: opt.buttons,
_policy_manager: mtm.map(|mtm| PolicyManager::new(mtm)),
_focus_manager: mtm.map(|mtm| FocusManager::new(mtm))
}
}
fn run(self) -> MessageDialogResult {
let alert_header = CFString::new(&self.alert_header[..]);
let alert_message = CFString::new(&self.alert_message[..]);
let default_button_title = self.default_button_title.map(|string| CFString::new(&string[..]));
let alternate_button_title = self.alternate_button_title.map(|value| CFString::new(&value[..]));
let other_button_title = self.other_button_title.map(|value| CFString::new(&value[..]));
let mut response_flags = MaybeUninit::<CFOptionFlags>::uninit();
let is_canel = unsafe { CFUserNotificationDisplayAlert(
self.timeout,
self.flags,
self.icon_url,
self.sound_url,
self.localization_url,
alert_header.as_concrete_TypeRef(),
alert_message.as_concrete_TypeRef(),
default_button_title.map_or(ptr::null(), |value| value.as_concrete_TypeRef()),
alternate_button_title.map_or(ptr::null(), |value| value.as_concrete_TypeRef()),
other_button_title.map_or(ptr::null(), |value| value.as_concrete_TypeRef()),
response_flags.as_mut_ptr()
) };
if is_canel != 0 {
return MessageDialogResult::Cancel;
}
let response = unsafe { response_flags.assume_init() };
match self.buttons {
MessageButtons::Ok if response == kCFUserNotificationDefaultResponse => MessageDialogResult::Ok,
MessageButtons::OkCancel if response == kCFUserNotificationDefaultResponse => MessageDialogResult::Ok,
MessageButtons::OkCancel if response == kCFUserNotificationAlternateResponse => MessageDialogResult::Cancel,
MessageButtons::YesNo if response == kCFUserNotificationDefaultResponse => MessageDialogResult::Yes,
MessageButtons::YesNo if response == kCFUserNotificationAlternateResponse => MessageDialogResult::No,
MessageButtons::YesNoCancel if response == kCFUserNotificationDefaultResponse => MessageDialogResult::Yes,
MessageButtons::YesNoCancel if response == kCFUserNotificationAlternateResponse => MessageDialogResult::No,
MessageButtons::YesNoCancel if response == kCFUserNotificationOtherResponse => MessageDialogResult::Cancel,
MessageButtons::OkCustom(custom) if response == kCFUserNotificationDefaultResponse => MessageDialogResult::Custom(custom.to_owned()),
MessageButtons::OkCancelCustom(custom, _) if response == kCFUserNotificationDefaultResponse => MessageDialogResult::Custom(custom.to_owned()),
MessageButtons::OkCancelCustom(_, custom) if response == kCFUserNotificationAlternateResponse => MessageDialogResult::Custom(custom.to_owned()),
MessageButtons::YesNoCancelCustom(custom, _, _) if response == kCFUserNotificationDefaultResponse => MessageDialogResult::Custom(custom.to_owned()),
MessageButtons::YesNoCancelCustom(_, custom, _) if response == kCFUserNotificationAlternateResponse => MessageDialogResult::Custom(custom.to_owned()),
MessageButtons::YesNoCancelCustom(_, _, custom) if response == kCFUserNotificationOtherResponse => MessageDialogResult::Custom(custom.to_owned()),
_ => MessageDialogResult::Cancel,
}
}
}
pub fn sync_pop_dialog(opt: MessageDialog, mtm: MainThreadMarker) -> MessageDialogResult {
UserAlert::new(opt, Some(mtm)).run()
}
pub fn async_pop_dialog(opt: MessageDialog) -> DialogFutureType<MessageDialogResult> {
let deferred_future = ThreadDeferredFuture::default();
let defer = deferred_future.defer();
let opt2 = opt.clone();
let result: Result<(), String> = ThreadPool::new().map_err(|err| err.to_string()).and_then(|thread_pool| thread_pool.spawn(async move {
let mut defer = defer.lock().unwrap_or_else(PoisonError::into_inner);
let message_dialog_result = UserAlert::new(opt2.clone(), None).run();
defer.complete(message_dialog_result);
}).map_err(|err| err.to_string()));
PolyMeilex marked this conversation as resolved.
Show resolved Hide resolved
match result {
Ok(_) => Box::pin(deferred_future),
Err(err) => {
eprintln!("\n Hi! It looks like you are running async dialog in unsupported environment, I will fallback to sync dialog for you. Reason is as below:\n {}", err);
Box::pin(future::ready(autoreleasepool(move |_| run_on_main(move |mtm| UserAlert::new(opt, Some(mtm)).run()))))
}
}
}