cidre 0.13.1

Apple frameworks bindings for rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use crate::{arc, cf, define_obj_type, define_opts, ns, objc};

define_obj_type!(
    #[doc(alias = "NSAttributedStringKey")]
    pub AttrStringKey(ns::String)
);

#[cfg(any(feature = "app", feature = "ui"))]
impl AttrStringKey {
    #[doc(alias = "NSFontAttributeName")]
    #[inline]
    pub fn font_name() -> &'static Self {
        unsafe { NSFontAttributeName }
    }

    #[doc(alias = "NSParagraphStyleAttributeName")]
    #[inline]
    pub fn paragraph_style() -> &'static Self {
        unsafe { NSParagraphStyleAttributeName }
    }

    #[doc(alias = "NSForegroundColorAttributeName")]
    #[inline]
    pub fn foreground_color() -> &'static Self {
        unsafe { NSForegroundColorAttributeName }
    }

    #[doc(alias = "NSBackgroundColorAttributeName")]
    #[inline]
    pub fn background_color() -> &'static Self {
        unsafe { NSBackgroundColorAttributeName }
    }

    #[doc(alias = "NSLigatureAttributeName")]
    #[inline]
    pub fn ligature() -> &'static Self {
        unsafe { NSLigatureAttributeName }
    }

    #[doc(alias = "NSKernAttributeName")]
    #[inline]
    pub fn kern() -> &'static Self {
        unsafe { NSKernAttributeName }
    }

    #[doc(alias = "NSTrackingAttributeName")]
    #[inline]
    pub fn tracking() -> &'static Self {
        unsafe { NSTrackingAttributeName }
    }

    #[doc(alias = "NSStrikethroughStyleAttributeName")]
    #[inline]
    pub fn strikethrough_style() -> &'static Self {
        unsafe { NSStrikethroughStyleAttributeName }
    }

    #[doc(alias = "NSUnderlineStyleAttributeName")]
    #[inline]
    pub fn underline_style() -> &'static Self {
        unsafe { NSUnderlineStyleAttributeName }
    }

    #[doc(alias = "NSStrokeColorAttributeName")]
    #[inline]
    pub fn stroke_color() -> &'static Self {
        unsafe { NSStrokeColorAttributeName }
    }

    #[doc(alias = "NSStrokeWidthAttributeName")]
    #[inline]
    pub fn stroke_width() -> &'static Self {
        unsafe { NSStrokeWidthAttributeName }
    }

    #[doc(alias = "NSShadowAttributeName")]
    #[inline]
    pub fn shadow() -> &'static Self {
        unsafe { NSShadowAttributeName }
    }

    #[doc(alias = "NSTextEffectAttributeName")]
    #[inline]
    pub fn text_effect() -> &'static Self {
        unsafe { NSTextEffectAttributeName }
    }

    #[doc(alias = "NSAttachmentAttributeName")]
    #[inline]
    pub fn attachment() -> &'static Self {
        unsafe { NSAttachmentAttributeName }
    }

    #[doc(alias = "NSLinkAttributeName")]
    #[inline]
    pub fn link() -> &'static Self {
        unsafe { NSLinkAttributeName }
    }

    #[doc(alias = "NSBaselineOffsetAttributeName")]
    #[inline]
    pub fn baseline_offset() -> &'static Self {
        unsafe { NSBaselineOffsetAttributeName }
    }

    #[doc(alias = "NSUnderlineColorAttributeName")]
    #[inline]
    pub fn underline_color() -> &'static Self {
        unsafe { NSUnderlineColorAttributeName }
    }

    #[doc(alias = "NSStrikethroughColorAttributeName")]
    #[inline]
    pub fn strikethrough_color() -> &'static Self {
        unsafe { NSStrikethroughColorAttributeName }
    }

    #[doc(alias = "NSWritingDirectionAttributeName")]
    #[inline]
    pub fn writing_direction() -> &'static Self {
        unsafe { NSWritingDirectionAttributeName }
    }
}

