From 19c1a54fea9a7114f151a52e6cca3ec17d843bb5 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 17 Oct 2023 18:45:01 +0200 Subject: [PATCH] WIP --- crates/gpui3/src/elements/div2.rs | 144 ++++++++++++++++++++--- crates/gpui3/src/elements/layout_node.rs | 2 +- 2 files changed, 129 insertions(+), 17 deletions(-) diff --git a/crates/gpui3/src/elements/div2.rs b/crates/gpui3/src/elements/div2.rs index f198711c16f0713d00af9aa370a9c0832d7e8a64..5c8d44c7186683cc85e5deaf305f58265a4df1af 100644 --- a/crates/gpui3/src/elements/div2.rs +++ b/crates/gpui3/src/elements/div2.rs @@ -1,7 +1,8 @@ use crate::{ AnonymousElementKind, AnyElement, AppContext, BorrowWindow, Bounds, DispatchPhase, Element, - ElementId, ElementKind, IntoAnyElement, LayoutId, MouseDownEvent, MouseMoveEvent, MouseUpEvent, - Pixels, ScrollWheelEvent, SharedString, Style, StyleRefinement, ViewContext, + ElementId, ElementKind, IdentifiedElement, IdentifiedElementKind, IntoAnyElement, LayoutId, + MouseDownEvent, MouseMoveEvent, MouseUpEvent, Overflow, Pixels, Point, ScrollWheelEvent, + SharedString, Style, StyleRefinement, ViewContext, }; use collections::HashMap; use parking_lot::Mutex; @@ -37,6 +38,44 @@ pub fn group_bounds(name: &SharedString, cx: &mut AppContext) -> Option>>); + +impl ScrollState { + pub fn x(&self) -> Pixels { + self.0.lock().x + } + + pub fn set_x(&self, value: Pixels) { + self.0.lock().x = value; + } + + pub fn y(&self) -> Pixels { + self.0.lock().y + } + + pub fn set_y(&self, value: Pixels) { + self.0.lock().y = value; + } +} + +pub fn div() -> Div +where + S: 'static + Send + Sync, +{ + Div { + kind: AnonymousElementKind, + children: SmallVec::new(), + group: None, + base_style: StyleRefinement::default(), + hover_style: StyleRefinement::default(), + group_hover: None, + active_style: StyleRefinement::default(), + group_active: None, + listeners: MouseEventListeners::default(), + } +} + pub struct Div { kind: K, children: SmallVec<[AnyElement; 2]>, @@ -54,11 +93,78 @@ struct GroupStyle { style: StyleRefinement, } +impl Div +where + V: 'static + Send + Sync, +{ + pub fn id(self, id: ElementId) -> Div { + Div { + kind: IdentifiedElementKind(id), + children: self.children, + group: self.group, + base_style: self.base_style, + hover_style: self.hover_style, + group_hover: self.group_hover, + active_style: self.active_style, + group_active: self.group_active, + listeners: self.listeners, + } + } +} + impl Div where V: 'static + Send + Sync, K: ElementKind, { + pub fn group(mut self, group: impl Into) -> Self { + self.group = Some(group.into()); + self + } + + pub fn z_index(mut self, z_index: u32) -> Self { + self.base_style.z_index = Some(z_index); + self + } + + pub fn overflow_hidden(mut self) -> Self { + self.base_style.overflow.x = Some(Overflow::Hidden); + self.base_style.overflow.y = Some(Overflow::Hidden); + self + } + + pub fn overflow_hidden_x(mut self) -> Self { + self.base_style.overflow.x = Some(Overflow::Hidden); + self + } + + pub fn overflow_hidden_y(mut self) -> Self { + self.base_style.overflow.y = Some(Overflow::Hidden); + self + } + + pub fn overflow_scroll(mut self, _scroll_state: ScrollState) -> Self { + // todo!("impl scrolling") + // self.scroll_state = Some(scroll_state); + self.base_style.overflow.x = Some(Overflow::Scroll); + self.base_style.overflow.y = Some(Overflow::Scroll); + self + } + + pub fn overflow_x_scroll(mut self, _scroll_state: ScrollState) -> Self { + // todo!("impl scrolling") + // self.scroll_state = Some(scroll_state); + self.base_style.overflow.x = Some(Overflow::Scroll); + self + } + + pub fn overflow_y_scroll(mut self, _scroll_state: ScrollState) -> Self { + // todo!("impl scrolling") + // self.scroll_state = Some(scroll_state); + self.base_style.overflow.y = Some(Overflow::Scroll); + self + } + fn with_element_id( &mut self, cx: &mut ViewContext, @@ -206,20 +312,6 @@ where } } -fn paint_hover_listener(bounds: Bounds, cx: &mut ViewContext) -where - V: 'static + Send + Sync, -{ - let hovered = bounds.contains_point(cx.mouse_position()); - cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| { - if phase == DispatchPhase::Capture { - if bounds.contains_point(event.position) != hovered { - cx.notify(); - } - } - }); -} - impl Element for Div where V: 'static + Send + Sync, @@ -321,6 +413,12 @@ where } } +impl IdentifiedElement for Div { + fn id(&self) -> ElementId { + self.kind.0.clone() + } +} + impl IntoAnyElement for Div where V: 'static + Send + Sync, @@ -384,3 +482,17 @@ impl Default for MouseEventListeners { } } } + +fn paint_hover_listener(bounds: Bounds, cx: &mut ViewContext) +where + V: 'static + Send + Sync, +{ + let hovered = bounds.contains_point(cx.mouse_position()); + cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| { + if phase == DispatchPhase::Capture { + if bounds.contains_point(event.position) != hovered { + cx.notify(); + } + } + }); +} diff --git a/crates/gpui3/src/elements/layout_node.rs b/crates/gpui3/src/elements/layout_node.rs index d84c5f71bae74fc369a2a056ad1c977ac090bff3..ce98711def78f87eb9bd593c48aca7d982277252 100644 --- a/crates/gpui3/src/elements/layout_node.rs +++ b/crates/gpui3/src/elements/layout_node.rs @@ -21,7 +21,7 @@ pub trait ElementKind: 'static + Send + Sync { fn id(&self) -> Option; } -pub struct IdentifiedElementKind(ElementId); +pub struct IdentifiedElementKind(pub(crate) ElementId); pub struct AnonymousElementKind; impl ElementKind for IdentifiedElementKind {