Skip to content

Commit

Permalink
Merge pull request #170 from zed-industries/people-panel
Browse files Browse the repository at this point in the history
People panel
  • Loading branch information
as-cii authored Sep 22, 2021
2 parents e6a0a46 + 6120ce3 commit 796139e
Show file tree
Hide file tree
Showing 34 changed files with 2,603 additions and 976 deletions.
1 change: 1 addition & 0 deletions .zed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
collaborators = ["nathansobo", "as-cii", "maxbrunsfeld", "iamnbutler"]
8 changes: 8 additions & 0 deletions gpui/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ impl Color {
Self(ColorU::from_u32(0xff0000ff))
}

pub fn green() -> Self {
Self(ColorU::from_u32(0x00ff00ff))
}

pub fn blue() -> Self {
Self(ColorU::from_u32(0x0000ffff))
}

pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self(ColorU::new(r, g, b, a))
}
Expand Down
33 changes: 30 additions & 3 deletions gpui/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod flex;
mod hook;
mod image;
mod label;
mod line_box;
mod list;
mod mouse_event_handler;
mod overlay;
Expand All @@ -19,8 +18,8 @@ mod uniform_list;

pub use self::{
align::*, canvas::*, constrained_box::*, container::*, empty::*, event_handler::*, flex::*,
hook::*, image::*, label::*, line_box::*, list::*, mouse_event_handler::*, overlay::*,
stack::*, svg::*, text::*, uniform_list::*,
hook::*, image::*, label::*, list::*, mouse_event_handler::*, overlay::*, stack::*, svg::*,
text::*, uniform_list::*,
};
pub use crate::presenter::ChildView;
use crate::{
Expand Down Expand Up @@ -109,6 +108,34 @@ pub trait Element {
element: Rc::new(RefCell::new(Lifecycle::Init { element: self })),
})
}

fn constrained(self) -> ConstrainedBox
where
Self: 'static + Sized,
{
ConstrainedBox::new(self.boxed())
}

fn aligned(self) -> Align
where
Self: 'static + Sized,
{
Align::new(self.boxed())
}

fn contained(self) -> Container
where
Self: 'static + Sized,
{
Container::new(self.boxed())
}

fn expanded(self, flex: f32) -> Expanded
where
Self: 'static + Sized,
{
Expanded::new(flex, self.boxed())
}
}

