1use crate::{
2 phi, point, rems, AbsoluteLength, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners,
3 CornersRefinement, DefiniteLength, Edges, EdgesRefinement, Font, FontFeatures, FontStyle,
4 FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Rems, Result, RunStyle, SharedString,
5 Size, SizeRefinement, ViewContext, WindowContext,
6};
7use refineable::Refineable;
8use smallvec::SmallVec;
9pub use taffy::style::{
10 AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent,
11 Overflow, Position,
12};
13
14#[derive(Clone, Refineable, Debug)]
15#[refineable(debug)]
16pub struct Style {
17 /// What layout strategy should be used?
18 pub display: Display,
19
20 // Overflow properties
21 /// How children overflowing their container should affect layout
22 #[refineable]
23 pub overflow: Point<Overflow>,
24 /// How much space (in points) should be reserved for the scrollbars of `Overflow::Scroll` and `Overflow::Auto` nodes.
25 pub scrollbar_width: f32,
26
27 // Position properties
28 /// What should the `position` value of this struct use as a base offset?
29 pub position: Position,
30 /// How should the position of this element be tweaked relative to the layout defined?
31 #[refineable]
32 pub inset: Edges<Length>,
33
34 // Size properies
35 /// Sets the initial size of the item
36 #[refineable]
37 pub size: Size<Length>,
38 /// Controls the minimum size of the item
39 #[refineable]
40 pub min_size: Size<Length>,
41 /// Controls the maximum size of the item
42 #[refineable]
43 pub max_size: Size<Length>,
44 /// Sets the preferred aspect ratio for the item. The ratio is calculated as width divided by height.
45 pub aspect_ratio: Option<f32>,
46
47 // Spacing Properties
48 /// How large should the margin be on each side?
49 #[refineable]
50 pub margin: Edges<Length>,
51 /// How large should the padding be on each side?
52 #[refineable]
53 pub padding: Edges<DefiniteLength>,
54 /// How large should the border be on each side?
55 #[refineable]
56 pub border_widths: Edges<AbsoluteLength>,
57
58 // Alignment properties
59 /// How this node's children aligned in the cross/block axis?
60 pub align_items: Option<AlignItems>,
61 /// How this node should be aligned in the cross/block axis. Falls back to the parents [`AlignItems`] if not set
62 pub align_self: Option<AlignSelf>,
63 /// How should content contained within this item be aligned in the cross/block axis
64 pub align_content: Option<AlignContent>,
65 /// How should contained within this item be aligned in the main/inline axis
66 pub justify_content: Option<JustifyContent>,
67 /// How large should the gaps between items in a flex container be?
68 #[refineable]
69 pub gap: Size<DefiniteLength>,
70
71 // Flexbox properies
72 /// Which direction does the main axis flow in?
73 pub flex_direction: FlexDirection,
74 /// Should elements wrap, or stay in a single line?
75 pub flex_wrap: FlexWrap,
76 /// Sets the initial main axis size of the item
77 pub flex_basis: Length,
78 /// The relative rate at which this item grows when it is expanding to fill space, 0.0 is the default value, and this value must be positive.
79 pub flex_grow: f32,
80 /// The relative rate at which this item shrinks when it is contracting to fit into space, 1.0 is the default value, and this value must be positive.
81 pub flex_shrink: f32,
82
83 /// The fill color of this element
84 pub fill: Option<Fill>,
85
86 /// The border color of this element
87 pub border_color: Option<Hsla>,
88
89 /// The radius of the corners of this element
90 #[refineable]
91 pub corner_radii: Corners<AbsoluteLength>,
92
93 /// Box Shadow of the element
94 pub box_shadow: SmallVec<[BoxShadow; 2]>,
95
96 /// TEXT
97 pub text: TextStyleRefinement,
98
99 pub z_index: Option<u32>,
100}
101
102#[derive(Clone, Debug)]
103pub struct BoxShadow {
104 pub color: Hsla,
105 pub offset: Point<Pixels>,
106 pub blur_radius: Pixels,
107 pub spread_radius: Pixels,
108}
109
110#[derive(Refineable, Clone, Debug)]
111#[refineable(debug)]
112pub struct TextStyle {
113 pub color: Hsla,
114 pub font_family: SharedString,
115 pub font_features: FontFeatures,
116 pub font_size: Rems,
117 pub line_height: DefiniteLength,
118 pub font_weight: FontWeight,
119 pub font_style: FontStyle,
120 pub underline: Option<UnderlineStyle>,
121}
122
123impl Default for TextStyle {
124 fn default() -> Self {
125 TextStyle {
126 color: Hsla::default(),
127 font_family: SharedString::default(),
128 font_features: FontFeatures::default(),
129 font_size: rems(1.),
130 line_height: phi(),
131 font_weight: FontWeight::default(),
132 font_style: FontStyle::default(),
133 underline: None,
134 }
135 }
136}
137
138impl TextStyle {
139 pub fn highlight(mut self, style: HighlightStyle) -> Result<Self> {
140 if let Some(weight) = style.font_weight {
141 self.font_weight = weight;
142 }
143 if let Some(style) = style.font_style {
144 self.font_style = style;
145 }
146
147 if let Some(color) = style.color {
148 self.color = self.color.blend(color);
149 }
150
151 if let Some(factor) = style.fade_out {
152 self.color.fade_out(factor);
153 }
154
155 if let Some(underline) = style.underline {
156 self.underline = Some(underline);
157 }
158
159 Ok(self)
160 }
161
162 pub fn to_run(&self) -> RunStyle {
163 RunStyle {
164 font: Font {
165 family: self.font_family.clone(),
166 features: Default::default(),
167 weight: self.font_weight,
168 style: self.font_style,
169 },
170 color: self.color,
171 underline: self.underline.clone(),
172 }
173 }
174}
175
176#[derive(Clone, Debug, Default, PartialEq)]
177pub struct HighlightStyle {
178 pub color: Option<Hsla>,
179 pub font_weight: Option<FontWeight>,
180 pub font_style: Option<FontStyle>,
181 pub underline: Option<UnderlineStyle>,
182 pub fade_out: Option<f32>,
183}
184
185impl Eq for HighlightStyle {}
186
187impl Style {
188 pub fn text_style(&self, _cx: &WindowContext) -> Option<&TextStyleRefinement> {
189 if self.text.is_some() {
190 Some(&self.text)
191 } else {
192 None
193 }
194 }
195
196 pub fn apply_text_style<C, F, R>(&self, cx: &mut C, f: F) -> R
197 where
198 C: BorrowAppContext,
199 F: FnOnce(&mut C) -> R,
200 {
201 if self.text.is_some() {
202 cx.with_text_style(self.text.clone(), f)
203 } else {
204 f(cx)
205 }
206 }
207
208 /// Apply overflow to content mask
209 pub fn apply_overflow<C, F, R>(&self, bounds: Bounds<Pixels>, cx: &mut C, f: F) -> R
210 where
211 C: BorrowWindow,
212 F: FnOnce(&mut C) -> R,
213 {
214 let current_mask = cx.content_mask();
215
216 let min = current_mask.bounds.origin;
217 let max = current_mask.bounds.lower_right();
218
219 let mask_bounds = match (
220 self.overflow.x == Overflow::Visible,
221 self.overflow.y == Overflow::Visible,
222 ) {
223 // x and y both visible
224 (true, true) => return f(cx),
225 // x visible, y hidden
226 (true, false) => Bounds::from_corners(
227 point(min.x, bounds.origin.y),
228 point(max.x, bounds.lower_right().y),
229 ),
230 // x hidden, y visible
231 (false, true) => Bounds::from_corners(
232 point(bounds.origin.x, min.y),
233 point(bounds.lower_right().x, max.y),
234 ),
235 // both hidden
236 (false, false) => bounds,
237 };
238 let mask = ContentMask {
239 bounds: mask_bounds,
240 };
241
242 cx.with_content_mask(mask, f)
243 }
244
245 /// Paints the background of an element styled with this style.
246 pub fn paint<V: 'static>(&self, bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {
247 let rem_size = cx.rem_size();
248
249 cx.stack(0, |cx| {
250 cx.paint_shadows(
251 bounds,
252 self.corner_radii.to_pixels(bounds.size, rem_size),
253 &self.box_shadow,
254 );
255 });
256
257 let background_color = self.fill.as_ref().and_then(Fill::color);
258 if background_color.is_some() || self.is_border_visible() {
259 cx.stack(1, |cx| {
260 cx.paint_quad(
261 bounds,
262 self.corner_radii.to_pixels(bounds.size, rem_size),
263 background_color.unwrap_or_default(),
264 self.border_widths.to_pixels(rem_size),
265 self.border_color.unwrap_or_default(),
266 );
267 });
268 }
269 }
270
271 fn is_border_visible(&self) -> bool {
272 self.border_color
273 .map_or(false, |color| !color.is_transparent())
274 && self.border_widths.any(|length| !length.is_zero())
275 }
276}
277
278impl Default for Style {
279 fn default() -> Self {
280 Style {
281 display: Display::Block,
282 overflow: Point {
283 x: Overflow::Visible,
284 y: Overflow::Visible,
285 },
286 scrollbar_width: 0.0,
287 position: Position::Relative,
288 inset: Edges::auto(),
289 margin: Edges::<Length>::zero(),
290 padding: Edges::<DefiniteLength>::zero(),
291 border_widths: Edges::<AbsoluteLength>::zero(),
292 size: Size::auto(),
293 min_size: Size::auto(),
294 max_size: Size::auto(),
295 aspect_ratio: None,
296 gap: Size::zero(),
297 // Aligment
298 align_items: None,
299 align_self: None,
300 align_content: None,
301 justify_content: None,
302 // Flexbox
303 flex_direction: FlexDirection::Row,
304 flex_wrap: FlexWrap::NoWrap,
305 flex_grow: 0.0,
306 flex_shrink: 1.0,
307 flex_basis: Length::Auto,
308 fill: None,
309 border_color: None,
310 corner_radii: Corners::default(),
311 box_shadow: Default::default(),
312 text: TextStyleRefinement::default(),
313 z_index: None,
314 }
315 }
316}
317
318#[derive(Refineable, Clone, Default, Debug, PartialEq, Eq)]
319#[refineable(debug)]
320pub struct UnderlineStyle {
321 pub thickness: Pixels,
322 pub color: Option<Hsla>,
323 pub wavy: bool,
324}
325
326#[derive(Clone, Debug)]
327pub enum Fill {
328 Color(Hsla),
329}
330
331impl Fill {
332 pub fn color(&self) -> Option<Hsla> {
333 match self {
334 Fill::Color(color) => Some(*color),
335 }
336 }
337}
338
339impl Default for Fill {
340 fn default() -> Self {
341 Self::Color(Hsla::default())
342 }
343}
344
345impl From<Hsla> for Fill {
346 fn from(color: Hsla) -> Self {
347 Self::Color(color)
348 }
349}
350
351impl From<TextStyle> for HighlightStyle {
352 fn from(other: TextStyle) -> Self {
353 Self::from(&other)
354 }
355}
356
357impl From<&TextStyle> for HighlightStyle {
358 fn from(other: &TextStyle) -> Self {
359 Self {
360 color: Some(other.color),
361 font_weight: Some(other.font_weight),
362 font_style: Some(other.font_style),
363 underline: other.underline.clone(),
364 fade_out: None,
365 }
366 }
367}
368
369impl HighlightStyle {
370 pub fn highlight(&mut self, other: HighlightStyle) {
371 match (self.color, other.color) {
372 (Some(self_color), Some(other_color)) => {
373 self.color = Some(Hsla::blend(other_color, self_color));
374 }
375 (None, Some(other_color)) => {
376 self.color = Some(other_color);
377 }
378 _ => {}
379 }
380
381 if other.font_weight.is_some() {
382 self.font_weight = other.font_weight;
383 }
384
385 if other.font_style.is_some() {
386 self.font_style = other.font_style;
387 }
388
389 if other.underline.is_some() {
390 self.underline = other.underline;
391 }
392
393 match (other.fade_out, self.fade_out) {
394 (Some(source_fade), None) => self.fade_out = Some(source_fade),
395 (Some(source_fade), Some(dest_fade)) => {
396 self.fade_out = Some((dest_fade * (1. + source_fade)).clamp(0., 1.));
397 }
398 _ => {}
399 }
400 }
401}
402
403impl From<Hsla> for HighlightStyle {
404 fn from(color: Hsla) -> Self {
405 Self {
406 color: Some(color),
407 ..Default::default()
408 }
409 }
410}