dock compiling with todos outstanding

Co-Authored-By: Kirill <kirill@zed.dev>
This commit is contained in:
KCaverly 2023-10-31 09:25:36 -04:00
parent bbe2dd1f8f
commit 8d0905e479

View file

@ -1,13 +1,12 @@
use crate::{Axis, Workspace}; use crate::{status_bar::StatusItemView, Axis, Workspace};
use gpui2::{ use gpui2::{
Action, AnyElement, AnyView, AppContext, Render, Subscription, View, ViewContext, WeakView, Action, AnyView, EventEmitter, Render, Subscription, View, ViewContext, WeakView, WindowContext,
WindowContext,
}; };
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
pub trait Panel: Render { pub trait Panel: Render + EventEmitter {
fn position(&self, cx: &WindowContext) -> DockPosition; fn position(&self, cx: &WindowContext) -> DockPosition;
fn position_is_valid(&self, position: DockPosition) -> bool; fn position_is_valid(&self, position: DockPosition) -> bool;
fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>); fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>);
@ -177,226 +176,226 @@ pub struct PanelButtons {
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
} }
impl Dock { // impl Dock {
pub fn new(position: DockPosition) -> Self { // pub fn new(position: DockPosition) -> Self {
Self { // Self {
position, // position,
panel_entries: Default::default(), // panel_entries: Default::default(),
active_panel_index: 0, // active_panel_index: 0,
is_open: false, // is_open: false,
} // }
} // }
pub fn position(&self) -> DockPosition { // pub fn position(&self) -> DockPosition {
self.position // self.position
} // }
pub fn is_open(&self) -> bool { // pub fn is_open(&self) -> bool {
self.is_open // self.is_open
} // }
pub fn has_focus(&self, cx: &WindowContext) -> bool { // pub fn has_focus(&self, cx: &WindowContext) -> bool {
self.visible_panel() // self.visible_panel()
.map_or(false, |panel| panel.has_focus(cx)) // .map_or(false, |panel| panel.has_focus(cx))
} // }
pub fn panel<T: Panel>(&self) -> Option<View<T>> { // pub fn panel<T: Panel>(&self) -> Option<View<T>> {
self.panel_entries // self.panel_entries
.iter() // .iter()
.find_map(|entry| entry.panel.as_any().clone().downcast()) // .find_map(|entry| entry.panel.as_any().clone().downcast())
} // }
pub fn panel_index_for_type<T: Panel>(&self) -> Option<usize> { // pub fn panel_index_for_type<T: Panel>(&self) -> Option<usize> {
self.panel_entries // self.panel_entries
.iter() // .iter()
.position(|entry| entry.panel.as_any().is::<T>()) // .position(|entry| entry.panel.as_any().is::<T>())
} // }
pub fn panel_index_for_ui_name(&self, ui_name: &str, cx: &AppContext) -> Option<usize> { // pub fn panel_index_for_ui_name(&self, ui_name: &str, cx: &AppContext) -> Option<usize> {
todo!() // todo!()
// self.panel_entries.iter().position(|entry| { // // self.panel_entries.iter().position(|entry| {
// let panel = entry.panel.as_any(); // // let panel = entry.panel.as_any();
// cx.view_ui_name(panel.window(), panel.id()) == Some(ui_name) // // cx.view_ui_name(panel.window(), panel.id()) == Some(ui_name)
// }) // // })
} // }
pub fn active_panel_index(&self) -> usize { // pub fn active_panel_index(&self) -> usize {
self.active_panel_index // self.active_panel_index
} // }
pub(crate) fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) { // pub(crate) fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
if open != self.is_open { // if open != self.is_open {
self.is_open = open; // self.is_open = open;
if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) { // if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
active_panel.panel.set_active(open, cx); // active_panel.panel.set_active(open, cx);
} // }
cx.notify(); // cx.notify();
} // }
} // }
pub fn set_panel_zoomed(&mut self, panel: &AnyView, zoomed: bool, cx: &mut ViewContext<Self>) { // pub fn set_panel_zoomed(&mut self, panel: &AnyView, zoomed: bool, cx: &mut ViewContext<Self>) {
for entry in &mut self.panel_entries { // for entry in &mut self.panel_entries {
if entry.panel.as_any() == panel { // if entry.panel.as_any() == panel {
if zoomed != entry.panel.is_zoomed(cx) { // if zoomed != entry.panel.is_zoomed(cx) {
entry.panel.set_zoomed(zoomed, cx); // entry.panel.set_zoomed(zoomed, cx);
} // }
} else if entry.panel.is_zoomed(cx) { // } else if entry.panel.is_zoomed(cx) {
entry.panel.set_zoomed(false, cx); // entry.panel.set_zoomed(false, cx);
} // }
} // }
cx.notify(); // cx.notify();
} // }
pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) { // pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {
for entry in &mut self.panel_entries { // for entry in &mut self.panel_entries {
if entry.panel.is_zoomed(cx) { // if entry.panel.is_zoomed(cx) {
entry.panel.set_zoomed(false, cx); // entry.panel.set_zoomed(false, cx);
} // }
} // }
} // }
pub(crate) fn add_panel<T: Panel>(&mut self, panel: View<T>, cx: &mut ViewContext<Self>) { // pub(crate) fn add_panel<T: Panel>(&mut self, panel: View<T>, cx: &mut ViewContext<Self>) {
let subscriptions = [ // let subscriptions = [
cx.observe(&panel, |_, _, cx| cx.notify()), // cx.observe(&panel, |_, _, cx| cx.notify()),
cx.subscribe(&panel, |this, panel, event, cx| { // cx.subscribe(&panel, |this, panel, event, cx| {
if T::should_activate_on_event(event) { // if T::should_activate_on_event(event) {
if let Some(ix) = this // if let Some(ix) = this
.panel_entries // .panel_entries
.iter() // .iter()
.position(|entry| entry.panel.id() == panel.id()) // .position(|entry| entry.panel.id() == panel.id())
{ // {
this.set_open(true, cx); // this.set_open(true, cx);
this.activate_panel(ix, cx); // this.activate_panel(ix, cx);
cx.focus(&panel); // cx.focus(&panel);
} // }
} else if T::should_close_on_event(event) // } else if T::should_close_on_event(event)
&& this.visible_panel().map_or(false, |p| p.id() == panel.id()) // && this.visible_panel().map_or(false, |p| p.id() == panel.id())
{ // {
this.set_open(false, cx); // this.set_open(false, cx);
} // }
}), // }),
]; // ];
let dock_view_id = cx.view_id(); // let dock_view_id = cx.view_id();
self.panel_entries.push(PanelEntry { // self.panel_entries.push(PanelEntry {
panel: Arc::new(panel), // panel: Arc::new(panel),
// todo!() // // todo!()
// context_menu: cx.add_view(|cx| { // // context_menu: cx.add_view(|cx| {
// let mut menu = ContextMenu::new(dock_view_id, cx); // // let mut menu = ContextMenu::new(dock_view_id, cx);
// menu.set_position_mode(OverlayPositionMode::Local); // // menu.set_position_mode(OverlayPositionMode::Local);
// menu // // menu
// }), // // }),
_subscriptions: subscriptions, // _subscriptions: subscriptions,
}); // });
cx.notify() // cx.notify()
} // }
pub fn remove_panel<T: Panel>(&mut self, panel: &View<T>, cx: &mut ViewContext<Self>) { // pub fn remove_panel<T: Panel>(&mut self, panel: &View<T>, cx: &mut ViewContext<Self>) {
if let Some(panel_ix) = self // if let Some(panel_ix) = self
.panel_entries // .panel_entries
.iter() // .iter()
.position(|entry| entry.panel.id() == panel.id()) // .position(|entry| entry.panel.id() == panel.id())
{ // {
if panel_ix == self.active_panel_index { // if panel_ix == self.active_panel_index {
self.active_panel_index = 0; // self.active_panel_index = 0;
self.set_open(false, cx); // self.set_open(false, cx);
} else if panel_ix < self.active_panel_index { // } else if panel_ix < self.active_panel_index {
self.active_panel_index -= 1; // self.active_panel_index -= 1;
} // }
self.panel_entries.remove(panel_ix); // self.panel_entries.remove(panel_ix);
cx.notify(); // cx.notify();
} // }
} // }
pub fn panels_len(&self) -> usize { // pub fn panels_len(&self) -> usize {
self.panel_entries.len() // self.panel_entries.len()
} // }
pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) { // pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
if panel_ix != self.active_panel_index { // if panel_ix != self.active_panel_index {
if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) { // if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
active_panel.panel.set_active(false, cx); // active_panel.panel.set_active(false, cx);
} // }
self.active_panel_index = panel_ix; // self.active_panel_index = panel_ix;
if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) { // if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
active_panel.panel.set_active(true, cx); // active_panel.panel.set_active(true, cx);
} // }
cx.notify(); // cx.notify();
} // }
} // }
pub fn visible_panel(&self) -> Option<&Arc<dyn PanelHandle>> { // pub fn visible_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
let entry = self.visible_entry()?; // let entry = self.visible_entry()?;
Some(&entry.panel) // Some(&entry.panel)
} // }
pub fn active_panel(&self) -> Option<&Arc<dyn PanelHandle>> { // pub fn active_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
Some(&self.panel_entries.get(self.active_panel_index)?.panel) // Some(&self.panel_entries.get(self.active_panel_index)?.panel)
} // }
fn visible_entry(&self) -> Option<&PanelEntry> { // fn visible_entry(&self) -> Option<&PanelEntry> {
if self.is_open { // if self.is_open {
self.panel_entries.get(self.active_panel_index) // self.panel_entries.get(self.active_panel_index)
} else { // } else {
None // None
} // }
} // }
pub fn zoomed_panel(&self, cx: &WindowContext) -> Option<Arc<dyn PanelHandle>> { // pub fn zoomed_panel(&self, cx: &WindowContext) -> Option<Arc<dyn PanelHandle>> {
let entry = self.visible_entry()?; // let entry = self.visible_entry()?;
if entry.panel.is_zoomed(cx) { // if entry.panel.is_zoomed(cx) {
Some(entry.panel.clone()) // Some(entry.panel.clone())
} else { // } else {
None // None
} // }
} // }
pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<f32> { // pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<f32> {
self.panel_entries // self.panel_entries
.iter() // .iter()
.find(|entry| entry.panel.id() == panel.id()) // .find(|entry| entry.panel.id() == panel.id())
.map(|entry| entry.panel.size(cx)) // .map(|entry| entry.panel.size(cx))
} // }
pub fn active_panel_size(&self, cx: &WindowContext) -> Option<f32> { // pub fn active_panel_size(&self, cx: &WindowContext) -> Option<f32> {
if self.is_open { // if self.is_open {
self.panel_entries // self.panel_entries
.get(self.active_panel_index) // .get(self.active_panel_index)
.map(|entry| entry.panel.size(cx)) // .map(|entry| entry.panel.size(cx))
} else { // } else {
None // None
} // }
} // }
pub fn resize_active_panel(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) { // pub fn resize_active_panel(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) {
if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) { // if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
entry.panel.set_size(size, cx); // entry.panel.set_size(size, cx);
cx.notify(); // cx.notify();
} // }
} // }
pub fn render_placeholder(&self, cx: &WindowContext) -> AnyElement<Workspace> { // pub fn render_placeholder(&self, cx: &WindowContext) -> AnyElement<Workspace> {
todo!() // todo!()
// if let Some(active_entry) = self.visible_entry() { // if let Some(active_entry) = self.visible_entry() {
// Empty::new() // Empty::new()
// .into_any() // .into_any()
// .contained() // .contained()
// .with_style(self.style(cx)) // .with_style(self.style(cx))
// .resizable::<WorkspaceBounds>( // .resizable::<WorkspaceBounds>(
// self.position.to_resize_handle_side(), // self.position.to_resize_handle_side(),
// active_entry.panel.size(cx), // active_entry.panel.size(cx),
// |_, _, _| {}, // |_, _, _| {},
// ) // )
// .into_any() // .into_any()
// } else { // } else {
// Empty::new().into_any() // Empty::new().into_any()
// } // }
} // }
} // }
// todo!() // todo!()
// impl View for Dock { // impl View for Dock {
@ -444,13 +443,17 @@ impl Dock {
// } // }
// } // }
// todo!() impl EventEmitter for PanelButtons {
// impl Entity for PanelButtons { type Event = ();
// type Event = (); }
// impl Render for PanelButtons {
// type Element = ();
// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
// todo!("")
// } // }
// todo!()
// impl View for PanelButtons {
// fn ui_name() -> &'static str { // fn ui_name() -> &'static str {
// "PanelButtons" // "PanelButtons"
// } // }
@ -593,14 +596,15 @@ impl Dock {
// } // }
// } // }
impl StatusItemView for PanelButtons { // impl StatusItemView for PanelButtons {
fn set_active_pane_item( // fn set_active_pane_item(
&mut self, // &mut self,
_: Option<&dyn crate::ItemHandle>, // active_pane_item: Option<&dyn crate::ItemHandle>,
_: &mut ViewContext<Self>, // cx: &mut ViewContext<Self>,
) { // ) {
} // todo!()
} // }
// }
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
pub mod test { pub mod test {
@ -625,6 +629,10 @@ pub mod test {
pub size: f32, pub size: f32,
} }
impl EventEmitter for TestPanel {
type Event = TestPanelEvent;
}
impl TestPanel { impl TestPanel {
pub fn new(position: DockPosition) -> Self { pub fn new(position: DockPosition) -> Self {
Self { Self {