Style avatar image with border and rounded corners

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2021-09-14 16:59:38 -06:00
parent 426d52d8c1
commit 0f415a594f
4 changed files with 25 additions and 20 deletions

View file

@ -1,16 +1,23 @@
use super::constrain_size_preserving_aspect_ratio;
use crate::{ use crate::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json::{json, ToJson}, json::{json, ToJson},
scene, Border, DebugContext, Element, Event, EventContext, ImageData, LayoutContext, scene, Border, DebugContext, Element, Event, EventContext, ImageData, LayoutContext,
PaintContext, SizeConstraint, PaintContext, SizeConstraint,
}; };
use serde::Deserialize;
use std::sync::Arc; use std::sync::Arc;
use super::constrain_size_preserving_aspect_ratio;
pub struct Image { pub struct Image {
data: Arc<ImageData>, data: Arc<ImageData>,
style: ImageStyle,
}
#[derive(Copy, Clone, Default, Deserialize)]
pub struct ImageStyle {
#[serde(default)]
border: Border, border: Border,
#[serde(default)]
corner_radius: f32, corner_radius: f32,
} }
@ -18,18 +25,12 @@ impl Image {
pub fn new(data: Arc<ImageData>) -> Self { pub fn new(data: Arc<ImageData>) -> Self {
Self { Self {
data, data,
border: Default::default(), style: Default::default(),
corner_radius: Default::default(),
} }
} }
pub fn with_corner_radius(mut self, corner_radius: f32) -> Self { pub fn with_style(mut self, style: ImageStyle) -> Self {
self.corner_radius = corner_radius; self.style = style;
self
}
pub fn with_border(mut self, border: Border) -> Self {
self.border = border;
self self
} }
} }
@ -57,8 +58,8 @@ impl Element for Image {
) -> Self::PaintState { ) -> Self::PaintState {
cx.scene.push_image(scene::Image { cx.scene.push_image(scene::Image {
bounds, bounds,
border: self.border, border: self.style.border,
corner_radius: self.corner_radius, corner_radius: self.style.corner_radius,
data: self.data.clone(), data: self.data.clone(),
}); });
} }

View file

@ -8,8 +8,9 @@ pane_divider = { width = 1, color = "$border.0" }
[workspace.titlebar] [workspace.titlebar]
border = { width = 1, bottom = true, color = "$border.0" } border = { width = 1, bottom = true, color = "$border.0" }
title = "$text.0" title = "$text.0"
icon_width = 16 avatar_width = 20
icon_signed_out = "$text.2.color" icon_signed_out = "$text.2.color"
avatar = { corner_radius = 10, border = { width = 1, color = "#00000088" } }
[workspace.tab] [workspace.tab]
text = "$text.2" text = "$text.2"
@ -28,7 +29,7 @@ background = "$surface.1"
text = "$text.0" text = "$text.0"
[workspace.sidebar] [workspace.sidebar]
width = 36 width = 38
border = { right = true, width = 1, color = "$border.0" } border = { right = true, width = 1, color = "$border.0" }
[workspace.sidebar.resize_handle] [workspace.sidebar.resize_handle]
@ -37,7 +38,7 @@ background = "$border.0"
[workspace.sidebar.icon] [workspace.sidebar.icon]
color = "$text.2.color" color = "$text.2.color"
height = 16 height = 18
[workspace.sidebar.active_icon] [workspace.sidebar.active_icon]
extends = "$workspace.sidebar.icon" extends = "$workspace.sidebar.icon"

View file

@ -4,7 +4,7 @@ mod theme_registry;
use anyhow::Result; use anyhow::Result;
use gpui::{ use gpui::{
color::Color, color::Color,
elements::{ContainerStyle, LabelStyle}, elements::{ContainerStyle, ImageStyle, LabelStyle},
fonts::{HighlightStyle, TextStyle}, fonts::{HighlightStyle, TextStyle},
Border, Border,
}; };
@ -47,8 +47,9 @@ pub struct Titlebar {
#[serde(flatten)] #[serde(flatten)]
pub container: ContainerStyle, pub container: ContainerStyle,
pub title: TextStyle, pub title: TextStyle,
pub icon_width: f32, pub avatar_width: f32,
pub icon_signed_out: Color, pub icon_signed_out: Color,
pub avatar: ImageStyle,
} }
#[derive(Clone, Deserialize)] #[derive(Clone, Deserialize)]

View file

@ -963,7 +963,9 @@ impl Workspace {
.as_ref() .as_ref()
.and_then(|user| user.avatar.clone()) .and_then(|user| user.avatar.clone())
{ {
Image::new(avatar).boxed() Image::new(avatar)
.with_style(theme.workspace.titlebar.avatar)
.boxed()
} else { } else {
Svg::new("icons/signed-out-12.svg") Svg::new("icons/signed-out-12.svg")
.with_color(theme.workspace.titlebar.icon_signed_out) .with_color(theme.workspace.titlebar.icon_signed_out)
@ -973,7 +975,7 @@ impl Workspace {
ConstrainedBox::new( ConstrainedBox::new(
Align::new( Align::new(
ConstrainedBox::new(avatar) ConstrainedBox::new(avatar)
.with_width(theme.workspace.titlebar.icon_width) .with_width(theme.workspace.titlebar.avatar_width)
.boxed(), .boxed(),
) )
.boxed(), .boxed(),