Skip to content

Commit

Permalink
clicking/active improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Oct 26, 2023
1 parent 286ffb9 commit 7fb3c9a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
12 changes: 9 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl ViewState {
}

let active_mouse = interact_state.is_hovered && !interact_state.using_keyboard_navigation;
if interact_state.is_active && (active_mouse || focused_keyboard) {
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);
}
Expand Down Expand Up @@ -238,6 +238,7 @@ pub struct AppState {
pub(crate) dragging_over: HashSet<Id>,
pub(crate) screen_size_bp: ScreenSizeBp,
pub(crate) grid_bps: GridBreakpoints,
pub(crate) clicking: HashSet<Id>,
pub(crate) hovered: HashSet<Id>,
/// This keeps track of all views that have an animation,
/// regardless of the status of the animation
Expand Down Expand Up @@ -276,6 +277,7 @@ impl AppState {
dragging: None,
drag_start: None,
dragging_over: HashSet::new(),
clicking: HashSet::new(),
hovered: HashSet::new(),
cursor: None,
last_cursor: CursorIcon::Default,
Expand Down Expand Up @@ -347,6 +349,10 @@ impl AppState {
self.active.map(|a| &a == id).unwrap_or(false)
}

pub fn is_clicking(&self, id: &Id) -> bool {
self.clicking.contains(id)
}

pub fn is_dragging(&self) -> bool {
self.dragging
.as_ref()
Expand All @@ -359,7 +365,7 @@ impl AppState {
is_hovered: self.is_hovered(id),
is_disabled: self.is_disabled(id),
is_focused: self.is_focused(id),
is_active: self.is_active(id),
is_clicking: self.is_clicking(id),
using_keyboard_navigation: self.keyboard_navigation,
}
}
Expand Down Expand Up @@ -656,7 +662,7 @@ pub struct InteractionState {
pub(crate) is_hovered: bool,
pub(crate) is_disabled: bool,
pub(crate) is_focused: bool,
pub(crate) is_active: bool,
pub(crate) is_clicking: bool,
pub(crate) using_keyboard_navigation: bool,
}

Expand Down
61 changes: 31 additions & 30 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ pub trait View {

match &event {
Event::PointerDown(event) => {
cx.app_state.clicking.insert(self.id());
if event.button.is_primary() {
let rect = cx.get_size(self.id()).unwrap_or_default().to_rect();
let now_focused = rect.contains(event.pos);
Expand All @@ -435,12 +436,10 @@ pub trait View {
{
let view_state = cx.app_state.view_state(id);
view_state.last_pointer_down = Some(event.clone());
cx.update_active(id);
}
if cx.has_event_listener(id, EventListener::Click) {
let view_state = cx.app_state.view_state(id);
view_state.last_pointer_down = Some(event.clone());
cx.update_active(id);
}

let bottom_left = {
Expand All @@ -467,7 +466,6 @@ pub trait View {
if cx.has_event_listener(id, EventListener::SecondaryClick) {
let view_state = cx.app_state.view_state(id);
view_state.last_pointer_down = Some(event.clone());
cx.update_active(id);
}
}
}
Expand Down Expand Up @@ -559,39 +557,42 @@ pub trait View {
}
}
}
} else {
if let Some(dragging) =
cx.app_state.dragging.as_mut().filter(|d| d.id == id)
} else if let Some(dragging) =
cx.app_state.dragging.as_mut().filter(|d| d.id == id)
{
let dragging_id = dragging.id;
dragging.released_at = Some(std::time::Instant::now());
id.request_paint();
if let Some(action) =
cx.get_event_listener(dragging_id, &EventListener::DragEnd)
{
let dragging_id = dragging.id;
dragging.released_at = Some(std::time::Instant::now());
id.request_paint();
if let Some(action) =
cx.get_event_listener(dragging_id, &EventListener::DragEnd)
{
(*action)(&event);
}
(*action)(&event);
}
let last_pointer_down =
cx.app_state.view_state(id).last_pointer_down.take();
if let Some(action) = cx.get_event_listener(id, &EventListener::DoubleClick)
}

let last_pointer_down = cx.app_state.view_state(id).last_pointer_down.take();
if let Some(action) = cx.get_event_listener(id, &EventListener::DoubleClick) {
if on_view
&& cx.app_state.is_clicking(&id)
&& last_pointer_down
.as_ref()
.map(|e| e.count == 2)
.unwrap_or(false)
&& (*action)(&event)
{
if on_view
&& last_pointer_down
.as_ref()
.map(|e| e.count == 2)
.unwrap_or(false)
&& (*action)(&event)
{
return true;
}
return true;
}
if let Some(action) = cx.get_event_listener(id, &EventListener::Click) {
if on_view && last_pointer_down.is_some() && (*action)(&event) {
return true;
}
}
if let Some(action) = cx.get_event_listener(id, &EventListener::Click) {
if on_view
&& cx.app_state.is_clicking(&id)
&& last_pointer_down.is_some()
&& (*action)(&event)
{
return true;
}
}

if let Some(action) = cx.get_event_listener(id, &EventListener::PointerUp) {
if (*action)(&event) {
return true;
Expand Down
23 changes: 23 additions & 0 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl WindowHandle {

let is_pointer_down = matches!(&event, Event::PointerDown(_));
let was_focused = if is_pointer_down {
cx.app_state.clicking.clear();
cx.app_state.focus.take()
} else {
cx.app_state.focus
Expand Down Expand Up @@ -266,6 +267,22 @@ impl WindowHandle {
cx.app_state.focus_changed(was_focused, cx.app_state.focus);
}

if is_pointer_down {
for id in cx.app_state.clicking.clone() {
if cx.app_state.has_style_for_sel(id, StyleSelector::Active) {
cx.app_state.request_layout(id);
}
}
}
if matches!(&event, Event::PointerUp(_)) {
for id in cx.app_state.clicking.clone() {
if cx.app_state.has_style_for_sel(id, StyleSelector::Active) {
cx.app_state.request_layout(id);
}
}
cx.app_state.clicking.clear();
}

self.process_update();
}

Expand Down Expand Up @@ -403,6 +420,12 @@ impl WindowHandle {
self.event(Event::PointerUp(event));
}
}
// fire an pointer move event in case there's view change
// and hover needs to be updated
self.event(Event::PointerMove(PointerMoveEvent {
pos: self.cursor_position,
modifiers: self.modifiers,
}));
}

pub(crate) fn focused(&mut self, focused: bool) {
Expand Down

0 comments on commit 7fb3c9a

Please sign in to comment.