Refactor to use load_or_register_class

This commit is contained in:
Alexander Czernay 2023-04-16 21:24:44 +02:00
parent 2803922f3a
commit 718c831bc4
24 changed files with 448 additions and 974 deletions

View file

@ -2,27 +2,11 @@
//! creates a custom `NSApplication` subclass that currently does nothing; this is meant as a hook
//! for potential future use.
use std::sync::Once;
use objc::class;
use objc::declare::ClassDecl;
use objc::runtime::Class;
use crate::foundation::load_or_register_class;
/// Used for injecting a custom NSApplication. Currently does nothing.
pub(crate) fn register_app_class() -> *const Class {
static mut APP_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTApplication";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { APP_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSApplication);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
APP_CLASS = decl.register();
});
}
unsafe { APP_CLASS }
load_or_register_class("NSApplication", "RSTApplication", |decl| unsafe {})
}

View file

@ -3,24 +3,19 @@
//! for potential future use.
use std::ffi::c_void;
use std::sync::Once;
use block::Block;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use objc::{msg_send, sel, sel_impl};
use url::Url;
use crate::appkit::app::{AppDelegate, APP_PTR};
use crate::appkit::printing::PrintSettings;
use crate::error::Error;
use crate::foundation::{id, nil, to_bool, NSArray, NSString, NSUInteger, BOOL, NO, YES};
use crate::user_activity::UserActivity;
#[cfg(feature = "cloudkit")]
use crate::cloudkit::share::CKShareMetaData;
use crate::error::Error;
use crate::foundation::{id, load_or_register_class, nil, to_bool, NSArray, NSString, NSUInteger, BOOL, NO, YES};
use crate::user_activity::UserActivity;
/// A handy method for grabbing our `AppDelegate` from the pointer. This is different from our
/// standard `utils` version as this doesn't require `RefCell` backing.
@ -295,17 +290,7 @@ extern "C" fn delegate_handles_key<T: AppDelegate>(this: &Object, _: Sel, _: id,
/// Registers an `NSObject` application delegate, and configures it for the various callbacks and
/// pointers we need to have.
pub(crate) fn register_app_delegate_class<T: AppDelegate + AppDelegate>() -> *const Class {
static mut DELEGATE_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTAppDelegate";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { DELEGATE_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSObject);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSObject", "RSTAppDelegate", |decl| unsafe {
decl.add_ivar::<usize>(APP_PTR);
// Launching Applications
@ -465,10 +450,5 @@ pub(crate) fn register_app_delegate_class<T: AppDelegate + AppDelegate>() -> *co
sel!(application:delegateHandlesKey:),
delegate_handles_key::<T> as extern "C" fn(&Object, _, _, id) -> BOOL
);
DELEGATE_CLASS = decl.register();
});
}
unsafe { DELEGATE_CLASS }
})
}

View file

@ -3,16 +3,13 @@
//! now.
use std::fmt;
use std::sync::Once;
use block::ConcreteBlock;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use objc_id::Id;
use crate::events::EventModifierFlag;
use crate::foundation::{id, nil, NSString, NSUInteger};
use crate::foundation::{id, load_or_register_class, NSString, NSUInteger};
static BLOCK_PTR: &'static str = "cacaoMenuItemBlockPtr";
@ -314,24 +311,10 @@ extern "C" fn fire_block_action(this: &Object, _: Sel, _item: id) {
/// In general, we do not want to do more than we need to here - menus are one of the last areas
/// where Carbon still lurks, and subclassing things can get weird.
pub(crate) fn register_menu_item_class() -> *const Class {
static mut APP_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "CacaoMenuItem";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { APP_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSMenuItem);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSMenuItem", "CacaoMenuItem", |decl| unsafe {
decl.add_ivar::<usize>(BLOCK_PTR);
decl.add_method(sel!(dealloc), dealloc_cacao_menuitem as extern "C" fn(&Object, _));
decl.add_method(sel!(fireBlockAction:), fire_block_action as extern "C" fn(&Object, _, id));
APP_CLASS = decl.register();
});
}
unsafe { APP_CLASS }
})
}

