1use crate::{StatusItemView, Workspace};
2use context_menu::{ContextMenu, ContextMenuItem};
3use gpui::{
4 elements::*, impl_actions, platform::CursorStyle, platform::MouseButton, AnyViewHandle,
5 AppContext, Axis, Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle,
6 WindowContext,
7};
8use serde::Deserialize;
9use settings::Settings;
10use std::rc::Rc;
11
12pub trait Panel: View {
13 fn position(&self, cx: &WindowContext) -> DockPosition;
14 fn position_is_valid(&self, position: DockPosition) -> bool;
15 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>);
16 fn default_size(&self, cx: &WindowContext) -> f32;
17 fn icon_path(&self) -> &'static str;
18 fn icon_tooltip(&self) -> String;
19 fn icon_label(&self, _: &AppContext) -> Option<String> {
20 None
21 }
22 fn should_change_position_on_event(_: &Self::Event) -> bool;
23 fn should_zoom_in_on_event(_: &Self::Event) -> bool;
24 fn should_zoom_out_on_event(_: &Self::Event) -> bool;
25 fn set_zoomed(&mut self, zoomed: bool, cx: &mut ViewContext<Self>);
26 fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool;
27 fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool;
28}
29
30pub trait PanelHandle {
31 fn id(&self) -> usize;
32 fn position(&self, cx: &WindowContext) -> DockPosition;
33 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;
34 fn set_position(&self, position: DockPosition, cx: &mut WindowContext);
35 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
36 fn default_size(&self, cx: &WindowContext) -> f32;
37 fn icon_path(&self, cx: &WindowContext) -> &'static str;
38 fn icon_tooltip(&self, cx: &WindowContext) -> String;
39 fn icon_label(&self, cx: &WindowContext) -> Option<String>;
40 fn is_focused(&self, cx: &WindowContext) -> bool;
41 fn as_any(&self) -> &AnyViewHandle;
42}
43
44impl<T> PanelHandle for ViewHandle<T>
45where
46 T: Panel,
47{
48 fn id(&self) -> usize {
49 self.id()
50 }
51
52 fn position(&self, cx: &WindowContext) -> DockPosition {
53 self.read(cx).position(cx)
54 }
55
56 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool {
57 self.read(cx).position_is_valid(position)
58 }
59
60 fn set_position(&self, position: DockPosition, cx: &mut WindowContext) {
61 self.update(cx, |this, cx| this.set_position(position, cx))
62 }
63
64 fn default_size(&self, cx: &WindowContext) -> f32 {
65 self.read(cx).default_size(cx)
66 }
67
68 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext) {
69 self.update(cx, |this, cx| this.set_zoomed(zoomed, cx))
70 }
71
72 fn icon_path(&self, cx: &WindowContext) -> &'static str {
73 self.read(cx).icon_path()
74 }
75
76 fn icon_tooltip(&self, cx: &WindowContext) -> String {
77 self.read(cx).icon_tooltip()
78 }
79
80 fn icon_label(&self, cx: &WindowContext) -> Option<String> {
81 self.read(cx).icon_label(cx)
82 }
83
84 fn is_focused(&self, cx: &WindowContext) -> bool {
85 ViewHandle::is_focused(self, cx)
86 }
87
88 fn as_any(&self) -> &AnyViewHandle {
89 self
90 }
91}
92
93impl From<&dyn PanelHandle> for AnyViewHandle {
94 fn from(val: &dyn PanelHandle) -> Self {
95 val.as_any().clone()
96 }
97}
98
99pub struct Dock {
100 position: DockPosition,
101 panel_entries: Vec<PanelEntry>,
102 is_open: bool,
103 active_panel_index: usize,
104}
105
106#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
107pub enum DockPosition {
108 Left,
109 Bottom,
110 Right,
111}
112
113impl DockPosition {
114 fn to_label(&self) -> &'static str {
115 match self {
116 Self::Left => "left",
117 Self::Bottom => "bottom",
118 Self::Right => "right",
119 }
120 }
121
122 fn to_resize_handle_side(self) -> HandleSide {
123 match self {
124 Self::Left => HandleSide::Right,
125 Self::Bottom => HandleSide::Top,
126 Self::Right => HandleSide::Left,
127 }
128 }
129
130 pub fn axis(&self) -> Axis {
131 match self {
132 Self::Left | Self::Right => Axis::Horizontal,
133 Self::Bottom => Axis::Vertical,
134 }
135 }
136}
137
138struct PanelEntry {
139 panel: Rc<dyn PanelHandle>,
140 size: f32,
141 context_menu: ViewHandle<ContextMenu>,
142 zoomed: bool,
143 _subscriptions: [Subscription; 2],
144}
145
146pub struct PanelButtons {
147 dock: ViewHandle<Dock>,
148 workspace: WeakViewHandle<Workspace>,
149}
150
151#[derive(Clone, Debug, Deserialize, PartialEq)]
152pub struct TogglePanel {
153 pub dock_position: DockPosition,
154 pub panel_index: usize,
155}
156
157impl_actions!(workspace, [TogglePanel]);
158
159impl Dock {
160 pub fn new(position: DockPosition) -> Self {
161 Self {
162 position,
163 panel_entries: Default::default(),
164 active_panel_index: 0,
165 is_open: false,
166 }
167 }
168
169 pub fn is_open(&self) -> bool {
170 self.is_open
171 }
172
173 pub fn active_panel_index(&self) -> usize {
174 self.active_panel_index
175 }
176
177 pub fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
178 if open != self.is_open {
179 self.is_open = open;
180 cx.notify();
181 }
182 }
183
184 pub fn toggle_open(&mut self, cx: &mut ViewContext<Self>) {
185 self.is_open = !self.is_open;
186 cx.notify();
187 }
188
189 pub fn set_panel_zoomed(
190 &mut self,
191 panel: &AnyViewHandle,
192 zoomed: bool,
193 cx: &mut ViewContext<Self>,
194 ) {
195 for entry in &mut self.panel_entries {
196 if entry.panel.as_any() == panel {
197 if zoomed != entry.zoomed {
198 entry.zoomed = zoomed;
199 entry.panel.set_zoomed(zoomed, cx);
200 }
201 } else if entry.zoomed {
202 entry.zoomed = false;
203 entry.panel.set_zoomed(false, cx);
204 }
205 }
206
207 cx.notify();
208 }
209
210 pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {
211 for entry in &mut self.panel_entries {
212 if entry.zoomed {
213 entry.zoomed = false;
214 entry.panel.set_zoomed(false, cx);
215 }
216 }
217 }
218
219 pub fn add_panel<T: Panel>(&mut self, panel: ViewHandle<T>, cx: &mut ViewContext<Self>) {
220 let subscriptions = [
221 cx.observe(&panel, |_, _, cx| cx.notify()),
222 cx.subscribe(&panel, |this, panel, event, cx| {
223 if panel.read(cx).should_activate_on_event(event, cx) {
224 if let Some(ix) = this
225 .panel_entries
226 .iter()
227 .position(|entry| entry.panel.id() == panel.id())
228 {
229 this.set_open(true, cx);
230 this.activate_panel(ix, cx);
231 cx.focus(&panel);
232 }
233 } else if panel.read(cx).should_close_on_event(event, cx)
234 && this.active_panel().map_or(false, |p| p.id() == panel.id())
235 {
236 this.set_open(false, cx);
237 }
238 }),
239 ];
240
241 let dock_view_id = cx.view_id();
242 let size = panel.default_size(cx);
243 self.panel_entries.push(PanelEntry {
244 panel: Rc::new(panel),
245 size,
246 zoomed: false,
247 context_menu: cx.add_view(|cx| {
248 let mut menu = ContextMenu::new(dock_view_id, cx);
249 menu.set_position_mode(OverlayPositionMode::Local);
250 menu
251 }),
252 _subscriptions: subscriptions,
253 });
254 cx.notify()
255 }
256
257 pub fn remove_panel<T: Panel>(&mut self, panel: &ViewHandle<T>, cx: &mut ViewContext<Self>) {
258 if let Some(panel_ix) = self
259 .panel_entries
260 .iter()
261 .position(|entry| entry.panel.id() == panel.id())
262 {
263 if panel_ix == self.active_panel_index {
264 self.active_panel_index = 0;
265 self.set_open(false, cx);
266 } else if panel_ix < self.active_panel_index {
267 self.active_panel_index -= 1;
268 }
269 self.panel_entries.remove(panel_ix);
270 cx.notify();
271 }
272 }
273
274 pub fn panels_len(&self) -> usize {
275 self.panel_entries.len()
276 }
277
278 pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
279 self.active_panel_index = panel_ix;
280 cx.notify();
281 }
282
283 pub fn toggle_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
284 if self.active_panel_index == panel_ix {
285 self.is_open = false;
286 } else {
287 self.active_panel_index = panel_ix;
288 }
289 cx.notify();
290 }
291
292 pub fn active_panel(&self) -> Option<&Rc<dyn PanelHandle>> {
293 let entry = self.active_entry()?;
294 Some(&entry.panel)
295 }
296
297 fn active_entry(&self) -> Option<&PanelEntry> {
298 if self.is_open {
299 self.panel_entries.get(self.active_panel_index)
300 } else {
301 None
302 }
303 }
304
305 pub fn zoomed_panel(&self) -> Option<AnyViewHandle> {
306 let entry = self.active_entry()?;
307 if entry.zoomed {
308 Some(entry.panel.as_any().clone())
309 } else {
310 None
311 }
312 }
313
314 pub fn panel_size(&self, panel: &dyn PanelHandle) -> Option<f32> {
315 self.panel_entries
316 .iter()
317 .find(|entry| entry.panel.id() == panel.id())
318 .map(|entry| entry.size)
319 }
320
321 pub fn resize_panel(&mut self, panel: &dyn PanelHandle, size: f32) {
322 let entry = self
323 .panel_entries
324 .iter_mut()
325 .find(|entry| entry.panel.id() == panel.id());
326 if let Some(entry) = entry {
327 entry.size = size;
328 }
329 }
330
331 pub fn active_panel_size(&self) -> Option<f32> {
332 if self.is_open {
333 self.panel_entries
334 .get(self.active_panel_index)
335 .map(|entry| entry.size)
336 } else {
337 None
338 }
339 }
340
341 pub fn resize_active_panel(&mut self, size: f32, cx: &mut ViewContext<Self>) {
342 if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
343 entry.size = size;
344 cx.notify();
345 }
346 }
347}
348
349impl Entity for Dock {
350 type Event = ();
351}
352
353impl View for Dock {
354 fn ui_name() -> &'static str {
355 "Dock"
356 }
357
358 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
359 if let Some(active_entry) = self.active_entry() {
360 if active_entry.zoomed {
361 Empty::new().into_any()
362 } else {
363 let size = self.active_panel_size().unwrap();
364 let style = &cx.global::<Settings>().theme.workspace.dock;
365 ChildView::new(active_entry.panel.as_any(), cx)
366 .contained()
367 .with_style(style.container)
368 .resizable(
369 self.position.to_resize_handle_side(),
370 size,
371 |dock: &mut Self, size, cx| dock.resize_active_panel(size, cx),
372 )
373 .into_any()
374 }
375 } else {
376 Empty::new().into_any()
377 }
378 }
379}
380
381impl PanelButtons {
382 pub fn new(
383 dock: ViewHandle<Dock>,
384 workspace: WeakViewHandle<Workspace>,
385 cx: &mut ViewContext<Self>,
386 ) -> Self {
387 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
388 Self { dock, workspace }
389 }
390}
391
392impl Entity for PanelButtons {
393 type Event = ();
394}
395
396impl View for PanelButtons {
397 fn ui_name() -> &'static str {
398 "PanelButtons"
399 }
400
401 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
402 let theme = &cx.global::<Settings>().theme;
403 let tooltip_style = theme.tooltip.clone();
404 let theme = &theme.workspace.status_bar.panel_buttons;
405 let button_style = theme.button.clone();
406 let dock = self.dock.read(cx);
407 let active_ix = dock.active_panel_index;
408 let is_open = dock.is_open;
409 let dock_position = dock.position;
410 let group_style = match dock_position {
411 DockPosition::Left => theme.group_left,
412 DockPosition::Bottom => theme.group_bottom,
413 DockPosition::Right => theme.group_right,
414 };
415 let menu_corner = match dock_position {
416 DockPosition::Left => AnchorCorner::BottomLeft,
417 DockPosition::Bottom | DockPosition::Right => AnchorCorner::BottomRight,
418 };
419
420 let panels = dock
421 .panel_entries
422 .iter()
423 .map(|item| (item.panel.clone(), item.context_menu.clone()))
424 .collect::<Vec<_>>();
425 Flex::row()
426 .with_children(
427 panels
428 .into_iter()
429 .enumerate()
430 .map(|(ix, (view, context_menu))| {
431 let action = TogglePanel {
432 dock_position,
433 panel_index: ix,
434 };
435
436 Stack::new()
437 .with_child(
438 MouseEventHandler::<Self, _>::new(ix, cx, |state, cx| {
439 let is_active = is_open && ix == active_ix;
440 let style = button_style.style_for(state, is_active);
441 Flex::row()
442 .with_child(
443 Svg::new(view.icon_path(cx))
444 .with_color(style.icon_color)
445 .constrained()
446 .with_width(style.icon_size)
447 .aligned(),
448 )
449 .with_children(if let Some(label) = view.icon_label(cx) {
450 Some(
451 Label::new(label, style.label.text.clone())
452 .contained()
453 .with_style(style.label.container)
454 .aligned(),
455 )
456 } else {
457 None
458 })
459 .constrained()
460 .with_height(style.icon_size)
461 .contained()
462 .with_style(style.container)
463 })
464 .with_cursor_style(CursorStyle::PointingHand)
465 .on_click(MouseButton::Left, {
466 let action = action.clone();
467 move |_, this, cx| {
468 if let Some(workspace) = this.workspace.upgrade(cx) {
469 let action = action.clone();
470 cx.window_context().defer(move |cx| {
471 workspace.update(cx, |workspace, cx| {
472 workspace.toggle_panel(&action, cx)
473 });
474 });
475 }
476 }
477 })
478 .on_click(MouseButton::Right, {
479 let view = view.clone();
480 let menu = context_menu.clone();
481 move |_, _, cx| {
482 const POSITIONS: [DockPosition; 3] = [
483 DockPosition::Left,
484 DockPosition::Right,
485 DockPosition::Bottom,
486 ];
487
488 menu.update(cx, |menu, cx| {
489 let items = POSITIONS
490 .into_iter()
491 .filter(|position| {
492 *position != dock_position
493 && view.position_is_valid(*position, cx)
494 })
495 .map(|position| {
496 let view = view.clone();
497 ContextMenuItem::handler(
498 format!("Dock {}", position.to_label()),
499 move |cx| view.set_position(position, cx),
500 )
501 })
502 .collect();
503 menu.show(Default::default(), menu_corner, items, cx);
504 })
505 }
506 })
507 .with_tooltip::<Self>(
508 ix,
509 view.icon_tooltip(cx),
510 Some(Box::new(action)),
511 tooltip_style.clone(),
512 cx,
513 ),
514 )
515 .with_child(ChildView::new(&context_menu, cx))
516 }),
517 )
518 .contained()
519 .with_style(group_style)
520 .into_any()
521 }
522}
523
524impl StatusItemView for PanelButtons {
525 fn set_active_pane_item(
526 &mut self,
527 _: Option<&dyn crate::ItemHandle>,
528 _: &mut ViewContext<Self>,
529 ) {
530 }
531}
532
533#[cfg(test)]
534pub(crate) mod test {
535 use super::*;
536 use gpui::Entity;
537
538 pub enum TestPanelEvent {
539 PositionChanged,
540 Activated,
541 Closed,
542 }
543
544 pub struct TestPanel {
545 pub position: DockPosition,
546 }
547
548 impl Entity for TestPanel {
549 type Event = TestPanelEvent;
550 }
551
552 impl View for TestPanel {
553 fn ui_name() -> &'static str {
554 "TestPanel"
555 }
556
557 fn render(&mut self, _: &mut ViewContext<'_, '_, Self>) -> AnyElement<Self> {
558 Empty::new().into_any()
559 }
560 }
561
562 impl Panel for TestPanel {
563 fn position(&self, _: &gpui::WindowContext) -> super::DockPosition {
564 self.position
565 }
566
567 fn position_is_valid(&self, _: super::DockPosition) -> bool {
568 true
569 }
570
571 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
572 self.position = position;
573 cx.emit(TestPanelEvent::PositionChanged);
574 }
575
576 fn set_zoomed(&mut self, _zoomed: bool, _cx: &mut ViewContext<Self>) {
577 unimplemented!()
578 }
579
580 fn default_size(&self, _: &WindowContext) -> f32 {
581 match self.position.axis() {
582 Axis::Horizontal => 300.,
583 Axis::Vertical => 200.,
584 }
585 }
586
587 fn icon_path(&self) -> &'static str {
588 "icons/test_panel.svg"
589 }
590
591 fn icon_tooltip(&self) -> String {
592 "Test Panel".into()
593 }
594
595 fn should_change_position_on_event(event: &Self::Event) -> bool {
596 matches!(event, TestPanelEvent::PositionChanged)
597 }
598
599 fn should_zoom_in_on_event(_: &Self::Event) -> bool {
600 false
601 }
602
603 fn should_zoom_out_on_event(_: &Self::Event) -> bool {
604 false
605 }
606
607 fn should_activate_on_event(&self, event: &Self::Event, _: &gpui::AppContext) -> bool {
608 matches!(event, TestPanelEvent::Activated)
609 }
610
611 fn should_close_on_event(&self, event: &Self::Event, _: &gpui::AppContext) -> bool {
612 matches!(event, TestPanelEvent::Closed)
613 }
614 }
615}