diff --git a/Cargo.lock b/Cargo.lock index 5b548babd78cae1a936fb939b1764917bdcb2310..260ecb630a60fd158b3c645972480862699d8b90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4987,7 +4987,8 @@ dependencies = [ name = "menu2" version = "0.1.0" dependencies = [ - "gpui2", + "serde", + "serde_derive", ] [[package]] @@ -6023,6 +6024,23 @@ dependencies = [ "workspace", ] +[[package]] +name = "picker2" +version = "0.1.0" +dependencies = [ + "ctor", + "editor2", + "env_logger 0.9.3", + "gpui2", + "menu2", + "parking_lot 0.11.2", + "serde_json", + "settings2", + "theme2", + "util", + "workspace2", +] + [[package]] name = "pico-args" version = "0.4.2" @@ -8539,9 +8557,14 @@ dependencies = [ "backtrace-on-stack-overflow", "chrono", "clap 4.4.4", + "editor2", + "fuzzy2", "gpui2", "itertools 0.11.0", + "language2", "log", + "menu2", + "picker2", "rust-embed", "serde", "settings2", diff --git a/Cargo.toml b/Cargo.toml index 01d6c50643642cd49e702b3c7b5237323e31b37f..81aff80c90fa51386b44f71a7a5babcef32cc993 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,7 @@ members = [ "crates/notifications", "crates/outline", "crates/picker", + "crates/picker2", "crates/plugin", "crates/plugin_macros", "crates/plugin_runtime", diff --git a/crates/editor2/src/editor.rs b/crates/editor2/src/editor.rs index 8c691b33509bd5930f8b3da6deafc29f3c03e0b9..ea76a7b57d9eb6545cd8e94c4369034b2f4b49a5 100644 --- a/crates/editor2/src/editor.rs +++ b/crates/editor2/src/editor.rs @@ -9192,7 +9192,7 @@ impl Editor { supports } - fn focus(&self, cx: &mut WindowContext) { + pub fn focus(&self, cx: &mut WindowContext) { cx.focus(&self.focus_handle) } } diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index 34ce39fd8a39404e308784a0162a399332a984f7..04b8494a888bcc8333de24effa8edf675ba02866 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -19,8 +19,8 @@ use gpui::{ BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchContext, DispatchPhase, Edges, Element, ElementId, Entity, GlobalElementId, Hsla, KeyDownEvent, KeyListener, KeyMatch, Line, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, - ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle, - TextSystem, ViewContext, WindowContext, + ScrollWheelEvent, ShapedGlyph, Size, Style, TextRun, TextStyle, TextSystem, ViewContext, + WindowContext, }; use itertools::Itertools; use language::language_settings::ShowWhitespaceSetting; @@ -3220,7 +3220,7 @@ impl PositionMap { let previous_valid = self.snapshot.clip_point(exact_unclipped, Bias::Left); let next_valid = self.snapshot.clip_point(exact_unclipped, Bias::Right); - let column_overshoot_after_line_end = (x_overshoot_after_line_end / self.em_advance).into(); + let column_overshoot_after_line_end = (x_overshoot_after_line_end / self.em_advance) as u32; *exact_unclipped.column_mut() += column_overshoot_after_line_end; PointForPosition { previous_valid, diff --git a/crates/gpui2/src/app/entity_map.rs b/crates/gpui2/src/app/entity_map.rs index e626f8c409d2a80ac9f7ac2116741855f7e5beb3..588091c7a0bb1e3027b3a81cc0be2fa55f98e0f3 100644 --- a/crates/gpui2/src/app/entity_map.rs +++ b/crates/gpui2/src/app/entity_map.rs @@ -13,6 +13,7 @@ use std::{ atomic::{AtomicUsize, Ordering::SeqCst}, Arc, Weak, }, + thread::panicking, }; slotmap::new_key_type! { pub struct EntityId; } @@ -140,9 +141,8 @@ impl<'a, T: 'static> core::ops::DerefMut for Lease<'a, T> { impl<'a, T> Drop for Lease<'a, T> { fn drop(&mut self) { - if self.entity.is_some() { - // We don't panic here, because other panics can cause us to drop the lease without ending it cleanly. - log::error!("Leases must be ended with EntityMap::end_lease") + if self.entity.is_some() && !panicking() { + panic!("Leases must be ended with EntityMap::end_lease") } } } diff --git a/crates/gpui2/src/color.rs b/crates/gpui2/src/color.rs index 77f8ebdf41cad82bd16a36380814d8bf1c946cd1..3ae247962b13071c0e7e711778f6d1c9b60dc9df 100644 --- a/crates/gpui2/src/color.rs +++ b/crates/gpui2/src/color.rs @@ -203,6 +203,15 @@ pub fn red() -> Hsla { } } +pub fn blue() -> Hsla { + Hsla { + h: 0.6, + s: 1., + l: 0.5, + a: 1., + } +} + impl Hsla { /// Returns true if the HSLA color is fully transparent, false otherwise. pub fn is_transparent(&self) -> bool { diff --git a/crates/gpui2/src/elements.rs b/crates/gpui2/src/elements.rs index 83c27b8a3b1a88e6dee517c485f5f99ea1df7a93..eb061f7d34c8e77c1a366123818b253d33620c7a 100644 --- a/crates/gpui2/src/elements.rs +++ b/crates/gpui2/src/elements.rs @@ -2,8 +2,10 @@ mod div; mod img; mod svg; mod text; +mod uniform_list; pub use div::*; pub use img::*; pub use svg::*; pub use text::*; +pub use uniform_list::*; diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index 061ec095f8917517c38c7c4ce71d3e95ff03d7f6..9b8c9dbf855d79ad0c36843250ded199e7b02c6b 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -1,28 +1,28 @@ use crate::{ point, AnyElement, BorrowWindow, Bounds, Component, Element, ElementFocus, ElementId, - ElementInteraction, FocusDisabled, FocusEnabled, FocusHandle, FocusListeners, Focusable, + ElementInteractivity, FocusDisabled, FocusEnabled, FocusHandle, FocusListeners, Focusable, GlobalElementId, GroupBounds, InteractiveElementState, LayoutId, Overflow, ParentElement, - Pixels, Point, SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction, - StatelessInteractive, Style, StyleRefinement, Styled, ViewContext, Visibility, + Pixels, Point, SharedString, StatefulInteractive, StatefulInteractivity, StatelessInteractive, + StatelessInteractivity, Style, StyleRefinement, Styled, ViewContext, Visibility, }; use refineable::Refineable; use smallvec::SmallVec; pub struct Div< V: 'static, - I: ElementInteraction = StatelessInteraction, + I: ElementInteractivity = StatelessInteractivity, F: ElementFocus = FocusDisabled, > { - interaction: I, + interactivity: I, focus: F, children: SmallVec<[AnyElement; 2]>, group: Option, base_style: StyleRefinement, } -pub fn div() -> Div, FocusDisabled> { +pub fn div() -> Div, FocusDisabled> { Div { - interaction: StatelessInteraction::default(), + interactivity: StatelessInteractivity::default(), focus: FocusDisabled, children: SmallVec::new(), group: None, @@ -30,14 +30,14 @@ pub fn div() -> Div, FocusDisabled> { } } -impl Div, F> +impl Div, F> where V: 'static, F: ElementFocus, { - pub fn id(self, id: impl Into) -> Div, F> { + pub fn id(self, id: impl Into) -> Div, F> { Div { - interaction: id.into().into(), + interactivity: id.into().into(), focus: self.focus, children: self.children, group: self.group, @@ -48,7 +48,7 @@ where impl Div where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { pub fn group(mut self, group: impl Into) -> Self { @@ -98,16 +98,20 @@ where let mut computed_style = Style::default(); computed_style.refine(&self.base_style); self.focus.refine_style(&mut computed_style, cx); - self.interaction - .refine_style(&mut computed_style, bounds, &element_state.interactive, cx); + self.interactivity.refine_style( + &mut computed_style, + bounds, + &element_state.interactive, + cx, + ); computed_style } } -impl Div, FocusDisabled> { - pub fn focusable(self) -> Div, FocusEnabled> { +impl Div, FocusDisabled> { + pub fn focusable(self) -> Div, FocusEnabled> { Div { - interaction: self.interaction, + interactivity: self.interactivity, focus: FocusEnabled::new(), children: self.children, group: self.group, @@ -118,9 +122,9 @@ impl Div, FocusDisabled> { pub fn track_focus( self, handle: &FocusHandle, - ) -> Div, FocusEnabled> { + ) -> Div, FocusEnabled> { Div { - interaction: self.interaction, + interactivity: self.interactivity, focus: FocusEnabled::tracked(handle), children: self.children, group: self.group, @@ -145,13 +149,13 @@ impl Div, FocusDisabled> { } } -impl Div, FocusDisabled> { +impl Div, FocusDisabled> { pub fn track_focus( self, handle: &FocusHandle, - ) -> Div, FocusEnabled> { + ) -> Div, FocusEnabled> { Div { - interaction: self.interaction.into_stateful(handle), + interactivity: self.interactivity.into_stateful(handle), focus: handle.clone().into(), children: self.children, group: self.group, @@ -163,7 +167,7 @@ impl Div, FocusDisabled> { impl Focusable for Div> where V: 'static, - I: ElementInteraction, + I: ElementInteractivity, { fn focus_listeners(&mut self) -> &mut FocusListeners { &mut self.focus.focus_listeners @@ -191,13 +195,13 @@ pub struct DivState { impl Element for Div where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { type ElementState = DivState; fn id(&self) -> Option { - self.interaction + self.interactivity .as_stateful() .map(|identified| identified.id.clone()) } @@ -209,7 +213,7 @@ where cx: &mut ViewContext, ) -> Self::ElementState { let mut element_state = element_state.unwrap_or_default(); - self.interaction.initialize(cx, |cx| { + self.interactivity.initialize(cx, |cx| { self.focus .initialize(element_state.focus_handle.take(), cx, |focus_handle, cx| { element_state.focus_handle = focus_handle; @@ -281,11 +285,11 @@ where (child_max - child_min).into() }; - cx.stack(z_index, |cx| { - cx.stack(0, |cx| { + cx.with_z_index(z_index, |cx| { + cx.with_z_index(0, |cx| { style.paint(bounds, cx); this.focus.paint(bounds, cx); - this.interaction.paint( + this.interactivity.paint( bounds, content_size, style.overflow, @@ -293,7 +297,7 @@ where cx, ); }); - cx.stack(1, |cx| { + cx.with_z_index(1, |cx| { style.apply_text_style(cx, |cx| { style.apply_overflow(bounds, cx, |cx| { let scroll_offset = element_state.interactive.scroll_offset(); @@ -316,7 +320,7 @@ where impl Component for Div where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { fn render(self) -> AnyElement { @@ -326,7 +330,7 @@ where impl ParentElement for Div where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { @@ -336,7 +340,7 @@ where impl Styled for Div where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { fn style(&mut self) -> &mut StyleRefinement { @@ -346,19 +350,19 @@ where impl StatelessInteractive for Div where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { - fn stateless_interaction(&mut self) -> &mut StatelessInteraction { - self.interaction.as_stateless_mut() + fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity { + self.interactivity.as_stateless_mut() } } -impl StatefulInteractive for Div, F> +impl StatefulInteractive for Div, F> where F: ElementFocus, { - fn stateful_interaction(&mut self) -> &mut StatefulInteraction { - &mut self.interaction + fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity { + &mut self.interactivity } } diff --git a/crates/gpui2/src/elements/img.rs b/crates/gpui2/src/elements/img.rs index a35436d74ec9f668e4081eb9a9c0f1ea4168a914..638665d4141d4a19362b2812932048a8ad46c348 100644 --- a/crates/gpui2/src/elements/img.rs +++ b/crates/gpui2/src/elements/img.rs @@ -1,15 +1,15 @@ use crate::{ div, AnyElement, BorrowWindow, Bounds, Component, Div, DivState, Element, ElementFocus, - ElementId, ElementInteraction, FocusDisabled, FocusEnabled, FocusListeners, Focusable, - LayoutId, Pixels, SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction, - StatelessInteractive, StyleRefinement, Styled, ViewContext, + ElementId, ElementInteractivity, FocusDisabled, FocusEnabled, FocusListeners, Focusable, + LayoutId, Pixels, SharedString, StatefulInteractive, StatefulInteractivity, + StatelessInteractive, StatelessInteractivity, StyleRefinement, Styled, ViewContext, }; use futures::FutureExt; use util::ResultExt; pub struct Img< V: 'static, - I: ElementInteraction = StatelessInteraction, + I: ElementInteractivity = StatelessInteractivity, F: ElementFocus = FocusDisabled, > { base: Div, @@ -17,7 +17,7 @@ pub struct Img< grayscale: bool, } -pub fn img() -> Img, FocusDisabled> { +pub fn img() -> Img, FocusDisabled> { Img { base: div(), uri: None, @@ -28,7 +28,7 @@ pub fn img() -> Img, FocusDisabled> { impl Img where V: 'static, - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { pub fn uri(mut self, uri: impl Into) -> Self { @@ -42,11 +42,11 @@ where } } -impl Img, F> +impl Img, F> where F: ElementFocus, { - pub fn id(self, id: impl Into) -> Img, F> { + pub fn id(self, id: impl Into) -> Img, F> { Img { base: self.base.id(id), uri: self.uri, @@ -57,7 +57,7 @@ where impl Component for Img where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { fn render(self) -> AnyElement { @@ -67,7 +67,7 @@ where impl Element for Img where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { type ElementState = DivState; @@ -101,7 +101,7 @@ where element_state: &mut Self::ElementState, cx: &mut ViewContext, ) { - cx.stack(0, |cx| { + cx.with_z_index(0, |cx| { self.base.paint(bounds, view, element_state, cx); }); @@ -118,7 +118,7 @@ where .and_then(ResultExt::log_err) { let corner_radii = corner_radii.to_pixels(bounds.size, cx.rem_size()); - cx.stack(1, |cx| { + cx.with_z_index(1, |cx| { cx.paint_image(bounds, corner_radii, data, self.grayscale) .log_err() }); @@ -136,7 +136,7 @@ where impl Styled for Img where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { fn style(&mut self) -> &mut StyleRefinement { @@ -146,27 +146,27 @@ where impl StatelessInteractive for Img where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { - fn stateless_interaction(&mut self) -> &mut StatelessInteraction { - self.base.stateless_interaction() + fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity { + self.base.stateless_interactivity() } } -impl StatefulInteractive for Img, F> +impl StatefulInteractive for Img, F> where F: ElementFocus, { - fn stateful_interaction(&mut self) -> &mut StatefulInteraction { - self.base.stateful_interaction() + fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity { + self.base.stateful_interactivity() } } impl Focusable for Img> where V: 'static, - I: ElementInteraction, + I: ElementInteractivity, { fn focus_listeners(&mut self) -> &mut FocusListeners { self.base.focus_listeners() diff --git a/crates/gpui2/src/elements/svg.rs b/crates/gpui2/src/elements/svg.rs index 7db4c5cf6db86d96bf154c9a4f243c630b0a3fe5..8e2ba9d8a1ba944f7cd82ca583e30e53de147c23 100644 --- a/crates/gpui2/src/elements/svg.rs +++ b/crates/gpui2/src/elements/svg.rs @@ -1,21 +1,21 @@ use crate::{ div, AnyElement, Bounds, Component, Div, DivState, Element, ElementFocus, ElementId, - ElementInteraction, FocusDisabled, FocusEnabled, FocusListeners, Focusable, LayoutId, Pixels, - SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction, - StatelessInteractive, StyleRefinement, Styled, ViewContext, + ElementInteractivity, FocusDisabled, FocusEnabled, FocusListeners, Focusable, LayoutId, Pixels, + SharedString, StatefulInteractive, StatefulInteractivity, StatelessInteractive, + StatelessInteractivity, StyleRefinement, Styled, ViewContext, }; use util::ResultExt; pub struct Svg< V: 'static, - I: ElementInteraction = StatelessInteraction, + I: ElementInteractivity = StatelessInteractivity, F: ElementFocus = FocusDisabled, > { base: Div, path: Option, } -pub fn svg() -> Svg, FocusDisabled> { +pub fn svg() -> Svg, FocusDisabled> { Svg { base: div(), path: None, @@ -24,7 +24,7 @@ pub fn svg() -> Svg, FocusDisabled> { impl Svg where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { pub fn path(mut self, path: impl Into) -> Self { @@ -33,11 +33,11 @@ where } } -impl Svg, F> +impl Svg, F> where F: ElementFocus, { - pub fn id(self, id: impl Into) -> Svg, F> { + pub fn id(self, id: impl Into) -> Svg, F> { Svg { base: self.base.id(id), path: self.path, @@ -47,7 +47,7 @@ where impl Component for Svg where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { fn render(self) -> AnyElement { @@ -57,7 +57,7 @@ where impl Element for Svg where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { type ElementState = DivState; @@ -107,7 +107,7 @@ where impl Styled for Svg where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { fn style(&mut self) -> &mut StyleRefinement { @@ -117,27 +117,27 @@ where impl StatelessInteractive for Svg where - I: ElementInteraction, + I: ElementInteractivity, F: ElementFocus, { - fn stateless_interaction(&mut self) -> &mut StatelessInteraction { - self.base.stateless_interaction() + fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity { + self.base.stateless_interactivity() } } -impl StatefulInteractive for Svg, F> +impl StatefulInteractive for Svg, F> where V: 'static, F: ElementFocus, { - fn stateful_interaction(&mut self) -> &mut StatefulInteraction { - self.base.stateful_interaction() + fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity { + self.base.stateful_interactivity() } } impl Focusable for Svg> where - I: ElementInteraction, + I: ElementInteractivity, { fn focus_listeners(&mut self) -> &mut FocusListeners { self.base.focus_listeners() diff --git a/crates/gpui2/src/elements/text.rs b/crates/gpui2/src/elements/text.rs index 76c587d22f3e641c248f38e79a96b2651aa7ab43..e258d3e7dc6dba8c7d7c625981b7ef340c1dc96c 100644 --- a/crates/gpui2/src/elements/text.rs +++ b/crates/gpui2/src/elements/text.rs @@ -127,6 +127,7 @@ impl Element for Text { let element_state = element_state .as_ref() .expect("measurement has not been performed"); + let line_height = element_state.line_height; let mut line_origin = bounds.origin; for line in &element_state.lines { diff --git a/crates/gpui2/src/elements/uniform_list.rs b/crates/gpui2/src/elements/uniform_list.rs new file mode 100644 index 0000000000000000000000000000000000000000..d43c6b59926de499959304d5af754bc8a97aec07 --- /dev/null +++ b/crates/gpui2/src/elements/uniform_list.rs @@ -0,0 +1,244 @@ +use crate::{ + point, px, AnyElement, AvailableSpace, BorrowWindow, Bounds, Component, Element, ElementId, + ElementInteractivity, InteractiveElementState, LayoutId, Pixels, Point, Size, + StatefulInteractive, StatefulInteractivity, StatelessInteractive, StatelessInteractivity, + StyleRefinement, Styled, ViewContext, +}; +use parking_lot::Mutex; +use smallvec::SmallVec; +use std::{cmp, ops::Range, sync::Arc}; +use taffy::style::Overflow; + +pub fn uniform_list( + id: Id, + item_count: usize, + f: impl 'static + Fn(&mut V, Range, &mut ViewContext) -> SmallVec<[C; 64]>, +) -> UniformList +where + Id: Into, + V: 'static, + C: Component, +{ + let id = id.into(); + UniformList { + id: id.clone(), + style: Default::default(), + item_count, + render_items: Box::new(move |view, visible_range, cx| { + f(view, visible_range, cx) + .into_iter() + .map(|component| component.render()) + .collect() + }), + interactivity: id.into(), + scroll_handle: None, + } +} + +pub struct UniformList { + id: ElementId, + style: StyleRefinement, + item_count: usize, + render_items: Box< + dyn for<'a> Fn( + &'a mut V, + Range, + &'a mut ViewContext, + ) -> SmallVec<[AnyElement; 64]>, + >, + interactivity: StatefulInteractivity, + scroll_handle: Option, +} + +#[derive(Clone)] +pub struct UniformListScrollHandle(Arc>>); + +#[derive(Clone, Debug)] +struct ScrollHandleState { + item_height: Pixels, + list_height: Pixels, + scroll_offset: Arc>>, +} + +impl UniformListScrollHandle { + pub fn new() -> Self { + Self(Arc::new(Mutex::new(None))) + } + + pub fn scroll_to_item(&self, ix: usize) { + if let Some(state) = &*self.0.lock() { + let mut scroll_offset = state.scroll_offset.lock(); + let item_top = state.item_height * ix; + let item_bottom = item_top + state.item_height; + let scroll_top = -scroll_offset.y; + if item_top < scroll_top { + scroll_offset.y = -item_top; + } else if item_bottom > scroll_top + state.list_height { + scroll_offset.y = -(item_bottom - state.list_height); + } + } + } +} + +impl Styled for UniformList { + fn style(&mut self) -> &mut StyleRefinement { + &mut self.style + } +} + +impl Element for UniformList { + type ElementState = InteractiveElementState; + + fn id(&self) -> Option { + Some(self.id.clone()) + } + + fn initialize( + &mut self, + _: &mut V, + element_state: Option, + _: &mut ViewContext, + ) -> Self::ElementState { + element_state.unwrap_or_default() + } + + fn layout( + &mut self, + _view_state: &mut V, + _element_state: &mut Self::ElementState, + cx: &mut ViewContext, + ) -> LayoutId { + cx.request_layout(&self.computed_style(), None) + } + + fn paint( + &mut self, + bounds: crate::Bounds, + view_state: &mut V, + element_state: &mut Self::ElementState, + cx: &mut ViewContext, + ) { + let style = self.computed_style(); + style.paint(bounds, cx); + + let border = style.border_widths.to_pixels(cx.rem_size()); + let padding = style.padding.to_pixels(bounds.size.into(), cx.rem_size()); + + let padded_bounds = Bounds::from_corners( + bounds.origin + point(border.left + padding.left, border.top + padding.top), + bounds.lower_right() + - point(border.right + padding.right, border.bottom + padding.bottom), + ); + + cx.with_z_index(style.z_index.unwrap_or(0), |cx| { + let content_size; + if self.item_count > 0 { + let item_height = self.measure_item_height(view_state, padded_bounds, cx); + if let Some(scroll_handle) = self.scroll_handle.clone() { + scroll_handle.0.lock().replace(ScrollHandleState { + item_height, + list_height: padded_bounds.size.height, + scroll_offset: element_state.track_scroll_offset(), + }); + } + let visible_item_count = if item_height > px(0.) { + (padded_bounds.size.height / item_height).ceil() as usize + 1 + } else { + 0 + }; + let scroll_offset = element_state + .scroll_offset() + .map_or((0.0).into(), |offset| offset.y); + let first_visible_element_ix = (-scroll_offset / item_height).floor() as usize; + let visible_range = first_visible_element_ix + ..cmp::min( + first_visible_element_ix + visible_item_count, + self.item_count, + ); + + let mut items = (self.render_items)(view_state, visible_range.clone(), cx); + + content_size = Size { + width: padded_bounds.size.width, + height: item_height * self.item_count, + }; + + cx.with_z_index(1, |cx| { + for (item, ix) in items.iter_mut().zip(visible_range) { + item.initialize(view_state, cx); + + let layout_id = item.layout(view_state, cx); + cx.compute_layout( + layout_id, + Size { + width: AvailableSpace::Definite(bounds.size.width), + height: AvailableSpace::Definite(item_height), + }, + ); + let offset = + padded_bounds.origin + point(px(0.), item_height * ix + scroll_offset); + cx.with_element_offset(Some(offset), |cx| item.paint(view_state, cx)) + } + }); + } else { + content_size = Size { + width: bounds.size.width, + height: px(0.), + }; + } + + let overflow = point(style.overflow.x, Overflow::Scroll); + + cx.with_z_index(0, |cx| { + self.interactivity + .paint(bounds, content_size, overflow, element_state, cx); + }); + }) + } +} + +impl UniformList { + fn measure_item_height( + &self, + view_state: &mut V, + list_bounds: Bounds, + cx: &mut ViewContext, + ) -> Pixels { + let mut items = (self.render_items)(view_state, 0..1, cx); + debug_assert!(items.len() == 1); + let mut item_to_measure = items.pop().unwrap(); + item_to_measure.initialize(view_state, cx); + let layout_id = item_to_measure.layout(view_state, cx); + cx.compute_layout( + layout_id, + Size { + width: AvailableSpace::Definite(list_bounds.size.width), + height: AvailableSpace::MinContent, + }, + ); + cx.layout_bounds(layout_id).size.height + } + + pub fn track_scroll(mut self, handle: UniformListScrollHandle) -> Self { + self.scroll_handle = Some(handle); + self + } +} + +impl StatelessInteractive for UniformList { + fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity { + self.interactivity.as_stateless_mut() + } +} + +impl StatefulInteractive for UniformList { + fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity { + &mut self.interactivity + } +} + +impl Component for UniformList { + fn render(self) -> AnyElement { + AnyElement::new(self) + } +} diff --git a/crates/gpui2/src/geometry.rs b/crates/gpui2/src/geometry.rs index e16635be362f6bc59370805a6c93413da50a7c64..c5209239aea6ed84bc43d715d52b6b1e0a895c7c 100644 --- a/crates/gpui2/src/geometry.rs +++ b/crates/gpui2/src/geometry.rs @@ -267,6 +267,24 @@ impl From> for Size { } } +impl From> for Size { + fn from(size: Size) -> Self { + Size { + width: size.width.into(), + height: size.height.into(), + } + } +} + +impl From> for Size { + fn from(size: Size) -> Self { + Size { + width: size.width.into(), + height: size.height.into(), + } + } +} + impl Size { pub fn full() -> Self { Self { @@ -558,6 +576,15 @@ impl Edges { left: px(0.).into(), } } + + pub fn to_pixels(&self, parent_size: Size, rem_size: Pixels) -> Edges { + Edges { + top: self.top.to_pixels(parent_size.height, rem_size), + right: self.right.to_pixels(parent_size.width, rem_size), + bottom: self.bottom.to_pixels(parent_size.height, rem_size), + left: self.left.to_pixels(parent_size.width, rem_size), + } + } } impl Edges { @@ -689,16 +716,16 @@ impl Copy for Corners where T: Copy + Clone + Default + Debug {} pub struct Pixels(pub(crate) f32); impl std::ops::Div for Pixels { - type Output = Self; + type Output = f32; fn div(self, rhs: Self) -> Self::Output { - Self(self.0 / rhs.0) + self.0 / rhs.0 } } impl std::ops::DivAssign for Pixels { fn div_assign(&mut self, rhs: Self) { - self.0 /= rhs.0; + *self = Self(self.0 / rhs.0); } } @@ -750,14 +777,6 @@ impl Pixels { pub const ZERO: Pixels = Pixels(0.0); pub const MAX: Pixels = Pixels(f32::MAX); - pub fn as_usize(&self) -> usize { - self.0 as usize - } - - pub fn as_isize(&self) -> isize { - self.0 as isize - } - pub fn floor(&self) -> Self { Self(self.0.floor()) } diff --git a/crates/gpui2/src/interactive.rs b/crates/gpui2/src/interactive.rs index 84beb56ef8c68c44824e7d00a2a7b218ae689722..308c39fe7ffe912456fc6ec677d98ecb8fb3982b 100644 --- a/crates/gpui2/src/interactive.rs +++ b/crates/gpui2/src/interactive.rs @@ -25,13 +25,13 @@ const TOOLTIP_DELAY: Duration = Duration::from_millis(500); const TOOLTIP_OFFSET: Point = Point::new(px(10.0), px(8.0)); pub trait StatelessInteractive: Element { - fn stateless_interaction(&mut self) -> &mut StatelessInteraction; + fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity; fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self where Self: Sized, { - self.stateless_interaction().hover_style = f(StyleRefinement::default()); + self.stateless_interactivity().hover_style = f(StyleRefinement::default()); self } @@ -43,7 +43,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction().group_hover_style = Some(GroupStyle { + self.stateless_interactivity().group_hover_style = Some(GroupStyle { group: group_name.into(), style: f(StyleRefinement::default()), }); @@ -58,7 +58,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction() + self.stateless_interactivity() .mouse_down_listeners .push(Box::new(move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Bubble @@ -79,7 +79,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction() + self.stateless_interactivity() .mouse_up_listeners .push(Box::new(move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Bubble @@ -100,7 +100,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction() + self.stateless_interactivity() .mouse_down_listeners .push(Box::new(move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Capture @@ -121,7 +121,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction() + self.stateless_interactivity() .mouse_up_listeners .push(Box::new(move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Capture @@ -141,7 +141,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction() + self.stateless_interactivity() .mouse_move_listeners .push(Box::new(move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { @@ -158,7 +158,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction() + self.stateless_interactivity() .scroll_wheel_listeners .push(Box::new(move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { @@ -174,23 +174,48 @@ pub trait StatelessInteractive: Element { C: TryInto, C::Error: Debug, { - self.stateless_interaction().dispatch_context = + self.stateless_interactivity().dispatch_context = context.try_into().expect("invalid dispatch context"); self } + /// Capture the given action, fires during the capture phase + fn capture_action( + mut self, + listener: impl Fn(&mut V, &A, &mut ViewContext) + 'static, + ) -> Self + where + Self: Sized, + { + self.stateless_interactivity().key_listeners.push(( + TypeId::of::(), + Box::new(move |view, event, _, phase, cx| { + let event = event.downcast_ref().unwrap(); + if phase == DispatchPhase::Capture { + listener(view, event, cx) + } + None + }), + )); + self + } + + /// Add a listener for the given action, fires during the bubble event phase fn on_action( mut self, - listener: impl Fn(&mut V, &A, DispatchPhase, &mut ViewContext) + 'static, + listener: impl Fn(&mut V, &A, &mut ViewContext) + 'static, ) -> Self where Self: Sized, { - self.stateless_interaction().key_listeners.push(( + self.stateless_interactivity().key_listeners.push(( TypeId::of::(), Box::new(move |view, event, _, phase, cx| { let event = event.downcast_ref().unwrap(); - listener(view, event, phase, cx); + if phase == DispatchPhase::Bubble { + listener(view, event, cx) + } + None }), )); @@ -204,7 +229,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction().key_listeners.push(( + self.stateless_interactivity().key_listeners.push(( TypeId::of::(), Box::new(move |view, event, _, phase, cx| { let event = event.downcast_ref().unwrap(); @@ -222,7 +247,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction().key_listeners.push(( + self.stateless_interactivity().key_listeners.push(( TypeId::of::(), Box::new(move |view, event, _, phase, cx| { let event = event.downcast_ref().unwrap(); @@ -237,7 +262,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction() + self.stateless_interactivity() .drag_over_styles .push((TypeId::of::(), f(StyleRefinement::default()))); self @@ -251,7 +276,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction().group_drag_over_styles.push(( + self.stateless_interactivity().group_drag_over_styles.push(( TypeId::of::(), GroupStyle { group: group_name.into(), @@ -268,7 +293,7 @@ pub trait StatelessInteractive: Element { where Self: Sized, { - self.stateless_interaction().drop_listeners.push(( + self.stateless_interactivity().drop_listeners.push(( TypeId::of::(), Box::new(move |view, dragged_view, cx| { listener(view, dragged_view.downcast().unwrap(), cx); @@ -279,13 +304,13 @@ pub trait StatelessInteractive: Element { } pub trait StatefulInteractive: StatelessInteractive { - fn stateful_interaction(&mut self) -> &mut StatefulInteraction; + fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity; fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self where Self: Sized, { - self.stateful_interaction().active_style = f(StyleRefinement::default()); + self.stateful_interactivity().active_style = f(StyleRefinement::default()); self } @@ -297,7 +322,7 @@ pub trait StatefulInteractive: StatelessInteractive { where Self: Sized, { - self.stateful_interaction().group_active_style = Some(GroupStyle { + self.stateful_interactivity().group_active_style = Some(GroupStyle { group: group_name.into(), style: f(StyleRefinement::default()), }); @@ -311,7 +336,7 @@ pub trait StatefulInteractive: StatelessInteractive { where Self: Sized, { - self.stateful_interaction() + self.stateful_interactivity() .click_listeners .push(Box::new(move |view, event, cx| listener(view, event, cx))); self @@ -326,10 +351,10 @@ pub trait StatefulInteractive: StatelessInteractive { W: 'static + Render, { debug_assert!( - self.stateful_interaction().drag_listener.is_none(), + self.stateful_interactivity().drag_listener.is_none(), "calling on_drag more than once on the same element is not supported" ); - self.stateful_interaction().drag_listener = + self.stateful_interactivity().drag_listener = Some(Box::new(move |view_state, cursor_offset, cx| AnyDrag { view: listener(view_state, cx).into(), cursor_offset, @@ -342,10 +367,10 @@ pub trait StatefulInteractive: StatelessInteractive { Self: Sized, { debug_assert!( - self.stateful_interaction().hover_listener.is_none(), + self.stateful_interactivity().hover_listener.is_none(), "calling on_hover more than once on the same element is not supported" ); - self.stateful_interaction().hover_listener = Some(Box::new(listener)); + self.stateful_interactivity().hover_listener = Some(Box::new(listener)); self } @@ -358,10 +383,10 @@ pub trait StatefulInteractive: StatelessInteractive { W: 'static + Render, { debug_assert!( - self.stateful_interaction().tooltip_builder.is_none(), + self.stateful_interactivity().tooltip_builder.is_none(), "calling tooltip more than once on the same element is not supported" ); - self.stateful_interaction().tooltip_builder = Some(Arc::new(move |view_state, cx| { + self.stateful_interactivity().tooltip_builder = Some(Arc::new(move |view_state, cx| { build_tooltip(view_state, cx).into() })); @@ -369,11 +394,11 @@ pub trait StatefulInteractive: StatelessInteractive { } } -pub trait ElementInteraction: 'static { - fn as_stateless(&self) -> &StatelessInteraction; - fn as_stateless_mut(&mut self) -> &mut StatelessInteraction; - fn as_stateful(&self) -> Option<&StatefulInteraction>; - fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteraction>; +pub trait ElementInteractivity: 'static { + fn as_stateless(&self) -> &StatelessInteractivity; + fn as_stateless_mut(&mut self) -> &mut StatelessInteractivity; + fn as_stateful(&self) -> Option<&StatefulInteractivity>; + fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity>; fn initialize( &mut self, @@ -736,11 +761,11 @@ pub trait ElementInteraction: 'static { } #[derive(Deref, DerefMut)] -pub struct StatefulInteraction { +pub struct StatefulInteractivity { pub id: ElementId, #[deref] #[deref_mut] - stateless: StatelessInteraction, + stateless: StatelessInteractivity, click_listeners: SmallVec<[ClickListener; 2]>, active_style: StyleRefinement, group_active_style: Option, @@ -749,29 +774,29 @@ pub struct StatefulInteraction { tooltip_builder: Option>, } -impl ElementInteraction for StatefulInteraction { - fn as_stateful(&self) -> Option<&StatefulInteraction> { +impl ElementInteractivity for StatefulInteractivity { + fn as_stateful(&self) -> Option<&StatefulInteractivity> { Some(self) } - fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteraction> { + fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity> { Some(self) } - fn as_stateless(&self) -> &StatelessInteraction { + fn as_stateless(&self) -> &StatelessInteractivity { &self.stateless } - fn as_stateless_mut(&mut self) -> &mut StatelessInteraction { + fn as_stateless_mut(&mut self) -> &mut StatelessInteractivity { &mut self.stateless } } -impl From for StatefulInteraction { +impl From for StatefulInteractivity { fn from(id: ElementId) -> Self { Self { id, - stateless: StatelessInteraction::default(), + stateless: StatelessInteractivity::default(), click_listeners: SmallVec::new(), drag_listener: None, hover_listener: None, @@ -784,7 +809,7 @@ impl From for StatefulInteraction { type DropListener = dyn Fn(&mut V, AnyView, &mut ViewContext) + 'static; -pub struct StatelessInteraction { +pub struct StatelessInteractivity { pub dispatch_context: DispatchContext, pub mouse_down_listeners: SmallVec<[MouseDownListener; 2]>, pub mouse_up_listeners: SmallVec<[MouseUpListener; 2]>, @@ -798,9 +823,9 @@ pub struct StatelessInteraction { drop_listeners: SmallVec<[(TypeId, Box>); 2]>, } -impl StatelessInteraction { - pub fn into_stateful(self, id: impl Into) -> StatefulInteraction { - StatefulInteraction { +impl StatelessInteractivity { + pub fn into_stateful(self, id: impl Into) -> StatefulInteractivity { + StatefulInteractivity { id: id.into(), stateless: self, click_listeners: SmallVec::new(), @@ -876,9 +901,15 @@ impl InteractiveElementState { .as_ref() .map(|offset| offset.lock().clone()) } + + pub fn track_scroll_offset(&mut self) -> Arc>> { + self.scroll_offset + .get_or_insert_with(|| Arc::new(Mutex::new(Default::default()))) + .clone() + } } -impl Default for StatelessInteraction { +impl Default for StatelessInteractivity { fn default() -> Self { Self { dispatch_context: DispatchContext::default(), @@ -896,20 +927,20 @@ impl Default for StatelessInteraction { } } -impl ElementInteraction for StatelessInteraction { - fn as_stateful(&self) -> Option<&StatefulInteraction> { +impl ElementInteractivity for StatelessInteractivity { + fn as_stateful(&self) -> Option<&StatefulInteractivity> { None } - fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteraction> { + fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity> { None } - fn as_stateless(&self) -> &StatelessInteraction { + fn as_stateless(&self) -> &StatelessInteractivity { self } - fn as_stateless_mut(&mut self) -> &mut StatelessInteraction { + fn as_stateless_mut(&mut self) -> &mut StatelessInteractivity { self } } @@ -1236,7 +1267,7 @@ pub type KeyListener = Box< mod test { use crate::{ self as gpui, div, Div, FocusHandle, KeyBinding, Keystroke, ParentElement, Render, - StatefulInteraction, StatelessInteractive, TestAppContext, VisualContext, + StatefulInteractivity, StatelessInteractive, TestAppContext, VisualContext, }; struct TestView { @@ -1248,7 +1279,7 @@ mod test { actions!(TestAction); impl Render for TestView { - type Element = Div>; + type Element = Div>; fn render(&mut self, _: &mut gpui::ViewContext) -> Self::Element { div().id("testview").child( @@ -1257,7 +1288,7 @@ mod test { dbg!("ola!"); this.saw_key_down = true }) - .on_action(|this: &mut TestView, _: &TestAction, _, _| { + .on_action(|this: &mut TestView, _: &TestAction, _| { dbg!("ola!"); this.saw_action = true }) diff --git a/crates/gpui2/src/style.rs b/crates/gpui2/src/style.rs index 5de173c2d4f729e90f9ba691bccb02bab664214a..6d3b31c1f7a2817cbf45b6b8c4221d045f6992ca 100644 --- a/crates/gpui2/src/style.rs +++ b/crates/gpui2/src/style.rs @@ -281,7 +281,7 @@ impl Style { pub fn paint(&self, bounds: Bounds, cx: &mut ViewContext) { let rem_size = cx.rem_size(); - cx.stack(0, |cx| { + cx.with_z_index(0, |cx| { cx.paint_shadows( bounds, self.corner_radii.to_pixels(bounds.size, rem_size), @@ -291,7 +291,7 @@ impl Style { let background_color = self.background.as_ref().and_then(Fill::color); if background_color.is_some() || self.is_border_visible() { - cx.stack(1, |cx| { + cx.with_z_index(1, |cx| { cx.paint_quad( bounds, self.corner_radii.to_pixels(bounds.size, rem_size), diff --git a/crates/gpui2/src/styled.rs b/crates/gpui2/src/styled.rs index 06be0368c02e88792e409e8675ba9c9c0aebedc8..c27347f2b1bdf40187fe77518a71a44bfccee98a 100644 --- a/crates/gpui2/src/styled.rs +++ b/crates/gpui2/src/styled.rs @@ -1,14 +1,19 @@ use crate::{ self as gpui, hsla, point, px, relative, rems, AbsoluteLength, AlignItems, CursorStyle, DefiniteLength, Display, Fill, FlexDirection, Hsla, JustifyContent, Length, Position, - SharedString, StyleRefinement, Visibility, + SharedString, Style, StyleRefinement, Visibility, }; use crate::{BoxShadow, TextStyleRefinement}; +use refineable::Refineable; use smallvec::smallvec; pub trait Styled { fn style(&mut self) -> &mut StyleRefinement; + fn computed_style(&mut self) -> Style { + Style::default().refined(self.style().clone()) + } + gpui2_macros::style_helpers!(); /// Sets the size of the element to the full width and height. diff --git a/crates/gpui2/src/text_system/line.rs b/crates/gpui2/src/text_system/line.rs index ad70a79bb1e411fdaa4b0fddc0de39bdbf96e7af..707274ad33f26ce2212512c97df5527bd420ead7 100644 --- a/crates/gpui2/src/text_system/line.rs +++ b/crates/gpui2/src/text_system/line.rs @@ -74,7 +74,6 @@ impl Line { glyph_origin.y += line_height; } prev_glyph_position = glyph.position; - let glyph_origin = glyph_origin + baseline_offset; let mut finished_underline: Option<(Point, UnderlineStyle)> = None; if glyph.index >= run_end { @@ -125,14 +124,14 @@ impl Line { if max_glyph_bounds.intersects(&content_mask.bounds) { if glyph.is_emoji { cx.paint_emoji( - glyph_origin, + glyph_origin + baseline_offset, run.font_id, glyph.id, self.layout.layout.font_size, )?; } else { cx.paint_glyph( - glyph_origin, + glyph_origin + baseline_offset, run.font_id, glyph.id, self.layout.layout.font_size, diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index e5791a2dcb4007df21e435c111a5e7260be2ad93..e55f0152d88dca97156a952a3499f74c71999fb4 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -563,6 +563,12 @@ impl<'a> WindowContext<'a> { .request_measured_layout(style, rem_size, measure) } + pub fn compute_layout(&mut self, layout_id: LayoutId, available_space: Size) { + self.window + .layout_engine + .compute_layout(layout_id, available_space) + } + /// Obtain the bounds computed for the given LayoutId relative to the window. This method should not /// be invoked until the paint phase begins, and will usually be invoked by GPUI itself automatically /// in order to pass your element its `Bounds` automatically. @@ -794,6 +800,7 @@ impl<'a> WindowContext<'a> { } /// Paint a monochrome (non-emoji) glyph into the scene for the current frame at the current z-index. + /// The y component of the origin is the baseline of the glyph. pub fn paint_glyph( &mut self, origin: Point, @@ -847,6 +854,7 @@ impl<'a> WindowContext<'a> { } /// Paint an emoji glyph into the scene for the current frame at the current z-index. + /// The y component of the origin is the baseline of the glyph. pub fn paint_emoji( &mut self, origin: Point, @@ -1707,8 +1715,8 @@ impl<'a, V: 'static> ViewContext<'a, V> { &mut self.window_cx } - pub fn stack(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R { - self.window.z_index_stack.push(order); + pub fn with_z_index(&mut self, z_index: u32, f: impl FnOnce(&mut Self) -> R) -> R { + self.window.z_index_stack.push(z_index); let result = f(self); self.window.z_index_stack.pop(); result diff --git a/crates/menu2/Cargo.toml b/crates/menu2/Cargo.toml index 9bf61db82c6c01d30c12e9c01844c3db464df40f..5fc33ddb11d41416c4b64aa15d7e3d1564952f94 100644 --- a/crates/menu2/Cargo.toml +++ b/crates/menu2/Cargo.toml @@ -9,4 +9,5 @@ path = "src/menu2.rs" doctest = false [dependencies] -gpui = { package = "gpui2", path = "../gpui2" } +serde.workspace = true +serde_derive.workspace = true diff --git a/crates/menu2/src/menu2.rs b/crates/menu2/src/menu2.rs index decd4aca22be0bda9c9c5ceae87346213e9d4392..da21bdcd2282cd4834f42eb67839553ea5c6ae91 100644 --- a/crates/menu2/src/menu2.rs +++ b/crates/menu2/src/menu2.rs @@ -1,25 +1,25 @@ -// todo!(use actions! macro) +use serde_derive::Deserialize; -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct Cancel; -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct Confirm; -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct SecondaryConfirm; -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct SelectPrev; -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct SelectNext; -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct SelectFirst; -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct SelectLast; -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct ShowContextMenu; diff --git a/crates/picker2/Cargo.toml b/crates/picker2/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..8d88c25366fa52bcaa20018503608d3dd86ebbf2 --- /dev/null +++ b/crates/picker2/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "picker2" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +path = "src/picker2.rs" +doctest = false + +[dependencies] +editor = { package = "editor2", path = "../editor2" } +gpui = { package = "gpui2", path = "../gpui2" } +menu = { package = "menu2", path = "../menu2" } +settings = { package = "settings2", path = "../settings2" } +util = { path = "../util" } +theme = { package = "theme2", path = "../theme2" } +workspace = { package = "workspace2", path = "../workspace2" } + +parking_lot.workspace = true + +[dev-dependencies] +editor = { package = "editor2", path = "../editor2", features = ["test-support"] } +gpui = { package = "gpui2", path = "../gpui2", features = ["test-support"] } +serde_json.workspace = true +workspace = { package = "workspace2", path = "../workspace2", features = ["test-support"] } +ctor.workspace = true +env_logger.workspace = true diff --git a/crates/picker2/src/picker2.rs b/crates/picker2/src/picker2.rs new file mode 100644 index 0000000000000000000000000000000000000000..075cf10ff6811eeb2ce8f8a82f31f9799c0a4a00 --- /dev/null +++ b/crates/picker2/src/picker2.rs @@ -0,0 +1,163 @@ +use editor::Editor; +use gpui::{ + div, uniform_list, Component, Div, FocusEnabled, ParentElement, Render, StatefulInteractivity, + StatelessInteractive, Styled, Task, UniformListScrollHandle, View, ViewContext, VisualContext, + WindowContext, +}; +use std::cmp; + +pub struct Picker { + pub delegate: D, + scroll_handle: UniformListScrollHandle, + editor: View, + pending_update_matches: Option>>, +} + +pub trait PickerDelegate: Sized + 'static { + type ListItem: Component>; + + fn match_count(&self) -> usize; + fn selected_index(&self) -> usize; + fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext>); + + // fn placeholder_text(&self) -> Arc; + fn update_matches(&mut self, query: String, cx: &mut ViewContext>) -> Task<()>; + + fn confirm(&mut self, secondary: bool, cx: &mut ViewContext>); + fn dismissed(&mut self, cx: &mut ViewContext>); + + fn render_match( + &self, + ix: usize, + selected: bool, + cx: &mut ViewContext>, + ) -> Self::ListItem; +} + +impl Picker { + pub fn new(delegate: D, cx: &mut ViewContext) -> Self { + let editor = cx.build_view(|cx| Editor::single_line(cx)); + cx.subscribe(&editor, Self::on_input_editor_event).detach(); + Self { + delegate, + scroll_handle: UniformListScrollHandle::new(), + pending_update_matches: None, + editor, + } + } + + pub fn focus(&self, cx: &mut WindowContext) { + self.editor.update(cx, |editor, cx| editor.focus(cx)); + } + + fn select_next(&mut self, _: &menu::SelectNext, cx: &mut ViewContext) { + let count = self.delegate.match_count(); + if count > 0 { + let index = self.delegate.selected_index(); + let ix = cmp::min(index + 1, count - 1); + self.delegate.set_selected_index(ix, cx); + self.scroll_handle.scroll_to_item(ix); + } + } + + fn select_prev(&mut self, _: &menu::SelectPrev, cx: &mut ViewContext) { + let count = self.delegate.match_count(); + if count > 0 { + let index = self.delegate.selected_index(); + let ix = index.saturating_sub(1); + self.delegate.set_selected_index(ix, cx); + self.scroll_handle.scroll_to_item(ix); + } + } + + fn select_first(&mut self, _: &menu::SelectFirst, cx: &mut ViewContext) { + let count = self.delegate.match_count(); + if count > 0 { + self.delegate.set_selected_index(0, cx); + self.scroll_handle.scroll_to_item(0); + } + } + + fn select_last(&mut self, _: &menu::SelectLast, cx: &mut ViewContext) { + let count = self.delegate.match_count(); + if count > 0 { + self.delegate.set_selected_index(count - 1, cx); + self.scroll_handle.scroll_to_item(count - 1); + } + } + + fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext) { + self.delegate.dismissed(cx); + } + + fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext) { + self.delegate.confirm(false, cx); + } + + fn secondary_confirm(&mut self, _: &menu::SecondaryConfirm, cx: &mut ViewContext) { + self.delegate.confirm(true, cx); + } + + fn on_input_editor_event( + &mut self, + _: View, + event: &editor::Event, + cx: &mut ViewContext, + ) { + if let editor::Event::BufferEdited = event { + let query = self.editor.read(cx).text(cx); + self.update_matches(query, cx); + } + } + + pub fn update_matches(&mut self, query: String, cx: &mut ViewContext) { + let update = self.delegate.update_matches(query, cx); + self.matches_updated(cx); + self.pending_update_matches = Some(cx.spawn(|this, mut cx| async move { + update.await; + this.update(&mut cx, |this, cx| { + this.matches_updated(cx); + }) + .ok() + })); + } + + fn matches_updated(&mut self, cx: &mut ViewContext) { + let index = self.delegate.selected_index(); + self.scroll_handle.scroll_to_item(index); + self.pending_update_matches = None; + cx.notify(); + } +} + +impl Render for Picker { + type Element = Div, FocusEnabled>; + + fn render(&mut self, _cx: &mut ViewContext) -> Self::Element { + div() + .context("picker") + .id("picker-container") + .focusable() + .size_full() + .on_action(Self::select_next) + .on_action(Self::select_prev) + .on_action(Self::select_first) + .on_action(Self::select_last) + .on_action(Self::cancel) + .on_action(Self::confirm) + .on_action(Self::secondary_confirm) + .child(self.editor.clone()) + .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(), + ) + } +} diff --git a/crates/storybook2/Cargo.toml b/crates/storybook2/Cargo.toml index 1f3a0b33cc2254671069d8e8c1f0fb0e453db9ae..7c6776c9309fd68352b7e18b846af8cd4e8fbdae 100644 --- a/crates/storybook2/Cargo.toml +++ b/crates/storybook2/Cargo.toml @@ -13,9 +13,12 @@ anyhow.workspace = true # TODO: Remove after diagnosing stack overflow. backtrace-on-stack-overflow = "0.3.0" clap = { version = "4.4", features = ["derive", "string"] } +editor = { package = "editor2", path = "../editor2" } chrono = "0.4" +fuzzy = { package = "fuzzy2", path = "../fuzzy2" } gpui = { package = "gpui2", path = "../gpui2" } itertools = "0.11.0" +language = { package = "language2", path = "../language2" } log.workspace = true rust-embed.workspace = true serde.workspace = true @@ -25,8 +28,10 @@ smallvec.workspace = true strum = { version = "0.25.0", features = ["derive"] } theme = { path = "../theme" } theme2 = { path = "../theme2" } +menu = { package = "menu2", path = "../menu2" } ui = { package = "ui2", path = "../ui2", features = ["stories"] } util = { path = "../util" } +picker = { package = "picker2", path = "../picker2" } [dev-dependencies] gpui = { package = "gpui2", path = "../gpui2", features = ["test-support"] } diff --git a/crates/storybook2/src/stories.rs b/crates/storybook2/src/stories.rs index 3d8a332fb93b16ae8480acd32e97bbd4a0c5ab25..2620e68d6c102600637f41551e5d6f65b96451e1 100644 --- a/crates/storybook2/src/stories.rs +++ b/crates/storybook2/src/stories.rs @@ -1,6 +1,7 @@ mod colors; mod focus; mod kitchen_sink; +mod picker; mod scroll; mod text; mod z_index; @@ -8,6 +9,7 @@ mod z_index; pub use colors::*; pub use focus::*; pub use kitchen_sink::*; +pub use picker::*; pub use scroll::*; pub use text::*; pub use z_index::*; diff --git a/crates/storybook2/src/stories/focus.rs b/crates/storybook2/src/stories/focus.rs index d2d565aadafdeaa5082417b6186bbf10c35dab90..984ee421db3b357545de400547bd5057365610da 100644 --- a/crates/storybook2/src/stories/focus.rs +++ b/crates/storybook2/src/stories/focus.rs @@ -1,6 +1,6 @@ use gpui::{ actions, div, Div, FocusEnabled, Focusable, KeyBinding, ParentElement, Render, - StatefulInteraction, StatelessInteractive, Styled, View, VisualContext, WindowContext, + StatefulInteractivity, StatelessInteractive, Styled, View, VisualContext, WindowContext, }; use theme2::ActiveTheme; @@ -21,7 +21,7 @@ impl FocusStory { } impl Render for FocusStory { - type Element = Div, FocusEnabled>; + type Element = Div, FocusEnabled>; fn render(&mut self, cx: &mut gpui::ViewContext) -> Self::Element { let theme = cx.theme(); @@ -38,20 +38,18 @@ impl Render for FocusStory { .id("parent") .focusable() .context("parent") - .on_action(|_, action: &ActionA, phase, cx| { - println!("Action A dispatched on parent during {:?}", phase); + .on_action(|_, action: &ActionA, cx| { + println!("Action A dispatched on parent during"); }) - .on_action(|_, action: &ActionB, phase, cx| { - println!("Action B dispatched on parent during {:?}", phase); + .on_action(|_, action: &ActionB, cx| { + println!("Action B dispatched on parent during"); }) .on_focus(|_, _, _| println!("Parent focused")) .on_blur(|_, _, _| println!("Parent blurred")) .on_focus_in(|_, _, _| println!("Parent focus_in")) .on_focus_out(|_, _, _| println!("Parent focus_out")) - .on_key_down(|_, event, phase, _| { - println!("Key down on parent {:?} {:?}", phase, event) - }) - .on_key_up(|_, event, phase, _| println!("Key up on parent {:?} {:?}", phase, event)) + .on_key_down(|_, event, phase, _| println!("Key down on parent {:?}", event)) + .on_key_up(|_, event, phase, _| println!("Key up on parent {:?}", event)) .size_full() .bg(color_1) .focus(|style| style.bg(color_2)) @@ -60,8 +58,8 @@ impl Render for FocusStory { div() .track_focus(&child_1) .context("child-1") - .on_action(|_, action: &ActionB, phase, cx| { - println!("Action B dispatched on child 1 during {:?}", phase); + .on_action(|_, action: &ActionB, cx| { + println!("Action B dispatched on child 1 during"); }) .w_full() .h_6() @@ -72,20 +70,16 @@ impl Render for FocusStory { .on_blur(|_, _, _| println!("Child 1 blurred")) .on_focus_in(|_, _, _| println!("Child 1 focus_in")) .on_focus_out(|_, _, _| println!("Child 1 focus_out")) - .on_key_down(|_, event, phase, _| { - println!("Key down on child 1 {:?} {:?}", phase, event) - }) - .on_key_up(|_, event, phase, _| { - println!("Key up on child 1 {:?} {:?}", phase, event) - }) + .on_key_down(|_, event, phase, _| println!("Key down on child 1 {:?}", event)) + .on_key_up(|_, event, phase, _| println!("Key up on child 1 {:?}", event)) .child("Child 1"), ) .child( div() .track_focus(&child_2) .context("child-2") - .on_action(|_, action: &ActionC, phase, cx| { - println!("Action C dispatched on child 2 during {:?}", phase); + .on_action(|_, action: &ActionC, cx| { + println!("Action C dispatched on child 2 during"); }) .w_full() .h_6() @@ -94,12 +88,8 @@ impl Render for FocusStory { .on_blur(|_, _, _| println!("Child 2 blurred")) .on_focus_in(|_, _, _| println!("Child 2 focus_in")) .on_focus_out(|_, _, _| println!("Child 2 focus_out")) - .on_key_down(|_, event, phase, _| { - println!("Key down on child 2 {:?} {:?}", phase, event) - }) - .on_key_up(|_, event, phase, _| { - println!("Key up on child 2 {:?} {:?}", phase, event) - }) + .on_key_down(|_, event, phase, _| println!("Key down on child 2 {:?}", event)) + .on_key_up(|_, event, phase, _| println!("Key up on child 2 {:?}", event)) .child("Child 2"), ) } diff --git a/crates/storybook2/src/stories/kitchen_sink.rs b/crates/storybook2/src/stories/kitchen_sink.rs index 54d6f2a3a967cc3f6e5a29be97e36291c45bdeff..6831ae27220ca4691f5f8ac371c0f43e6dd7b013 100644 --- a/crates/storybook2/src/stories/kitchen_sink.rs +++ b/crates/storybook2/src/stories/kitchen_sink.rs @@ -1,5 +1,5 @@ use crate::{story::Story, story_selector::ComponentStory}; -use gpui::{Div, Render, StatefulInteraction, View, VisualContext}; +use gpui::{Div, Render, StatefulInteractivity, View, VisualContext}; use strum::IntoEnumIterator; use ui::prelude::*; @@ -12,7 +12,7 @@ impl KitchenSinkStory { } impl Render for KitchenSinkStory { - type Element = Div>; + type Element = Div>; fn render(&mut self, cx: &mut ViewContext) -> Self::Element { let component_stories = ComponentStory::iter() diff --git a/crates/storybook2/src/stories/picker.rs b/crates/storybook2/src/stories/picker.rs new file mode 100644 index 0000000000000000000000000000000000000000..82a010e6b30e820681b903232b74c35cad6b8584 --- /dev/null +++ b/crates/storybook2/src/stories/picker.rs @@ -0,0 +1,214 @@ +use std::sync::Arc; + +use fuzzy::StringMatchCandidate; +use gpui::{ + div, Component, Div, KeyBinding, ParentElement, Render, StatelessInteractive, Styled, Task, + View, VisualContext, WindowContext, +}; +use picker::{Picker, PickerDelegate}; +use theme2::ActiveTheme; + +pub struct PickerStory { + picker: View>, +} + +struct Delegate { + candidates: Arc<[StringMatchCandidate]>, + matches: Vec, + selected_ix: usize, +} + +impl Delegate { + fn new(strings: &[&str]) -> Self { + Self { + candidates: strings + .iter() + .copied() + .enumerate() + .map(|(id, string)| StringMatchCandidate { + id, + char_bag: string.into(), + string: string.into(), + }) + .collect(), + matches: vec![], + selected_ix: 0, + } + } +} + +impl PickerDelegate for Delegate { + type ListItem = Div>; + + fn match_count(&self) -> usize { + self.candidates.len() + } + + fn render_match( + &self, + ix: usize, + selected: bool, + cx: &mut gpui::ViewContext>, + ) -> Self::ListItem { + let colors = cx.theme().colors(); + let Some(candidate_ix) = self.matches.get(ix) else { + return div(); + }; + let candidate = self.candidates[*candidate_ix].string.clone(); + + div() + .text_color(colors.text) + .when(selected, |s| { + s.border_l_10().border_color(colors.terminal_ansi_yellow) + }) + .hover(|style| { + style + .bg(colors.element_active) + .text_color(colors.text_accent) + }) + .child(candidate) + } + + fn selected_index(&self) -> usize { + self.selected_ix + } + + fn set_selected_index(&mut self, ix: usize, cx: &mut gpui::ViewContext>) { + self.selected_ix = ix; + cx.notify(); + } + + fn confirm(&mut self, secondary: bool, cx: &mut gpui::ViewContext>) { + let candidate_ix = self.matches[self.selected_ix]; + let candidate = self.candidates[candidate_ix].string.clone(); + + if secondary { + eprintln!("Secondary confirmed {}", candidate) + } else { + eprintln!("Confirmed {}", candidate) + } + } + + fn dismissed(&mut self, cx: &mut gpui::ViewContext>) { + cx.quit(); + } + + fn update_matches( + &mut self, + query: String, + cx: &mut gpui::ViewContext>, + ) -> Task<()> { + let candidates = self.candidates.clone(); + self.matches = cx + .background_executor() + .block(fuzzy::match_strings( + &candidates, + &query, + true, + 100, + &Default::default(), + cx.background_executor().clone(), + )) + .into_iter() + .map(|r| r.candidate_id) + .collect(); + self.selected_ix = 0; + Task::ready(()) + } +} + +impl PickerStory { + pub fn new(cx: &mut WindowContext) -> View { + cx.build_view(|cx| { + cx.bind_keys([ + KeyBinding::new("up", menu::SelectPrev, Some("picker")), + KeyBinding::new("pageup", menu::SelectFirst, Some("picker")), + KeyBinding::new("shift-pageup", menu::SelectFirst, Some("picker")), + KeyBinding::new("ctrl-p", menu::SelectPrev, Some("picker")), + KeyBinding::new("down", menu::SelectNext, Some("picker")), + KeyBinding::new("pagedown", menu::SelectLast, Some("picker")), + KeyBinding::new("shift-pagedown", menu::SelectFirst, Some("picker")), + KeyBinding::new("ctrl-n", menu::SelectNext, Some("picker")), + KeyBinding::new("cmd-up", menu::SelectFirst, Some("picker")), + KeyBinding::new("cmd-down", menu::SelectLast, Some("picker")), + KeyBinding::new("enter", menu::Confirm, Some("picker")), + KeyBinding::new("ctrl-enter", menu::ShowContextMenu, Some("picker")), + KeyBinding::new("cmd-enter", menu::SecondaryConfirm, Some("picker")), + KeyBinding::new("escape", menu::Cancel, Some("picker")), + KeyBinding::new("ctrl-c", menu::Cancel, Some("picker")), + ]); + + PickerStory { + picker: cx.build_view(|cx| { + let mut delegate = Delegate::new(&[ + "Baguette (France)", + "Baklava (Turkey)", + "Beef Wellington (UK)", + "Biryani (India)", + "Borscht (Ukraine)", + "Bratwurst (Germany)", + "Bulgogi (Korea)", + "Burrito (USA)", + "Ceviche (Peru)", + "Chicken Tikka Masala (India)", + "Churrasco (Brazil)", + "Couscous (North Africa)", + "Croissant (France)", + "Dim Sum (China)", + "Empanada (Argentina)", + "Fajitas (Mexico)", + "Falafel (Middle East)", + "Feijoada (Brazil)", + "Fish and Chips (UK)", + "Fondue (Switzerland)", + "Goulash (Hungary)", + "Haggis (Scotland)", + "Kebab (Middle East)", + "Kimchi (Korea)", + "Lasagna (Italy)", + "Maple Syrup Pancakes (Canada)", + "Moussaka (Greece)", + "Pad Thai (Thailand)", + "Paella (Spain)", + "Pancakes (USA)", + "Pasta Carbonara (Italy)", + "Pavlova (Australia)", + "Peking Duck (China)", + "Pho (Vietnam)", + "Pierogi (Poland)", + "Pizza (Italy)", + "Poutine (Canada)", + "Pretzel (Germany)", + "Ramen (Japan)", + "Rendang (Indonesia)", + "Sashimi (Japan)", + "Satay (Indonesia)", + "Shepherd's Pie (Ireland)", + "Sushi (Japan)", + "Tacos (Mexico)", + "Tandoori Chicken (India)", + "Tortilla (Spain)", + "Tzatziki (Greece)", + "Wiener Schnitzel (Austria)", + ]); + delegate.update_matches("".into(), cx).detach(); + + let picker = Picker::new(delegate, cx); + picker.focus(cx); + picker + }), + } + }) + } +} + +impl Render for PickerStory { + type Element = Div; + + fn render(&mut self, cx: &mut gpui::ViewContext) -> Self::Element { + div() + .bg(cx.theme().styles.colors.background) + .size_full() + .child(self.picker.clone()) + } +} diff --git a/crates/storybook2/src/stories/scroll.rs b/crates/storybook2/src/stories/scroll.rs index 744d0206f9a8c7f12aae1363e448acb8e448d2a9..296dc50cb491a1def608c050f65cf7ff33cedb2b 100644 --- a/crates/storybook2/src/stories/scroll.rs +++ b/crates/storybook2/src/stories/scroll.rs @@ -1,5 +1,5 @@ use gpui::{ - div, px, Component, Div, ParentElement, Render, SharedString, StatefulInteraction, Styled, + div, px, Component, Div, ParentElement, Render, SharedString, StatefulInteractivity, Styled, View, VisualContext, WindowContext, }; use theme2::ActiveTheme; @@ -13,7 +13,7 @@ impl ScrollStory { } impl Render for ScrollStory { - type Element = Div>; + type Element = Div>; fn render(&mut self, cx: &mut gpui::ViewContext) -> Self::Element { let theme = cx.theme(); diff --git a/crates/storybook2/src/story_selector.rs b/crates/storybook2/src/story_selector.rs index 47807def2515e955fc8ea8733badadb761d1d923..040bd75189104aeb8d1a5437e96b7a3935d9efcc 100644 --- a/crates/storybook2/src/story_selector.rs +++ b/crates/storybook2/src/story_selector.rs @@ -52,6 +52,7 @@ pub enum ComponentStory { TrafficLights, Workspace, ZIndex, + Picker, } impl ComponentStory { @@ -96,6 +97,7 @@ impl ComponentStory { Self::TrafficLights => cx.build_view(|_| ui::TrafficLightsStory).into(), Self::Workspace => ui::WorkspaceStory::view(cx).into(), Self::ZIndex => cx.build_view(|_| ZIndexStory).into(), + Self::Picker => PickerStory::new(cx).into(), } } } diff --git a/crates/storybook2/src/storybook2.rs b/crates/storybook2/src/storybook2.rs index c8849c134278ebe36d583c34a14b6e8961f4885b..f0ba124162d546bcdbdc1fce12e986364f950dad 100644 --- a/crates/storybook2/src/storybook2.rs +++ b/crates/storybook2/src/storybook2.rs @@ -72,6 +72,8 @@ fn main() { ThemeSettings::override_global(theme_settings, cx); ui::settings::init(cx); + language::init(cx); + editor::init(cx); let window = cx.open_window( WindowOptions { diff --git a/crates/terminal2/src/mappings/mouse.rs b/crates/terminal2/src/mappings/mouse.rs index eac6ad17ff257384396c4c639dd6f7ea39acea5f..edced3156faf90cdf9dc227ee646f0e4cccb68e3 100644 --- a/crates/terminal2/src/mappings/mouse.rs +++ b/crates/terminal2/src/mappings/mouse.rs @@ -186,9 +186,9 @@ pub fn mouse_side( } pub fn grid_point(pos: Point, cur_size: TerminalSize, display_offset: usize) -> AlacPoint { - let col = GridCol((pos.x / cur_size.cell_width).as_usize()); + let col = GridCol((cur_size.cell_width / pos.x) as usize); let col = min(col, cur_size.last_column()); - let line = (pos.y / cur_size.line_height).as_isize() as i32; + let line = (cur_size.line_height / pos.y) as i32; let line = min(line, cur_size.bottommost_line().0); AlacPoint::new(GridLine(line - display_offset as i32), col) } diff --git a/crates/terminal2/src/terminal2.rs b/crates/terminal2/src/terminal2.rs index ba5c4815f29253152c355c1ba4d998e54c2fc9e5..3d06b488123016c19102e8d911d6b24568124ab1 100644 --- a/crates/terminal2/src/terminal2.rs +++ b/crates/terminal2/src/terminal2.rs @@ -1121,8 +1121,7 @@ impl Terminal { None => return, }; - let scroll_lines = - (scroll_delta / self.last_content.size.line_height).as_isize() as i32; + let scroll_lines = (scroll_delta / self.last_content.size.line_height) as i32; self.events .push_back(InternalEvent::Scroll(AlacScroll::Delta(scroll_lines))); @@ -1280,11 +1279,11 @@ impl Terminal { } /* Calculate the appropriate scroll lines */ TouchPhase::Moved => { - let old_offset = (self.scroll_px / line_height).as_isize() as i32; + let old_offset = (self.scroll_px / line_height) as i32; self.scroll_px += e.delta.pixel_delta(line_height).y * scroll_multiplier; - let new_offset = (self.scroll_px / line_height).as_isize() as i32; + let new_offset = (self.scroll_px / line_height) as i32; // Whenever we hit the edges, reset our stored scroll to 0 // so we can respond to changes in direction quickly @@ -1396,9 +1395,9 @@ fn all_search_matches<'a, T>( } fn content_index_for_mouse(pos: Point, size: &TerminalSize) -> usize { - let col = (pos.x / size.cell_width()).round().as_usize(); + let col = (pos.x / size.cell_width()).round() as usize; let clamped_col = min(col, size.columns() - 1); - let row = (pos.y / size.line_height()).round().as_usize(); + let row = (pos.y / size.line_height()).round() as usize; let clamped_row = min(row, size.screen_lines() - 1); clamped_row * size.columns() + clamped_col } diff --git a/crates/ui2/src/components/checkbox.rs b/crates/ui2/src/components/checkbox.rs index 9f7c10a10433eef59249e3ab0df408b94a8a2aff..20dad747124ca2a652679324f04590dbf9d3f05d 100644 --- a/crates/ui2/src/components/checkbox.rs +++ b/crates/ui2/src/components/checkbox.rs @@ -128,7 +128,7 @@ impl Checkbox { // click area for the checkbox. .size_5() // Because we've enlarged the click area, we need to create a - // `group` to pass down interaction events to the checkbox. + // `group` to pass down interactivity events to the checkbox. .group(group_id.clone()) .child( div() @@ -148,7 +148,7 @@ impl Checkbox { .bg(bg_color) .border() .border_color(border_color) - // We only want the interaction states to fire when we + // We only want the interactivity states to fire when we // are in a checkbox that isn't disabled. .when(!self.disabled, |this| { // Here instead of `hover()` we use `group_hover()` diff --git a/crates/workspace2/src/modal_layer.rs b/crates/workspace2/src/modal_layer.rs index 01f940273a6cb7f92505b2438f1a7dad2dcc9b43..4121574b6a490867ec1dd612aca0b417cd138b6f 100644 --- a/crates/workspace2/src/modal_layer.rs +++ b/crates/workspace2/src/modal_layer.rs @@ -1,11 +1,8 @@ -use std::{any::TypeId, sync::Arc}; - +use crate::Workspace; use gpui::{ - div, AnyView, AppContext, DispatchPhase, Div, ParentElement, Render, StatelessInteractive, - View, ViewContext, + div, AnyView, AppContext, Div, ParentElement, Render, StatelessInteractive, View, ViewContext, }; - -use crate::Workspace; +use std::{any::TypeId, sync::Arc}; pub struct ModalRegistry { registered_modals: Vec<(TypeId, Box) -> Div>)>, @@ -42,15 +39,7 @@ impl ModalRegistry { let build_view = build_view.clone(); div.on_action( - move |workspace: &mut Workspace, - event: &A, - phase: DispatchPhase, - cx: &mut ViewContext| { - dbg!("GOT HERE"); - if phase == DispatchPhase::Capture { - return; - } - + move |workspace: &mut Workspace, event: &A, cx: &mut ViewContext| { let new_modal = (build_view)(workspace, cx); workspace.modal_layer.update(cx, |modal_layer, _| { modal_layer.open_modal = Some(new_modal.into()); diff --git a/crates/zed/src/languages/racket/highlights.scm b/crates/zed/src/languages/racket/highlights.scm index 2c0caf89357cfbe8f966bffbbc712272b3c1e59d..3caf1d88e97dd0fcdc2e08d6ef866ad74c436a31 100644 --- a/crates/zed/src/languages/racket/highlights.scm +++ b/crates/zed/src/languages/racket/highlights.scm @@ -37,4 +37,3 @@ ((symbol) @comment (#match? @comment "^#[cC][iIsS]$")) - diff --git a/crates/zed2/src/languages/racket/highlights.scm b/crates/zed2/src/languages/racket/highlights.scm index 2c0caf89357cfbe8f966bffbbc712272b3c1e59d..3caf1d88e97dd0fcdc2e08d6ef866ad74c436a31 100644 --- a/crates/zed2/src/languages/racket/highlights.scm +++ b/crates/zed2/src/languages/racket/highlights.scm @@ -37,4 +37,3 @@ ((symbol) @comment (#match? @comment "^#[cC][iIsS]$")) -