native-theme 0.7.1

Cross-platform native theme detection and loading for Rust GUI applications
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! macOS theme change watcher using notification-center observers.
//!
//! One observer object is registered for the notification of each of the
//! macOS reader's sources (`macos.rs`, "Sources"):
//!
//! - `AppleInterfaceThemeChangedNotification` on the default
//!   `NSDistributedNotificationCenter`, with the suspension behaviour
//!   `DeliverImmediately`: `NSApplication` suspends distributed delivery
//!   while the application is not active (Apple,
//!   <https://developer.apple.com/documentation/foundation/distributednotificationcenter/suspended>),
//!   and the change would otherwise wait until it is
//!   (<https://developer.apple.com/documentation/foundation/distributednotificationcenter/addobserver(_:selector:name:object:suspensionbehavior:)>).
//! - `NSSystemColorsDidChangeNotification` (the colours, the accent among
//!   them) and `NSPreferredScrollerStyleDidChangeNotification` on the
//!   process's default `NSNotificationCenter`. Apple's pages for them name
//!   no center, so the default one is an inference, the conventional center
//!   for AppKit's own notifications; no test sees a real AppKit post, so it
//!   stays unverified until checked on a Mac (`docs/todo.md`).
//! - `NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification` on
//!   `NSWorkspace`'s `notificationCenter`, the one center it is posted on
//!   (<https://developer.apple.com/documentation/appkit/nsworkspace/accessibilitydisplayoptionsdidchangenotification>).
//!
//! Distributed notifications are delivered through the process's run loop,
//! usually the main thread's, so the application must run one in a common
//! mode, as every GUI toolkit's event loop does (AppKit's
//! `NSApplication::run`): Apple, Notification Programming Topics,
//! "Notification Centers"
//! (<https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Notifications/Articles/NotificationCenters.html>).
//! The observer's method runs on the thread that posts the notification; it
//! only records it and queues the callback on a private serial
//! `NSOperationQueue`, so the callback never runs on the main thread and
//! never twice at once. The callback fires once per burst of notifications,
//! 300ms after the last of them, should one change post more than one.
//! There is no watcher thread: dropping the `ThemeSubscription` removes the
//! observer.

// Objective-C FFI via objc2 -- no safe alternative for platform bindings.
// This follows the same pattern as src/macos.rs (the theme reader).
#![allow(unsafe_code)]

use std::sync::{Arc, Condvar, Mutex};
use std::time::Instant;

use block2::RcBlock;
use objc2::rc::Retained;
use objc2::runtime::AnyObject;
use objc2::{AnyThread, DefinedClass, Message, define_class, msg_send, sel};
use objc2_app_kit::{
    NSPreferredScrollerStyleDidChangeNotification, NSSystemColorsDidChangeNotification,
    NSWorkspace, NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification,
};
use objc2_foundation::{
    NSDistributedNotificationCenter, NSNotification, NSNotificationCenter, NSNotificationName,
    NSNotificationSuspensionBehavior, NSObject, NSObjectProtocol, NSOperationQueue, NSString,
};

use super::ThemeChangeEvent;

/// A notification center the observer registers with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Center {
    /// The default `NSDistributedNotificationCenter`, delivering at once.
    Distributed,
    /// The process's default `NSNotificationCenter`; for AppKit's colour
    /// and scroller notifications an inference, as Apple names no center.
    Local,
    /// `NSWorkspace`'s `notificationCenter`.
    Workspace,
}

/// A notification the observer registers for.
pub(super) struct Registration {
    /// The notification's constant, as the source table (`watch/mod.rs`)
    /// names it.
    #[cfg_attr(not(test), allow(dead_code))]
    pub(super) name_id: &'static str,
    center: Center,
    name: Retained<NSNotificationName>,
}

