|
| 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 | + |
| 83 | + use super::*; |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_new() { |
| 87 | + let s = NSAttributedString::new(); |
| 88 | + assert_eq!(&s.string().to_string(), ""); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn test_copy() { |
| 93 | + let s1 = NSMutableAttributedString::from_nsstring(&NSString::from_str("abc")); |
| 94 | + let s2 = s1.copy(); |
| 95 | + assert_ne!(s1.as_ptr() as *const NSAttributedString, s2.as_ptr()); |
| 96 | + assert!(s2.is_kind_of(NSAttributedString::class())); |
| 97 | + |
| 98 | + let s3 = s1.mutable_copy(); |
| 99 | + assert_ne!(s1.as_ptr(), s3.as_ptr()); |
| 100 | + assert!(s3.is_kind_of(NSMutableAttributedString::class())); |
| 101 | + } |
| 102 | +} |
0 commit comments