#[cfg(all(feature = "app", target_os = "macos"))]
#[link(name = "AppKit", kind = "framework")]
unsafe extern "C" {
    static NSFontAttributeName: &'static AttrStringKey;
    static NSParagraphStyleAttributeName: &'static AttrStringKey;
    static NSForegroundColorAttributeName: &'static AttrStringKey;
    static NSBackgroundColorAttributeName: &'static AttrStringKey;
    static NSLigatureAttributeName: &'static AttrStringKey;
    static NSKernAttributeName: &'static AttrStringKey;
    static NSTrackingAttributeName: &'static AttrStringKey;
    static NSStrikethroughStyleAttributeName: &'static AttrStringKey;
    static NSUnderlineStyleAttributeName: &'static AttrStringKey;
    static NSStrokeColorAttributeName: &'static AttrStringKey;
    static NSStrokeWidthAttributeName: &'static AttrStringKey;
    static NSShadowAttributeName: &'static AttrStringKey;
    static NSTextEffectAttributeName: &'static AttrStringKey;
    static NSAttachmentAttributeName: &'static AttrStringKey;
    static NSLinkAttributeName: &'static AttrStringKey;
    static NSBaselineOffsetAttributeName: &'static AttrStringKey;
    static NSUnderlineColorAttributeName: &'static AttrStringKey;
    static NSStrikethroughColorAttributeName: &'static AttrStringKey;
    static NSWritingDirectionAttributeName: &'static AttrStringKey;
}

#[cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos"))]
#[link(name = "UIKit", kind = "framework")]
unsafe extern "C" {
    static NSFontAttributeName: &'static AttrStringKey;
    static NSParagraphStyleAttributeName: &'static AttrStringKey;
    static NSForegroundColorAttributeName: &'static AttrStringKey;
    static NSBackgroundColorAttributeName: &'static AttrStringKey;
    static NSLigatureAttributeName: &'static AttrStringKey;
    static NSKernAttributeName: &'static AttrStringKey;
    static NSTrackingAttributeName: &'static AttrStringKey;
    static NSStrikethroughStyleAttributeName: &'static AttrStringKey;
    static NSUnderlineStyleAttributeName: &'static AttrStringKey;
    static NSStrokeColorAttributeName: &'static AttrStringKey;
    static NSStrokeWidthAttributeName: &'static AttrStringKey;
    static NSShadowAttributeName: &'static AttrStringKey;
    static NSTextEffectAttributeName: &'static AttrStringKey;
    static NSAttachmentAttributeName: &'static AttrStringKey;
    static NSLinkAttributeName: &'static AttrStringKey;
    static NSBaselineOffsetAttributeName: &'static AttrStringKey;
    static NSUnderlineColorAttributeName: &'static AttrStringKey;
    static NSStrikethroughColorAttributeName: &'static AttrStringKey;
    static NSWritingDirectionAttributeName: &'static AttrStringKey;
}

define_obj_type!(
    #[doc(alias = "NSAttributedStringFormattingContextKey")]
    pub AttrStringFormatCtxKey(ns::String)
);

define_obj_type!(
    #[doc(alias = "NSAttributedString")]
    pub AttrString(ns::Id),
    NS_ATTRIBUTED_STRING
);

impl arc::A<AttrString> {
    #[objc::msg_send(initWithString:)]
    pub fn init_with_string(self, str: &ns::String) -> arc::R<AttrString>;

    #[objc::msg_send(initWithAttributedString:)]
    pub fn init_with_attr_string(self, str: &ns::AttrString) -> arc::R<AttrString>;
}

impl AttrString {
    /// For performance reasons, this property returns the current backing store of
    /// the attributed string object. If you want to maintain a snapshot of this as
    /// you manipulate the returned string, you should make a copy of the appropriate substring.
    #[objc::msg_send(string)]
    pub fn string(&self) -> arc::R<ns::String>;

