Skip to content

Commit

Permalink
Remove dependency on core-foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jul 20, 2022
1 parent 9e48cbd commit b49cbfb
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 34 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ rustdoc-args = ["--cfg", "docsrs"]
objc = { version = "=0.3.0-beta.1", package = "objc2" }
block = { version = "=0.2.0-alpha.5", package = "block2" }
objc2-foundation = "=0.2.0-alpha.6"
# Temporary: Patched versions that implement `Encode` for common types
# Branch: `objc2`
core-foundation = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "c506e7d70da010c0070048e9471ff0b441506e65", features = ["with-chrono"] }
dispatch = "0.2.0"
infer = { version = "0.4", optional = true }
lazy_static = "1.4.0"
Expand All @@ -37,7 +34,7 @@ uuid = { version = "0.8", features = ["v4"], optional = true }
eval = "0.4"

[features]
appkit = ["core-foundation/mac_os_10_8_features"]
appkit = []
uikit = []
autolayout = []
default = ["appkit", "autolayout"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ The following are a list of [Cargo features][cargo-features] that can be enabled

## 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` is used internally and re-exported for general use.
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
2 changes: 1 addition & 1 deletion src/button/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl Button {
#[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 Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
//!
//! [cargo-features]: https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section

pub use core_foundation;
pub use lazy_static;
pub use objc;
pub use url;
Expand Down
10 changes: 5 additions & 5 deletions src/text/attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use std::{fmt, slice, str};
use objc::rc::{Id, Owned};
use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};
use objc2_foundation::NSRange;

use crate::color::Color;
use crate::foundation::{id, to_bool, NSString, BOOL, NO, YES};
use crate::utils::CFRange;

use super::Font;

Expand Down Expand Up @@ -43,9 +43,9 @@ impl AttributedString {
}

/// Sets the text (foreground) color for the specified range.
pub fn set_text_color<C: AsRef<Color>>(&mut self, color: C, range: Range<isize>) {
pub fn set_text_color<C: AsRef<Color>>(&mut self, color: C, range: Range<usize>) {
let color: id = color.as_ref().into();
let range = CFRange::init(range.start, range.end);
let range = NSRange::from(range);

unsafe {
let _: () = msg_send![
Expand All @@ -58,8 +58,8 @@ impl AttributedString {
}

/// Set the font for the specified range.
pub fn set_font(&mut self, font: Font, range: Range<isize>) {
let range = CFRange::init(range.start, range.end);
pub fn set_font(&mut self, font: Font, range: Range<usize>) {
let range = NSRange::from(range);

unsafe {
let _: () = msg_send![
Expand Down
22 changes: 0 additions & 22 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//! belong to. These are typically internal, and if you rely on them... well, don't be surprised if
//! they go away one day.

use core_foundation::base::CFIndex;

use objc::rc::{Id, Shared};
use objc::runtime::Object;
use objc::{class, msg_send, sel};
Expand Down Expand Up @@ -66,26 +64,6 @@ where
queue.exec_sync(method);
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct CFRange {
pub location: CFIndex,
pub length: CFIndex
}

impl CFRange {
pub fn init(location: CFIndex, length: CFIndex) -> CFRange {
CFRange {
location: location,
length: length
}
}
}

unsafe impl Encode for CFRange {
const ENCODING: Encoding<'static> = Encoding::Struct("CFRange", &[CFIndex::ENCODING, CFIndex::ENCODING]);
}

/// A helper method for ensuring that Cocoa is running in multi-threaded mode.
///
/// Why do we need this? According to Apple, if you're going to make use of standard POSIX threads,
Expand Down

0 comments on commit b49cbfb

Please sign in to comment.