Skip to content

Commit

Permalink
Replace core_graphics geometry types with those from objc2_foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jul 20, 2022
1 parent ffeb4b3 commit ed05094
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 131 deletions.
6 changes: 3 additions & 3 deletions src/appkit/toolbar/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
//!
//! UNFORTUNATELY, this is a very old and janky API. So... yeah.
use core_graphics::geometry::CGSize;
use std::fmt;

use objc::rc::{Id, Owned, Shared};
use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};
use objc2_foundation::NSSize;

use crate::button::{BezelStyle, Button};
use crate::foundation::{id, NSString, NO, YES};
Expand Down Expand Up @@ -87,15 +87,15 @@ impl ToolbarItem {
/// Sets the minimum size for this button.
pub fn set_min_size(&mut self, width: f64, height: f64) {
unsafe {
let size = CGSize::new(width.into(), height.into());
let size = NSSize::new(width.into(), height.into());
let _: () = msg_send![&*self.objc, setMinSize: size];
}
}

/// Sets the maximum size for this button.
pub fn set_max_size(&mut self, width: f64, height: f64) {
unsafe {
let size = CGSize::new(width.into(), height.into());
let size = NSSize::new(width.into(), height.into());
let _: () = msg_send![&*self.objc, setMaxSize: size];
}
}
Expand Down
22 changes: 8 additions & 14 deletions src/appkit/window/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use std::sync::Once;

use core_graphics::base::CGFloat;
use objc2_foundation::{CGFloat, NSSize};

use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, sel};

use crate::appkit::window::{WindowDelegate, WINDOW_DELEGATE_PTR};
use crate::foundation::{id, load_or_register_class, NSUInteger, BOOL, NO, YES};
use crate::utils::{load, CGSize};
use crate::utils::load;

/// Called when an `NSWindowDelegate` receives a `windowWillClose:` event.
/// Good place to clean up memory and what not.
Expand Down Expand Up @@ -56,14 +56,11 @@ extern "C" fn did_change_screen_profile<T: WindowDelegate>(this: &Object, _: Sel
}

