1use crate::{status_bar::StatusItemView, Workspace};
2use crate::{DockClickReset, DockDragState};
3use gpui::{
4 div, px, Action, AnchorCorner, AnyView, AppContext, Axis, ClickEvent, Div, Entity, EntityId,
5 EventEmitter, FocusHandle, FocusableView, IntoElement, MouseButton, ParentElement, Render,
6 SharedString, Styled, Subscription, View, ViewContext, VisualContext, WeakView, WindowContext,
7};
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use std::sync::Arc;
11use ui::{h_stack, ContextMenu, IconButton, Tooltip};
12use ui::{prelude::*, right_click_menu};
13
14pub enum PanelEvent {
15 ChangePosition,
16 ZoomIn,
17 ZoomOut,
18 Activate,
19 Close,
20 Focus,
21}
22
23pub trait Panel: FocusableView + EventEmitter<PanelEvent> {
24 fn persistent_name() -> &'static str;
25 fn position(&self, cx: &WindowContext) -> DockPosition;
26 fn position_is_valid(&self, position: DockPosition) -> bool;
27 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>);
28 fn size(&self, cx: &WindowContext) -> f32;
29 fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>);
30 // todo!("We should have a icon tooltip method, rather than using persistant_name")
31 fn icon(&self, cx: &WindowContext) -> Option<ui::Icon>;
32 fn toggle_action(&self) -> Box<dyn Action>;
33 fn icon_label(&self, _: &WindowContext) -> Option<String> {
34 None
35 }
36 fn is_zoomed(&self, _cx: &WindowContext) -> bool {
37 false
38 }
39 fn set_zoomed(&mut self, _zoomed: bool, _cx: &mut ViewContext<Self>) {}
40 fn set_active(&mut self, _active: bool, _cx: &mut ViewContext<Self>) {}
41}
42
43pub trait PanelHandle: Send + Sync {
44 fn entity_id(&self) -> EntityId;
45 fn persistent_name(&self) -> &'static str;
46 fn position(&self, cx: &WindowContext) -> DockPosition;
47 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;
48 fn set_position(&self, position: DockPosition, cx: &mut WindowContext);
49 fn is_zoomed(&self, cx: &WindowContext) -> bool;
50 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
51 fn set_active(&self, active: bool, cx: &mut WindowContext);
52 fn size(&self, cx: &WindowContext) -> f32;
53 fn set_size(&self, size: Option<f32>, cx: &mut WindowContext);
54 fn icon(&self, cx: &WindowContext) -> Option<ui::Icon>;
55 fn toggle_action(&self, cx: &WindowContext) -> Box<dyn Action>;
56 fn icon_label(&self, cx: &WindowContext) -> Option<String>;
57 fn focus_handle(&self, cx: &AppContext) -> FocusHandle;
58 fn to_any(&self) -> AnyView;
59}
60
61impl<T> PanelHandle for View<T>
62where
63 T: Panel,
64{
65 fn entity_id(&self) -> EntityId {
66 Entity::entity_id(self)
67 }
68
69 fn persistent_name(&self) -> &'static str {
70 T::persistent_name()
71 }
72
73 fn position(&self, cx: &WindowContext) -> DockPosition {
74 self.read(cx).position(cx)
75 }
76
77 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool {
78 self.read(cx).position_is_valid(position)
79 }
80
81 fn set_position(&self, position: DockPosition, cx: &mut WindowContext) {
82 self.update(cx, |this, cx| this.set_position(position, cx))
83 }
84
85 fn is_zoomed(&self, cx: &WindowContext) -> bool {
86 self.read(cx).is_zoomed(cx)
87 }
88
89 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext) {
90 self.update(cx, |this, cx| this.set_zoomed(zoomed, cx))
91 }
92
93 fn set_active(&self, active: bool, cx: &mut WindowContext) {
94 self.update(cx, |this, cx| this.set_active(active, cx))
95 }
96
97 fn size(&self, cx: &WindowContext) -> f32 {
98 self.read(cx).size(cx)
99 }
100
101 fn set_size(&self, size: Option<f32>, cx: &mut WindowContext) {
102 self.update(cx, |this, cx| this.set_size(size, cx))
103 }
104
105 fn icon(&self, cx: &WindowContext) -> Option<ui::Icon> {
106 self.read(cx).icon(cx)
107 }
108
109 fn toggle_action(&self, cx: &WindowContext) -> Box<dyn Action> {
110 self.read(cx).toggle_action()
111 }
112
113 fn icon_label(&self, cx: &WindowContext) -> Option<String> {
114 self.read(cx).icon_label(cx)
115 }
116
117 fn to_any(&self) -> AnyView {
118 self.clone().into()
119 }
120
121 fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
122 self.read(cx).focus_handle(cx).clone()
123 }
124}
125
126impl From<&dyn PanelHandle> for AnyView {
127 fn from(val: &dyn PanelHandle) -> Self {
128 val.to_any()
129 }
130}
131
132pub struct Dock {
133 position: DockPosition,
134 panel_entries: Vec<PanelEntry>,
135 is_open: bool,
136 active_panel_index: usize,
137 focus_handle: FocusHandle,
138 focus_subscription: Subscription,
139}
140
141impl FocusableView for Dock {
142 fn focus_handle(&self, _: &AppContext) -> FocusHandle {
143 self.focus_handle.clone()
144 }
145}
146
147#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
148#[serde(rename_all = "lowercase")]
149pub enum DockPosition {
150 Left,
151 Bottom,
152 Right,
153}
154
155impl DockPosition {
156 fn to_label(&self) -> &'static str {
157 match self {
158 Self::Left => "left",
159 Self::Bottom => "bottom",
160 Self::Right => "right",
161 }
162 }
163
164 // todo!()
165 // fn to_resize_handle_side(self) -> HandleSide {
166 // match self {
167 // Self::Left => HandleSide::Right,
168 // Self::Bottom => HandleSide::Top,
169 // Self::Right => HandleSide::Left,
170 // }
171 // }
172
173 pub fn axis(&self) -> Axis {
174 match self {
175 Self::Left | Self::Right => Axis::Horizontal,
176 Self::Bottom => Axis::Vertical,
177 }
178 }
179}
180
181struct PanelEntry {
182 panel: Arc<dyn PanelHandle>,
183 // todo!()
184 // context_menu: View<ContextMenu>,
185 _subscriptions: [Subscription; 2],
186}
187
188pub struct PanelButtons {
189 dock: View<Dock>,
190 workspace: WeakView<Workspace>,
191}
192
193impl Dock {
194 pub fn new(position: DockPosition, cx: &mut ViewContext<'_, Self>) -> Self {
195 let focus_handle = cx.focus_handle();
196 let focus_subscription = cx.on_focus(&focus_handle, |dock, cx| {
197 if let Some(active_entry) = dock.panel_entries.get(dock.active_panel_index) {
198 active_entry.panel.focus_handle(cx).focus(cx)
199 }
200 });
201 Self {
202 position,
203 panel_entries: Default::default(),
204 active_panel_index: 0,
205 is_open: false,
206 focus_handle,
207 focus_subscription,
208 }
209 }
210
211 pub fn position(&self) -> DockPosition {
212 self.position
213 }
214
215 pub fn is_open(&self) -> bool {
216 self.is_open
217 }
218
219 // todo!()
220 // pub fn has_focus(&self, cx: &WindowContext) -> bool {
221 // self.visible_panel()
222 // .map_or(false, |panel| panel.has_focus(cx))
223 // }
224
225 pub fn panel<T: Panel>(&self) -> Option<View<T>> {
226 self.panel_entries
227 .iter()
228 .find_map(|entry| entry.panel.to_any().clone().downcast().ok())
229 }
230
231 pub fn panel_index_for_type<T: Panel>(&self) -> Option<usize> {
232 self.panel_entries
233 .iter()
234 .position(|entry| entry.panel.to_any().downcast::<T>().is_ok())
235 }
236
237 pub fn panel_index_for_persistent_name(
238 &self,
239 ui_name: &str,
240 _cx: &AppContext,
241 ) -> Option<usize> {
242 self.panel_entries
243 .iter()
244 .position(|entry| entry.panel.persistent_name() == ui_name)
245 }
246
247 pub fn active_panel_index(&self) -> usize {
248 self.active_panel_index
249 }
250
251 pub(crate) fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
252 if open != self.is_open {
253 self.is_open = open;
254 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
255 active_panel.panel.set_active(open, cx);
256 }
257
258 cx.notify();
259 }
260 }
261
262 pub fn set_panel_zoomed(&mut self, panel: &AnyView, zoomed: bool, cx: &mut ViewContext<Self>) {
263 for entry in &mut self.panel_entries {
264 if entry.panel.entity_id() == panel.entity_id() {
265 if zoomed != entry.panel.is_zoomed(cx) {
266 entry.panel.set_zoomed(zoomed, cx);
267 }
268 } else if entry.panel.is_zoomed(cx) {
269 entry.panel.set_zoomed(false, cx);
270 }
271 }
272
273 cx.notify();
274 }
275
276 pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {
277 for entry in &mut self.panel_entries {
278 if entry.panel.is_zoomed(cx) {
279 entry.panel.set_zoomed(false, cx);
280 }
281 }
282 }
283
284 pub(crate) fn add_panel<T: Panel>(
285 &mut self,
286 panel: View<T>,
287 workspace: WeakView<Workspace>,
288 cx: &mut ViewContext<Self>,
289 ) {
290 let subscriptions = [
291 cx.observe(&panel, |_, _, cx| cx.notify()),
292 cx.subscribe(&panel, move |this, panel, event, cx| match event {
293 PanelEvent::ChangePosition => {
294 let new_position = panel.read(cx).position(cx);
295
296 let Ok(new_dock) = workspace.update(cx, |workspace, cx| {
297 if panel.is_zoomed(cx) {
298 workspace.zoomed_position = Some(new_position);
299 }
300 match new_position {
301 DockPosition::Left => &workspace.left_dock,
302 DockPosition::Bottom => &workspace.bottom_dock,
303 DockPosition::Right => &workspace.right_dock,
304 }
305 .clone()
306 }) else {
307 return;
308 };
309
310 let was_visible = this.is_open()
311 && this.visible_panel().map_or(false, |active_panel| {
312 active_panel.entity_id() == Entity::entity_id(&panel)
313 });
314
315 this.remove_panel(&panel, cx);
316
317 new_dock.update(cx, |new_dock, cx| {
318 new_dock.add_panel(panel.clone(), workspace.clone(), cx);
319 if was_visible {
320 new_dock.set_open(true, cx);
321 new_dock.activate_panel(this.panels_len() - 1, cx);
322 }
323 });
324 }
325 PanelEvent::ZoomIn => {
326 this.set_panel_zoomed(&panel.to_any(), true, cx);
327 if !panel.focus_handle(cx).contains_focused(cx) {
328 cx.focus_view(&panel);
329 }
330 workspace
331 .update(cx, |workspace, cx| {
332 workspace.zoomed = Some(panel.downgrade().into());
333 workspace.zoomed_position = Some(panel.read(cx).position(cx));
334 })
335 .ok();
336 }
337 PanelEvent::ZoomOut => {
338 this.set_panel_zoomed(&panel.to_any(), false, cx);
339 workspace
340 .update(cx, |workspace, cx| {
341 if workspace.zoomed_position == Some(this.position) {
342 workspace.zoomed = None;
343 workspace.zoomed_position = None;
344 }
345 cx.notify();
346 })
347 .ok();
348 }
349 PanelEvent::Activate => {
350 if let Some(ix) = this
351 .panel_entries
352 .iter()
353 .position(|entry| entry.panel.entity_id() == Entity::entity_id(&panel))
354 {
355 this.set_open(true, cx);
356 this.activate_panel(ix, cx);
357 cx.focus_view(&panel);
358 }
359 }
360 PanelEvent::Close => {
361 if this
362 .visible_panel()
363 .map_or(false, |p| p.entity_id() == Entity::entity_id(&panel))
364 {
365 this.set_open(false, cx);
366 }
367 }
368 PanelEvent::Focus => {}
369 }),
370 ];
371
372 // todo!()
373 // let dock_view_id = cx.view_id();
374 self.panel_entries.push(PanelEntry {
375 panel: Arc::new(panel),
376 // todo!()
377 // context_menu: cx.add_view(|cx| {
378 // let mut menu = ContextMenu::new(dock_view_id, cx);
379 // menu.set_position_mode(OverlayPositionMode::Local);
380 // menu
381 // }),
382 _subscriptions: subscriptions,
383 });
384 cx.notify()
385 }
386
387 pub fn remove_panel<T: Panel>(&mut self, panel: &View<T>, cx: &mut ViewContext<Self>) {
388 if let Some(panel_ix) = self
389 .panel_entries
390 .iter()
391 .position(|entry| entry.panel.entity_id() == Entity::entity_id(panel))
392 {
393 if panel_ix == self.active_panel_index {
394 self.active_panel_index = 0;
395 self.set_open(false, cx);
396 } else if panel_ix < self.active_panel_index {
397 self.active_panel_index -= 1;
398 }
399 self.panel_entries.remove(panel_ix);
400 cx.notify();
401 }
402 }
403
404 pub fn panels_len(&self) -> usize {
405 self.panel_entries.len()
406 }
407
408 pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
409 if panel_ix != self.active_panel_index {
410 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
411 active_panel.panel.set_active(false, cx);
412 }
413
414 self.active_panel_index = panel_ix;
415 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
416 active_panel.panel.set_active(true, cx);
417 }
418
419 cx.notify();
420 }
421 }
422
423 pub fn visible_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
424 let entry = self.visible_entry()?;
425 Some(&entry.panel)
426 }
427
428 pub fn active_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
429 Some(&self.panel_entries.get(self.active_panel_index)?.panel)
430 }
431
432 fn visible_entry(&self) -> Option<&PanelEntry> {
433 if self.is_open {
434 self.panel_entries.get(self.active_panel_index)
435 } else {
436 None
437 }
438 }
439
440 pub fn zoomed_panel(&self, cx: &WindowContext) -> Option<Arc<dyn PanelHandle>> {
441 let entry = self.visible_entry()?;
442 if entry.panel.is_zoomed(cx) {
443 Some(entry.panel.clone())
444 } else {
445 None
446 }
447 }
448
449 pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<f32> {
450 self.panel_entries
451 .iter()
452 .find(|entry| entry.panel.entity_id() == panel.entity_id())
453 .map(|entry| entry.panel.size(cx))
454 }
455
456 pub fn active_panel_size(&self, cx: &WindowContext) -> Option<f32> {
457 if self.is_open {
458 self.panel_entries
459 .get(self.active_panel_index)
460 .map(|entry| entry.panel.size(cx))
461 } else {
462 None
463 }
464 }
465
466 pub fn resize_active_panel(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) {
467 if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
468 entry.panel.set_size(size, cx);
469 cx.notify();
470 }
471 }
472
473 pub fn toggle_action(&self) -> Box<dyn Action> {
474 match self.position {
475 DockPosition::Left => crate::ToggleLeftDock.boxed_clone(),
476 DockPosition::Bottom => crate::ToggleBottomDock.boxed_clone(),
477 DockPosition::Right => crate::ToggleRightDock.boxed_clone(),
478 }
479 }
480}
481
482impl Render for Dock {
483 type Element = Div;
484
485 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
486 if let Some(entry) = self.visible_entry() {
487 let size = entry.panel.size(cx);
488
489 let mut pre_resize_handle = None;
490 let mut post_resize_handle = None;
491 let position = self.position;
492 let handler = div()
493 .id("resize-handle")
494 .bg(cx.theme().colors().border)
495 .on_mouse_down(gpui::MouseButton::Left, move |_, cx| {
496 cx.update_global(|drag: &mut DockDragState, cx| drag.0 = Some(position))
497 })
498 .on_click(cx.listener(|v, e: &ClickEvent, cx| {
499 if e.down.button == MouseButton::Left {
500 cx.update_global(|state: &mut DockClickReset, cx| {
501 if state.0.is_some() {
502 state.0 = None;
503 v.resize_active_panel(None, cx)
504 } else {
505 let double_click = cx.double_click_interval();
506 let timer = cx.background_executor().timer(double_click);
507 state.0 = Some(cx.spawn(|_, mut cx| async move {
508 timer.await;
509 cx.update_global(|state: &mut DockClickReset, cx| {
510 state.0 = None;
511 })
512 .ok();
513 }));
514 }
515 })
516 }
517 }));
518
519 match self.position() {
520 DockPosition::Left => {
521 post_resize_handle = Some(handler.w_1().h_full().cursor_col_resize())
522 }
523 DockPosition::Bottom => {
524 pre_resize_handle = Some(handler.w_full().h_1().cursor_row_resize())
525 }
526 DockPosition::Right => {
527 pre_resize_handle = Some(handler.w_full().h_1().cursor_col_resize())
528 }
529 }
530
531 div()
532 .border_color(cx.theme().colors().border)
533 .map(|this| match self.position().axis() {
534 Axis::Horizontal => this.w(px(size)).h_full(),
535 Axis::Vertical => this.h(px(size)).w_full(),
536 })
537 .map(|this| match self.position() {
538 DockPosition::Left => this.border_r(),
539 DockPosition::Right => this.border_l(),
540 DockPosition::Bottom => this.border_t(),
541 })
542 .children(pre_resize_handle)
543 .child(entry.panel.to_any())
544 .children(post_resize_handle)
545 } else {
546 div()
547 }
548 }
549}
550
551impl PanelButtons {
552 pub fn new(
553 dock: View<Dock>,
554 workspace: WeakView<Workspace>,
555 cx: &mut ViewContext<Self>,
556 ) -> Self {
557 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
558 Self { dock, workspace }
559 }
560}
561
562// impl Render for PanelButtons {
563// type Element = ();
564
565// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
566// todo!("")
567// }
568
569// fn ui_name() -> &'static str {
570// "PanelButtons"
571// }
572
573// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
574// let theme = &settings::get::<ThemeSettings>(cx).theme;
575// let tooltip_style = theme.tooltip.clone();
576// let theme = &theme.workspace.status_bar.panel_buttons;
577// let button_style = theme.button.clone();
578// let dock = self.dock.read(cx);
579// let active_ix = dock.active_panel_index;
580// let is_open = dock.is_open;
581// let dock_position = dock.position;
582// let group_style = match dock_position {
583// DockPosition::Left => theme.group_left,
584// DockPosition::Bottom => theme.group_bottom,
585// DockPosition::Right => theme.group_right,
586// };
587// let menu_corner = match dock_position {
588// DockPosition::Left => AnchorCorner::BottomLeft,
589// DockPosition::Bottom | DockPosition::Right => AnchorCorner::BottomRight,
590// };
591
592// let panels = dock
593// .panel_entries
594// .iter()
595// .map(|item| (item.panel.clone(), item.context_menu.clone()))
596// .collect::<Vec<_>>();
597// Flex::row()
598// .with_children(panels.into_iter().enumerate().filter_map(
599// |(panel_ix, (view, context_menu))| {
600// let icon_path = view.icon_path(cx)?;
601// let is_active = is_open && panel_ix == active_ix;
602// let (tooltip, tooltip_action) = if is_active {
603// (
604// format!("Close {} dock", dock_position.to_label()),
605// Some(match dock_position {
606// DockPosition::Left => crate::ToggleLeftDock.boxed_clone(),
607// DockPosition::Bottom => crate::ToggleBottomDock.boxed_clone(),
608// DockPosition::Right => crate::ToggleRightDock.boxed_clone(),
609// }),
610// )
611// } else {
612// view.icon_tooltip(cx)
613// };
614// Some(
615// Stack::new()
616// .with_child(
617// MouseEventHandler::new::<Self, _>(panel_ix, cx, |state, cx| {
618// let style = button_style.in_state(is_active);
619
620// let style = style.style_for(state);
621// Flex::row()
622// .with_child(
623// Svg::new(icon_path)
624// .with_color(style.icon_color)
625// .constrained()
626// .with_width(style.icon_size)
627// .aligned(),
628// )
629// .with_children(if let Some(label) = view.icon_label(cx) {
630// Some(
631// Label::new(label, style.label.text.clone())
632// .contained()
633// .with_style(style.label.container)
634// .aligned(),
635// )
636// } else {
637// None
638// })
639// .constrained()
640// .with_height(style.icon_size)
641// .contained()
642// .with_style(style.container)
643// })
644// .with_cursor_style(CursorStyle::PointingHand)
645// .on_click(MouseButton::Left, {
646// let tooltip_action =
647// tooltip_action.as_ref().map(|action| action.boxed_clone());
648// move |_, this, cx| {
649// if let Some(tooltip_action) = &tooltip_action {
650// let window = cx.window();
651// let view_id = this.workspace.id();
652// let tooltip_action = tooltip_action.boxed_clone();
653// cx.spawn(|_, mut cx| async move {
654// window.dispatch_action(
655// view_id,
656// &*tooltip_action,
657// &mut cx,
658// );
659// })
660// .detach();
661// }
662// }
663// })
664// .on_click(MouseButton::Right, {
665// let view = view.clone();
666// let menu = context_menu.clone();
667// move |_, _, cx| {
668// const POSITIONS: [DockPosition; 3] = [
669// DockPosition::Left,
670// DockPosition::Right,
671// DockPosition::Bottom,
672// ];
673
674// menu.update(cx, |menu, cx| {
675// let items = POSITIONS
676// .into_iter()
677// .filter(|position| {
678// *position != dock_position
679// && view.position_is_valid(*position, cx)
680// })
681// .map(|position| {
682// let view = view.clone();
683// ContextMenuItem::handler(
684// format!("Dock {}", position.to_label()),
685// move |cx| view.set_position(position, cx),
686// )
687// })
688// .collect();
689// menu.show(Default::default(), menu_corner, items, cx);
690// })
691// }
692// })
693// .with_tooltip::<Self>(
694// panel_ix,
695// tooltip,
696// tooltip_action,
697// tooltip_style.clone(),
698// cx,
699// ),
700// )
701// .with_child(ChildView::new(&context_menu, cx)),
702// )
703// },
704// ))
705// .contained()
706// .with_style(group_style)
707// .into_any()
708// }
709// }
710
711// here be kittens
712impl Render for PanelButtons {
713 type Element = Div;
714
715 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
716 // todo!()
717 let dock = self.dock.read(cx);
718 let active_index = dock.active_panel_index;
719 let is_open = dock.is_open;
720 let dock_position = dock.position;
721
722 let (menu_anchor, menu_attach) = match dock.position {
723 DockPosition::Left => (AnchorCorner::BottomLeft, AnchorCorner::TopLeft),
724 DockPosition::Bottom | DockPosition::Right => {
725 (AnchorCorner::BottomRight, AnchorCorner::TopRight)
726 }
727 };
728
729 let buttons = dock
730 .panel_entries
731 .iter()
732 .enumerate()
733 .filter_map(|(i, entry)| {
734 let icon = entry.panel.icon(cx)?;
735 let name = entry.panel.persistent_name();
736 let panel = entry.panel.clone();
737
738 let is_active_button = i == active_index && is_open;
739
740 let (action, tooltip) = if is_active_button {
741 let action = dock.toggle_action();
742
743 let tooltip: SharedString =
744 format!("Close {} dock", dock.position.to_label()).into();
745
746 (action, tooltip)
747 } else {
748 let action = entry.panel.toggle_action(cx);
749
750 (action, name.into())
751 };
752
753 Some(
754 right_click_menu(name)
755 .menu(move |cx| {
756 const POSITIONS: [DockPosition; 3] = [
757 DockPosition::Left,
758 DockPosition::Right,
759 DockPosition::Bottom,
760 ];
761
762 ContextMenu::build(cx, |mut menu, cx| {
763 for position in POSITIONS {
764 if position != dock_position
765 && panel.position_is_valid(position, cx)
766 {
767 let panel = panel.clone();
768 menu = menu.entry(position.to_label(), move |cx| {
769 panel.set_position(position, cx);
770 })
771 }
772 }
773 menu
774 })
775 })
776 .anchor(menu_anchor)
777 .attach(menu_attach)
778 .trigger(
779 IconButton::new(name, icon)
780 .selected(is_active_button)
781 .on_click({
782 let action = action.boxed_clone();
783 move |_, cx| cx.dispatch_action(action.boxed_clone())
784 })
785 .tooltip(move |cx| {
786 Tooltip::for_action(tooltip.clone(), &*action, cx)
787 }),
788 ),
789 )
790 });
791
792 h_stack().gap_0p5().children(buttons)
793 }
794}
795
796impl StatusItemView for PanelButtons {
797 fn set_active_pane_item(
798 &mut self,
799 _active_pane_item: Option<&dyn crate::ItemHandle>,
800 _cx: &mut ViewContext<Self>,
801 ) {
802 // Nothing to do, panel buttons don't depend on the active center item
803 }
804}
805
806#[cfg(any(test, feature = "test-support"))]
807pub mod test {
808 use super::*;
809 use gpui::{actions, div, Div, ViewContext, WindowContext};
810
811 pub struct TestPanel {
812 pub position: DockPosition,
813 pub zoomed: bool,
814 pub active: bool,
815 pub focus_handle: FocusHandle,
816 pub size: f32,
817 }
818 actions!(test, [ToggleTestPanel]);
819
820 impl EventEmitter<PanelEvent> for TestPanel {}
821
822 impl TestPanel {
823 pub fn new(position: DockPosition, cx: &mut WindowContext) -> Self {
824 Self {
825 position,
826 zoomed: false,
827 active: false,
828 focus_handle: cx.focus_handle(),
829 size: 300.,
830 }
831 }
832 }
833
834 impl Render for TestPanel {
835 type Element = Div;
836
837 fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
838 div()
839 }
840 }
841
842 impl Panel for TestPanel {
843 fn persistent_name() -> &'static str {
844 "TestPanel"
845 }
846
847 fn position(&self, _: &gpui::WindowContext) -> super::DockPosition {
848 self.position
849 }
850
851 fn position_is_valid(&self, _: super::DockPosition) -> bool {
852 true
853 }
854
855 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
856 self.position = position;
857 cx.emit(PanelEvent::ChangePosition);
858 }
859
860 fn size(&self, _: &WindowContext) -> f32 {
861 self.size
862 }
863
864 fn set_size(&mut self, size: Option<f32>, _: &mut ViewContext<Self>) {
865 self.size = size.unwrap_or(300.);
866 }
867
868 fn icon(&self, _: &WindowContext) -> Option<ui::Icon> {
869 None
870 }
871
872 fn toggle_action(&self) -> Box<dyn Action> {
873 ToggleTestPanel.boxed_clone()
874 }
875
876 fn is_zoomed(&self, _: &WindowContext) -> bool {
877 self.zoomed
878 }
879
880 fn set_zoomed(&mut self, zoomed: bool, _cx: &mut ViewContext<Self>) {
881 self.zoomed = zoomed;
882 }
883
884 fn set_active(&mut self, active: bool, _cx: &mut ViewContext<Self>) {
885 self.active = active;
886 }
887 }
888
889 impl FocusableView for TestPanel {
890 fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
891 self.focus_handle.clone()
892 }
893 }
894}