View file

@ -1,31 +1,15 @@
//! Everything useful for the `WindowController`. Handles injecting an `NSWindowController` subclass
//! into the Objective C runtime, which loops back to give us lifecycle methods.
use std::sync::Once;
use objc::class;
use objc::declare::ClassDecl;
use objc::runtime::Class;
use crate::appkit::window::{WindowDelegate, WINDOW_DELEGATE_PTR};
use crate::foundation::load_or_register_class;
/// Injects an `NSWindowController` subclass, with some callback and pointer ivars for what we
/// need to do.
pub(crate) fn register_window_controller_class<T: WindowDelegate>() -> *const Class {
static mut DELEGATE_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTWindowController";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { DELEGATE_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSWindowController);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSWindowController", "RSTWindowController", |decl| unsafe {
decl.add_ivar::<usize>(WINDOW_DELEGATE_PTR);
DELEGATE_CLASS = decl.register();
});
}
unsafe { DELEGATE_CLASS }
})
}

View file

@ -21,36 +21,28 @@
//! my_view.add_subview(&button);
//! ```
use std::fmt;
use std::sync::Once;
use std::cell::RefCell;
use std::rc::Rc;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use objc::runtime::{Class, Object};
use objc::{msg_send, sel, sel_impl};
use objc_id::ShareId;
pub use enums::*;
#[cfg(feature = "appkit")]
use crate::appkit::FocusRingType;
use crate::color::Color;
use crate::control::Control;
use crate::foundation::{id, nil, NSString, NSUInteger, BOOL, NO, YES};
use crate::foundation::{id, load_or_register_class, nil, NSString, NSUInteger, NO, YES};
use crate::image::Image;
use crate::invoker::TargetActionHandler;
use crate::keys::Key;
use crate::layout::Layout;
use crate::objc_access::ObjcAccess;
use crate::text::{AttributedString, Font};
use crate::utils::{load, properties::ObjcProperty};
#[cfg(feature = "autolayout")]
use crate::layout::{LayoutAnchorDimension, LayoutAnchorX, LayoutAnchorY};
#[cfg(feature = "appkit")]
use crate::appkit::FocusRingType;
use crate::objc_access::ObjcAccess;
use crate::text::{AttributedString, Font};
use crate::utils::properties::ObjcProperty;
mod enums;
pub use enums::*;
/// Wraps `NSButton` on appkit, and `UIButton` on iOS and tvOS.
///
@ -343,20 +335,5 @@ impl Drop for Button {
/// Registers an `NSButton` subclass, and configures it to hold some ivars
/// for various things we need to store.
fn register_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTButton";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe {
VIEW_CLASS = c;
}
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSButton);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("NSButton", "RSTButton", |decl| unsafe {})
}

View file

@ -10,16 +10,11 @@
//! that enables this functionality, we want to be able to provide this with some level of
//! backwards compatibility for Mojave, as that's still a supported OS.
use std::os::raw::c_void;
use std::sync::Once;
use core_graphics::base::CGFloat;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use crate::foundation::{id, nil, NSArray, NSInteger, NSString, NSUInteger, NO, YES};
use crate::foundation::{id, load_or_register_class, nil, NSArray, NSInteger};
use crate::utils::os;
pub(crate) const AQUA_LIGHT_COLOR_NORMAL_CONTRAST: &'static str = "AQUA_LIGHT_COLOR_NORMAL_CONTRAST";
@ -259,17 +254,7 @@ extern "C" fn color_with_system_effect(this: &Object, _: Sel, effect: NSInteger)
}
pub(crate) fn register_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "CacaoDynamicColor";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSColor);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSColor", "CacaoDynamicColor", |decl| unsafe {
// These methods all need to be forwarded, so let's hook them up.
decl.add_method(sel!(colorSpace), color_space as extern "C" fn(&Object, _) -> id);
decl.add_method(
@ -363,10 +348,5 @@ pub(crate) fn register_class() -> *const Class {
decl.add_ivar::<id>(AQUA_LIGHT_COLOR_HIGH_CONTRAST);
decl.add_ivar::<id>(AQUA_DARK_COLOR_NORMAL_CONTRAST);
decl.add_ivar::<id>(AQUA_DARK_COLOR_HIGH_CONTRAST);
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}

View file

@ -7,38 +7,15 @@
//! for in the modern era. It also implements a few helpers for things like setting a background
//! color, and enforcing layer backing by default.
use std::sync::Once;
use objc::runtime::Class;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, sel, sel_impl};
use objc_id::Id;
use crate::dragdrop::DragInfo;
use crate::foundation::{id, NSUInteger, NO, YES};
use crate::utils::load;
use crate::view::{ViewDelegate, VIEW_DELEGATE_PTR};
use crate::foundation::load_or_register_class;
/// Injects an `NSView` subclass. This is used for the default views that don't use delegates - we
/// have separate classes here since we don't want to waste cycles on methods that will never be
/// used if there's no delegates.
pub(crate) fn register_image_view_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTImageView";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSImageView);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSImageView", "RSTImageView", |decl| unsafe {
//decl.add_method(sel!(isFlipped), enforce_normalcy as extern "C" fn(&Object, _) -> BOOL);
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}

