Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

terminal_view: Show terminal inline assist keybinding in tooltip (#55903)

di404be7e5b0338db
diff --git a/crates/terminal_view/src/terminal_panel.rs b/crates/terminal_view/src/terminal_panel.rs
index 25e4ad9d999..e1f31c3614c 100644
--- a/crates/terminal_view/src/terminal_panel.rs
+++ b/crates/terminal_view/src/terminal_panel.rs
@@ -11,9 +11,9 @@ use collections::HashMap;
use db::kvp::KeyValueStore;
use futures::{channel::oneshot, future::join_all};
use gpui::{
- Action, Anchor, AnyView, App, AsyncApp, AsyncWindowContext, Context, Entity, EventEmitter,
- FocusHandle, Focusable, IntoElement, ParentElement, Pixels, Render, Styled, Task, TaskExt,
- WeakEntity, Window, actions,
+ Action, Anchor, App, AsyncApp, AsyncWindowContext, Context, Entity, EventEmitter, FocusHandle,
+ Focusable, IntoElement, ParentElement, Pixels, Render, Styled, Task, TaskExt, WeakEntity,
+ Window, actions,
};
use itertools::Itertools;
use project::{Fs, Project};
@@ -83,7 +83,6 @@ pub struct TerminalPanel {
pending_terminals_to_add: usize,
deferred_tasks: HashMap<TaskId, Task<()>>,
assistant_enabled: bool,
- assistant_tab_bar_button: Option<AnyView>,
active: bool,
}
@@ -101,7 +100,6 @@ impl TerminalPanel {
pending_terminals_to_add: 0,
deferred_tasks: HashMap::default(),
assistant_enabled: false,
- assistant_tab_bar_button: None,
active: false,
};
terminal_panel.apply_tab_bar_buttons(&terminal_panel.active_pane, cx);
@@ -110,20 +108,6 @@ impl TerminalPanel {
pub fn set_assistant_enabled(&mut self, enabled: bool, cx: &mut Context<Self>) {
self.assistant_enabled = enabled;
- if enabled {
- let focus_handle = self
- .active_pane
- .read(cx)
- .active_item()
- .map(|item| item.item_focus_handle(cx))
- .unwrap_or(self.focus_handle(cx));
- self.assistant_tab_bar_button = Some(
- cx.new(move |_| InlineAssistTabBarButton { focus_handle })
- .into(),
- );
- } else {
- self.assistant_tab_bar_button = None;
- }
for pane in self.center.panes() {
self.apply_tab_bar_buttons(pane, cx);
}
@@ -134,7 +118,7 @@ impl TerminalPanel {
terminal_pane: &Entity<Pane>,
cx: &mut Context<Self>,
) {
- let assistant_tab_bar_button = self.assistant_tab_bar_button.clone();
+ let assistant_enabled = self.assistant_enabled;
terminal_pane.update(cx, |pane, cx| {
pane.set_render_tab_bar_buttons(cx, move |pane, window, cx| {
let split_context = pane
@@ -182,7 +166,11 @@ impl TerminalPanel {
Some(menu)
}),
)
- .children(assistant_tab_bar_button.clone())
+ .when(assistant_enabled, |this| {
+ this.when_some(split_context.clone(), |this, focus_handle| {
+ this.child(InlineAssistTabBarButton { focus_handle })
+ })
+ })
.child(
PopoverMenu::new("terminal-pane-tab-bar-split")
.trigger_with_tooltip(
@@ -1705,18 +1693,22 @@ impl workspace::TerminalProvider for TerminalProvider {
}
}
+#[derive(IntoElement)]
struct InlineAssistTabBarButton {
focus_handle: FocusHandle,
}
-impl Render for InlineAssistTabBarButton {
- fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
- let focus_handle = self.focus_handle.clone();
+impl RenderOnce for InlineAssistTabBarButton {
+ fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
+ let focus_handle = self.focus_handle;
IconButton::new("terminal_inline_assistant", IconName::ZedAssistant)
.icon_size(IconSize::Small)
- .on_click(cx.listener(|_, _, window, cx| {
- window.dispatch_action(InlineAssist::default().boxed_clone(), cx);
- }))
+ .on_click({
+ let focus_handle = focus_handle.clone();
+ move |_, window, cx| {
+ focus_handle.dispatch_action(&InlineAssist::default(), window, cx);
+ }
+ })
.tooltip(move |_window, cx| {
Tooltip::for_action_in("Inline Assist", &InlineAssist::default(), &focus_handle, cx)
})
@@ -1728,7 +1720,7 @@ mod tests {
use std::num::NonZero;
use super::*;
- use gpui::{TestAppContext, UpdateGlobal as _};
+ use gpui::{Modifiers, TestAppContext, UpdateGlobal as _, VisualTestContext};
use pretty_assertions::assert_eq;
use project::FakeFs;
use settings::SettingsStore;
@@ -1920,6 +1912,47 @@ mod tests {
);
}
+ #[gpui::test]
+ async fn test_inline_assist_tooltip_shows_keybinding_of_active_terminal(
+ cx: &mut TestAppContext,
+ ) {
+ cx.executor().allow_parking();
+ init_test(cx);
+
+ cx.update(|cx| {
+ cx.bind_keys([gpui::KeyBinding::new(
+ "ctrl-enter",
+ InlineAssist::default(),
+ Some("Terminal"),
+ )])
+ });
+
+ let (window_handle, terminal_panel) = init_workspace_with_panel(cx).await;
+ let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
+
+ terminal_panel.update(cx, |panel, cx| panel.set_assistant_enabled(true, cx));
+ terminal_panel
+ .update_in(cx, |panel, window, cx| {
+ panel.add_terminal_shell(None, RevealStrategy::Always, window, cx)
+ })
+ .await
+ .unwrap();
+ cx.run_until_parked();
+
+ let button_bounds = cx
+ .debug_bounds("ICON-ZedAssistant")
+ .expect("inline assist button should be rendered in the terminal tab bar");
+ cx.simulate_mouse_move(button_bounds.center(), None, Modifiers::default());
+
+ cx.executor().advance_clock(Duration::from_millis(600));
+ cx.run_until_parked();
+
+ assert!(
+ cx.debug_bounds("KEY_BINDING-enter").is_some(),
+ "tooltip should show the InlineAssist keybinding resolved in the terminal's context"
+ );
+ }
+
async fn init_workspace_with_panel(
cx: &mut TestAppContext,
) -> (gpui::WindowHandle<MultiWorkspace>, Entity<TerminalPanel>) {