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

Properly support setting the parent window for the sync macOS backend. #171

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Update `raw-window-handle` to 0.6.
- Update `winit` in example to 0.29.
- Update wasm CSS to respect the color scheme (including dark mode)
- Fix macOS sync backend incorrectly setting the parent window

## 0.13.0
- **[Breaking]** Users of the `xdg-portal` feature must now also select the `tokio`
Expand Down
33 changes: 21 additions & 12 deletions src/backend/macos/file_dialog/panel_ffi.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::FileDialog;

use std::ops::Deref;
use std::mem;
use std::path::Path;
use std::{ops::DerefMut, path::PathBuf};

use objc::{class, msg_send, sel, sel_impl};
use objc_id::Id;
use objc_id::{Id, Shared};

use super::super::utils::{INSURL, NSURL};

Expand All @@ -30,6 +30,7 @@ fn make_nsstring(s: &str) -> Id<NSString> {

pub struct Panel {
pub(crate) panel: Id<Object>,
parent: Option<Id<NSWindow, Shared>>,
_focus_manager: FocusManager,
_policy_manager: PolicyManager,
}
Expand All @@ -51,6 +52,7 @@ impl Panel {
panel: unsafe { Id::from_ptr(panel) },
_focus_manager,
_policy_manager,
parent: None,
}
}

Expand All @@ -63,6 +65,16 @@ impl Panel {
}

pub fn run_modal(&self) -> i32 {
if let Some(parent) = self.parent.clone() {
let completion = { block::ConcreteBlock::new(|_: isize| {}) };

unsafe {
msg_send![self.panel, beginSheetModalForWindow: parent completionHandler: &completion]
}

mem::forget(completion);
}

unsafe { msg_send![self.panel, runModal] }
}

Expand Down Expand Up @@ -130,11 +142,8 @@ impl Panel {
}
}

pub fn set_parent(&self, parent: &RawWindowHandle) {
let id = NSWindow::from_raw_window_handle(parent);
unsafe {
let () = msg_send![id, addChildWindow: self.panel.deref() ordered: 1];
}
pub fn set_parent(&mut self, parent: &RawWindowHandle) {
self.parent = Some(NSWindow::from_raw_window_handle(parent).share());
}

pub fn get_result(&self) -> PathBuf {
Expand Down Expand Up @@ -162,7 +171,7 @@ impl Panel {

impl Panel {
pub fn build_pick_file(opt: &FileDialog) -> Self {
let panel = Panel::open_panel();
let mut panel = Panel::open_panel();

if !opt.filters.is_empty() {
panel.add_filters(&opt);
Expand Down Expand Up @@ -191,7 +200,7 @@ impl Panel {
}

pub fn build_save_file(opt: &FileDialog) -> Self {
let panel = Panel::save_panel();
let mut panel = Panel::save_panel();

if !opt.filters.is_empty() {
panel.add_filters(&opt);
Expand All @@ -217,7 +226,7 @@ impl Panel {
}

pub fn build_pick_folder(opt: &FileDialog) -> Self {
let panel = Panel::open_panel();
let mut panel = Panel::open_panel();

if let Some(path) = &opt.starting_directory {
panel.set_path(path, opt.file_name.as_deref());
Expand All @@ -239,7 +248,7 @@ impl Panel {
}

pub fn build_pick_folders(opt: &FileDialog) -> Self {
let panel = Panel::open_panel();
let mut panel = Panel::open_panel();

if let Some(path) = &opt.starting_directory {
panel.set_path(path, opt.file_name.as_deref());
Expand All @@ -262,7 +271,7 @@ impl Panel {
}

pub fn build_pick_files(opt: &FileDialog) -> Self {
let panel = Panel::open_panel();
let mut panel = Panel::open_panel();

if !opt.filters.is_empty() {
panel.add_filters(&opt);
Expand Down
23 changes: 21 additions & 2 deletions src/backend/macos/message_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::mem;
use std::ops::DerefMut;

use crate::backend::DialogFutureType;
Expand All @@ -9,7 +10,7 @@ use super::{
AsModal,
};

use super::utils::{INSWindow, NSWindow};
use super::utils::{INSApplication, INSWindow, NSApplication, NSWindow};
use objc::runtime::Object;
use objc::{class, msg_send, sel, sel_impl};
use objc_foundation::{INSString, NSString};
Expand All @@ -35,6 +36,7 @@ enum NSAlertReturn {
pub struct NSAlert {
buttons: MessageButtons,
alert: Id<Object>,
parent: Option<Id<NSWindow>>,
_focus_manager: FocusManager,
_policy_manager: PolicyManager,
}
Expand Down Expand Up @@ -93,13 +95,30 @@ impl NSAlert {

Self {
alert: unsafe { Id::from_retained_ptr(alert) },
parent: opt.parent.map(|x| NSWindow::from_raw_window_handle(&x)),
buttons: opt.buttons,
_focus_manager,
_policy_manager,
}
}

pub fn run(self) -> MessageDialogResult {
pub fn run(mut self) -> MessageDialogResult {
if let Some(parent) = self.parent.take() {
let completion = {
block::ConcreteBlock::new(|result: isize| {
let _: () = unsafe {
msg_send![NSApplication::shared_application(), stopModalWithCode: result]
};
})
};

unsafe {
msg_send![self.alert, beginSheetModalForWindow: parent completionHandler: &completion]
}

mem::forget(completion);
}

let ret: i64 = unsafe { msg_send![self.alert, runModal] };
dialog_result(&self.buttons, ret)
}
Expand Down
Loading