View file

@ -1,31 +1,10 @@
use std::sync::Once;
use objc::runtime::Class;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, sel, sel_impl};
use objc_id::Id;
use crate::foundation::{id, NSUInteger, NO, YES};
use crate::utils::load;
use crate::view::{ViewDelegate, VIEW_DELEGATE_PTR};
use crate::foundation::load_or_register_class;
/// Injects an `NSView` subclass. This is used for the default views that don't use delegates - we
/// have separate classes here since we don't want to waste cycles on methods that will never be
/// used if there's no delegates.
pub(crate) fn register_image_view_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTImageView";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(UIImageView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).expect("Failed to get RSTVIEW");
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("UIImageView", "RSTImageView", |decl| unsafe {})
}

View file

@ -1,12 +1,7 @@
use std::sync::Once;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, msg_send, sel, sel_impl};
use objc_id::Id;
use objc::{msg_send, sel, sel_impl};
use crate::dragdrop::DragInfo;
use crate::foundation::{id, load_or_register_class, NSString, NSUInteger, NO, YES};
use crate::foundation::{id, load_or_register_class, NSString, NO, YES};
use crate::input::{TextFieldDelegate, TEXTFIELD_DELEGATE_PTR};
use crate::utils::load;
@ -52,21 +47,7 @@ extern "C" fn text_should_end_editing<T: TextFieldDelegate>(this: &mut Object, _
/// have separate classes here since we don't want to waste cycles on methods that will never be
/// used if there's no delegates.
pub(crate) fn register_view_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTTextInputField";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSTextField);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("NSTextField", "RSTTextInputField", |decl| unsafe {})
}
/// Injects an `NSTextField` subclass, with some callback and pointer ivars for what we

View file

@ -9,15 +9,12 @@
//! is going away.
use std::fmt;
use std::sync::{Arc, Mutex, Once};
use block::{Block, ConcreteBlock, RcBlock};
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use objc::{msg_send, sel, sel_impl};
use objc_id::ShareId;
use crate::foundation::{id, nil, NSString};
use crate::foundation::{id, load_or_register_class};
use crate::utils::load;
pub static ACTION_CALLBACK_PTR: &str = "rstTargetActionPtr";
@ -95,25 +92,8 @@ extern "C" fn perform<F: Fn() + 'static>(this: &mut Object, _: Sel, _sender: id)
/// on drop. We handle the heap copy on the Rust side, so setting the block
/// is just an ivar.
pub(crate) fn register_invoker_class<F: Fn() + 'static>() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTTargetActionHandler";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe {
VIEW_CLASS = c;
}
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSObject);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSObject", "RSTTargetActionHandler", |decl| unsafe {
decl.add_ivar::<usize>(ACTION_CALLBACK_PTR);
decl.add_method(sel!(perform:), perform::<F> as extern "C" fn(&mut Object, _, id));
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}

View file

