| 1 | use crate::{App, Global, Rgba, Window, WindowAppearance, rgb}; |
| 2 | use std::ops::Deref; |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | #[derive(Clone, Debug)] |
| 9 | pub struct Colors { |
| 10 | |
| 11 | pub text: Rgba, |
| 12 | |
| 13 | pub selected_text: Rgba, |
| 14 | |
| 15 | pub background: Rgba, |
| 16 | |
| 17 | pub disabled: Rgba, |
| 18 | |
| 19 | pub selected: Rgba, |
| 20 | |
| 21 | pub border: Rgba, |
| 22 | |
| 23 | pub separator: Rgba, |
| 24 | |
| 25 | pub container: Rgba, |
| 26 | } |
| 27 | |
| 28 | impl Default for Colors { |
| 29 | fn default() -> Self { |
| 30 | Self::light() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl Colors { |
| 35 | |
| 36 | pub fn for_appearance(window: &Window) -> Self { |
| 37 | match window.appearance() { |
| 38 | WindowAppearance::Light | WindowAppearance::VibrantLight => Self::light(), |
| 39 | WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::dark(), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | |
| 44 | pub fn dark() -> Self { |
| 45 | Self { |
| 46 | text: rgb(0xffffff), |
| 47 | selected_text: rgb(0xffffff), |
| 48 | disabled: rgb(0x565656), |
| 49 | selected: rgb(0x2457ca), |
| 50 | background: rgb(0x222222), |
| 51 | border: rgb(0x000000), |
| 52 | separator: rgb(0xd9d9d9), |
| 53 | container: rgb(0x262626), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | pub fn light() -> Self { |
| 59 | Self { |
| 60 | text: rgb(0x252525), |
| 61 | selected_text: rgb(0xffffff), |
| 62 | background: rgb(0xffffff), |
| 63 | disabled: rgb(0xb0b0b0), |
| 64 | selected: rgb(0x2a63d9), |
| 65 | border: rgb(0xd9d9d9), |
| 66 | separator: rgb(0xe6e6e6), |
| 67 | container: rgb(0xf4f5f5), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | pub fn get_global(cx: &App) -> &Arc<Colors> { |
| 73 | &cx.global::<GlobalColors>().0 |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | #[derive(Clone, Debug)] |
| 79 | pub struct GlobalColors(pub Arc<Colors>); |
| 80 | |
| 81 | impl Deref for GlobalColors { |
| 82 | type Target = Arc<Colors>; |
| 83 | |
| 84 | fn deref(&self) -> &Self::Target { |
| 85 | &self.0 |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl Global for GlobalColors {} |
| 90 | |
| 91 | |
| 92 | pub trait DefaultColors { |
| 93 | |
| 94 | fn default_colors(&self) -> &Arc<Colors>; |
| 95 | } |
| 96 | |
| 97 | impl DefaultColors for App { |
| 98 | fn default_colors(&self) -> &Arc<Colors> { |
| 99 | &self.global::<GlobalColors>().0 |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 107 | pub enum DefaultAppearance { |
| 108 | |
| 109 | #[default] |
| 110 | Light, |
| 111 | |
| 112 | Dark, |
| 113 | } |
| 114 | |
| 115 | impl From<WindowAppearance> for DefaultAppearance { |
| 116 | fn from(appearance: WindowAppearance) -> Self { |
| 117 | match appearance { |
| 118 | WindowAppearance::Light | WindowAppearance::VibrantLight => Self::Light, |
| 119 | WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark, |
| 120 | } |
| 121 | } |
| 122 | } |