Still need to wire up MouseMove with the new regions
This commit is contained in:
parent
3709eff34b
commit
b910c85f7f
7 changed files with 82 additions and 33 deletions
|
@ -7,11 +7,15 @@ use gpui::{
|
|||
};
|
||||
use refineable::Refineable;
|
||||
|
||||
use crate::{element::Element, style::StyleRefinement};
|
||||
use crate::{
|
||||
element::{Element, ParentElement},
|
||||
style::StyleRefinement,
|
||||
};
|
||||
|
||||
pub struct Hoverable<V, E> {
|
||||
hover_style: StyleRefinement,
|
||||
computed_style: Option<StyleRefinement>,
|
||||
hovered: Rc<Cell<bool>>,
|
||||
view_type: PhantomData<V>,
|
||||
child: E,
|
||||
}
|
||||
|
@ -21,6 +25,7 @@ impl<V, E> Hoverable<V, E> {
|
|||
Self {
|
||||
hover_style: StyleRefinement::default(),
|
||||
computed_style: None,
|
||||
hovered: Default::default(),
|
||||
view_type: PhantomData,
|
||||
child,
|
||||
}
|
||||
|
@ -37,8 +42,9 @@ impl<V: 'static, E: Element<V>> Element<V> for Hoverable<V, E> {
|
|||
fn computed_style(&mut self, cx: &mut ViewContext<V>) -> &StyleRefinement {
|
||||
self.computed_style.get_or_insert_with(|| {
|
||||
let mut style = self.child.computed_style(cx).clone();
|
||||
// if hovered
|
||||
style.refine(&self.hover_style);
|
||||
if self.hovered.get() {
|
||||
style.refine(&self.hover_style);
|
||||
}
|
||||
style
|
||||
})
|
||||
}
|
||||
|
@ -63,17 +69,24 @@ impl<V: 'static, E: Element<V>> Element<V> for Hoverable<V, E> {
|
|||
) -> anyhow::Result<()> {
|
||||
let EngineLayout { bounds, order } = layout.from_engine;
|
||||
let window_bounds = RectF::new(Vector2F::zero(), cx.window_size());
|
||||
let was_hovered = Rc::new(Cell::new(false));
|
||||
let hovered = self.hovered.clone();
|
||||
|
||||
self.child.paint(layout, view, cx)?;
|
||||
|
||||
let mouse_within_bounds = bounds.contains_point(cx.mouse_position());
|
||||
if mouse_within_bounds != hovered.get() {
|
||||
hovered.set(mouse_within_bounds);
|
||||
cx.repaint();
|
||||
}
|
||||
|
||||
cx.draw_interactive_region(
|
||||
order,
|
||||
window_bounds,
|
||||
false,
|
||||
move |view, event: &MouseMove, cx| {
|
||||
let is_hovered = bounds.contains_point(cx.mouse_position());
|
||||
if is_hovered != was_hovered.get() {
|
||||
was_hovered.set(is_hovered);
|
||||
let mouse_within_bounds = bounds.contains_point(cx.mouse_position());
|
||||
if mouse_within_bounds != hovered.get() {
|
||||
hovered.set(mouse_within_bounds);
|
||||
cx.repaint();
|
||||
}
|
||||
},
|
||||
|
@ -81,3 +94,23 @@ impl<V: 'static, E: Element<V>> Element<V> for Hoverable<V, E> {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: 'static, P: ParentElement<V>> ParentElement<V> for Hoverable<V, P> {
|
||||
fn child(mut self, child: impl crate::element::IntoElement<V>) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.child = self.child.child(child);
|
||||
self
|
||||
}
|
||||
|
||||
fn children<I, E>(mut self, children: I) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
I: IntoIterator<Item = E>,
|
||||
E: crate::element::IntoElement<V>,
|
||||
{
|
||||
self.child = self.child.children(children);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue