ferroui-ios 0.3.0

The iOS platform of FerroUI: a UIKit view, touch input and Metal and OpenGL ES graphics
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
//! The platform settings: the colour scheme, the contrast preference and
//! the tint colour of the system, and the preferred language of the user.
//!
//! The settings ask the system through [`ISystemSettings`], which the
//! tests replace with fixed values; on iOS the answers come from the
//! current trait collection, the preferred tint and the locale.

use ferroui_base::input::platform::PlatformHotkeyConfiguration;
use ferroui_base::input::PointerType;
use ferroui_base::media::Color;
use ferroui_base::platform::{
    ColorContrastPreference, DefaultPlatformSettings, IPlatformSettings, PlatformColorValues, PlatformThemeVariant,
};
use ferroui_base::reactive::IDisposable;
use ferroui_base::Size;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::time::Duration;

/// What the system says of its colours.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SystemColors {
    /// Whether the user interface style of the current traits is dark.
    pub is_dark: bool,
    /// Whether the accessibility contrast of the current traits is high.
    pub is_high_contrast: bool,
    /// The preferred tint colour, as red, green, blue and alpha between 0
    /// and 1, when the system has one.
    pub tint_color: Option<[f64; 4]>,
}

/// What the settings ask the system.
pub trait ISystemSettings {
    /// The colours of the current traits.
    fn colors(&self) -> SystemColors;

    /// The preferred languages of the user, the most preferred first.
    fn preferred_languages(&self) -> Vec<String>;
}

/// The colour values of the framework for the colours of the system.
pub fn color_values(colors: SystemColors) -> PlatformColorValues {
    let theme_variant = if colors.is_dark { PlatformThemeVariant::Dark } else { PlatformThemeVariant::Light };

    let contrast_preference =
        if colors.is_high_contrast { ColorContrastPreference::High } else { ColorContrastPreference::NoPreference };

    let values = PlatformColorValues::new().with_theme_variant(theme_variant).with_contrast_preference(contrast_preference);

    if let Some([red, green, blue, alpha]) = colors.tint_color {
        if red != 0.0 && green != 0.0 && blue != 0.0 && alpha != 0.0 {
            return values.with_accent_color1(Color::from_argb(
                (alpha * 255.0) as u8,
                (red * 255.0) as u8,
                (green * 255.0) as u8,
                (blue * 255.0) as u8,
            ));
        }
    }

    values
}

// TODO: ideally should be created per view/activity.
/// The platform settings of iOS.
pub struct PlatformSettings {
    base: DefaultPlatformSettings,
    color_values: Cell<Option<PlatformColorValues>>,
    last_language: RefCell<Option<String>>,
    system: Box<dyn ISystemSettings>,
}

impl PlatformSettings {
    /// Creates the settings over what answers for the system.
    pub fn with_system(system: Box<dyn ISystemSettings>) -> Rc<Self> {
        Rc::new(Self {
            base: DefaultPlatformSettings::new(),
            color_values: Cell::new(None),
            last_language: RefCell::new(None),
            system,
        })
    }

    fn get_uncached_color_values(&self) -> PlatformColorValues {
        color_values(self.system.colors())
    }

    /// The traits of a view or its tint colour changed.
    pub fn trait_collection_did_change(&self) {
        let old_color_values = self.color_values.get();
        let color_values = self.get_uncached_color_values();

        if old_color_values != Some(color_values) {
            self.color_values.set(Some(color_values));
            self.base.on_color_values_changed(color_values);
        }
    }

    fn query_preferred_application_language(&self) -> String {
        match self.system.preferred_languages().into_iter().next() {
            Some(language) if !language.is_empty() => language,
            _ => self.base.preferred_application_language(),
        }
    }

    /// The current locale of the system changed.
    pub fn on_preferred_language_changed(&self) {
        let old_language = self.last_language.borrow_mut().take();

        if old_language != Some(self.preferred_application_language()) {
            self.base.on_preferred_application_language_changed();
        }
    }
}

impl IPlatformSettings for PlatformSettings {
    fn get_tap_size(&self, type_: PointerType) -> Size {
        self.base.get_tap_size(type_)
    }

    fn get_double_tap_size(&self, type_: PointerType) -> Size {
        self.base.get_double_tap_size(type_)
    }

    fn get_double_tap_time(&self, type_: PointerType) -> Duration {
        self.base.get_double_tap_time(type_)
    }

    fn hold_wait_duration(&self) -> Duration {
        self.base.hold_wait_duration()
    }

    fn hotkey_configuration(&self) -> Rc<PlatformHotkeyConfiguration> {
        self.base.hotkey_configuration()
    }

    fn preferred_application_language(&self) -> String {
        let last_language = self.last_language.borrow().clone();
        match last_language {
            Some(language) => language,
            None => {
                let language = self.query_preferred_application_language();
                *self.last_language.borrow_mut() = Some(language.clone());
                language
            }
        }
    }

    fn get_color_values(&self) -> PlatformColorValues {
        match self.color_values.get() {
            Some(color_values) => color_values,
            None => {
                let color_values = self.get_uncached_color_values();
                self.color_values.set(Some(color_values));
                color_values
            }
        }
    }

    fn color_values_changed(&self, handler: Rc<dyn Fn(&PlatformColorValues)>) -> Rc<dyn IDisposable> {
        self.base.color_values_changed(handler)
    }

    fn preferred_application_language_changed(&self, handler: Rc<dyn Fn()>) -> Rc<dyn IDisposable> {
        self.base.preferred_application_language_changed(handler)
    }
}

#[cfg(target_os = "ios")]
mod uikit {
    use super::{ISystemSettings, PlatformSettings, SystemColors};
    use block2::RcBlock;
    use objc2::rc::Retained;
    use objc2_core_foundation::CGFloat;
    use objc2_foundation::{
        NSCurrentLocaleDidChangeNotification, NSLocale, NSNotification, NSNotificationCenter, NSOperationQueue,
    };
    use objc2_ui_kit::{
        UIAccessibilityContrast, UIColor, UIConfigurationColorTransformerPreferredTint, UITraitCollection,
        UIUserInterfaceStyle,
    };
    use std::ptr::NonNull;
    use std::rc::Rc;

    /// The settings of the system, read from UIKit and Foundation.
    struct SystemSettings;

    impl ISystemSettings for SystemSettings {
        fn colors(&self) -> SystemColors {
            // SAFETY: the current trait collection is a property of the
            // thread that asks; the settings are an object of the main
            // thread (they are not `Send`), where UIKit sets it.
            let (style, contrast) = unsafe {
                let traits = UITraitCollection::currentTraitCollection();
                (traits.userInterfaceStyle(), traits.accessibilityContrast())
            };

            let clear = UIColor::clearColor();
            // SAFETY: the transformer is a constant block of UIKit (iOS 14
            // and later, the minimum of the port) that takes a colour and
            // returns one that it does not hand over the ownership of, so
            // the result is retained here.
            let tint_color: Option<Retained<UIColor>> = unsafe {
                UIConfigurationColorTransformerPreferredTint
                    .as_ref()
                    .and_then(|transformer| Retained::retain(transformer.call((NonNull::from(&*clear),)).as_ptr()))
            };

            let tint_color = tint_color.map(|tint_color| {
                let (mut red, mut green, mut blue, mut alpha): (CGFloat, CGFloat, CGFloat, CGFloat) =
                    (0.0, 0.0, 0.0, 0.0);
                // SAFETY: the four pointers are to variables of this
                // function, which live for the call.
                unsafe {
                    tint_color.getRed_green_blue_alpha(&mut red, &mut green, &mut blue, &mut alpha);
                }
                [red, green, blue, alpha]
            });

            SystemColors {
                is_dark: style == UIUserInterfaceStyle::Dark,
                is_high_contrast: contrast == UIAccessibilityContrast::High,
                tint_color,
            }
        }

        fn preferred_languages(&self) -> Vec<String> {
            NSLocale::preferredLanguages().iter().map(|language| language.to_string()).collect()
        }
    }

    impl PlatformSettings {
        /// Creates the settings of the system and observes the changes of
        /// its locale.
        pub fn new() -> Rc<Self> {
            let this = Self::with_system(Box::new(SystemSettings));

            let weak = Rc::downgrade(&this);
            let block = RcBlock::new(move |_notification: NonNull<NSNotification>| {
                if let Some(this) = weak.upgrade() {
                    this.on_preferred_language_changed();
                }
            });
            // SAFETY: the name is a constant of Foundation. The block
            // takes the one argument of a notification block and is run
            // on the main queue, which is the thread the settings belong
            // to, whichever thread posts the notification.
            let observer = unsafe {
                NSNotificationCenter::defaultCenter().addObserverForName_object_queue_usingBlock(
                    Some(NSCurrentLocaleDidChangeNotification),
                    None,
                    Some(&NSOperationQueue::mainQueue()),
                    &block,
                )
            };
            // The settings live as long as the process, and so does the
            // subscription (the reference does not remove it either).
            std::mem::forget(observer);

            this
        }
    }
}

#[cfg(test)]
mod tests {
    // Not from the reference, which has no tests of this file.
    use super::*;
    use std::cell::Cell;

    #[derive(Default)]
    struct FakeSystemState {
        colors: Cell<SystemColors>,
        languages: RefCell<Vec<String>>,
        color_queries: Cell<u32>,
        language_queries: Cell<u32>,
    }

    struct FakeSystem(Rc<FakeSystemState>);

    impl ISystemSettings for FakeSystem {
        fn colors(&self) -> SystemColors {
            self.0.color_queries.set(self.0.color_queries.get() + 1);
            self.0.colors.get()
        }

        fn preferred_languages(&self) -> Vec<String> {
            self.0.language_queries.set(self.0.language_queries.get() + 1);
            self.0.languages.borrow().clone()
        }
    }

    fn settings() -> (Rc<PlatformSettings>, Rc<FakeSystemState>) {
        let state = Rc::new(FakeSystemState::default());
        (PlatformSettings::with_system(Box::new(FakeSystem(state.clone()))), state)
    }

