This commit is contained in:
Antonio Scandurra
2023-10-10 15:03:47 +02:00
parent 012a7743ad
commit 48a12be538
3 changed files with 55 additions and 77 deletions
+1
View File
@@ -1,4 +1,5 @@
mod div; mod div;
mod hoverable;
mod img; mod img;
mod stateless; mod stateless;
mod svg; mod svg;
+48 -71
View File
@@ -1,23 +1,11 @@
use crate::{ use crate::{
element::{AnyElement, Element, IntoElement, Layout, ParentElement}, Bounds, Element, Interactive, MouseEventListeners, Pixels, Style, Styled, ViewContext,
interactive::{InteractionHandlers, Interactive},
style::{Style, StyleHelpers, Styleable},
ViewContext,
}; };
use anyhow::Result; use anyhow::Result;
use gpui::{geometry::vector::Vector2F, platform::MouseMovedEvent, LayoutId}; use refineable::{CascadeSlot, RefinementCascade};
use refineable::{CascadeSlot, Refineable, RefinementCascade};
use smallvec::SmallVec;
use std::{cell::Cell, rc::Rc}; use std::{cell::Cell, rc::Rc};
pub struct Hoverable<E: Styleable> { pub fn hoverable<E: Styled>(mut child: E) -> Hoverable<E> {
hovered: Rc<Cell<bool>>,
cascade_slot: CascadeSlot,
hovered_style: <E::Style as Refineable>::Refinement,
child: E,
}
pub fn hoverable<E: Styleable>(mut child: E) -> Hoverable<E> {
Hoverable { Hoverable {
hovered: Rc::new(Cell::new(false)), hovered: Rc::new(Cell::new(false)),
cascade_slot: child.style_cascade().reserve(), cascade_slot: child.style_cascade().reserve(),
@@ -26,80 +14,69 @@ pub fn hoverable<E: Styleable>(mut child: E) -> Hoverable<E> {
} }
} }
impl<E: Styleable> Styleable for Hoverable<E> { pub struct Hoverable<E: Styled> {
hovered: Rc<Cell<bool>>,
cascade_slot: CascadeSlot,
hovered_style: RefinementCascade<E::Style>,
child: E,
}
impl<E> Styled for Hoverable<E>
where
E: Styled,
{
type Style = E::Style; type Style = E::Style;
fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> { fn style_cascade(&mut self) -> &mut RefinementCascade<E::Style> {
self.child.style_cascade() self.child.style_cascade()
} }
fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement { fn declared_style(&mut self) -> &mut <Self::Style as refineable::Refineable>::Refinement {
&mut self.hovered_style &mut self.hovered_style
} }
} }
impl<V: 'static, E: Element<V> + Styleable> Element<V> for Hoverable<E> { impl<S: 'static + Send + Sync, E: Interactive<S> + Styled> Interactive<S> for Hoverable<E> {
type PaintState = E::PaintState; fn listeners(&mut self) -> &mut MouseEventListeners<S> {
self.child.listeners()
}
}
impl<E: Element + Styled> Element for Hoverable<E> {
type State = E::State;
type FrameState = E::FrameState;
fn layout( fn layout(
&mut self, &mut self,
view: &mut V, state: &mut Self::State,
cx: &mut ViewContext<V>, cx: &mut ViewContext<Self::State>,
) -> Result<(LayoutId, Self::PaintState)> ) -> Result<(crate::LayoutId, Self::FrameState)> {
where Ok(self.child.layout(state, cx)?)
Self: Sized,
{
Ok(self.child.layout(view, cx)?)
} }
fn paint( fn paint(
&mut self, &mut self,
view: &mut V, bounds: Bounds<Pixels>,
parent_origin: Vector2F, state: &mut Self::State,
layout: &Layout, frame_state: &mut Self::FrameState,
paint_state: &mut Self::PaintState, cx: &mut ViewContext<Self::State>,
cx: &mut ViewContext<V>, ) -> Result<()> {
) where todo!()
Self: Sized, // self.hovered.set(bounds.contains_point(cx.mouse_position()));
{
let bounds = layout.bounds + parent_origin;
self.hovered.set(bounds.contains_point(cx.mouse_position()));
let slot = self.cascade_slot; // let slot = self.cascade_slot;
let style = self.hovered.get().then_some(self.hovered_style.clone()); // let style = self.hovered.get().then_some(self.hovered_style.clone());
self.style_cascade().set(slot, style); // self.style_cascade().set(slot, style);
let hovered = self.hovered.clone(); // let hovered = self.hovered.clone();
cx.on_event(layout.order, move |_view, _: &MouseMovedEvent, cx| { // cx.on_event(layout.order, move |_view, _: &MouseMovedEvent, cx| {
cx.bubble_event(); // cx.bubble_event();
if bounds.contains_point(cx.mouse_position()) != hovered.get() { // if bounds.contains_point(cx.mouse_position()) != hovered.get() {
cx.repaint(); // cx.repaint();
} // }
}); // });
self.child // self.child
.paint(view, parent_origin, layout, paint_state, cx); // .paint(view, parent_origin, layout, paint_state, cx);
}
}
impl<E: Styleable<Style = Style>> StyleHelpers for Hoverable<E> {}
impl<V: 'static, E: Interactive<V> + Styleable> Interactive<V> for Hoverable<E> {
fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V> {
self.child.interaction_handlers()
}
}
impl<V: 'static, E: ParentElement<V> + Styleable> ParentElement<V> for Hoverable<E> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
self.child.children_mut()
}
}
impl<V: 'static, E: Element<V> + Styleable> IntoElement<V> for Hoverable<E> {
type Element = Self;
fn into_element(self) -> Self::Element {
self
} }
} }
+6 -6
View File
@@ -10,12 +10,12 @@ pub trait Styled {
Self::Style::from_refinement(&self.style_cascade().merged()) Self::Style::from_refinement(&self.style_cascade().merged())
} }
// fn hover(self) -> Hoverable<Self> fn hover(self) -> Hoverable<Self>
// where where
// Self: Sized, Self: Sized,
// { {
// hoverable(self) hoverable(self)
// } }
// fn active(self) -> Pressable<Self> // fn active(self) -> Pressable<Self>
// where // where