/// The notifications of the macOS reader's sources.
pub(super) fn registrations() -> [Registration; 4] {
    // SAFETY: Each is an immutable NSString constant that AppKit defines
    // (objc2-app-kit 0.3.2 `NSColor.rs:1068`, `NSScroller.rs:324`,
    // `NSAccessibility.rs:190`), valid for the life of the process.
    let (system_colors, scroller_style, accessibility_display) = unsafe {
        (
            NSSystemColorsDidChangeNotification.retain(),
            NSPreferredScrollerStyleDidChangeNotification.retain(),
            NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification.retain(),
        )
    };
    [
        Registration {
            name_id: "AppleInterfaceThemeChangedNotification",
            center: Center::Distributed,
            name: NSString::from_str("AppleInterfaceThemeChangedNotification"),
        },
        Registration {
            name_id: "NSSystemColorsDidChangeNotification",
            center: Center::Local,
            name: system_colors,
        },
        Registration {
            name_id: "NSPreferredScrollerStyleDidChangeNotification",
            center: Center::Local,
            name: scroller_style,
        },
        Registration {
            name_id: "NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification",
            center: Center::Workspace,
            name: accessibility_display,
        },
    ]
}

/// Whether a callback is queued, and since when it waits.
#[derive(Default)]
struct Pending {
    /// When the last notification not yet fired arrived; `None` while no
    /// callback is queued.
    last: Option<Instant>,
    /// Set by the guard's drop: no callback is queued or fired after it.
    closed: bool,
}

/// What the observer's notifications share: the coalescing state, the
/// serial queue the callback runs on, and the callback.
struct Delivery {
    pending: Mutex<Pending>,
    /// Wakes a queued callback that waits out the debounce when the guard
    /// drops.
    wake: Condvar,
    queue: Retained<NSOperationQueue>,
    /// The callback is only `Send`, and the queue may run it on any of its
    /// threads, so it is shared through a mutex. `fire` contains a panic
    /// inside the lock, so the lock is never poisoned.
    callback: Mutex<Box<dyn Fn(ThemeChangeEvent) + Send>>,
}

impl Delivery {
    /// Record a notification, and queue the callback unless one is queued.
    fn notify(self: &Arc<Self>) {
        let Ok(mut pending) = self.pending.lock() else {
            return;
        };
        if pending.closed {
            return;
        }
        if pending.last.replace(Instant::now()).is_none() {
            let delivery = Arc::clone(self);
            let block = RcBlock::new(move || delivery.run());
            // SAFETY: The block is sendable: it captures only an `Arc` of a
            // `Delivery`, which is `Send + Sync`.
            unsafe { self.queue.addOperationWithBlock(&block) };
        }
    }

    /// Wait until [`super::DEBOUNCE`] has passed since the last
    /// notification, then fire the callback, unless the guard dropped
    /// meanwhile. Runs on the queue. The window is T20's KDE one reused to
    /// keep one user action one event, not a macOS value.
    fn run(&self) {
        {
            let Ok(mut pending) = self.pending.lock() else {
                return;
            };
            loop {
                if pending.closed {
                    return;
                }
                let Some(last) = pending.last else {
                    return;
                };
                let wait = super::DEBOUNCE.saturating_sub(last.elapsed());
                if wait.is_zero() {
                    pending.last = None;
                    break;
                }
                pending = match self.wake.wait_timeout(pending, wait) {
                    Ok((pending, _)) => pending,
                    Err(_) => return,
                };
            }
        }
        if let Ok(callback) = self.callback.lock() {
            super::fire(&*callback);
        }
    }

    /// Stop queueing and firing the callback, and wake a queued one.
    fn close(&self) {
        if let Ok(mut pending) = self.pending.lock() {
            pending.closed = true;
        }
        self.wake.notify_all();
    }
}

define_class!(
    // SAFETY:
    // - The superclass NSObject does not have any subclassing requirements.
    // - `Observer` does not implement `Drop`.
    #[unsafe(super(NSObject))]
    #[ivars = Arc<Delivery>]
    struct Observer;

    impl Observer {
        // SAFETY: The signature is a notification observer's: one
        // NSNotification argument, no return value.
        #[unsafe(method(themeSettingChanged:))]
        fn theme_setting_changed(&self, _notification: &NSNotification) {
            self.ivars().notify();
        }
    }

    // SAFETY: `Observer` subclasses NSObject (the `super` above), which conforms
    // to NSObjectProtocol; the protocol adds no requirement of its own.
    unsafe impl NSObjectProtocol for Observer {}
);