    #[test]
    fn the_style_and_the_contrast_of_the_traits_are_the_variant_and_the_preference() {
        let values = color_values(SystemColors::default());
        assert_eq!(PlatformThemeVariant::Light, values.theme_variant());
        assert_eq!(ColorContrastPreference::NoPreference, values.contrast_preference());
        assert_eq!(PlatformColorValues::new().accent_color1(), values.accent_color1());

        let values = color_values(SystemColors { is_dark: true, is_high_contrast: true, tint_color: None });
        assert_eq!(PlatformThemeVariant::Dark, values.theme_variant());
        assert_eq!(ColorContrastPreference::High, values.contrast_preference());
    }

    #[test]
    fn the_tint_colour_is_the_accent_colour() {
        let values =
            color_values(SystemColors { tint_color: Some([0.0, 0.478, 1.0, 1.0]), ..SystemColors::default() });
        // A colour with a component of zero is passed over, as in the
        // reference (the blue of the system has no red).
        assert_eq!(PlatformColorValues::new().accent_color1(), values.accent_color1());

        let values =
            color_values(SystemColors { tint_color: Some([1.0, 0.5, 0.25, 1.0]), ..SystemColors::default() });
        assert_eq!(Color::from_argb(255, 255, 127, 63), values.accent_color1());

        let values =
            color_values(SystemColors { tint_color: Some([1.0, 0.5, 0.25, 0.0]), ..SystemColors::default() });
        assert_eq!(PlatformColorValues::new().accent_color1(), values.accent_color1());
    }

    #[test]
    fn the_colour_values_are_asked_for_once_and_raised_when_they_change() {
        let (settings, state) = settings();
        let raised = Rc::new(RefCell::new(Vec::new()));
        let sink = raised.clone();
        let _subscription = settings
            .color_values_changed(Rc::new(move |values: &PlatformColorValues| sink.borrow_mut().push(*values)));

        assert_eq!(PlatformThemeVariant::Light, settings.get_color_values().theme_variant());
        assert_eq!(PlatformThemeVariant::Light, settings.get_color_values().theme_variant());
        assert_eq!(1, state.color_queries.get());

        // The same traits: nothing is raised.
        settings.trait_collection_did_change();
        assert!(raised.borrow().is_empty());

        state.colors.set(SystemColors { is_dark: true, ..SystemColors::default() });
        // The cached values answer until the view reports the change.
        assert_eq!(PlatformThemeVariant::Light, settings.get_color_values().theme_variant());
        settings.trait_collection_did_change();
        assert_eq!(1, raised.borrow().len());
        assert_eq!(PlatformThemeVariant::Dark, raised.borrow()[0].theme_variant());
        assert_eq!(PlatformThemeVariant::Dark, settings.get_color_values().theme_variant());

        settings.trait_collection_did_change();
        assert_eq!(1, raised.borrow().len());
    }

    #[test]
    fn a_change_of_the_traits_before_the_values_were_asked_for_is_raised() {
        let (settings, _state) = settings();
        let raised = Rc::new(Cell::new(0));
        let sink = raised.clone();
        let _subscription = settings.color_values_changed(Rc::new(move |_| sink.set(sink.get() + 1)));
        settings.trait_collection_did_change();
        assert_eq!(1, raised.get());
    }

    #[test]
    fn the_first_preferred_language_is_the_language_of_the_application() {
        let (settings, state) = settings();
        *state.languages.borrow_mut() = vec!["pl-PL".to_string(), "en-US".to_string()];
        assert_eq!("pl-PL", settings.preferred_application_language());
        assert_eq!("pl-PL", settings.preferred_application_language());
        assert_eq!(1, state.language_queries.get());
    }

    #[test]
    fn without_a_preferred_language_the_default_answers() {
        let (settings, state) = settings();
        let default = DefaultPlatformSettings::new().preferred_application_language();
        assert_eq!(default, settings.preferred_application_language());

        let (settings, state2) = (settings, state);
        *state2.languages.borrow_mut() = vec![String::new()];
        settings.on_preferred_language_changed();
        assert_eq!(default, settings.preferred_application_language());
    }

    #[test]
    fn a_change_of_the_locale_is_raised_when_the_language_changed() {
        let (settings, state) = settings();
        *state.languages.borrow_mut() = vec!["en-US".to_string()];
        let raised = Rc::new(Cell::new(0));
        let sink = raised.clone();
        let _subscription =
            settings.preferred_application_language_changed(Rc::new(move || sink.set(sink.get() + 1)));
        assert_eq!("en-US", settings.preferred_application_language());

        // The locale changed and the language did not.
        settings.on_preferred_language_changed();
        assert_eq!(0, raised.get());

        *state.languages.borrow_mut() = vec!["de-DE".to_string(), "en-US".to_string()];
        // The cached language answers until the notification.
        assert_eq!("en-US", settings.preferred_application_language());
        settings.on_preferred_language_changed();
        assert_eq!(1, raised.get());
        assert_eq!("de-DE", settings.preferred_application_language());
    }
}