1use gpui::{
2 App, BoxShadow, Corners, DefiniteLength, Div, Edges, FocusHandle, Hsla, Pixels,
3 Refineable as _, Role, StyleRefinement, Styled, Window, div, hsla, point,
4};
5
6#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
7pub enum RoleOverride {
8 #[default]
9 Implicit,
10 Presentational,
11 Role(Role),
12}
13
14impl RoleOverride {
15 pub fn resolve(self, default: impl FnOnce() -> Role) -> Option<Role> {
16 match self {
17 Self::Implicit => Some(default()),
18 Self::Presentational => None,
19 Self::Role(role) => Some(role),
20 }
21 }
22}
23impl From<Role> for RoleOverride {
24 fn from(role: Role) -> Self {
25 Self::Role(role)
26 }
27}
28impl From<Option<Role>> for RoleOverride {
29 fn from(role: Option<Role>) -> Self {
30 role.map_or(Self::Presentational, Self::Role)
31 }
32}
33
34pub fn h_flex() -> Div {
35 div().h_flex()
36}
37pub fn v_flex() -> Div {
38 div().v_flex()
39}
40
41pub fn box_shadow(
42 x: impl Into<Pixels>,
43 y: impl Into<Pixels>,
44 blur: impl Into<Pixels>,
45 spread: impl Into<Pixels>,
46 color: Hsla,
47) -> BoxShadow {
48 BoxShadow {
49 offset: point(x.into(), y.into()),
50 blur_radius: blur.into(),
51 spread_radius: spread.into(),
52 inset: false,
53 color,
54 }
55}
56
57macro_rules! font_weight {
58 ($method:ident, $weight:ident) => {
59 fn $method(self) -> Self {
60 self.font_weight(gpui::FontWeight::$weight)
61 }
62 };
63}
64
65#[cfg_attr(
66 any(feature = "inspector", debug_assertions),
67 gpui_macros::derive_inspector_reflection
68)]
69pub trait StyledExt: Styled + Sized {
70 fn refine_style(mut self, style: &StyleRefinement) -> Self {
71 self.style().refine(style);
72 self
73 }
74
75 fn h_flex(self) -> Self {
76 self.flex().flex_row().items_center()
77 }
78
79 fn v_flex(self) -> Self {
80 self.flex().flex_col()
81 }
82
83 fn paddings<L>(self, paddings: impl Into<Edges<L>>) -> Self
84 where
85 L: Into<DefiniteLength> + Clone + Default + std::fmt::Debug + PartialEq,
86 {
87 let paddings = paddings.into();
88 self.pt(paddings.top.into())
89 .pb(paddings.bottom.into())
90 .pl(paddings.left.into())
91 .pr(paddings.right.into())
92 }
93
94 fn margins<L>(self, margins: impl Into<Edges<L>>) -> Self
95 where
96 L: Into<DefiniteLength> + Clone + Default + std::fmt::Debug + PartialEq,
97 {
98 let margins = margins.into();
99 self.mt(margins.top.into())
100 .mb(margins.bottom.into())
101 .ml(margins.left.into())
102 .mr(margins.right.into())
103 }
104
105 fn debug_red(self) -> Self {
106 if cfg!(debug_assertions) {
107 self.border_1().border_color(hsl(0., 72.2, 50.6))
108 } else {
109 self
110 }
111 }
112
113 fn debug_blue(self) -> Self {
114 if cfg!(debug_assertions) {
115 self.border_1().border_color(hsl(217.2, 91.2, 59.8))
116 } else {
117 self
118 }
119 }
120
121 fn debug_yellow(self) -> Self {
122 if cfg!(debug_assertions) {
123 self.border_1().border_color(hsl(47.9, 95.8, 53.1))
124 } else {
125 self
126 }
127 }
128
129 fn debug_green(self) -> Self {
130 if cfg!(debug_assertions) {
131 self.border_1().border_color(hsl(142.1, 70.6, 45.3))
132 } else {
133 self
134 }
135 }
136
137 fn debug_pink(self) -> Self {
138 if cfg!(debug_assertions) {
139 self.border_1().border_color(hsl(330.4, 81.2, 60.4))
140 } else {
141 self
142 }
143 }
144
145 fn debug_focused(self, focus_handle: &FocusHandle, window: &Window, cx: &App) -> Self {
146 if cfg!(debug_assertions) && focus_handle.contains_focused(window, cx) {
147 self.debug_blue()
148 } else {
149 self
150 }
151 }
152
153 font_weight!(font_thin, THIN);
154 font_weight!(font_extralight, EXTRA_LIGHT);
155 font_weight!(font_light, LIGHT);
156 font_weight!(font_normal, NORMAL);
157 font_weight!(font_medium, MEDIUM);
158 font_weight!(font_semibold, SEMIBOLD);
159 font_weight!(font_bold, BOLD);
160 font_weight!(font_extrabold, EXTRA_BOLD);
161 font_weight!(font_black, BLACK);
162
163 fn corner_radii(self, radius: Corners<Pixels>) -> Self {
164 self.rounded_tl(radius.top_left)
165 .rounded_tr(radius.top_right)
166 .rounded_bl(radius.bottom_left)
167 .rounded_br(radius.bottom_right)
168 }
169}
170
171impl<E: Styled> StyledExt for E {}
172
173#[cfg(any(feature = "inspector", debug_assertions))]
174pub fn styled_ext_reflection_methods<T: Styled + 'static>()
175-> Vec<gpui::inspector_reflection::FunctionReflection<T>> {
176 styled_ext_reflection::methods::<T>()
177}
178
179fn hsl(hue: f32, saturation: f32, lightness: f32) -> Hsla {
180 hsla(hue / 360., saturation / 100., lightness / 100., 1.)
181}