clairlune-live 0.36.0

Clairlune's live wallpaper: plays a scene behind the desktop icons, drawing only what changed
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
//! A solid menu bar while app windows all but fill a screen (macOS).
//!
//! From macOS 26 the menu bar is clear, and above a zoomed window it is the
//! only strip of wallpaper left: a busy one behind the menus. While windows
//! all but fill a screen ([`cover::backing`]), a window of one colour sits
//! behind its menu bar, just above the wallpaper: dark grey over a dark
//! wallpaper, white over a light one. macOS picks the menus' own colour from
//! the wallpaper, a moment after it changes; the strip goes by what it picked
//! ([`is_dark`]) where that is known, so the menus always read. Earlier
//! systems blur the bar into one colour themselves.
//!
//! It comes and goes with the windows, in one frame. Nothing tells an app of
//! other apps' windows without a permission, so it looks: twice a second, and
//! [`FAST`] for a moment after a click or a modifier key (what minimizing,
//! zooming, closing and switching start with) and while windows move.
//!
//! Everything here runs on the main thread (AppKit's rule).

use crate::cover::{Backing, CORNERS, Rect, Watch};
use crate::mac::{AppWindows, WINDOW_CHECK, desktop_level, join_desktop, usable_area};
use clairlune_platform::macos::find_screen;
use objc2::rc::Retained;
use objc2::runtime::{NSObjectProtocol, ProtocolObject};
use objc2::{MainThreadMarker, MainThreadOnly, define_class, msg_send};
use objc2_app_kit::{
    NSAppearanceCustomization, NSBackingStoreType, NSColor, NSScreen, NSStatusItem, NSView,
    NSWindow, NSWindowStyleMask,
};
use objc2_core_foundation::CFRetained;
use objc2_core_graphics::{CGColor, CGEventSource, CGEventSourceStateID, CGEventType};
use objc2_foundation::{NSActivityOptions, NSPoint, NSProcessInfo, NSRect, NSSize, ns_string};
use objc2_quartz_core::{CALayer, CATransaction};
use std::time::{Duration, Instant};

define_class!(
    // SAFETY: NSWindow asks nothing of its subclasses, and this one has no
    // state and no Drop.
    #[unsafe(super(NSWindow))]
    #[thread_kind = MainThreadOnly]
    #[name = "ClairluneMenuBarWindow"]
    struct BarWindow;

    impl BarWindow {
        /// AppKit moves windows out from under the menu bar; this one's
        /// place is there.
        #[unsafe(method(constrainFrameRect:toScreen:))]
        fn constrain(&self, frame: NSRect, _screen: Option<&NSScreen>) -> NSRect {
            frame
        }
    }
);

/// The strip behind one screen's menu bar.
struct Strip {
    /// The menu bar, and below it as far as a window's rounded corners reach.
    frame: NSRect,
    layer: Retained<CALayer>,
    window: Retained<BarWindow>,
}

struct Bar {
    /// The monitor's id, and whether its wallpaper is dark.
    monitor: String,
    dark: bool,
    /// The screen's usable part, and where the strip goes: none on a screen
    /// without a menu bar.
    place: Option<(Rect, NSRect)>,
    /// What is behind the menu bar, and whether that is dark.
    watch: Watch,
    shown: (Backing, bool),
    strip: Option<Strip>,
}

/// How often to look while something may be moving: one 60 Hz frame.
const FAST: Duration = Duration::from_millis(16);
/// For how long after a click, a modifier key or an app change, and after
/// windows moved.
const FAST_FOR: Duration = Duration::from_millis(550);
/// How often to look for new windows too, meanwhile.
const FIND: Duration = Duration::from_millis(80);
/// How often to re-measure screen and Dock frames when nothing notified us.
const MEASURE_CHECK: Duration = Duration::from_secs(5);
/// How often to ask whether there was a click or a modifier key while active
/// (backing off to [`WINDOW_CHECK`] once idle).
const INPUT_ACTIVE: Duration = Duration::from_millis(100);
const INPUT_WARM: Duration = Duration::from_millis(250);

