| 1 | pub use crate::component_traits::{Collapsible, Disableable, Selectable}; |
| 2 | pub use crate::sizing::{Sizable, Size, StyleSized}; |
| 3 | use gpui::{ |
| 4 | App, BoxShadow, Corners, Edges, Hsla, InteractiveElement as _, ParentElement, Pixels, |
| 5 | StyleRefinement, Styled, Window, div, hsla, prelude::FluentBuilder as _, px, |
| 6 | }; |
| 7 | pub use gpui_base::{FocusableExt, RoleOverride, StyledExt, box_shadow, h_flex, v_flex}; |
| 8 | |
| 9 | use crate::ActiveTheme as _; |
| 10 | |
| 11 | const FOCUS_RING_WIDTH: Pixels = px(3.); |
| 12 | const FOCUS_RING_OPACITY: f32 = 0.5; |
| 13 | /// Gap between a borderless element's edge and a focus line drawn off it, in rem. |
| 14 | const FOCUS_LINE_GAP: f32 = 0.125; |
| 15 | |
| 16 | /// Ink every layer of a surface's shadow carries — the `rgb(0 0 0 / 0.1)` |
| 17 | /// shadcn/ui spends at each elevation. |
| 18 | const SURFACE_SHADOW_INK: f32 = 0.1; |
| 19 | |
| 20 | /// Ink of the hairline ring standing in for a popover's border. |
| 21 | /// |
| 22 | /// shadcn/ui draws no border on a popup surface at all: its edge is a 1px |
| 23 | /// `rgb(0 0 0 / 0.1)` ring spent as a shadow layer. Because the ring is |
| 24 | /// translucent the shadow shows *through* it, which is what makes the edge read |
| 25 | /// as part of one grounded surface rather than as an outline with a separate |
| 26 | /// shadow below it. An opaque border cannot reproduce that — a border composites |
| 27 | /// over the element's own background, not over the shadow. |
| 28 | const POPOVER_RING_INK: f32 = 0.1; |
| 29 | |
| 30 | /// The colour of a popup surface's hairline ring in this theme. |
| 31 | /// |
| 32 | /// shadcn spends black on it in light mode and white in dark |
| 33 | /// (`oklch(1 0 0 / 10%)`), so it follows the foreground rather than the border |
| 34 | /// token: a fixed black ring would all but vanish on a dark surface. |
| 35 | /// |
| 36 | pub(crate) fn popover_ring(cx: &App) -> Hsla { |
| 37 | cx.theme().foreground.alpha(POPOVER_RING_INK) |
| 38 | } |
| 39 | |
| 40 | /// shadcn/ui's `shadow-sm`, the elevation it spends on a control raised out of |
| 41 | /// the container it sits in — the active pill of a segmented tab bar — at full |
| 42 | /// ink. |
| 43 | /// |
| 44 | /// Unlike a popover or a toast this surface is not floating over the page: it |
| 45 | /// sits *inside* a trough only a few pixels wider than itself, and that trough |
| 46 | /// clips. Both are reasons to keep the falloff tight — there is no room for a |
| 47 | /// wide one, and a wide one would read as grime against the trough wall rather |
| 48 | /// than as lift. |
| 49 | /// |
| 50 | /// The radii are Tailwind's halved, for the reason [`crate::elevation`] explains: |
| 51 | /// CSS defines a box shadow's blur radius as twice the gaussian's standard |
| 52 | /// deviation, while GPUI's shader takes the field as the deviation itself |
| 53 | /// (`gaussian(y, sigma)`). Copying Tailwind's `3px` and `2px` across therefore |
| 54 | /// spreads the shadow over twice the distance, which is why |
| 55 | /// [`Styled::shadow_sm`] leaves a haze around a 24px pill where shadcn draws a |
| 56 | /// compact line. |
| 57 | pub(crate) fn raised_shadow() -> Vec<BoxShadow> { |
| 58 | let ink = hsla(0., 0., 0., SURFACE_SHADOW_INK); |
| 59 | vec![ |
| 60 | BoxShadow::new(px(0.), px(1.), ink).blur_radius(px(1.5)), |
| 61 | BoxShadow::new(px(0.), px(1.), ink) |
| 62 | .blur_radius(px(1.)) |
| 63 | .spread_radius(px(-1.)), |
| 64 | ] |
| 65 | } |
| 66 | |
| 67 | /// Finished styles that read the theme. |
| 68 | /// |
| 69 | /// Separate from [`StyledExt`], which holds neutral helpers that make no |
| 70 | /// visual decisions. Everything here does: it reaches into the theme and |
| 71 | /// produces a specific look, which is why it belongs above the base layer. |
| 72 | pub trait ThemeStyled: Styled + Sized { |
| 73 | /// Give this element the focus appearance the framework's own controls |
| 74 | /// use: its border tinted with the focus colour, and the ring outside it. |
| 75 | /// |
| 76 | /// The ring is dropped when [`crate::Theme::focus_ring`] is off, leaving |
| 77 | /// the tinted border — an application whose layout clips its containers can |
| 78 | /// turn it off rather than finding room for the ring in each of them. An |
| 79 | /// element without a border draws a 1px line on its edge instead, so |
| 80 | /// borderless controls still show focus. |
| 81 | /// |
| 82 | /// Calling this turns the ring on; gate it with `when` for the conditions |
| 83 | /// that decide whether the control shows one at all — its focus state, |
| 84 | /// [`FocusableExt::focus_ring`], appearance, and so on. |
| 85 | /// |
| 86 | /// The ring sits outside the element's border, so an ancestor that clips |
| 87 | /// its content will cut it off — leave it a few pixels of room, or don't |
| 88 | /// clip. |
| 89 | fn focus_ring_style(self, window: &Window, cx: &App) -> Self |
| 90 | where |
| 91 | Self: ParentElement; |
| 92 | |
| 93 | /// Give this element the surface, shadow and radius of a flyout at the |
| 94 | /// elevation level `window` currently opens flyouts at. |
| 95 | /// |
| 96 | /// This is the one surface every popup shares — Popover, PopupMenu, Select, |
| 97 | /// Combobox, DatePicker and the editor's hover popovers — so they cannot |
| 98 | /// drift apart. See [`crate::elevation`]. |
| 99 | fn popover_style(self, window: &Window, cx: &App) -> Self; |
| 100 | |
| 101 | /// Round this element as far as its size allows — a circle if it is square, |
| 102 | /// a pill if it is not — unless the theme squares its corners. |
| 103 | /// |
| 104 | /// Use this instead of [`gpui::Styled::rounded_full`] on anything the theme |
| 105 | /// owns. A hardcoded `rounded_full` survives [`crate::Theme::radius`] being |
| 106 | /// set to zero, which leaves avatars, badge dots and slider thumbs round in |
| 107 | /// a UI that is square everywhere else. See [`crate::Theme::radius_full`]. |
| 108 | fn rounded_full_style(self, cx: &App) -> Self { |
| 109 | self.rounded(cx.theme().radius_full()) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | impl<T: Styled + Sized> ThemeStyled for T { |
| 114 | /// Draw the focus ring the framework's own controls use. |
| 115 | /// |
| 116 | /// Calling this turns the ring on; gate it with `when` for the conditions |
| 117 | /// that decide whether the control shows one at all — its focus state, |
| 118 | /// [`crate::FocusableExt::focus_ring`], appearance, and so on. |
| 119 | /// |
| 120 | /// The ring sits outside the element's border, so an ancestor that clips |
| 121 | /// its content will cut it off — leave it a few pixels of room, or don't |
| 122 | /// clip. |
| 123 | fn focus_ring_style(self, window: &Window, cx: &App) -> Self |
| 124 | where |
| 125 | Self: ParentElement, |
| 126 | { |
| 127 | focus_style(self, FocusLine::Edge, window, cx) |
| 128 | } |
| 129 | |
| 130 | fn popover_style(self, window: &Window, cx: &App) -> Self { |
| 131 | let theme = cx.theme(); |
| 132 | let elevation = crate::elevation::flyout(window, cx); |
| 133 | self.bg(elevation.fill) |
| 134 | .shadow(elevation.shadow) |
| 135 | .text_color(theme.popover_foreground) |
| 136 | .rounded(theme.radius_lg) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | fn border_widths(style: &StyleRefinement, rem_size: Pixels) -> Edges<Pixels> { |
| 141 | let width = |value: Option<gpui::AbsoluteLength>| { |
| 142 | value.map(|v| v.to_pixels(rem_size)).unwrap_or_default() |
| 143 | }; |
| 144 | let widths = &style.border_widths; |
| 145 | Edges { |
| 146 | top: width(widths.top), |
| 147 | bottom: width(widths.bottom), |
| 148 | left: width(widths.left), |
| 149 | right: width(widths.right), |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | fn corner_radii(style: &StyleRefinement, rem_size: Pixels) -> Corners<Pixels> { |
| 154 | let radius = |value: Option<gpui::AbsoluteLength>| { |
| 155 | value.map(|v| v.to_pixels(rem_size)).unwrap_or_default() |
| 156 | }; |
| 157 | let radii = &style.corner_radii; |
| 158 | Corners { |
| 159 | top_left: radius(radii.top_left), |
| 160 | top_right: radius(radii.top_right), |
| 161 | bottom_left: radius(radii.bottom_left), |
| 162 | bottom_right: radius(radii.bottom_right), |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | fn corner_radii_refinement(radius: Corners<Pixels>) -> StyleRefinement { |
| 167 | let mut style = StyleRefinement::default(); |
| 168 | style.corner_radii.top_left = Some(radius.top_left.into()); |
| 169 | style.corner_radii.top_right = Some(radius.top_right.into()); |
| 170 | style.corner_radii.bottom_left = Some(radius.bottom_left.into()); |
| 171 | style.corner_radii.bottom_right = Some(radius.bottom_right.into()); |
| 172 | style |
| 173 | } |
| 174 | |
| 175 | /// Where a borderless element draws its 1px focus line when |
| 176 | /// [`crate::Theme::focus_ring`] is off. |
| 177 | #[derive(Clone, Copy)] |
| 178 | pub(crate) enum FocusLine { |
| 179 | /// On the element's edge, in the `ring` colour. For elements whose |
| 180 | /// content sits clear of the edge and that have no fill of their own. |
| 181 | Edge, |
| 182 | /// Inset from the edge, in the given colour. For filled elements, where |
| 183 | /// the `ring` colour can land close to the fill; pass a colour that |
| 184 | /// contrasts with it, such as the element's foreground. |
| 185 | Inside(Hsla), |
| 186 | /// Just outside the edge, in the `ring` colour. For elements with no |
| 187 | /// padding, where a line on the edge would touch their text. |
| 188 | Outside, |
| 189 | } |
| 190 | |
| 191 | /// Style a focused element as [`ThemeStyled::focus_ring_style`] does, with |
| 192 | /// `line` choosing where a borderless element draws its focus line. |
| 193 | pub(crate) fn focus_style<T: Styled + ParentElement>( |
| 194 | mut element: T, |
| 195 | line: FocusLine, |
| 196 | window: &Window, |
| 197 | cx: &App, |
| 198 | ) -> T { |
| 199 | let theme = cx.theme(); |
| 200 | if theme.focus_ring { |
| 201 | return focus_ring( |
| 202 | element.border_color(theme.ring), |
| 203 | window, |
| 204 | theme.ring.alpha(FOCUS_RING_OPACITY), |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | // The ring is painted outside the border, so a clipping ancestor cuts it |
| 209 | // off. An application whose layout clips heavily turns it off in the theme |
| 210 | // and keeps the tinted border, which takes no space. |
| 211 | let rem_size = window.rem_size(); |
| 212 | if border_widths(element.style(), rem_size).any(|width| *width > Pixels::ZERO) { |
| 213 | return element.border_color(theme.ring); |
| 214 | } |
| 215 | |
| 216 | let gap = rem_size * FOCUS_LINE_GAP; |
| 217 | let (color, inset) = match line { |
| 218 | FocusLine::Edge => (theme.ring, Pixels::ZERO), |
| 219 | FocusLine::Inside(color) => (color, gap), |
| 220 | FocusLine::Outside => (theme.ring, -gap), |
| 221 | }; |
| 222 | // Shrinking or growing the box by `inset` keeps the line concentric with |
| 223 | // the element's own corners. |
| 224 | let radius = |
| 225 | corner_radii(element.style(), rem_size).map(|value| (*value - inset).max(Pixels::ZERO)); |
| 226 | element.child( |
| 227 | div() |
| 228 | .when(cfg!(test), |this| { |
| 229 | this.debug_selector(|| "focus-ring".into()) |
| 230 | }) |
| 231 | .flex_none() |
| 232 | .absolute() |
| 233 | .top(inset) |
| 234 | .left(inset) |
| 235 | .right(inset) |
| 236 | .bottom(inset) |
| 237 | .border_1() |
| 238 | .border_color(color) |
| 239 | .refine_style(&corner_radii_refinement(radius)), |
| 240 | ) |
| 241 | } |
| 242 | |
| 243 | /// Paint only the outside band, preserving translucent control backgrounds. |
| 244 | pub(crate) fn focus_ring<T: Styled + ParentElement>( |
| 245 | mut element: T, |
| 246 | window: &Window, |
| 247 | color: Hsla, |
| 248 | ) -> T { |
| 249 | let rem_size = window.rem_size(); |
| 250 | let border_widths = border_widths(element.style(), rem_size); |
| 251 | let radius = corner_radii(element.style(), rem_size).map(|value| *value + FOCUS_RING_WIDTH); |
| 252 | let ring_style = corner_radii_refinement(radius); |
| 253 | let inset = FOCUS_RING_WIDTH; |
| 254 | |
| 255 | element.child( |
| 256 | div() |
| 257 | .when(cfg!(test), |this| { |
| 258 | this.debug_selector(|| "focus-ring".into()) |
| 259 | }) |
| 260 | .flex_none() |
| 261 | .absolute() |
| 262 | .top(-(inset + border_widths.top)) |
| 263 | .left(-(inset + border_widths.left)) |
| 264 | .right(-(inset + border_widths.right)) |
| 265 | .bottom(-(inset + border_widths.bottom)) |
| 266 | .border(FOCUS_RING_WIDTH) |
| 267 | .border_color(color) |
| 268 | .refine_style(&ring_style), |
| 269 | ) |
| 270 | } |