Skip to content

Commit

Permalink
ns::Notification and fixes for ca::Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed May 2, 2023
1 parent 25a473b commit 0985976
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
4 changes: 4 additions & 0 deletions cidre/pomace/ns/ns.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Class NS_UUID;
Class NS_VALUE;

Class NS_FILE_MANAGER;
Class NS_NOTIFICATION;
Class NS_CODER;

__attribute__((constructor))
static void common_initializer(void)
Expand Down Expand Up @@ -99,6 +101,8 @@ static void common_initializer(void)
NS_VALUE = [NSValue class];

NS_FILE_MANAGER = [NSFileManager class];
NS_NOTIFICATION = [NSNotification class];
NS_CODER = [NSCoder class];

initialized = 1;
}
Expand Down
13 changes: 5 additions & 8 deletions cidre/pomace/sc/sc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ static void mtl_initializer(void)
{
static int initialized = 0;
if (!initialized) {
if (@available(macOS 12.3, *)) {
SC_STREAM_CONFIGURATION = [SCStreamConfiguration class];
SC_CONTENT_FILTER = [SCContentFilter class];
SC_STREAM = [SCStream class];
SC_SHAREABLE_CONTENT = [SCShareableContent class];
} else {
// Fallback on earlier versions
}

SC_STREAM_CONFIGURATION = [SCStreamConfiguration class];
SC_CONTENT_FILTER = [SCContentFilter class];
SC_STREAM = [SCStream class];
SC_SHAREABLE_CONTENT = [SCShareableContent class];

initialized = 1;
}
Expand Down
6 changes: 3 additions & 3 deletions cidre/src/ca/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{arc, cf, cv, define_cls, define_obj_type, ns, objc};
use crate::{arc, ca, cf, cg, cv, define_cls, define_obj_type, mtl, ns, objc};

define_obj_type!(OptionKey(ns::String));

Expand All @@ -24,7 +24,7 @@ impl Renderer {
options: Option<&ns::Dictionary<OptionKey, ns::Id>>,
) -> &'ar Self;

#[objc::rar_retain]
#[objc::cls_rar_retain]
pub fn with_mtl_texture(
texture: &mtl::Texture,
options: Option<&ns::Dictionary<OptionKey, ns::Id>>,
Expand All @@ -46,7 +46,7 @@ impl Renderer {
#[objc::msg_send(beginFrameAtTime:timeStamp:)]
pub fn begin_frame_at(&mut self, time_stamp: cf::TimeInterval, ts: Option<&cv::TimeStamp>);

#[objc::msg_send(updateBounds)]
#[objc::msg_send(updateBounds:)]
pub fn update_bounds(&mut self, bounds: cg::Rect);

#[objc::msg_send(addUpdateRect:)]
Expand Down
7 changes: 7 additions & 0 deletions cidre/src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,10 @@ pub use path_utilities::temporary_directory;
pub use path_utilities::user_name;
pub use path_utilities::SearchPathDirectory;
pub use path_utilities::SearchPathDomainMask;

mod notification;
pub use notification::Notification;
pub use notification::NotificationName;

mod coder;
pub use coder::Coder;
38 changes: 38 additions & 0 deletions cidre/src/ns/coder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::{arc, define_obj_type, ns, objc};

/// Describes the action an ns::Coder should take when it encounters decode failures (e.g. corrupt data)
/// for non-TopLevel decodes.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[repr(isize)]
pub enum DecodingFailurePolicy {
/// On decode failure, the ns::Coder will raise an exception internally
/// to propagate failure messages (and unwind the stack). This exception can be transformed
/// into an ns::Error via any of the TopLevel decode APIs.
RaiseException,

/// On decode failure, the NSCoder will capture the failure as an ns::Error,
/// and prevent further decodes (by returning 0 / None equivalent as appropriate).
/// Clients should consider using this policy if they know that all encoded objects behave correctly
/// in the presence of decode failures (e.g. they use fail_with_error to communicate decode failures
/// and don't raise exceptions for error propagation)
SetErrorAndReturn,
}

define_obj_type!(Coder(ns::Id), NS_CODER);
impl Coder {}

#[link(name = "ns", kind = "static")]
extern "C" {
static NS_CODER: &'static objc::Class<Coder>;
}

#[cfg(test)]
mod tests {
use crate::ns;

#[test]
fn basics() {
let coder = ns::Coder::new();
println!("{coder:?}");
}
}
48 changes: 48 additions & 0 deletions cidre/src/ns/notification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::{arc, define_cls, define_obj_type, ns, objc};

define_obj_type!(NotificationName(ns::String));

define_obj_type!(Notification(ns::Id));

impl arc::A<Notification> {
#[objc::msg_send(initWithName:object:userInfo:)]
pub fn init_with_name(
self,
name: &ns::NotificationName,
object: Option<&ns::Id>,
user_info: Option<&ns::Dictionary<ns::Id, ns::Id>>,
) -> arc::R<Notification>;

#[objc::msg_send(initWithCoder:)]
pub fn init_with_coder(self, coder: &ns::Coder) -> arc::R<Notification>;
}

impl Notification {
define_cls!(NS_NOTIFICATION);

#[objc::msg_send(name)]
pub fn name(&self) -> &ns::NotificationName;

#[objc::msg_send(object)]
pub fn id(&self) -> Option<&ns::Id>;

#[objc::msg_send(userInfo)]
pub fn user_info(&self) -> Option<&ns::Dictionary<ns::Id, ns::Id>>;

pub fn with_name(
name: &ns::NotificationName,
object: Option<&ns::Id>,
user_info: Option<&ns::Dictionary<ns::Id, ns::Id>>,
) -> arc::R<Self> {
Self::alloc().init_with_name(name, object, user_info)
}

pub fn with_coder(coder: &ns::Coder) -> arc::R<Self> {
Self::alloc().init_with_coder(coder)
}
}

#[link(name = "ns", kind = "static")]
extern "C" {
static NS_NOTIFICATION: &'static objc::Class<Notification>;
}

0 comments on commit 0985976

Please sign in to comment.