impl Observer {
    fn new(delivery: Arc<Delivery>) -> Retained<Self> {
        let this = Self::alloc().set_ivars(delivery);
        // SAFETY: NSObject's `init` takes no argument and returns the
        // initialized object.
        unsafe { msg_send![super(this), init] }
    }
}

/// A registered observer. Dropping it removes the observer from its
/// centers.
struct ObserverGuard {
    /// Kept alive while it is registered: a center does not retain it.
    observer: Retained<Observer>,
    centers: Vec<Retained<NSNotificationCenter>>,
}

impl Drop for ObserverGuard {
    fn drop(&mut self) {
        let observer: &AnyObject = &self.observer;
        for center in &self.centers {
            // SAFETY: `observer` is the object registered on `center`, alive
            // until this guard drops it.
            unsafe { center.removeObserver(observer) };
        }
        let delivery = self.observer.ivars();
        delivery.close();
        // removeObserver does not wait for a callback already queued, so
        // wait for the queue to drain: no callback runs after the drop
        // returns. A drop from inside the callback runs on the queue itself,
        // where the wait would deadlock, so it returns without waiting.
        let on_own_queue = NSOperationQueue::currentQueue()
            .is_some_and(|current| Retained::as_ptr(&current) == Retained::as_ptr(&delivery.queue));
        if !on_own_queue {
            delivery.queue.waitUntilAllOperationsAreFinished();
        }
    }
}

/// Register one observer for every notification of `registrations`, which
/// fires `callback` on a new serial operation queue.
fn observe(
    registrations: &[Registration],
    callback: impl Fn(ThemeChangeEvent) + Send + 'static,
) -> ObserverGuard {
    let queue = NSOperationQueue::new();
    queue.setMaxConcurrentOperationCount(1);
    let observer = Observer::new(Arc::new(Delivery {
        pending: Mutex::new(Pending::default()),
        wake: Condvar::new(),
        queue,
        callback: Mutex::new(Box::new(callback)),
    }));
    let observer_any: &AnyObject = &observer;

    let mut centers: Vec<Retained<NSNotificationCenter>> = Vec::new();
    for registration in registrations {
        let name: &NSNotificationName = &registration.name;
        let center = match registration.center {
            Center::Distributed => {
                let center = NSDistributedNotificationCenter::defaultCenter();
                // SAFETY: `observer_any` is an NSObject the guard keeps alive
                // until it removes it, the selector is `Observer`'s method
                // taking one NSNotification, and no object filter is given.
                unsafe {
                    center.addObserver_selector_name_object_suspensionBehavior(
                        observer_any,
                        sel!(themeSettingChanged:),
                        Some(name),
                        None,
                        NSNotificationSuspensionBehavior::DeliverImmediately,
                    );
                }
                center.into_super()
            }
            Center::Local | Center::Workspace => {
                let center = if registration.center == Center::Local {
                    NSNotificationCenter::defaultCenter()
                } else {
                    NSWorkspace::sharedWorkspace().notificationCenter()
                };
                // SAFETY: as above.
                unsafe {
                    center.addObserver_selector_name_object(
                        observer_any,
                        sel!(themeSettingChanged:),
                        Some(name),
                        None,
                    );
                }
                center
            }
        };
        if !centers
            .iter()
            .any(|known| Retained::as_ptr(known) == Retained::as_ptr(&center))
        {
            centers.push(center);
        }
    }

    ObserverGuard { observer, centers }
}

