diff --git a/crates/gpui3/src/active.rs b/crates/gpui3/src/active.rs new file mode 100644 index 0000000000000000000000000000000000000000..707e83e587092488305d5ece1957943a183e9959 --- /dev/null +++ b/crates/gpui3/src/active.rs @@ -0,0 +1,13 @@ +use crate::StyleRefinement; + +pub trait Active { + fn set_active_style(&mut self, style: StyleRefinement); + + fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self + where + Self: Sized, + { + self.set_active_style(f(StyleRefinement::default())); + self + } +} diff --git a/crates/gpui3/src/elements/div.rs b/crates/gpui3/src/elements/div.rs index 0838183976d5e7ca712f3941677a6951afd88496..09cf0ec96ba77a91e3a61fc50f3b2dac28679397 100644 --- a/crates/gpui3/src/elements/div.rs +++ b/crates/gpui3/src/elements/div.rs @@ -1,9 +1,9 @@ use crate::{ - AnonymousElement, AnyElement, AppContext, BorrowWindow, Bounds, Clickable, DispatchPhase, - Element, ElementId, ElementIdentity, IdentifiedElement, Interactive, IntoAnyElement, LayoutId, - MouseClickEvent, MouseDownEvent, MouseEventListeners, MouseMoveEvent, MouseUpEvent, Overflow, - ParentElement, Pixels, Point, ScrollWheelEvent, SharedString, Style, StyleRefinement, Styled, - ViewContext, + Active, AnonymousElement, AnyElement, AppContext, BorrowWindow, Bounds, Click, DispatchPhase, + Element, ElementId, ElementIdentity, Hover, IdentifiedElement, Interactive, IntoAnyElement, + LayoutId, MouseClickEvent, MouseDownEvent, MouseEventListeners, MouseMoveEvent, MouseUpEvent, + Overflow, ParentElement, Pixels, Point, ScrollWheelEvent, SharedString, Style, StyleRefinement, + Styled, ViewContext, }; use collections::HashMap; use parking_lot::Mutex; @@ -449,7 +449,26 @@ where } } -impl Clickable for Div where V: 'static + Send + Sync {} +impl Hover for Div +where + V: 'static + Send + Sync, + K: ElementIdentity, +{ + fn set_hover_style(&mut self, style: StyleRefinement) { + self.hover_style = style; + } +} + +impl Click for Div where V: 'static + Send + Sync {} + +impl Active for Div +where + V: 'static + Send + Sync, +{ + fn set_active_style(&mut self, style: StyleRefinement) { + self.active_style = style; + } +} fn paint_hover_listener(bounds: Bounds, cx: &mut ViewContext) where diff --git a/crates/gpui3/src/elements/img.rs b/crates/gpui3/src/elements/img.rs index ce74a78990eea0cb014fa514bf00351762811af7..7b1f374b9b996f677418d3864e84584a8a1df30f 100644 --- a/crates/gpui3/src/elements/img.rs +++ b/crates/gpui3/src/elements/img.rs @@ -1,6 +1,6 @@ use crate::{ - div, AnonymousElement, AnyElement, BorrowWindow, Bounds, Clickable, Div, DivState, Element, - ElementId, ElementIdentity, IdentifiedElement, Interactive, IntoAnyElement, LayoutId, + div, Active, AnonymousElement, AnyElement, BorrowWindow, Bounds, Click, Div, DivState, Element, + ElementId, ElementIdentity, Hover, IdentifiedElement, Interactive, IntoAnyElement, LayoutId, MouseEventListeners, Pixels, SharedString, StyleRefinement, Styled, ViewContext, }; use futures::FutureExt; @@ -141,4 +141,23 @@ where } } -impl Clickable for Img where V: 'static + Send + Sync {} +impl Hover for Img +where + V: 'static + Send + Sync, + K: ElementIdentity, +{ + fn set_hover_style(&mut self, style: StyleRefinement) { + self.base.set_hover_style(style); + } +} + +impl Click for Img where V: 'static + Send + Sync {} + +impl Active for Img +where + V: 'static + Send + Sync, +{ + fn set_active_style(&mut self, style: StyleRefinement) { + self.base.set_active_style(style) + } +} diff --git a/crates/gpui3/src/elements/svg.rs b/crates/gpui3/src/elements/svg.rs index 92b9d9ef9b4e6a9d144ceb844c6431abd75f019a..cc511c3fb7f1ee0497d63b5707324f521185a20b 100644 --- a/crates/gpui3/src/elements/svg.rs +++ b/crates/gpui3/src/elements/svg.rs @@ -1,7 +1,7 @@ use crate::{ - div, AnonymousElement, AnyElement, Bounds, Clickable, Div, DivState, Element, ElementId, - ElementIdentity, IdentifiedElement, Interactive, IntoAnyElement, LayoutId, MouseEventListeners, - Pixels, SharedString, StyleRefinement, Styled, + div, Active, AnonymousElement, AnyElement, Bounds, Click, Div, DivState, Element, ElementId, + ElementIdentity, Hover, IdentifiedElement, Interactive, IntoAnyElement, LayoutId, + MouseEventListeners, Pixels, SharedString, StyleRefinement, Styled, }; use util::ResultExt; @@ -116,4 +116,23 @@ where } } -impl Clickable for Svg where V: 'static + Send + Sync {} +impl Hover for Svg +where + V: 'static + Send + Sync, + K: ElementIdentity, +{ + fn set_hover_style(&mut self, style: StyleRefinement) { + self.base.set_hover_style(style); + } +} + +impl Click for Svg where V: 'static + Send + Sync {} + +impl Active for Svg +where + V: 'static + Send + Sync, +{ + fn set_active_style(&mut self, style: StyleRefinement) { + self.base.set_active_style(style) + } +} diff --git a/crates/gpui3/src/gpui3.rs b/crates/gpui3/src/gpui3.rs index f858191e6bfa18b05f81831568e04a1a4c548985..8e841e6c2e499f2be38a2d5ddd9466c4cb64f0c7 100644 --- a/crates/gpui3/src/gpui3.rs +++ b/crates/gpui3/src/gpui3.rs @@ -1,3 +1,4 @@ +mod active; mod app; mod assets; mod color; @@ -6,6 +7,7 @@ mod elements; mod events; mod executor; mod geometry; +mod hover; mod image_cache; mod interactive; mod platform; @@ -20,6 +22,7 @@ mod util; mod view; mod window; +pub use active::*; pub use anyhow::Result; pub use app::*; pub use assets::*; @@ -30,6 +33,7 @@ pub use events::*; pub use executor::*; pub use geometry::*; pub use gpui3_macros::*; +pub use hover::*; pub use image_cache::*; pub use interactive::*; pub use platform::*; diff --git a/crates/gpui3/src/hover.rs b/crates/gpui3/src/hover.rs new file mode 100644 index 0000000000000000000000000000000000000000..7c47eda84d731efdf2c97966b284a8d6043cb67d --- /dev/null +++ b/crates/gpui3/src/hover.rs @@ -0,0 +1,13 @@ +use crate::StyleRefinement; + +pub trait Hover { + fn set_hover_style(&mut self, style: StyleRefinement); + + fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self + where + Self: Sized, + { + self.set_hover_style(f(StyleRefinement::default())); + self + } +} diff --git a/crates/gpui3/src/interactive.rs b/crates/gpui3/src/interactive.rs index a482f0366cab023f54e62a0a0755e363a53abe9e..bc8976439a0e6e8f18915bd6c6c9eb9f873920d5 100644 --- a/crates/gpui3/src/interactive.rs +++ b/crates/gpui3/src/interactive.rs @@ -146,7 +146,7 @@ pub trait Interactive: Element { } } -pub trait Clickable: Interactive { +pub trait Click: Interactive { fn on_click( mut self, handler: impl Fn(&mut Self::ViewState, &MouseClickEvent, &mut ViewContext) diff --git a/crates/ui2/src/prelude.rs b/crates/ui2/src/prelude.rs index 4b2d6133e8a5026b1c37df3af49e4ec685d5f5cf..4fddabe3a5365880e7ceef1efe2dc4566fa53cb6 100644 --- a/crates/ui2/src/prelude.rs +++ b/crates/ui2/src/prelude.rs @@ -1,6 +1,6 @@ pub use gpui3::{ - div, Clickable, Element, Hoverable, IntoAnyElement, ParentElement, ScrollState, Styled, - ViewContext, WindowContext, + div, Click, Element, Hover, IntoAnyElement, ParentElement, ScrollState, Styled, ViewContext, + WindowContext, }; pub use crate::{theme, ButtonVariant, ElementExt, Theme};