Give focus to the selected terminal

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Petros Amoiridis 2023-03-08 19:47:57 +02:00
parent baa9e271d5
commit ad7e49ed06
No known key found for this signature in database
6 changed files with 43 additions and 20 deletions

1
Cargo.lock generated
View file

@ -8299,6 +8299,7 @@ dependencies = [
"serde_json", "serde_json",
"settings", "settings",
"smallvec", "smallvec",
"terminal",
"theme", "theme",
"util", "util",
"uuid 1.2.2", "uuid 1.2.2",

View file

@ -1,21 +1,26 @@
use context_menu::{ContextMenu, ContextMenuItem}; use context_menu::{ContextMenu, ContextMenuItem};
use gpui::{ use gpui::{
actions, elements::*, geometry::vector::vec2f, CursorStyle, Element, ElementBox, Entity, actions, elements::*, geometry::vector::vec2f, impl_internal_actions, CursorStyle, Element,
MouseButton, MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle, ElementBox, Entity, MouseButton, MutableAppContext, RenderContext, View, ViewContext,
ViewHandle, WeakModelHandle, WeakViewHandle,
}; };
use settings::Settings; use settings::Settings;
use terminal::Terminal;
use workspace::{dock::FocusDock, item::ItemHandle, NewTerminal, StatusItemView, Workspace};
use crate::{dock::FocusDock, item::ItemHandle, NewTerminal, StatusItemView, Workspace}; use crate::TerminalView;
// #[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
// pub struct DeployTerminalMenu { pub struct FocusTerminal {
// position: Vector2F, terminal_handle: WeakModelHandle<Terminal>,
// } }
actions!(terminal, [DeployTerminalMenu]); actions!(terminal, [DeployTerminalMenu]);
impl_internal_actions!(terminal, [FocusTerminal]);
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_action(TerminalButton::deploy_terminal_menu); cx.add_action(TerminalButton::deploy_terminal_menu);
cx.add_action(TerminalButton::focus_terminal);
} }
pub struct TerminalButton { pub struct TerminalButton {
@ -125,10 +130,11 @@ impl TerminalButton {
for local_terminal_handle in local_terminal_handles { for local_terminal_handle in local_terminal_handles {
if let Some(terminal) = local_terminal_handle.upgrade(cx) { if let Some(terminal) = local_terminal_handle.upgrade(cx) {
// TODO: Replace the `NewTerminal` action with an action that instead focuses the selected terminal
menu_options.push(ContextMenuItem::item( menu_options.push(ContextMenuItem::item(
terminal.read(cx).title(), terminal.read(cx).title(),
NewTerminal, FocusTerminal {
terminal_handle: local_terminal_handle.clone(),
},
)) ))
} }
} }
@ -138,6 +144,21 @@ impl TerminalButton {
menu.show(vec2f(0., 0.), AnchorCorner::TopRight, menu_options, cx); menu.show(vec2f(0., 0.), AnchorCorner::TopRight, menu_options, cx);
}); });
} }
pub fn focus_terminal(&mut self, action: &FocusTerminal, cx: &mut ViewContext<Self>) {
if let Some(workspace) = self.workspace.upgrade(cx) {
workspace.update(cx, |workspace, cx| {
let terminal = workspace
.items_of_type::<TerminalView>(cx)
.find(|terminal| {
terminal.read(cx).model().downgrade() == action.terminal_handle
});
if let Some(terminal) = terminal {
workspace.activate_item(&terminal, cx);
}
});
}
}
} }
impl StatusItemView for TerminalButton { impl StatusItemView for TerminalButton {

View file

@ -1,4 +1,5 @@
mod persistence; mod persistence;
pub mod terminal_button;
pub mod terminal_element; pub mod terminal_element;
use std::{ use std::{
@ -177,8 +178,8 @@ impl TerminalView {
} }
} }
pub fn handle(&self) -> ModelHandle<Terminal> { pub fn model(&self) -> &ModelHandle<Terminal> {
self.terminal.clone() &self.terminal
} }
pub fn has_new_content(&self) -> bool { pub fn has_new_content(&self) -> bool {

View file

@ -31,6 +31,7 @@ language = { path = "../language" }
menu = { path = "../menu" } menu = { path = "../menu" }
project = { path = "../project" } project = { path = "../project" }
settings = { path = "../settings" } settings = { path = "../settings" }
terminal = { path = "../terminal" }
theme = { path = "../theme" } theme = { path = "../theme" }
util = { path = "../util" } util = { path = "../util" }
async-recursion = "1.0.0" async-recursion = "1.0.0"

View file

@ -12,7 +12,6 @@ pub mod searchable;
pub mod shared_screen; pub mod shared_screen;
pub mod sidebar; pub mod sidebar;
mod status_bar; mod status_bar;
pub mod terminal_button;
mod toolbar; mod toolbar;
pub use smallvec; pub use smallvec;
@ -57,7 +56,6 @@ use std::{
sync::Arc, sync::Arc,
time::Duration, time::Duration,
}; };
use terminal_button::TerminalButton;
use crate::{ use crate::{
notifications::simple_message_notification::{MessageNotification, OsOpen}, notifications::simple_message_notification::{MessageNotification, OsOpen},
@ -83,7 +81,7 @@ use status_bar::StatusBar;
pub use status_bar::StatusItemView; pub use status_bar::StatusItemView;
use theme::{Theme, ThemeRegistry}; use theme::{Theme, ThemeRegistry};
pub use toolbar::{ToolbarItemLocation, ToolbarItemView}; pub use toolbar::{ToolbarItemLocation, ToolbarItemView};
use util::{ResultExt, StaffMode}; use util::ResultExt;
lazy_static! { lazy_static! {
static ref ZED_WINDOW_SIZE: Option<Vector2F> = env::var("ZED_WINDOW_SIZE") static ref ZED_WINDOW_SIZE: Option<Vector2F> = env::var("ZED_WINDOW_SIZE")
@ -185,7 +183,6 @@ impl_actions!(workspace, [ActivatePane]);
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) { pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
pane::init(cx); pane::init(cx);
dock::init(cx); dock::init(cx);
terminal_button::init(cx);
notifications::init(cx); notifications::init(cx);
cx.add_global_action(open); cx.add_global_action(open);
@ -587,7 +584,6 @@ impl Workspace {
let left_sidebar = cx.add_view(|_| Sidebar::new(SidebarSide::Left)); let left_sidebar = cx.add_view(|_| Sidebar::new(SidebarSide::Left));
let right_sidebar = cx.add_view(|_| Sidebar::new(SidebarSide::Right)); let right_sidebar = cx.add_view(|_| Sidebar::new(SidebarSide::Right));
let left_sidebar_buttons = cx.add_view(|cx| SidebarButtons::new(left_sidebar.clone(), cx)); let left_sidebar_buttons = cx.add_view(|cx| SidebarButtons::new(left_sidebar.clone(), cx));
let toggle_terminal = cx.add_view(|cx| TerminalButton::new(handle.clone(), cx));
let toggle_dock = cx.add_view(|cx| ToggleDockButton::new(handle, cx)); let toggle_dock = cx.add_view(|cx| ToggleDockButton::new(handle, cx));
let right_sidebar_buttons = let right_sidebar_buttons =
cx.add_view(|cx| SidebarButtons::new(right_sidebar.clone(), cx)); cx.add_view(|cx| SidebarButtons::new(right_sidebar.clone(), cx));
@ -596,10 +592,6 @@ impl Workspace {
status_bar.add_left_item(left_sidebar_buttons, cx); status_bar.add_left_item(left_sidebar_buttons, cx);
status_bar.add_right_item(right_sidebar_buttons, cx); status_bar.add_right_item(right_sidebar_buttons, cx);
status_bar.add_right_item(toggle_dock, cx); status_bar.add_right_item(toggle_dock, cx);
// TOOD: Remove this when things are done
if **cx.default_global::<StaffMode>() {
status_bar.add_right_item(toggle_terminal, cx);
}
status_bar status_bar
}); });

View file

@ -31,6 +31,7 @@ use serde::Deserialize;
use serde_json::to_string_pretty; use serde_json::to_string_pretty;
use settings::{keymap_file_json_schema, settings_file_json_schema, Settings}; use settings::{keymap_file_json_schema, settings_file_json_schema, Settings};
use std::{borrow::Cow, env, path::Path, str, sync::Arc}; use std::{borrow::Cow, env, path::Path, str, sync::Arc};
use terminal_view::terminal_button::{self, TerminalButton};
use util::{channel::ReleaseChannel, paths, ResultExt, StaffMode}; use util::{channel::ReleaseChannel, paths, ResultExt, StaffMode};
use uuid::Uuid; use uuid::Uuid;
pub use workspace; pub use workspace;
@ -73,6 +74,7 @@ actions!(
const MIN_FONT_SIZE: f32 = 6.0; const MIN_FONT_SIZE: f32 = 6.0;
pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) { pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
terminal_button::init(cx);
cx.add_action(about); cx.add_action(about);
cx.add_global_action(|_: &Hide, cx: &mut gpui::MutableAppContext| { cx.add_global_action(|_: &Hide, cx: &mut gpui::MutableAppContext| {
cx.platform().hide(); cx.platform().hide();
@ -330,6 +332,7 @@ pub fn initialize_workspace(
) )
}); });
let toggle_terminal = cx.add_view(|cx| TerminalButton::new(workspace_handle.clone(), cx));
let diagnostic_summary = let diagnostic_summary =
cx.add_view(|cx| diagnostics::items::DiagnosticIndicator::new(workspace.project(), cx)); cx.add_view(|cx| diagnostics::items::DiagnosticIndicator::new(workspace.project(), cx));
let activity_indicator = let activity_indicator =
@ -340,6 +343,10 @@ pub fn initialize_workspace(
workspace.status_bar().update(cx, |status_bar, cx| { workspace.status_bar().update(cx, |status_bar, cx| {
status_bar.add_left_item(diagnostic_summary, cx); status_bar.add_left_item(diagnostic_summary, cx);
status_bar.add_left_item(activity_indicator, cx); status_bar.add_left_item(activity_indicator, cx);
// TODO: Remove this when things are done
if **cx.default_global::<StaffMode>() {
status_bar.add_right_item(toggle_terminal, cx);
}
status_bar.add_right_item(cursor_position, cx); status_bar.add_right_item(cursor_position, cx);
status_bar.add_right_item(feedback_button, cx); status_bar.add_right_item(feedback_button, cx);
}); });