@ -7,16 +7,13 @@
//! for in the modern era. It also implements a few helpers for things like setting a background
//! color, and enforcing layer backing by default.
use std::sync::Once;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, msg_send, sel, sel_impl};
use objc::{msg_send, sel, sel_impl};
use objc_id::Id;
use crate::appkit::menu::{Menu, MenuItem};
use crate::appkit::menu::Menu;
use crate::dragdrop::DragInfo;
use crate::foundation::{id, load_or_register_class, nil, NSArray, NSInteger, NSUInteger, NO, YES};
use crate::foundation::{id, load_or_register_class, NSArray, NSInteger, NSUInteger, NO, YES};
use crate::listview::{ListViewDelegate, RowEdge, LISTVIEW_DELEGATE_PTR};
use crate::utils::load;
@ -185,21 +182,7 @@ extern "C" fn dragging_exited<T: ListViewDelegate>(this: &mut Object, _: Sel, in
/// `UITableView` semantics; if `NSTableView`'s multi column behavior is needed, then it can
/// be added in.
pub(crate) fn register_listview_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &'static str = "RSTListView";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSTableView);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("NSTableView", "RSTListView", |decl| unsafe {})
}
/// Injects an `NSTableView` subclass, with some callback and pointer ivars for what we

View file

@ -7,15 +7,12 @@
//! for in the modern era. It also implements a few helpers for things like setting a background
//! color, and enforcing layer backing by default.
use std::sync::Once;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, msg_send, sel, sel_impl};
use objc_id::Id;
use crate::dragdrop::DragInfo;
use crate::foundation::{id, nil, NSUInteger, NO, YES};
use crate::foundation::{id, load_or_register_class, nil, NSUInteger, NO, YES};
use crate::listview::row::{ViewDelegate, BACKGROUND_COLOR, LISTVIEW_ROW_DELEGATE_PTR};
use crate::utils::load;
@ -107,40 +104,15 @@ extern "C" fn dealloc<T: ViewDelegate>(this: &Object, _: Sel) {
/// have separate classes here since we don't want to waste cycles on methods that will never be
/// used if there's no delegates.
pub(crate) fn register_listview_row_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTTableViewRow";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSView", "RSTTableViewRow", |decl| unsafe {
decl.add_method(sel!(isFlipped), enforce_normalcy as extern "C" fn(&Object, _) -> BOOL);
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}
/// Injects an `NSView` subclass, with some callback and pointer ivars for what we
/// need to do.
pub(crate) fn register_listview_row_class_with_delegate<T: ViewDelegate>() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTableViewRowWithDelegate";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSView", "RSTableViewRowWithDelegate", |decl| unsafe {
// A pointer to the "view controller" on the Rust side. It's expected that this doesn't
// move.
decl.add_ivar::<usize>(LISTVIEW_ROW_DELEGATE_PTR);
@ -173,10 +145,5 @@ pub(crate) fn register_listview_row_class_with_delegate<T: ViewDelegate>() -> *c
// Cleanup
decl.add_method(sel!(dealloc), dealloc::<T> as extern "C" fn(&Object, _));
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}

View file

@ -1,53 +1,24 @@
use std::sync::Once;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, sel, sel_impl};
use objc_id::Id;
use crate::foundation::{id, YES, NO, NSUInteger};
use crate::dragdrop::DragInfo;
use crate::view::{VIEW_DELEGATE_PTR, ViewDelegate};
use crate::foundation::{id, NSUInteger, NO, YES};
use crate::utils::load;
use crate::view::{ViewDelegate, VIEW_DELEGATE_PTR};
/// Injects an `NSView` subclass. This is used for the default views that don't use delegates - we
/// have separate classes here since we don't want to waste cycles on methods that will never be
/// used if there's no delegates.
pub(crate) fn register_view_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTView";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(UIView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("UIView", "RSTView", |decl| unsafe {})
}
/// Injects an `NSView` subclass, with some callback and pointer ivars for what we
/// need to do.
pub(crate) fn register_view_class_with_delegate<T: ViewDelegate>() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTViewWithDelegate";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(UIView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("UIView", "RSTViewWithDelegate", |decl| unsafe {
decl.add_ivar::<usize>(VIEW_DELEGATE_PTR);
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}

