Remove 2 suffix for workspace

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-01-03 11:08:51 -08:00
parent 492805af9c
commit 789ce8dd75
66 changed files with 4758 additions and 19021 deletions

View file

@ -1,38 +1,35 @@
use crate::ItemHandle;
use gpui::{
elements::*, AnyElement, AnyViewHandle, AppContext, Entity, View, ViewContext, ViewHandle,
AnyView, Entity, EntityId, EventEmitter, ParentElement as _, Render, Styled, View, ViewContext,
WindowContext,
};
use ui::prelude::*;
use ui::{h_stack, v_stack};
pub trait ToolbarItemView: View {
pub enum ToolbarItemEvent {
ChangeLocation(ToolbarItemLocation),
}
pub trait ToolbarItemView: Render + EventEmitter<ToolbarItemEvent> {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn crate::ItemHandle>,
cx: &mut ViewContext<Self>,
) -> ToolbarItemLocation;
fn location_for_event(
&self,
_event: &Self::Event,
current_location: ToolbarItemLocation,
_cx: &AppContext,
) -> ToolbarItemLocation {
current_location
}
fn pane_focus_update(&mut self, _pane_focused: bool, _cx: &mut ViewContext<Self>) {}
/// Number of times toolbar's height will be repeated to get the effective height.
/// Useful when multiple rows one under each other are needed.
/// The rows have the same width and act as a whole when reacting to resizes and similar events.
fn row_count(&self, _cx: &ViewContext<Self>) -> usize {
fn row_count(&self, _cx: &WindowContext) -> usize {
1
}
}
trait ToolbarItemViewHandle {
fn id(&self) -> usize;
fn as_any(&self) -> &AnyViewHandle;
trait ToolbarItemViewHandle: Send {
fn id(&self) -> EntityId;
fn to_any(&self) -> AnyView;
fn set_active_pane_item(
&self,
active_pane_item: Option<&dyn ItemHandle>,
@ -45,8 +42,8 @@ trait ToolbarItemViewHandle {
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ToolbarItemLocation {
Hidden,
PrimaryLeft { flex: Option<(f32, bool)> },
PrimaryRight { flex: Option<(f32, bool)> },
PrimaryLeft,
PrimaryRight,
Secondary,
}
@ -57,140 +54,161 @@ pub struct Toolbar {
items: Vec<(Box<dyn ToolbarItemViewHandle>, ToolbarItemLocation)>,
}
impl Entity for Toolbar {
type Event = ();
}
impl View for Toolbar {
fn ui_name() -> &'static str {
"Toolbar"
impl Toolbar {
fn has_any_visible_items(&self) -> bool {
self.items
.iter()
.any(|(_item, location)| *location != ToolbarItemLocation::Hidden)
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
let theme = &theme::current(cx).workspace.toolbar;
let mut primary_left_items = Vec::new();
let mut primary_right_items = Vec::new();
let mut secondary_item = None;
let spacing = theme.item_spacing;
let mut primary_items_row_count = 1;
for (item, position) in &self.items {
match *position {
ToolbarItemLocation::Hidden => {}
ToolbarItemLocation::PrimaryLeft { flex } => {
primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
let left_item = ChildView::new(item.as_any(), cx).aligned();
if let Some((flex, expanded)) = flex {
primary_left_items.push(left_item.flex(flex, expanded).into_any());
} else {
primary_left_items.push(left_item.into_any());
}
}
ToolbarItemLocation::PrimaryRight { flex } => {
primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
let right_item = ChildView::new(item.as_any(), cx).aligned().flex_float();
if let Some((flex, expanded)) = flex {
primary_right_items.push(right_item.flex(flex, expanded).into_any());
} else {
primary_right_items.push(right_item.into_any());
}
}
ToolbarItemLocation::Secondary => {
secondary_item = Some(
ChildView::new(item.as_any(), cx)
.constrained()
.with_height(theme.height * item.row_count(cx) as f32)
.into_any(),
);
}
fn left_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
self.items.iter().filter_map(|(item, location)| {
if *location == ToolbarItemLocation::PrimaryLeft {
Some(item.as_ref())
} else {
None
}
}
})
}
let container_style = theme.container;
let height = theme.height * primary_items_row_count as f32;
fn right_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
self.items.iter().filter_map(|(item, location)| {
if *location == ToolbarItemLocation::PrimaryRight {
Some(item.as_ref())
} else {
None
}
})
}
let mut primary_items = Flex::row().with_spacing(spacing);
primary_items.extend(primary_left_items);
primary_items.extend(primary_right_items);
let mut toolbar = Flex::column();
if !primary_items.is_empty() {
toolbar.add_child(primary_items.constrained().with_height(height));
}
if let Some(secondary_item) = secondary_item {
toolbar.add_child(secondary_item);
}
if toolbar.is_empty() {
toolbar.into_any_named("toolbar")
} else {
toolbar
.contained()
.with_style(container_style)
.into_any_named("toolbar")
}
fn secondary_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
self.items.iter().filter_map(|(item, location)| {
if *location == ToolbarItemLocation::Secondary {
Some(item.as_ref())
} else {
None
}
})
}
}
// <<<<<<< HEAD
// =======
// #[allow(clippy::too_many_arguments)]
// fn nav_button<A: Action, F: 'static + Fn(&mut Toolbar, &mut ViewContext<Toolbar>)>(
// svg_path: &'static str,
// style: theme::Interactive<theme::IconButton>,
// nav_button_height: f32,
// tooltip_style: TooltipStyle,
// enabled: bool,
// spacing: f32,
// on_click: F,
// tooltip_action: A,
// action_name: &'static str,
// cx: &mut ViewContext<Toolbar>,
// ) -> AnyElement<Toolbar> {
// MouseEventHandler::new::<A, _>(0, cx, |state, _| {
// let style = if enabled {
// style.style_for(state)
impl Render for Toolbar {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
if !self.has_any_visible_items() {
return div();
}
let secondary_item = self.secondary_items().next().map(|item| item.to_any());
let has_left_items = self.left_items().count() > 0;
let has_right_items = self.right_items().count() > 0;
v_stack()
.p_2()
.when(has_left_items || has_right_items, |this| this.gap_2())
.border_b()
.border_color(cx.theme().colors().border_variant)
.bg(cx.theme().colors().toolbar_background)
.child(
h_stack()
.justify_between()
.when(has_left_items, |this| {
this.child(
h_stack()
.flex_1()
.justify_start()
.children(self.left_items().map(|item| item.to_any())),
)
})
.when(has_right_items, |this| {
this.child(
h_stack()
.flex_1()
.justify_end()
.children(self.right_items().map(|item| item.to_any())),
)
}),
)
.children(secondary_item)
}
}
// todo!()
// impl View for Toolbar {
// fn ui_name() -> &'static str {
// "Toolbar"
// }
// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
// let theme = &theme::current(cx).workspace.toolbar;
// let mut primary_left_items = Vec::new();
// let mut primary_right_items = Vec::new();
// let mut secondary_item = None;
// let spacing = theme.item_spacing;
// let mut primary_items_row_count = 1;
// for (item, position) in &self.items {
// match *position {
// ToolbarItemLocation::Hidden => {}
// ToolbarItemLocation::PrimaryLeft { flex } => {
// primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
// let left_item = ChildView::new(item.as_any(), cx).aligned();
// if let Some((flex, expanded)) = flex {
// primary_left_items.push(left_item.flex(flex, expanded).into_any());
// } else {
// primary_left_items.push(left_item.into_any());
// }
// }
// ToolbarItemLocation::PrimaryRight { flex } => {
// primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
// let right_item = ChildView::new(item.as_any(), cx).aligned().flex_float();
// if let Some((flex, expanded)) = flex {
// primary_right_items.push(right_item.flex(flex, expanded).into_any());
// } else {
// primary_right_items.push(right_item.into_any());
// }
// }
// ToolbarItemLocation::Secondary => {
// secondary_item = Some(
// ChildView::new(item.as_any(), cx)
// .constrained()
// .with_height(theme.height * item.row_count(cx) as f32)
// .into_any(),
// );
// }
// }
// }
// let container_style = theme.container;
// let height = theme.height * primary_items_row_count as f32;
// let mut primary_items = Flex::row().with_spacing(spacing);
// primary_items.extend(primary_left_items);
// primary_items.extend(primary_right_items);
// let mut toolbar = Flex::column();
// if !primary_items.is_empty() {
// toolbar.add_child(primary_items.constrained().with_height(height));
// }
// if let Some(secondary_item) = secondary_item {
// toolbar.add_child(secondary_item);
// }
// if toolbar.is_empty() {
// toolbar.into_any_named("toolbar")
// } else {
// style.disabled_style()
// };
// Svg::new(svg_path)
// .with_color(style.color)
// .constrained()
// .with_width(style.icon_width)
// .aligned()
// .contained()
// .with_style(style.container)
// .constrained()
// .with_width(style.button_width)
// .with_height(nav_button_height)
// .aligned()
// .top()
// })
// .with_cursor_style(if enabled {
// CursorStyle::PointingHand
// } else {
// CursorStyle::default()
// })
// .on_click(MouseButton::Left, move |_, toolbar, cx| {
// on_click(toolbar, cx)
// })
// .with_tooltip::<A>(
// 0,
// action_name,
// Some(Box::new(tooltip_action)),
// tooltip_style,
// cx,
// )
// .contained()
// .with_margin_right(spacing)
// .into_any_named("nav button")
// toolbar
// .contained()
// .with_style(container_style)
// .into_any_named("toolbar")
// }
// }
// }
// >>>>>>> 139cbbfd3aebd0863a7d51b0c12d748764cf0b2e
impl Toolbar {
pub fn new() -> Self {
Self {
@ -206,7 +224,7 @@ impl Toolbar {
cx.notify();
}
pub fn add_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
pub fn add_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
where
T: 'static + ToolbarItemView,
{
@ -215,12 +233,13 @@ impl Toolbar {
if let Some((_, current_location)) =
this.items.iter_mut().find(|(i, _)| i.id() == item.id())
{
let new_location = item
.read(cx)
.location_for_event(event, *current_location, cx);
if new_location != *current_location {
*current_location = new_location;
cx.notify();
match event {
ToolbarItemEvent::ChangeLocation(new_location) => {
if new_location != current_location {
*current_location = *new_location;
cx.notify();
}
}
}
}
})
@ -252,10 +271,10 @@ impl Toolbar {
}
}
pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<ViewHandle<T>> {
pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<View<T>> {
self.items
.iter()
.find_map(|(item, _)| item.as_any().clone().downcast())
.find_map(|(item, _)| item.to_any().downcast().ok())
}
pub fn hidden(&self) -> bool {
@ -263,13 +282,13 @@ impl Toolbar {
}
}
impl<T: ToolbarItemView> ToolbarItemViewHandle for ViewHandle<T> {
fn id(&self) -> usize {
self.id()
impl<T: ToolbarItemView> ToolbarItemViewHandle for View<T> {
fn id(&self) -> EntityId {
self.entity_id()
}
fn as_any(&self) -> &AnyViewHandle {
self
fn to_any(&self) -> AnyView {
self.clone().into()
}
fn set_active_pane_item(
@ -290,12 +309,13 @@ impl<T: ToolbarItemView> ToolbarItemViewHandle for ViewHandle<T> {
}
fn row_count(&self, cx: &WindowContext) -> usize {
self.read_with(cx, |this, cx| this.row_count(cx))
self.read(cx).row_count(cx)
}
}
impl From<&dyn ToolbarItemViewHandle> for AnyViewHandle {
fn from(val: &dyn ToolbarItemViewHandle) -> Self {
val.as_any().clone()
}
}
// todo!()
// impl From<&dyn ToolbarItemViewHandle> for AnyViewHandle {
// fn from(val: &dyn ToolbarItemViewHandle) -> Self {
// val.as_any().clone()
// }
// }