Revert "Add a proc macro for deriving override structs with optional fields"

This reverts commit ab9356e9d8.
This commit is contained in:
Nathan Sobo 2023-08-17 13:48:46 -06:00
parent ab9356e9d8
commit f3a275d339
9 changed files with 96 additions and 209 deletions

View file

@ -1,7 +1,7 @@
use crate::{
adapter::Adapter,
color::Hsla,
style::{Display, ElementStyle, ElementStyleOverrides, Fill, Overflow, Position},
style::{Display, ElementStyle, Fill, Overflow, Position},
};
use anyhow::Result;
pub use gpui::LayoutContext;
@ -27,7 +27,6 @@ pub struct Layout<'a, E: ?Sized> {
pub struct ElementMetadata<V> {
pub style: ElementStyle,
pub hover_style: ElementStyleOverrides,
pub handlers: Vec<EventHandler<V>>,
}
@ -51,7 +50,6 @@ impl<V> Default for ElementMetadata<V> {
fn default() -> Self {
Self {
style: ElementStyle::default(),
hover_style: ElementStyleOverrides::default(),
handlers: Vec::new(),
}
}
@ -60,7 +58,8 @@ impl<V> Default for ElementMetadata<V> {
pub trait Element<V: 'static>: 'static {
type Layout: 'static;
fn metadata(&mut self) -> &mut ElementMetadata<V>;
fn style_mut(&mut self) -> &mut ElementStyle;
fn handlers_mut(&mut self) -> &mut Vec<EventHandler<V>>;
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>)
-> Result<(NodeId, Self::Layout)>;
@ -127,7 +126,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().handlers.push(EventHandler {
self.handlers_mut().push(EventHandler {
handler: Rc::new(move |view, event, event_cx| {
let event = event.downcast_ref::<MouseButtonEvent>().unwrap();
if event.button == button && event.is_down {
@ -148,7 +147,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().handlers.push(EventHandler {
self.handlers_mut().push(EventHandler {
handler: Rc::new(move |view, event, event_cx| {
let event = event.downcast_ref::<MouseButtonEvent>().unwrap();
if event.button == button && event.is_down {
@ -169,7 +168,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().handlers.push(EventHandler {
self.handlers_mut().push(EventHandler {
handler: Rc::new(move |view, event, event_cx| {
let event = event.downcast_ref::<MouseButtonEvent>().unwrap();
if event.button == button && !event.is_down {
@ -190,7 +189,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().handlers.push(EventHandler {
self.handlers_mut().push(EventHandler {
handler: Rc::new(move |view, event, event_cx| {
let event = event.downcast_ref::<MouseButtonEvent>().unwrap();
if event.button == button && !event.is_down {
@ -209,7 +208,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.display = Display::Block;
self.style_mut().display = Display::Block;
self
}
@ -217,7 +216,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.display = Display::Flex;
self.style_mut().display = Display::Flex;
self
}
@ -225,7 +224,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.display = Display::Grid;
self.style_mut().display = Display::Grid;
self
}
@ -235,8 +234,8 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.x = Overflow::Visible;
self.metadata().style.overflow.y = Overflow::Visible;
self.style_mut().overflow.x = Overflow::Visible;
self.style_mut().overflow.y = Overflow::Visible;
self
}
@ -244,8 +243,8 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.x = Overflow::Hidden;
self.metadata().style.overflow.y = Overflow::Hidden;
self.style_mut().overflow.x = Overflow::Hidden;
self.style_mut().overflow.y = Overflow::Hidden;
self
}
@ -253,8 +252,8 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.x = Overflow::Scroll;
self.metadata().style.overflow.y = Overflow::Scroll;
self.style_mut().overflow.x = Overflow::Scroll;
self.style_mut().overflow.y = Overflow::Scroll;
self
}
@ -262,7 +261,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.x = Overflow::Visible;
self.style_mut().overflow.x = Overflow::Visible;
self
}
@ -270,7 +269,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.x = Overflow::Hidden;
self.style_mut().overflow.x = Overflow::Hidden;
self
}
@ -278,7 +277,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.x = Overflow::Scroll;
self.style_mut().overflow.x = Overflow::Scroll;
self
}
@ -286,7 +285,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.y = Overflow::Visible;
self.style_mut().overflow.y = Overflow::Visible;
self
}
@ -294,7 +293,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.y = Overflow::Hidden;
self.style_mut().overflow.y = Overflow::Hidden;
self
}
@ -302,7 +301,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.overflow.y = Overflow::Scroll;
self.style_mut().overflow.y = Overflow::Scroll;
self
}
@ -312,7 +311,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.position = Position::Relative;
self.style_mut().position = Position::Relative;
self
}
@ -320,7 +319,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.position = Position::Absolute;
self.style_mut().position = Position::Absolute;
self
}
@ -330,10 +329,10 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.inset.top = length;
self.metadata().style.inset.right = length;
self.metadata().style.inset.bottom = length;
self.metadata().style.inset.left = length;
self.style_mut().inset.top = length;
self.style_mut().inset.right = length;
self.style_mut().inset.bottom = length;
self.style_mut().inset.left = length;
self
}
@ -341,7 +340,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.size.width = width.into();
self.style_mut().size.width = width.into();
self
}
@ -349,7 +348,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.size.width = Length::Auto;
self.style_mut().size.width = Length::Auto;
self
}
@ -358,7 +357,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.size.width = length;
self.style_mut().size.width = length;
self
}
@ -367,7 +366,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.min_size.width = length;
self.style_mut().min_size.width = length;
self
}
@ -375,7 +374,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.size.height = height.into();
self.style_mut().size.height = height.into();
self
}
@ -383,7 +382,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.size.height = Length::Auto;
self.style_mut().size.height = Length::Auto;
self
}
@ -392,7 +391,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.size.height = height;
self.style_mut().size.height = height;
self
}
@ -401,7 +400,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.min_size.height = length;
self.style_mut().min_size.height = length;
self
}
@ -409,7 +408,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.fill = fill.into();
self.style_mut().fill = fill.into();
self
}
@ -417,14 +416,15 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.metadata().style.text_color = Some(color.into());
self.style_mut().text_color = Some(color.into());
self
}
}
// Object-safe counterpart of Element used by AnyElement to store elements as trait objects.
trait ElementObject<V> {
fn metadata(&mut self) -> &mut ElementMetadata<V>;
fn style_mut(&mut self) -> &mut ElementStyle;
fn handlers_mut(&mut self) -> &mut Vec<EventHandler<V>>;
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>)
-> Result<(NodeId, Box<dyn Any>)>;
fn paint(
@ -436,8 +436,12 @@ trait ElementObject<V> {
}
impl<V: 'static, E: Element<V>> ElementObject<V> for E {
fn metadata(&mut self) -> &mut ElementMetadata<V> {
Element::metadata(self)
fn style_mut(&mut self) -> &mut ElementStyle {
Element::style_mut(self)
}
fn handlers_mut(&mut self) -> &mut Vec<EventHandler<V>> {
Element::handlers_mut(self)
}
fn layout(
@ -486,7 +490,7 @@ impl<V: 'static> AnyElement<V> {
}
pub fn push_text_style(&mut self, cx: &mut impl RenderContext) -> bool {
let text_style = self.element.metadata().style.text_style();
let text_style = self.element.style_mut().text_style();
if let Some(text_style) = text_style {
let mut current_text_style = cx.text_style();
text_style.apply(&mut current_text_style);
@ -512,7 +516,7 @@ impl<V: 'static> AnyElement<V> {
from_element: element_layout.as_mut(),
};
for event_handler in self.element.metadata().handlers.iter().cloned() {
for event_handler in self.element.handlers_mut().iter().cloned() {
let EngineLayout { order, bounds } = layout.from_engine;
let view_id = cx.view_id();
@ -547,8 +551,12 @@ impl<V: 'static> AnyElement<V> {
impl<V: 'static> Element<V> for AnyElement<V> {
type Layout = ();
fn metadata(&mut self) -> &mut ElementMetadata<V> {
self.element.metadata()
fn style_mut(&mut self) -> &mut ElementStyle {
self.element.style_mut()
}
fn handlers_mut(&mut self) -> &mut Vec<EventHandler<V>> {
self.element.handlers_mut()
}
fn layout(