1use crate::{status_bar::StatusItemView, Axis, Workspace};
2use gpui::{
3 div, Action, AnyView, AppContext, Div, Entity, EntityId, EventEmitter, FocusHandle,
4 ParentElement, Render, Subscription, View, ViewContext, WeakView, WindowContext,
5};
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9
10pub enum PanelEvent {
11 ChangePosition,
12 ZoomIn,
13 ZoomOut,
14 Activate,
15 Close,
16 Focus,
17}
18
19pub trait Panel: Render + EventEmitter<PanelEvent> {
20 fn persistent_name(&self) -> &'static str;
21 fn position(&self, cx: &WindowContext) -> DockPosition;
22 fn position_is_valid(&self, position: DockPosition) -> bool;
23 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>);
24 fn size(&self, cx: &WindowContext) -> f32;
25 fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>);
26 fn icon_path(&self, cx: &WindowContext) -> Option<&'static str>;
27 fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>);
28 fn icon_label(&self, _: &WindowContext) -> Option<String> {
29 None
30 }
31 fn is_zoomed(&self, _cx: &WindowContext) -> bool {
32 false
33 }
34 fn set_zoomed(&mut self, _zoomed: bool, _cx: &mut ViewContext<Self>) {}
35 fn set_active(&mut self, _active: bool, _cx: &mut ViewContext<Self>) {}
36 fn has_focus(&self, cx: &WindowContext) -> bool;
37 fn focus_handle(&self, cx: &WindowContext) -> FocusHandle;
38}
39
40pub trait PanelHandle: Send + Sync {
41 fn id(&self) -> EntityId;
42 fn persistent_name(&self, cx: &WindowContext) -> &'static str;
43 fn position(&self, cx: &WindowContext) -> DockPosition;
44 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;
45 fn set_position(&self, position: DockPosition, cx: &mut WindowContext);
46 fn is_zoomed(&self, cx: &WindowContext) -> bool;
47 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
48 fn set_active(&self, active: bool, cx: &mut WindowContext);
49 fn size(&self, cx: &WindowContext) -> f32;
50 fn set_size(&self, size: Option<f32>, cx: &mut WindowContext);
51 fn icon_path(&self, cx: &WindowContext) -> Option<&'static str>;
52 fn icon_tooltip(&self, cx: &WindowContext) -> (String, Option<Box<dyn Action>>);
53 fn icon_label(&self, cx: &WindowContext) -> Option<String>;
54 fn has_focus(&self, cx: &WindowContext) -> bool;
55 fn focus_handle(&self, cx: &WindowContext) -> FocusHandle;
56 fn to_any(&self) -> AnyView;
57}
58
59impl<T> PanelHandle for View<T>
60where
61 T: Panel,
62{
63 fn id(&self) -> EntityId {
64 self.entity_id()
65 }
66
67 fn persistent_name(&self, cx: &WindowContext) -> &'static str {
68 self.read(cx).persistent_name()
69 }
70
71 fn position(&self, cx: &WindowContext) -> DockPosition {
72 self.read(cx).position(cx)
73 }
74
75 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool {
76 self.read(cx).position_is_valid(position)
77 }
78
79 fn set_position(&self, position: DockPosition, cx: &mut WindowContext) {
80 self.update(cx, |this, cx| this.set_position(position, cx))
81 }
82
83 fn is_zoomed(&self, cx: &WindowContext) -> bool {
84 self.read(cx).is_zoomed(cx)
85 }
86
87 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext) {
88 self.update(cx, |this, cx| this.set_zoomed(zoomed, cx))
89 }
90
91 fn set_active(&self, active: bool, cx: &mut WindowContext) {
92 self.update(cx, |this, cx| this.set_active(active, cx))
93 }
94
95 fn size(&self, cx: &WindowContext) -> f32 {
96 self.read(cx).size(cx)
97 }
98
99 fn set_size(&self, size: Option<f32>, cx: &mut WindowContext) {
100 self.update(cx, |this, cx| this.set_size(size, cx))
101 }
102
103 fn icon_path(&self, cx: &WindowContext) -> Option<&'static str> {
104 self.read(cx).icon_path(cx)
105 }
106
107 fn icon_tooltip(&self, cx: &WindowContext) -> (String, Option<Box<dyn Action>>) {
108 self.read(cx).icon_tooltip()
109 }
110
111 fn icon_label(&self, cx: &WindowContext) -> Option<String> {
112 self.read(cx).icon_label(cx)
113 }
114
115 fn has_focus(&self, cx: &WindowContext) -> bool {
116 self.read(cx).has_focus(cx)
117 }
118
119 fn to_any(&self) -> AnyView {
120 self.clone().into()
121 }
122
123 fn focus_handle(&self, cx: &WindowContext) -> FocusHandle {
124 self.read(cx).focus_handle(cx).clone()
125 }
126}
127
128impl From<&dyn PanelHandle> for AnyView {
129 fn from(val: &dyn PanelHandle) -> Self {
130 val.to_any()
131 }
132}
133
134pub struct Dock {
135 position: DockPosition,
136 panel_entries: Vec<PanelEntry>,
137 is_open: bool,
138 active_panel_index: usize,
139}
140
141#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
142#[serde(rename_all = "lowercase")]
143pub enum DockPosition {
144 Left,
145 Bottom,
146 Right,
147}
148
149impl DockPosition {
150 fn to_label(&self) -> &'static str {
151 match self {
152 Self::Left => "left",
153 Self::Bottom => "bottom",
154 Self::Right => "right",
155 }
156 }
157
158 // todo!()
159 // fn to_resize_handle_side(self) -> HandleSide {
160 // match self {
161 // Self::Left => HandleSide::Right,
162 // Self::Bottom => HandleSide::Top,
163 // Self::Right => HandleSide::Left,
164 // }
165 // }
166
167 pub fn axis(&self) -> Axis {
168 match self {
169 Self::Left | Self::Right => Axis::Horizontal,
170 Self::Bottom => Axis::Vertical,
171 }
172 }
173}
174
175struct PanelEntry {
176 panel: Arc<dyn PanelHandle>,
177 // todo!()
178 // context_menu: View<ContextMenu>,
179 _subscriptions: [Subscription; 2],
180}
181
182pub struct PanelButtons {
183 dock: View<Dock>,
184 workspace: WeakView<Workspace>,
185}
186
187impl Dock {
188 pub fn new(position: DockPosition) -> Self {
189 Self {
190 position,
191 panel_entries: Default::default(),
192 active_panel_index: 0,
193 is_open: false,
194 }
195 }
196
197 pub fn position(&self) -> DockPosition {
198 self.position
199 }
200
201 pub fn is_open(&self) -> bool {
202 self.is_open
203 }
204
205 // pub fn has_focus(&self, cx: &WindowContext) -> bool {
206 // self.visible_panel()
207 // .map_or(false, |panel| panel.has_focus(cx))
208 // }
209
210 // pub fn panel<T: Panel>(&self) -> Option<View<T>> {
211 // self.panel_entries
212 // .iter()
213 // .find_map(|entry| entry.panel.as_any().clone().downcast())
214 // }
215
216 // pub fn panel_index_for_type<T: Panel>(&self) -> Option<usize> {
217 // self.panel_entries
218 // .iter()
219 // .position(|entry| entry.panel.as_any().is::<T>())
220 // }
221
222 pub fn panel_index_for_ui_name(&self, _ui_name: &str, _cx: &AppContext) -> Option<usize> {
223 todo!()
224 // self.panel_entries.iter().position(|entry| {
225 // let panel = entry.panel.as_any();
226 // cx.view_ui_name(panel.window(), panel.id()) == Some(ui_name)
227 // })
228 }
229
230 pub fn active_panel_index(&self) -> usize {
231 self.active_panel_index
232 }
233
234 pub(crate) fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
235 if open != self.is_open {
236 self.is_open = open;
237 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
238 active_panel.panel.set_active(open, cx);
239 }
240
241 cx.notify();
242 }
243 }
244
245 // todo!()
246 // pub fn set_panel_zoomed(&mut self, panel: &AnyView, zoomed: bool, cx: &mut ViewContext<Self>) {
247 // for entry in &mut self.panel_entries {
248 // if entry.panel.as_any() == panel {
249 // if zoomed != entry.panel.is_zoomed(cx) {
250 // entry.panel.set_zoomed(zoomed, cx);
251 // }
252 // } else if entry.panel.is_zoomed(cx) {
253 // entry.panel.set_zoomed(false, cx);
254 // }
255 // }
256
257 // cx.notify();
258 // }
259
260 pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {
261 for entry in &mut self.panel_entries {
262 if entry.panel.is_zoomed(cx) {
263 entry.panel.set_zoomed(false, cx);
264 }
265 }
266 }
267
268 pub(crate) fn add_panel<T: Panel>(&mut self, panel: View<T>, cx: &mut ViewContext<Self>) {
269 let subscriptions = [
270 cx.observe(&panel, |_, _, cx| cx.notify()),
271 cx.subscribe(&panel, |this, panel, event, cx| {
272 match event {
273 PanelEvent::ChangePosition => {
274 //todo!()
275 // see: Workspace::add_panel_with_extra_event_handler
276 }
277 PanelEvent::ZoomIn => {
278 //todo!()
279 // see: Workspace::add_panel_with_extra_event_handler
280 }
281 PanelEvent::ZoomOut => {
282 // todo!()
283 // // see: Workspace::add_panel_with_extra_event_handler
284 }
285 PanelEvent::Activate => {
286 if let Some(ix) = this
287 .panel_entries
288 .iter()
289 .position(|entry| entry.panel.id() == panel.id())
290 {
291 this.set_open(true, cx);
292 this.activate_panel(ix, cx);
293 //` todo!()
294 // cx.focus(&panel);
295 }
296 }
297 PanelEvent::Close => {
298 if this.visible_panel().map_or(false, |p| p.id() == panel.id()) {
299 this.set_open(false, cx);
300 }
301 }
302 PanelEvent::Focus => todo!(),
303 }
304 }),
305 ];
306
307 // todo!()
308 // let dock_view_id = cx.view_id();
309 self.panel_entries.push(PanelEntry {
310 panel: Arc::new(panel),
311 // todo!()
312 // context_menu: cx.add_view(|cx| {
313 // let mut menu = ContextMenu::new(dock_view_id, cx);
314 // menu.set_position_mode(OverlayPositionMode::Local);
315 // menu
316 // }),
317 _subscriptions: subscriptions,
318 });
319 cx.notify()
320 }
321
322 pub fn remove_panel<T: Panel>(&mut self, panel: &View<T>, cx: &mut ViewContext<Self>) {
323 if let Some(panel_ix) = self
324 .panel_entries
325 .iter()
326 .position(|entry| entry.panel.id() == panel.id())
327 {
328 if panel_ix == self.active_panel_index {
329 self.active_panel_index = 0;
330 self.set_open(false, cx);
331 } else if panel_ix < self.active_panel_index {
332 self.active_panel_index -= 1;
333 }
334 self.panel_entries.remove(panel_ix);
335 cx.notify();
336 }
337 }
338
339 pub fn panels_len(&self) -> usize {
340 self.panel_entries.len()
341 }
342
343 pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
344 if panel_ix != self.active_panel_index {
345 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
346 active_panel.panel.set_active(false, cx);
347 }
348
349 self.active_panel_index = panel_ix;
350 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
351 active_panel.panel.set_active(true, cx);
352 }
353
354 cx.notify();
355 }
356 }
357
358 pub fn visible_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
359 let entry = self.visible_entry()?;
360 Some(&entry.panel)
361 }
362
363 pub fn active_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
364 Some(&self.panel_entries.get(self.active_panel_index)?.panel)
365 }
366
367 fn visible_entry(&self) -> Option<&PanelEntry> {
368 if self.is_open {
369 self.panel_entries.get(self.active_panel_index)
370 } else {
371 None
372 }
373 }
374
375 pub fn zoomed_panel(&self, cx: &WindowContext) -> Option<Arc<dyn PanelHandle>> {
376 let entry = self.visible_entry()?;
377 if entry.panel.is_zoomed(cx) {
378 Some(entry.panel.clone())
379 } else {
380 None
381 }
382 }
383
384 pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<f32> {
385 self.panel_entries
386 .iter()
387 .find(|entry| entry.panel.id() == panel.id())
388 .map(|entry| entry.panel.size(cx))
389 }
390
391 pub fn active_panel_size(&self, cx: &WindowContext) -> Option<f32> {
392 if self.is_open {
393 self.panel_entries
394 .get(self.active_panel_index)
395 .map(|entry| entry.panel.size(cx))
396 } else {
397 None
398 }
399 }
400
401 pub fn resize_active_panel(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) {
402 if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
403 entry.panel.set_size(size, cx);
404 cx.notify();
405 }
406 }
407
408 // pub fn render_placeholder(&self, cx: &WindowContext) -> AnyElement<Workspace> {
409 // todo!()
410 // if let Some(active_entry) = self.visible_entry() {
411 // Empty::new()
412 // .into_any()
413 // .contained()
414 // .with_style(self.style(cx))
415 // .resizable::<WorkspaceBounds>(
416 // self.position.to_resize_handle_side(),
417 // active_entry.panel.size(cx),
418 // |_, _, _| {},
419 // )
420 // .into_any()
421 // } else {
422 // Empty::new().into_any()
423 // }
424 // }
425}
426
427impl Render for Dock {
428 type Element = Div<Self>;
429
430 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
431 todo!()
432 }
433}
434
435// todo!()
436// impl View for Dock {
437// fn ui_name() -> &'static str {
438// "Dock"
439// }
440
441// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
442// if let Some(active_entry) = self.visible_entry() {
443// let style = self.style(cx);
444// ChildView::new(active_entry.panel.as_any(), cx)
445// .contained()
446// .with_style(style)
447// .resizable::<WorkspaceBounds>(
448// self.position.to_resize_handle_side(),
449// active_entry.panel.size(cx),
450// |dock: &mut Self, size, cx| dock.resize_active_panel(size, cx),
451// )
452// .into_any()
453// } else {
454// Empty::new().into_any()
455// }
456// }
457
458// fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
459// if cx.is_self_focused() {
460// if let Some(active_entry) = self.visible_entry() {
461// cx.focus(active_entry.panel.as_any());
462// } else {
463// cx.focus_parent();
464// }
465// }
466// }
467// }
468
469impl PanelButtons {
470 pub fn new(
471 dock: View<Dock>,
472 workspace: WeakView<Workspace>,
473 cx: &mut ViewContext<Self>,
474 ) -> Self {
475 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
476 Self { dock, workspace }
477 }
478}
479
480// impl Render for PanelButtons {
481// type Element = ();
482
483// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
484// todo!("")
485// }
486
487// fn ui_name() -> &'static str {
488// "PanelButtons"
489// }
490
491// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
492// let theme = &settings::get::<ThemeSettings>(cx).theme;
493// let tooltip_style = theme.tooltip.clone();
494// let theme = &theme.workspace.status_bar.panel_buttons;
495// let button_style = theme.button.clone();
496// let dock = self.dock.read(cx);
497// let active_ix = dock.active_panel_index;
498// let is_open = dock.is_open;
499// let dock_position = dock.position;
500// let group_style = match dock_position {
501// DockPosition::Left => theme.group_left,
502// DockPosition::Bottom => theme.group_bottom,
503// DockPosition::Right => theme.group_right,
504// };
505// let menu_corner = match dock_position {
506// DockPosition::Left => AnchorCorner::BottomLeft,
507// DockPosition::Bottom | DockPosition::Right => AnchorCorner::BottomRight,
508// };
509
510// let panels = dock
511// .panel_entries
512// .iter()
513// .map(|item| (item.panel.clone(), item.context_menu.clone()))
514// .collect::<Vec<_>>();
515// Flex::row()
516// .with_children(panels.into_iter().enumerate().filter_map(
517// |(panel_ix, (view, context_menu))| {
518// let icon_path = view.icon_path(cx)?;
519// let is_active = is_open && panel_ix == active_ix;
520// let (tooltip, tooltip_action) = if is_active {
521// (
522// format!("Close {} dock", dock_position.to_label()),
523// Some(match dock_position {
524// DockPosition::Left => crate::ToggleLeftDock.boxed_clone(),
525// DockPosition::Bottom => crate::ToggleBottomDock.boxed_clone(),
526// DockPosition::Right => crate::ToggleRightDock.boxed_clone(),
527// }),
528// )
529// } else {
530// view.icon_tooltip(cx)
531// };
532// Some(
533// Stack::new()
534// .with_child(
535// MouseEventHandler::new::<Self, _>(panel_ix, cx, |state, cx| {
536// let style = button_style.in_state(is_active);
537
538// let style = style.style_for(state);
539// Flex::row()
540// .with_child(
541// Svg::new(icon_path)
542// .with_color(style.icon_color)
543// .constrained()
544// .with_width(style.icon_size)
545// .aligned(),
546// )
547// .with_children(if let Some(label) = view.icon_label(cx) {
548// Some(
549// Label::new(label, style.label.text.clone())
550// .contained()
551// .with_style(style.label.container)
552// .aligned(),
553// )
554// } else {
555// None
556// })
557// .constrained()
558// .with_height(style.icon_size)
559// .contained()
560// .with_style(style.container)
561// })
562// .with_cursor_style(CursorStyle::PointingHand)
563// .on_click(MouseButton::Left, {
564// let tooltip_action =
565// tooltip_action.as_ref().map(|action| action.boxed_clone());
566// move |_, this, cx| {
567// if let Some(tooltip_action) = &tooltip_action {
568// let window = cx.window();
569// let view_id = this.workspace.id();
570// let tooltip_action = tooltip_action.boxed_clone();
571// cx.spawn(|_, mut cx| async move {
572// window.dispatch_action(
573// view_id,
574// &*tooltip_action,
575// &mut cx,
576// );
577// })
578// .detach();
579// }
580// }
581// })
582// .on_click(MouseButton::Right, {
583// let view = view.clone();
584// let menu = context_menu.clone();
585// move |_, _, cx| {
586// const POSITIONS: [DockPosition; 3] = [
587// DockPosition::Left,
588// DockPosition::Right,
589// DockPosition::Bottom,
590// ];
591
592// menu.update(cx, |menu, cx| {
593// let items = POSITIONS
594// .into_iter()
595// .filter(|position| {
596// *position != dock_position
597// && view.position_is_valid(*position, cx)
598// })
599// .map(|position| {
600// let view = view.clone();
601// ContextMenuItem::handler(
602// format!("Dock {}", position.to_label()),
603// move |cx| view.set_position(position, cx),
604// )
605// })
606// .collect();
607// menu.show(Default::default(), menu_corner, items, cx);
608// })
609// }
610// })
611// .with_tooltip::<Self>(
612// panel_ix,
613// tooltip,
614// tooltip_action,
615// tooltip_style.clone(),
616// cx,
617// ),
618// )
619// .with_child(ChildView::new(&context_menu, cx)),
620// )
621// },
622// ))
623// .contained()
624// .with_style(group_style)
625// .into_any()
626// }
627// }
628
629impl Render for PanelButtons {
630 type Element = Div<Self>;
631
632 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
633 // todo!()
634 let dock = self.dock.read(cx);
635 div().children(
636 dock.panel_entries
637 .iter()
638 .map(|panel| panel.panel.persistent_name(cx)),
639 )
640 }
641}
642
643impl StatusItemView for PanelButtons {
644 fn set_active_pane_item(
645 &mut self,
646 _active_pane_item: Option<&dyn crate::ItemHandle>,
647 _cx: &mut ViewContext<Self>,
648 ) {
649 // Nothing to do, panel buttons don't depend on the active center item
650 }
651}
652
653#[cfg(any(test, feature = "test-support"))]
654pub mod test {
655 use super::*;
656 use gpui::{div, Div, ViewContext, WindowContext};
657
658 pub struct TestPanel {
659 pub position: DockPosition,
660 pub zoomed: bool,
661 pub active: bool,
662 pub has_focus: bool,
663 pub size: f32,
664 }
665
666 impl EventEmitter<PanelEvent> for TestPanel {}
667
668 impl TestPanel {
669 pub fn new(position: DockPosition) -> Self {
670 Self {
671 position,
672 zoomed: false,
673 active: false,
674 has_focus: false,
675 size: 300.,
676 }
677 }
678 }
679
680 impl Render for TestPanel {
681 type Element = Div<Self>;
682
683 fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
684 div()
685 }
686 }
687
688 impl Panel for TestPanel {
689 fn persistent_name(&self) -> &'static str {
690 "TestPanel"
691 }
692
693 fn position(&self, _: &gpui::WindowContext) -> super::DockPosition {
694 self.position
695 }
696
697 fn position_is_valid(&self, _: super::DockPosition) -> bool {
698 true
699 }
700
701 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
702 self.position = position;
703 cx.emit(PanelEvent::ChangePosition);
704 }
705
706 fn size(&self, _: &WindowContext) -> f32 {
707 self.size
708 }
709
710 fn set_size(&mut self, size: Option<f32>, _: &mut ViewContext<Self>) {
711 self.size = size.unwrap_or(300.);
712 }
713
714 fn icon_path(&self, _: &WindowContext) -> Option<&'static str> {
715 Some("icons/test_panel.svg")
716 }
717
718 fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>) {
719 ("Test Panel".into(), None)
720 }
721
722 fn is_zoomed(&self, _: &WindowContext) -> bool {
723 self.zoomed
724 }
725
726 fn set_zoomed(&mut self, zoomed: bool, _cx: &mut ViewContext<Self>) {
727 self.zoomed = zoomed;
728 }
729
730 fn set_active(&mut self, active: bool, _cx: &mut ViewContext<Self>) {
731 self.active = active;
732 }
733
734 fn has_focus(&self, _cx: &WindowContext) -> bool {
735 self.has_focus
736 }
737
738 fn focus_handle(&self, cx: &WindowContext) -> FocusHandle {
739 unimplemented!()
740 }
741 }
742}