/// Watch for macOS theme changes.
///
/// Registers an observer for the notifications of [`registrations`]. The
/// callback fires on a private serial queue whenever the user changes the
/// appearance, the accent or highlight colour, the scroll bars or an
/// accessibility display option in System Settings, provided the
/// application runs its main run loop.
pub(crate) fn watch_macos(
    callback: impl Fn(ThemeChangeEvent) + Send + 'static,
) -> crate::Result<super::ThemeSubscription> {
    let guard = observe(&registrations(), callback);

    Ok(super::ThemeSubscription::from_platform_shutdown(Box::new(
        move || drop(guard),
    )))
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use super::*;

    /// A registration for the test notification `name` on the default
    /// (in-process) center.
    fn local(name: &str) -> Registration {
        Registration {
            name_id: "test",
            center: Center::Local,
            name: NSString::from_str(name),
        }
    }

    /// Post `name` on the default center, as AppKit posts its notifications.
    fn post(name: &NSNotificationName) {
        // SAFETY: `name` is a valid NSString and no object is attached.
        unsafe { NSNotificationCenter::defaultCenter().postNotificationName_object(name, None) };
    }

    /// The queue the guard's callback runs on.
    fn queue(guard: &ObserverGuard) -> Retained<NSOperationQueue> {
        guard.observer.ivars().queue.clone()
    }

    /// A distributed notification reaches the process only through its main
    /// run loop, which a test binary does not run, so the guard is checked
    /// on the default (in-process) center: it posts on the calling thread,
    /// and the callback runs on the guard's queue either way.
    #[test]
    fn dropping_the_guard_removes_the_observer() {
        let registrations = [local("native-theme.watch.macos.test")];
        let count = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&count);
        let guard = observe(&registrations, move |_| {
            counter.fetch_add(1, Ordering::SeqCst);
        });
        let queue = queue(&guard);

        post(&registrations[0].name);
        queue.waitUntilAllOperationsAreFinished();
        assert_eq!(count.load(Ordering::SeqCst), 1);

        drop(guard);
        post(&registrations[0].name);
        queue.waitUntilAllOperationsAreFinished();
        assert_eq!(count.load(Ordering::SeqCst), 1);
    }

    /// A callback that panics does not end the observer: the next
    /// notification still runs it (M4-33).
    #[test]
    fn a_panicking_callback_does_not_stop_the_observer() {
        let registrations = [local("native-theme.watch.macos.panic")];
        let count = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&count);
        let guard = observe(&registrations, move |_| {
            let call = counter.fetch_add(1, Ordering::SeqCst);
            assert!(call > 0, "the first callback panics");
        });
        let queue = queue(&guard);

        for _ in 0..2 {
            post(&registrations[0].name);
            queue.waitUntilAllOperationsAreFinished();
        }
        assert_eq!(count.load(Ordering::SeqCst), 2);
        drop(guard);
    }

    /// Two notifications of one change, posted together, fire the callback
    /// once. Each notification's time is taken under the lock that the
    /// queued callback checks it under, so two posts less than
    /// [`super::super::DEBOUNCE`] apart fire once whatever the scheduler
    /// does. A loaded machine can stall the test between the posts, and
    /// then they are two changes, which may fire twice.
    #[test]
    fn notifications_posted_together_fire_once() {
        let registrations = [
            local("native-theme.watch.macos.burst-a"),
            local("native-theme.watch.macos.burst-b"),
        ];
        let count = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&count);
        let guard = observe(&registrations, move |_| {
            counter.fetch_add(1, Ordering::SeqCst);
        });
        let queue = queue(&guard);

        let before = Instant::now();
        post(&registrations[0].name);
        post(&registrations[1].name);
        let posting = before.elapsed();
        queue.waitUntilAllOperationsAreFinished();
        let fires = count.load(Ordering::SeqCst);
        if posting < super::super::DEBOUNCE {
            assert_eq!(fires, 1, "posts {posting:?} apart fired {fires} times");
        } else {
            assert!(
                (1..=2).contains(&fires),
                "posts {posting:?} apart fired {fires} times"
            );
        }
        drop(guard);
    }

    /// The watcher registers for every notification of the reader's
    /// sources, and its drop returns.
    #[test]
    fn the_watcher_registers_and_drops() {
        let guard = observe(&registrations(), |_| {});
        assert_eq!(guard.centers.len(), 3);
        drop(guard);
    }
}