From 87fe0e3e122d2e7a290c3386b90264c3ddc5fb58 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 20 Jul 2022 05:47:02 +0200 Subject: [PATCH] Use msg_send_id! and rc::Id --- ARCHITECTURE.md | 2 +- src/appkit/alert.rs | 16 +++--- src/appkit/app/mod.rs | 21 ++++---- src/appkit/event/mod.rs | 15 +++--- src/appkit/menu/item.rs | 37 ++++++++------ src/appkit/menu/menu.rs | 14 +++--- src/appkit/printing/settings.rs | 6 +-- src/appkit/toolbar/item.rs | 13 +++-- src/appkit/toolbar/mod.rs | 22 +++++---- src/appkit/window/controller/mod.rs | 14 +++--- src/appkit/window/mod.rs | 66 +++++++++++++------------ src/button/mod.rs | 6 +-- src/cloudkit/share.rs | 6 +-- src/color/mod.rs | 37 ++++++++------ src/defaults/mod.rs | 12 ++--- src/dragdrop.rs | 4 +- src/filesystem/manager.rs | 14 +++--- src/filesystem/save.rs | 13 +++-- src/filesystem/select.rs | 13 +++-- src/foundation/array.rs | 40 ++++++++------- src/foundation/autoreleasepool.rs | 8 +-- src/foundation/data.rs | 31 +++++++----- src/foundation/dictionary.rs | 8 +-- src/foundation/number.rs | 19 +++---- src/foundation/string.rs | 37 ++++++++------ src/foundation/urls/mod.rs | 12 ++--- src/image/appkit.rs | 2 +- src/image/handle.rs | 6 +-- src/image/image.rs | 50 ++++++++++--------- src/image/mod.rs | 2 +- src/image/uikit.rs | 2 +- src/input/appkit.rs | 2 +- src/input/mod.rs | 2 +- src/invoker.rs | 22 ++++----- src/layout/animator.rs | 8 +-- src/layout/constraint.rs | 9 ++-- src/layout/dimension.rs | 16 +++--- src/layout/horizontal.rs | 26 +++++----- src/layout/traits.rs | 2 +- src/layout/vertical.rs | 18 +++---- src/lib.rs | 59 ---------------------- src/listview/actions.rs | 13 ++--- src/listview/appkit.rs | 12 ++--- src/listview/mod.rs | 41 ++++++++------- src/listview/row/appkit.rs | 12 ++--- src/listview/row/mod.rs | 2 +- src/listview/row/uikit.rs | 2 +- src/networking/mod.rs | 8 +-- src/notification_center/mod.rs | 10 ++-- src/pasteboard/mod.rs | 14 +++--- src/progress/mod.rs | 2 +- src/quicklook/config.rs | 2 +- src/quicklook/mod.rs | 8 +-- src/scrollview/appkit.rs | 12 ++--- src/scrollview/mod.rs | 2 +- src/select/mod.rs | 6 +-- src/switch.rs | 6 +-- src/text/attributed_string.rs | 12 ++--- src/text/font.rs | 12 ++--- src/text/label/appkit.rs | 2 +- src/text/label/mod.rs | 2 +- src/uikit/scene/config.rs | 14 +++--- src/uikit/scene/mod.rs | 6 +-- src/uikit/scene/options.rs | 6 +-- src/uikit/scene/session.rs | 6 +-- src/uikit/window/mod.rs | 10 ++-- src/user_activity.rs | 6 +-- src/user_notifications/notifications.rs | 14 +++--- src/utils/mod.rs | 4 +- src/utils/properties.rs | 8 +-- src/view/animator.rs | 8 +-- src/view/appkit.rs | 12 ++--- src/view/controller/mod.rs | 16 +++--- src/view/splitviewcontroller/mod.rs | 30 +++++------ src/view/uikit.rs | 2 +- src/webview/config.rs | 11 ++--- src/webview/mod.rs | 12 ++--- 77 files changed, 515 insertions(+), 542 deletions(-) diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 71c87851..927f4f9f 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -412,7 +412,7 @@ Here, we just want to tell `NSView` to use top,left as the origin point, so we n extern "C" fn dragging_entered(this: &mut Object, _: Sel, info: id) -> NSUInteger { let view = utils::load::(this, VIEW_DELEGATE_PTR); view.dragging_entered(DragInfo { - info: unsafe { Id::from_ptr(info) } + info: unsafe { Id::retain(info).unwrap() } }).into() } ``` diff --git a/src/appkit/alert.rs b/src/appkit/alert.rs index 25c48e8f..bcfa9740 100644 --- a/src/appkit/alert.rs +++ b/src/appkit/alert.rs @@ -24,16 +24,16 @@ //! } //! ``` -use crate::id_shim::Id; +use objc::rc::{Id, Owned}; use objc::runtime::Object; -use objc::{class, msg_send, sel}; +use objc::{class, msg_send, msg_send_id, sel}; use crate::foundation::{id, NSString}; /// Represents an `NSAlert`. Has no information other than the retained pointer to the Objective C /// side, so... don't bother inspecting this. #[derive(Debug)] -pub struct Alert(Id); +pub struct Alert(Id); impl Alert { /// Creates a basic `NSAlert`, storing a pointer to it in the Objective C runtime. @@ -44,11 +44,11 @@ impl Alert { let ok = NSString::new("OK"); Alert(unsafe { - let alert: id = msg_send![class!(NSAlert), new]; - let _: () = msg_send![alert, setMessageText: &*title]; - let _: () = msg_send![alert, setInformativeText: &*message]; - let _: () = msg_send![alert, addButtonWithTitle: &*ok]; - Id::from_ptr(alert) + let mut alert = msg_send_id![class!(NSAlert), new].unwrap(); + let _: () = msg_send![&mut alert, setMessageText: &*title]; + let _: () = msg_send![&mut alert, setInformativeText: &*message]; + let _: () = msg_send![&mut alert, addButtonWithTitle: &*ok]; + alert }) } diff --git a/src/appkit/app/mod.rs b/src/appkit/app/mod.rs index c706c7af..b13193c0 100644 --- a/src/appkit/app/mod.rs +++ b/src/appkit/app/mod.rs @@ -39,9 +39,9 @@ use std::sync::{Arc, Mutex}; use lazy_static::lazy_static; -use crate::id_shim::Id; +use objc::rc::{Id, Owned}; use objc::runtime::Object; -use objc::{class, msg_send, sel}; +use objc::{class, msg_send, msg_send_id, sel}; use crate::appkit::menu::Menu; use crate::foundation::{id, nil, AutoReleasePool, NSUInteger, NO, YES}; @@ -86,11 +86,11 @@ fn shared_application(handler: F) { /// application. pub struct App { /// The underlying Objective-C Object. - pub objc: Id, + pub objc: Id, /// The underlying Objective-C Object, which in this case is a delegate that forwards to the /// app delegate. - pub objc_delegate: Id, + pub objc_delegate: Id, /// The stored `AppDelegate`. pub delegate: Box, @@ -142,20 +142,17 @@ where let pool = AutoReleasePool::new(); - let objc = unsafe { - let app: id = msg_send![register_app_class(), sharedApplication]; - Id::from_ptr(app) - }; + let objc = unsafe { msg_send_id![register_app_class(), sharedApplication].unwrap() }; let app_delegate = Box::new(delegate); let objc_delegate = unsafe { let delegate_class = register_app_delegate_class::(); - let delegate: id = msg_send![delegate_class, new]; + let mut delegate: Id = msg_send_id![delegate_class, new].unwrap(); let delegate_ptr: *const T = &*app_delegate; - (&mut *delegate).set_ivar(APP_PTR, delegate_ptr as usize); - let _: () = msg_send![&*objc, setDelegate: delegate]; - Id::from_ptr(delegate) + delegate.set_ivar(APP_PTR, delegate_ptr as usize); + let _: () = msg_send![&*objc, setDelegate: &*delegate]; + delegate }; App { diff --git a/src/appkit/event/mod.rs b/src/appkit/event/mod.rs index 8db0239f..fc6bcda6 100644 --- a/src/appkit/event/mod.rs +++ b/src/appkit/event/mod.rs @@ -1,8 +1,8 @@ use block::ConcreteBlock; -use crate::id_shim::Id; +use objc::rc::{Id, Owned}; use objc::runtime::Object; -use objc::{class, msg_send, sel}; +use objc::{class, msg_send, msg_send_id, sel}; use crate::foundation::{id, nil, NSString}; @@ -14,15 +14,15 @@ pub enum EventMask { /// A wrapper over an `NSEvent`. #[derive(Debug)] -pub struct EventMonitor(pub Id); +pub struct EventMonitor(pub Id); /// A wrapper over an `NSEvent`. #[derive(Debug)] -pub struct Event(pub Id); +pub struct Event(pub Id); impl Event { pub(crate) fn new(objc: id) -> Self { - Event(unsafe { Id::from_ptr(objc) }) + Event(unsafe { Id::retain(objc).unwrap() }) } pub fn characters(&self) -> String { @@ -68,11 +68,12 @@ impl Event { let block = block.copy(); EventMonitor(unsafe { - Id::from_ptr(msg_send![ + msg_send_id![ class!(NSEvent), addLocalMonitorForEventsMatchingMask: 1024, handler: &*block, - ]) + ] + .unwrap() }) } } diff --git a/src/appkit/menu/item.rs b/src/appkit/menu/item.rs index b4105d6e..6712c606 100644 --- a/src/appkit/menu/item.rs +++ b/src/appkit/menu/item.rs @@ -5,11 +5,11 @@ use std::fmt; use std::sync::Once; -use crate::id_shim::Id; use block::ConcreteBlock; use objc::declare::ClassDecl; +use objc::rc::{Id, Owned}; use objc::runtime::{Class, Object, Sel}; -use objc::{class, msg_send, sel}; +use objc::{class, msg_send, msg_send_id, sel}; use crate::events::EventModifierFlag; use crate::foundation::{id, nil, NSString, NSUInteger}; @@ -40,7 +40,7 @@ fn make_menu_item>( key: Option<&str>, action: Option, modifiers: Option<&[EventModifierFlag]> -) -> Id { +) -> Id { unsafe { let title = NSString::new(title.as_ref()); @@ -53,14 +53,22 @@ fn make_menu_item>( // Stock menu items that use selectors targeted at system pieces are just standard // `NSMenuItem`s. If there's no custom ones, we use our subclass that has a slot to store a // handler pointer. - let alloc: id = msg_send![register_menu_item_class(), alloc]; - let item = Id::from_retained_ptr(match action { - Some(a) => msg_send![alloc, initWithTitle:&*title action:a keyEquivalent:&*key], - - None => msg_send![alloc, initWithTitle:&*title - action:sel!(fireBlockAction:) - keyEquivalent:&*key] - }); + let alloc = msg_send_id![register_menu_item_class(), alloc]; + let item = match action { + Some(a) => msg_send_id![ + alloc, + initWithTitle: &*title, + action: a, + keyEquivalent: &*key, + ], + None => msg_send_id![ + alloc, + initWithTitle: &*title, + action: sel!(fireBlockAction:), + keyEquivalent: &*key, + ] + } + .unwrap(); if let Some(modifiers) = modifiers { let mut key_mask: NSUInteger = 0; @@ -86,7 +94,7 @@ pub enum MenuItem { /// You can (and should) create this variant via the `new(title)` method, but if you need to do /// something crazier, then wrap it in this and you can hook into the Cacao menu system /// accordingly. - Custom(Id), + Custom(Id), /// Shows a standard "About" item, which will bring up the necessary window when clicked /// (include a `credits.html` in your App to make use of here). The argument baked in here @@ -153,7 +161,7 @@ pub enum MenuItem { impl MenuItem { /// Consumes and returns a handle for the underlying MenuItem. This is internal as we make a few assumptions /// for how it interacts with our `Menu` setup, but this could be made public in the future. - pub(crate) unsafe fn to_objc(self) -> Id { + pub(crate) unsafe fn to_objc(self) -> Id { match self { Self::Custom(objc) => objc, @@ -210,8 +218,7 @@ impl MenuItem { Self::Separator => { let cls = class!(NSMenuItem); - let separator: id = msg_send![cls, separatorItem]; - Id::from_ptr(separator) + msg_send_id![cls, separatorItem].unwrap() } } } diff --git a/src/appkit/menu/menu.rs b/src/appkit/menu/menu.rs index 00106f4d..30f7691d 100644 --- a/src/appkit/menu/menu.rs +++ b/src/appkit/menu/menu.rs @@ -2,9 +2,9 @@ use std::sync::{Arc, Mutex}; -use crate::id_shim::{Id, ShareId}; +use objc::rc::{Id, Owned, Shared}; use objc::runtime::Object; -use objc::{class, msg_send, sel}; +use objc::{class, msg_send, msg_send_id, sel}; use crate::appkit::menu::item::MenuItem; use crate::foundation::{id, NSInteger, NSString}; @@ -12,7 +12,7 @@ use crate::foundation::{id, NSInteger, NSString}; /// A struct that represents an `NSMenu`. It takes ownership of items, and handles instrumenting /// them throughout the application lifecycle. #[derive(Debug)] -pub struct Menu(pub Id); +pub struct Menu(pub Id); impl Menu { /// Creates a new `Menu` with the given title, and uses the passed items as submenu items. @@ -27,16 +27,16 @@ impl Menu { pub fn new(title: &str, items: Vec) -> Self { Menu(unsafe { let cls = class!(NSMenu); - let alloc: id = msg_send![cls, alloc]; + let alloc = msg_send_id![cls, alloc]; let title = NSString::new(title); - let menu: id = msg_send![alloc, initWithTitle:&*title]; + let mut menu = msg_send_id![alloc, initWithTitle: &*title].unwrap(); for item in items.into_iter() { let objc = item.to_objc(); - let _: () = msg_send![menu, addItem:&*objc]; + let _: () = msg_send![&mut menu, addItem:&*objc]; } - Id::from_retained_ptr(menu) + menu }) } diff --git a/src/appkit/printing/settings.rs b/src/appkit/printing/settings.rs index 40df0841..b13281bb 100644 --- a/src/appkit/printing/settings.rs +++ b/src/appkit/printing/settings.rs @@ -1,7 +1,7 @@ //! Represents settings for printing items. Backed by an `NSDictionary` in Objective-C, this struct //! aims to make it easier to query/process printing operations. -use crate::id_shim::ShareId; +use objc::rc::{Id, Shared}; use objc::runtime::Object; use crate::foundation::id; @@ -10,14 +10,14 @@ use crate::foundation::id; /// application/user. #[derive(Clone, Debug)] pub struct PrintSettings { - pub inner: ShareId + pub inner: Id } impl PrintSettings { /// Internal method, constructs a wrapper around the backing `NSDictionary` print settings. pub(crate) fn with_inner(inner: id) -> Self { PrintSettings { - inner: unsafe { ShareId::from_ptr(inner) } + inner: unsafe { Id::retain(inner).unwrap() } } } } diff --git a/src/appkit/toolbar/item.rs b/src/appkit/toolbar/item.rs index c3908ab4..b8a36b80 100644 --- a/src/appkit/toolbar/item.rs +++ b/src/appkit/toolbar/item.rs @@ -6,9 +6,9 @@ use core_graphics::geometry::CGSize; use std::fmt; -use crate::id_shim::{Id, ShareId}; +use objc::rc::{Id, Owned, Shared}; use objc::runtime::Object; -use objc::{class, msg_send, sel}; +use objc::{class, msg_send, msg_send_id, sel}; use crate::button::{BezelStyle, Button}; use crate::foundation::{id, NSString, NO, YES}; @@ -19,7 +19,7 @@ use crate::invoker::TargetActionHandler; #[derive(Debug)] pub struct ToolbarItem { pub identifier: String, - pub objc: Id, + pub objc: Id, pub button: Option