| 1 | //! Fluent 2 dark shadows as Teams paints them. GPUI's shader takes the blur as |
| 2 | //! the gaussian's sigma while CSS takes twice that, so every CSS blur is halved. |
| 3 | |
| 4 | use std::collections::HashMap; |
| 5 | |
| 6 | use gpui::{App, BoxShadow, Global, Hsla, Window, WindowId, hsla, point, px}; |
| 7 | |
| 8 | use crate::ActiveTheme as _; |
| 9 | |
| 10 | const AMBIENT_INK: f32 = 0.24; |
| 11 | const KEY_INK: f32 = 0.28; |
| 12 | |
| 13 | #[derive(Clone, Debug, PartialEq)] |
| 14 | pub struct Elevation { |
| 15 | pub fill: Hsla, |
| 16 | pub hover: Hsla, |
| 17 | pub shadow: Vec<BoxShadow>, |
| 18 | } |
| 19 | |
| 20 | impl Elevation { |
| 21 | pub fn shadow_at(&self, strength: f32) -> Vec<BoxShadow> { |
| 22 | let strength = strength.clamp(0., 1.); |
| 23 | self.shadow |
| 24 | .iter() |
| 25 | .cloned() |
| 26 | .map(|mut layer| { |
| 27 | layer.color = layer.color.alpha(layer.color.a * strength); |
| 28 | layer |
| 29 | }) |
| 30 | .collect() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[derive(Clone, Debug, PartialEq)] |
| 35 | pub struct ElevationTokens { |
| 36 | pub flyouts: Vec<Elevation>, |
| 37 | pub dialog: Elevation, |
| 38 | pub toast: Elevation, |
| 39 | } |
| 40 | |
| 41 | impl Global for ElevationTokens {} |
| 42 | |
| 43 | fn fluent_shadow( |
| 44 | ambient_blur: f32, |
| 45 | key_offset: f32, |
| 46 | key_blur: f32, |
| 47 | ambient_ink: f32, |
| 48 | key_ink: f32, |
| 49 | ) -> Vec<BoxShadow> { |
| 50 | vec![ |
| 51 | BoxShadow { |
| 52 | color: hsla(0., 0., 0., ambient_ink), |
| 53 | offset: point(px(0.), px(0.)), |
| 54 | blur_radius: px(ambient_blur / 2.), |
| 55 | spread_radius: px(0.), |
| 56 | inset: false, |
| 57 | }, |
| 58 | BoxShadow { |
| 59 | color: hsla(0., 0., 0., key_ink), |
| 60 | offset: point(px(0.), px(key_offset)), |
| 61 | blur_radius: px(key_blur / 2.), |
| 62 | spread_radius: px(0.), |
| 63 | inset: false, |
| 64 | }, |
| 65 | ] |
| 66 | } |
| 67 | |
| 68 | pub fn shadow8() -> Vec<BoxShadow> { |
| 69 | fluent_shadow(2., 4., 8., AMBIENT_INK, KEY_INK) |
| 70 | } |
| 71 | |
| 72 | pub fn shadow16() -> Vec<BoxShadow> { |
| 73 | fluent_shadow(2., 8., 16., AMBIENT_INK, KEY_INK) |
| 74 | } |
| 75 | |
| 76 | pub fn shadow28() -> Vec<BoxShadow> { |
| 77 | fluent_shadow(8., 14., 28., AMBIENT_INK, KEY_INK) |
| 78 | } |
| 79 | |
| 80 | pub fn shadow64() -> Vec<BoxShadow> { |
| 81 | fluent_shadow(8., 32., 64., AMBIENT_INK, KEY_INK) |
| 82 | } |
| 83 | |
| 84 | impl ElevationTokens { |
| 85 | pub fn from_theme(cx: &App) -> Self { |
| 86 | let theme = cx.theme(); |
| 87 | let flyout = Elevation { |
| 88 | fill: theme.popover, |
| 89 | hover: theme.sidebar_accent, |
| 90 | shadow: shadow16(), |
| 91 | }; |
| 92 | let nested = Elevation { |
| 93 | fill: theme.sidebar_accent, |
| 94 | hover: theme.secondary_hover, |
| 95 | shadow: shadow16(), |
| 96 | }; |
| 97 | Self { |
| 98 | flyouts: vec![flyout.clone(), nested], |
| 99 | dialog: Elevation { |
| 100 | shadow: shadow64(), |
| 101 | ..flyout.clone() |
| 102 | }, |
| 103 | toast: Elevation { |
| 104 | shadow: shadow8(), |
| 105 | ..flyout |
| 106 | }, |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn flyout_at(&self, modal_layers: usize) -> &Elevation { |
| 111 | let last = self.flyouts.len().saturating_sub(1); |
| 112 | &self.flyouts[modal_layers.min(last)] |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[derive(Default, Clone, Copy, Debug, PartialEq, Eq)] |
| 117 | struct ModalLayers { |
| 118 | app: usize, |
| 119 | framework: usize, |
| 120 | } |
| 121 | |
| 122 | impl ModalLayers { |
| 123 | fn total(self) -> usize { |
| 124 | self.app + self.framework |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #[derive(Default)] |
| 129 | struct WindowModalLayers(HashMap<WindowId, ModalLayers>); |
| 130 | |
| 131 | impl Global for WindowModalLayers {} |
| 132 | |
| 133 | fn update_layers(window: &Window, cx: &mut App, update: impl FnOnce(&mut ModalLayers)) { |
| 134 | let window_id = window.window_handle().window_id(); |
| 135 | let current = cx |
| 136 | .try_global::<WindowModalLayers>() |
| 137 | .and_then(|layers| layers.0.get(&window_id).copied()) |
| 138 | .unwrap_or_default(); |
| 139 | let mut next = current; |
| 140 | update(&mut next); |
| 141 | if next == current { |
| 142 | return; |
| 143 | } |
| 144 | let layers = cx.default_global::<WindowModalLayers>(); |
| 145 | if next == ModalLayers::default() { |
| 146 | layers.0.remove(&window_id); |
| 147 | } else { |
| 148 | layers.0.insert(window_id, next); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | pub fn set_app_modal_layers(window: &Window, count: usize, cx: &mut App) { |
| 153 | update_layers(window, cx, |layers| layers.app = count); |
| 154 | } |
| 155 | |
| 156 | pub(crate) fn set_framework_modal_layers(window: &Window, count: usize, cx: &mut App) { |
| 157 | update_layers(window, cx, |layers| layers.framework = count); |
| 158 | } |
| 159 | |
| 160 | pub fn modal_layers(window: &Window, cx: &App) -> usize { |
| 161 | let window_id = window.window_handle().window_id(); |
| 162 | cx.try_global::<WindowModalLayers>() |
| 163 | .and_then(|layers| layers.0.get(&window_id).copied()) |
| 164 | .map(ModalLayers::total) |
| 165 | .unwrap_or(0) |
| 166 | } |
| 167 | |
| 168 | pub fn tokens(cx: &App) -> ElevationTokens { |
| 169 | cx.try_global::<ElevationTokens>() |
| 170 | .cloned() |
| 171 | .unwrap_or_else(|| ElevationTokens::from_theme(cx)) |
| 172 | } |
| 173 | |
| 174 | pub fn flyout(window: &Window, cx: &App) -> Elevation { |
| 175 | tokens(cx).flyout_at(modal_layers(window, cx)).clone() |
| 176 | } |
| 177 | |
| 178 | pub fn dialog(cx: &App) -> Elevation { |
| 179 | tokens(cx).dialog |
| 180 | } |
| 181 | |
| 182 | pub fn toast(cx: &App) -> Elevation { |
| 183 | tokens(cx).toast |
| 184 | } |