View file

@ -7,15 +7,12 @@
//! for in the modern era. It also implements a few helpers for things like setting a background
//! color, and enforcing layer backing by default.
use std::sync::Once;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, sel, sel_impl};
use objc::{sel, sel_impl};
use objc_id::Id;
use crate::dragdrop::DragInfo;
use crate::foundation::{id, NSUInteger, NO, YES};
use crate::foundation::{id, load_or_register_class, NSUInteger, NO, YES};
use crate::scrollview::{ScrollViewDelegate, SCROLLVIEW_DELEGATE_PTR};
use crate::utils::load;
@ -77,37 +74,13 @@ extern "C" fn dragging_exited<T: ScrollViewDelegate>(this: &mut Object, _: Sel,
/// Injects an `NSScrollView` subclass.
pub(crate) fn register_scrollview_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &'static str = "RSTScrollView";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSScrollView);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("NSScrollView", "RSTScrollView", |decl| unsafe {})
}
/// Injects an `NSView` subclass, with some callback and pointer ivars for what we
/// need to do.
pub(crate) fn register_scrollview_class_with_delegate<T: ScrollViewDelegate>() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &'static str = "RSTScrollViewWithDelegate";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSScrollView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSScrollView", "RSTScrollViewWithDelegate", |decl| unsafe {
// A pointer to the "view controller" on the Rust side. It's expected that this doesn't
// move.
decl.add_ivar::<usize>(SCROLLVIEW_DELEGATE_PTR);
@ -135,10 +108,5 @@ pub(crate) fn register_scrollview_class_with_delegate<T: ScrollViewDelegate>() -
sel!(draggingExited:),
dragging_exited::<T> as extern "C" fn(&mut Object, _, _)
);
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}

View file

@ -1,24 +1,19 @@
//! Implements a Select-style dropdown. By default this uses NSPopupSelect on macOS.
use std::sync::Once;
use core_graphics::geometry::CGRect;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use objc::runtime::{Class, Object};
use objc::{msg_send, sel, sel_impl};
use objc_id::ShareId;
use crate::control::Control;
use crate::foundation::{id, nil, NSInteger, NSString, NO, YES};
use crate::foundation::{id, load_or_register_class, nil, NSInteger, NSString, NO, YES};
use crate::geometry::Rect;
use crate::invoker::TargetActionHandler;
use crate::layout::Layout;
use crate::objc_access::ObjcAccess;
use crate::utils::properties::ObjcProperty;
#[cfg(feature = "autolayout")]
use crate::layout::{LayoutAnchorDimension, LayoutAnchorX, LayoutAnchorY};
use crate::objc_access::ObjcAccess;
use crate::utils::properties::ObjcProperty;
/// Wraps `NSPopUpSelect` on AppKit. Not currently implemented for iOS.
///
@ -268,19 +263,5 @@ impl Drop for Select {
/// Registers an `NSSelect` subclass, and configures it to hold some ivars
/// for various things we need to store.
fn register_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "CacaoSelect";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSPopUpButton);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("NSPopUpButton", "CacaoSelect", |decl| unsafe {})
}

View file

@ -1,22 +1,17 @@
//! A wrapper for NSSwitch. Currently the epitome of jank - if you're poking around here, expect
//! that this will change at some point.
use std::fmt;
use std::sync::Once;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use objc::runtime::{Class, Object};
use objc::{msg_send, sel, sel_impl};
use objc_id::ShareId;
use crate::foundation::{id, nil, NSString, BOOL, NO, YES};
use crate::foundation::{id, load_or_register_class, nil, NSString, NO};
use crate::invoker::TargetActionHandler;
use crate::layout::Layout;
use crate::objc_access::ObjcAccess;
use crate::utils::{load, properties::ObjcProperty};
#[cfg(feature = "autolayout")]
use crate::layout::{LayoutAnchorDimension, LayoutAnchorX, LayoutAnchorY};
use crate::objc_access::ObjcAccess;
use crate::utils::properties::ObjcProperty;
/// A wrapper for `NSSwitch`. Holds (retains) pointers for the Objective-C runtime
/// where our `NSSwitch` lives.
@ -178,21 +173,5 @@ impl Drop for Switch {
/// Registers an `NSButton` subclass, and configures it to hold some ivars
/// for various things we need to store.
fn register_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTSwitch";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe {
VIEW_CLASS = c;
}
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSButton);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("NSButton", "RSTSwitch", |decl| unsafe {})
}

