Skip to content

Commit

Permalink
Remove hover_style, focus_style, focus_visible_style, `active_s…
Browse files Browse the repository at this point in the history
…tyle` and `disabled_style`
  • Loading branch information
Zoxc committed Nov 2, 2023
1 parent ad24e8e commit 6a869a8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 124 deletions.
7 changes: 4 additions & 3 deletions examples/widget-gallery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ fn app_view() -> impl View {
s.background(Color::GRAY)
})
.focus_visible(|s| s.border(2.).border_color(Color::BLUE))
})
.hover_style(|s| {
s.background(Color::LIGHT_GRAY).cursor(CursorStyle::Pointer)
.hover(|s| {
s.background(Color::LIGHT_GRAY)
.cursor(CursorStyle::Pointer)
})
})
},
)
Expand Down
60 changes: 1 addition & 59 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ pub struct ViewState {
pub(crate) base_style: Option<Style>,
pub(crate) style: Style,
pub(crate) dragging_style: Option<Style>,
pub(crate) hover_style: Option<Style>,
pub(crate) disabled_style: Option<Style>,
pub(crate) focus_style: Option<Style>,
pub(crate) focus_visible_style: Option<Style>,
pub(crate) responsive_styles: HashMap<ScreenSizeBp, Vec<Style>>,
pub(crate) active_style: Option<Style>,
pub(crate) combined_style: Style,
pub(crate) event_listeners: HashMap<EventListener, Box<EventCallback>>,
pub(crate) context_menu: Option<Box<MenuCallback>>,
Expand All @@ -88,12 +83,7 @@ impl ViewState {
base_style: None,
style: Style::new(),
combined_style: Style::new(),
hover_style: None,
dragging_style: None,
disabled_style: None,
focus_style: None,
focus_visible_style: None,
active_style: None,
responsive_styles: HashMap::new(),
children_nodes: Vec::new(),
event_listeners: HashMap::new(),
Expand Down Expand Up @@ -130,39 +120,6 @@ impl ViewState {
}
}

if interact_state.is_hovered && !interact_state.is_disabled {
if let Some(hover_style) = self.hover_style.clone() {
computed_style = computed_style.apply(hover_style);
}
}

if interact_state.is_focused {
if let Some(focus_style) = self.focus_style.clone() {
computed_style = computed_style.apply(focus_style);
}
}

let focused_keyboard =
interact_state.using_keyboard_navigation && interact_state.is_focused;
if focused_keyboard {
if let Some(focus_visible_style) = self.focus_visible_style.clone() {
computed_style = computed_style.apply(focus_visible_style);
}
}

let active_mouse = interact_state.is_hovered && !interact_state.using_keyboard_navigation;
if interact_state.is_clicking && (active_mouse || focused_keyboard) {
if let Some(active_style) = self.active_style.clone() {
computed_style = computed_style.apply(active_style);
}
}

if interact_state.is_disabled {
if let Some(disabled_style) = self.disabled_style.clone() {
computed_style = computed_style.apply(disabled_style);
}
}

'anim: {
if let Some(animation) = self.animation.as_mut() {
if animation.is_completed() && animation.is_auto_reverse() {
Expand Down Expand Up @@ -499,14 +456,7 @@ impl AppState {
let view_state = self.view_state(id);

view_state.has_style_selectors.has(selector_kind)
|| match selector_kind {
StyleSelector::Hover => view_state.hover_style.is_some(),
StyleSelector::Focus => view_state.focus_style.is_some(),
StyleSelector::FocusVisible => view_state.focus_visible_style.is_some(),
StyleSelector::Disabled => view_state.disabled_style.is_some(),
StyleSelector::Active => view_state.active_style.is_some(),
StyleSelector::Dragging => view_state.dragging_style.is_some(),
}
|| (selector_kind == StyleSelector::Dragging && view_state.dragging_style.is_some())
}

// TODO: animated should be a HashMap<Id, AnimId>
Expand Down Expand Up @@ -607,14 +557,6 @@ impl<'a> EventCx<'a> {
.map(|s| &s.combined_style)
}

pub fn get_hover_style(&self, id: Id) -> Option<&Style> {
if let Some(vs) = self.app_state.view_states.get(&id) {
return vs.hover_style.as_ref();
}

None
}

pub fn get_layout(&self, id: Id) -> Option<Layout> {
self.app_state.get_layout(id)
}
Expand Down
2 changes: 1 addition & 1 deletion src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Id {
self.add_update_message(UpdateMessage::Style { id: *self, style });
}

pub fn update_style_selector(&self, style: Style, selector: StyleSelector) {
pub(crate) fn update_style_selector(&self, style: Style, selector: StyleSelector) {
self.add_update_message(UpdateMessage::StyleSelector {
id: *self,
style,
Expand Down
2 changes: 2 additions & 0 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ impl Style {
self
}

/// The visual style to apply when the mouse hovers over the element
pub fn hover(self, style: impl Fn(Style) -> Style + 'static) -> Self {
self.selector(StyleSelector::Hover, style)
}
Expand All @@ -807,6 +808,7 @@ impl Style {
self.selector(StyleSelector::Focus, style)
}

/// Similar to the `:focus-visible` css selector, this style only activates when tab navigation is used.
pub fn focus_visible(self, style: impl Fn(Style) -> Style + 'static) -> Self {
self.selector(StyleSelector::FocusVisible, style)
}
Expand Down
47 changes: 0 additions & 47 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ pub trait Decorators: View + Sized {
self
}

/// The visual style to apply when the mouse hovers over the element
fn hover_style(self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let style = style(Style::new());
id.update_style_selector(style, StyleSelector::Hover);
});
self
}

/// The visual style to apply when the mouse hovers over the element
fn dragging_style(self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
Expand All @@ -89,25 +79,6 @@ pub trait Decorators: View + Sized {
self
}

fn focus_style(self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let style = style(Style::new());
id.update_style_selector(style, StyleSelector::Focus);
});
self
}

/// Similar to the `:focus-visible` css selector, this style only activates when tab navigation is used.
fn focus_visible_style(self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let style = style(Style::new());
id.update_style_selector(style, StyleSelector::FocusVisible);
});
self
}

/// Allows the element to be navigated to with the keyboard. Similar to setting tabindex="0" in html.
fn keyboard_navigatable(self) -> Self {
let id = self.id();
Expand All @@ -121,24 +92,6 @@ pub trait Decorators: View + Sized {
self
}

fn active_style(self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let style = style(Style::new());
id.update_style_selector(style, StyleSelector::Active);
});
self
}

fn disabled_style(self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let style = style(Style::new());
id.update_style_selector(style, StyleSelector::Disabled);
});
self
}

