1use crate::{status_bar::StatusItemView, Axis, Workspace};
2use gpui::{
3 div, Action, AnyView, AppContext, Div, Entity, EntityId, EventEmitter, FocusHandle,
4 ParentElement, Render, Styled, 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 if let Some(entry) = self.visible_entry() {
432 div().size_full().child(entry.panel.to_any())
433 } else {
434 div()
435 }
436 }
437}
438
439// todo!()
440// impl View for Dock {
441// fn ui_name() -> &'static str {
442// "Dock"
443// }
444
445// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
446// if let Some(active_entry) = self.visible_entry() {
447// let style = self.style(cx);
448// ChildView::new(active_entry.panel.as_any(), cx)
449// .contained()
450// .with_style(style)
451// .resizable::<WorkspaceBounds>(
452// self.position.to_resize_handle_side(),
453// active_entry.panel.size(cx),
454// |dock: &mut Self, size, cx| dock.resize_active_panel(size, cx),
455// )
456// .into_any()
457// } else {
458// Empty::new().into_any()
459// }
460// }
461
462// fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
463// if cx.is_self_focused() {
464// if let Some(active_entry) = self.visible_entry() {
465// cx.focus(active_entry.panel.as_any());
466// } else {
467// cx.focus_parent();
468// }
469// }
470// }
471// }
472
473impl PanelButtons {
474 pub fn new(
475 dock: View<Dock>,
476 workspace: WeakView<Workspace>,
477 cx: &mut ViewContext<Self>,
478 ) -> Self {
479 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
480 Self { dock, workspace }
481 }
482}
483
484// impl Render for PanelButtons {
485// type Element = ();
486
487// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
488// todo!("")
489// }
490
491// fn ui_name() -> &'static str {
492// "PanelButtons"
493// }
494
495// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
496// let theme = &settings::get::<ThemeSettings>(cx).theme;
497// let tooltip_style = theme.tooltip.clone();
498// let theme = &theme.workspace.status_bar.panel_buttons;
499// let button_style = theme.button.clone();
500// let dock = self.dock.read(cx);
501// let active_ix = dock.active_panel_index;
502// let is_open = dock.is_open;
503// let dock_position = dock.position;
504// let group_style = match dock_position {
505// DockPosition::Left => theme.group_left,
506// DockPosition::Bottom => theme.group_bottom,
507// DockPosition::Right => theme.group_right,
508// };
509// let menu_corner = match dock_position {
510// DockPosition::Left => AnchorCorner::BottomLeft,
511// DockPosition::Bottom | DockPosition::Right => AnchorCorner::BottomRight,
512// };
513
514// let panels = dock
515// .panel_entries
516// .iter()
517// .map(|item| (item.panel.clone(), item.context_menu.clone()))
518// .collect::<Vec<_>>();
519// Flex::row()
520// .with_children(panels.into_iter().enumerate().filter_map(
521// |(panel_ix, (view, context_menu))| {
522// let icon_path = view.icon_path(cx)?;
523// let is_active = is_open && panel_ix == active_ix;
524// let (tooltip, tooltip_action) = if is_active {
525// (
526// format!("Close {} dock", dock_position.to_label()),
527// Some(match dock_position {
528// DockPosition::Left => crate::ToggleLeftDock.boxed_clone(),
529// DockPosition::Bottom => crate::ToggleBottomDock.boxed_clone(),
530// DockPosition::Right => crate::ToggleRightDock.boxed_clone(),
531// }),
532// )
533// } else {
534// view.icon_tooltip(cx)
535// };
536// Some(
537// Stack::new()
538// .with_child(
539// MouseEventHandler::new::<Self, _>(panel_ix, cx, |state, cx| {
540// let style = button_style.in_state(is_active);
541
542// let style = style.style_for(state);
543// Flex::row()
544// .with_child(
545// Svg::new(icon_path)
546// .with_color(style.icon_color)
547// .constrained()
548// .with_width(style.icon_size)
549// .aligned(),
550// )
551// .with_children(if let Some(label) = view.icon_label(cx) {
552// Some(
553// Label::new(label, style.label.text.clone())
554// .contained()
555// .with_style(style.label.container)
556// .aligned(),
557// )
558// } else {
559// None
560// })
561// .constrained()
562// .with_height(style.icon_size)
563// .contained()
564// .with_style(style.container)
565// })
566// .with_cursor_style(CursorStyle::PointingHand)
567// .on_click(MouseButton::Left, {
568// let tooltip_action =
569// tooltip_action.as_ref().map(|action| action.boxed_clone());
570// move |_, this, cx| {
571// if let Some(tooltip_action) = &tooltip_action {
572// let window = cx.window();
573// let view_id = this.workspace.id();
574// let tooltip_action = tooltip_action.boxed_clone();
575// cx.spawn(|_, mut cx| async move {
576// window.dispatch_action(
577// view_id,
578// &*tooltip_action,
579// &mut cx,
580// );
581// })
582// .detach();
583// }
584// }
585// })
586// .on_click(MouseButton::Right, {
587// let view = view.clone();
588// let menu = context_menu.clone();
589// move |_, _, cx| {
590// const POSITIONS: [DockPosition; 3] = [
591// DockPosition::Left,
592// DockPosition::Right,
593// DockPosition::Bottom,
594// ];
595
596// menu.update(cx, |menu, cx| {
597// let items = POSITIONS
598// .into_iter()
599// .filter(|position| {
600// *position != dock_position
601// && view.position_is_valid(*position, cx)
602// })
603// .map(|position| {
604// let view = view.clone();
605// ContextMenuItem::handler(
606// format!("Dock {}", position.to_label()),
607// move |cx| view.set_position(position, cx),
608// )
609// })
610// .collect();
611// menu.show(Default::default(), menu_corner, items, cx);
612// })
613// }
614// })
615// .with_tooltip::<Self>(
616// panel_ix,
617// tooltip,
618// tooltip_action,
619// tooltip_style.clone(),
620// cx,
621// ),
622// )
623// .with_child(ChildView::new(&context_menu, cx)),
624// )
625// },
626// ))
627// .contained()
628// .with_style(group_style)
629// .into_any()
630// }
631// }
632
633impl Render for PanelButtons {
634 type Element = Div<Self>;
635
636 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
637 // todo!()
638 let dock = self.dock.read(cx);
639 div().children(
640 dock.panel_entries
641 .iter()
642 .map(|panel| panel.panel.persistent_name(cx)),
643 )
644 }
645}
646
647impl StatusItemView for PanelButtons {
648 fn set_active_pane_item(
649 &mut self,
650 _active_pane_item: Option<&dyn crate::ItemHandle>,
651 _cx: &mut ViewContext<Self>,
652 ) {
653 // Nothing to do, panel buttons don't depend on the active center item
654 }
655}
656
657#[cfg(any(test, feature = "test-support"))]
658pub mod test {
659 use super::*;
660 use gpui::{div, Div, ViewContext, WindowContext};
661
662 pub struct TestPanel {
663 pub position: DockPosition,
664 pub zoomed: bool,
665 pub active: bool,
666 pub has_focus: bool,
667 pub size: f32,
668 }
669
670 impl EventEmitter<PanelEvent> for TestPanel {}
671
672 impl TestPanel {
673 pub fn new(position: DockPosition) -> Self {
674 Self {
675 position,
676 zoomed: false,
677 active: false,
678 has_focus: false,
679 size: 300.,
680 }
681 }
682 }
683
684 impl Render for TestPanel {
685 type Element = Div<Self>;
686
687 fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
688 div()
689 }
690 }
691
692 impl Panel for TestPanel {
693 fn persistent_name(&self) -> &'static str {
694 "TestPanel"
695 }
696
697 fn position(&self, _: &gpui::WindowContext) -> super::DockPosition {
698 self.position
699 }
700
701 fn position_is_valid(&self, _: super::DockPosition) -> bool {
702 true
703 }
704
705 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
706 self.position = position;
707 cx.emit(PanelEvent::ChangePosition);
708 }
709
710 fn size(&self, _: &WindowContext) -> f32 {
711 self.size
712 }
713
714 fn set_size(&mut self, size: Option<f32>, _: &mut ViewContext<Self>) {
715 self.size = size.unwrap_or(300.);
716 }
717
718 fn icon_path(&self, _: &WindowContext) -> Option<&'static str> {
719 Some("icons/test_panel.svg")
720 }
721
722 fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>) {
723 ("Test Panel".into(), None)
724 }
725
726 fn is_zoomed(&self, _: &WindowContext) -> bool {
727 self.zoomed
728 }
729
730 fn set_zoomed(&mut self, zoomed: bool, _cx: &mut ViewContext<Self>) {
731 self.zoomed = zoomed;
732 }
733
734 fn set_active(&mut self, active: bool, _cx: &mut ViewContext<Self>) {
735 self.active = active;
736 }
737
738 fn has_focus(&self, _cx: &WindowContext) -> bool {
739 self.has_focus
740 }
741
742 fn focus_handle(&self, cx: &WindowContext) -> FocusHandle {
743 unimplemented!()
744 }
745 }
746}