Add new terminal when the terminal panel is activated, and not on focus

This commit is contained in:
Antonio Scandurra 2023-05-19 16:07:47 +02:00
parent 924ec961ff
commit e49281699c
3 changed files with 58 additions and 38 deletions

View file

@ -1401,6 +1401,8 @@ impl workspace::dock::Panel for ProjectPanel {
fn set_zoomed(&mut self, _: bool, _: &mut ViewContext<Self>) {} fn set_zoomed(&mut self, _: bool, _: &mut ViewContext<Self>) {}
fn set_active(&mut self, _: bool, _: &mut ViewContext<Self>) {}
fn icon_path(&self) -> &'static str { fn icon_path(&self) -> &'static str {
"icons/folder_tree_16.svg" "icons/folder_tree_16.svg"
} }

View file

@ -1,9 +1,8 @@
use crate::TerminalView; use crate::TerminalView;
use gpui::{ use gpui::{
actions, elements::*, AppContext, Entity, ModelHandle, Subscription, View, ViewContext, actions, anyhow, elements::*, AppContext, Entity, Subscription, View, ViewContext, ViewHandle,
ViewHandle, WeakViewHandle, WindowContext, WeakViewHandle, WindowContext,
}; };
use project::Project;
use settings::{settings_file::SettingsFile, Settings, TerminalDockPosition, WorkingDirectory}; use settings::{settings_file::SettingsFile, Settings, TerminalDockPosition, WorkingDirectory};
use util::ResultExt; use util::ResultExt;
use workspace::{ use workspace::{
@ -26,7 +25,6 @@ pub enum Event {
} }
pub struct TerminalPanel { pub struct TerminalPanel {
project: ModelHandle<Project>,
pane: ViewHandle<Pane>, pane: ViewHandle<Pane>,
workspace: WeakViewHandle<Workspace>, workspace: WeakViewHandle<Workspace>,
_subscriptions: Vec<Subscription>, _subscriptions: Vec<Subscription>,
@ -86,7 +84,6 @@ impl TerminalPanel {
cx.subscribe(&pane, Self::handle_pane_event), cx.subscribe(&pane, Self::handle_pane_event),
]; ];
Self { Self {
project: workspace.project().clone(),
pane, pane,
workspace: workspace.weak_handle(), workspace: workspace.weak_handle(),
_subscriptions: subscriptions, _subscriptions: subscriptions,
@ -109,30 +106,34 @@ impl TerminalPanel {
} }
fn add_terminal(&mut self, _: &workspace::NewTerminal, cx: &mut ViewContext<Self>) { fn add_terminal(&mut self, _: &workspace::NewTerminal, cx: &mut ViewContext<Self>) {
if let Some(workspace) = self.workspace.upgrade(cx) { let workspace = self.workspace.clone();
let working_directory_strategy = cx cx.spawn(|this, mut cx| async move {
.global::<Settings>() let pane = this.read_with(&cx, |this, _| this.pane.clone())?;
.terminal_overrides workspace.update(&mut cx, |workspace, cx| {
.working_directory let working_directory_strategy = cx
.clone() .global::<Settings>()
.unwrap_or(WorkingDirectory::CurrentProjectDirectory); .terminal_overrides
let working_directory = .working_directory
crate::get_working_directory(workspace.read(cx), cx, working_directory_strategy); .clone()
let window_id = cx.window_id(); .unwrap_or(WorkingDirectory::CurrentProjectDirectory);
if let Some(terminal) = self.project.update(cx, |project, cx| { let working_directory =
project crate::get_working_directory(workspace, cx, working_directory_strategy);
.create_terminal(working_directory, window_id, cx) let window_id = cx.window_id();
.log_err() if let Some(terminal) = workspace.project().update(cx, |project, cx| {
}) { project
workspace.update(cx, |workspace, cx| { .create_terminal(working_directory, window_id, cx)
.log_err()
}) {
let terminal = let terminal =
Box::new(cx.add_view(|cx| { Box::new(cx.add_view(|cx| {
TerminalView::new(terminal, workspace.database_id(), cx) TerminalView::new(terminal, workspace.database_id(), cx)
})); }));
Pane::add_item(workspace, &self.pane, terminal, true, true, None, cx); Pane::add_item(workspace, &pane, terminal, true, true, None, cx);
}); }
} })?;
} anyhow::Ok(())
})
.detach_and_log_err(cx);
} }
} }
@ -150,10 +151,6 @@ impl View for TerminalPanel {
} }
fn focus_in(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext<Self>) { fn focus_in(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext<Self>) {
if self.pane.read(cx).items_len() == 0 {
self.add_terminal(&Default::default(), cx)
}
if cx.is_self_focused() { if cx.is_self_focused() {
cx.focus(&self.pane); cx.focus(&self.pane);
} }
@ -215,6 +212,12 @@ impl Panel for TerminalPanel {
self.pane.update(cx, |pane, cx| pane.set_zoomed(zoomed, cx)); self.pane.update(cx, |pane, cx| pane.set_zoomed(zoomed, cx));
} }
fn set_active(&mut self, active: bool, cx: &mut ViewContext<Self>) {
if active && self.pane.read(cx).items_len() == 0 {
self.add_terminal(&Default::default(), cx)
}
}
fn icon_path(&self) -> &'static str { fn icon_path(&self) -> &'static str {
"icons/terminal_12.svg" "icons/terminal_12.svg"
} }

