|
| 1 | +use core::ptr::NonNull; |
| 2 | + |
| 3 | +use objc2::msg_send; |
| 4 | +use objc2::rc::{DefaultId, Id, Shared}; |
| 5 | +use objc2::runtime::Object; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + NSCopying, NSDictionary, NSMutableAttributedString, NSMutableCopying, NSObject, NSString, |
| 9 | +}; |
| 10 | + |
| 11 | +object! { |
| 12 | + /// A string that has associated attributes for portions of its text. |
| 13 | + /// |
| 14 | + /// Examples of attributes could be: Visual style, hyperlinks, or |
| 15 | + /// accessibility data. |
| 16 | + /// |
| 17 | + /// Conceptually, each UTF-16 code unit in an attributed string has its |
| 18 | + /// own collection of attributes - most often though |
| 19 | + /// |
| 20 | + /// Only the most basic functionality is defined here, the `AppKit` |
| 21 | + /// framework contains most of the extension methods. |
| 22 | + /// |
| 23 | + /// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsattributedstring?language=objc). |
| 24 | + unsafe pub struct NSAttributedString: NSObject; |
| 25 | +} |
| 26 | + |
| 27 | +// TODO: SAFETY |
| 28 | +unsafe impl Sync for NSAttributedString {} |
| 29 | +unsafe impl Send for NSAttributedString {} |
| 30 | + |
| 31 | +/// Attributes that you can apply to text in an attributed string. |
| 32 | +pub type NSAttributedStringKey = NSString; |
| 33 | + |
| 34 | +/// Creating attributed strings. |
| 35 | +impl NSAttributedString { |
| 36 | + unsafe_def_fn! { |
| 37 | + /// Construct an empty attributed string. |
| 38 | + pub fn new -> Shared; |
| 39 | + } |
| 40 | + |
| 41 | + /// Creates a new attributed string from the given string and attributes. |
| 42 | + /// |
| 43 | + /// The attributes are associated with every UTF-16 code unit in the |
| 44 | + /// string. |
| 45 | + #[doc(alias = "initWithString:")] |
| 46 | + pub fn new_with_attributes( |
| 47 | + string: &NSString, |
| 48 | + // TODO: Mutability of the dictionary should be (Shared, Shared) |
| 49 | + attributes: &NSDictionary<NSAttributedStringKey, Object>, |
| 50 | + ) -> Id<Self, Shared> { |
| 51 | + unsafe { |
| 52 | + let obj: *mut Self = msg_send![Self::class(), alloc]; |
| 53 | + let obj: *mut Self = msg_send![obj, initWithString: string, attributes: attributes]; |
| 54 | + Id::new(NonNull::new_unchecked(obj)) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// Creates a new attributed string without any attributes. |
| 59 | + #[doc(alias = "initWithString:")] |
| 60 | + pub fn from_nsstring(string: &NSString) -> Id<Self, Shared> { |
| 61 | + unsafe { |
| 62 | + let obj: *mut Self = msg_send![Self::class(), alloc]; |
| 63 | + let obj: *mut Self = msg_send![obj, initWithString: string]; |
| 64 | + Id::new(NonNull::new_unchecked(obj)) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Querying. |
| 70 | +impl NSAttributedString { |
| 71 | + // TODO: Lifetimes? |
| 72 | + pub fn string(&self) -> &NSString { |
| 73 | + unsafe { msg_send![self, string] } |
| 74 | + } |
| 75 | + |
| 76 | + /// Alias for `self.string().len_utf16()`. |
| 77 | + #[doc(alias = "length")] |
| 78 | + #[allow(unused)] |
| 79 | + // TODO: Finish this |
| 80 | + fn len_utf16(&self) -> usize { |
| 81 | + unsafe { msg_send![self, length] } |
| 82 | + } |
| 83 | + |
| 84 | + // /// TODO |
| 85 | + // /// |
| 86 | + // /// See [Apple's documentation on Effective and Maximal Ranges][doc]. |
| 87 | + // /// |
| 88 | + // /// [doc]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/AccessingAttrs.html#//apple_ref/doc/uid/20000161-SW2 |
| 89 | + // #[doc(alias = "attributesAtIndex:effectiveRange:")] |
| 90 | + // pub fn attributes_in_effective_range( |
| 91 | + // &self, |
| 92 | + // index: usize, |
| 93 | + // range: Range<usize>, |
| 94 | + // ) -> Id<Self, Shared> { |
| 95 | + // let range = NSRange::from(range); |
| 96 | + // todo!() |
| 97 | + // } |
| 98 | + // |
| 99 | + // attributesAtIndex:longestEffectiveRange:inRange: |
| 100 | + |
| 101 | + // TODO: attributedSubstringFromRange: |
| 102 | + // TODO: enumerateAttributesInRange:options:usingBlock: |
| 103 | +} |
| 104 | + |
| 105 | +impl DefaultId for NSAttributedString { |
| 106 | + type Ownership = Shared; |
| 107 | + |
| 108 | + #[inline] |
| 109 | + fn default_id() -> Id<Self, Self::Ownership> { |
| 110 | + Self::new() |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +unsafe impl NSCopying for NSAttributedString { |
| 115 | + type Ownership = Shared; |
| 116 | + type Output = NSAttributedString; |
| 117 | +} |
| 118 | + |
| 119 | +unsafe impl NSMutableCopying for NSAttributedString { |
| 120 | + type Output = NSMutableAttributedString; |
| 121 | +} |
| 122 | + |
| 123 | +#[cfg(test)] |
| 124 | +mod tests { |
| 125 | + use alloc::string::ToString; |
| 126 | + use objc2::rc::autoreleasepool; |
| 127 | + |
| 128 | + use super::*; |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_new() { |
| 132 | + let s = NSAttributedString::new(); |
| 133 | + assert_eq!(&s.string().to_string(), ""); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_string_bound_to_attributed() { |
| 138 | + let attr_s = { |
| 139 | + let source = NSString::from_str("Hello world!"); |
| 140 | + NSAttributedString::from_nsstring(&source) |
| 141 | + }; |
| 142 | + let s = autoreleasepool(|_| attr_s.string()); |
| 143 | + assert_eq!(s.len(), 12); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn test_from_nsstring() { |
| 148 | + let s = NSAttributedString::from_nsstring(&NSString::from_str("abc")); |
| 149 | + assert_eq!(&s.string().to_string(), "abc"); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_copy() { |
| 154 | + let s1 = NSAttributedString::from_nsstring(&NSString::from_str("abc")); |
| 155 | + let s2 = s1.copy(); |
| 156 | + // NSAttributedString performs this optimization in GNUStep's runtime, |
| 157 | + // but not in Apple's; so we don't test for it! |
| 158 | + // assert_eq!(s1.as_ptr(), s2.as_ptr()); |
| 159 | + assert!(s2.is_kind_of(NSAttributedString::class())); |
| 160 | + |
| 161 | + let s3 = s1.mutable_copy(); |
| 162 | + assert_ne!(s1.as_ptr(), s3.as_ptr() as *mut NSAttributedString); |
| 163 | + assert!(s3.is_kind_of(NSMutableAttributedString::class())); |
| 164 | + } |
| 165 | +} |
0 commit comments