From 77d92ff65a98ea29934aa91c07c8084075a3d678 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 9 Nov 2023 20:58:35 -0700 Subject: [PATCH] Tidy up --- .../command_palette2/src/command_palette.rs | 15 ++-- crates/go_to_line2/src/go_to_line.rs | 33 ++++---- crates/gpui2/src/elements/uniform_list.rs | 26 ++++-- crates/gpui2/src/window.rs | 5 ++ crates/picker2/src/picker2.rs | 33 +++++--- crates/workspace2/src/modal_layer.rs | 84 ++++++++++++++----- 6 files changed, 134 insertions(+), 62 deletions(-) diff --git a/crates/command_palette2/src/command_palette.rs b/crates/command_palette2/src/command_palette.rs index fda3dfa8b7..6fa24b7a2e 100644 --- a/crates/command_palette2/src/command_palette.rs +++ b/crates/command_palette2/src/command_palette.rs @@ -4,7 +4,7 @@ use fuzzy::{StringMatch, StringMatchCandidate}; use gpui::{ actions, div, Action, AnyElement, AnyWindowHandle, AppContext, BorrowWindow, Component, Div, Element, EventEmitter, FocusHandle, Keystroke, ParentElement, Render, StatelessInteractive, - Styled, View, ViewContext, VisualContext, WeakView, + Styled, View, ViewContext, VisualContext, WeakView, WindowContext, }; use picker::{Picker, PickerDelegate}; use std::cmp::{self, Reverse}; @@ -14,7 +14,7 @@ use util::{ channel::{parse_zed_link, ReleaseChannel, RELEASE_CHANNEL}, ResultExt, }; -use workspace::{ModalEvent, Workspace}; +use workspace::{Modal, ModalEvent, Workspace}; use zed_actions::OpenZedURL; actions!(Toggle); @@ -24,7 +24,7 @@ pub fn init(cx: &mut AppContext) { cx.observe_new_views( |workspace: &mut Workspace, _: &mut ViewContext| { - workspace.modal_layer().register_modal(Toggle, |_, cx| { + workspace.modal_layer().register_modal(Toggle, |cx| { let Some(previous_focus_handle) = cx.focused() else { return None; }; @@ -73,7 +73,13 @@ impl CommandPalette { Self { picker } } } + impl EventEmitter for CommandPalette {} +impl Modal for CommandPalette { + fn focus(&self, cx: &mut WindowContext) { + self.picker.update(cx, |picker, cx| picker.focus(cx)); + } +} impl Render for CommandPalette { type Element = Div; @@ -147,7 +153,6 @@ impl PickerDelegate for CommandPaletteDelegate { type ListItem = Div>; fn match_count(&self) -> usize { - dbg!(self.matches.len()); self.matches.len() } @@ -254,7 +259,6 @@ impl PickerDelegate for CommandPaletteDelegate { picker .update(&mut cx, |picker, _| { let delegate = &mut picker.delegate; - dbg!(&matches); delegate.commands = commands; delegate.matches = matches; if delegate.matches.is_empty() { @@ -269,6 +273,7 @@ impl PickerDelegate for CommandPaletteDelegate { } fn dismissed(&mut self, cx: &mut ViewContext>) { + cx.focus(&self.previous_focus_handle); self.command_palette .update(cx, |_, cx| cx.emit(ModalEvent::Dismissed)) .log_err(); diff --git a/crates/go_to_line2/src/go_to_line.rs b/crates/go_to_line2/src/go_to_line.rs index c65373e6ac..ca68a9ae79 100644 --- a/crates/go_to_line2/src/go_to_line.rs +++ b/crates/go_to_line2/src/go_to_line.rs @@ -8,22 +8,24 @@ use text::{Bias, Point}; use theme::ActiveTheme; use ui::{h_stack, modal, v_stack, Label, LabelColor}; use util::paths::FILE_ROW_COLUMN_DELIMITER; -use workspace::{ModalEvent, Workspace}; +use workspace::{Modal, ModalEvent, Workspace}; actions!(Toggle); pub fn init(cx: &mut AppContext) { cx.observe_new_views( - |workspace: &mut Workspace, _: &mut ViewContext| { - workspace - .modal_layer() - .register_modal(Toggle, |workspace, cx| { - let editor = workspace - .active_item(cx) - .and_then(|active_item| active_item.downcast::())?; + |workspace: &mut Workspace, cx: &mut ViewContext| { + let handle = cx.view().downgrade(); - Some(cx.build_view(|cx| GoToLine::new(editor, cx))) - }); + workspace.modal_layer().register_modal(Toggle, move |cx| { + let workspace = handle.upgrade()?; + let editor = workspace + .read(cx) + .active_item(cx) + .and_then(|active_item| active_item.downcast::())?; + + Some(cx.build_view(|cx| GoToLine::new(editor, cx))) + }); }, ) .detach(); @@ -44,14 +46,15 @@ pub enum Event { impl EventEmitter for GoToLine {} impl EventEmitter for GoToLine {} +impl Modal for GoToLine { + fn focus(&self, cx: &mut WindowContext) { + self.line_editor.update(cx, |editor, cx| editor.focus(cx)) + } +} impl GoToLine { pub fn new(active_editor: View, cx: &mut ViewContext) -> Self { - let line_editor = cx.build_view(|cx| { - let editor = Editor::single_line(cx); - editor.focus(cx); - editor - }); + let line_editor = cx.build_view(|cx| Editor::single_line(cx)); let line_editor_change = cx.subscribe(&line_editor, Self::on_line_editor_event); let editor = active_editor.read(cx); diff --git a/crates/gpui2/src/elements/uniform_list.rs b/crates/gpui2/src/elements/uniform_list.rs index 151696b8c9..181803e1e6 100644 --- a/crates/gpui2/src/elements/uniform_list.rs +++ b/crates/gpui2/src/elements/uniform_list.rs @@ -9,6 +9,9 @@ use smallvec::SmallVec; use std::{cmp, ops::Range, sync::Arc}; use taffy::style::Overflow; +/// uniform_list provides lazy rendering for a set of items that are of uniform height. +/// When rendered into a container with overflow-y: hidden and a fixed (or max) height, +/// uniform_list will only render the visibile subset of items. pub fn uniform_list( id: Id, item_count: usize, @@ -20,9 +23,12 @@ where C: Component, { let id = id.into(); + let mut style = StyleRefinement::default(); + style.overflow.y = Some(Overflow::Hidden); + UniformList { id: id.clone(), - style: Default::default(), + style, item_count, render_items: Box::new(move |view, visible_range, cx| { f(view, visible_range, cx) @@ -123,6 +129,7 @@ impl Element for UniformList { let max_items = self.item_count; let item_size = element_state.item_size; let rem_size = cx.rem_size(); + cx.request_measured_layout( self.computed_style(), rem_size, @@ -132,15 +139,12 @@ impl Element for UniformList { .width .unwrap_or(match available_space.width { AvailableSpace::Definite(x) => x, - AvailableSpace::MinContent => item_size.width, - AvailableSpace::MaxContent => item_size.width, + AvailableSpace::MinContent | AvailableSpace::MaxContent => item_size.width, }); let height = match available_space.height { AvailableSpace::Definite(x) => desired_height.min(x), - AvailableSpace::MinContent => desired_height, - AvailableSpace::MaxContent => desired_height, + AvailableSpace::MinContent | AvailableSpace::MaxContent => desired_height, }; - dbg!(known_dimensions, available_space, size(width, height)); size(width, height) }, ) @@ -171,7 +175,6 @@ impl Element for UniformList { let item_height = self .measure_first_item(view_state, Some(padded_bounds.size.width), cx) .height; - dbg!(item_height, padded_bounds); if let Some(scroll_handle) = self.scroll_handle.clone() { scroll_handle.0.lock().replace(ScrollHandleState { item_height, @@ -184,7 +187,6 @@ impl Element for UniformList { } else { 0 }; - dbg!(visible_item_count); let scroll_offset = element_state .interactive .scroll_offset() @@ -289,3 +291,11 @@ impl Component for UniformList { AnyElement::new(self) } } + +#[cfg(test)] +mod test { + use crate::{self as gpui, TestAppContext}; + + #[gpui::test] + fn test_uniform_list(cx: &mut TestAppContext) {} +} diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index 6a464a4554..0e60e28dc1 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -146,6 +146,11 @@ impl FocusHandle { } } + /// Moves the focus to the element associated with this handle. + pub fn focus(&self, cx: &mut WindowContext) { + cx.focus(self) + } + /// Obtains whether the element associated with this handle is currently focused. pub fn is_focused(&self, cx: &WindowContext) -> bool { self.id.is_focused(cx) diff --git a/crates/picker2/src/picker2.rs b/crates/picker2/src/picker2.rs index 2651d3a190..9d0019b2dc 100644 --- a/crates/picker2/src/picker2.rs +++ b/crates/picker2/src/picker2.rs @@ -59,6 +59,7 @@ impl Picker { let ix = cmp::min(index + 1, count - 1); self.delegate.set_selected_index(ix, cx); self.scroll_handle.scroll_to_item(ix); + cx.notify(); } } @@ -69,6 +70,7 @@ impl Picker { let ix = index.saturating_sub(1); self.delegate.set_selected_index(ix, cx); self.scroll_handle.scroll_to_item(ix); + cx.notify(); } } @@ -77,6 +79,7 @@ impl Picker { if count > 0 { self.delegate.set_selected_index(0, cx); self.scroll_handle.scroll_to_item(0); + cx.notify(); } } @@ -85,6 +88,7 @@ impl Picker { if count > 0 { self.delegate.set_selected_index(count - 1, cx); self.scroll_handle.scroll_to_item(count - 1); + cx.notify(); } } @@ -163,18 +167,23 @@ impl Render for Picker { .bg(cx.theme().colors().element_background), ) .child( - v_stack().py_0p5().px_1().grow().max_h_96().child( - uniform_list("candidates", self.delegate.match_count(), { - move |this: &mut Self, visible_range, cx| { - let selected_ix = this.delegate.selected_index(); - visible_range - .map(|ix| this.delegate.render_match(ix, ix == selected_ix, cx)) - .collect() - } - }) - .track_scroll(self.scroll_handle.clone()) - .size_full(), - ), + v_stack() + .py_0p5() + .px_1() + .grow() + .child( + uniform_list("candidates", self.delegate.match_count(), { + move |this: &mut Self, visible_range, cx| { + let selected_ix = this.delegate.selected_index(); + visible_range + .map(|ix| this.delegate.render_match(ix, ix == selected_ix, cx)) + .collect() + } + }) + .track_scroll(self.scroll_handle.clone()), + ) + .max_h_72() + .overflow_hidden(), ) } } diff --git a/crates/workspace2/src/modal_layer.rs b/crates/workspace2/src/modal_layer.rs index fc85ae8351..8a3f724972 100644 --- a/crates/workspace2/src/modal_layer.rs +++ b/crates/workspace2/src/modal_layer.rs @@ -1,14 +1,21 @@ use crate::Workspace; use gpui::{ - div, px, AnyView, Component, Div, EventEmitter, ParentElement, Render, StatefulInteractivity, - StatelessInteractive, Styled, Subscription, View, ViewContext, + div, px, AnyView, Component, Div, EventEmitter, FocusHandle, ParentElement, Render, + StatefulInteractivity, StatelessInteractive, Styled, Subscription, View, ViewContext, + WindowContext, }; use std::{any::TypeId, sync::Arc}; use ui::v_stack; +pub struct ActiveModal { + modal: AnyView, + subscription: Subscription, + previous_focus_handle: Option, + focus_handle: FocusHandle, +} + pub struct ModalLayer { - open_modal: Option, - subscription: Option, + active_modal: Option, registered_modals: Vec<( TypeId, Box< @@ -19,6 +26,10 @@ pub struct ModalLayer { )>, } +pub trait Modal: Render + EventEmitter { + fn focus(&self, cx: &mut WindowContext); +} + pub enum ModalEvent { Dismissed, } @@ -26,16 +37,15 @@ pub enum ModalEvent { impl ModalLayer { pub fn new() -> Self { Self { - open_modal: None, - subscription: None, + active_modal: None, registered_modals: Vec::new(), } } pub fn register_modal(&mut self, action: A, build_view: B) where - V: EventEmitter + Render, - B: Fn(&mut Workspace, &mut ViewContext) -> Option> + 'static, + V: Modal, + B: Fn(&mut WindowContext) -> Option> + 'static, { let build_view = Arc::new(build_view); @@ -45,29 +55,56 @@ impl ModalLayer { let build_view = build_view.clone(); div.on_action(move |workspace, event: &A, cx| { - let Some(new_modal) = (build_view)(workspace, cx) else { + let previous_focus = cx.focused(); + if let Some(active_modal) = &workspace.modal_layer().active_modal { + if active_modal.modal.clone().downcast::().is_ok() { + workspace.modal_layer().hide_modal(cx); + return; + } + } + let Some(new_modal) = (build_view)(cx) else { return; }; - workspace.modal_layer().show_modal(new_modal, cx); + workspace + .modal_layer() + .show_modal(previous_focus, new_modal, cx); }) }), )); } - pub fn show_modal(&mut self, new_modal: View, cx: &mut ViewContext) - where + pub fn show_modal( + &mut self, + previous_focus: Option, + new_modal: View, + cx: &mut ViewContext, + ) where V: EventEmitter + Render, { - self.subscription = Some(cx.subscribe(&new_modal, |this, modal, e, cx| match e { - ModalEvent::Dismissed => this.modal_layer().hide_modal(cx), - })); - self.open_modal = Some(new_modal.into()); + self.active_modal = Some(ActiveModal { + modal: new_modal.clone().into(), + subscription: cx.subscribe(&new_modal, |this, modal, e, cx| match e { + ModalEvent::Dismissed => this.modal_layer().hide_modal(cx), + }), + previous_focus_handle: previous_focus, + focus_handle: cx.focus_handle(), + }); cx.notify(); } pub fn hide_modal(&mut self, cx: &mut ViewContext) { - self.open_modal.take(); - self.subscription.take(); + dbg!("hiding..."); + if let Some(active_modal) = self.active_modal.take() { + dbg!("something"); + if let Some(previous_focus) = active_modal.previous_focus_handle { + dbg!("oohthing"); + if active_modal.focus_handle.contains_focused(cx) { + dbg!("aahthing"); + previous_focus.focus(cx); + } + } + } + cx.notify(); } @@ -81,7 +118,7 @@ impl ModalLayer { parent = (action)(parent); } - parent.when_some(self.open_modal.as_ref(), |parent, open_modal| { + parent.when_some(self.active_modal.as_ref(), |parent, open_modal| { let container1 = div() .absolute() .flex() @@ -92,10 +129,13 @@ impl ModalLayer { .left_0() .z_index(400); - // transparent layer - let container2 = v_stack().h(px(0.0)).relative().top_20(); + let container2 = v_stack() + .h(px(0.0)) + .relative() + .top_20() + .track_focus(&open_modal.focus_handle); - parent.child(container1.child(container2.child(open_modal.clone()))) + parent.child(container1.child(container2.child(open_modal.modal.clone()))) }) } }