View file

@ -24,6 +24,7 @@ pub trait Panel: View {
fn should_zoom_out_on_event(_: &Self::Event) -> bool; fn should_zoom_out_on_event(_: &Self::Event) -> bool;
fn is_zoomed(&self, cx: &WindowContext) -> bool; fn is_zoomed(&self, cx: &WindowContext) -> bool;
fn set_zoomed(&mut self, zoomed: bool, cx: &mut ViewContext<Self>); fn set_zoomed(&mut self, zoomed: bool, cx: &mut ViewContext<Self>);
fn set_active(&mut self, active: bool, cx: &mut ViewContext<Self>);
fn should_activate_on_event(_: &Self::Event) -> bool; fn should_activate_on_event(_: &Self::Event) -> bool;
fn should_close_on_event(_: &Self::Event) -> bool; fn should_close_on_event(_: &Self::Event) -> bool;
fn has_focus(&self, cx: &WindowContext) -> bool; fn has_focus(&self, cx: &WindowContext) -> bool;
@ -37,6 +38,7 @@ pub trait PanelHandle {
fn set_position(&self, position: DockPosition, cx: &mut WindowContext); fn set_position(&self, position: DockPosition, cx: &mut WindowContext);
fn is_zoomed(&self, cx: &WindowContext) -> bool; fn is_zoomed(&self, cx: &WindowContext) -> bool;
fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext); fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
fn set_active(&self, active: bool, cx: &mut WindowContext);
fn default_size(&self, cx: &WindowContext) -> f32; fn default_size(&self, cx: &WindowContext) -> f32;
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;
@ -77,6 +79,10 @@ where
self.update(cx, |this, cx| this.set_zoomed(zoomed, cx)) self.update(cx, |this, cx| this.set_zoomed(zoomed, cx))
} }
fn set_active(&self, active: bool, cx: &mut WindowContext) {
self.update(cx, |this, cx| this.set_active(active, cx))
}
fn icon_path(&self, cx: &WindowContext) -> &'static str { fn icon_path(&self, cx: &WindowContext) -> &'static str {
self.read(cx).icon_path() self.read(cx).icon_path()
} }
@ -202,12 +208,16 @@ impl Dock {
pub fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) { pub fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
if open != self.is_open { if open != self.is_open {
self.is_open = open; self.is_open = open;
if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
active_panel.panel.set_active(open, cx);
}
cx.notify(); cx.notify();
} }
} }
pub fn toggle_open(&mut self, cx: &mut ViewContext<Self>) { pub fn toggle_open(&mut self, cx: &mut ViewContext<Self>) {
self.is_open = !self.is_open; self.set_open(!self.is_open, cx);
cx.notify(); cx.notify();
} }
@ -297,17 +307,18 @@ impl Dock {
} }
pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) { pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
self.active_panel_index = panel_ix; if panel_ix != self.active_panel_index {
cx.notify(); if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
} active_panel.panel.set_active(false, cx);
}
pub fn toggle_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
if self.active_panel_index == panel_ix {
self.is_open = false;
} else {
self.active_panel_index = panel_ix; self.active_panel_index = panel_ix;
if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
active_panel.panel.set_active(true, cx);
}
cx.notify();
} }
cx.notify();
} }
pub fn active_panel(&self) -> Option<&Rc<dyn PanelHandle>> { pub fn active_panel(&self) -> Option<&Rc<dyn PanelHandle>> {
@ -615,6 +626,10 @@ pub(crate) mod test {
unimplemented!() unimplemented!()
} }
fn set_active(&mut self, _active: bool, _cx: &mut ViewContext<Self>) {
unimplemented!()
}
fn default_size(&self, _: &WindowContext) -> f32 { fn default_size(&self, _: &WindowContext) -> f32 {
match self.position.axis() { match self.position.axis() {
Axis::Horizontal => 300., Axis::Horizontal => 300.,