From 6fc7b172599d04d3d1461b8b699bd2b875750ad5 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 3 Nov 2023 11:00:57 +0100 Subject: [PATCH] Expose a focus handle from workspace::Item --- crates/editor2/src/items.rs | 8 ++++++-- crates/workspace2/src/item.rs | 10 ++++++++-- crates/workspace2/src/pane.rs | 8 ++++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/editor2/src/items.rs b/crates/editor2/src/items.rs index b0b344a868354f79a982328116e4095be86743cc..3d5bfabaced2d95c3dc2e62dc09c68eb387a62b4 100644 --- a/crates/editor2/src/items.rs +++ b/crates/editor2/src/items.rs @@ -7,8 +7,8 @@ use anyhow::{anyhow, Context, Result}; use collections::HashSet; use futures::future::try_join_all; use gpui::{ - point, AnyElement, AppContext, AsyncAppContext, Entity, EntityId, Model, Pixels, SharedString, - Subscription, Task, View, ViewContext, VisualContext, WeakView, + point, AnyElement, AppContext, AsyncAppContext, Entity, EntityId, FocusHandle, Model, Pixels, + SharedString, Subscription, Task, View, ViewContext, VisualContext, WeakView, }; use language::{ proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point, @@ -515,6 +515,10 @@ fn deserialize_anchor(buffer: &MultiBufferSnapshot, anchor: proto::EditorAnchor) } impl Item for Editor { + fn focus_handle(&self) -> FocusHandle { + self.focus_handle.clone() + } + fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { todo!(); // if let Ok(data) = data.downcast::() { diff --git a/crates/workspace2/src/item.rs b/crates/workspace2/src/item.rs index 5d02f5d6fdcc194c0bef7fc57125d3673520d3ff..43ebab4521da35562949c5c3a88bc578d4e77e49 100644 --- a/crates/workspace2/src/item.rs +++ b/crates/workspace2/src/item.rs @@ -12,8 +12,8 @@ use client2::{ Client, }; use gpui2::{ - AnyElement, AnyView, AppContext, Entity, EntityId, EventEmitter, HighlightStyle, Model, Pixels, - Point, Render, SharedString, Task, View, ViewContext, WeakView, WindowContext, + AnyElement, AnyView, AppContext, Entity, EntityId, EventEmitter, FocusHandle, HighlightStyle, + Model, Pixels, Point, Render, SharedString, Task, View, ViewContext, WeakView, WindowContext, }; use parking_lot::Mutex; use project2::{Project, ProjectEntryId, ProjectPath}; @@ -91,6 +91,7 @@ pub struct BreadcrumbText { } pub trait Item: Render + EventEmitter { + fn focus_handle(&self) -> FocusHandle; fn deactivated(&mut self, _: &mut ViewContext) {} fn workspace_deactivated(&mut self, _: &mut ViewContext) {} fn navigate(&mut self, _: Box, _: &mut ViewContext) -> bool { @@ -212,6 +213,7 @@ pub trait Item: Render + EventEmitter { } pub trait ItemHandle: 'static + Send { + fn focus_handle(&self, cx: &WindowContext) -> FocusHandle; fn subscribe_to_item_events( &self, cx: &mut WindowContext, @@ -290,6 +292,10 @@ impl dyn ItemHandle { } impl ItemHandle for View { + fn focus_handle(&self, cx: &WindowContext) -> FocusHandle { + self.read(cx).focus_handle() + } + fn subscribe_to_item_events( &self, cx: &mut WindowContext, diff --git a/crates/workspace2/src/pane.rs b/crates/workspace2/src/pane.rs index b30ec0b7f82dba2ea98d1bae2372b87805dcc170..2754df92c32eb05e2e0720a77b6f6c4fe25202b3 100644 --- a/crates/workspace2/src/pane.rs +++ b/crates/workspace2/src/pane.rs @@ -1185,10 +1185,10 @@ impl Pane { } pub fn focus_active_item(&mut self, cx: &mut ViewContext) { - todo!(); - // if let Some(active_item) = self.active_item() { - // cx.focus(active_item.as_any()); - // } + if let Some(active_item) = self.active_item() { + let focus_handle = active_item.focus_handle(cx); + cx.focus(&focus_handle); + } } // pub fn split(&mut self, direction: SplitDirection, cx: &mut ViewContext) {