Rebind ctrl-` to toggle terminal panel focus

Also, add `ctrl-~` to create new terminals.

Co-Authored-By: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-05-17 17:35:10 +02:00
parent 747fbfadeb
commit f097444546
5 changed files with 42 additions and 34 deletions

View file

@ -39,7 +39,8 @@
"cmd-shift-n": "workspace::NewWindow", "cmd-shift-n": "workspace::NewWindow",
"cmd-o": "workspace::Open", "cmd-o": "workspace::Open",
"alt-cmd-o": "projects::OpenRecent", "alt-cmd-o": "projects::OpenRecent",
"ctrl-`": "workspace::NewTerminal" "ctrl-~": "workspace::NewTerminal",
"ctrl-`": "terminal_panel::ToggleFocus"
} }
}, },
{ {

View file

@ -1,7 +1,7 @@
use crate::TerminalView; use crate::TerminalView;
use gpui::{ use gpui::{
elements::*, AppContext, Entity, ModelHandle, Subscription, View, ViewContext, ViewHandle, actions, elements::*, AppContext, Entity, ModelHandle, Subscription, View, ViewContext,
WeakViewHandle, WindowContext, ViewHandle, WeakViewHandle, WindowContext,
}; };
use project::Project; use project::Project;
use settings::{settings_file::SettingsFile, Settings, TerminalDockPosition, WorkingDirectory}; use settings::{settings_file::SettingsFile, Settings, TerminalDockPosition, WorkingDirectory};
@ -11,6 +11,8 @@ use workspace::{
pane, DraggedItem, Pane, Workspace, pane, DraggedItem, Pane, Workspace,
}; };
actions!(terminal_panel, [ToggleFocus]);
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.add_action(TerminalPanel::add_terminal); cx.add_action(TerminalPanel::add_terminal);
} }

View file

@ -176,6 +176,12 @@ impl Dock {
.map_or(false, |panel| panel.has_focus(cx)) .map_or(false, |panel| panel.has_focus(cx))
} }
pub fn panel_index<T: Panel>(&self) -> Option<usize> {
self.panel_entries
.iter()
.position(|entry| entry.panel.as_any().is::<T>())
}
pub fn active_panel_index(&self) -> usize { pub fn active_panel_index(&self) -> usize {
self.active_panel_index self.active_panel_index
} }

View file

@ -116,6 +116,7 @@ actions!(
FollowNextCollaborator, FollowNextCollaborator,
ToggleLeftDock, ToggleLeftDock,
NewTerminal, NewTerminal,
ToggleTerminalFocus,
NewSearch, NewSearch,
Feedback, Feedback,
Restart, Restart,
@ -1475,33 +1476,27 @@ impl Workspace {
cx.notify(); cx.notify();
} }
pub fn toggle_panel_focus( pub fn toggle_panel_focus<T: Panel>(&mut self, cx: &mut ViewContext<Self>) {
&mut self, for dock in [&self.left_dock, &self.bottom_dock, &self.right_dock] {
dock_position: DockPosition, if let Some(panel_index) = dock.read(cx).panel_index::<T>() {
panel_index: usize, let active_item = dock.update(cx, |dock, cx| {
cx: &mut ViewContext<Self>, dock.set_open(true, cx);
) { dock.activate_panel(panel_index, cx);
let dock = match dock_position { dock.active_panel().cloned()
DockPosition::Left => &mut self.left_dock, });
DockPosition::Bottom => &mut self.bottom_dock, if let Some(active_item) = active_item {
DockPosition::Right => &mut self.right_dock, if active_item.has_focus(cx) {
}; cx.focus_self();
let active_item = dock.update(cx, |dock, cx| { } else {
dock.set_open(true, cx); cx.focus(active_item.as_any());
dock.activate_panel(panel_index, cx); }
dock.active_panel().cloned() }
});
if let Some(active_item) = active_item { self.serialize_workspace(cx);
if active_item.has_focus(cx) { cx.notify();
cx.focus_self(); break;
} else {
cx.focus(active_item.as_any());
} }
} }
self.serialize_workspace(cx);
cx.notify();
} }
fn zoom_out(&mut self, cx: &mut ViewContext<Self>) { fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {

View file

@ -31,14 +31,11 @@ use serde::Deserialize;
use serde_json::to_string_pretty; use serde_json::to_string_pretty;
use settings::{Settings, DEFAULT_SETTINGS_ASSET_PATH}; use settings::{Settings, DEFAULT_SETTINGS_ASSET_PATH};
use std::{borrow::Cow, str, sync::Arc}; use std::{borrow::Cow, str, sync::Arc};
use terminal_view::terminal_panel::TerminalPanel; use terminal_view::terminal_panel::{self, TerminalPanel};
use util::{channel::ReleaseChannel, paths, ResultExt}; use util::{channel::ReleaseChannel, paths, ResultExt};
use uuid::Uuid; use uuid::Uuid;
pub use workspace; pub use workspace;
use workspace::{ use workspace::{create_and_open_local_file, open_new, AppState, NewFile, NewWindow, Workspace};
create_and_open_local_file, dock::DockPosition, open_new, AppState, NewFile, NewWindow,
Workspace,
};
#[derive(Deserialize, Clone, PartialEq)] #[derive(Deserialize, Clone, PartialEq)]
pub struct OpenBrowser { pub struct OpenBrowser {
@ -242,7 +239,14 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::AppContext) {
|workspace: &mut Workspace, |workspace: &mut Workspace,
_: &project_panel::ToggleFocus, _: &project_panel::ToggleFocus,
cx: &mut ViewContext<Workspace>| { cx: &mut ViewContext<Workspace>| {
workspace.toggle_panel_focus(DockPosition::Left, 0, cx); workspace.toggle_panel_focus::<ProjectPanel>(cx);
},
);
cx.add_action(
|workspace: &mut Workspace,
_: &terminal_panel::ToggleFocus,
cx: &mut ViewContext<Workspace>| {
workspace.toggle_panel_focus::<TerminalPanel>(cx);
}, },
); );
cx.add_global_action({ cx.add_global_action({