pub enum Lifecycle<T: Element> {
Expand Down
5 changes: 5 additions & 0 deletions gpui/src/elements/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ impl Align {
self
}

pub fn left(mut self) -> Self {
self.alignment.set_x(-1.0);
self
}

pub fn right(mut self) -> Self {
self.alignment.set_x(1.0);
self
Expand Down
5 changes: 5 additions & 0 deletions gpui/src/elements/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ impl Container {
self
}

pub fn with_margin_right(mut self, margin: f32) -> Self {
self.style.margin.right = margin;
self
}

pub fn with_horizontal_padding(mut self, padding: f32) -> Self {
self.style.padding.left = padding;
self.style.padding.right = padding;
Expand Down
23 changes: 18 additions & 5 deletions gpui/src/elements/image.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -16,9 +19,13 @@ pub struct Image {
#[derive(Copy, Clone, Default, Deserialize)]
pub struct ImageStyle {
#[serde(default)]
border: Border,
pub border: Border,
#[serde(default)]
corner_radius: f32,
pub corner_radius: f32,
#[serde(default)]
pub height: Option<f32>,
#[serde(default)]
pub width: Option<f32>,
}

impl Image {
Expand All @@ -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, ())
}

Expand Down
3 changes: 1 addition & 2 deletions gpui/src/elements/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ impl Element for Label {
let size = vec2f(
line.width().max(constraint.min.x()).min(constraint.max.x()),
cx.font_cache
.line_height(self.style.text.font_id, self.style.text.font_size)
.ceil(),
.line_height(self.style.text.font_id, self.style.text.font_size),
);

(size, line)
Expand Down
87 changes: 0 additions & 87 deletions gpui/src/elements/line_box.rs

This file was deleted.

35 changes: 32 additions & 3 deletions gpui/src/elements/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{cell::RefCell, collections::VecDeque, ops::Range, rc::Rc};

pub struct List {
state: ListState,
invalidated_elements: Vec<ElementRc>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -79,7 +80,10 @@ struct Height(f32);

impl List {
pub fn new(state: ListState) -> Self {
Self { state }
Self {
state,
invalidated_elements: Default::default(),
}
}
}

Expand Down Expand Up @@ -258,10 +262,35 @@ impl Element for List {
let mut handled = false;

let mut state = self.state.0.borrow_mut();
for (mut element, _) in state.visible_elements(bounds, scroll_top) {
handled = element.dispatch_event(event, cx) || handled;
let mut item_origin = bounds.origin() - vec2f(0., scroll_top.offset_in_item);
let mut cursor = state.items.cursor::<Count, ()>();
let mut new_items = cursor.slice(&Count(scroll_top.item_ix), Bias::Right, &());
while let Some(item) = cursor.item() {
if item_origin.y() > bounds.max_y() {
break;
}

if let ListItem::Rendered(element) = item {
let prev_notify_count = cx.notify_count();
let mut element = element.clone();
handled = element.dispatch_event(event, cx) || handled;
item_origin.set_y(item_origin.y() + element.size().y());
if cx.notify_count() > prev_notify_count {
new_items.push(ListItem::Unrendered, &());
self.invalidated_elements.push(element);
} else {
new_items.push(item.clone(), &());
}
cursor.next(&());
} else {
unreachable!();
}
}

new_items.push_tree(cursor.suffix(&()), &());
drop(cursor);
state.items = new_items;

match event {
Event::ScrollWheel {
position,
Expand Down
12 changes: 12 additions & 0 deletions gpui/src/font_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ impl FontCache {
self.metric(font_id, |m| m.cap_height) * self.em_scale(font_id, font_size)
}

pub fn x_height(&self, font_id: FontId, font_size: f32) -> f32 {
self.metric(font_id, |m| m.x_height) * self.em_scale(font_id, font_size)
}

pub fn ascent(&self, font_id: FontId, font_size: f32) -> f32 {
self.metric(font_id, |m| m.ascent) * self.em_scale(font_id, font_size)
}
Expand All @@ -178,6 +182,14 @@ impl FontCache {
font_size / self.metric(font_id, |m| m.units_per_em as f32)
}

pub fn baseline_offset(&self, font_id: FontId, font_size: f32) -> f32 {
let line_height = self.line_height(font_id, font_size);
let ascent = self.ascent(font_id, font_size);
let descent = self.descent(font_id, font_size);
let padding_top = (line_height - ascent - descent) / 2.;
padding_top + ascent
}

pub fn line_wrapper(self: &Arc<Self>, font_id: FontId, font_size: f32) -> LineWrapperHandle {
let mut state = self.0.write();
let wrappers = state
Expand Down
12 changes: 12 additions & 0 deletions gpui/src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ impl TextStyle {
font_cache.line_height(self.font_id, self.font_size)
}

pub fn cap_height(&self, font_cache: &FontCache) -> f32 {
font_cache.cap_height(self.font_id, self.font_size)
}

pub fn x_height(&self, font_cache: &FontCache) -> f32 {
font_cache.x_height(self.font_id, self.font_size)
}

pub fn em_width(&self, font_cache: &FontCache) -> f32 {
font_cache.em_width(self.font_id, self.font_size)
}
Expand All @@ -140,6 +148,10 @@ impl TextStyle {
font_cache.metric(self.font_id, |m| m.descent) * self.em_scale(font_cache)
}

pub fn baseline_offset(&self, font_cache: &FontCache) -> f32 {
font_cache.baseline_offset(self.font_id, self.font_size)
}

fn em_scale(&self, font_cache: &FontCache) -> f32 {
font_cache.em_scale(self.font_id, self.font_size)
}
Expand Down
14 changes: 14 additions & 0 deletions gpui/src/presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl Presenter {
text_layout_cache: &self.text_layout_cache,
view_stack: Default::default(),
invalidated_views: Default::default(),
notify_count: 0,
app: cx,
}
}
Expand Down Expand Up @@ -300,6 +301,7 @@ pub struct EventContext<'a> {
pub font_cache: &'a FontCache,
pub text_layout_cache: &'a TextLayoutCache,
pub app: &'a mut MutableAppContext,
pub notify_count: usize,
view_stack: Vec<usize>,
invalidated_views: HashSet<usize>,
}
Expand All @@ -325,10 +327,15 @@ impl<'a> EventContext<'a> {
}

pub fn notify(&mut self) {
self.notify_count += 1;
if let Some(view_id) = self.view_stack.last() {
self.invalidated_views.insert(*view_id);
}
}

pub fn notify_count(&self) -> usize {
self.notify_count
}
}

impl<'a> Deref for EventContext<'a> {
Expand Down Expand Up @@ -432,6 +439,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 {
Expand Down
Loading

0 comments on commit 796139e

Please sign in to comment.