From 6f8abe82f5610bc1a2748c52a416e5a89c8ff7ed 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 | 12 ++++++------ src/utils/mod.rs | 22 ---------------------- 6 files changed, 9 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 703c2454..2a2a9268 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,9 +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" } dispatch = "0.2.0" infer = { version = "0.15", optional = true } lazy_static = "1.4.0" @@ -39,7 +36,7 @@ core-graphics = "0.23" foreign-types = "0.5" [features] -appkit = ["core-foundation/mac_os_10_8_features"] +appkit = [] uikit = [] autolayout = [] default = ["appkit", "autolayout"] diff --git a/README.md b/README.md index 5ab3cfe3..e0cc46f9 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,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 5fd80cb0..1c3bd434 100644 --- a/src/button/mod.rs +++ b/src/button/mod.rs @@ -257,7 +257,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 dec6c69d..23843828 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,7 +92,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 e5b63666..54199592 100644 --- a/src/text/attributed_string.rs +++ b/src/text/attributed_string.rs @@ -3,13 +3,13 @@ use std::ops::{Deref, DerefMut, Range}; use std::os::raw::c_char; use std::{fmt, slice, str}; +use objc::foundation::NSRange; use objc::rc::{Id, Owned}; use objc::runtime::Object; use objc::{class, msg_send, msg_send_id, sel}; use crate::color::Color; -use crate::foundation::{id, to_bool, NSString, BOOL, NO, YES}; -use crate::utils::CFRange; +use crate::foundation::{id, NSString}; 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 38a1f6d5..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 = 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,