View file

@ -7,60 +7,24 @@
//! for in the modern era. It also implements a few helpers for things like setting a background
//! color, and enforcing layer backing by default.
use std::sync::Once;
use objc::runtime::Class;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, sel, sel_impl};
use objc_id::Id;
use crate::dragdrop::DragInfo;
use crate::foundation::{id, NSUInteger, NO, YES};
use crate::foundation::load_or_register_class;
use crate::text::label::{LabelDelegate, LABEL_DELEGATE_PTR};
use crate::utils::load;
/// Injects an `NSTextField` subclass. This is used for the default views that don't use delegates - we
/// have separate classes here since we don't want to waste cycles on methods that will never be
/// used if there's no delegates.
pub(crate) fn register_view_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTTextField";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSTextField);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("NSTextField", "RSTTextField", |decl| unsafe {})
}
/// Injects an `NSTextField` subclass, with some callback and pointer ivars for what we
/// need to do.
pub(crate) fn register_view_class_with_delegate<T: LabelDelegate>() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTTextFieldWithDelegate";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSView", "RSTTextFieldWithDelegate", |decl| unsafe {
// A pointer to the "view controller" on the Rust side. It's expected that this doesn't
// move.
decl.add_ivar::<usize>(LABEL_DELEGATE_PTR);
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}

View file

@ -2,27 +2,11 @@
//! creates a custom `UIApplication` subclass that currently does nothing; this is meant as a hook
//! for potential future use.
use std::sync::Once;
use objc::class;
use objc::declare::ClassDecl;
use objc::runtime::Class;
use crate::foundation::load_or_register_class;
/// Used for injecting a custom UIApplication. Currently does nothing.
pub(crate) fn register_app_class() -> *const Class {
static mut APP_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTApplication";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { APP_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(UIApplication);
let decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
APP_CLASS = decl.register();
});
}
unsafe { APP_CLASS }
load_or_register_class("UIApplication", "RSTApplication", |decl| unsafe {})
}

View file

@ -2,28 +2,20 @@
//! creates a custom `UIApplication` subclass that currently does nothing; this is meant as a hook
//! for potential future use.
//use std::ffi::c_void;
use std::sync::Once;
use objc::runtime::{Class, Object, Sel};
use objc::{sel, sel_impl};
//use crate::error::Error;
use crate::foundation::{id, load_or_register_class, BOOL, YES};
use crate::uikit::app::{AppDelegate, APP_DELEGATE};
use crate::uikit::scene::{SceneConnectionOptions, SceneSession};
//use std::unreachable;
//use block::Block;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use url::Url;
//use crate::error::Error;
use crate::foundation::{id, nil, NSArray, NSString, NSUInteger, BOOL, NO, YES};
//use crate::user_activity::UserActivity;
use crate::uikit::app::{AppDelegate, APP_DELEGATE};
use crate::uikit::scene::{SceneConfig, SceneConnectionOptions, SceneSession};
#[cfg(feature = "cloudkit")]
use crate::cloudkit::share::CKShareMetaData;
/// A handy method for grabbing our `AppDelegate` from the pointer. This is different from our
/// standard `utils` version as this doesn't require `RefCell` backing.
fn app<T>(this: &Object) -> &T {
@ -49,17 +41,7 @@ extern "C" fn configuration_for_scene_session<T: AppDelegate>(this: &Object, _:
/// Registers an `NSObject` application delegate, and configures it for the various callbacks and
/// pointers we need to have.
pub(crate) fn register_app_delegate_class<T: AppDelegate>() -> *const Class {
static mut DELEGATE_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTAppDelegate";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { DELEGATE_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSObject);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSObject", "RSTAppDelegate", |decl| unsafe {
// Launching Applications
decl.add_method(
sel!(application:didFinishLaunchingWithOptions:),
@ -75,10 +57,5 @@ pub(crate) fn register_app_delegate_class<T: AppDelegate>() -> *const Class {
sel!(application:didDiscardSceneSessions:),
did_discard_scene_sessions::<T> as extern "C" fn(&Object, _, _, id)
);*/
DELEGATE_CLASS = decl.register();
});
}
unsafe { DELEGATE_CLASS }
})
}

