Show keystroke in panel tooltips

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-05-24 16:13:58 +02:00
parent 0cf1632d39
commit f67a22828b
4 changed files with 102 additions and 116 deletions

View file

@ -15,8 +15,8 @@ use gpui::{
geometry::vector::Vector2F, geometry::vector::Vector2F,
keymap_matcher::KeymapContext, keymap_matcher::KeymapContext,
platform::{CursorStyle, MouseButton, PromptLevel}, platform::{CursorStyle, MouseButton, PromptLevel},
AnyElement, AppContext, AsyncAppContext, ClipboardItem, Element, Entity, ModelHandle, Task, Action, AnyElement, AppContext, AsyncAppContext, ClipboardItem, Element, Entity, ModelHandle,
View, ViewContext, ViewHandle, WeakViewHandle, WindowContext, Task, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext,
}; };
use menu::{Confirm, SelectNext, SelectPrev}; use menu::{Confirm, SelectNext, SelectPrev};
use project::{ use project::{
@ -1507,8 +1507,8 @@ impl workspace::dock::Panel for ProjectPanel {
"icons/folder_tree_16.svg" "icons/folder_tree_16.svg"
} }
fn icon_tooltip(&self) -> String { fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>) {
"Project Panel".into() ("Project Panel".into(), Some(Box::new(ToggleFocus)))
} }
fn should_change_position_on_event(event: &Self::Event) -> bool { fn should_change_position_on_event(event: &Self::Event) -> bool {

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use crate::TerminalView; use crate::TerminalView;
use db::kvp::KEY_VALUE_STORE; use db::kvp::KEY_VALUE_STORE;
use gpui::{ use gpui::{
actions, anyhow::Result, elements::*, serde_json, AppContext, AsyncAppContext, Entity, actions, anyhow::Result, elements::*, serde_json, Action, AppContext, AsyncAppContext, Entity,
Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext,
}; };
use project::Fs; use project::Fs;
@ -365,8 +365,8 @@ impl Panel for TerminalPanel {
"icons/terminal_12.svg" "icons/terminal_12.svg"
} }
fn icon_tooltip(&self) -> String { fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>) {
"Terminal Panel".into() ("Terminal Panel".into(), Some(Box::new(ToggleFocus)))
} }
fn icon_label(&self, cx: &WindowContext) -> Option<String> { fn icon_label(&self, cx: &WindowContext) -> Option<String> {

View file

@ -1,9 +1,8 @@
use crate::{StatusItemView, Workspace}; use crate::{StatusItemView, Workspace};
use context_menu::{ContextMenu, ContextMenuItem}; use context_menu::{ContextMenu, ContextMenuItem};
use gpui::{ use gpui::{
elements::*, impl_actions, platform::CursorStyle, platform::MouseButton, AnyViewHandle, elements::*, platform::CursorStyle, platform::MouseButton, Action, AnyViewHandle, AppContext,
AppContext, Axis, Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle, Axis, Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext,
WindowContext,
}; };
use serde::Deserialize; use serde::Deserialize;
use std::rc::Rc; use std::rc::Rc;
@ -16,7 +15,7 @@ pub trait Panel: View {
fn size(&self, cx: &WindowContext) -> f32; fn size(&self, cx: &WindowContext) -> f32;
fn set_size(&mut self, size: f32, cx: &mut ViewContext<Self>); fn set_size(&mut self, size: f32, cx: &mut ViewContext<Self>);
fn icon_path(&self) -> &'static str; fn icon_path(&self) -> &'static str;
fn icon_tooltip(&self) -> String; fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>);
fn icon_label(&self, _: &WindowContext) -> Option<String> { fn icon_label(&self, _: &WindowContext) -> Option<String> {
None None
} }
@ -43,7 +42,7 @@ pub trait PanelHandle {
fn size(&self, cx: &WindowContext) -> f32; fn size(&self, cx: &WindowContext) -> f32;
fn set_size(&self, size: f32, cx: &mut WindowContext); fn set_size(&self, size: f32, cx: &mut WindowContext);
fn icon_path(&self, cx: &WindowContext) -> &'static str; fn icon_path(&self, cx: &WindowContext) -> &'static str;
fn icon_tooltip(&self, cx: &WindowContext) -> String; fn icon_tooltip(&self, cx: &WindowContext) -> (String, Option<Box<dyn Action>>);
fn icon_label(&self, cx: &WindowContext) -> Option<String>; fn icon_label(&self, cx: &WindowContext) -> Option<String>;
fn has_focus(&self, cx: &WindowContext) -> bool; fn has_focus(&self, cx: &WindowContext) -> bool;
fn as_any(&self) -> &AnyViewHandle; fn as_any(&self) -> &AnyViewHandle;
@ -93,7 +92,7 @@ where
self.read(cx).icon_path() self.read(cx).icon_path()
} }
fn icon_tooltip(&self, cx: &WindowContext) -> String { fn icon_tooltip(&self, cx: &WindowContext) -> (String, Option<Box<dyn Action>>) {
self.read(cx).icon_tooltip() self.read(cx).icon_tooltip()
} }
@ -166,14 +165,6 @@ pub struct PanelButtons {
workspace: WeakViewHandle<Workspace>, workspace: WeakViewHandle<Workspace>,
} }
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct TogglePanel {
pub dock_position: DockPosition,
pub panel_index: usize,
}
impl_actions!(workspace, [TogglePanel]);
impl Dock { impl Dock {
pub fn new(position: DockPosition) -> Self { pub fn new(position: DockPosition) -> Self {
Self { Self {
@ -480,98 +471,89 @@ impl View for PanelButtons {
.map(|item| (item.panel.clone(), item.context_menu.clone())) .map(|item| (item.panel.clone(), item.context_menu.clone()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Flex::row() Flex::row()
.with_children( .with_children(panels.into_iter().enumerate().map(
panels |(panel_ix, (view, context_menu))| {
.into_iter() let (tooltip, tooltip_action) = view.icon_tooltip(cx);
.enumerate() Stack::new()
.map(|(ix, (view, context_menu))| { .with_child(
let action = TogglePanel { MouseEventHandler::<Self, _>::new(panel_ix, cx, |state, cx| {
dock_position, let is_active = is_open && panel_ix == active_ix;
panel_index: ix, let style = button_style.style_for(state, is_active);
}; Flex::row()
.with_child(
Stack::new() Svg::new(view.icon_path(cx))
.with_child( .with_color(style.icon_color)
MouseEventHandler::<Self, _>::new(ix, cx, |state, cx| { .constrained()
let is_active = is_open && ix == active_ix; .with_width(style.icon_size)
let style = button_style.style_for(state, is_active); .aligned(),
Flex::row() )
.with_child( .with_children(if let Some(label) = view.icon_label(cx) {
Svg::new(view.icon_path(cx)) Some(
.with_color(style.icon_color) Label::new(label, style.label.text.clone())
.constrained() .contained()
.with_width(style.icon_size) .with_style(style.label.container)
.aligned(), .aligned(),
) )
.with_children(if let Some(label) = view.icon_label(cx) { } else {
Some( None
Label::new(label, style.label.text.clone()) })
.contained() .constrained()
.with_style(style.label.container) .with_height(style.icon_size)
.aligned(), .contained()
) .with_style(style.container)
} else { })
None .with_cursor_style(CursorStyle::PointingHand)
}) .on_click(MouseButton::Left, {
.constrained() move |_, this, cx| {
.with_height(style.icon_size) if let Some(workspace) = this.workspace.upgrade(cx) {
.contained() cx.window_context().defer(move |cx| {
.with_style(style.container) workspace.update(cx, |workspace, cx| {
}) workspace.toggle_panel(dock_position, panel_ix, cx)
.with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, {
let action = action.clone();
move |_, this, cx| {
if let Some(workspace) = this.workspace.upgrade(cx) {
let action = action.clone();
cx.window_context().defer(move |cx| {
workspace.update(cx, |workspace, cx| {
workspace.toggle_panel(&action, cx)
});
}); });
} });
} }
}) }
.on_click(MouseButton::Right, { })
let view = view.clone(); .on_click(MouseButton::Right, {
let menu = context_menu.clone(); let view = view.clone();
move |_, _, cx| { let menu = context_menu.clone();
const POSITIONS: [DockPosition; 3] = [ move |_, _, cx| {
DockPosition::Left, const POSITIONS: [DockPosition; 3] = [
DockPosition::Right, DockPosition::Left,
DockPosition::Bottom, DockPosition::Right,
]; DockPosition::Bottom,
];
menu.update(cx, |menu, cx| { menu.update(cx, |menu, cx| {
let items = POSITIONS let items = POSITIONS
.into_iter() .into_iter()
.filter(|position| { .filter(|position| {
*position != dock_position *position != dock_position
&& view.position_is_valid(*position, cx) && view.position_is_valid(*position, cx)
}) })
.map(|position| { .map(|position| {
let view = view.clone(); let view = view.clone();
ContextMenuItem::handler( ContextMenuItem::handler(
format!("Dock {}", position.to_label()), format!("Dock {}", position.to_label()),
move |cx| view.set_position(position, cx), move |cx| view.set_position(position, cx),
) )
}) })
.collect(); .collect();
menu.show(Default::default(), menu_corner, items, cx); menu.show(Default::default(), menu_corner, items, cx);
}) })
} }
}) })
.with_tooltip::<Self>( .with_tooltip::<Self>(
ix, panel_ix,
view.icon_tooltip(cx), tooltip,
Some(Box::new(action)), tooltip_action,
tooltip_style.clone(), tooltip_style.clone(),
cx, cx,
), ),
) )
.with_child(ChildView::new(&context_menu, cx)) .with_child(ChildView::new(&context_menu, cx))
}), },
) ))
.contained() .contained()
.with_style(group_style) .with_style(group_style)
.into_any() .into_any()
@ -682,8 +664,8 @@ pub(crate) mod test {
"icons/test_panel.svg" "icons/test_panel.svg"
} }
fn icon_tooltip(&self) -> String { fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>) {
"Test Panel".into() ("Test Panel".into(), None)
} }
fn should_change_position_on_event(event: &Self::Event) -> bool { fn should_change_position_on_event(event: &Self::Event) -> bool {

View file

@ -64,7 +64,7 @@ use crate::{
DockData, DockStructure, SerializedPane, SerializedPaneGroup, SerializedWorkspace, DockData, DockStructure, SerializedPane, SerializedPaneGroup, SerializedWorkspace,
}, },
}; };
use dock::{Dock, DockPosition, Panel, PanelButtons, PanelHandle, TogglePanel}; use dock::{Dock, DockPosition, Panel, PanelButtons, PanelHandle};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use notifications::{NotificationHandle, NotifyResultExt}; use notifications::{NotificationHandle, NotifyResultExt};
pub use pane::*; pub use pane::*;
@ -259,7 +259,6 @@ pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {
workspace.save_active_item(true, cx).detach_and_log_err(cx); workspace.save_active_item(true, cx).detach_and_log_err(cx);
}, },
); );
cx.add_action(Workspace::toggle_panel);
cx.add_action(|workspace: &mut Workspace, _: &ActivatePreviousPane, cx| { cx.add_action(|workspace: &mut Workspace, _: &ActivatePreviousPane, cx| {
workspace.activate_previous_pane(cx) workspace.activate_previous_pane(cx)
}); });
@ -1497,19 +1496,24 @@ impl Workspace {
self.serialize_workspace(cx); self.serialize_workspace(cx);
} }
pub fn toggle_panel(&mut self, action: &TogglePanel, cx: &mut ViewContext<Self>) { pub fn toggle_panel(
let dock = match action.dock_position { &mut self,
position: DockPosition,
panel_index: usize,
cx: &mut ViewContext<Self>,
) {
let dock = match position {
DockPosition::Left => &mut self.left_dock, DockPosition::Left => &mut self.left_dock,
DockPosition::Bottom => &mut self.bottom_dock, DockPosition::Bottom => &mut self.bottom_dock,
DockPosition::Right => &mut self.right_dock, DockPosition::Right => &mut self.right_dock,
}; };
let active_item = dock.update(cx, move |dock, cx| { let active_item = dock.update(cx, move |dock, cx| {
if dock.is_open() && dock.active_panel_index() == action.panel_index { if dock.is_open() && dock.active_panel_index() == panel_index {
dock.set_open(false, cx); dock.set_open(false, cx);
None None
} else { } else {
dock.set_open(true, cx); dock.set_open(true, cx);
dock.activate_panel(action.panel_index, cx); dock.activate_panel(panel_index, cx);
dock.active_panel().cloned() dock.active_panel().cloned()
} }
}); });