aylis_core/
system_bars.rs1use std::sync::atomic::{AtomicBool, Ordering};
9
10use crate::color::Color;
11use crate::ui::Ui;
12
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
15pub enum BarIcons {
16 #[default]
18 Auto,
19 Light,
21 Dark,
23}
24
25impl crate::prop::PropEnum for BarIcons {
26 const NAMES: &'static [&'static str] = &["auto", "light", "dark"];
27 fn from_index(i: usize) -> Option<Self> {
28 [BarIcons::Auto, BarIcons::Light, BarIcons::Dark].get(i).copied()
29 }
30 fn index(self) -> usize {
31 self as usize
32 }
33}
34
35#[derive(Clone, Debug, PartialEq)]
37pub struct SystemBarsStyle {
38 pub status_color: Option<Color>,
39 pub navigation_color: Option<Color>,
41 pub icons: BarIcons,
42 pub status_visible: bool,
43 pub navigation_visible: bool,
44}
45
46impl Default for SystemBarsStyle {
47 fn default() -> Self {
48 Self { status_color: None, navigation_color: None, icons: BarIcons::Auto, status_visible: true, navigation_visible: true }
49 }
50}
51
52impl SystemBarsStyle {
53 pub fn dark_icons(&self, background: Color) -> bool {
55 match self.icons {
56 BarIcons::Dark => true,
57 BarIcons::Light => false,
58 BarIcons::Auto => 0.299 * background.r as f32 + 0.587 * background.g as f32 + 0.114 * background.b as f32 > 140.0,
59 }
60 }
61}
62
63static AVAILABLE: AtomicBool = AtomicBool::new(false);
65
66pub fn set_available(available: bool) {
68 AVAILABLE.store(available, Ordering::Relaxed);
69}
70
71pub fn available() -> bool {
73 AVAILABLE.load(Ordering::Relaxed)
74}
75
76impl Ui {
77 pub fn set_system_bars(&mut self, style: Option<SystemBarsStyle>) {
79 if self.system_bars != style {
80 self.system_bars = style;
81 self.system_bars_version += 1;
82 self.request_repaint();
83 }
84 }
85
86 pub fn system_bars(&self) -> Option<&SystemBarsStyle> {
88 self.system_bars.as_ref()
89 }
90
91 pub fn system_bars_version(&self) -> u64 {
93 self.system_bars_version
94 }
95
96 pub fn set_paints_status_strip(&mut self, paints: bool) {
99 self.paints_status_strip = paints;
100 }
101
102 pub fn paints_status_strip(&self) -> bool {
103 self.paints_status_strip
104 }
105
106 pub(crate) fn status_strip(&self, root: crate::component::ComponentId) -> Option<(crate::geometry::RectF, Color)> {
108 if !self.paints_status_strip || self.main_form() != Some(root) {
109 return None;
110 }
111 let style = self.system_bars.as_ref().filter(|s| s.status_visible)?;
112 let color = style.status_color?;
113 let cb = self.cb(root)?;
114 let top = cb.safe_area.top;
115 (top > 0.0).then(|| (crate::geometry::RectF::new(0.0, 0.0, cb.bounds().width, top), color))
116 }
117}