View file

@ -1,25 +1,10 @@
use std::ffi::c_void;
use std::sync::Once;
use std::unreachable;
use block::Block;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::runtime::{Class, Object, Protocol, Sel};
use objc::{class, msg_send, sel, sel_impl};
use url::Url;
use crate::error::Error;
use crate::foundation::{id, nil, NSArray, NSString, NSUInteger, BOOL, NO, YES};
use crate::user_activity::UserActivity;
use crate::utils::load;
use crate::foundation::{id, load_or_register_class};
use crate::uikit::app::SCENE_DELEGATE_VENDOR;
use crate::uikit::scene::{Scene, SceneConfig, SceneConnectionOptions, SceneSession, WindowSceneDelegate};
#[cfg(feature = "cloudkit")]
use crate::cloudkit::share::CKShareMetaData;
use crate::uikit::scene::{Scene, SceneConnectionOptions, SceneSession, WindowSceneDelegate};
use crate::utils::load;
pub(crate) static WINDOW_SCENE_PTR: &str = "rstWindowSceneDelegatePtr";
@ -60,18 +45,7 @@ extern "C" fn scene_will_connect_to_session_with_options<T: WindowSceneDelegate>
/// Registers an `NSObject` application delegate, and configures it for the various callbacks and
/// pointers we need to have.
pub(crate) fn register_window_scene_delegate_class<T: WindowSceneDelegate, F: Fn() -> Box<T>>() -> *const Class {
static mut DELEGATE_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTWindowSceneDelegate";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { DELEGATE_CLASS = c };
} else {
use objc::runtime::Protocol;
INIT.call_once(|| unsafe {
let superclass = class!(UIResponder);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("UIResponder", "RSTWindowSceneDelegate", |decl| unsafe {
let p = Protocol::get("UIWindowSceneDelegate").unwrap();
// A spot to hold a pointer to
@ -86,11 +60,5 @@ pub(crate) fn register_window_scene_delegate_class<T: WindowSceneDelegate, F: Fn
sel!(scene:willConnectToSession:options:),
scene_will_connect_to_session_with_options::<T> as extern "C" fn(&Object, _, _, _, _)
);
// Launching Applications
DELEGATE_CLASS = decl.register();
});
}
unsafe { DELEGATE_CLASS }
})
}

View file

@ -7,8 +7,6 @@
//! for in the modern era. It also implements a few helpers for things like setting a background
//! color, and enforcing layer backing by default.
use std::sync::Once;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, msg_send, sel, sel_impl};
@ -92,28 +90,13 @@ extern "C" fn update_layer(this: &Object, _: Sel) {
/// have separate classes here since we don't want to waste cycles on methods that will never be
/// used if there's no delegates.
pub(crate) fn register_view_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTView";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(NSView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("NSView", "RSTView", |decl| unsafe {
decl.add_method(sel!(isFlipped), enforce_normalcy as extern "C" fn(&Object, _) -> BOOL);
decl.add_method(sel!(updateLayer), update_layer as extern "C" fn(&Object, _));
decl.add_method(sel!(wantsUpdateLayer), enforce_normalcy as extern "C" fn(&Object, _) -> BOOL);
decl.add_ivar::<id>(BACKGROUND_COLOR);
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}
/// Injects an `NSView` subclass, with some callback and pointer ivars for what we

View file

