From 1bd6cd09783b52cfd328f029fb8c2913b2e9e7bb Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 21 Sep 2021 18:27:26 +0200 Subject: [PATCH] Allow size to be specified in `ImageStyle` Co-Authored-By: Nathan Sobo --- gpui/src/elements/image.rs | 19 ++++++++++++++++--- gpui/src/presenter.rs | 7 +++++++ zed/assets/themes/_base.toml | 4 ++-- zed/src/people_panel.rs | 20 ++++++-------------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/gpui/src/elements/image.rs b/gpui/src/elements/image.rs index 421e18ec95ce7bc64df77d675797a10c41ff3541..57644986dc005d9f9fa848b409055472961d6b71 100644 --- a/gpui/src/elements/image.rs +++ b/gpui/src/elements/image.rs @@ -1,6 +1,9 @@ use super::constrain_size_preserving_aspect_ratio; use crate::{ - geometry::{rect::RectF, vector::Vector2F}, + geometry::{ + rect::RectF, + vector::{vec2f, Vector2F}, + }, json::{json, ToJson}, scene, Border, DebugContext, Element, Event, EventContext, ImageData, LayoutContext, PaintContext, SizeConstraint, @@ -19,6 +22,10 @@ pub struct ImageStyle { border: Border, #[serde(default)] corner_radius: f32, + #[serde(default)] + height: Option, + #[serde(default)] + width: Option, } impl Image { @@ -44,8 +51,14 @@ impl Element for Image { constraint: SizeConstraint, _: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - let size = - constrain_size_preserving_aspect_ratio(constraint.max, self.data.size().to_f32()); + let desired_size = vec2f( + self.style.width.unwrap_or(constraint.max.x()), + self.style.height.unwrap_or(constraint.max.y()), + ); + let size = constrain_size_preserving_aspect_ratio( + constraint.constrain(desired_size), + self.data.size().to_f32(), + ); (size, ()) } diff --git a/gpui/src/presenter.rs b/gpui/src/presenter.rs index 2062397e9e6547dbe4ea9083485a8b8c0a519475..d6765bec29876889df2f5056d5624a7353de31a0 100644 --- a/gpui/src/presenter.rs +++ b/gpui/src/presenter.rs @@ -432,6 +432,13 @@ impl SizeConstraint { Axis::Vertical => self.min.y(), } } + + pub fn constrain(&self, size: Vector2F) -> Vector2F { + vec2f( + size.x().min(self.max.x()).max(self.min.x()), + size.y().min(self.max.y()).max(self.min.y()), + ) + } } impl ToJson for SizeConstraint { diff --git a/zed/assets/themes/_base.toml b/zed/assets/themes/_base.toml index eda656b267ec03866ae51e7879075076294e5cf8..7b1fd8b9ffd75e918bc8c4da116f5c6437114828 100644 --- a/zed/assets/themes/_base.toml +++ b/zed/assets/themes/_base.toml @@ -126,8 +126,8 @@ color = "$text.1.color" [people_panel] extends = "$panel" host_username = { extends = "$text.0", padding.left = 5 } -worktree_host_avatar = { corner_radius = 10 } -worktree_guest_avatar = { corner_radius = 8 } +worktree_host_avatar = { corner_radius = 10, width = 20 } +worktree_guest_avatar = { corner_radius = 8, width = 16 } [people_panel.worktree_name] extends = "$text.0" diff --git a/zed/src/people_panel.rs b/zed/src/people_panel.rs index 771fd6713fb945604003e355b7ebe60e1e9d3163..769b2d7d9b80d1e1690e457c557dc8db8749f8f8 100644 --- a/zed/src/people_panel.rs +++ b/zed/src/people_panel.rs @@ -69,13 +69,9 @@ impl PeoplePanel { .with_child( Flex::row() .with_children(collaborator.user.avatar.clone().map(|avatar| { - ConstrainedBox::new( - Image::new(avatar) - .with_style(theme.worktree_host_avatar) - .boxed(), - ) - .with_width(20.) - .boxed() + Image::new(avatar) + .with_style(theme.worktree_host_avatar) + .boxed() })) .with_child( Container::new( @@ -152,13 +148,9 @@ impl PeoplePanel { ) .with_children(worktree.participants.iter().filter_map(|participant| { participant.avatar.clone().map(|avatar| { - ConstrainedBox::new( - Image::new(avatar) - .with_style(theme.worktree_guest_avatar) - .boxed(), - ) - .with_width(16.) - .boxed() + Image::new(avatar) + .with_style(theme.worktree_guest_avatar) + .boxed() }) })) .boxed()