    #[objc::msg_send(length)]
    pub fn len(&self) -> usize;

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[objc::msg_send(attributesAtIndex:effectiveRange:)]
    pub unsafe fn attrs_at_throws(
        &self,
        index: usize,
        effective_range: Option<&ns::Range>,
    ) -> arc::R<ns::Dictionary<AttrStringKey, ns::Id>>;

    pub fn attrs_at<'ear>(
        &self,
        index: usize,
        effective_range: Option<&ns::Range>,
    ) -> ns::ExResult<'ear, arc::R<ns::Dictionary<AttrStringKey, ns::Id>>> {
        ns::try_catch(|| unsafe { self.attrs_at_throws(index, effective_range) })
    }

    #[inline]
    pub fn with_str(str: &str) -> arc::R<Self> {
        let str = ns::String::with_str(str);
        Self::alloc().init_with_string(&str)
    }

    #[inline]
    pub fn with_string(str: &ns::String) -> arc::R<Self> {
        Self::alloc().init_with_string(str)
    }

    #[inline]
    pub fn with_attr_string(str: &ns::AttrString) -> arc::R<Self> {
        Self::alloc().init_with_attr_string(str)
    }

    #[inline]
    pub fn as_cf(&self) -> &cf::AttrString {
        unsafe { std::mem::transmute(self) }
    }

    #[objc::msg_send(copy)]
    pub fn copy(&self) -> arc::Retained<AttrString>;

    #[objc::msg_send(mutableCopy)]
    pub fn copy_mut(&self) -> arc::Retained<AttrStringMut>;
}

define_obj_type!(
    #[doc(alias = "NSMutableAttributedString")]
    pub AttrStringMut(AttrString),
    NS_MUTABLE_ATTRIBUTED_STRING
);

impl arc::A<AttrStringMut> {
    #[objc::msg_send(initWithString:)]
    pub fn init_with_string(self, str: &ns::String) -> arc::R<AttrStringMut>;

    #[objc::msg_send(initWithAttributedString:)]
    pub fn init_with_attr_string(self, str: &ns::AttrString) -> arc::R<AttrStringMut>;
}

impl AttrStringMut {
    #[inline]
    pub fn with_str(str: &str) -> arc::R<Self> {
        let str = ns::String::with_str(str);
        Self::alloc().init_with_string(&str)
    }

    #[inline]
    pub fn with_string(str: &ns::String) -> arc::R<Self> {
        Self::alloc().init_with_string(str)
    }

    #[inline]
    pub fn with_attr_string(str: &ns::AttrString) -> arc::R<Self> {
        Self::alloc().init_with_attr_string(str)
    }

    #[objc::msg_send(mutableString)]
    pub fn string_mut(&mut self) -> &mut ns::StringMut;

    #[objc::msg_send(addAttribute:value:range:)]
    pub unsafe fn add_attr_throws(
        &mut self,
        name: &ns::AttrStringKey,
        val: &ns::Id,
        range: ns::Range,
    );

