Remove 2 suffix for language_tools, search, terminal_view, auto_update

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-01-03 10:52:40 -08:00
parent 292b3397ab
commit 0ac8aae17b
51 changed files with 3900 additions and 14333 deletions

File diff suppressed because it is too large Load diff

View file

@ -3,110 +3,107 @@ use std::{path::PathBuf, sync::Arc};
use crate::TerminalView;
use db::kvp::KEY_VALUE_STORE;
use gpui::{
actions, anyhow::Result, elements::*, serde_json, Action, AppContext, AsyncAppContext, Entity,
Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext,
actions, div, serde_json, AppContext, AsyncWindowContext, Entity, EventEmitter, ExternalPaths,
FocusHandle, FocusableView, IntoElement, ParentElement, Pixels, Render, Styled, Subscription,
Task, View, ViewContext, VisualContext, WeakView, WindowContext,
};
use project::Fs;
use serde::{Deserialize, Serialize};
use settings::SettingsStore;
use settings::{Settings, SettingsStore};
use terminal::terminal_settings::{TerminalDockPosition, TerminalSettings};
use ui::{h_stack, ButtonCommon, Clickable, IconButton, IconSize, Selectable, Tooltip};
use util::{ResultExt, TryFutureExt};
use workspace::{
dock::{DockPosition, Panel},
dock::{DockPosition, Panel, PanelEvent},
item::Item,
pane, DraggedItem, Pane, Workspace,
pane,
ui::Icon,
Pane, Workspace,
};
use anyhow::Result;
const TERMINAL_PANEL_KEY: &'static str = "TerminalPanel";
actions!(terminal_panel, [ToggleFocus]);
pub fn init(cx: &mut AppContext) {
cx.add_action(TerminalPanel::new_terminal);
cx.add_action(TerminalPanel::open_terminal);
}
#[derive(Debug)]
pub enum Event {
Close,
DockPositionChanged,
ZoomIn,
ZoomOut,
Focus,
cx.observe_new_views(
|workspace: &mut Workspace, _: &mut ViewContext<Workspace>| {
workspace.register_action(TerminalPanel::new_terminal);
workspace.register_action(TerminalPanel::open_terminal);
workspace.register_action(|workspace, _: &ToggleFocus, cx| {
workspace.toggle_panel_focus::<TerminalPanel>(cx);
});
},
)
.detach();
}
pub struct TerminalPanel {
pane: ViewHandle<Pane>,
pane: View<Pane>,
fs: Arc<dyn Fs>,
workspace: WeakViewHandle<Workspace>,
width: Option<f32>,
height: Option<f32>,
workspace: WeakView<Workspace>,
width: Option<Pixels>,
height: Option<Pixels>,
pending_serialization: Task<Option<()>>,
_subscriptions: Vec<Subscription>,
}
impl TerminalPanel {
fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self {
let weak_self = cx.weak_handle();
let pane = cx.add_view(|cx| {
let window = cx.window();
let terminal_panel = cx.view().clone();
let pane = cx.new_view(|cx| {
let mut pane = Pane::new(
workspace.weak_handle(),
workspace.project().clone(),
workspace.app_state().background_actions,
Default::default(),
Some(Arc::new(|a, cx| {
if let Some(tab) = a.downcast_ref::<workspace::pane::DraggedTab>() {
if let Some(item) = tab.pane.read(cx).item_for_index(tab.ix) {
return item.downcast::<TerminalView>().is_some();
}
}
if a.downcast_ref::<ExternalPaths>().is_some() {
return true;
}
false
})),
cx,
);
pane.set_can_split(false, cx);
pane.set_can_navigate(false, cx);
pane.on_can_drop(move |drag_and_drop, cx| {
drag_and_drop
.currently_dragged::<DraggedItem>(window)
.map_or(false, |(_, item)| {
item.handle.act_as::<TerminalView>(cx).is_some()
})
});
pane.display_nav_history_buttons(false);
pane.set_render_tab_bar_buttons(cx, move |pane, cx| {
let this = weak_self.clone();
Flex::row()
.with_child(Pane::render_tab_bar_button(
0,
"icons/plus.svg",
false,
Some(("New Terminal", Some(Box::new(workspace::NewTerminal)))),
cx,
move |_, cx| {
let this = this.clone();
cx.window_context().defer(move |cx| {
if let Some(this) = this.upgrade(cx) {
this.update(cx, |this, cx| {
this.add_terminal(None, cx);
});
}
h_stack()
.gap_2()
.child(
IconButton::new("plus", Icon::Plus)
.icon_size(IconSize::Small)
.on_click(cx.listener_for(&terminal_panel, |terminal_panel, _, cx| {
terminal_panel.add_terminal(None, cx);
}))
.tooltip(|cx| Tooltip::text("New Terminal", cx)),
)
.child({
let zoomed = pane.is_zoomed();
IconButton::new("toggle_zoom", Icon::Maximize)
.icon_size(IconSize::Small)
.selected(zoomed)
.selected_icon(Icon::Minimize)
.on_click(cx.listener(|pane, _, cx| {
pane.toggle_zoom(&workspace::ToggleZoom, cx);
}))
.tooltip(move |cx| {
Tooltip::text(if zoomed { "Zoom Out" } else { "Zoom In" }, cx)
})
},
|_, _| {},
None,
))
.with_child(Pane::render_tab_bar_button(
1,
if pane.is_zoomed() {
"icons/minimize.svg"
} else {
"icons/maximize.svg"
},
pane.is_zoomed(),
Some(("Toggle Zoom".into(), Some(Box::new(workspace::ToggleZoom)))),
cx,
move |pane, cx| pane.toggle_zoom(&Default::default(), cx),
|_, _| {},
None,
))
.into_any()
})
.into_any_element()
});
let buffer_search_bar = cx.add_view(search::BufferSearchBar::new);
pane.toolbar()
.update(cx, |toolbar, cx| toolbar.add_item(buffer_search_bar, cx));
// let buffer_search_bar = cx.build_view(search::BufferSearchBar::new);
// pane.toolbar()
// .update(cx, |toolbar, cx| toolbar.add_item(buffer_search_bar, cx));
pane
});
let subscriptions = vec![
@ -123,105 +120,103 @@ impl TerminalPanel {
_subscriptions: subscriptions,
};
let mut old_dock_position = this.position(cx);
cx.observe_global::<SettingsStore, _>(move |this, cx| {
cx.observe_global::<SettingsStore>(move |this, cx| {
let new_dock_position = this.position(cx);
if new_dock_position != old_dock_position {
old_dock_position = new_dock_position;
cx.emit(Event::DockPositionChanged);
cx.emit(PanelEvent::ChangePosition);
}
})
.detach();
this
}
pub fn load(
workspace: WeakViewHandle<Workspace>,
cx: AsyncAppContext,
) -> Task<Result<ViewHandle<Self>>> {
cx.spawn(|mut cx| async move {
let serialized_panel = if let Some(panel) = cx
.background()
.spawn(async move { KEY_VALUE_STORE.read_kvp(TERMINAL_PANEL_KEY) })
.await
.log_err()
.flatten()
{
Some(serde_json::from_str::<SerializedTerminalPanel>(&panel)?)
} else {
None
};
let (panel, pane, items) = workspace.update(&mut cx, |workspace, cx| {
let panel = cx.add_view(|cx| TerminalPanel::new(workspace, cx));
let items = if let Some(serialized_panel) = serialized_panel.as_ref() {
panel.update(cx, |panel, cx| {
cx.notify();
panel.height = serialized_panel.height;
panel.width = serialized_panel.width;
panel.pane.update(cx, |_, cx| {
serialized_panel
.items
.iter()
.map(|item_id| {
TerminalView::deserialize(
workspace.project().clone(),
workspace.weak_handle(),
workspace.database_id(),
*item_id,
cx,
)
})
.collect::<Vec<_>>()
})
})
} else {
Default::default()
};
let pane = panel.read(cx).pane.clone();
(panel, pane, items)
})?;
pub async fn load(
workspace: WeakView<Workspace>,
mut cx: AsyncWindowContext,
) -> Result<View<Self>> {
let serialized_panel = cx
.background_executor()
.spawn(async move { KEY_VALUE_STORE.read_kvp(TERMINAL_PANEL_KEY) })
.await
.log_err()
.flatten()
.map(|panel| serde_json::from_str::<SerializedTerminalPanel>(&panel))
.transpose()
.log_err()
.flatten();
let pane = pane.downgrade();
let items = futures::future::join_all(items).await;
pane.update(&mut cx, |pane, cx| {
let active_item_id = serialized_panel
.as_ref()
.and_then(|panel| panel.active_item_id);
let mut active_ix = None;
for item in items {
if let Some(item) = item.log_err() {
let item_id = item.id();
pane.add_item(Box::new(item), false, false, None, cx);
if Some(item_id) == active_item_id {
active_ix = Some(pane.items_len() - 1);
}
let (panel, pane, items) = workspace.update(&mut cx, |workspace, cx| {
let panel = cx.new_view(|cx| TerminalPanel::new(workspace, cx));
let items = if let Some(serialized_panel) = serialized_panel.as_ref() {
panel.update(cx, |panel, cx| {
cx.notify();
panel.height = serialized_panel.height;
panel.width = serialized_panel.width;
panel.pane.update(cx, |_, cx| {
serialized_panel
.items
.iter()
.map(|item_id| {
TerminalView::deserialize(
workspace.project().clone(),
workspace.weak_handle(),
workspace.database_id(),
*item_id,
cx,
)
})
.collect::<Vec<_>>()
})
})
} else {
Default::default()
};
let pane = panel.read(cx).pane.clone();
(panel, pane, items)
})?;
let pane = pane.downgrade();
let items = futures::future::join_all(items).await;
pane.update(&mut cx, |pane, cx| {
let active_item_id = serialized_panel
.as_ref()
.and_then(|panel| panel.active_item_id);
let mut active_ix = None;
for item in items {
if let Some(item) = item.log_err() {
let item_id = item.entity_id().as_u64();
pane.add_item(Box::new(item), false, false, None, cx);
if Some(item_id) == active_item_id {
active_ix = Some(pane.items_len() - 1);
}
}
}
if let Some(active_ix) = active_ix {
pane.activate_item(active_ix, false, false, cx)
}
})?;
if let Some(active_ix) = active_ix {
pane.activate_item(active_ix, false, false, cx)
}
})?;
Ok(panel)
})
Ok(panel)
}
fn handle_pane_event(
&mut self,
_pane: ViewHandle<Pane>,
_pane: View<Pane>,
event: &pane::Event,
cx: &mut ViewContext<Self>,
) {
match event {
pane::Event::ActivateItem { .. } => self.serialize(cx),
pane::Event::RemoveItem { .. } => self.serialize(cx),
pane::Event::Remove => cx.emit(Event::Close),
pane::Event::ZoomIn => cx.emit(Event::ZoomIn),
pane::Event::ZoomOut => cx.emit(Event::ZoomOut),
pane::Event::Focus => cx.emit(Event::Focus),
pane::Event::Remove => cx.emit(PanelEvent::Close),
pane::Event::ZoomIn => cx.emit(PanelEvent::ZoomIn),
pane::Event::ZoomOut => cx.emit(PanelEvent::ZoomOut),
pane::Event::Focus => cx.emit(PanelEvent::Focus),
pane::Event::AddItem { item } => {
if let Some(workspace) = self.workspace.upgrade(cx) {
if let Some(workspace) = self.workspace.upgrade() {
let pane = self.pane.clone();
workspace.update(cx, |workspace, cx| item.added_to_pane(workspace, pane, cx))
}
@ -261,24 +256,23 @@ impl TerminalPanel {
fn add_terminal(&mut self, working_directory: Option<PathBuf>, cx: &mut ViewContext<Self>) {
let workspace = self.workspace.clone();
cx.spawn(|this, mut cx| async move {
let pane = this.read_with(&cx, |this, _| this.pane.clone())?;
let pane = this.update(&mut cx, |this, _| this.pane.clone())?;
workspace.update(&mut cx, |workspace, cx| {
let working_directory = if let Some(working_directory) = working_directory {
Some(working_directory)
} else {
let working_directory_strategy = settings::get::<TerminalSettings>(cx)
.working_directory
.clone();
let working_directory_strategy =
TerminalSettings::get_global(cx).working_directory.clone();
crate::get_working_directory(workspace, cx, working_directory_strategy)
};
let window = cx.window();
let window = cx.window_handle();
if let Some(terminal) = workspace.project().update(cx, |project, cx| {
project
.create_terminal(working_directory, window, cx)
.log_err()
}) {
let terminal = Box::new(cx.add_view(|cx| {
let terminal = Box::new(cx.new_view(|cx| {
TerminalView::new(
terminal,
workspace.weak_handle(),
@ -287,7 +281,7 @@ impl TerminalPanel {
)
}));
pane.update(cx, |pane, cx| {
let focus = pane.has_focus();
let focus = pane.has_focus(cx);
pane.add_item(terminal, true, focus, None, cx);
});
}
@ -303,12 +297,16 @@ impl TerminalPanel {
.pane
.read(cx)
.items()
.map(|item| item.id())
.map(|item| item.item_id().as_u64())
.collect::<Vec<_>>();
let active_item_id = self.pane.read(cx).active_item().map(|item| item.id());
let active_item_id = self
.pane
.read(cx)
.active_item()
.map(|item| item.item_id().as_u64());
let height = self.height;
let width = self.width;
self.pending_serialization = cx.background().spawn(
self.pending_serialization = cx.background_executor().spawn(
async move {
KEY_VALUE_STORE
.write_kvp(
@ -328,29 +326,23 @@ impl TerminalPanel {
}
}
impl Entity for TerminalPanel {
type Event = Event;
impl EventEmitter<PanelEvent> for TerminalPanel {}
impl Render for TerminalPanel {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
div().size_full().child(self.pane.clone())
}
}
impl View for TerminalPanel {
fn ui_name() -> &'static str {
"TerminalPanel"
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> gpui::AnyElement<Self> {
ChildView::new(&self.pane, cx).into_any()
}
fn focus_in(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext<Self>) {
if cx.is_self_focused() {
cx.focus(&self.pane);
}
impl FocusableView for TerminalPanel {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.pane.focus_handle(cx)
}
}
impl Panel for TerminalPanel {
fn position(&self, cx: &WindowContext) -> DockPosition {
match settings::get::<TerminalSettings>(cx).dock {
match TerminalSettings::get_global(cx).dock {
TerminalDockPosition::Left => DockPosition::Left,
TerminalDockPosition::Bottom => DockPosition::Bottom,
TerminalDockPosition::Right => DockPosition::Right,
@ -372,8 +364,8 @@ impl Panel for TerminalPanel {
});
}
fn size(&self, cx: &WindowContext) -> f32 {
let settings = settings::get::<TerminalSettings>(cx);
fn size(&self, cx: &WindowContext) -> Pixels {
let settings = TerminalSettings::get_global(cx);
match self.position(cx) {
DockPosition::Left | DockPosition::Right => {
self.width.unwrap_or_else(|| settings.default_width)
@ -382,7 +374,7 @@ impl Panel for TerminalPanel {
}
}
fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) {
fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
match self.position(cx) {
DockPosition::Left | DockPosition::Right => self.width = size,
DockPosition::Bottom => self.height = size,
@ -391,14 +383,6 @@ impl Panel for TerminalPanel {
cx.notify();
}
fn should_zoom_in_on_event(event: &Event) -> bool {
matches!(event, Event::ZoomIn)
}
fn should_zoom_out_on_event(event: &Event) -> bool {
matches!(event, Event::ZoomOut)
}
fn is_zoomed(&self, cx: &WindowContext) -> bool {
self.pane.read(cx).is_zoomed()
}
@ -413,14 +397,6 @@ impl Panel for TerminalPanel {
}
}
fn icon_path(&self, _: &WindowContext) -> Option<&'static str> {
Some("icons/terminal.svg")
}
fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>) {
("Terminal Panel".into(), Some(Box::new(ToggleFocus)))
}
fn icon_label(&self, cx: &WindowContext) -> Option<String> {
let count = self.pane.read(cx).items_len();
if count == 0 {
@ -430,31 +406,32 @@ impl Panel for TerminalPanel {
}
}
fn should_change_position_on_event(event: &Self::Event) -> bool {
matches!(event, Event::DockPositionChanged)
fn persistent_name() -> &'static str {
"TerminalPanel"
}
fn should_activate_on_event(_: &Self::Event) -> bool {
false
// todo!()
// fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>) {
// ("Terminal Panel".into(), Some(Box::new(ToggleFocus)))
// }
fn icon(&self, _cx: &WindowContext) -> Option<Icon> {
Some(Icon::Terminal)
}
fn should_close_on_event(event: &Event) -> bool {
matches!(event, Event::Close)
fn icon_tooltip(&self, _cx: &WindowContext) -> Option<&'static str> {
Some("Terminal Panel")
}
fn has_focus(&self, cx: &WindowContext) -> bool {
self.pane.read(cx).has_focus()
}
fn is_focus_event(event: &Self::Event) -> bool {
matches!(event, Event::Focus)
fn toggle_action(&self) -> Box<dyn gpui::Action> {
Box::new(ToggleFocus)
}
}
#[derive(Serialize, Deserialize)]
struct SerializedTerminalPanel {
items: Vec<usize>,
active_item_id: Option<usize>,
width: Option<f32>,
height: Option<f32>,
items: Vec<u64>,
active_item_id: Option<u64>,
width: Option<Pixels>,
height: Option<Pixels>,
}

View file

@ -2,48 +2,47 @@ mod persistence;
pub mod terminal_element;
pub mod terminal_panel;
use crate::{persistence::TERMINAL_DB, terminal_element::TerminalElement};
use anyhow::Context;
use context_menu::{ContextMenu, ContextMenuItem};
use dirs::home_dir;
// todo!()
// use crate::terminal_element::TerminalElement;
use editor::{scroll::autoscroll::Autoscroll, Editor};
use gpui::{
actions,
elements::{AnchorCorner, ChildView, Flex, Label, ParentElement, Stack},
geometry::vector::Vector2F,
impl_actions,
keymap_matcher::{KeymapContext, Keystroke},
platform::{KeyDownEvent, ModifiersChangedEvent},
AnyElement, AnyViewHandle, AppContext, Element, Entity, ModelHandle, Task, View, ViewContext,
ViewHandle, WeakViewHandle,
div, impl_actions, overlay, AnyElement, AppContext, DismissEvent, EventEmitter, FocusHandle,
FocusableView, KeyContext, KeyDownEvent, Keystroke, Model, MouseButton, MouseDownEvent, Pixels,
Render, Styled, Subscription, Task, View, VisualContext, WeakView,
};
use language::Bias;
use persistence::TERMINAL_DB;
use project::{search::SearchQuery, LocalWorktree, Project};
use serde::Deserialize;
use smallvec::{smallvec, SmallVec};
use smol::Timer;
use std::{
borrow::Cow,
ops::RangeInclusive,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
use terminal::{
alacritty_terminal::{
index::Point,
term::{search::RegexSearch, TermMode},
},
terminal_settings::{TerminalBlink, TerminalSettings, WorkingDirectory},
Event, MaybeNavigationTarget, Terminal,
Clear, Copy, Event, MaybeNavigationTarget, Paste, ShowCharacterPalette, Terminal,
};
use terminal_element::TerminalElement;
use ui::{h_stack, prelude::*, ContextMenu, Icon, IconElement, Label};
use util::{paths::PathLikeWithPosition, ResultExt};
use workspace::{
item::{BreadcrumbText, Item, ItemEvent},
notifications::NotifyResultExt,
pane, register_deserializable_item,
searchable::{SearchEvent, SearchOptions, SearchableItem, SearchableItemHandle},
NewCenterTerminal, Pane, ToolbarItemLocation, Workspace, WorkspaceId,
register_deserializable_item,
searchable::{SearchEvent, SearchOptions, SearchableItem},
CloseActiveItem, NewCenterTerminal, Pane, ToolbarItemLocation, Workspace, WorkspaceId,
};
use anyhow::Context;
use dirs::home_dir;
use serde::Deserialize;
use settings::Settings;
use smol::Timer;
use std::{
ops::RangeInclusive,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
@ -52,18 +51,13 @@ const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
#[derive(Clone, Debug, PartialEq)]
pub struct ScrollTerminal(pub i32);
#[derive(Clone, Default, Deserialize, PartialEq)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct SendText(String);
#[derive(Clone, Default, Deserialize, PartialEq)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct SendKeystroke(String);
actions!(
terminal,
[Clear, Copy, Paste, ShowCharacterPalette, SearchTest]
);
impl_actions!(terminal, [SendText, SendKeystroke]);
impl_actions!(terminal_view, [SendText, SendKeystroke]);
pub fn init(cx: &mut AppContext) {
terminal_panel::init(cx);
@ -71,35 +65,37 @@ pub fn init(cx: &mut AppContext) {
register_deserializable_item::<TerminalView>(cx);
cx.add_action(TerminalView::deploy);
//Useful terminal views
cx.add_action(TerminalView::send_text);
cx.add_action(TerminalView::send_keystroke);
cx.add_action(TerminalView::copy);
cx.add_action(TerminalView::paste);
cx.add_action(TerminalView::clear);
cx.add_action(TerminalView::show_character_palette);
cx.add_action(TerminalView::select_all)
cx.observe_new_views(|workspace: &mut Workspace, _| {
workspace.register_action(TerminalView::deploy);
})
.detach();
}
///A terminal view, maintains the PTY's file handles and communicates with the terminal
pub struct TerminalView {
terminal: ModelHandle<Terminal>,
terminal: Model<Terminal>,
focus_handle: FocusHandle,
has_new_content: bool,
//Currently using iTerm bell, show bell emoji in tab until input is received
has_bell: bool,
context_menu: ViewHandle<ContextMenu>,
context_menu: Option<(View<ContextMenu>, gpui::Point<Pixels>, Subscription)>,
blink_state: bool,
blinking_on: bool,
blinking_paused: bool,
blink_epoch: usize,
can_navigate_to_selected_word: bool,
workspace_id: WorkspaceId,
_subscriptions: Vec<Subscription>,
}
impl Entity for TerminalView {
type Event = Event;
impl EventEmitter<Event> for TerminalView {}
impl EventEmitter<ItemEvent> for TerminalView {}
impl EventEmitter<SearchEvent> for TerminalView {}
impl FocusableView for TerminalView {
fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
self.focus_handle.clone()
}
}
impl TerminalView {
@ -109,11 +105,11 @@ impl TerminalView {
_: &NewCenterTerminal,
cx: &mut ViewContext<Workspace>,
) {
let strategy = settings::get::<TerminalSettings>(cx);
let strategy = TerminalSettings::get_global(cx);
let working_directory =
get_working_directory(workspace, cx, strategy.working_directory.clone());
let window = cx.window();
let window = cx.window_handle();
let terminal = workspace
.project()
.update(cx, |project, cx| {
@ -122,7 +118,7 @@ impl TerminalView {
.notify_err(workspace, cx);
if let Some(terminal) = terminal {
let view = cx.add_view(|cx| {
let view = cx.new_view(|cx| {
TerminalView::new(
terminal,
workspace.weak_handle(),
@ -135,20 +131,21 @@ impl TerminalView {
}
pub fn new(
terminal: ModelHandle<Terminal>,
workspace: WeakViewHandle<Workspace>,
terminal: Model<Terminal>,
workspace: WeakView<Workspace>,
workspace_id: WorkspaceId,
cx: &mut ViewContext<Self>,
) -> Self {
let view_id = cx.view_id();
cx.observe(&terminal, |_, _, cx| cx.notify()).detach();
cx.subscribe(&terminal, move |this, _, event, cx| match event {
Event::Wakeup => {
if !cx.is_self_focused() {
if !this.focus_handle.is_focused(cx) {
this.has_new_content = true;
}
cx.notify();
cx.emit(Event::Wakeup);
cx.emit(ItemEvent::UpdateTab);
cx.emit(SearchEvent::MatchesInvalidated);
}
Event::Bell => {
@ -159,15 +156,16 @@ impl TerminalView {
Event::BlinkChanged => this.blinking_on = !this.blinking_on,
Event::TitleChanged => {
cx.emit(ItemEvent::UpdateTab);
if let Some(foreground_info) = &this.terminal().read(cx).foreground_process_info {
let cwd = foreground_info.cwd.clone();
let item_id = cx.view_id();
let item_id = cx.entity_id();
let workspace_id = this.workspace_id;
cx.background()
cx.background_executor()
.spawn(async move {
TERMINAL_DB
.save_working_directory(item_id, workspace_id, cwd)
.save_working_directory(item_id.as_u64(), workspace_id, cwd)
.await
.log_err();
})
@ -186,7 +184,7 @@ impl TerminalView {
}
Event::Open(maybe_navigation_target) => match maybe_navigation_target {
MaybeNavigationTarget::Url(url) => cx.platform().open_url(url),
MaybeNavigationTarget::Url(url) => cx.open_url(url),
MaybeNavigationTarget::PathLike(maybe_path) => {
if !this.can_navigate_to_selected_word {
@ -252,26 +250,37 @@ impl TerminalView {
}
}
},
_ => cx.emit(event.clone()),
Event::BreadcrumbsChanged => cx.emit(ItemEvent::UpdateBreadcrumbs),
Event::CloseTerminal => cx.emit(ItemEvent::CloseItem),
Event::SelectionsChanged => cx.emit(SearchEvent::ActiveMatchChanged),
})
.detach();
let focus_handle = cx.focus_handle();
let focus_in = cx.on_focus_in(&focus_handle, |terminal_view, cx| {
terminal_view.focus_in(cx);
});
let focus_out = cx.on_focus_out(&focus_handle, |terminal_view, cx| {
terminal_view.focus_out(cx);
});
Self {
terminal,
has_new_content: true,
has_bell: false,
context_menu: cx.add_view(|cx| ContextMenu::new(view_id, cx)),
focus_handle: cx.focus_handle(),
context_menu: None,
blink_state: true,
blinking_on: false,
blinking_paused: false,
blink_epoch: 0,
can_navigate_to_selected_word: false,
workspace_id,
_subscriptions: vec![focus_in, focus_out],
}
}
pub fn model(&self) -> &ModelHandle<Terminal> {
pub fn model(&self) -> &Model<Terminal> {
&self.terminal
}
@ -288,17 +297,29 @@ impl TerminalView {
cx.emit(Event::Wakeup);
}
pub fn deploy_context_menu(&mut self, position: Vector2F, cx: &mut ViewContext<Self>) {
let menu_entries = vec![
ContextMenuItem::action("Clear", Clear),
ContextMenuItem::action("Close", pane::CloseActiveItem { save_intent: None }),
];
self.context_menu.update(cx, |menu, cx| {
menu.show(position, AnchorCorner::TopLeft, menu_entries, cx)
pub fn deploy_context_menu(
&mut self,
position: gpui::Point<Pixels>,
cx: &mut ViewContext<Self>,
) {
let context_menu = ContextMenu::build(cx, |menu, _| {
menu.action("Clear", Box::new(Clear))
.action("Close", Box::new(CloseActiveItem { save_intent: None }))
});
cx.notify();
cx.focus_view(&context_menu);
let subscription =
cx.subscribe(&context_menu, |this, _, _: &DismissEvent, cx| {
if this.context_menu.as_ref().is_some_and(|context_menu| {
context_menu.0.focus_handle(cx).contains_focused(cx)
}) {
cx.focus_self();
}
this.context_menu.take();
cx.notify();
});
self.context_menu = Some((context_menu, position, subscription));
}
fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext<Self>) {
@ -314,7 +335,7 @@ impl TerminalView {
self.terminal.update(cx, |term, cx| {
term.try_keystroke(
&Keystroke::parse("ctrl-cmd-space").unwrap(),
settings::get::<TerminalSettings>(cx).option_as_meta,
TerminalSettings::get_global(cx).option_as_meta,
)
});
}
@ -345,7 +366,7 @@ impl TerminalView {
return true;
}
match settings::get::<TerminalSettings>(cx).blinking {
match TerminalSettings::get_global(cx).blinking {
//If the user requested to never blink, don't blink it.
TerminalBlink::Off => true,
//If the terminal is controlling it, check terminal mode
@ -392,11 +413,11 @@ impl TerminalView {
self.terminal
.update(cx, |term, cx| term.find_matches(searcher, cx))
} else {
cx.background().spawn(async { Vec::new() })
cx.background_executor().spawn(async { Vec::new() })
}
}
pub fn terminal(&self) -> &ModelHandle<Terminal> {
pub fn terminal(&self) -> &Model<Terminal> {
&self.terminal
}
@ -436,19 +457,91 @@ impl TerminalView {
if let Some(keystroke) = Keystroke::parse(&text.0).log_err() {
self.clear_bel(cx);
self.terminal.update(cx, |term, cx| {
term.try_keystroke(
&keystroke,
settings::get::<TerminalSettings>(cx).option_as_meta,
);
term.try_keystroke(&keystroke, TerminalSettings::get_global(cx).option_as_meta);
});
}
}
fn dispatch_context(&self, cx: &AppContext) -> KeyContext {
let mut dispatch_context = KeyContext::default();
dispatch_context.add("Terminal");
let mode = self.terminal.read(cx).last_content.mode;
dispatch_context.set(
"screen",
if mode.contains(TermMode::ALT_SCREEN) {
"alt"
} else {
"normal"
},
);
if mode.contains(TermMode::APP_CURSOR) {
dispatch_context.add("DECCKM");
}
if mode.contains(TermMode::APP_KEYPAD) {
dispatch_context.add("DECPAM");
} else {
dispatch_context.add("DECPNM");
}
if mode.contains(TermMode::SHOW_CURSOR) {
dispatch_context.add("DECTCEM");
}
if mode.contains(TermMode::LINE_WRAP) {
dispatch_context.add("DECAWM");
}
if mode.contains(TermMode::ORIGIN) {
dispatch_context.add("DECOM");
}
if mode.contains(TermMode::INSERT) {
dispatch_context.add("IRM");
}
//LNM is apparently the name for this. https://vt100.net/docs/vt510-rm/LNM.html
if mode.contains(TermMode::LINE_FEED_NEW_LINE) {
dispatch_context.add("LNM");
}
if mode.contains(TermMode::FOCUS_IN_OUT) {
dispatch_context.add("report_focus");
}
if mode.contains(TermMode::ALTERNATE_SCROLL) {
dispatch_context.add("alternate_scroll");
}
if mode.contains(TermMode::BRACKETED_PASTE) {
dispatch_context.add("bracketed_paste");
}
if mode.intersects(TermMode::MOUSE_MODE) {
dispatch_context.add("any_mouse_reporting");
}
{
let mouse_reporting = if mode.contains(TermMode::MOUSE_REPORT_CLICK) {
"click"
} else if mode.contains(TermMode::MOUSE_DRAG) {
"drag"
} else if mode.contains(TermMode::MOUSE_MOTION) {
"motion"
} else {
"off"
};
dispatch_context.set("mouse_reporting", mouse_reporting);
}
{
let format = if mode.contains(TermMode::SGR_MOUSE) {
"sgr"
} else if mode.contains(TermMode::UTF8_MOUSE) {
"utf8"
} else {
"normal"
};
dispatch_context.set("mouse_format", format);
};
dispatch_context
}
}
fn possible_open_targets(
workspace: &WeakViewHandle<Workspace>,
workspace: &WeakView<Workspace>,
maybe_path: &String,
cx: &mut ViewContext<'_, '_, TerminalView>,
cx: &mut ViewContext<'_, TerminalView>,
) -> Vec<PathLikeWithPosition<PathBuf>> {
let path_like = PathLikeWithPosition::parse_str(maybe_path.as_str(), |path_str| {
Ok::<_, std::convert::Infallible>(Path::new(path_str).to_path_buf())
@ -467,7 +560,7 @@ fn possible_open_targets(
} else {
Vec::new()
}
} else if let Some(workspace) = workspace.upgrade(cx) {
} else if let Some(workspace) = workspace.upgrade() {
workspace.update(cx, |workspace, cx| {
workspace
.worktrees(cx)
@ -495,198 +588,102 @@ pub fn regex_search_for_query(query: &project::search::SearchQuery) -> Option<Re
searcher.ok()
}
impl View for TerminalView {
fn ui_name() -> &'static str {
"Terminal"
}
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> AnyElement<Self> {
let terminal_handle = self.terminal.clone().downgrade();
let self_id = cx.view_id();
let focused = cx
.focused_view_id()
.filter(|view_id| *view_id == self_id)
.is_some();
Stack::new()
.with_child(
TerminalElement::new(
terminal_handle,
focused,
self.should_show_cursor(focused, cx),
self.can_navigate_to_selected_word,
)
.contained(),
)
.with_child(ChildView::new(&self.context_menu, cx))
.into_any()
}
fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
self.has_new_content = false;
self.terminal.read(cx).focus_in();
self.blink_cursors(self.blink_epoch, cx);
cx.notify();
}
fn focus_out(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
self.terminal.update(cx, |terminal, _| {
terminal.focus_out();
});
cx.notify();
}
fn modifiers_changed(
&mut self,
event: &ModifiersChangedEvent,
cx: &mut ViewContext<Self>,
) -> bool {
let handled = self
.terminal()
.update(cx, |term, _| term.try_modifiers_change(&event.modifiers));
if handled {
cx.notify();
}
handled
}
fn key_down(&mut self, event: &KeyDownEvent, cx: &mut ViewContext<Self>) -> bool {
impl TerminalView {
fn key_down(&mut self, event: &KeyDownEvent, cx: &mut ViewContext<Self>) {
self.clear_bel(cx);
self.pause_cursor_blinking(cx);
self.terminal.update(cx, |term, cx| {
term.try_keystroke(
&event.keystroke,
settings::get::<TerminalSettings>(cx).option_as_meta,
TerminalSettings::get_global(cx).option_as_meta,
)
})
}
//IME stuff
fn selected_text_range(&self, cx: &AppContext) -> Option<std::ops::Range<usize>> {
if self
.terminal
.read(cx)
.last_content
.mode
.contains(TermMode::ALT_SCREEN)
{
None
} else {
Some(0..0)
}
}
fn replace_text_in_range(
&mut self,
_: Option<std::ops::Range<usize>>,
text: &str,
cx: &mut ViewContext<Self>,
) {
self.terminal.update(cx, |terminal, _| {
terminal.input(text.into());
});
}
fn update_keymap_context(&self, keymap: &mut KeymapContext, cx: &gpui::AppContext) {
Self::reset_to_default_keymap_context(keymap);
fn focus_in(&mut self, cx: &mut ViewContext<Self>) {
self.has_new_content = false;
self.terminal.read(cx).focus_in();
self.blink_cursors(self.blink_epoch, cx);
cx.notify();
}
let mode = self.terminal.read(cx).last_content.mode;
keymap.add_key(
"screen",
if mode.contains(TermMode::ALT_SCREEN) {
"alt"
} else {
"normal"
},
);
fn focus_out(&mut self, cx: &mut ViewContext<Self>) {
self.terminal.update(cx, |terminal, _| {
terminal.focus_out();
});
cx.notify();
}
}
if mode.contains(TermMode::APP_CURSOR) {
keymap.add_identifier("DECCKM");
}
if mode.contains(TermMode::APP_KEYPAD) {
keymap.add_identifier("DECPAM");
} else {
keymap.add_identifier("DECPNM");
}
if mode.contains(TermMode::SHOW_CURSOR) {
keymap.add_identifier("DECTCEM");
}
if mode.contains(TermMode::LINE_WRAP) {
keymap.add_identifier("DECAWM");
}
if mode.contains(TermMode::ORIGIN) {
keymap.add_identifier("DECOM");
}
if mode.contains(TermMode::INSERT) {
keymap.add_identifier("IRM");
}
//LNM is apparently the name for this. https://vt100.net/docs/vt510-rm/LNM.html
if mode.contains(TermMode::LINE_FEED_NEW_LINE) {
keymap.add_identifier("LNM");
}
if mode.contains(TermMode::FOCUS_IN_OUT) {
keymap.add_identifier("report_focus");
}
if mode.contains(TermMode::ALTERNATE_SCROLL) {
keymap.add_identifier("alternate_scroll");
}
if mode.contains(TermMode::BRACKETED_PASTE) {
keymap.add_identifier("bracketed_paste");
}
if mode.intersects(TermMode::MOUSE_MODE) {
keymap.add_identifier("any_mouse_reporting");
}
{
let mouse_reporting = if mode.contains(TermMode::MOUSE_REPORT_CLICK) {
"click"
} else if mode.contains(TermMode::MOUSE_DRAG) {
"drag"
} else if mode.contains(TermMode::MOUSE_MOTION) {
"motion"
} else {
"off"
};
keymap.add_key("mouse_reporting", mouse_reporting);
}
{
let format = if mode.contains(TermMode::SGR_MOUSE) {
"sgr"
} else if mode.contains(TermMode::UTF8_MOUSE) {
"utf8"
} else {
"normal"
};
keymap.add_key("mouse_format", format);
}
impl Render for TerminalView {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let terminal_handle = self.terminal.clone();
let focused = self.focus_handle.is_focused(cx);
div()
.size_full()
.relative()
.track_focus(&self.focus_handle)
.key_context(self.dispatch_context(cx))
.on_action(cx.listener(TerminalView::send_text))
.on_action(cx.listener(TerminalView::send_keystroke))
.on_action(cx.listener(TerminalView::copy))
.on_action(cx.listener(TerminalView::paste))
.on_action(cx.listener(TerminalView::clear))
.on_action(cx.listener(TerminalView::show_character_palette))
.on_action(cx.listener(TerminalView::select_all))
.on_key_down(cx.listener(Self::key_down))
.on_mouse_down(
MouseButton::Right,
cx.listener(|this, event: &MouseDownEvent, cx| {
this.deploy_context_menu(event.position, cx);
cx.notify();
}),
)
.child(
// TODO: Oddly this wrapper div is needed for TerminalElement to not steal events from the context menu
div().size_full().child(TerminalElement::new(
terminal_handle,
self.focus_handle.clone(),
focused,
self.should_show_cursor(focused, cx),
self.can_navigate_to_selected_word,
)),
)
.children(self.context_menu.as_ref().map(|(menu, positon, _)| {
overlay()
.position(*positon)
.anchor(gpui::AnchorCorner::TopLeft)
.child(menu.clone())
}))
}
}
impl Item for TerminalView {
fn tab_tooltip_text(&self, cx: &AppContext) -> Option<Cow<str>> {
type Event = ItemEvent;
fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString> {
Some(self.terminal().read(cx).title().into())
}
fn tab_content<T: 'static>(
fn tab_content(
&self,
_detail: Option<usize>,
tab_theme: &theme::Tab,
cx: &gpui::AppContext,
) -> AnyElement<T> {
selected: bool,
cx: &WindowContext,
) -> AnyElement {
let title = self.terminal().read(cx).title();
Flex::row()
.with_child(
gpui::elements::Svg::new("icons/terminal.svg")
.with_color(tab_theme.label.text.color)
.constrained()
.with_width(tab_theme.type_icon_width)
.aligned()
.contained()
.with_margin_right(tab_theme.spacing),
)
.with_child(Label::new(title, tab_theme.label.clone()).aligned())
h_stack()
.gap_2()
.child(IconElement::new(Icon::Terminal))
.child(Label::new(title).color(if selected {
Color::Default
} else {
Color::Muted
}))
.into_any()
}
@ -694,7 +691,7 @@ impl Item for TerminalView {
&self,
_workspace_id: WorkspaceId,
_cx: &mut ViewContext<Self>,
) -> Option<Self> {
) -> Option<View<Self>> {
//From what I can tell, there's no way to tell the current working
//Directory of the terminal from outside the shell. There might be
//solutions to this, but they are non-trivial and require more IPC
@ -717,21 +714,13 @@ impl Item for TerminalView {
false
}
fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
Some(Box::new(handle.clone()))
}
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
match event {
Event::BreadcrumbsChanged => smallvec![ItemEvent::UpdateBreadcrumbs],
Event::TitleChanged | Event::Wakeup => smallvec![ItemEvent::UpdateTab],
Event::CloseTerminal => smallvec![ItemEvent::CloseItem],
_ => smallvec![],
}
}
// todo!(search)
// fn as_searchable(&self, handle: &View<Self>) -> Option<Box<dyn SearchableItemHandle>> {
// Some(Box::new(handle.clone()))
// }
fn breadcrumb_location(&self) -> ToolbarItemLocation {
ToolbarItemLocation::PrimaryLeft { flex: None }
ToolbarItemLocation::PrimaryLeft
}
fn breadcrumbs(&self, _: &theme::Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
@ -746,51 +735,55 @@ impl Item for TerminalView {
}
fn deserialize(
project: ModelHandle<Project>,
workspace: WeakViewHandle<Workspace>,
project: Model<Project>,
workspace: WeakView<Workspace>,
workspace_id: workspace::WorkspaceId,
item_id: workspace::ItemId,
cx: &mut ViewContext<Pane>,
) -> Task<anyhow::Result<ViewHandle<Self>>> {
let window = cx.window();
) -> Task<anyhow::Result<View<Self>>> {
let window = cx.window_handle();
cx.spawn(|pane, mut cx| async move {
let cwd = TERMINAL_DB
.get_working_directory(item_id, workspace_id)
.log_err()
.flatten()
.or_else(|| {
cx.read(|cx| {
let strategy = settings::get::<TerminalSettings>(cx)
.working_directory
.clone();
cx.update(|_, cx| {
let strategy = TerminalSettings::get_global(cx).working_directory.clone();
workspace
.upgrade(cx)
.upgrade()
.map(|workspace| {
get_working_directory(workspace.read(cx), cx, strategy)
})
.flatten()
})
.ok()
.flatten()
});
let terminal = project.update(&mut cx, |project, cx| {
project.create_terminal(cwd, window, cx)
})?;
Ok(pane.update(&mut cx, |_, cx| {
cx.add_view(|cx| TerminalView::new(terminal, workspace, workspace_id, cx))
})?)
})??;
pane.update(&mut cx, |_, cx| {
cx.new_view(|cx| TerminalView::new(terminal, workspace, workspace_id, cx))
})
})
}
fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
cx.background()
cx.background_executor()
.spawn(TERMINAL_DB.update_workspace_id(
workspace.database_id(),
self.workspace_id,
cx.view_id(),
cx.entity_id().as_u64(),
))
.detach();
self.workspace_id = workspace.database_id();
}
fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
f(*event)
}
}
impl SearchableItem for TerminalView {
@ -805,19 +798,6 @@ impl SearchableItem for TerminalView {
}
}
/// Convert events raised by this item into search-relevant events (if applicable)
fn to_search_event(
&mut self,
event: &Self::Event,
_: &mut ViewContext<Self>,
) -> Option<SearchEvent> {
match event {
Event::Wakeup => Some(SearchEvent::MatchesInvalidated),
Event::SelectionsChanged => Some(SearchEvent::ActiveMatchChanged),
_ => None,
}
}
/// Clear stored matches
fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
self.terminal().update(cx, |term, _| term.matches.clear())
@ -1074,12 +1054,10 @@ mod tests {
}
/// Creates a worktree with 1 file: /root.txt
pub async fn init_test(
cx: &mut TestAppContext,
) -> (ModelHandle<Project>, ViewHandle<Workspace>) {
pub async fn init_test(cx: &mut TestAppContext) -> (Model<Project>, View<Workspace>) {
let params = cx.update(AppState::test);
cx.update(|cx| {
theme::init((), cx);
theme::init(theme::LoadThemes::JustBase, cx);
Project::init_settings(cx);
language::init(cx);
});
@ -1087,35 +1065,36 @@ mod tests {
let project = Project::test(params.fs.clone(), [], cx).await;
let workspace = cx
.add_window(|cx| Workspace::test_new(project.clone(), cx))
.root(cx);
.root_view(cx)
.unwrap();
(project, workspace)
}
/// Creates a worktree with 1 folder: /root{suffix}/
async fn create_folder_wt(
project: ModelHandle<Project>,
project: Model<Project>,
path: impl AsRef<Path>,
cx: &mut TestAppContext,
) -> (ModelHandle<Worktree>, Entry) {
) -> (Model<Worktree>, Entry) {
create_wt(project, true, path, cx).await
}
/// Creates a worktree with 1 file: /root{suffix}.txt
async fn create_file_wt(
project: ModelHandle<Project>,
project: Model<Project>,
path: impl AsRef<Path>,
cx: &mut TestAppContext,
) -> (ModelHandle<Worktree>, Entry) {
) -> (Model<Worktree>, Entry) {
create_wt(project, false, path, cx).await
}
async fn create_wt(
project: ModelHandle<Project>,
project: Model<Project>,
is_dir: bool,
path: impl AsRef<Path>,
cx: &mut TestAppContext,
) -> (ModelHandle<Worktree>, Entry) {
) -> (Model<Worktree>, Entry) {
let (wt, _) = project
.update(cx, |project, cx| {
project.find_or_create_local_worktree(path, true, cx)
@ -1139,9 +1118,9 @@ mod tests {
}
pub fn insert_active_entry_for(
wt: ModelHandle<Worktree>,
wt: Model<Worktree>,
entry: Entry,
project: ModelHandle<Project>,
project: Model<Project>,
cx: &mut TestAppContext,
) {
cx.update(|cx| {