diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index f20e7af0e33..3c6d585d17f 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -807,6 +807,7 @@ impl Item for Editor { h_flex() .gap_1() + .when(!params.selected, |tab| tab.track_focus(&self.focus_handle)) .when(params.truncate_title_middle, |this| { this.w_full().min_w_0().overflow_hidden() }) @@ -1216,17 +1217,6 @@ impl Item for Editor { _window: &mut Window, cx: &mut Context, ) -> Vec<(SharedString, Box)> { - let editor_id = cx.entity_id(); - // Inactive tabs aren't rendered, so the preview actions can't be dispatched to them. - let is_active_in_pane = self - .workspace() - .and_then(|workspace| workspace.read(cx).pane_for_item_id(editor_id)) - .and_then(|pane| pane.read(cx).active_item()) - .is_some_and(|item| item.item_id() == editor_id); - if !is_active_in_pane { - return Vec::new(); - } - let mut actions = Vec::new(); let is_markdown = self diff --git a/crates/markdown_preview/src/markdown_preview_view.rs b/crates/markdown_preview/src/markdown_preview_view.rs index 28b31cb7222..517151007a7 100644 --- a/crates/markdown_preview/src/markdown_preview_view.rs +++ b/crates/markdown_preview/src/markdown_preview_view.rs @@ -14,10 +14,10 @@ use editor::{ Editor, EditorEvent, EditorSettingsScrollbarProxy, MultiBufferOffset, SelectionEffects, }; use gpui::{ - Action, App, ClipboardItem, Context, Entity, EntityId, EventEmitter, FocusHandle, Focusable, - Global, ImageSource, InteractiveElement, IntoElement, IsZero, Pixels, Render, Resource, - RetainAllImageCache, ScrollHandle, SharedString, SharedUri, Subscription, Task, WeakEntity, - Window, point, px, + Action, AnyElement, App, ClipboardItem, Context, Entity, EntityId, EventEmitter, FocusHandle, + Focusable, Global, ImageSource, InteractiveElement, IntoElement, IsZero, Pixels, Render, + Resource, RetainAllImageCache, ScrollHandle, SharedString, SharedUri, Subscription, Task, + WeakEntity, Window, point, px, }; use language::{Buffer, LanguageRegistry}; use markdown::{ @@ -39,7 +39,9 @@ use util::{ paths::{PathStyle, PathWithPosition}, rel_path::RelPath, }; -use workspace::item::{Item, ItemBufferKind, ItemHandle, SaveOptions, SerializableItem}; +use workspace::item::{ + Item, ItemBufferKind, ItemHandle, SaveOptions, SerializableItem, TabContentParams, +}; use workspace::notifications::{NotifyResultExt, NotifyTaskExt}; use workspace::path_link::{PathMatching, resolve_open_target}; use workspace::searchable::{ @@ -65,6 +67,7 @@ fn reset_persisted_font_size(settings: &mut settings::SettingsContent) { pub struct MarkdownPreviewView { workspace: WeakEntity, + self_handle: WeakEntity, active_editor: Option, focus_handle: FocusHandle, markdown: Entity, @@ -191,8 +194,11 @@ impl MarkdownPreviewView { .detach(); workspace.register_action(move |workspace, _: &OpenPreview, window, cx| { - if let Some(editor) = Self::resolve_active_item_as_markdown_editor(workspace, cx) { - let pane = workspace.active_pane().clone(); + if let Some(item) = workspace.item_for_action(window, cx) + && let Some(editor) = item.act_as::(cx) + && Self::is_markdown_file(&editor, cx) + && let Some(pane) = workspace.pane_for_item_id(item.item_id()) + { Self::open_preview_in_pane(workspace, editor, pane, window, cx); } }); @@ -397,6 +403,7 @@ impl MarkdownPreviewView { active_editor: None, focus_handle: cx.focus_handle(), workspace: workspace.clone(), + self_handle: cx.weak_entity(), _markdown_subscription: cx.observe( &markdown, |this: &mut Self, markdown: Entity, cx| { @@ -1023,20 +1030,29 @@ impl MarkdownPreviewView { window.defer(cx, move |window, cx| { workspace.update(cx, |workspace, cx| { + let Some(pane) = workspace.pane_for_item_id(preview_id) else { + return; + }; if !workspace.activate_item(&editor, true, true, window, cx) { // Re-adding the source emits `ItemAdded`; suppress its automatic preview once. cx.global_mut::() .0 .insert(editor.entity_id()); - workspace.add_item_to_active_pane(Box::new(editor), None, true, window, cx); + workspace.add_item( + pane.clone(), + Box::new(editor), + None, + true, + true, + window, + cx, + ); } - if let Some(pane) = workspace.pane_for_item_id(preview_id) { - pane.update(cx, |pane, cx| { - pane.close_item_by_id(preview_id, SaveIntent::Skip, window, cx) - }) - .detach_and_log_err(cx); - } + pane.update(cx, |pane, cx| { + pane.close_item_by_id(preview_id, SaveIntent::Skip, window, cx) + }) + .detach_and_log_err(cx); }); }); } @@ -1630,6 +1646,25 @@ impl Item for MarkdownPreviewView { Some(Icon::new(IconName::FileDoc)) } + fn tab_content(&self, params: TabContentParams, _window: &Window, cx: &App) -> AnyElement { + let preview = self.self_handle.clone(); + h_flex() + .when(!params.selected, |tab| tab.track_focus(&self.focus_handle)) + .on_action(move |action: &CloseAndReturnToEditor, window, cx| { + preview + .update(cx, |preview, cx| { + preview.close_and_return_to_editor(action, window, cx); + }) + .ok(); + }) + .child( + Label::new(self.tab_content_text(params.detail.unwrap_or_default(), cx)) + .single_line() + .color(params.text_color()), + ) + .into_any_element() + } + fn tab_content_text(&self, _detail: usize, cx: &App) -> SharedString { self.active_editor .as_ref() @@ -1655,20 +1690,9 @@ impl Item for MarkdownPreviewView { fn tab_extra_context_menu_actions( &self, - _window: &mut Window, - cx: &mut Context, + _: &mut Window, + _: &mut Context, ) -> Vec<(SharedString, Box)> { - let preview_id = cx.entity_id(); - // Inactive tabs aren't rendered, so the action can't be dispatched to them. - let is_active_in_pane = self - .workspace - .upgrade() - .and_then(|workspace| workspace.read(cx).pane_for_item_id(preview_id)) - .and_then(|pane| pane.read(cx).active_item()) - .is_some_and(|item| item.item_id() == preview_id); - if !is_active_in_pane { - return Vec::new(); - } vec![("Show Source".into(), Box::new(CloseAndReturnToEditor))] } @@ -2230,7 +2254,8 @@ mod tests { use fs::FakeFs; use gpui::UpdateGlobal as _; use gpui::{ - App, AppContext as _, Entity, Focusable as _, Modifiers, TestAppContext, WindowHandle, px, + App, AppContext as _, Entity, Focusable as _, Modifiers, MouseButton, MouseDownEvent, + MouseUpEvent, TestAppContext, VisualTestContext, WindowHandle, px, }; use language::{Buffer, DiskState, Point}; use project::{Project, ProjectPath}; @@ -3173,87 +3198,101 @@ mod tests { } #[gpui::test] - async fn tab_context_menu_offers_show_source(cx: &mut TestAppContext) { - let (multi_workspace, _editor) = - open_markdown_file(cx, "note.md", "# Note\n\nBody text\n").await; - let preview = open_preview_for_active_editor(cx, &multi_workspace); - cx.run_until_parked(); + async fn double_click_editor_and_preview_tabs(cx: &mut TestAppContext) { + let (multi_workspace, _) = open_markdown_file(cx, "note.md", "# Note\n").await; + let cx = &mut VisualTestContext::from_window(multi_workspace.into(), cx); + click_tab_menu_target(cx, "TAB-0", MouseButton::Left, 2); + let preview = open_preview_for_active_editor(cx, &multi_workspace); + click_tab_menu_target(cx, "TAB-1", MouseButton::Left, 2); multi_workspace + .update(cx, |multi_workspace, _, cx| { + let pane = multi_workspace.workspace().read(cx).active_pane().read(cx); + assert_eq!(pane.active_item().unwrap().item_id(), preview.entity_id()); + assert_eq!(pane.preview_item_id(), None); + }) + .unwrap(); + } + + #[gpui::test] + async fn tab_context_menu_targets_inactive_items(cx: &mut TestAppContext) { + let (project, workspace, multi_workspace) = markdown_workspace( + cx, + json!({ "plain.txt": "Plain", "note.md": "# Note", "other.md": "# Other" }), + false, + ) + .await; + open_project_file(cx, &project, &multi_workspace, "plain.txt", None, true).await; + let editor = open_project_file(cx, &project, &multi_workspace, "note.md", None, true) + .await + .downcast::() + .unwrap(); + let pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone()); + let other_pane = multi_workspace .update(cx, |_, window, cx| { - let actions = preview.update(cx, |preview, cx| { - preview.tab_extra_context_menu_actions(window, cx) - }); - let [(label, action)] = actions.as_slice() else { - panic!("expected exactly one tab menu action"); - }; - assert_eq!(label.as_ref(), "Show Source"); - assert!(action.as_any().is::()); - }) - .unwrap(); - } - - #[gpui::test] - async fn tab_context_menu_hides_show_source_for_inactive_preview(cx: &mut TestAppContext) { - let (multi_workspace, editor) = - open_markdown_file(cx, "note.md", "# Note\n\nBody text\n").await; - let preview = open_preview_for_active_editor(cx, &multi_workspace); - cx.run_until_parked(); - - multi_workspace - .update(cx, |multi_workspace, window, cx| { - let workspace = multi_workspace.workspace().clone(); workspace.update(cx, |workspace, cx| { - assert!(workspace.activate_item(&editor, true, true, window, cx)); - }); - let actions = preview.update(cx, |preview, cx| { - preview.tab_extra_context_menu_actions(window, cx) - }); - assert!(actions.is_empty()); + workspace.split_pane(pane.clone(), workspace::SplitDirection::Right, window, cx) + }) }) .unwrap(); - } - - #[gpui::test] - async fn tab_context_menu_offers_open_preview_only_for_active_editor(cx: &mut TestAppContext) { - let (multi_workspace, editor) = - open_markdown_file(cx, "note.md", "# Note\n\nBody text\n").await; - editor.update(cx, |editor, cx| { - let buffer = editor.buffer().read(cx).as_singleton().unwrap(); - buffer.update(cx, |buffer, cx| { - buffer.set_language( - Some(Arc::new(language::Language::new( - markdown_language_config(), - None, - ))), - cx, - ); + let other_editor = open_project_file( + cx, + &project, + &multi_workspace, + "other.md", + Some(&other_pane), + true, + ) + .await + .downcast::() + .unwrap(); + let cx = &mut VisualTestContext::from_window(multi_workspace.into(), cx); + pane.update_in(cx, |pane, window, cx| { + assert_eq!(pane.index_for_item(&editor), Some(1)); + pane.activate_item(0, false, false, window, cx); + }); + click_tab_menu_target(cx, "TAB-1", MouseButton::Right, 1); + click_tab_menu_target(cx, "MENU_ITEM-Open Markdown Preview", MouseButton::Left, 1); + cx.run_until_parked(); + let preview = cx.update(|_, cx| { + assert_eq!(workspace.read(cx).active_pane(), &pane); + let preview = pane + .read(cx) + .active_item() + .unwrap() + .downcast::() + .unwrap(); + assert_eq!( + preview.read(cx).active_editor.as_ref().unwrap().editor, + editor + ); + preview + }); + cx.update(|window, cx| { + pane.update(cx, |pane, cx| { + pane.remove_item(editor.entity_id(), false, false, window, cx); + assert_eq!(pane.index_for_item(&preview), Some(1)); + pane.activate_item(0, false, false, window, cx); + }); + workspace.update(cx, |workspace, cx| { + assert!(workspace.activate_item(&other_editor, true, true, window, cx)); }); }); - - multi_workspace - .update(cx, |_, window, cx| { - let actions = editor.update(cx, |editor, cx| { - editor.tab_extra_context_menu_actions(window, cx) - }); - let [(label, _)] = actions.as_slice() else { - panic!("expected exactly one tab menu action"); - }; - assert_eq!(label.as_ref(), "Open Markdown Preview"); - }) - .unwrap(); - - open_preview_for_active_editor(cx, &multi_workspace); + click_tab_menu_target(cx, "TAB-1", MouseButton::Right, 1); + click_tab_menu_target(cx, "MENU_ITEM-Show Source", MouseButton::Left, 1); cx.run_until_parked(); - - multi_workspace - .update(cx, |_, window, cx| { - let actions = editor.update(cx, |editor, cx| { - editor.tab_extra_context_menu_actions(window, cx) - }); - assert!(actions.is_empty()); - }) - .unwrap(); + assert_editor_is_active_and_focused(cx, &multi_workspace, &editor); + assert_no_markdown_preview_items(cx, &multi_workspace); + cx.update(|_, cx| { + assert_eq!( + workspace.read(cx).pane_for_item_id(editor.entity_id()), + Some(pane) + ); + assert_eq!( + other_pane.read(cx).active_item().unwrap().item_id(), + other_editor.entity_id() + ); + }); } #[gpui::test] @@ -4485,6 +4524,32 @@ mod tests { }) } + fn click_tab_menu_target( + cx: &mut VisualTestContext, + selector: &'static str, + button: MouseButton, + click_count: usize, + ) { + cx.run_until_parked(); + let position = cx + .debug_bounds(selector) + .unwrap_or_else(|| panic!("missing click target: {selector}")) + .center(); + cx.simulate_event(MouseDownEvent { + position, + button, + modifiers: Modifiers::default(), + click_count, + first_mouse: false, + }); + cx.simulate_event(MouseUpEvent { + position, + button, + modifiers: Modifiers::default(), + click_count, + }); + } + fn markdown_fixture_directory(tree: &TempTree) -> PathBuf { tree.path().join("docs") } diff --git a/crates/svg_preview/src/svg_preview_view.rs b/crates/svg_preview/src/svg_preview_view.rs index 44f5bc38783..8ab81b9ae9d 100644 --- a/crates/svg_preview/src/svg_preview_view.rs +++ b/crates/svg_preview/src/svg_preview_view.rs @@ -248,8 +248,11 @@ impl SvgPreviewView { pub fn register(workspace: &mut Workspace, _window: &mut Window, _cx: &mut Context) { workspace.register_action(move |workspace, _: &OpenPreview, window, cx| { - if let Some(buffer) = Self::resolve_active_item_as_svg_buffer(workspace, cx) { - let pane = workspace.active_pane().clone(); + if let Some(item) = workspace.item_for_action(window, cx) + && let Some(buffer) = item.act_as::(cx) + && Self::is_svg_file(&buffer, cx) + && let Some(pane) = workspace.pane_for_item_id(item.item_id()) + { Self::open_preview_in_pane(workspace, buffer, pane, window, cx); } }); diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 7d50ce7297f..fcf5713767d 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -4306,6 +4306,13 @@ impl Workspace { self.active_pane().read(cx).active_item() } + pub fn item_for_action(&self, window: &Window, cx: &App) -> Option> { + self.items(cx) + .find(|item| item.item_focus_handle(cx).contains_focused(window, cx)) + .map(|item| item.boxed_clone()) + .or_else(|| self.active_item(cx)) + } + pub fn active_item_as(&self, cx: &App) -> Option> { let item = self.active_item(cx)?; // Prefer an exact downcast so that we return the active item itself when