From f3979a9f283181cb1a0feb15322247704f78f7df Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Sat, 21 Oct 2023 15:59:52 +0200 Subject: [PATCH] Checkpoint --- Cargo.lock | 30 -- Cargo.toml | 2 - crates/gpui2/Cargo.toml | 32 -- crates/gpui2/src/adapter.rs | 76 --- crates/gpui2/src/color.rs | 324 ------------ crates/gpui2/src/element.rs | 232 -------- crates/gpui2/src/elements.rs | 10 - crates/gpui2/src/elements/div.rs | 320 ----------- crates/gpui2/src/elements/hoverable.rs | 105 ---- crates/gpui2/src/elements/img.rs | 110 ---- crates/gpui2/src/elements/pressable.rs | 113 ---- crates/gpui2/src/elements/svg.rs | 84 --- crates/gpui2/src/elements/text.rs | 105 ---- crates/gpui2/src/gpui2.rs | 22 - crates/gpui2/src/interactive.rs | 165 ------ crates/gpui2/src/style.rs | 498 ------------------ crates/gpui2/src/view.rs | 26 - crates/gpui2/src/view_context.rs | 79 --- crates/gpui2/src/view_handle.rs | 60 --- crates/gpui2_macros/Cargo.toml | 14 - crates/gpui2_macros/src/derive_element.rs | 93 ---- .../gpui2_macros/src/derive_into_element.rs | 69 --- crates/gpui2_macros/src/gpui2_macros.rs | 20 - crates/gpui2_macros/src/styleable_helpers.rs | 408 -------------- 24 files changed, 2997 deletions(-) delete mode 100644 crates/gpui2/Cargo.toml delete mode 100644 crates/gpui2/src/adapter.rs delete mode 100644 crates/gpui2/src/color.rs delete mode 100644 crates/gpui2/src/element.rs delete mode 100644 crates/gpui2/src/elements.rs delete mode 100644 crates/gpui2/src/elements/div.rs delete mode 100644 crates/gpui2/src/elements/hoverable.rs delete mode 100644 crates/gpui2/src/elements/img.rs delete mode 100644 crates/gpui2/src/elements/pressable.rs delete mode 100644 crates/gpui2/src/elements/svg.rs delete mode 100644 crates/gpui2/src/elements/text.rs delete mode 100644 crates/gpui2/src/gpui2.rs delete mode 100644 crates/gpui2/src/interactive.rs delete mode 100644 crates/gpui2/src/style.rs delete mode 100644 crates/gpui2/src/view.rs delete mode 100644 crates/gpui2/src/view_context.rs delete mode 100644 crates/gpui2/src/view_handle.rs delete mode 100644 crates/gpui2_macros/Cargo.toml delete mode 100644 crates/gpui2_macros/src/derive_element.rs delete mode 100644 crates/gpui2_macros/src/derive_into_element.rs delete mode 100644 crates/gpui2_macros/src/gpui2_macros.rs delete mode 100644 crates/gpui2_macros/src/styleable_helpers.rs diff --git a/Cargo.lock b/Cargo.lock index e18edc304a..5c87911491 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3271,36 +3271,6 @@ dependencies = [ "waker-fn", ] -[[package]] -name = "gpui2" -version = "0.1.0" -dependencies = [ - "anyhow", - "derive_more", - "futures 0.3.28", - "gpui", - "gpui2_macros", - "log", - "parking_lot 0.11.2", - "refineable", - "rust-embed", - "serde", - "settings", - "simplelog", - "smallvec", - "theme", - "util", -] - -[[package]] -name = "gpui2_macros" -version = "0.1.0" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "gpui3" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 715d085371..16907100a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,8 +34,6 @@ members = [ "crates/go_to_line", "crates/gpui", "crates/gpui_macros", - "crates/gpui2", - "crates/gpui2_macros", "crates/gpui3", "crates/gpui3_macros", "crates/install_cli", diff --git a/crates/gpui2/Cargo.toml b/crates/gpui2/Cargo.toml deleted file mode 100644 index 093ab1e347..0000000000 --- a/crates/gpui2/Cargo.toml +++ /dev/null @@ -1,32 +0,0 @@ -[package] -name = "gpui2" -version = "0.1.0" -edition = "2021" -publish = false - -[lib] -name = "gpui2" -path = "src/gpui2.rs" - -[features] -test-support = ["gpui/test-support"] - -[dependencies] -anyhow.workspace = true -derive_more.workspace = true -gpui = { path = "../gpui" } -log.workspace = true -futures.workspace = true -gpui2_macros = { path = "../gpui2_macros" } -parking_lot.workspace = true -refineable.workspace = true -rust-embed.workspace = true -serde.workspace = true -settings = { path = "../settings" } -simplelog = "0.9" -smallvec.workspace = true -theme = { path = "../theme" } -util = { path = "../util" } - -[dev-dependencies] -gpui = { path = "../gpui", features = ["test-support"] } diff --git a/crates/gpui2/src/adapter.rs b/crates/gpui2/src/adapter.rs deleted file mode 100644 index c36966d722..0000000000 --- a/crates/gpui2/src/adapter.rs +++ /dev/null @@ -1,76 +0,0 @@ -use crate::ViewContext; -use gpui::{geometry::rect::RectF, LayoutEngine, LayoutId}; -use util::ResultExt; - -/// Makes a new, gpui2-style element into a legacy element. -pub struct AdapterElement(pub(crate) crate::element::AnyElement); - -impl gpui::Element for AdapterElement { - type LayoutState = Option<(LayoutEngine, LayoutId)>; - type PaintState = (); - - fn layout( - &mut self, - constraint: gpui::SizeConstraint, - view: &mut V, - cx: &mut gpui::ViewContext, - ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { - cx.push_layout_engine(LayoutEngine::new()); - - let mut cx = ViewContext::new(cx); - let layout_id = self.0.layout(view, &mut cx).log_err(); - if let Some(layout_id) = layout_id { - cx.layout_engine() - .unwrap() - .compute_layout(layout_id, constraint.max) - .log_err(); - } - - let layout_engine = cx.pop_layout_engine(); - debug_assert!(layout_engine.is_some(), - "unexpected layout stack state. is there an unmatched pop_layout_engine in the called code?" - ); - - (constraint.max, layout_engine.zip(layout_id)) - } - - fn paint( - &mut self, - bounds: RectF, - _visible_bounds: RectF, - layout_data: &mut Option<(LayoutEngine, LayoutId)>, - view: &mut V, - cx: &mut gpui::ViewContext, - ) -> Self::PaintState { - let (layout_engine, layout_id) = layout_data.take().unwrap(); - cx.push_layout_engine(layout_engine); - self.0 - .paint(view, bounds.origin(), &mut ViewContext::new(cx)); - *layout_data = cx.pop_layout_engine().zip(Some(layout_id)); - debug_assert!(layout_data.is_some()); - } - - fn rect_for_text_range( - &self, - _range_utf16: std::ops::Range, - _bounds: RectF, - _visible_bounds: RectF, - _layout: &Self::LayoutState, - _paint: &Self::PaintState, - _view: &V, - _cx: &gpui::ViewContext, - ) -> Option { - todo!("implement before merging to main") - } - - fn debug( - &self, - _bounds: RectF, - _layout: &Self::LayoutState, - _paint: &Self::PaintState, - _view: &V, - _cx: &gpui::ViewContext, - ) -> gpui::serde_json::Value { - todo!("implement before merging to main") - } -} diff --git a/crates/gpui2/src/color.rs b/crates/gpui2/src/color.rs deleted file mode 100644 index 11590f967c..0000000000 --- a/crates/gpui2/src/color.rs +++ /dev/null @@ -1,324 +0,0 @@ -#![allow(dead_code)] - -use serde::de::{self, Deserialize, Deserializer, Visitor}; -use smallvec::SmallVec; -use std::fmt; -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; - let b = (hex & 0xFF) as f32 / 255.0; - Rgba { r, g, b, a: 1.0 }.into() -} - -#[derive(Clone, Copy, Default, Debug)] -pub struct Rgba { - pub r: f32, - pub g: f32, - pub b: f32, - pub a: f32, -} - -struct RgbaVisitor; - -impl<'de> Visitor<'de> for RgbaVisitor { - type Value = Rgba; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a string in the format #rrggbb or #rrggbbaa") - } - - fn visit_str(self, value: &str) -> Result { - if value.len() == 7 || value.len() == 9 { - let r = u8::from_str_radix(&value[1..3], 16).unwrap() as f32 / 255.0; - let g = u8::from_str_radix(&value[3..5], 16).unwrap() as f32 / 255.0; - let b = u8::from_str_radix(&value[5..7], 16).unwrap() as f32 / 255.0; - let a = if value.len() == 9 { - u8::from_str_radix(&value[7..9], 16).unwrap() as f32 / 255.0 - } else { - 1.0 - }; - Ok(Rgba { r, g, b, a }) - } else { - Err(E::custom( - "Bad format for RGBA. Expected #rrggbb or #rrggbbaa.", - )) - } - } -} - -impl<'de> Deserialize<'de> for Rgba { - fn deserialize>(deserializer: D) -> Result { - deserializer.deserialize_str(RgbaVisitor) - } -} - -pub trait Lerp { - fn lerp(&self, level: f32) -> Hsla; -} - -impl Lerp for Range { - fn lerp(&self, level: f32) -> Hsla { - let level = level.clamp(0., 1.); - Hsla { - h: self.start.h + (level * (self.end.h - self.start.h)), - s: self.start.s + (level * (self.end.s - self.start.s)), - l: self.start.l + (level * (self.end.l - self.start.l)), - a: self.start.a + (level * (self.end.a - self.start.a)), - } - } -} - -impl From for Rgba { - fn from(value: gpui::color::Color) -> Self { - Self { - r: value.0.r as f32 / 255.0, - g: value.0.g as f32 / 255.0, - b: value.0.b as f32 / 255.0, - a: value.0.a as f32 / 255.0, - } - } -} - -impl From for Rgba { - fn from(color: Hsla) -> Self { - let h = color.h; - let s = color.s; - let l = color.l; - - let c = (1.0 - (2.0 * l - 1.0).abs()) * s; - let x = c * (1.0 - ((h * 6.0) % 2.0 - 1.0).abs()); - let m = l - c / 2.0; - let cm = c + m; - let xm = x + m; - - let (r, g, b) = match (h * 6.0).floor() as i32 { - 0 | 6 => (cm, xm, m), - 1 => (xm, cm, m), - 2 => (m, cm, xm), - 3 => (m, xm, cm), - 4 => (xm, m, cm), - _ => (cm, m, xm), - }; - - Rgba { - r, - g, - b, - a: color.a, - } - } -} - -impl TryFrom<&'_ str> for Rgba { - type Error = ParseIntError; - - fn try_from(value: &'_ str) -> Result { - let r = u8::from_str_radix(&value[1..3], 16)? as f32 / 255.0; - let g = u8::from_str_radix(&value[3..5], 16)? as f32 / 255.0; - let b = u8::from_str_radix(&value[5..7], 16)? as f32 / 255.0; - let a = if value.len() > 7 { - u8::from_str_radix(&value[7..9], 16)? as f32 / 255.0 - } else { - 1.0 - }; - - Ok(Rgba { r, g, b, a }) - } -} - -impl Into for Rgba { - fn into(self) -> gpui::color::Color { - gpui::color::rgba(self.r, self.g, self.b, self.a) - } -} - -#[derive(Default, Copy, Clone, Debug, PartialEq)] -pub struct Hsla { - pub h: f32, - pub s: f32, - pub l: f32, - pub a: f32, -} - -pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Hsla { - Hsla { - h: h.clamp(0., 1.), - s: s.clamp(0., 1.), - l: l.clamp(0., 1.), - a: a.clamp(0., 1.), - } -} - -pub fn black() -> Hsla { - Hsla { - h: 0., - s: 0., - l: 0., - a: 1., - } -} - -impl From for Hsla { - fn from(color: Rgba) -> Self { - let r = color.r; - let g = color.g; - let b = color.b; - - let max = r.max(g.max(b)); - let min = r.min(g.min(b)); - let delta = max - min; - - let l = (max + min) / 2.0; - let s = if l == 0.0 || l == 1.0 { - 0.0 - } else if l < 0.5 { - delta / (2.0 * l) - } else { - delta / (2.0 - 2.0 * l) - }; - - let h = if delta == 0.0 { - 0.0 - } else if max == r { - ((g - b) / delta).rem_euclid(6.0) / 6.0 - } else if max == g { - ((b - r) / delta + 2.0) / 6.0 - } else { - ((r - g) / delta + 4.0) / 6.0 - }; - - Hsla { - h, - s, - l, - a: color.a, - } - } -} - -impl Hsla { - /// Scales the saturation and lightness by the given values, clamping at 1.0. - pub fn scale_sl(mut self, s: f32, l: f32) -> Self { - self.s = (self.s * s).clamp(0., 1.); - self.l = (self.l * l).clamp(0., 1.); - self - } - - /// Increases the saturation of the color by a certain amount, with a max - /// value of 1.0. - pub fn saturate(mut self, amount: f32) -> Self { - self.s += amount; - self.s = self.s.clamp(0.0, 1.0); - self - } - - /// Decreases the saturation of the color by a certain amount, with a min - /// value of 0.0. - pub fn desaturate(mut self, amount: f32) -> Self { - self.s -= amount; - self.s = self.s.max(0.0); - if self.s < 0.0 { - self.s = 0.0; - } - self - } - - /// Brightens the color by increasing the lightness by a certain amount, - /// with a max value of 1.0. - pub fn brighten(mut self, amount: f32) -> Self { - self.l += amount; - self.l = self.l.clamp(0.0, 1.0); - self - } - - /// Darkens the color by decreasing the lightness by a certain amount, - /// with a max value of 0.0. - pub fn darken(mut self, amount: f32) -> Self { - self.l -= amount; - self.l = self.l.clamp(0.0, 1.0); - self - } -} - -impl From for Hsla { - fn from(value: gpui::color::Color) -> Self { - Rgba::from(value).into() - } -} - -impl Into for Hsla { - fn into(self) -> gpui::color::Color { - Rgba::from(self).into() - } -} - -impl<'de> Deserialize<'de> for Hsla { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - // First, deserialize it into Rgba - let rgba = Rgba::deserialize(deserializer)?; - - // Then, use the From for Hsla implementation to convert it - Ok(Hsla::from(rgba)) - } -} - -pub struct ColorScale { - colors: SmallVec<[Hsla; 2]>, - positions: SmallVec<[f32; 2]>, -} - -pub fn scale(colors: I) -> ColorScale -where - I: IntoIterator, - C: Into, -{ - let mut scale = ColorScale { - colors: colors.into_iter().map(Into::into).collect(), - positions: SmallVec::new(), - }; - let num_colors: f32 = scale.colors.len() as f32 - 1.0; - scale.positions = (0..scale.colors.len()) - .map(|i| i as f32 / num_colors) - .collect(); - scale -} - -impl ColorScale { - fn at(&self, t: f32) -> Hsla { - // Ensure that the input is within [0.0, 1.0] - debug_assert!( - 0.0 <= t && t <= 1.0, - "t value {} is out of range. Expected value in range 0.0 to 1.0", - t - ); - - let position = match self - .positions - .binary_search_by(|a| a.partial_cmp(&t).unwrap()) - { - Ok(index) | Err(index) => index, - }; - let lower_bound = position.saturating_sub(1); - let upper_bound = position.min(self.colors.len() - 1); - let lower_color = &self.colors[lower_bound]; - let upper_color = &self.colors[upper_bound]; - - match upper_bound.checked_sub(lower_bound) { - Some(0) | None => *lower_color, - Some(_) => { - let interval_t = (t - self.positions[lower_bound]) - / (self.positions[upper_bound] - self.positions[lower_bound]); - let h = lower_color.h + interval_t * (upper_color.h - lower_color.h); - let s = lower_color.s + interval_t * (upper_color.s - lower_color.s); - let l = lower_color.l + interval_t * (upper_color.l - lower_color.l); - let a = lower_color.a + interval_t * (upper_color.a - lower_color.a); - Hsla { h, s, l, a } - } - } - } -} diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs deleted file mode 100644 index 5fb7288585..0000000000 --- a/crates/gpui2/src/element.rs +++ /dev/null @@ -1,232 +0,0 @@ -pub use crate::ViewContext; -use anyhow::Result; -use gpui::geometry::vector::Vector2F; -pub use gpui::{Layout, LayoutId}; -use smallvec::SmallVec; - -pub trait Element: 'static + IntoElement { - type PaintState; - - fn layout( - &mut self, - view: &mut V, - cx: &mut ViewContext, - ) -> Result<(LayoutId, Self::PaintState)> - where - Self: Sized; - - fn paint( - &mut self, - view: &mut V, - parent_origin: Vector2F, - layout: &Layout, - state: &mut Self::PaintState, - cx: &mut ViewContext, - ) where - Self: Sized; - - fn into_any(self) -> AnyElement - where - Self: 'static + Sized, - { - AnyElement(Box::new(StatefulElement { - element: self, - phase: ElementPhase::Init, - })) - } - - /// Applies a given function `then` to the current element if `condition` is true. - /// This function is used to conditionally modify the element based on a given condition. - /// If `condition` is false, it just returns the current element as it is. - /// - /// # Parameters - /// - `self`: The current element - /// - `condition`: The boolean condition based on which `then` is applied to the element. - /// - `then`: A function that takes in the current element and returns a possibly modified element. - /// - /// # Return - /// It returns the potentially modified element. - fn when(mut self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self - where - Self: Sized, - { - if condition { - self = then(self); - } - self - } -} - -/// Used to make ElementState into a trait object, so we can wrap it in AnyElement. -trait AnyStatefulElement { - fn layout(&mut self, view: &mut V, cx: &mut ViewContext) -> Result; - fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext); -} - -/// A wrapper around an element that stores its layout state. -struct StatefulElement> { - element: E, - phase: ElementPhase, -} - -enum ElementPhase> { - Init, - PostLayout { - layout_id: LayoutId, - paint_state: E::PaintState, - }, - #[allow(dead_code)] - PostPaint { - layout: Layout, - paint_state: E::PaintState, - }, - Error(String), -} - -impl> std::fmt::Debug for ElementPhase { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - ElementPhase::Init => write!(f, "Init"), - ElementPhase::PostLayout { layout_id, .. } => { - write!(f, "PostLayout with layout id: {:?}", layout_id) - } - ElementPhase::PostPaint { layout, .. } => { - write!(f, "PostPaint with layout: {:?}", layout) - } - ElementPhase::Error(err) => write!(f, "Error: {}", err), - } - } -} - -impl> Default for ElementPhase { - fn default() -> Self { - Self::Init - } -} - -/// We blanket-implement the object-safe ElementStateObject interface to make ElementStates into trait objects -impl> AnyStatefulElement for StatefulElement { - fn layout(&mut self, view: &mut V, cx: &mut ViewContext) -> Result { - let result; - self.phase = match self.element.layout(view, cx) { - Ok((layout_id, paint_state)) => { - result = Ok(layout_id); - ElementPhase::PostLayout { - layout_id, - paint_state, - } - } - Err(error) => { - let message = error.to_string(); - result = Err(error); - ElementPhase::Error(message) - } - }; - result - } - - fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext) { - self.phase = match std::mem::take(&mut self.phase) { - ElementPhase::PostLayout { - layout_id, - mut paint_state, - } => match cx.computed_layout(layout_id) { - Ok(layout) => { - self.element - .paint(view, parent_origin, &layout, &mut paint_state, cx); - ElementPhase::PostPaint { - layout, - paint_state, - } - } - Err(error) => ElementPhase::Error(error.to_string()), - }, - ElementPhase::PostPaint { - layout, - mut paint_state, - } => { - self.element - .paint(view, parent_origin, &layout, &mut paint_state, cx); - ElementPhase::PostPaint { - layout, - paint_state, - } - } - phase @ ElementPhase::Error(_) => phase, - - phase @ _ => { - panic!("invalid element phase to call paint: {:?}", phase); - } - }; - } -} - -/// A dynamic element. -pub struct AnyElement(Box>); - -impl AnyElement { - pub fn layout(&mut self, view: &mut V, cx: &mut ViewContext) -> Result { - self.0.layout(view, cx) - } - - pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext) { - self.0.paint(view, parent_origin, cx) - } -} - -pub trait ParentElement { - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>; - - fn child(mut self, child: impl IntoElement) -> Self - where - Self: Sized, - { - self.children_mut().push(child.into_element().into_any()); - self - } - - fn children(mut self, children: I) -> Self - where - I: IntoIterator, - E: IntoElement, - Self: Sized, - { - self.children_mut().extend( - children - .into_iter() - .map(|child| child.into_element().into_any()), - ); - self - } - - // HACK: This is a temporary hack to get children working for the purposes - // of building UI on top of the current version of gpui2. - // - // We'll (hopefully) be moving away from this in the future. - fn children_any(mut self, children: I) -> Self - where - I: IntoIterator>, - Self: Sized, - { - self.children_mut().extend(children.into_iter()); - self - } - - // HACK: This is a temporary hack to get children working for the purposes - // of building UI on top of the current version of gpui2. - // - // We'll (hopefully) be moving away from this in the future. - fn child_any(mut self, children: AnyElement) -> Self - where - Self: Sized, - { - self.children_mut().push(children); - self - } -} - -pub trait IntoElement { - type Element: Element; - - fn into_element(self) -> Self::Element; -} diff --git a/crates/gpui2/src/elements.rs b/crates/gpui2/src/elements.rs deleted file mode 100644 index 5b4942fc46..0000000000 --- a/crates/gpui2/src/elements.rs +++ /dev/null @@ -1,10 +0,0 @@ -pub mod div; -pub mod hoverable; -mod img; -pub mod pressable; -pub mod svg; -pub mod text; - -pub use div::div; -pub use img::img; -pub use svg::svg; diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs deleted file mode 100644 index 2fd85f6465..0000000000 --- a/crates/gpui2/src/elements/div.rs +++ /dev/null @@ -1,320 +0,0 @@ -use std::{cell::Cell, rc::Rc}; - -use crate::{ - element::{AnyElement, Element, IntoElement, Layout, ParentElement}, - hsla, - style::{CornerRadii, Overflow, Style, StyleHelpers, Styleable}, - InteractionHandlers, Interactive, ViewContext, -}; -use anyhow::Result; -use gpui::{ - geometry::{rect::RectF, vector::Vector2F, Point}, - platform::{MouseButton, MouseButtonEvent, MouseMovedEvent, ScrollWheelEvent}, - scene::{self}, - LayoutId, -}; -use refineable::{Cascade, Refineable}; -use smallvec::SmallVec; -use util::ResultExt; - -pub struct Div { - styles: Cascade