fn responsive_style(self, size: ScreenSize, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
Expand Down
22 changes: 8 additions & 14 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ impl WindowHandle {
let hovered = &cx.app_state.hovered.clone();
for id in was_hovered.unwrap().symmetric_difference(hovered) {
let view_state = cx.app_state.view_state(*id);
if view_state.hover_style.is_some()
|| view_state.active_style.is_some()
|| view_state.animation.is_some()
if view_state.animation.is_some()
|| view_state.has_style_selectors.has(StyleSelector::Hover)
|| view_state.has_style_selectors.has(StyleSelector::Active)
{
Expand Down Expand Up @@ -371,8 +369,8 @@ impl WindowHandle {
let was_hovered = std::mem::take(&mut cx.app_state.hovered);
for id in was_hovered {
let view_state = cx.app_state.view_state(id);
if view_state.hover_style.is_some()
|| view_state.active_style.is_some()
if view_state.has_style_selectors.has(StyleSelector::Hover)
|| view_state.has_style_selectors.has(StyleSelector::Active)
|| view_state.animation.is_some()
{
cx.app_state.request_layout(id);
Expand Down Expand Up @@ -715,12 +713,8 @@ impl WindowHandle {
let state = cx.app_state.view_state(id);
let style = Some(style);
match selector {
StyleSelector::Hover => state.hover_style = style,
StyleSelector::Focus => state.focus_style = style,
StyleSelector::FocusVisible => state.focus_visible_style = style,
StyleSelector::Disabled => state.disabled_style = style,
StyleSelector::Active => state.active_style = style,
StyleSelector::Dragging => state.dragging_style = style,
_ => panic!(),
}
cx.request_layout(id);
}
Expand Down Expand Up @@ -1244,10 +1238,10 @@ fn context_menu_view(
.padding_horiz(20.0)
.justify_between()
.items_center()
})
.hover_style(|s| s.border_radius(10.0).background(Color::rgb8(65, 65, 65)))
.active_style(|s| s.border_radius(10.0).background(Color::rgb8(92, 92, 92)))
.disabled_style(|s| s.color(Color::rgb8(92, 92, 92))),
.hover(|s| s.border_radius(10.0).background(Color::rgb8(65, 65, 65)))
.active(|s| s.border_radius(10.0).background(Color::rgb8(92, 92, 92)))
.disabled(|s| s.color(Color::rgb8(92, 92, 92)))
}),
list(
move || menu.children.clone().unwrap_or_default(),
move |s| s.clone(),
Expand Down

0 comments on commit 6a869a8

Please sign in to comment.