/// Called when an `NSWindowDelegate` receives a `windowDidChangeScreen:` event.
extern "C" fn will_resize<T: WindowDelegate>(this: &Object, _: Sel, _: id, size: CGSize) -> CGSize {
extern "C" fn will_resize<T: WindowDelegate>(this: &Object, _: Sel, _: id, size: NSSize) -> NSSize {
let window = load::<T>(this, WINDOW_DELEGATE_PTR);
let s = window.will_resize(size.width as f64, size.height as f64);
let s = window.will_resize(size.width() as f64, size.height() as f64);

CGSize {
width: s.0 as CGFloat,
height: s.1 as CGFloat
}
NSSize::new(s.0 as CGFloat, s.1 as CGFloat)
}

/// Called when an `NSWindowDelegate` receives a `windowDidChangeScreen:` event.
Expand Down Expand Up @@ -115,15 +112,12 @@ extern "C" fn did_enter_full_screen<T: WindowDelegate>(this: &Object, _: Sel, _:
}

/// Called when an `NSWindowDelegate` receives a `windowDidChangeScreenProfile:` event.
extern "C" fn content_size_for_full_screen<T: WindowDelegate>(this: &Object, _: Sel, _: id, size: CGSize) -> CGSize {
extern "C" fn content_size_for_full_screen<T: WindowDelegate>(this: &Object, _: Sel, _: id, size: NSSize) -> NSSize {
let window = load::<T>(this, WINDOW_DELEGATE_PTR);

let (width, height) = window.content_size_for_full_screen(size.width as f64, size.height as f64);
let (width, height) = window.content_size_for_full_screen(size.width() as f64, size.height() as f64);

CGSize {
width: width as CGFloat,
height: height as CGFloat
}
NSSize::new(width as CGFloat, height as CGFloat)
}

/// Called when an `NSWindowDelegate` receives a `windowDidChangeScreenProfile:` event.
Expand Down
15 changes: 7 additions & 8 deletions src/appkit/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use block::ConcreteBlock;

use core_graphics::base::CGFloat;
use core_graphics::geometry::{CGRect, CGSize};
use objc2_foundation::{CGFloat, NSRect, NSSize};

use objc::rc::{Id, Owned, Shared};
use objc::runtime::Object;
Expand Down Expand Up @@ -74,7 +73,7 @@ impl Window {
// Other types of backing (Retained/NonRetained) are archaic, dating back to the
// NeXTSTEP era, and are outright deprecated... so we don't allow setting them.
let buffered: NSUInteger = 2;
let dimensions: CGRect = config.initial_dimensions.into();
let dimensions: NSRect = config.initial_dimensions.into();
let window = msg_send_id![
msg_send_id![class!(NSWindow), alloc],
initWithContentRect: dimensions,
Expand Down Expand Up @@ -135,7 +134,7 @@ where
// Other types of backing (Retained/NonRetained) are archaic, dating back to the
// NeXTSTEP era, and are outright deprecated... so we don't allow setting them.
let buffered: NSUInteger = 2;
let dimensions: CGRect = config.initial_dimensions.into();
let dimensions: NSRect = config.initial_dimensions.into();
let mut window: Id<Object, Owned> = msg_send_id![
msg_send_id![class, alloc],
initWithContentRect: dimensions,
Expand Down Expand Up @@ -238,31 +237,31 @@ impl<T> Window<T> {
/// Sets the content size for this window.
pub fn set_content_size<F: Into<f64>>(&self, width: F, height: F) {
unsafe {
let size = CGSize::new(width.into(), height.into());
let size = NSSize::new(width.into(), height.into());
let _: () = msg_send![&*self.objc, setContentSize: size];
}
}

/// Sets the minimum size this window can shrink to.
pub fn set_minimum_content_size<F: Into<f64>>(&self, width: F, height: F) {
unsafe {
let size = CGSize::new(width.into(), height.into());
let size = NSSize::new(width.into(), height.into());
let _: () = msg_send![&*self.objc, setContentMinSize: size];
}
}

/// Sets the maximum size this window can shrink to.
pub fn set_maximum_content_size<F: Into<f64>>(&self, width: F, height: F) {
unsafe {
let size = CGSize::new(width.into(), height.into());
let size = NSSize::new(width.into(), height.into());
let _: () = msg_send![&*self.objc, setContentMaxSize: size];
}
}

/// Sets the minimum size this window can shrink to.
pub fn set_minimum_size<F: Into<f64>>(&self, width: F, height: F) {
unsafe {
let size = CGSize::new(width.into(), height.into());
let size = NSSize::new(width.into(), height.into());
let _: () = msg_send![&*self.objc, setMinSize: size];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/color/appkit_dynamic_color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use std::os::raw::c_void;
use std::sync::Once;

use core_graphics::base::CGFloat;
use objc2_foundation::CGFloat;

use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
Expand Down
2 changes: 1 addition & 1 deletion src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use std::sync::{Arc, RwLock};

use core_foundation::base::TCFType;
use core_graphics::base::CGFloat;
use core_graphics::color::CGColor;
use objc2_foundation::CGFloat;

use objc::rc::{Id, Owned};
use objc::runtime::Object;
Expand Down
16 changes: 8 additions & 8 deletions src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Wrapper methods for various geometry types (rects, sizes, ec).
use core_graphics::geometry::{CGPoint, CGRect, CGSize};
use objc2_foundation::{NSPoint, NSRect, NSSize};

/// A struct that represents a box - top, left, width and height. You might use this for, say,
/// setting the initial frame of a view.
Expand Down Expand Up @@ -41,19 +41,19 @@ impl Rect {
}
}

impl From<Rect> for CGRect {
fn from(rect: Rect) -> CGRect {
CGRect::new(&CGPoint::new(rect.left, rect.top), &CGSize::new(rect.width, rect.height))
impl From<Rect> for NSRect {
fn from(rect: Rect) -> NSRect {
NSRect::new(NSPoint::new(rect.left, rect.top), NSSize::new(rect.width, rect.height))
}
}

impl From<CGRect> for Rect {
fn from(rect: CGRect) -> Rect {
impl From<NSRect> for Rect {
fn from(rect: NSRect) -> Rect {
Rect {
top: rect.origin.y as f64,
left: rect.origin.x as f64,
width: rect.size.width as f64,
height: rect.size.height as f64
width: rect.size.width() as f64,
height: rect.size.height() as f64
}
}
}
59 changes: 27 additions & 32 deletions src/image/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use objc::{class, msg_send, msg_send_id, sel};
use block::ConcreteBlock;

use core_graphics::context::{CGContext, CGContextRef};
use core_graphics::{
base::CGFloat,
geometry::{CGPoint, CGRect, CGSize}
};
use objc2_foundation::{CGFloat, NSPoint, NSRect, NSSize};

use super::icons::*;
use crate::foundation::{id, NSData, NSString, NO, YES};
Expand Down Expand Up @@ -56,49 +53,47 @@ fn min_cgfloat(x: CGFloat, y: CGFloat) -> CGFloat {
impl ResizeBehavior {
/// Given a source and target rectangle, configures and returns a new rectangle configured with
/// the resizing properties of this enum.
pub fn apply(&self, source: CGRect, target: CGRect) -> CGRect {
pub fn apply(&self, source: NSRect, target: NSRect) -> NSRect {
// if equal, just return source
if source.origin.x == target.origin.x
&& source.origin.y == target.origin.y
&& source.size.width == target.size.width
&& source.size.height == target.size.height
{
if source == target {
return source;
}

if source.origin.x == 0. && source.origin.y == 0. && source.size.width == 0. && source.size.height == 0. {
if source == NSRect::ZERO {
return source;
}

let mut scales = CGSize::new(0., 0.);
scales.width = (target.size.width / source.size.width).abs();
scales.height = (target.size.height / source.size.height).abs();
let mut scale_width = (target.size.width() / source.size.width()).abs();
let mut scale_height = (target.size.height() / source.size.height()).abs();

match self {
ResizeBehavior::AspectFit => {
scales.width = min_cgfloat(scales.width, scales.height);
scales.height = scales.width;
scale_width = min_cgfloat(scale_width, scale_height);
scale_height = scale_width;
},

ResizeBehavior::AspectFill => {
scales.width = max_cgfloat(scales.width, scales.height);
scales.height = scales.width;
scale_width = max_cgfloat(scale_width, scale_height);
scale_height = scale_width;
},

ResizeBehavior::Stretch => { /* will do this as default */ },

ResizeBehavior::Center => {
scales.width = 1.;
scales.height = 1.;
scale_width = 1.;
scale_height = 1.;
}
}

let mut result = source;
result.size.width *= scales.width;
result.size.height *= scales.height;
result.origin.x = target.origin.x + (target.size.width - result.size.width) / 2.;
result.origin.y = target.origin.y + (target.size.height - result.size.height) / 2.;
result
let result_size = NSSize::new(source.size.width() * scale_width, source.size.height() * scale_height);

NSRect::new(
NSPoint::new(
target.origin.x + (target.size.width() - result_size.width()) / 2.,
target.origin.y + (target.size.height() - result_size.height()) / 2.
),
result_size
)
}
}

Expand Down Expand Up @@ -221,24 +216,24 @@ impl Image {
/// Draw a custom image and get it back as a returned `Image`.
pub fn draw<F>(config: DrawConfig, handler: F) -> Self
where
F: Fn(CGRect, &CGContextRef) -> bool + 'static
F: Fn(NSRect, &CGContextRef) -> bool + 'static
{
let source_frame = CGRect::new(&CGPoint::new(0., 0.), &CGSize::new(config.source.0, config.source.1));
let source_frame = NSRect::new(NSPoint::new(0., 0.), NSSize::new(config.source.0, config.source.1));

let target_frame = CGRect::new(&CGPoint::new(0., 0.), &CGSize::new(config.target.0, config.target.1));
let target_frame = NSRect::new(NSPoint::new(0., 0.), NSSize::new(config.target.0, config.target.1));

let resized_frame = config.resize.apply(source_frame, target_frame);

let block = ConcreteBlock::new(move |_destination: CGRect| unsafe {
let block = ConcreteBlock::new(move |_destination: NSRect| unsafe {
let current_context: id = msg_send![class!(NSGraphicsContext), currentContext];
let context_ptr: core_graphics::sys::CGContextRef = msg_send![current_context, CGContext];
let context = CGContext::from_existing_context_ptr(context_ptr);
let _: () = msg_send![class!(NSGraphicsContext), saveGraphicsState];

context.translate(resized_frame.origin.x, resized_frame.origin.y);
context.scale(
resized_frame.size.width / config.source.0,
resized_frame.size.height / config.source.1
resized_frame.size.width() / config.source.0,
resized_frame.size.height() / config.source.1
);

let result = handler(resized_frame, &context);
Expand Down
2 changes: 1 addition & 1 deletion src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! view.layer.set_corner_radius(4.0);
//! ```
use core_graphics::base::CGFloat;
use objc2_foundation::CGFloat;

use objc::{class, msg_send, sel};

Expand Down
2 changes: 1 addition & 1 deletion src/layout/animator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core_graphics::base::CGFloat;
use objc2_foundation::CGFloat;

use objc::rc::{Id, Shared};
use objc::runtime::{Class, Object};
Expand Down
2 changes: 1 addition & 1 deletion src/layout/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! escape hatch, if you need it (we use it for things like width and height, which aren't handled
//! by an axis).
use core_graphics::base::CGFloat;
use objc2_foundation::CGFloat;

use objc::rc::{Id, Shared};
use objc::runtime::Object;
Expand Down
2 changes: 1 addition & 1 deletion src/layout/dimension.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core_graphics::base::CGFloat;
use objc2_foundation::CGFloat;

use objc::rc::{Id, Shared};
use objc::runtime::Object;
Expand Down
8 changes: 3 additions & 5 deletions src/layout/traits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Various traits related to controllers opting in to autolayout routines and support for view
//! heirarchies.
use core_graphics::base::CGFloat;
use core_graphics::geometry::{CGPoint, CGRect, CGSize};

use objc::rc::{Id, Shared};
use objc::runtime::Object;
use objc::{msg_send, sel};
use objc2_foundation::{CGFloat, NSRect};

use crate::foundation::{id, nil, to_bool, NSArray, NSString, NO, YES};
use crate::geometry::Rect;
Expand Down Expand Up @@ -53,8 +51,8 @@ pub trait Layout: ObjcAccess {
/// Note that Cacao, by default, opts into autolayout - you need to call
/// `set_translates_autoresizing_mask_into_constraints` to enable frame-based layout calls (or
/// use an appropriate initializer for a given view type).
fn set_frame<R: Into<CGRect>>(&self, rect: R) {
let frame: CGRect = rect.into();
fn set_frame<R: Into<NSRect>>(&self, rect: R) {
let frame: NSRect = rect.into();

self.with_backing_obj_mut(move |backing_node| unsafe {
let _: () = msg_send![backing_node, setFrame: frame];
Expand Down
6 changes: 3 additions & 3 deletions src/listview/appkit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ extern "C" fn view_for_column<T: ListViewDelegate>(
_table_column: id,
item: NSInteger
) -> id {
/*use core_graphics::geometry::CGRect;
/*use objc2_foundation::NSRect;
unsafe {
//let superview: id = msg_send![table_view, superview];
let frame: CGRect = msg_send![table_view, frame];
let _: () = msg_send![table_column, setWidth:frame.size.width];
let frame: NSRect = msg_send![table_view, frame];
let _: () = msg_send![table_column, setWidth:frame.size.width()];
}*/

let view = load::<T>(this, LISTVIEW_DELEGATE_PTR);
Expand Down
Loading

0 comments on commit ed05094

Please sign in to comment.