From 746f77bf7c557a4dcc9f92ab65259709ec14aace Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 30 Aug 2023 14:40:43 -0600 Subject: [PATCH] Checkpoint --- Cargo.lock | 24 +- Cargo.toml | 3 +- crates/gpui/playground/Cargo.toml | 2 +- crates/gpui/playground/src/components.rs | 2 +- crates/gpui/playground/src/style.rs | 2 +- crates/gpui/playground/src/workspace.rs | 2 +- crates/gpui2/Cargo.toml | 9 + crates/gpui2/src/lib.rs | 14 + .../Cargo.toml | 4 +- .../src/derive_element.rs | 0 .../src/derive_into_element.rs | 0 .../src/gpui2_macros.rs} | 0 .../src/styleable_helpers.rs | 0 test.rs | 414 ++++++++---------- 14 files changed, 226 insertions(+), 250 deletions(-) create mode 100644 crates/gpui2/Cargo.toml create mode 100644 crates/gpui2/src/lib.rs rename crates/{gpui/playground_macros => gpui2_macros}/Cargo.toml (71%) rename crates/{gpui/playground_macros => gpui2_macros}/src/derive_element.rs (100%) rename crates/{gpui/playground_macros => gpui2_macros}/src/derive_into_element.rs (100%) rename crates/{gpui/playground_macros/src/playground_macros.rs => gpui2_macros/src/gpui2_macros.rs} (100%) rename crates/{gpui/playground_macros => gpui2_macros}/src/styleable_helpers.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 3fb2915d6d..cc02b70783 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3163,6 +3163,19 @@ dependencies = [ "waker-fn", ] +[[package]] +name = "gpui2" +version = "0.1.0" + +[[package]] +name = "gpui2_macros" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "gpui_macros" version = "0.1.0" @@ -5172,9 +5185,9 @@ dependencies = [ "anyhow", "derive_more", "gpui", + "gpui2_macros", "log", "parking_lot 0.11.2", - "playground_macros", "refineable", "rust-embed", "serde", @@ -5185,15 +5198,6 @@ dependencies = [ "util", ] -[[package]] -name = "playground_macros" -version = "0.1.0" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "plist" version = "1.5.0" diff --git a/Cargo.toml b/Cargo.toml index 5938ecb402..119d0c8947 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,9 @@ members = [ "crates/go_to_line", "crates/gpui", "crates/gpui/playground", - "crates/gpui/playground_macros", "crates/gpui_macros", + "crates/gpui2", + "crates/gpui2_macros", "crates/install_cli", "crates/journal", "crates/language", diff --git a/crates/gpui/playground/Cargo.toml b/crates/gpui/playground/Cargo.toml index 1fee470d3d..a817387dc0 100644 --- a/crates/gpui/playground/Cargo.toml +++ b/crates/gpui/playground/Cargo.toml @@ -13,7 +13,7 @@ anyhow.workspace = true derive_more.workspace = true gpui = { path = ".." } log.workspace = true -playground_macros = { path = "../playground_macros" } +gpui2_macros = { path = "../../gpui2_macros" } parking_lot.workspace = true refineable.workspace = true rust-embed.workspace = true diff --git a/crates/gpui/playground/src/components.rs b/crates/gpui/playground/src/components.rs index 8513a550c1..b2492a5292 100644 --- a/crates/gpui/playground/src/components.rs +++ b/crates/gpui/playground/src/components.rs @@ -7,7 +7,7 @@ use crate::{ // themes::Theme, }; use gpui::{platform::MouseButton, ViewContext}; -use playground_macros::Element; +use gpui2_macros::Element; use std::{marker::PhantomData, rc::Rc}; struct ButtonHandlers { diff --git a/crates/gpui/playground/src/style.rs b/crates/gpui/playground/src/style.rs index d2f639fdc2..f0b631993d 100644 --- a/crates/gpui/playground/src/style.rs +++ b/crates/gpui/playground/src/style.rs @@ -16,7 +16,7 @@ use gpui::{ }, taffy, }; -use playground_macros::styleable_helpers; +use gpui2_macros::styleable_helpers; use refineable::{Refineable, RefinementCascade}; #[derive(Clone, Refineable)] diff --git a/crates/gpui/playground/src/workspace.rs b/crates/gpui/playground/src/workspace.rs index 36a3caf1b7..c0185889c8 100644 --- a/crates/gpui/playground/src/workspace.rs +++ b/crates/gpui/playground/src/workspace.rs @@ -5,7 +5,7 @@ use crate::{ themes::theme, }; use gpui::{geometry::pixels, ViewContext}; -use playground_macros::Element; +use gpui2_macros::Element; use crate as playground; #[derive(Element)] diff --git a/crates/gpui2/Cargo.toml b/crates/gpui2/Cargo.toml new file mode 100644 index 0000000000..e7d6ba9f23 --- /dev/null +++ b/crates/gpui2/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "gpui2" +version = "0.1.0" +edition = "2021" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/crates/gpui2/src/lib.rs b/crates/gpui2/src/lib.rs new file mode 100644 index 0000000000..7d12d9af81 --- /dev/null +++ b/crates/gpui2/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/crates/gpui/playground_macros/Cargo.toml b/crates/gpui2_macros/Cargo.toml similarity index 71% rename from crates/gpui/playground_macros/Cargo.toml rename to crates/gpui2_macros/Cargo.toml index f85a622edd..eb44334095 100644 --- a/crates/gpui/playground_macros/Cargo.toml +++ b/crates/gpui2_macros/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "playground_macros" +name = "gpui2_macros" version = "0.1.0" edition = "2021" publish = false [lib] -path = "src/playground_macros.rs" +path = "src/gpui2_macros.rs" proc-macro = true [dependencies] diff --git a/crates/gpui/playground_macros/src/derive_element.rs b/crates/gpui2_macros/src/derive_element.rs similarity index 100% rename from crates/gpui/playground_macros/src/derive_element.rs rename to crates/gpui2_macros/src/derive_element.rs diff --git a/crates/gpui/playground_macros/src/derive_into_element.rs b/crates/gpui2_macros/src/derive_into_element.rs similarity index 100% rename from crates/gpui/playground_macros/src/derive_into_element.rs rename to crates/gpui2_macros/src/derive_into_element.rs diff --git a/crates/gpui/playground_macros/src/playground_macros.rs b/crates/gpui2_macros/src/gpui2_macros.rs similarity index 100% rename from crates/gpui/playground_macros/src/playground_macros.rs rename to crates/gpui2_macros/src/gpui2_macros.rs diff --git a/crates/gpui/playground_macros/src/styleable_helpers.rs b/crates/gpui2_macros/src/styleable_helpers.rs similarity index 100% rename from crates/gpui/playground_macros/src/styleable_helpers.rs rename to crates/gpui2_macros/src/styleable_helpers.rs diff --git a/test.rs b/test.rs index 1347f5dba0..974c37c477 100644 --- a/test.rs +++ b/test.rs @@ -17,10 +17,10 @@ use simplelog::SimpleLogger; use themes::{rose_pine, ThemeColors}; use view::view; mod adapter { + use crate::element::AnyElement; use crate::element::{LayoutContext, PaintContext}; use gpui::{geometry::rect::RectF, LayoutEngine}; use util::ResultExt; - use crate::element::AnyElement; pub struct Adapter(pub(crate) AnyElement); impl gpui::Element for Adapter { type LayoutState = Option; @@ -90,8 +90,8 @@ mod adapter { } mod color { #![allow(dead_code)] - use std::{num::ParseIntError, ops::Range}; use smallvec::SmallVec; + use std::{num::ParseIntError, ops::Range}; pub fn rgb>(hex: u32) -> C { let r = ((hex >> 16) & 0xFF) as f32 / 255.0; let g = ((hex >> 8) & 0xFF) as f32 / 255.0; @@ -130,16 +130,7 @@ mod color { impl ::core::fmt::Debug for Rgba { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { ::core::fmt::Formatter::debug_struct_field4_finish( - f, - "Rgba", - "r", - &self.r, - "g", - &self.g, - "b", - &self.b, - "a", - &&self.a, + f, "Rgba", "r", &self.r, "g", &self.g, "b", &self.b, "a", &&self.a, ) } } @@ -185,7 +176,12 @@ mod color { 4 => (xm, m, cm), _ => (cm, m, xm), }; - Rgba { r, g, b, a: color.a } + Rgba { + r, + g, + b, + a: color.a, + } } } impl TryFrom<&'_ str> for Rgba { @@ -239,16 +235,7 @@ mod color { impl ::core::fmt::Debug for Hsla { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { ::core::fmt::Formatter::debug_struct_field4_finish( - f, - "Hsla", - "h", - &self.h, - "s", - &self.s, - "l", - &self.l, - "a", - &&self.a, + f, "Hsla", "h", &self.h, "s", &self.s, "l", &self.l, "a", &&self.a, ) } } @@ -258,8 +245,7 @@ mod color { impl ::core::cmp::PartialEq for Hsla { #[inline] fn eq(&self, other: &Hsla) -> bool { - self.h == other.h && self.s == other.s && self.l == other.l - && self.a == other.a + self.h == other.h && self.s == other.s && self.l == other.l && self.a == other.a } } pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Hsla { @@ -271,7 +257,12 @@ mod color { } } pub fn black() -> Hsla { - Hsla { h: 0., s: 0., l: 0., a: 1. } + Hsla { + h: 0., + s: 0., + l: 0., + a: 1., + } } impl From for Hsla { fn from(color: Rgba) -> Self { @@ -298,7 +289,12 @@ mod color { } else { ((r - g) / delta + 4.0) / 6.0 }; - Hsla { h, s, l, a: color.a } + Hsla { + h, + s, + l, + a: color.a, + } } } impl Hsla { @@ -364,8 +360,7 @@ mod color { positions: SmallVec::new(), }; let num_colors: f32 = scale.colors.len() as f32 - 1.0; - scale - .positions = (0..scale.colors.len()) + scale.positions = (0..scale.colors.len()) .map(|i| i as f32 / num_colors) .collect(); scale @@ -375,12 +370,10 @@ mod color { if true { if !(0.0 <= t && t <= 1.0) { { - ::core::panicking::panic_fmt( - format_args!( - "t value {0} is out of range. Expected value in range 0.0 to 1.0", - t, - ), - ); + ::core::panicking::panic_fmt(format_args!( + "t value {0} is out of range. Expected value in range 0.0 to 1.0", + t, + )); } } } @@ -412,10 +405,12 @@ mod color { mod components { use crate::{ element::{Element, ElementMetadata}, - frame, text::ArcCow, themes::rose_pine, + frame, + text::ArcCow, + themes::rose_pine, }; use gpui::{platform::MouseButton, ViewContext}; - use playground_macros::Element; + use gpui2_macros::Element; use std::{marker::PhantomData, rc::Rc}; struct ButtonHandlers { click: Option)>>, @@ -498,18 +493,11 @@ mod components { self.icon = Some(icon.into()); self } - pub fn click( - self, - handler: impl Fn(&mut V, &D, &mut ViewContext) + 'static, - ) -> Self { + pub fn click(self, handler: impl Fn(&mut V, &D, &mut ViewContext) + 'static) -> Self { let data = self.data.clone(); - Element::click( - self, - MouseButton::Left, - move |view, _, cx| { - handler(view, data.as_ref(), cx); - }, - ) + Element::click(self, MouseButton::Left, move |view, _, cx| { + handler(view, data.as_ref(), cx); + }) } } pub fn button() -> Button { @@ -523,11 +511,9 @@ mod components { .children(self.label.clone()); if let Some(handler) = self.handlers.click.clone() { let data = self.data.clone(); - button - .mouse_down( - MouseButton::Left, - move |view, event, cx| { handler(view, data.as_ref(), cx) }, - ) + button.mouse_down(MouseButton::Left, move |view, event, cx| { + handler(view, data.as_ref(), cx) + }) } else { button } @@ -535,8 +521,11 @@ mod components { } } mod element { + pub use crate::paint_context::PaintContext; use crate::{ - adapter::Adapter, color::Hsla, hoverable::Hoverable, + adapter::Adapter, + color::Hsla, + hoverable::Hoverable, style::{Display, Fill, OptionalStyle, Overflow, Position}, }; use anyhow::Result; @@ -546,12 +535,12 @@ mod element { platform::{MouseButton, MouseButtonEvent}, EngineLayout, EventContext, RenderContext, ViewContext, }; - use playground_macros::tailwind_lengths; + use gpui2_macros::tailwind_lengths; use std::{ any::{Any, TypeId}, - cell::Cell, rc::Rc, + cell::Cell, + rc::Rc, }; - pub use crate::paint_context::PaintContext; pub use taffy::tree::NodeId; pub struct Layout<'a, E: ?Sized> { pub from_engine: EngineLayout, @@ -627,33 +616,24 @@ mod element { Self: Sized, { let pressed: Rc> = Default::default(); - self.mouse_down( - button, - { - let pressed = pressed.clone(); - move |_, _, _| { - pressed.set(true); - } - }, - ) - .mouse_up_outside( - button, - { - let pressed = pressed.clone(); - move |_, _, _| { - pressed.set(false); - } - }, - ) - .mouse_up( - button, - move |view, event, event_cx| { - if pressed.get() { - pressed.set(false); - handler(view, event, event_cx); - } - }, - ) + self.mouse_down(button, { + let pressed = pressed.clone(); + move |_, _, _| { + pressed.set(true); + } + }) + .mouse_up_outside(button, { + let pressed = pressed.clone(); + move |_, _, _| { + pressed.set(false); + } + }) + .mouse_up(button, move |view, event, event_cx| { + if pressed.get() { + pressed.set(false); + handler(view, event, event_cx); + } + }) } fn mouse_down( mut self, @@ -663,17 +643,16 @@ mod element { where Self: Sized, { - self.handlers_mut() - .push(EventHandler { - handler: Rc::new(move |view, event, event_cx| { - let event = event.downcast_ref::().unwrap(); - if event.button == button && event.is_down { - handler(view, event, event_cx); - } - }), - event_type: TypeId::of::(), - outside_bounds: false, - }); + self.handlers_mut().push(EventHandler { + handler: Rc::new(move |view, event, event_cx| { + let event = event.downcast_ref::().unwrap(); + if event.button == button && event.is_down { + handler(view, event, event_cx); + } + }), + event_type: TypeId::of::(), + outside_bounds: false, + }); self } fn mouse_down_outside( @@ -684,17 +663,16 @@ mod element { where Self: Sized, { - self.handlers_mut() - .push(EventHandler { - handler: Rc::new(move |view, event, event_cx| { - let event = event.downcast_ref::().unwrap(); - if event.button == button && event.is_down { - handler(view, event, event_cx); - } - }), - event_type: TypeId::of::(), - outside_bounds: true, - }); + self.handlers_mut().push(EventHandler { + handler: Rc::new(move |view, event, event_cx| { + let event = event.downcast_ref::().unwrap(); + if event.button == button && event.is_down { + handler(view, event, event_cx); + } + }), + event_type: TypeId::of::(), + outside_bounds: true, + }); self } fn mouse_up( @@ -705,17 +683,16 @@ mod element { where Self: Sized, { - self.handlers_mut() - .push(EventHandler { - handler: Rc::new(move |view, event, event_cx| { - let event = event.downcast_ref::().unwrap(); - if event.button == button && !event.is_down { - handler(view, event, event_cx); - } - }), - event_type: TypeId::of::(), - outside_bounds: false, - }); + self.handlers_mut().push(EventHandler { + handler: Rc::new(move |view, event, event_cx| { + let event = event.downcast_ref::().unwrap(); + if event.button == button && !event.is_down { + handler(view, event, event_cx); + } + }), + event_type: TypeId::of::(), + outside_bounds: false, + }); self } fn mouse_up_outside( @@ -726,17 +703,16 @@ mod element { where Self: Sized, { - self.handlers_mut() - .push(EventHandler { - handler: Rc::new(move |view, event, event_cx| { - let event = event.downcast_ref::().unwrap(); - if event.button == button && !event.is_down { - handler(view, event, event_cx); - } - }), - event_type: TypeId::of::(), - outside_bounds: true, - }); + self.handlers_mut().push(EventHandler { + handler: Rc::new(move |view, event, event_cx| { + let event = event.downcast_ref::().unwrap(); + if event.button == button && !event.is_down { + handler(view, event, event_cx); + } + }), + event_type: TypeId::of::(), + outside_bounds: true, + }); self } fn block(mut self) -> Self @@ -764,9 +740,7 @@ mod element { where Self: Sized, { - self - .declared_style() - .overflow = OptionalPoint { + self.declared_style().overflow = OptionalPoint { x: Some(Overflow::Visible), y: Some(Overflow::Visible), }; @@ -776,9 +750,7 @@ mod element { where Self: Sized, { - self - .declared_style() - .overflow = OptionalPoint { + self.declared_style().overflow = OptionalPoint { x: Some(Overflow::Hidden), y: Some(Overflow::Hidden), }; @@ -788,9 +760,7 @@ mod element { where Self: Sized, { - self - .declared_style() - .overflow = OptionalPoint { + self.declared_style().overflow = OptionalPoint { x: Some(Overflow::Scroll), y: Some(Overflow::Scroll), }; @@ -4485,11 +4455,7 @@ mod element { layout: Option<(NodeId, Box)>, } impl AnyElement { - pub fn layout( - &mut self, - view: &mut V, - cx: &mut LayoutContext, - ) -> Result { + pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { let pushed_text_style = self.push_text_style(cx); let (node_id, layout) = self.element.layout(view, cx)?; self.layout = Some((node_id, layout)); @@ -4511,30 +4477,25 @@ mod element { } pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext) -> Result<()> { let pushed_text_style = self.push_text_style(cx); - let (layout_node_id, element_layout) = self - .layout - .as_mut() - .expect("paint called before layout"); + let (layout_node_id, element_layout) = + self.layout.as_mut().expect("paint called before layout"); let layout = Layout { from_engine: cx .layout_engine() .unwrap() .computed_layout(*layout_node_id) - .expect( - "you can currently only use playground elements within an adapter", - ), + .expect("you can currently only use playground elements within an adapter"), from_element: element_layout.as_mut(), }; let style = self.element.style(); let fill_color = style.fill.flatten().and_then(|fill| fill.color()); if let Some(fill_color) = fill_color { - cx.scene - .push_quad(gpui::scene::Quad { - bounds: layout.from_engine.bounds, - background: Some(fill_color.into()), - border: Default::default(), - corner_radii: Default::default(), - }); + cx.scene.push_quad(gpui::scene::Quad { + bounds: layout.from_engine.bounds, + background: Some(fill_color.into()), + border: Default::default(), + corner_radii: Default::default(), + }); } for event_handler in self.element.handlers_mut().iter().cloned() { let EngineLayout { order, bounds } = layout.from_engine; @@ -4547,10 +4508,7 @@ mod element { bounds, outside_bounds: event_handler.outside_bounds, event_handler: Rc::new(move |view, event, window_cx, view_id| { - let mut view_context = ViewContext::mutable( - window_cx, - view_id, - ); + let mut view_context = ViewContext::mutable(window_cx, view_id); let mut event_context = EventContext::new(&mut view_context); view_event_handler( view.downcast_mut().unwrap(), @@ -4607,14 +4565,14 @@ mod element { mod frame { use crate::{ element::{ - AnyElement, Element, EventHandler, IntoElement, Layout, LayoutContext, - NodeId, PaintContext, + AnyElement, Element, EventHandler, IntoElement, Layout, LayoutContext, NodeId, + PaintContext, }, style::{OptionalStyle, Style}, }; use anyhow::{anyhow, Result}; use gpui::LayoutNodeId; - use playground_macros::IntoElement; + use gpui2_macros::IntoElement; #[element_crate = "crate"] pub struct Frame { style: OptionalStyle, @@ -4656,12 +4614,13 @@ mod frame { let style: Style = self.style.into(); let node_id = cx .layout_engine() - .ok_or_else(|| ::anyhow::__private::must_use({ - let error = ::anyhow::__private::format_err( - format_args!("no layout engine"), - ); - error - }))? + .ok_or_else(|| { + ::anyhow::__private::must_use({ + let error = + ::anyhow::__private::format_err(format_args!("no layout engine")); + error + }) + })? .add_node(style.to_taffy(rem_size), child_layout_node_ids)?; Ok((node_id, ())) } @@ -4687,18 +4646,23 @@ mod frame { I: IntoIterator, E: IntoElement, { - self.children.extend(children.into_iter().map(|e| e.into_any_element())); + self.children + .extend(children.into_iter().map(|e| e.into_any_element())); self } } } mod hoverable { - use std::{cell::Cell, marker::PhantomData, rc::Rc}; + use crate::{ + element::Element, + style::{OptionalStyle, Style}, + }; use gpui::{ geometry::{rect::RectF, vector::Vector2F}, - scene::MouseMove, EngineLayout, + scene::MouseMove, + EngineLayout, }; - use crate::{element::Element, style::{OptionalStyle, Style}}; + use std::{cell::Cell, marker::PhantomData, rc::Rc}; pub struct Hoverable { hover_style: OptionalStyle, computed_style: Option