From eb4ac2c27688267a13cbfdd8b358054adb8af7d0 Mon Sep 17 00:00:00 2001 From: KCaverly Date: Tue, 31 Oct 2023 10:12:40 -0400 Subject: [PATCH] wip --- crates/workspace2/src/status_bar.rs | 162 ++++++++++++++-------------- 1 file changed, 83 insertions(+), 79 deletions(-) diff --git a/crates/workspace2/src/status_bar.rs b/crates/workspace2/src/status_bar.rs index b68b366c7cae59c8cb4430f468bc871f28dad119..4567367be63435ae69ce3476131724785d517f70 100644 --- a/crates/workspace2/src/status_bar.rs +++ b/crates/workspace2/src/status_bar.rs @@ -1,6 +1,8 @@ +use std::any::TypeId; + use crate::{ItemHandle, Pane}; use gpui2::{AnyView, Render, Subscription, View, ViewContext, WindowContext}; -use std::ops::Range; +use util::ResultExt; pub trait StatusItemView: Render { fn set_active_pane_item( @@ -10,14 +12,14 @@ pub trait StatusItemView: Render { ); } -trait StatusItemViewHandle { +trait StatusItemViewHandle: Send { fn to_any(&self) -> AnyView; fn set_active_pane_item( &self, active_pane_item: Option<&dyn ItemHandle>, cx: &mut WindowContext, ); - fn ui_name(&self) -> &'static str; + fn item_type(&self) -> TypeId; } pub struct StatusBar { @@ -87,7 +89,7 @@ impl StatusBar { self.left_items .iter() .chain(self.right_items.iter()) - .find_map(|item| item.as_any().clone().downcast()) + .find_map(|item| item.to_any().clone().downcast().log_err()) } pub fn position_of_item(&self) -> Option @@ -95,12 +97,12 @@ impl StatusBar { T: StatusItemView, { for (index, item) in self.left_items.iter().enumerate() { - if item.as_ref().ui_name() == T::ui_name() { + if item.item_type() == TypeId::of::() { return Some(index); } } for (index, item) in self.right_items.iter().enumerate() { - if item.as_ref().ui_name() == T::ui_name() { + if item.item_type() == TypeId::of::() { return Some(index + self.left_items.len()); } } @@ -110,7 +112,7 @@ impl StatusBar { pub fn insert_item_after( &mut self, position: usize, - item: ViewHandle, + item: View, cx: &mut ViewContext, ) where T: 'static + StatusItemView, @@ -141,7 +143,7 @@ impl StatusBar { cx.notify(); } - pub fn set_active_pane(&mut self, active_pane: &ViewHandle, cx: &mut ViewContext) { + pub fn set_active_pane(&mut self, active_pane: &View, cx: &mut ViewContext) { self.active_pane = active_pane.clone(); self._observe_active_pane = cx.observe(active_pane, |this, _, cx| this.update_active_pane_item(cx)); @@ -156,9 +158,9 @@ impl StatusBar { } } -impl StatusItemViewHandle for ViewHandle { - fn as_any(&self) -> &AnyViewHandle { - self +impl StatusItemViewHandle for View { + fn to_any(&self) -> AnyView { + self.clone().into_any() } fn set_active_pane_item( @@ -171,88 +173,90 @@ impl StatusItemViewHandle for ViewHandle { }); } - fn ui_name(&self) -> &'static str { - T::ui_name() + fn item_type(&self) -> TypeId { + TypeId::of::() } } -impl From<&dyn StatusItemViewHandle> for AnyViewHandle { +impl From<&dyn StatusItemViewHandle> for AnyView { fn from(val: &dyn StatusItemViewHandle) -> Self { - val.as_any().clone() + val.to_any().clone() } } -struct StatusBarElement { - left: AnyElement, - right: AnyElement, -} +// todo!() +// struct StatusBarElement { +// left: AnyElement, +// right: AnyElement, +// } -impl Element for StatusBarElement { - type LayoutState = (); - type PaintState = (); +// todo!() +// impl Element for StatusBarElement { +// type LayoutState = (); +// type PaintState = (); - fn layout( - &mut self, - mut constraint: SizeConstraint, - view: &mut StatusBar, - cx: &mut ViewContext, - ) -> (Vector2F, Self::LayoutState) { - let max_width = constraint.max.x(); - constraint.min = vec2f(0., constraint.min.y()); +// fn layout( +// &mut self, +// mut constraint: SizeConstraint, +// view: &mut StatusBar, +// cx: &mut ViewContext, +// ) -> (Vector2F, Self::LayoutState) { +// let max_width = constraint.max.x(); +// constraint.min = vec2f(0., constraint.min.y()); - let right_size = self.right.layout(constraint, view, cx); - let constraint = SizeConstraint::new( - vec2f(0., constraint.min.y()), - vec2f(max_width - right_size.x(), constraint.max.y()), - ); +// let right_size = self.right.layout(constraint, view, cx); +// let constraint = SizeConstraint::new( +// vec2f(0., constraint.min.y()), +// vec2f(max_width - right_size.x(), constraint.max.y()), +// ); - self.left.layout(constraint, view, cx); +// self.left.layout(constraint, view, cx); - (vec2f(max_width, right_size.y()), ()) - } +// (vec2f(max_width, right_size.y()), ()) +// } - fn paint( - &mut self, - bounds: RectF, - visible_bounds: RectF, - _: &mut Self::LayoutState, - view: &mut StatusBar, - cx: &mut ViewContext, - ) -> Self::PaintState { - let origin_y = bounds.upper_right().y(); - let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); +// fn paint( +// &mut self, +// bounds: RectF, +// visible_bounds: RectF, +// _: &mut Self::LayoutState, +// view: &mut StatusBar, +// cx: &mut ViewContext, +// ) -> Self::PaintState { +// let origin_y = bounds.upper_right().y(); +// let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); - let left_origin = vec2f(bounds.lower_left().x(), origin_y); - self.left.paint(left_origin, visible_bounds, view, cx); +// let left_origin = vec2f(bounds.lower_left().x(), origin_y); +// self.left.paint(left_origin, visible_bounds, view, cx); - let right_origin = vec2f(bounds.upper_right().x() - self.right.size().x(), origin_y); - self.right.paint(right_origin, visible_bounds, view, cx); - } +// let right_origin = vec2f(bounds.upper_right().x() - self.right.size().x(), origin_y); +// self.right.paint(right_origin, visible_bounds, view, cx); +// } - fn rect_for_text_range( - &self, - _: Range, - _: RectF, - _: RectF, - _: &Self::LayoutState, - _: &Self::PaintState, - _: &StatusBar, - _: &ViewContext, - ) -> Option { - None - } +// fn rect_for_text_range( +// &self, +// _: Range, +// _: RectF, +// _: RectF, +// _: &Self::LayoutState, +// _: &Self::PaintState, +// _: &StatusBar, +// _: &ViewContext, +// ) -> Option { +// None +// } - fn debug( - &self, - bounds: RectF, - _: &Self::LayoutState, - _: &Self::PaintState, - _: &StatusBar, - _: &ViewContext, - ) -> serde_json::Value { - json!({ - "type": "StatusBarElement", - "bounds": bounds.to_json() - }) - } -} +// fn debug( +// &self, +// bounds: RectF, +// _: &Self::LayoutState, +// _: &Self::PaintState, +// _: &StatusBar, +// _: &ViewContext, +// ) -> serde_json::Value { +// json!({ +// "type": "StatusBarElement", +// "bounds": bounds.to_json() +// }) +// } +// }