/// How many clicks and modifier key presses there were (excluding scroll/trackpad
/// gestures and drags, so scrolling a page doesn't spin up window polling).
/// Counting them needs no permission; which they were isn't known.
fn input_count() -> u32 {
    [
        CGEventType::LeftMouseDown,
        CGEventType::LeftMouseUp,
        CGEventType::FlagsChanged,
    ]
    .into_iter()
    .map(|t| CGEventSource::counter_for_event_type(CGEventSourceStateID::CombinedSessionState, t))
    .fold(0, u32::wrapping_add)
}

/// The menu bars of the screens with a wallpaper of ours.
#[derive(Default)]
pub struct MenuBars {
    bars: Vec<Bar>,
    /// Whether macOS takes the menu bar for a dark one, when known.
    seen_dark: Option<bool>,
    seen_at: Option<Instant>,
    /// When the windows, the screens and the input were last looked at.
    checked: Option<Instant>,
    measured: Option<Instant>,
    found: Option<Instant>,
    asked_input: Option<Instant>,
    last_input: Option<Instant>,
    input: u32,
    fast_until: Option<Instant>,
    /// Held while a menu bar is solid: macOS slows down (App Nap) an app
    /// whose wallpaper nobody sees, and then it would clear late.
    awake: Option<Retained<ProtocolObject<dyn NSObjectProtocol>>>,
}

/// Whether the menu bar is clear here. An app built for an older system is
/// told that macOS 26 is 16.
fn clear_menu_bar() -> bool {
    NSProcessInfo::processInfo()
        .operatingSystemVersion()
        .majorVersion
        >= 16
}

/// Whether macOS takes the menu bar for a dark one (its menus are white),
/// from how it draws an item there.
pub fn is_dark(item: &NSStatusItem) -> Option<bool> {
    let button = item.button(MainThreadMarker::new()?)?;
    Some(
        button
            .effectiveAppearance()
            .name()
            .to_string()
            .contains("Dark"),
    )
}

impl MenuBars {
    /// The screens with a wallpaper of ours: the monitor's id, and whether
    /// the wallpaper is dark.
    pub fn set(&mut self, screens: Vec<(String, bool)>) {
        if !clear_menu_bar() {
            return;
        }
        let same = screens.len() == self.bars.len()
            && screens
                .iter()
                .zip(&self.bars)
                .all(|((id, _), b)| *id == b.monitor);
        if same {
            for ((_, dark), bar) in screens.into_iter().zip(&mut self.bars) {
                bar.dark = dark;
            }
        } else {
            self.bars = screens
                .into_iter()
                .map(|(monitor, dark)| Bar {
                    monitor,
                    dark,
                    place: None,
                    watch: Watch::default(),
                    shown: (Backing::Clear, dark),
                    strip: None,
                })
                .collect();
        }
        self.checked = None;
        self.measured = None;
        self.seen_at = None;
        self.stay_awake();
    }

    /// Whether [`MenuBars::see`] wants a fresh look at the menu bar's text
    /// appearance (twice a second at most, not every animation frame).
    pub fn appearance_due(&self, now: Instant) -> bool {
        !self.bars.is_empty()
            && !clairlune_platform::macos::screens_asleep()
            && self.seen_at.is_none_or(|t| now >= t + WINDOW_CHECK)
    }

    /// Whether [`MenuBars::tick`] would look now.
    pub fn due(&self, now: Instant) -> bool {
        !self.bars.is_empty() && self.checked.is_none_or(|t| now >= t + self.pace(now))
    }

    /// Notes that an app activated, hid or unhid, or Spaces/screens changed,
    /// so windows are re-checked right away.
    pub fn note_windows_changed(&mut self, now: Instant) {
        if self.bars.is_empty() {
            return;
        }
        self.checked = None;
        self.measured = None;
        self.found = None;
        self.last_input = Some(now);
        self.fast_until = Some(now + FAST_FOR);
    }

