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
//! The color of the icons the platform draws in its status and navigation bars
//! over an app that paints its own background under them.
use std::sync::{Mutex, PoisonError, atomic::Ordering};
use cranpose_core::DisposableEffect;
use portable_atomic::AtomicU64;
use crate::host_controller;
/// The color of the icons the platform draws in its status and navigation bars
/// over the app.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SystemBarIcons {
/// What the platform shows for the system's light or dark theme.
#[default]
FollowSystem,
/// Dark icons, readable over a light background.
Dark,
/// Light icons, readable over a dark background.
Light,
}
// Every active `SystemBarAppearance` with the icons it asks for, oldest first.
static REQUESTS: Mutex<Vec<(u64, SystemBarIcons)>> = Mutex::new(Vec::new());
static NEXT_REQUEST: AtomicU64 = AtomicU64::new(0);
/// The icons the most recent active [`SystemBarAppearance`] asks for, or
/// [`SystemBarIcons::FollowSystem`] when none is in composition.
pub fn system_bar_icons() -> SystemBarIcons {
REQUESTS
.lock()
.unwrap_or_else(PoisonError::into_inner)
.last()
.map_or(SystemBarIcons::FollowSystem, |&(_, icons)| icons)
}
pub(crate) fn show_current_system_bar_icons() {
if let Some(host) = host_controller() {
host.set_system_bar_icons(system_bar_icons());
}
}
fn request(icons: SystemBarIcons) -> u64 {
let id = NEXT_REQUEST.fetch_add(1, Ordering::Relaxed);
REQUESTS
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push((id, icons));
show_current_system_bar_icons();
id
}
fn withdraw(id: u64) {
REQUESTS
.lock()
.unwrap_or_else(PoisonError::into_inner)
.retain(|&(request, _)| request != id);
show_current_system_bar_icons();
}
/// Shows `icons` in the status and navigation bars while this call stays in
/// composition, for an app that paints its own background under the bars:
/// Android's light and dark system bar appearance, and the iOS status bar
/// style. The most recent active call wins; when it leaves composition the one
/// before it applies again, and with none left the bars follow the system
/// theme. Platforms without system bars over the app ignore it.
#[expect(non_snake_case, reason = "names follow Compose's casing")]
#[track_caller]
pub fn SystemBarAppearance(icons: SystemBarIcons) {
DisposableEffect(icons, move |scope| {
let id = request(icons);
scope.on_dispose(move || withdraw(id))
});
}
#[cfg(test)]
#[path = "tests/system_bars_tests.rs"]
mod tests;