// Hand-written parts of gpui/src/style.rs. The Style / TextStyle structs and // their refinements are generated (style_generated.mbt, tools/gen_style.py). // // Not ported yet: `Style::paint` (needs Window, milestone M6) and the // conversions to taffy styles (milestone M2). ///| fn[T] refine_value(value : T?, current : T) -> T { match value { Some(v) => v None => current } } ///| fn[T] refine_option(value : T?, current : T?) -> T? { match value { Some(_) => value None => current } } ///| /// GPUI's defaults: block layout, visible, relative position, auto size, no /// margin/padding/border, flex-grow 0, flex-shrink 1. pub impl Default for Style with fn default() { { display: Block, visibility: Visible, overflow: { x: Visible, y: Visible, }, scrollbar_width: Default::default(), allow_concurrent_scroll: false, restrict_scroll_to_axis: false, position: Relative, inset: @geometry.Edges::auto(), size: @geometry.Size::auto(), min_size: @geometry.Size::auto(), max_size: @geometry.Size::auto(), aspect_ratio: Option::None, margin: @geometry.Edges::zero_length(), padding: @geometry.Edges::zero_definite(), border_widths: @geometry.Edges::zero_absolute(), align_items: Option::None, align_self: Option::None, align_content: Option::None, justify_content: Option::None, gap: Default::default(), flex_direction: Row, flex_wrap: NoWrap, flex_basis: Auto, flex_grow: 0.0, flex_shrink: 1.0, background: Option::None, border_color: Option::None, border_style: Default::default(), corner_radii: Default::default(), box_shadow: [], text: Default::default(), mouse_cursor: Option::None, opacity: Option::None, grid_cols: Option::None, grid_rows: Option::None, grid_location: Option::None, debug: false, debug_below: false, } } ///| /// GPUI's default text style: black, the system UI font at 1rem, line height /// φ (≈1.618) times the font size. pub impl Default for TextStyle with fn default() { { color: @color.black(), font_family: @font.system_ui_font, font_features: Default::default(), font_fallbacks: Option::None, font_size: @geometry.rems(1.0).to_absolute(), line_height: @geometry.phi(), font_weight: Default::default(), font_style: Default::default(), background_color: Option::None, underline: Option::None, strikethrough: Option::None, white_space: Normal, text_overflow: Option::None, text_align: Left, line_clamp: Option::None, } } ///| /// Whether the background is a fill that is not fully transparent. pub fn Style::has_opaque_background(self : Style) -> Bool { match self.background { Some(fill) => match fill.color() { Some(color) => !color.is_transparent() Option::None => false } Option::None => false } } ///| /// The text style refinement, if any field is set. pub fn Style::text_style(self : Style) -> TextStyleRefinement? { if self.text.is_some() { Some(self.text) } else { Option::None } } ///| /// The region children are clipped to when overflow is not visible: the /// bounds, minus visible borders, on the clipped axes. pub fn Style::overflow_mask( self : Style, bounds : @geometry.Bounds[@geometry.Pixels], rem_size : @geometry.Pixels, ) -> @geometry.ContentMask[@geometry.Pixels]? { let x_visible = self.overflow.x == Visible let y_visible = self.overflow.y == Visible if x_visible && y_visible { return Option::None } let mut min = bounds.origin let mut max = bounds.bottom_right() let border_visible = match self.border_color { Some(color) => !color.is_transparent() Option::None => false } if border_visible { let widths = self.border_widths.absolute_to_pixels(rem_size) min = { x: min.x + widths.left, y: min.y + widths.top, } max = { x: max.x - widths.right, y: max.y - widths.bottom, } } let clipped = if x_visible { @geometry.Bounds::from_corners({ x: min.x, y: bounds.origin.y, }, { x: max.x, y: bounds.bottom_right().y, }) } else if y_visible { @geometry.Bounds::from_corners({ x: bounds.origin.x, y: min.y, }, { x: bounds.bottom_right().x, y: max.y, }) } else { @geometry.Bounds::from_corners(min, max) } Some({ bounds: clipped, }) } ///| /// Whether a border would be painted: a visible color and a non-zero width. pub fn Style::is_border_visible(self : Style) -> Bool { let color_visible = match self.border_color { Some(color) => !color.is_transparent() Option::None => false } color_visible && self.border_widths.any(fn(width) { !@geometry.IsZero::is_zero(width) }) } ///| /// The font this text style selects. pub fn TextStyle::font(self : TextStyle) -> @font.Font { { family: self.font_family, features: self.font_features, fallbacks: self.font_fallbacks, weight: self.font_weight, style: self.font_style, } } ///| /// The line height in pixels, rounded. pub fn TextStyle::line_height_in_pixels( self : TextStyle, rem_size : @geometry.Pixels, ) -> @geometry.Pixels { self.line_height.to_pixels(self.font_size, rem_size).round() } ///| /// Applies a highlight on top of this style. pub fn TextStyle::highlight( self : TextStyle, style : HighlightStyle, ) -> TextStyle { let mut result = self if style.font_weight is Some(weight) { result = { ..result, font_weight: weight, } } if style.font_style is Some(font_style) { result = { ..result, font_style, } } if style.color is Some(color) { result = { ..result, color: result.color.blend(color), } } if style.fade_out is Some(factor) { result = { ..result, color: result.color.fade_out(factor), } } if style.background_color is Some(_) { result = { ..result, background_color: style.background_color, } } if style.underline is Some(_) { result = { ..result, underline: style.underline, } } if style.strikethrough is Some(_) { result = { ..result, strikethrough: style.strikethrough, } } result } // --------------------------------------------------------------------------- // HighlightStyle // --------------------------------------------------------------------------- ///| /// A partial text style used to highlight ranges (syntax highlighting, /// search matches, ...). #derive(Eq, Default, Debug) pub(all) struct HighlightStyle { color : @color.Hsla? font_weight : @font.FontWeight? font_style : @font.FontStyle? background_color : @color.Hsla? underline : UnderlineStyle? strikethrough : StrikethroughStyle? /// Fade the color out by this factor (0 to 1). fade_out : Double? } ///| pub fn HighlightStyle::color(color : @color.Hsla) -> HighlightStyle { { ..Default::default(), color: Some(color), } } ///| /// The highlight equivalent of a full text style. pub fn HighlightStyle::from_text_style(style : TextStyle) -> HighlightStyle { { color: Some(style.color), font_weight: Some(style.font_weight), font_style: Some(style.font_style), background_color: style.background_color, underline: style.underline, strikethrough: style.strikethrough, fade_out: Option::None, } } ///| /// Layers `other` on top of `self`: colors blend, fades compound, other /// fields set in `other` win. pub fn HighlightStyle::highlight( self : HighlightStyle, other : HighlightStyle, ) -> HighlightStyle { let color = match (self.color, other.color) { (Some(color), Some(other_color)) => Some(color.blend(other_color)) (Option::None, Some(other_color)) => Some(other_color) (current, Option::None) => current } let fade_out = match (self.fade_out, other.fade_out) { (Some(dest), Some(source)) => { let combined = dest * (1.0 + source) Some( if combined < 0.0 { 0.0 } else if combined > 1.0 { 1.0 } else { combined }, ) } (Option::None, Some(source)) => Some(source) (current, Option::None) => current } { color, font_weight: refine_option(other.font_weight, self.font_weight), font_style: refine_option(other.font_style, self.font_style), background_color: refine_option( other.background_color, self.background_color, ), underline: refine_option(other.underline, self.underline), strikethrough: refine_option(other.strikethrough, self.strikethrough), fade_out, } } ///| /// A half-open byte range `[start, end)` with a highlight. #derive(Eq, Debug) pub(all) struct HighlightRange { start : Int32 end : Int32 style : HighlightStyle } ///| /// Merges two lists of highlighted ranges into non-overlapping ranges, each /// carrying the combination of every highlight covering it. Port of GPUI's /// `combine_highlights`. pub fn combine_highlights( a : Array[HighlightRange], b : Array[HighlightRange], ) -> Array[HighlightRange] { // (position, highlight id, is_start) let endpoints : Array[(Int32, Int32, Bool)] = [] let highlights : Array[HighlightStyle] = [] for range in [..a, ..b] { if range.start < range.end { let id = highlights.length() endpoints.push((range.start, id, true)) endpoints.push((range.end, id, false)) highlights.push(range.style) } } // Stable by position; ties keep insertion order. let keyed = endpoints.mapi(fn(i, e) { (e.0, i) }) keyed.sort() let sorted = keyed.map(fn(k) { endpoints[k.1] }) let active : @hashset.HashSet[Int32] = @hashset.HashSet([]) let result = [] let mut index = 0 for endpoint in sorted { let (position, id, is_start) = endpoint let prev = index index = position if index > prev && !active.is_empty() { // Combine in id order so the result does not depend on set iteration. let ids = active.iter().collect() ids.sort() let mut style : HighlightStyle = Default::default() for active_id in ids { style = style.highlight(highlights[active_id]) } result.push({ start: prev, end: index, style, }) } if is_start { active.add(id) } else { active.remove(id) } } result } // --------------------------------------------------------------------------- // ObjectFit // --------------------------------------------------------------------------- ///| /// How an image is fitted into its bounds (CSS `object-fit`). #derive(Eq, Hash, Debug) pub(all) enum ObjectFit { Fill Contain Cover ScaleDown None } ///| /// Where an image of `image_size` device pixels is drawn inside `bounds`. pub fn ObjectFit::get_bounds( self : ObjectFit, bounds : @geometry.Bounds[@geometry.Pixels], image_size : @geometry.Size[@geometry.DevicePixels], ) -> @geometry.Bounds[@geometry.Pixels] { let image_w = image_size.width.0.to_double() let image_h = image_size.height.0.to_double() let bounds_w = bounds.size.width.0 let bounds_h = bounds.size.height.0 let image_ratio = image_w / image_h let bounds_ratio = bounds_w / bounds_h let centered = fn( w : Double, h : Double, ) -> @geometry.Bounds[@geometry.Pixels] { { origin: { x: @geometry.px(bounds.origin.x.0 + (bounds_w - w) / 2.0), y: @geometry.px(bounds.origin.y.0 + (bounds_h - h) / 2.0), }, size: { width: @geometry.px(w), height: @geometry.px(h), }, } } let contain = fn() { if bounds_ratio > image_ratio { centered(image_w * (bounds_h / image_h), bounds_h) } else { centered(bounds_w, image_h * (bounds_w / image_w)) } } match self { Fill => bounds Contain => contain() ScaleDown => if image_w > bounds_w || image_h > bounds_h { contain() } else { centered(image_w, image_h) } Cover => if bounds_ratio > image_ratio { centered(bounds_w, image_h * (bounds_w / image_w)) } else { centered(image_w * (bounds_h / image_h), bounds_h) } None => { origin: bounds.origin, size: { width: @geometry.px(image_w), height: @geometry.px(image_h), }, } } }