    pub fn add_attr<'ear>(
        &mut self,
        name: &ns::AttrStringKey,
        val: &ns::Id,
        range: ns::Range,
    ) -> ns::ExResult<'ear> {
        ns::try_catch(|| unsafe { self.add_attr_throws(name, val, range) })
    }

    #[objc::msg_send(addAttributes:range:)]
    pub unsafe fn add_attrs_throws(
        &mut self,
        attrs: &ns::Dictionary<ns::AttrStringKey, ns::Id>,
        range: ns::Range,
    );

    pub fn add_attrs<'ear>(
        &mut self,
        attrs: &ns::Dictionary<ns::AttrStringKey, ns::Id>,
        range: ns::Range,
    ) -> ns::ExResult<'ear> {
        ns::try_catch(|| unsafe { self.add_attrs_throws(attrs, range) })
    }

    #[objc::msg_send(removeAttribute:range:)]
    pub unsafe fn remove_attr_throws(&mut self, name: &ns::AttrStringKey, range: ns::Range);

    pub fn remove_attr<'ear>(
        &mut self,
        name: &ns::AttrStringKey,
        range: ns::Range,
    ) -> ns::ExResult<'ear> {
        ns::try_catch(|| unsafe { self.remove_attr_throws(name, range) })
    }

    #[objc::msg_send(insertAttributedString:atIndex:)]
    pub fn insert_attr_string(&mut self, val: &ns::AttrString, at: usize);

    #[objc::msg_send(appendAttributedString:)]
    pub fn append_attr_string(&mut self, val: &ns::AttrString);

    #[objc::msg_send(replaceCharactersInRange:withAttributedString:)]
    pub unsafe fn replace_with_attr_string_throws(
        &mut self,
        range: ns::Range,
        attr_str: &ns::AttrString,
    );

    pub fn replace_with_attr_string<'ear>(
        &mut self,
        range: ns::Range,
        attr_str: &ns::AttrString,
    ) -> ns::ExResult<'ear> {
        ns::try_catch(|| unsafe { self.replace_with_attr_string_throws(range, attr_str) })
    }
}

define_opts!(
    #[doc(alias = "NSInlinePresentationIntent")]
    pub InlinePresentationIntent(usize)
);

impl InlinePresentationIntent {
    pub const EMPHASIZED: Self = Self(1 << 0);
    pub const STRONGLY_EMPHASIZED: Self = Self(1 << 1);
    pub const CODE: Self = Self(1 << 2);
    pub const STRIKETHROUGH: Self = Self(1 << 5);
    pub const SOFT_BREAK: Self = Self(1 << 6);
    pub const LINE_BREAK: Self = Self(1 << 7);
    pub const INLINE_HTML: Self = Self(1 << 8);
    pub const BLOCK_HTML: Self = Self(1 << 9);
}

unsafe extern "C" {
    static NS_ATTRIBUTED_STRING: &'static objc::Class<AttrString>;
    static NS_MUTABLE_ATTRIBUTED_STRING: &'static objc::Class<AttrStringMut>;
}

#[cfg(all(test, target_os = "macos"))]
mod tests {
    use crate::{ns, objc::Obj};

    #[test]
    fn basics() {
        let str = ns::String::with_str("Hello");
        let astr = ns::AttrString::with_string(&str);

        assert_eq!(&astr.string(), &str);
        let attrs = astr.attrs_at(0, None).unwrap();
        assert!(attrs.is_empty());

        let copy = astr.copy_mut();
        let copy = copy.copy();
        let copy = ns::AttrString::with_attr_string(&copy);

        assert_eq!(copy.len(), 5);

        let mut mcopy = copy.copy_mut();

        // let mut mstr = mcopy.string_mut();

        let color_key = ns::AttrStringKey::foreground_color();
        let color_val = ns::Color::with_rgba(0.5, 0.5, 1.0, 1.0);

        let attrs = ns::Dictionary::with_keys_values(&[color_key], &[color_val.as_id_ref()]);

        mcopy.add_attrs(&attrs, ns::Range::new(0, 5)).expect("ok");
        mcopy
            .add_attr(color_key, &color_val, ns::Range::new(1000, 10))
            .expect_err("ok");
        mcopy
            .add_attr(color_key, &color_val, ns::Range::new(0, 3))
            .expect("ok");

        mcopy
            .remove_attr(color_key, ns::Range::new(100, 200))
            .expect_err("should be out of bounds");

        mcopy
            .remove_attr(color_key, ns::Range::new(0, 5))
            .expect("failed to remove valid range");

        let mstr = ns::AttrStringMut::with_str("nice");
        assert_eq!(mstr.len(), 4);
        // TODO: investigate
        // astr.attrs_at(1000, Some(&ns::Range::new(1000, 1000)))
        // .expect_err("Should be Exception");
    }
}