Skip to content

Commit

Permalink
Use objc2 v0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 26, 2025
1 parent 4f983c7 commit 8a6a05f
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 124 deletions.
93 changes: 24 additions & 69 deletions Cargo.lock

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

16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ raw-window-handle = "0.6"
log = "0.4"

[target.'cfg(target_os = "macos")'.dependencies]
block2 = "0.5.0"
objc2 = "0.5.1"
objc2-foundation = { version = "0.2.0", features = [
"dispatch",
block2 = "0.6.0"
dispatch2 = "0.2.0"
objc2 = "0.6.0"
objc2-foundation = { version = "0.3.0", default-features = false, features = [
"std",
"NSArray",
"NSEnumerator",
"NSString",
"NSThread",
"NSURL",
] }
objc2-app-kit = { version = "0.2.0", features = [
objc2-app-kit = { version = "0.3.0", default-features = false, features = [
"std",
"block2",
"NSAlert",
"NSApplication",
Expand Down Expand Up @@ -67,7 +69,9 @@ windows-sys = { version = "0.59", 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.10", optional = true, default-features = false, features = ["raw_handle"] }
ashpd = { version = "0.10", optional = true, default-features = false, features = [
"raw_handle",
] }
urlencoding = { version = "2.1.0", optional = true }
pollster = { version = "0.4", optional = true }
# GTK
Expand Down
37 changes: 15 additions & 22 deletions src/backend/macos/file_dialog/panel_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::path::Path;
use std::path::PathBuf;

use block2::Block;
use objc2::rc::Id;

use objc2::rc::Retained;
use objc2::MainThreadMarker;
use objc2_app_kit::{NSModalResponse, NSOpenPanel, NSSavePanel, NSWindow, NSWindowLevel};
use objc2_foundation::{MainThreadMarker, NSArray, NSObjectProtocol, NSString, NSURL};
use objc2_foundation::{NSArray, NSString, NSURL};
use raw_window_handle::RawWindowHandle;

use super::super::{
Expand All @@ -22,14 +22,14 @@ extern "C" {

pub struct Panel {
// Either `NSSavePanel` or the subclass `NSOpenPanel`
pub(crate) panel: Id<NSSavePanel>,
parent: Option<Id<NSWindow>>,
pub(crate) panel: Retained<NSSavePanel>,
parent: Option<Retained<NSWindow>>,
_focus_manager: FocusManager,
_policy_manager: PolicyManager,
}

impl AsModal for Panel {
fn inner_modal(&self) -> &NSSavePanel {
fn inner_modal(&self) -> &(impl InnerModal + 'static) {
&*self.panel
}
}
Expand All @@ -46,17 +46,10 @@ impl InnerModal for NSSavePanel {

impl Panel {
fn as_open_panel(&self) -> Option<&NSOpenPanel> {
if self.panel.is_kind_of::<NSOpenPanel>() {
let ptr: *const NSSavePanel = &*self.panel;
let ptr: *const NSOpenPanel = ptr.cast();
// SAFETY: Just checked that the panel is an `NSOpenPanel`
Some(unsafe { &*ptr })
} else {
None
}
self.panel.downcast_ref::<NSOpenPanel>()
}

pub fn new(panel: Id<NSSavePanel>, parent: Option<&RawWindowHandle>) -> Self {
pub fn new(panel: Retained<NSSavePanel>, parent: Option<&RawWindowHandle>) -> Self {
let _policy_manager = PolicyManager::new(MainThreadMarker::from(&*panel));

let _focus_manager = FocusManager::new(MainThreadMarker::from(&*panel));
Expand Down Expand Up @@ -119,7 +112,7 @@ trait PanelExt {
}

let f_raw: Vec<_> = exts.iter().map(|ext| NSString::from_str(ext)).collect();
let array = NSArray::from_vec(f_raw);
let array = NSArray::from_retained_slice(&f_raw);

unsafe {
#[allow(deprecated)]
Expand Down Expand Up @@ -158,13 +151,13 @@ trait PanelExt {
}
}

impl PanelExt for Id<NSSavePanel> {
impl PanelExt for Retained<NSSavePanel> {
fn panel(&self) -> &NSSavePanel {
&self
}
}

impl PanelExt for Id<NSOpenPanel> {
impl PanelExt for Retained<NSOpenPanel> {
fn panel(&self) -> &NSSavePanel {
&self
}
Expand Down Expand Up @@ -197,7 +190,7 @@ impl Panel {
unsafe { panel.setCanChooseDirectories(false) };
unsafe { panel.setCanChooseFiles(true) };

Self::new(Id::into_super(panel), opt.parent.as_ref())
Self::new(Retained::into_super(panel), opt.parent.as_ref())
}

pub fn build_save_file(opt: &FileDialog, mtm: MainThreadMarker) -> Self {
Expand Down Expand Up @@ -243,7 +236,7 @@ impl Panel {
unsafe { panel.setCanChooseDirectories(true) };
unsafe { panel.setCanChooseFiles(false) };

Self::new(Id::into_super(panel), opt.parent.as_ref())
Self::new(Retained::into_super(panel), opt.parent.as_ref())
}

pub fn build_pick_folders(opt: &FileDialog, mtm: MainThreadMarker) -> Self {
Expand All @@ -264,7 +257,7 @@ impl Panel {
unsafe { panel.setCanChooseFiles(false) };
unsafe { panel.setAllowsMultipleSelection(true) };

Self::new(Id::into_super(panel), opt.parent.as_ref())
Self::new(Retained::into_super(panel), opt.parent.as_ref())
}

pub fn build_pick_files(opt: &FileDialog, mtm: MainThreadMarker) -> Self {
Expand All @@ -290,6 +283,6 @@ impl Panel {
unsafe { panel.setCanChooseFiles(true) };
unsafe { panel.setAllowsMultipleSelection(true) };

Self::new(Id::into_super(panel), opt.parent.as_ref())
Self::new(Retained::into_super(panel), opt.parent.as_ref())
}
}
12 changes: 6 additions & 6 deletions src/backend/macos/message_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ use super::{

use super::utils::window_from_raw_window_handle;
use block2::Block;
use objc2::rc::{autoreleasepool, Retained};
use objc2::MainThreadMarker;
use objc2_app_kit::{
NSAlert, NSAlertFirstButtonReturn, NSAlertSecondButtonReturn, NSAlertStyle,
NSAlertThirdButtonReturn, NSApplication, NSModalResponse, NSWindow,
};
use objc2_foundation::{MainThreadMarker, NSString};

use objc2::rc::{autoreleasepool, Id};
use objc2_foundation::NSString;

pub struct Alert {
buttons: MessageButtons,
alert: Id<NSAlert>,
parent: Option<Id<NSWindow>>,
alert: Retained<NSAlert>,
parent: Option<Retained<NSWindow>>,
_focus_manager: FocusManager,
_policy_manager: PolicyManager,
}
Expand Down Expand Up @@ -137,7 +137,7 @@ fn dialog_result(buttons: &MessageButtons, ret: NSModalResponse) -> MessageDialo
}

impl AsModal for Alert {
fn inner_modal(&self) -> &NSAlert {
fn inner_modal(&self) -> &(impl InnerModal + 'static) {
&*self.alert
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/backend/macos/modal_future.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use block2::Block;
use objc2::mutability::MainThreadOnly;
use objc2::rc::Id;
use objc2::ClassType;
use dispatch2::run_on_main;
use objc2::rc::Retained;
use objc2::{ClassType, MainThreadMarker, MainThreadOnly, Message};
use objc2_app_kit::{NSApplication, NSModalResponse, NSWindow};
use objc2_foundation::{run_on_main, MainThreadMarker};

use std::pin::Pin;
use std::sync::{Arc, Mutex};
Expand All @@ -16,7 +15,7 @@ pub(super) trait AsModal {
fn inner_modal(&self) -> &(impl InnerModal + 'static);
}

pub(super) trait InnerModal: ClassType<Mutability = MainThreadOnly> {
pub(super) trait InnerModal: ClassType + MainThreadOnly {
fn begin_modal(&self, window: &NSWindow, handler: &Block<dyn Fn(NSModalResponse)>);
fn run_modal(&self) -> NSModalResponse;
}
Expand All @@ -37,7 +36,7 @@ unsafe impl<R, D> Send for ModalFuture<R, D> {}

impl<R: 'static + Default, D: AsModal + 'static> ModalFuture<R, D> {
pub fn new<F, DBULD: FnOnce(MainThreadMarker) -> D + Send>(
win: Option<Id<NSWindow>>,
win: Option<Retained<NSWindow>>,
build_modal: DBULD,
cb: F,
) -> Self
Expand Down Expand Up @@ -72,7 +71,7 @@ impl<R: 'static + Default, D: AsModal + 'static> ModalFuture<R, D> {
let win = if let Some(win) = win {
Some(win)
} else {
unsafe { app.mainWindow() }.or_else(|| app.windows().first_retained())
unsafe { app.mainWindow() }.or_else(|| app.windows().firstObject())
};

// if async exec is possible start sheet modal
Expand All @@ -81,7 +80,7 @@ impl<R: 'static + Default, D: AsModal + 'static> ModalFuture<R, D> {
let state = state.clone();

// Hack to work around us getting the window above
struct WindowWrapper(Id<NSWindow>);
struct WindowWrapper(Retained<NSWindow>);
unsafe impl Send for WindowWrapper {}
let window = WindowWrapper(win.unwrap());

Expand Down
Loading

0 comments on commit 8a6a05f

Please sign in to comment.