| 1 | use crate::{ |
| 2 | self as gpui, AbsoluteLength, AlignContent, AlignItems, AlignSelf, BorderStyle, CursorStyle, |
| 3 | DefiniteLength, Display, Fill, FlexDirection, FlexWrap, Font, FontFeatures, FontStyle, |
| 4 | FontWeight, GridPlacement, GridTemplate, GridTemplateMinSize, Hsla, JustifyContent, Length, |
| 5 | SharedString, StrikethroughStyle, StyleRefinement, TextAlign, TextOverflow, |
| 6 | TextStyleRefinement, UnderlineStyle, WhiteSpace, px, relative, rems, |
| 7 | }; |
| 8 | pub use gpui_macros::{ |
| 9 | border_style_methods, box_shadow_style_methods, cursor_style_methods, margin_style_methods, |
| 10 | overflow_style_methods, padding_style_methods, position_style_methods, |
| 11 | visibility_style_methods, |
| 12 | }; |
| 13 | const ELLIPSIS: SharedString = SharedString::new_static("…"); |
| 14 | |
| 15 | /// A trait for elements that can be styled. |
| 16 | /// Use this to opt-in to a utility CSS-like styling API. |
| 17 | // gate on rust-analyzer so rust-analyzer never needs to expand this macro, it takes up to 10 seconds to expand due to inefficiencies in rust-analyzers proc-macro srv |
| 18 | #[cfg_attr( |
| 19 | all(any(feature = "inspector", debug_assertions), not(rust_analyzer)), |
| 20 | gpui_macros::derive_inspector_reflection |
| 21 | )] |
| 22 | pub trait Styled: Sized { |
| 23 | /// Returns a reference to the style memory of this element. |
| 24 | fn style(&mut self) -> &mut StyleRefinement; |
| 25 | |
| 26 | gpui_macros::style_helpers!(); |
| 27 | gpui_macros::visibility_style_methods!(); |
| 28 | gpui_macros::margin_style_methods!(); |
| 29 | gpui_macros::padding_style_methods!(); |
| 30 | gpui_macros::position_style_methods!(); |
| 31 | gpui_macros::overflow_style_methods!(); |
| 32 | gpui_macros::cursor_style_methods!(); |
| 33 | gpui_macros::border_style_methods!(); |
| 34 | gpui_macros::box_shadow_style_methods!(); |
| 35 | |
| 36 | /// Sets the display type of the element to `block`. |
| 37 | /// [Docs](https://tailwindcss.com/docs/display) |
| 38 | fn block(mut self) -> Self { |
| 39 | self.style().display = Some(Display::Block); |
| 40 | self |
| 41 | } |
| 42 | |
| 43 | /// Sets the display type of the element to `flex`. |
| 44 | /// [Docs](https://tailwindcss.com/docs/display) |
| 45 | fn flex(mut self) -> Self { |
| 46 | self.style().display = Some(Display::Flex); |
| 47 | self |
| 48 | } |
| 49 | |
| 50 | /// Sets the display type of the element to `grid`. |
| 51 | /// [Docs](https://tailwindcss.com/docs/display) |
| 52 | fn grid(mut self) -> Self { |
| 53 | self.style().display = Some(Display::Grid); |
| 54 | self |
| 55 | } |
| 56 | |
| 57 | /// Sets the display type of the element to `none`. |
| 58 | /// [Docs](https://tailwindcss.com/docs/display) |
| 59 | fn hidden(mut self) -> Self { |
| 60 | self.style().display = Some(Display::None); |
| 61 | self |
| 62 | } |
| 63 | |
| 64 | /// Set the space to be reserved for rendering the scrollbar. |
| 65 | /// |
| 66 | /// This will only affect the layout of the element when overflow for this element is set to |
| 67 | /// `Overflow::Scroll`. |
| 68 | fn scrollbar_width(mut self, width: impl Into<AbsoluteLength>) -> Self { |
| 69 | self.style().scrollbar_width = Some(width.into()); |
| 70 | self |
| 71 | } |
| 72 | |
| 73 | /// Sets the whitespace of the element to `normal`. |
| 74 | /// [Docs](https://tailwindcss.com/docs/whitespace#normal) |
| 75 | fn whitespace_normal(mut self) -> Self { |
| 76 | self.text_style().white_space = Some(WhiteSpace::Normal); |
| 77 | self |
| 78 | } |
| 79 | |
| 80 | /// Sets the whitespace of the element to `nowrap`. |
| 81 | /// [Docs](https://tailwindcss.com/docs/whitespace#nowrap) |
| 82 | fn whitespace_nowrap(mut self) -> Self { |
| 83 | self.text_style().white_space = Some(WhiteSpace::Nowrap); |
| 84 | self |
| 85 | } |
| 86 | |
| 87 | /// Sets the truncate overflowing text with an ellipsis (…) at the end if needed. |
| 88 | /// [Docs](https://tailwindcss.com/docs/text-overflow#ellipsis) |
| 89 | fn text_ellipsis(mut self) -> Self { |
| 90 | self.text_style().text_overflow = Some(TextOverflow::Truncate(ELLIPSIS)); |
| 91 | self |
| 92 | } |
| 93 | |
| 94 | /// Sets the truncate overflowing text with an ellipsis (…) at the start if needed. |
| 95 | /// Typically more adequate for file paths where the end is more important than the beginning. |
| 96 | /// Note: This doesn't exist in Tailwind CSS. |
| 97 | fn text_ellipsis_start(mut self) -> Self { |
| 98 | self.text_style().text_overflow = Some(TextOverflow::TruncateStart(ELLIPSIS)); |
| 99 | self |
| 100 | } |
| 101 | |
| 102 | /// Sets the truncate overflowing text with an ellipsis (…) in the middle if needed. |
| 103 | /// Preserves the beginning and end of the text. Useful for filenames. |
| 104 | /// Note: This doesn't exist in Tailwind CSS. |
| 105 | fn text_ellipsis_middle(mut self) -> Self { |
| 106 | self.text_style().text_overflow = Some(TextOverflow::TruncateMiddle(ELLIPSIS)); |
| 107 | self |
| 108 | } |
| 109 | |
| 110 | /// Sets the text overflow behavior of the element. |
| 111 | fn text_overflow(mut self, overflow: TextOverflow) -> Self { |
| 112 | self.text_style().text_overflow = Some(overflow); |
| 113 | self |
| 114 | } |
| 115 | |
| 116 | /// Set the text alignment of the element. |
| 117 | fn text_align(mut self, align: TextAlign) -> Self { |
| 118 | self.text_style().text_align = Some(align); |
| 119 | self |
| 120 | } |
| 121 | |
| 122 | /// Sets the text alignment to left |
| 123 | fn text_left(mut self) -> Self { |
| 124 | self.text_align(TextAlign::Left) |
| 125 | } |
| 126 | |
| 127 | /// Sets the text alignment to center |
| 128 | fn text_center(mut self) -> Self { |
| 129 | self.text_align(TextAlign::Center) |
| 130 | } |
| 131 | |
| 132 | /// Sets the text alignment to right |
| 133 | fn text_right(mut self) -> Self { |
| 134 | self.text_align(TextAlign::Right) |
| 135 | } |
| 136 | |
| 137 | /// Sets the truncate to prevent text from wrapping and truncate overflowing text with an ellipsis (…) if needed. |
| 138 | /// [Docs](https://tailwindcss.com/docs/text-overflow#truncate) |
| 139 | fn truncate(mut self) -> Self { |
| 140 | self.overflow_hidden().whitespace_nowrap().text_ellipsis() |
| 141 | } |
| 142 | |
| 143 | /// Sets number of lines to show before truncating the text. |
| 144 | /// [Docs](https://tailwindcss.com/docs/line-clamp) |
| 145 | fn line_clamp(mut self, lines: usize) -> Self { |
| 146 | let mut text_style = self.text_style(); |
| 147 | text_style.line_clamp = Some(lines); |
| 148 | self.overflow_hidden() |
| 149 | } |
| 150 | |
| 151 | /// Sets the flex direction of the element to `column`. |
| 152 | /// [Docs](https://tailwindcss.com/docs/flex-direction#column) |
| 153 | fn flex_col(mut self) -> Self { |
| 154 | self.style().flex_direction = Some(FlexDirection::Column); |
| 155 | self |
| 156 | } |
| 157 | |
| 158 | /// Sets the flex direction of the element to `column-reverse`. |
| 159 | /// [Docs](https://tailwindcss.com/docs/flex-direction#column-reverse) |
| 160 | fn flex_col_reverse(mut self) -> Self { |
| 161 | self.style().flex_direction = Some(FlexDirection::ColumnReverse); |
| 162 | self |
| 163 | } |
| 164 | |
| 165 | /// Sets the flex direction of the element to `row`. |
| 166 | /// [Docs](https://tailwindcss.com/docs/flex-direction#row) |
| 167 | fn flex_row(mut self) -> Self { |
| 168 | self.style().flex_direction = Some(FlexDirection::Row); |
| 169 | self |
| 170 | } |
| 171 | |
| 172 | /// Sets the flex direction of the element to `row-reverse`. |
| 173 | /// [Docs](https://tailwindcss.com/docs/flex-direction#row-reverse) |
| 174 | fn flex_row_reverse(mut self) -> Self { |
| 175 | self.style().flex_direction = Some(FlexDirection::RowReverse); |
| 176 | self |
| 177 | } |
| 178 | |
| 179 | /// Sets the element to allow a flex item to grow and shrink as needed, ignoring its initial size. |
| 180 | /// [Docs](https://tailwindcss.com/docs/flex#flex-1) |
| 181 | fn flex_1(mut self) -> Self { |
| 182 | self.style().flex_grow = Some(1.); |
| 183 | self.style().flex_shrink = Some(1.); |
| 184 | self.style().flex_basis = Some(relative(0.).into()); |
| 185 | self |
| 186 | } |
| 187 | |
| 188 | /// Sets the element to allow a flex item to grow and shrink, taking into account its initial size. |
| 189 | /// [Docs](https://tailwindcss.com/docs/flex#auto) |
| 190 | fn flex_auto(mut self) -> Self { |
| 191 | self.style().flex_grow = Some(1.); |
| 192 | self.style().flex_shrink = Some(1.); |
| 193 | self.style().flex_basis = Some(Length::Auto); |
| 194 | self |
| 195 | } |
| 196 | |
| 197 | /// Sets the element to allow a flex item to shrink but not grow, taking into account its initial size. |
| 198 | /// [Docs](https://tailwindcss.com/docs/flex#initial) |
| 199 | fn flex_initial(mut self) -> Self { |
| 200 | self.style().flex_grow = Some(0.); |
| 201 | self.style().flex_shrink = Some(1.); |
| 202 | self.style().flex_basis = Some(Length::Auto); |
| 203 | self |
| 204 | } |
| 205 | |
| 206 | /// Sets the element to prevent a flex item from growing or shrinking. |
| 207 | /// [Docs](https://tailwindcss.com/docs/flex#none) |
| 208 | fn flex_none(mut self) -> Self { |
| 209 | self.style().flex_grow = Some(0.); |
| 210 | self.style().flex_shrink = Some(0.); |
| 211 | self.style().flex_basis = Some(Length::Auto); |
| 212 | self |
| 213 | } |
| 214 | |
| 215 | /// Sets the initial size of flex items for this element. |
| 216 | /// [Docs](https://tailwindcss.com/docs/flex-basis) |
| 217 | fn flex_basis(mut self, basis: impl Into<Length>) -> Self { |
| 218 | self.style().flex_basis = Some(basis.into()); |
| 219 | self |
| 220 | } |
| 221 | |
| 222 | /// Sets the flex item's grow factor. |
| 223 | /// [Docs](https://tailwindcss.com/docs/flex-grow) |
| 224 | fn flex_grow(mut self, grow: f32) -> Self { |
| 225 | self.style().flex_grow = Some(grow); |
| 226 | self |
| 227 | } |
| 228 | |
| 229 | /// Disables flex item growth (flex-grow: 0). |
| 230 | /// [Docs](https://tailwindcss.com/docs/flex-grow#dont-grow) |
| 231 | fn flex_grow_0(mut self) -> Self { |
| 232 | self.style().flex_grow = Some(0.); |
| 233 | self |
| 234 | } |
| 235 | |
| 236 | /// Enables flex item growth (flex-grow: 1). |
| 237 | /// [Docs](https://tailwindcss.com/docs/flex-grow#grow-1) |
| 238 | fn flex_grow_1(mut self) -> Self { |
| 239 | self.style().flex_grow = Some(1.); |
| 240 | self |
| 241 | } |
| 242 | |
| 243 | /// Sets the flex item's shrink factor. |
| 244 | /// [Docs](https://tailwindcss.com/docs/flex-shrink) |
| 245 | fn flex_shrink(mut self, shrink: f32) -> Self { |
| 246 | self.style().flex_shrink = Some(shrink); |
| 247 | self |
| 248 | } |
| 249 | |
| 250 | /// Disables flex item shrinking (flex-shrink: 0). |
| 251 | /// [Docs](https://tailwindcss.com/docs/flex-shrink#dont-shrink) |
| 252 | fn flex_shrink_0(mut self) -> Self { |
| 253 | self.style().flex_shrink = Some(0.); |
| 254 | self |
| 255 | } |
| 256 | |
| 257 | /// Enables flex item shrinking (flex-shrink: 1). |
| 258 | /// [Docs](https://tailwindcss.com/docs/flex-shrink#shrink-1) |
| 259 | fn flex_shrink_1(mut self) -> Self { |
| 260 | self.style().flex_shrink = Some(1.); |
| 261 | self |
| 262 | } |
| 263 | |
| 264 | /// Sets the element to allow flex items to wrap. |
| 265 | /// [Docs](https://tailwindcss.com/docs/flex-wrap#wrap-normally) |
| 266 | fn flex_wrap(mut self) -> Self { |
| 267 | self.style().flex_wrap = Some(FlexWrap::Wrap); |
| 268 | self |
| 269 | } |
| 270 | |
| 271 | /// Sets the element wrap flex items in the reverse direction. |
| 272 | /// [Docs](https://tailwindcss.com/docs/flex-wrap#wrap-reversed) |
| 273 | fn flex_wrap_reverse(mut self) -> Self { |
| 274 | self.style().flex_wrap = Some(FlexWrap::WrapReverse); |
| 275 | self |
| 276 | } |
| 277 | |
| 278 | /// Sets the element to prevent flex items from wrapping, causing inflexible items to overflow the container if necessary. |
| 279 | /// [Docs](https://tailwindcss.com/docs/flex-wrap#dont-wrap) |
| 280 | fn flex_nowrap(mut self) -> Self { |
| 281 | self.style().flex_wrap = Some(FlexWrap::NoWrap); |
| 282 | self |
| 283 | } |
| 284 | |
| 285 | /// Sets the element to align flex items to the start of the container's cross axis. |
| 286 | /// [Docs](https://tailwindcss.com/docs/align-items#start) |
| 287 | fn items_start(mut self) -> Self { |
| 288 | self.style().align_items = Some(AlignItems::FlexStart); |
| 289 | self |
| 290 | } |
| 291 | |
| 292 | /// Sets the element to align flex items to the end of the container's cross axis. |
| 293 | /// [Docs](https://tailwindcss.com/docs/align-items#end) |
| 294 | fn items_end(mut self) -> Self { |
| 295 | self.style().align_items = Some(AlignItems::FlexEnd); |
| 296 | self |
| 297 | } |
| 298 | |
| 299 | /// Sets the element to align flex items along the center of the container's cross axis. |
| 300 | /// [Docs](https://tailwindcss.com/docs/align-items#center) |
| 301 | fn items_center(mut self) -> Self { |
| 302 | self.style().align_items = Some(AlignItems::Center); |
| 303 | self |
| 304 | } |
| 305 | |
| 306 | /// Sets the element to align flex items along the baseline of the container's cross axis. |
| 307 | /// [Docs](https://tailwindcss.com/docs/align-items#baseline) |
| 308 | fn items_baseline(mut self) -> Self { |
| 309 | self.style().align_items = Some(AlignItems::Baseline); |
| 310 | self |
| 311 | } |
| 312 | |
| 313 | /// Sets the element to stretch flex items to fill the available space along the container's cross axis. |
| 314 | /// [Docs](https://tailwindcss.com/docs/align-items#stretch) |
| 315 | fn items_stretch(mut self) -> Self { |
| 316 | self.style().align_items = Some(AlignItems::Stretch); |
| 317 | self |
| 318 | } |
| 319 | |
| 320 | /// Sets how this specific element is aligned along the container's cross axis. |
| 321 | /// [Docs](https://tailwindcss.com/docs/align-self#start) |
| 322 | fn self_start(mut self) -> Self { |
| 323 | self.style().align_self = Some(AlignSelf::Start); |
| 324 | self |
| 325 | } |
| 326 | |
| 327 | /// Sets this element to align against the end of the container's cross axis. |
| 328 | /// [Docs](https://tailwindcss.com/docs/align-self#end) |
| 329 | fn self_end(mut self) -> Self { |
| 330 | self.style().align_self = Some(AlignSelf::End); |
| 331 | self |
| 332 | } |
| 333 | |
| 334 | /// Sets this element to align against the start of the container's cross axis. |
| 335 | /// [Docs](https://tailwindcss.com/docs/align-self#start) |
| 336 | fn self_flex_start(mut self) -> Self { |
| 337 | self.style().align_self = Some(AlignSelf::FlexStart); |
| 338 | self |
| 339 | } |
| 340 | |
| 341 | /// Sets this element to align against the end of the container's cross axis. |
| 342 | /// [Docs](https://tailwindcss.com/docs/align-self#end) |
| 343 | fn self_flex_end(mut self) -> Self { |
| 344 | self.style().align_self = Some(AlignSelf::FlexEnd); |
| 345 | self |
| 346 | } |
| 347 | |
| 348 | /// Sets this element to align along the center of the container's cross axis. |
| 349 | /// [Docs](https://tailwindcss.com/docs/align-self#center) |
| 350 | fn self_center(mut self) -> Self { |
| 351 | self.style().align_self = Some(AlignSelf::Center); |
| 352 | self |
| 353 | } |
| 354 | |
| 355 | /// Sets this element to align along the baseline of the container's cross axis. |
| 356 | /// [Docs](https://tailwindcss.com/docs/align-self#baseline) |
| 357 | fn self_baseline(mut self) -> Self { |
| 358 | self.style().align_self = Some(AlignSelf::Baseline); |
| 359 | self |
| 360 | } |
| 361 | |
| 362 | /// Sets this element to stretch to fill the available space along the container's cross axis. |
| 363 | /// [Docs](https://tailwindcss.com/docs/align-self#stretch) |
| 364 | fn self_stretch(mut self) -> Self { |
| 365 | self.style().align_self = Some(AlignSelf::Stretch); |
| 366 | self |
| 367 | } |
| 368 | |
| 369 | /// Sets the element to justify flex items against the start of the container's main axis. |
| 370 | /// [Docs](https://tailwindcss.com/docs/justify-content#start) |
| 371 | fn justify_start(mut self) -> Self { |
| 372 | self.style().justify_content = Some(JustifyContent::Start); |
| 373 | self |
| 374 | } |
| 375 | |
| 376 | /// Sets the element to justify flex items against the end of the container's main axis. |
| 377 | /// [Docs](https://tailwindcss.com/docs/justify-content#end) |
| 378 | fn justify_end(mut self) -> Self { |
| 379 | self.style().justify_content = Some(JustifyContent::End); |
| 380 | self |
| 381 | } |
| 382 | |
| 383 | /// Sets the element to justify flex items along the center of the container's main axis. |
| 384 | /// [Docs](https://tailwindcss.com/docs/justify-content#center) |
| 385 | fn justify_center(mut self) -> Self { |
| 386 | self.style().justify_content = Some(JustifyContent::Center); |
| 387 | self |
| 388 | } |
| 389 | |
| 390 | /// Sets the element to justify flex items along the container's main axis |
| 391 | /// such that there is an equal amount of space between each item. |
| 392 | /// [Docs](https://tailwindcss.com/docs/justify-content#space-between) |
| 393 | fn justify_between(mut self) -> Self { |
| 394 | self.style().justify_content = Some(JustifyContent::SpaceBetween); |
| 395 | self |
| 396 | } |
| 397 | |
| 398 | /// Sets the element to justify items along the container's main axis such |
| 399 | /// that there is an equal amount of space on each side of each item. |
| 400 | /// [Docs](https://tailwindcss.com/docs/justify-content#space-around) |
| 401 | fn justify_around(mut self) -> Self { |
| 402 | self.style().justify_content = Some(JustifyContent::SpaceAround); |
| 403 | self |
| 404 | } |
| 405 | |
| 406 | /// Sets the element to justify items along the container's main axis such |
| 407 | /// that there is an equal amount of space around each item, but also |
| 408 | /// accounting for the doubling of space you would normally see between |
| 409 | /// each item when using justify-around. |
| 410 | /// [Docs](https://tailwindcss.com/docs/justify-content#space-evenly) |
| 411 | fn justify_evenly(mut self) -> Self { |
| 412 | self.style().justify_content = Some(JustifyContent::SpaceEvenly); |
| 413 | self |
| 414 | } |
| 415 | |
| 416 | /// Sets the element to pack content items in their default position as if no align-content value was set. |
| 417 | /// [Docs](https://tailwindcss.com/docs/align-content#normal) |
| 418 | fn content_normal(mut self) -> Self { |
| 419 | self.style().align_content = None; |
| 420 | self |
| 421 | } |
| 422 | |
| 423 | /// Sets the element to pack content items in the center of the container's cross axis. |
| 424 | /// [Docs](https://tailwindcss.com/docs/align-content#center) |
| 425 | fn content_center(mut self) -> Self { |
| 426 | self.style().align_content = Some(AlignContent::Center); |
| 427 | self |
| 428 | } |
| 429 | |
| 430 | /// Sets the element to pack content items against the start of the container's cross axis. |
| 431 | /// [Docs](https://tailwindcss.com/docs/align-content#start) |
| 432 | fn content_start(mut self) -> Self { |
| 433 | self.style().align_content = Some(AlignContent::FlexStart); |
| 434 | self |
| 435 | } |
| 436 | |
| 437 | /// Sets the element to pack content items against the end of the container's cross axis. |
| 438 | /// [Docs](https://tailwindcss.com/docs/align-content#end) |
| 439 | fn content_end(mut self) -> Self { |
| 440 | self.style().align_content = Some(AlignContent::FlexEnd); |
| 441 | self |
| 442 | } |
| 443 | |
| 444 | /// Sets the element to pack content items along the container's cross axis |
| 445 | /// such that there is an equal amount of space between each item. |
| 446 | /// [Docs](https://tailwindcss.com/docs/align-content#space-between) |
| 447 | fn content_between(mut self) -> Self { |
| 448 | self.style().align_content = Some(AlignContent::SpaceBetween); |
| 449 | self |
| 450 | } |
| 451 | |
| 452 | /// Sets the element to pack content items along the container's cross axis |
| 453 | /// such that there is an equal amount of space on each side of each item. |
| 454 | /// [Docs](https://tailwindcss.com/docs/align-content#space-around) |
| 455 | fn content_around(mut self) -> Self { |
| 456 | self.style().align_content = Some(AlignContent::SpaceAround); |
| 457 | self |
| 458 | } |
| 459 | |
| 460 | /// Sets the element to pack content items along the container's cross axis |
| 461 | /// such that there is an equal amount of space between each item. |
| 462 | /// [Docs](https://tailwindcss.com/docs/align-content#space-evenly) |
| 463 | fn content_evenly(mut self) -> Self { |
| 464 | self.style().align_content = Some(AlignContent::SpaceEvenly); |
| 465 | self |
| 466 | } |
| 467 | |
| 468 | /// Sets the element to allow content items to fill the available space along the container's cross axis. |
| 469 | /// [Docs](https://tailwindcss.com/docs/align-content#stretch) |
| 470 | fn content_stretch(mut self) -> Self { |
| 471 | self.style().align_content = Some(AlignContent::Stretch); |
| 472 | self |
| 473 | } |
| 474 | |
| 475 | /// Sets the aspect ratio of the element. |
| 476 | /// [Docs](https://tailwindcss.com/docs/aspect-ratio) |
| 477 | fn aspect_ratio(mut self, ratio: f32) -> Self { |
| 478 | self.style().aspect_ratio = Some(ratio); |
| 479 | self |
| 480 | } |
| 481 | |
| 482 | /// Sets the aspect ratio of the element to 1/1 – equal width and height. |
| 483 | /// [Docs](https://tailwindcss.com/docs/aspect-ratio) |
| 484 | fn aspect_square(mut self) -> Self { |
| 485 | self.style().aspect_ratio = Some(1.0); |
| 486 | self |
| 487 | } |
| 488 | |
| 489 | /// Sets the background color of the element. |
| 490 | fn bg<F>(mut self, fill: F) -> Self |
| 491 | where |
| 492 | F: Into<Fill>, |
| 493 | Self: Sized, |
| 494 | { |
| 495 | self.style().background = Some(fill.into()); |
| 496 | self |
| 497 | } |
| 498 | |
| 499 | /// Sets the border style of the element. |
| 500 | fn border_dashed(mut self) -> Self { |
| 501 | self.style().border_style = Some(BorderStyle::Dashed); |
| 502 | self |
| 503 | } |
| 504 | |
| 505 | /// Returns a mutable reference to the text style that has been configured on this element. |
| 506 | fn text_style(&mut self) -> &mut TextStyleRefinement { |
| 507 | let style: &mut StyleRefinement = self.style(); |
| 508 | &mut style.text |
| 509 | } |
| 510 | |
| 511 | /// Sets the text color of this element. |
| 512 | /// |
| 513 | /// This value cascades to its child elements. |
| 514 | fn text_color(mut self, color: impl Into<Hsla>) -> Self { |
| 515 | self.text_style().color = Some(color.into()); |
| 516 | self |
| 517 | } |
| 518 | |
| 519 | /// Sets the font weight of this element |
| 520 | /// |
| 521 | /// This value cascades to its child elements. |
| 522 | fn font_weight(mut self, weight: FontWeight) -> Self { |
| 523 | self.text_style().font_weight = Some(weight); |
| 524 | self |
| 525 | } |
| 526 | |
| 527 | /// Sets the background color of this element. |
| 528 | /// |
| 529 | /// This value cascades to its child elements. |
| 530 | fn text_bg(mut self, bg: impl Into<Hsla>) -> Self { |
| 531 | self.text_style().background_color = Some(bg.into()); |
| 532 | self |
| 533 | } |
| 534 | |
| 535 | /// Sets the text size of this element. |
| 536 | /// |
| 537 | /// This value cascades to its child elements. |
| 538 | fn text_size(mut self, size: impl Into<AbsoluteLength>) -> Self { |
| 539 | self.text_style().font_size = Some(size.into()); |
| 540 | self |
| 541 | } |
| 542 | |
| 543 | /// Sets the text size to 'extra small'. |
| 544 | /// [Docs](https://tailwindcss.com/docs/font-size#setting-the-font-size) |
| 545 | fn text_xs(mut self) -> Self { |
| 546 | self.text_style().font_size = Some(rems(0.75).into()); |
| 547 | self |
| 548 | } |
| 549 | |
| 550 | /// Sets the text size to 'small'. |
| 551 | /// [Docs](https://tailwindcss.com/docs/font-size#setting-the-font-size) |
| 552 | fn text_sm(mut self) -> Self { |
| 553 | self.text_style().font_size = Some(rems(0.875).into()); |
| 554 | self |
| 555 | } |
| 556 | |
| 557 | /// Sets the text size to 'base'. |
| 558 | /// [Docs](https://tailwindcss.com/docs/font-size#setting-the-font-size) |
| 559 | fn text_base(mut self) -> Self { |
| 560 | self.text_style().font_size = Some(rems(1.0).into()); |
| 561 | self |
| 562 | } |
| 563 | |
| 564 | /// Sets the text size to 'large'. |
| 565 | /// [Docs](https://tailwindcss.com/docs/font-size#setting-the-font-size) |
| 566 | fn text_lg(mut self) -> Self { |
| 567 | self.text_style().font_size = Some(rems(1.125).into()); |
| 568 | self |
| 569 | } |
| 570 | |
| 571 | /// Sets the text size to 'extra large'. |
| 572 | /// [Docs](https://tailwindcss.com/docs/font-size#setting-the-font-size) |
| 573 | fn text_xl(mut self) -> Self { |
| 574 | self.text_style().font_size = Some(rems(1.25).into()); |
| 575 | self |
| 576 | } |
| 577 | |
| 578 | /// Sets the text size to 'extra extra large'. |
| 579 | /// [Docs](https://tailwindcss.com/docs/font-size#setting-the-font-size) |
| 580 | fn text_2xl(mut self) -> Self { |
| 581 | self.text_style().font_size = Some(rems(1.5).into()); |
| 582 | self |
| 583 | } |
| 584 | |
| 585 | /// Sets the text size to 'extra extra extra large'. |
| 586 | /// [Docs](https://tailwindcss.com/docs/font-size#setting-the-font-size) |
| 587 | fn text_3xl(mut self) -> Self { |
| 588 | self.text_style().font_size = Some(rems(1.875).into()); |
| 589 | self |
| 590 | } |
| 591 | |
| 592 | /// Sets the font style of the element to italic. |
| 593 | /// [Docs](https://tailwindcss.com/docs/font-style#italicizing-text) |
| 594 | fn italic(mut self) -> Self { |
| 595 | self.text_style().font_style = Some(FontStyle::Italic); |
| 596 | self |
| 597 | } |
| 598 | |
| 599 | /// Sets the font style of the element to normal (not italic). |
| 600 | /// [Docs](https://tailwindcss.com/docs/font-style#displaying-text-normally) |
| 601 | fn not_italic(mut self) -> Self { |
| 602 | self.text_style().font_style = Some(FontStyle::Normal); |
| 603 | self |
| 604 | } |
| 605 | |
| 606 | /// Sets the text decoration to underline. |
| 607 | /// [Docs](https://tailwindcss.com/docs/text-decoration-line#underling-text) |
| 608 | fn underline(mut self) -> Self { |
| 609 | let style = self.text_style(); |
| 610 | style.underline = Some(UnderlineStyle { |
| 611 | thickness: px(1.), |
| 612 | ..Default::default() |
| 613 | }); |
| 614 | self |
| 615 | } |
| 616 | |
| 617 | /// Sets the decoration of the text to have a line through it. |
| 618 | /// [Docs](https://tailwindcss.com/docs/text-decoration-line#adding-a-line-through-text) |
| 619 | fn line_through(mut self) -> Self { |
| 620 | let style = self.text_style(); |
| 621 | style.strikethrough = Some(StrikethroughStyle { |
| 622 | thickness: px(1.), |
| 623 | ..Default::default() |
| 624 | }); |
| 625 | self |
| 626 | } |
| 627 | |
| 628 | /// Removes the text decoration on this element. |
| 629 | /// |
| 630 | /// This value cascades to its child elements. |
| 631 | fn text_decoration_none(mut self) -> Self { |
| 632 | self.text_style().underline = None; |
| 633 | self |
| 634 | } |
| 635 | |
| 636 | /// Sets the color for the underline on this element |
| 637 | fn text_decoration_color(mut self, color: impl Into<Hsla>) -> Self { |
| 638 | let style = self.text_style(); |
| 639 | let underline = style.underline.get_or_insert_with(Default::default); |
| 640 | underline.color = Some(color.into()); |
| 641 | self |
| 642 | } |
| 643 | |
| 644 | /// Sets the text decoration style to a solid line. |
| 645 | /// [Docs](https://tailwindcss.com/docs/text-decoration-style) |
| 646 | fn text_decoration_solid(mut self) -> Self { |
| 647 | let style = self.text_style(); |
| 648 | let underline = style.underline.get_or_insert_with(Default::default); |
| 649 | underline.wavy = false; |
| 650 | self |
| 651 | } |
| 652 | |
| 653 | /// Sets the text decoration style to a wavy line. |
| 654 | /// [Docs](https://tailwindcss.com/docs/text-decoration-style) |
| 655 | fn text_decoration_wavy(mut self) -> Self { |
| 656 | let style = self.text_style(); |
| 657 | let underline = style.underline.get_or_insert_with(Default::default); |
| 658 | underline.wavy = true; |
| 659 | self |
| 660 | } |
| 661 | |
| 662 | /// Sets the text decoration to be 0px thick. |
| 663 | /// [Docs](https://tailwindcss.com/docs/text-decoration-thickness) |
| 664 | fn text_decoration_0(mut self) -> Self { |
| 665 | let style = self.text_style(); |
| 666 | let underline = style.underline.get_or_insert_with(Default::default); |
| 667 | underline.thickness = px(0.); |
| 668 | self |
| 669 | } |
| 670 | |
| 671 | /// Sets the text decoration to be 1px thick. |
| 672 | /// [Docs](https://tailwindcss.com/docs/text-decoration-thickness) |
| 673 | fn text_decoration_1(mut self) -> Self { |
| 674 | let style = self.text_style(); |
| 675 | let underline = style.underline.get_or_insert_with(Default::default); |
| 676 | underline.thickness = px(1.); |
| 677 | self |
| 678 | } |
| 679 | |
| 680 | /// Sets the text decoration to be 2px thick. |
| 681 | /// [Docs](https://tailwindcss.com/docs/text-decoration-thickness) |
| 682 | fn text_decoration_2(mut self) -> Self { |
| 683 | let style = self.text_style(); |
| 684 | let underline = style.underline.get_or_insert_with(Default::default); |
| 685 | underline.thickness = px(2.); |
| 686 | self |
| 687 | } |
| 688 | |
| 689 | /// Sets the text decoration to be 4px thick. |
| 690 | /// [Docs](https://tailwindcss.com/docs/text-decoration-thickness) |
| 691 | fn text_decoration_4(mut self) -> Self { |
| 692 | let style = self.text_style(); |
| 693 | let underline = style.underline.get_or_insert_with(Default::default); |
| 694 | underline.thickness = px(4.); |
| 695 | self |
| 696 | } |
| 697 | |
| 698 | /// Sets the text decoration to be 8px thick. |
| 699 | /// [Docs](https://tailwindcss.com/docs/text-decoration-thickness) |
| 700 | fn text_decoration_8(mut self) -> Self { |
| 701 | let style = self.text_style(); |
| 702 | let underline = style.underline.get_or_insert_with(Default::default); |
| 703 | underline.thickness = px(8.); |
| 704 | self |
| 705 | } |
| 706 | |
| 707 | /// Sets the font family of this element and its children. |
| 708 | fn font_family(mut self, family_name: impl Into<SharedString>) -> Self { |
| 709 | self.text_style().font_family = Some(family_name.into()); |
| 710 | self |
| 711 | } |
| 712 | |
| 713 | /// Sets the font features of this element and its children. |
| 714 | fn font_features(mut self, features: FontFeatures) -> Self { |
| 715 | self.text_style().font_features = Some(features); |
| 716 | self |
| 717 | } |
| 718 | |
| 719 | /// Sets the font of this element and its children. |
| 720 | fn font(mut self, font: Font) -> Self { |
| 721 | let Font { |
| 722 | family, |
| 723 | features, |
| 724 | fallbacks, |
| 725 | weight, |
| 726 | style, |
| 727 | } = font; |
| 728 | |
| 729 | let text_style = self.text_style(); |
| 730 | text_style.font_family = Some(family); |
| 731 | text_style.font_features = Some(features); |
| 732 | text_style.font_weight = Some(weight); |
| 733 | text_style.font_style = Some(style); |
| 734 | text_style.font_fallbacks = fallbacks; |
| 735 | |
| 736 | self |
| 737 | } |
| 738 | |
| 739 | /// Sets the line height of this element and its children. |
| 740 | fn line_height(mut self, line_height: impl Into<DefiniteLength>) -> Self { |
| 741 | self.text_style().line_height = Some(line_height.into()); |
| 742 | self |
| 743 | } |
| 744 | |
| 745 | /// Sets the opacity of this element and its children. |
| 746 | fn opacity(mut self, opacity: f32) -> Self { |
| 747 | self.style().opacity = Some(opacity); |
| 748 | self |
| 749 | } |
| 750 | |
| 751 | /// Sets the grid columns of this element. |
| 752 | fn grid_cols(mut self, cols: u16) -> Self { |
| 753 | self.style().grid_cols = Some(GridTemplate { |
| 754 | repeat: cols, |
| 755 | min_size: GridTemplateMinSize::Zero, |
| 756 | }); |
| 757 | self |
| 758 | } |
| 759 | |
| 760 | /// Sets the grid columns with min-content minimum sizing. |
| 761 | /// Unlike grid_cols, it won't shrink to width 0 in AvailableSpace::MinContent constraints. |
| 762 | fn grid_cols_min_content(mut self, cols: u16) -> Self { |
| 763 | self.style().grid_cols = Some(GridTemplate { |
| 764 | repeat: cols, |
| 765 | min_size: GridTemplateMinSize::MinContent, |
| 766 | }); |
| 767 | self |
| 768 | } |
| 769 | |
| 770 | /// Sets the grid columns with max-content maximum sizing for content-based column widths. |
| 771 | fn grid_cols_max_content(mut self, cols: u16) -> Self { |
| 772 | self.style().grid_cols = Some(GridTemplate { |
| 773 | repeat: cols, |
| 774 | min_size: GridTemplateMinSize::MaxContent, |
| 775 | }); |
| 776 | self |
| 777 | } |
| 778 | |
| 779 | /// Sets the grid rows of this element. |
| 780 | fn grid_rows(mut self, rows: u16) -> Self { |
| 781 | self.style().grid_rows = Some(GridTemplate { |
| 782 | repeat: rows, |
| 783 | min_size: GridTemplateMinSize::Zero, |
| 784 | }); |
| 785 | self |
| 786 | } |
| 787 | |
| 788 | /// Sets the grid rows with min-content minimum sizing. |
| 789 | /// Unlike grid_rows, it won't shrink to height 0 in AvailableSpace::MinContent constraints. |
| 790 | fn grid_rows_min_content(mut self, rows: u16) -> Self { |
| 791 | self.style().grid_rows = Some(GridTemplate { |
| 792 | repeat: rows, |
| 793 | min_size: GridTemplateMinSize::MinContent, |
| 794 | }); |
| 795 | self |
| 796 | } |
| 797 | |
| 798 | /// Sets the grid rows with max-content maximum sizing for content-based row heights. |
| 799 | fn grid_rows_max_content(mut self, rows: u16) -> Self { |
| 800 | self.style().grid_rows = Some(GridTemplate { |
| 801 | repeat: rows, |
| 802 | min_size: GridTemplateMinSize::MaxContent, |
| 803 | }); |
| 804 | self |
| 805 | } |
| 806 | |
| 807 | /// Sets the column start of this element. |
| 808 | fn col_start(mut self, start: i16) -> Self { |
| 809 | let grid_location = self.style().grid_location_mut(); |
| 810 | grid_location.column.start = GridPlacement::Line(start); |
| 811 | self |
| 812 | } |
| 813 | |
| 814 | /// Sets the column start of this element to auto. |
| 815 | fn col_start_auto(mut self) -> Self { |
| 816 | let grid_location = self.style().grid_location_mut(); |
| 817 | grid_location.column.start = GridPlacement::Auto; |
| 818 | self |
| 819 | } |
| 820 | |
| 821 | /// Sets the column end of this element. |
| 822 | fn col_end(mut self, end: i16) -> Self { |
| 823 | let grid_location = self.style().grid_location_mut(); |
| 824 | grid_location.column.end = GridPlacement::Line(end); |
| 825 | self |
| 826 | } |
| 827 | |
| 828 | /// Sets the column end of this element to auto. |
| 829 | fn col_end_auto(mut self) -> Self { |
| 830 | let grid_location = self.style().grid_location_mut(); |
| 831 | grid_location.column.end = GridPlacement::Auto; |
| 832 | self |
| 833 | } |
| 834 | |
| 835 | /// Sets the column span of this element. |
| 836 | fn col_span(mut self, span: u16) -> Self { |
| 837 | let grid_location = self.style().grid_location_mut(); |
| 838 | grid_location.column = GridPlacement::Span(span)..GridPlacement::Span(span); |
| 839 | self |
| 840 | } |
| 841 | |
| 842 | /// Sets the row span of this element. |
| 843 | fn col_span_full(mut self) -> Self { |
| 844 | let grid_location = self.style().grid_location_mut(); |
| 845 | grid_location.column = GridPlacement::Line(1)..GridPlacement::Line(-1); |
| 846 | self |
| 847 | } |
| 848 | |
| 849 | /// Sets the row start of this element. |
| 850 | fn row_start(mut self, start: i16) -> Self { |
| 851 | let grid_location = self.style().grid_location_mut(); |
| 852 | grid_location.row.start = GridPlacement::Line(start); |
| 853 | self |
| 854 | } |
| 855 | |
| 856 | /// Sets the row start of this element to "auto" |
| 857 | fn row_start_auto(mut self) -> Self { |
| 858 | let grid_location = self.style().grid_location_mut(); |
| 859 | grid_location.row.start = GridPlacement::Auto; |
| 860 | self |
| 861 | } |
| 862 | |
| 863 | /// Sets the row end of this element. |
| 864 | fn row_end(mut self, end: i16) -> Self { |
| 865 | let grid_location = self.style().grid_location_mut(); |
| 866 | grid_location.row.end = GridPlacement::Line(end); |
| 867 | self |
| 868 | } |
| 869 | |
| 870 | /// Sets the row end of this element to "auto" |
| 871 | fn row_end_auto(mut self) -> Self { |
| 872 | let grid_location = self.style().grid_location_mut(); |
| 873 | grid_location.row.end = GridPlacement::Auto; |
| 874 | self |
| 875 | } |
| 876 | |
| 877 | /// Sets the row span of this element. |
| 878 | fn row_span(mut self, span: u16) -> Self { |
| 879 | let grid_location = self.style().grid_location_mut(); |
| 880 | grid_location.row = GridPlacement::Span(span)..GridPlacement::Span(span); |
| 881 | self |
| 882 | } |
| 883 | |
| 884 | /// Sets the row span of this element. |
| 885 | fn row_span_full(mut self) -> Self { |
| 886 | let grid_location = self.style().grid_location_mut(); |
| 887 | grid_location.row = GridPlacement::Line(1)..GridPlacement::Line(-1); |
| 888 | self |
| 889 | } |
| 890 | |
| 891 | /// Draws a debug border around this element. |
| 892 | #[cfg(debug_assertions)] |
| 893 | fn debug(mut self) -> Self { |
| 894 | self.style().debug = Some(true); |
| 895 | self |
| 896 | } |
| 897 | |
| 898 | /// Draws a debug border on all conforming elements below this element. |
| 899 | #[cfg(debug_assertions)] |
| 900 | fn debug_below(mut self) -> Self { |
| 901 | self.style().debug_below = Some(true); |
| 902 | self |
| 903 | } |
| 904 | } |