    /// How long from one look at the windows to the next.
    fn pace(&self, now: Instant) -> Duration {
        match self.fast_until {
            Some(t) if now < t => FAST,
            _ => WINDOW_CHECK,
        }
    }

    /// How often to check click/modifier counters: brisk right after input,
    /// settling to [`WINDOW_CHECK`] once the Mac has been untouched for 30 s.
    fn input_pace(&self, now: Instant) -> Duration {
        let Some(last) = self.last_input else {
            return WINDOW_CHECK;
        };
        let idle = now.saturating_duration_since(last);
        if idle < Duration::from_secs(5) {
            INPUT_ACTIVE
        } else if idle < Duration::from_secs(30) {
            INPUT_WARM
        } else {
            WINDOW_CHECK
        }
    }

    /// Whether macOS takes the menu bar for a dark one ([`is_dark`]).
    pub fn see(&mut self, now: Instant, dark: Option<bool>) {
        self.seen_at = Some(now);
        self.seen_dark = dark;
    }

    /// Looks where the windows are when that's due; returns when to come
    /// back.
    pub fn tick(&mut self, now: Instant, windows: &mut AppWindows) -> Option<Instant> {
        if self.bars.is_empty() || clairlune_platform::macos::screens_asleep() {
            return None;
        }
        let ask_every = self.input_pace(now);
        if self.asked_input.is_none_or(|t| now >= t + ask_every) {
            self.asked_input = Some(now);
            let input = input_count();
            let prev = std::mem::replace(&mut self.input, input);
            if prev != input {
                self.last_input = Some(now);
                self.fast_until = Some(now + FAST_FOR);
            }
        }
        if self.due(now)
            && let Some(mtm) = MainThreadMarker::new()
        {
            self.checked = Some(now);
            if self.measured.is_none_or(|t| now >= t + MEASURE_CHECK) {
                self.measured = Some(now);
                for bar in &mut self.bars {
                    bar.measure(mtm);
                }
            }
            let windows = if self.found.is_none_or(|t| now >= t + FIND) {
                self.found = Some(now);
                windows.look_now()
            } else {
                windows.look_again()
            };
            let mut moved = false;
            for bar in &mut self.bars {
                moved |= bar.update(mtm, now, windows, self.seen_dark);
            }
            if moved {
                self.fast_until = Some(now + FAST_FOR);
            }
            self.stay_awake();
        }
        let look = self.checked.map(|t| t + self.pace(now));
        let ask = self.asked_input.map(|t| t + self.input_pace(now));
        [look, ask].into_iter().flatten().min()
    }

    fn stay_awake(&mut self) {
        let solid = self.bars.iter().any(|b| b.shown.0 != Backing::Clear);
        if solid == self.awake.is_some() {
            return;
        }
        let process = NSProcessInfo::processInfo();
        match self.awake.take() {
            // SAFETY: what beginActivity gave.
            Some(activity) => unsafe { process.endActivity(&activity) },
            None => {
                self.awake = Some(process.beginActivityWithOptions_reason(
                    NSActivityOptions::UserInitiatedAllowingIdleSystemSleep,
                    ns_string!("The menu bar follows the windows"),
                ));
            }
        }
    }
}

impl Bar {
    /// Where the screen and its menu bar are.
    fn measure(&mut self, mtm: MainThreadMarker) {
        self.place = find_screen(mtm, &self.monitor)
            .and_then(|s| Some((usable_area(mtm, &s)?, strip_frame(&s)?)));
        // Another resolution, or the menu bar or the screen went.
        let frame = self.place.map(|(_, f)| f);
        if self.strip.as_ref().is_some_and(|s| Some(s.frame) != frame) {
            self.strip = None;
            self.watch = Watch::default();
            self.shown.0 = Backing::Clear;
        }
    }