@ -1,13 +1,10 @@
use std::sync::Once;
use std::unreachable;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use crate::foundation::{BOOL};
use crate::view::{VIEW_DELEGATE_PTR, ViewDelegate};
use crate::utils::{load, as_bool};
use crate::foundation::BOOL;
use crate::utils::{as_bool, load};
use crate::view::{ViewDelegate, VIEW_DELEGATE_PTR};
/// Called when the view controller receives a `viewWillAppear:` message.
extern "C" fn will_appear<T: ViewDelegate>(this: &mut Object, _: Sel, animated: BOOL) {
@ -51,17 +48,7 @@ extern "C" fn did_disappear<T: ViewDelegate>(this: &mut Object, _: Sel, animated
/// Registers an `NSViewDelegate`.
pub(crate) fn register_view_controller_class<T: ViewDelegate + 'static>() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTViewController";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(UIViewController);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
load_or_register_class("UIViewController", "RSTViewController", |decl| unsafe {
decl.add_ivar::<usize>(VIEW_DELEGATE_PTR);
decl.add_method(sel!(viewWillAppear:), will_appear::<T> as extern "C" fn(&mut Object, _, BOOL));
@ -74,9 +61,5 @@ pub(crate) fn register_view_controller_class<T: ViewDelegate + 'static>() -> *co
sel!(viewDidDisappear:),
did_disappear::<T> as extern "C" fn(&mut Object, _, BOOL)
);
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
})
}

View file

@ -1,5 +1,3 @@
use std::sync::Once;
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, sel, sel_impl};
@ -14,21 +12,7 @@ use crate::view::{ViewDelegate, VIEW_DELEGATE_PTR};
/// have separate classes here since we don't want to waste cycles on methods that will never be
/// used if there's no delegates.
pub(crate) fn register_view_class() -> *const Class {
static mut VIEW_CLASS: *const Class = 0 as *const Class;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTView";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { VIEW_CLASS = c };
} else {
INIT.call_once(|| unsafe {
let superclass = class!(UIView);
let mut decl = ClassDecl::new(CLASS_NAME, superclass).unwrap();
VIEW_CLASS = decl.register();
});
}
unsafe { VIEW_CLASS }
load_or_register_class("UIView", "RSTView", |decl| unsafe {})
}
/// Injects a `UIView` subclass, with some callback and pointer ivars for what we

View file

@ -4,46 +4,23 @@
//!
//! If you use that feature, there are no guarantees you'll be accepted into the App Store.
use std::sync::Once;
use std::ffi::c_void;
use block::Block;
use cocoa::foundation::{NSRect, NSPoint, NSSize, NSString, NSArray, NSInteger};
use cocoa::foundation::{NSArray, NSInteger, NSPoint, NSRect, NSSize, NSString};
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::{class, msg_send, sel, sel_impl};
use crate::foundation::{id, nil, YES, NO};
use crate::foundation::{id, nil, NO, YES};
use crate::webview::traits::WebViewController;
extern "C" fn download_delegate(this: &Object, _: Sel) -> id {
println!("YO!");
unsafe {
NSString::alloc(nil).init_str("")
}
unsafe { NSString::alloc(nil).init_str("") }
}
pub fn register_process_pool() -> *const Object {
static mut PROCESS_POOL: *const Object = 0 as *const Object;
static INIT: Once = Once::new();
const CLASS_NAME: &str = "RSTWebViewProcessPool";
if let Some(c) = Class::get(CLASS_NAME) {
unsafe { PROCESS_POOL = c };
} else {
INIT.call_once(|| unsafe {
let superclass = Class::get("WKProcessPool").unwrap();
let mut decl = ClassDecl::new("RSTWebViewProcessPool", superclass).unwrap();
load_or_register_class("WKProcessPool", "RSTWebViewProcessPool", |decl| unsafe {
//decl.add_ivar::<id>(DOWNLOAD_DELEGATE_PTR);
decl.add_method(sel!(_downloadDelegate), download_delegate as extern "C" fn(&Object, _) -> id);
//PROCESS_POOL = decl.register();
PROCESS_POOL = msg_send![decl.register(), new];
});
}
unsafe { PROCESS_POOL }
})
}