Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix objc2 warnings #34

Draft
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,13 @@ impl View {
/// so on. It returns a generic `View<T>`, which the caller can then customize as needed.
pub(crate) fn init<T>(view: id) -> View<T> {
unsafe {
let _: () = msg_send![view, setTranslatesAutoresizingMaskIntoConstraints:NO];
let _: () = msg_send![
view,
setTranslatesAutoresizingMaskIntoConstraints: false,
];

#[cfg(target_os = "macos")]
let _: () = msg_send![view, setWantsLayer:YES];
let _: () = msg_send![view, setWantsLayer: true];
}

View {
Expand Down Expand Up @@ -357,7 +360,7 @@ pub(crate) fn register_view_class() -> &'static Class {

INIT.call_once(|| unsafe {
let superclass = class!(NSView);
let mut decl = ClassDecl::new("RSTView", superclass).unwrap();
let mut decl = ClassBuilder::new("RSTView", superclass).unwrap();

decl.add_method(sel!(isFlipped), enforce_normalcy as extern "C" fn(_, _) -> _);

Expand Down
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ rustdoc-args = ["--cfg", "docsrs"]
bitmask-enum = "2.2.1"
objc = { version = "=0.3.0-beta.2", package = "objc2" }
block = { version = "=0.2.0-alpha.6", package = "block2" }
# Temporary: Patched versions that implement `Encode` for common types
# Branch: `objc2`
core-foundation = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "7d593d016175755e492a92ef89edca68ac3bd5cd" }
core-graphics = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "7d593d016175755e492a92ef89edca68ac3bd5cd" }
dispatch = "0.2.0"
infer = { version = "0.15", optional = true }
lazy_static = "1.4.0"
Expand All @@ -36,9 +32,11 @@ uuid = { version = "1.1", features = ["v4"], optional = true }

[dev-dependencies]
eval = "0.4"
core-graphics = "0.23"
foreign-types = "0.5"

[features]
appkit = ["core-foundation/mac_os_10_8_features"]
appkit = []
uikit = []
autolayout = []
default = ["appkit", "autolayout"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ The following are a list of [Cargo features][cargo-features] that can be enabled
[cargo-features]: https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section

## General Notes
**Why not extend the existing cocoa-rs crate?**
A good question. At the end of the day, that crate (I believe, and someone can correct me if I'm wrong) is somewhat tied to Servo, and I wanted to experiment with what the best approach for representing the Cocoa UI model in Rust was. This crate doesn't ignore their work entirely, either - `core_foundation` and `core_graphics` are used internally and re-exported for general use.
**Why not extend the existing cocoa-rs crate?**
A good question. At the end of the day, that crate (I believe, and someone can correct me if I'm wrong) is somewhat tied to Servo, and I wanted to experiment with what the best approach for representing the Cocoa UI model in Rust was.

**Why should I write in Rust, rather than X language?**
In _my_ case, I want to be able to write native applications for my devices (and the platform I like to build products for) without being locked in to writing in Apple-specific languages... and without writing in C/C++ or JavaScript (note: the _toolchain_, not the language - ES6/Typescript are fine). I want to do this because I'm tired of hitting a mountain of work when I want to port my applications to other ecosystems. I think that Rust offers a (growing, but significant) viable model for sharing code across platforms and ecosystems without sacrificing performance.
Expand Down
10 changes: 9 additions & 1 deletion examples/custom_image_drawing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! This example showcases how to do custom drawing on an ImageView
//! with CoreGraphics. Feel free to modify it and play around!

use core_graphics::context::CGContextRef;
use foreign_types::ForeignTypeRef;

use cacao::appkit::menu::{Menu, MenuItem};
use cacao::appkit::window::Window;
use cacao::appkit::{App, AppDelegate};
Expand Down Expand Up @@ -30,7 +33,12 @@ impl Default for BasicApp {
window: Window::default(),
content_view: View::new(),
image_view: ImageView::new(),
image: Image::draw(config, |_cg_rect, context| {
image: Image::draw(config, |resized_frame, source, context| {
let context = unsafe { CGContextRef::from_ptr(context.cast()) };

context.translate(resized_frame.top, resized_frame.left);
context.scale(resized_frame.width / source.0, resized_frame.height / source.1);

context.move_to_point(11.25, 8.19);
context.add_line_to_point(11.25, 5.);
context.add_line_to_point(6.56, 5.);
Expand Down
2 changes: 1 addition & 1 deletion src/appkit/app/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::user_activity::UserActivity;
/// standard `utils` version as this doesn't require `RefCell` backing.
fn app<T>(this: &Object) -> &T {
unsafe {
let app_ptr: usize = *this.get_ivar(APP_PTR);
let app_ptr: usize = *this.ivar(APP_PTR);
let app = app_ptr as *const T;
&*app
}
Expand Down
1 change: 1 addition & 0 deletions src/appkit/app/enums.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Various types used at the AppController level.
#![allow(unused_parens)]

use crate::foundation::NSUInteger;

Expand Down
11 changes: 4 additions & 7 deletions src/appkit/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};

use crate::appkit::menu::Menu;
use crate::foundation::{id, nil, AutoReleasePool, NSUInteger, NO, YES};
use crate::foundation::{id, nil, AutoReleasePool, NSUInteger};
use crate::invoker::TargetActionHandler;
use crate::notification_center::Dispatcher;
use crate::utils::activate_cocoa_multithreading;
Expand Down Expand Up @@ -195,7 +195,7 @@ where
queue.exec_async(move || unsafe {
let app: id = msg_send![register_app_class(), sharedApplication];
let app_delegate: id = msg_send![app, delegate];
let delegate_ptr: usize = *(*app_delegate).get_ivar(APP_PTR);
let delegate_ptr: usize = *(*app_delegate).ivar(APP_PTR);
let delegate = delegate_ptr as *const T;
(&*delegate).on_ui_message(message);
});
Expand All @@ -209,7 +209,7 @@ where
queue.exec_async(move || unsafe {
let app: id = msg_send![register_app_class(), sharedApplication];
let app_delegate: id = msg_send![app, delegate];
let delegate_ptr: usize = *(*app_delegate).get_ivar(APP_PTR);
let delegate_ptr: usize = *(*app_delegate).ivar(APP_PTR);
let delegate = delegate_ptr as *const T;
(&*delegate).on_background_message(message);
});
Expand Down Expand Up @@ -246,10 +246,7 @@ impl App {
/// from your trait implementation of `should_terminate()`.
pub fn reply_to_termination_request(should_terminate: bool) {
shared_application(|app| unsafe {
let _: () = msg_send![app, replyToApplicationShouldTerminate:match should_terminate {
true => YES,
false => NO
}];
let _: () = msg_send![app, replyToApplicationShouldTerminate: should_terminate];
});
}

Expand Down
7 changes: 2 additions & 5 deletions src/appkit/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use objc::{class, msg_send, sel};

use crate::foundation::{id, NO, YES};
use crate::foundation::id;

/// Represents a type of cursor that you can associate with mouse movement.
/// @TODO: Loading?
Expand Down Expand Up @@ -163,10 +163,7 @@ impl Cursor {
/// Trying to invert this with `unhide` will result in undefined system behavior.
pub fn set_hidden_until_mouse_moves(status: bool) {
unsafe {
let _: () = msg_send![class!(NSCursor), setHiddenUntilMouseMoves:match status {
true => YES,
false => NO
}];
let _: () = msg_send![class!(NSCursor), setHiddenUntilMouseMoves: status];
}
}
}
3 changes: 2 additions & 1 deletion src/appkit/event/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use bitmask_enum::bitmask;
use block::ConcreteBlock;

use objc::foundation::NSPoint;
use objc::rc::{Id, Owned};
use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};

use crate::events::EventType;
use crate::foundation::{id, nil, NSInteger, NSPoint, NSString};
use crate::foundation::{id, nil, NSInteger, NSString};

/// An EventMask describes the type of event.
#[bitmask(u64)]
Expand Down
2 changes: 1 addition & 1 deletion src/appkit/menu/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl MenuItem {
/// need to do some extra logic to ensure release calls are properly sent.
extern "C" fn dealloc_cacao_menuitem(this: &Object, _: Sel) {
unsafe {
let ptr: usize = *this.get_ivar(BLOCK_PTR);
let ptr: usize = *this.ivar(BLOCK_PTR);
let obj = ptr as *mut Action;

if !obj.is_null() {
Expand Down
24 changes: 9 additions & 15 deletions src/appkit/segmentedcontrol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use std::sync::Once;
use std::cell::RefCell;
use std::rc::Rc;

use objc::declare::ClassDecl;
use objc::declare::ClassBuilder;
use objc::rc::{Id, Shared};
use objc::runtime::{Class, Object, Sel};
use objc::runtime::{Bool, Class, Object, Sel};
use objc::{class, msg_send, msg_send_id, sel};

use crate::color::Color;
use crate::control::Control;
use crate::foundation::{id, nil, NSArray, NSString, NSUInteger, BOOL, NO, YES};
use crate::foundation::{id, nil, NSArray, NSString, NSUInteger};
use crate::image::Image;
use crate::invoker::TargetActionHandler;
use crate::keys::Key;
Expand Down Expand Up @@ -120,10 +120,10 @@ impl SegmentedControl {
action:nil
];

let _: () = msg_send![control, setWantsLayer: YES];
let _: () = msg_send![control, setWantsLayer: true];

#[cfg(feature = "autolayout")]
let _: () = msg_send![control, setTranslatesAutoresizingMaskIntoConstraints: NO];
let _: () = msg_send![control, setTranslatesAutoresizingMaskIntoConstraints: false];

control
};
Expand Down Expand Up @@ -239,7 +239,7 @@ impl SegmentedControl {
#[cfg(feature = "appkit")]
self.objc.with_mut(move |obj| unsafe {
let text: id = msg_send![obj, attributedTitle];
let len: isize = msg_send![text, length];
let len: usize = msg_send![text, length];

let mut attr_str = AttributedString::wrap(text);
attr_str.set_text_color(color.as_ref(), 0..len);
Expand All @@ -253,10 +253,7 @@ impl SegmentedControl {
#[cfg(feature = "appkit")]
pub fn set_bordered(&self, is_bordered: bool) {
self.objc.with_mut(|obj| unsafe {
let _: () = msg_send![obj, setBordered:match is_bordered {
true => YES,
false => NO
}];
let _: () = msg_send![obj, setBordered: is_bordered];
});
}

Expand Down Expand Up @@ -284,10 +281,7 @@ impl SegmentedControl {
/// Toggles the highlighted status of the button.
pub fn set_highlighted(&self, highlight: bool) {
self.objc.with_mut(|obj| unsafe {
let _: () = msg_send![obj, highlight:match highlight {
true => YES,
false => NO
}];
let _: () = msg_send![obj, highlight: highlight];
});
}
}
Expand Down Expand Up @@ -338,7 +332,7 @@ fn register_class() -> *const Class {

INIT.call_once(|| unsafe {
let superclass = class!(NSSegmentedControl);
let decl = ClassDecl::new("RSTSegmentedControl", superclass).unwrap();
let decl = ClassBuilder::new("RSTSegmentedControl", superclass).unwrap();
VIEW_CLASS = decl.register();
});

Expand Down
1 change: 0 additions & 1 deletion src/appkit/toolbar/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::sync::Once;

use objc::declare::ClassDecl;
use objc::rc::Id;
use objc::runtime::{Bool, Class, Object, Sel};
use objc::{class, msg_send, sel};
Expand Down
13 changes: 5 additions & 8 deletions src/appkit/toolbar/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
//!
//! UNFORTUNATELY, this is a very old and janky API. So... yeah.

use core_graphics::geometry::CGSize;
use std::fmt;

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

use crate::appkit::segmentedcontrol::SegmentedControl;
use crate::button::{BezelStyle, Button};
use crate::foundation::{id, NSString, NO, YES};
use crate::foundation::{id, NSString};
use crate::image::Image;
use crate::invoker::TargetActionHandler;

Expand Down Expand Up @@ -89,15 +89,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 All @@ -110,10 +110,7 @@ impl ToolbarItem {

pub fn set_bordered(&self, bordered: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setBordered:match bordered {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setBordered: bordered];
}
}
}
12 changes: 3 additions & 9 deletions src/appkit/toolbar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use objc::rc::{Id, Owned, Shared};
use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};

use crate::foundation::{id, nil, NSString, NSUInteger, NO, YES};
use crate::foundation::{id, nil, NSString, NSUInteger};

mod class;
use class::register_toolbar_class;
Expand Down Expand Up @@ -88,10 +88,7 @@ impl<T> Toolbar<T> {
/// contents.
pub fn set_shows_baseline_separator(&self, shows: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setShowsBaselineSeparator:match shows {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setShowsBaselineSeparator: shows];
}
}

Expand All @@ -116,10 +113,7 @@ impl<T> Toolbar<T> {
/// Set whether the toolbar is visible or not.
pub fn set_visible(&self, visibility: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setVisible:match visibility {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setVisible: visibility];
}
}

Expand Down
24 changes: 8 additions & 16 deletions src/appkit/window/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@

use std::sync::Once;

use core_graphics::base::CGFloat;

use objc::declare::ClassDecl;
use objc::foundation::{CGFloat, NSSize};
use objc::runtime::{Bool, Class, Object, Sel};
use objc::{class, sel};

use crate::appkit::window::{WindowDelegate, WINDOW_DELEGATE_PTR};
use crate::foundation::{id, load_or_register_class, NSUInteger};
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 @@ -53,14 +51,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 @@ -112,15 +107,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
Loading
Loading