    /// Looks, and shows what it finds; whether windows moved.
    fn update(
        &mut self,
        mtm: MainThreadMarker,
        now: Instant,
        windows: &[Rect],
        seen: Option<bool>,
    ) -> bool {
        let Some((area, frame)) = self.place else {
            return false;
        };
        let (backing, moved) = self.watch.look(now, area, windows);
        let solid = backing != Backing::Clear;
        let dark = if solid {
            seen.unwrap_or(self.dark)
        } else {
            self.shown.1
        };
        if (backing, dark) != self.shown {
            if solid != (self.shown.0 != Backing::Clear) || dark != self.shown.1 {
                tracing::info!(target: "clairlune", screen = %self.monitor, solid, dark, "menu bar");
            }
            self.shown = (backing, dark);
            self.strip
                .get_or_insert_with(|| Strip::new(mtm, frame, dark))
                .show(backing, dark);
        }
        moved
    }
}

/// Where the strip goes; none on a screen without a menu bar.
fn strip_frame(screen: &NSScreen) -> Option<NSRect> {
    let (f, v) = (screen.frame(), screen.visibleFrame());
    let bottom = v.origin.y + v.size.height;
    let height = f.origin.y + f.size.height - bottom;
    (height >= 1.0).then(|| {
        NSRect::new(
            NSPoint::new(f.origin.x, bottom - CORNERS),
            NSSize::new(f.size.width, height + CORNERS),
        )
    })
}

fn colour(dark: bool) -> CFRetained<CGColor> {
    let (r, g, b) = if dark {
        (0.157, 0.157, 0.165)
    } else {
        (1.0, 1.0, 1.0)
    };
    CGColor::new_srgb(r, g, b, 1.0)
}

impl Strip {
    fn new(mtm: MainThreadMarker, frame: NSRect, dark: bool) -> Strip {
        let this = BarWindow::alloc(mtm).set_ivars(());
        // SAFETY: NSWindow's own initialiser.
        let window: Retained<BarWindow> = unsafe {
            msg_send![
                super(this),
                initWithContentRect: frame,
                styleMask: NSWindowStyleMask::Borderless,
                backing: NSBackingStoreType::Buffered,
                defer: false,
            ]
        };
        // Above the wallpaper (ours, live, is at the desktop's own level) and
        // below the desktop icons.
        join_desktop(&window, desktop_level() + 1);
        window.setOpaque(false);
        window.setBackgroundColor(Some(&NSColor::clearColor()));
        let view =
            NSView::initWithFrame(NSView::alloc(mtm), NSRect::new(NSPoint::ZERO, frame.size));
        let root = CALayer::new();
        view.setLayer(Some(&root));
        view.setWantsLayer(true);
        let layer = CALayer::new();
        layer.setBackgroundColor(Some(&colour(dark)));
        layer.setOpacity(0.0);
        root.addSublayer(&layer);
        window.setContentView(Some(&view));
        window.setFrame_display(frame, false);
        window.orderFrontRegardless();
        Strip {
            frame,
            layer,
            window,
        }
    }

    /// Shows the backing, in the next frame.
    fn show(&self, backing: Backing, dark: bool) {
        let size = self.frame.size;
        let below = match backing {
            Backing::BarAndCorners => 0.0,
            Backing::Bar | Backing::Clear => CORNERS,
        };
        CATransaction::begin();
        CATransaction::setDisableActions(true);
        self.layer.setFrame(NSRect::new(
            NSPoint::new(0.0, below),
            NSSize::new(size.width, size.height - below),
        ));
        self.layer.setBackgroundColor(Some(&colour(dark)));
        self.layer
            .setOpacity(if backing == Backing::Clear { 0.0 } else { 1.0 });
        CATransaction::commit();
        CATransaction::flush();
    }
}

impl Drop for Strip {
    fn drop(&mut self) {
        self.window.orderOut(None);
        self.window.close();
    }
}