From 02d879497915a9d58e7b3a4bea92461e2f2b3836 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 11 Jul 2022 02:00:25 +0200 Subject: [PATCH] Remove dependency on core-foundation --- Cargo.toml | 5 +---- README.md | 2 +- src/button/mod.rs | 2 +- src/lib.rs | 1 - src/text/attributed_string.rs | 10 +++++----- src/utils/mod.rs | 22 ---------------------- 6 files changed, 8 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7553bc5e..6e276b2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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"] diff --git a/README.md b/README.md index 261c5d95..1a3746b9 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/button/mod.rs b/src/button/mod.rs index f6198b71..241e99ba 100644 --- a/src/button/mod.rs +++ b/src/button/mod.rs @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 12eb003d..2a8ac0b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/text/attributed_string.rs b/src/text/attributed_string.rs index a3624d56..cd84311f 100644 --- a/src/text/attributed_string.rs +++ b/src/text/attributed_string.rs @@ -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; @@ -43,9 +43,9 @@ impl AttributedString { } /// Sets the text (foreground) color for the specified range. - pub fn set_text_color>(&mut self, color: C, range: Range) { + pub fn set_text_color>(&mut self, color: C, range: Range) { let color: id = color.as_ref().into(); - let range = CFRange::init(range.start, range.end); + let range = NSRange::from(range); unsafe { let _: () = msg_send![ @@ -58,8 +58,8 @@ impl AttributedString { } /// Set the font for the specified range. - pub fn set_font(&mut self, font: Font, range: Range) { - let range = CFRange::init(range.start, range.end); + pub fn set_font(&mut self, font: Font, range: Range) { + let range = NSRange::from(range); unsafe { let _: () = msg_send![ diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8e68555d..187b5362 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -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}; @@ -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,