|
| 1 | +use core::ptr::NonNull; |
| 2 | + |
| 3 | +use objc2::msg_send; |
| 4 | +use objc2::rc::{DefaultId, Id, Owned, Shared}; |
| 5 | + |
| 6 | +use crate::{NSAttributedString, NSCopying, NSMutableCopying, NSString}; |
| 7 | + |
| 8 | +object! { |
| 9 | + /// A mutable string that has associated attributes. |
| 10 | + /// |
| 11 | + /// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsmutableattributedstring?language=objc). |
| 12 | + unsafe pub struct NSMutableAttributedString: NSAttributedString; |
| 13 | +} |
| 14 | + |
| 15 | +// TODO: SAFETY |
| 16 | +unsafe impl Sync for NSMutableAttributedString {} |
| 17 | +unsafe impl Send for NSMutableAttributedString {} |
| 18 | + |
| 19 | +/// Creating mutable attributed strings. |
| 20 | +impl NSMutableAttributedString { |
| 21 | + unsafe_def_fn! { |
| 22 | + /// Construct an empty mutable attributed string. |
| 23 | + pub fn new -> Owned; |
| 24 | + } |
| 25 | + |
| 26 | + // TODO: new_with_attributes |
| 27 | + |
| 28 | + #[doc(alias = "initWithString:")] |
| 29 | + pub fn from_nsstring(string: &NSString) -> Id<Self, Owned> { |
| 30 | + unsafe { |
| 31 | + let obj: *mut Self = msg_send![Self::class(), alloc]; |
| 32 | + let obj: *mut Self = msg_send![obj, initWithString: string]; |
| 33 | + Id::new(NonNull::new_unchecked(obj)) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + #[doc(alias = "initWithAttributedString:")] |
| 38 | + pub fn from_attributed_nsstring(attributed_string: &NSAttributedString) -> Id<Self, Owned> { |
| 39 | + unsafe { |
| 40 | + let obj: *mut Self = msg_send![Self::class(), alloc]; |
| 41 | + let obj: *mut Self = msg_send![obj, initWithAttributedString: attributed_string]; |
| 42 | + Id::new(NonNull::new_unchecked(obj)) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Modifying the attributed string. |
| 48 | +impl NSMutableAttributedString { |
| 49 | + // TODO |
| 50 | + // - mutableString |
| 51 | + // - replaceCharactersInRange:withString: |
| 52 | + // - setAttributes:range: |
| 53 | + |
| 54 | + /// Replaces the entire attributed string. |
| 55 | + #[doc(alias = "setAttributedString:")] |
| 56 | + pub fn replace(&mut self, attributed_string: &NSAttributedString) { |
| 57 | + unsafe { msg_send![self, setAttributedString: attributed_string] } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl DefaultId for NSMutableAttributedString { |
| 62 | + type Ownership = Owned; |
| 63 | + |
| 64 | + #[inline] |
| 65 | + fn default_id() -> Id<Self, Self::Ownership> { |
| 66 | + Self::new() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +unsafe impl NSCopying for NSMutableAttributedString { |
| 71 | + type Ownership = Shared; |
| 72 | + type Output = NSAttributedString; |
| 73 | +} |
| 74 | + |
| 75 | +unsafe impl NSMutableCopying for NSMutableAttributedString { |
| 76 | + type Output = NSMutableAttributedString; |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use alloc::string::ToString; |
| 82 | + use objc2::rc::autoreleasepool; |
| 83 | + |
| 84 | + use super::*; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_new() { |
| 88 | + let s = NSAttributedString::new(); |
| 89 | + assert_eq!(&s.string().to_string(), ""); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_string_bound_to_attributed() { |
| 94 | + let attr_s = { |
| 95 | + let source = NSString::from_str("Hello world!"); |
| 96 | + NSAttributedString::from_nsstring(&source) |
| 97 | + }; |
| 98 | + let s = autoreleasepool(|_| attr_s.string()); |
| 99 | + assert_eq!(s.len(), 12); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_from_nsstring() { |
| 104 | + let s = NSAttributedString::from_nsstring(&NSString::from_str("abc")); |
| 105 | + assert_eq!(&s.string().to_string(), "abc"); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_copy() { |
| 110 | + let s1 = NSAttributedString::from_nsstring(&NSString::from_str("abc")); |
| 111 | + let s2 = s1.copy(); |
| 112 | + // NSAttributedString also performs this optimization |
| 113 | + assert_eq!(s1.as_ptr(), s2.as_ptr()); |
| 114 | + assert!(s2.is_kind_of(NSAttributedString::class())); |
| 115 | + |
| 116 | + // let s3 = s1.mutable_copy(); |
| 117 | + // assert_ne!(s1.as_ptr(), s3.as_ptr() as *mut NSAttributedString); |
| 118 | + // assert!(s3.is_kind_of(NSMutableAttributedString::class())); |
| 119 | + } |
| 120 | +} |
0 commit comments