From 39623ad70404d426aa18d3cbbef716106338f953 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 14 May 2024 21:02:18 -0400 Subject: [PATCH] fix(workspace-list): debounce scroll events --- .../src/components/app.rs | 10 ++--- cosmic-applet-workspaces/src/wayland.rs | 38 +++++++++++++++---- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/cosmic-applet-workspaces/src/components/app.rs b/cosmic-applet-workspaces/src/components/app.rs index fd410e12..c2367c1c 100644 --- a/cosmic-applet-workspaces/src/components/app.rs +++ b/cosmic-applet-workspaces/src/components/app.rs @@ -112,12 +112,12 @@ impl cosmic::Application for IcedWorkspacesApplet { } } Message::WheelScrolled(delta) => { - let delta = match delta { - ScrollDelta::Lines { x, y } => x + y, - ScrollDelta::Pixels { x, y } => x + y, - } as f64; + let (delta, debounce) = match delta { + ScrollDelta::Lines { x, y } => ((x + y) as f64, false), + ScrollDelta::Pixels { x, y } => ((x + y) as f64, true), + }; if let Some(tx) = self.workspace_tx.as_mut() { - let _ = tx.try_send(WorkspaceEvent::Scroll(delta)); + let _ = tx.try_send(WorkspaceEvent::Scroll(delta, debounce)); } } Message::WorkspaceOverview => { diff --git a/cosmic-applet-workspaces/src/wayland.rs b/cosmic-applet-workspaces/src/wayland.rs index b6403bed..7bac1662 100644 --- a/cosmic-applet-workspaces/src/wayland.rs +++ b/cosmic-applet-workspaces/src/wayland.rs @@ -17,9 +17,12 @@ use cctk::{ }; use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1; use futures::{channel::mpsc, executor::block_on, SinkExt}; -use std::os::{ - fd::{FromRawFd, RawFd}, - unix::net::UnixStream, +use std::{ + os::{ + fd::{FromRawFd, RawFd}, + unix::net::UnixStream, + }, + time::{Duration, Instant}, }; use wayland_client::backend::ObjectId; use wayland_client::{ @@ -32,7 +35,7 @@ use wayland_client::{Connection, QueueHandle, WEnum}; #[derive(Debug, Clone)] pub enum WorkspaceEvent { Activate(ObjectId), - Scroll(f64), + Scroll(f64, bool), } pub type WorkspaceList = Vec<(String, Option, ObjectId)>; @@ -80,6 +83,8 @@ pub fn spawn_workspaces(tx: mpsc::Sender) -> SyncSender) -> SyncSender { - // reset scroll if we're scrolling in the opposite direction - if state.scroll * v < 0.0 { + Event::Msg(WorkspaceEvent::Scroll(v, debounce)) => { + let dur = if debounce { + Duration::from_millis(350) + } else { + Duration::from_millis(200) + }; + if state.last_scroll.elapsed() > Duration::from_millis(100) + || state.scroll * v < 0.0 + { + state.next_scroll = None; state.scroll = 0.0; } + state.last_scroll = Instant::now(); + state.scroll += v; + if let Some(next) = state.next_scroll { + if next > Instant::now() { + return; + } + state.next_scroll = None; + } + if state.scroll.abs() < 1.0 { return; } + state.next_scroll = Some(Instant::now() + dur); if let Some((w_g, w_i)) = state .workspace_state .workspace_groups() @@ -190,6 +212,8 @@ pub struct State { workspace_state: WorkspaceState, have_workspaces: bool, scroll: f64, + next_scroll: Option, + last_scroll: Instant, } impl State {