@@ -21,6 +21,7 @@ use cosmic::{
2121 widget:: { button, column, row} ,
2222 } ,
2323 iced_core:: { Background , Border } ,
24+ scroll:: DiscreteScrollState ,
2425 surface,
2526 widget:: { Id , autosize, container, horizontal_space, vertical_space} ,
2627} ;
@@ -31,11 +32,7 @@ use crate::{
3132 wayland_subscription:: { WorkspacesUpdate , workspaces} ,
3233} ;
3334
34- use std:: {
35- process:: Command as ShellCommand ,
36- sync:: LazyLock ,
37- time:: { Duration , Instant } ,
38- } ;
35+ use std:: { process:: Command as ShellCommand , sync:: LazyLock } ;
3936
4037static AUTOSIZE_MAIN_ID : LazyLock < Id > = LazyLock :: new ( || Id :: new ( "autosize-main" ) ) ;
4138
@@ -54,9 +51,7 @@ struct IcedWorkspacesApplet {
5451 workspaces : Vec < Workspace > ,
5552 workspace_tx : Option < SyncSender < WorkspaceEvent > > ,
5653 layout : Layout ,
57- scroll : f64 ,
58- next_scroll : Option < Instant > ,
59- last_scroll : Instant ,
54+ scroll : DiscreteScrollState ,
6055}
6156
6257impl IcedWorkspacesApplet {
@@ -111,9 +106,7 @@ impl cosmic::Application for IcedWorkspacesApplet {
111106 core,
112107 workspaces : Vec :: new ( ) ,
113108 workspace_tx : Option :: default ( ) ,
114- scroll : 0.0 ,
115- next_scroll : None ,
116- last_scroll : Instant :: now ( ) ,
109+ scroll : DiscreteScrollState :: default ( ) ,
117110 } ,
118111 Task :: none ( ) ,
119112 )
@@ -148,53 +141,21 @@ impl cosmic::Application for IcedWorkspacesApplet {
148141 }
149142 }
150143 Message :: WheelScrolled ( delta) => {
151- let ( delta, debounce) = match delta {
152- ScrollDelta :: Lines { x, y } => ( ( x + y) as f64 , false ) ,
153- ScrollDelta :: Pixels { x, y } => ( ( x + y) as f64 , true ) ,
154- } ;
144+ let discrete_delta = self . scroll . update ( delta) ;
145+ if discrete_delta. y != 0 {
146+ if let Some ( w_i) = self
147+ . workspaces
148+ . iter ( )
149+ . position ( |w| w. state . contains ( ext_workspace_handle_v1:: State :: Active ) )
150+ {
151+ let d_i = ( w_i as isize - discrete_delta. y )
152+ . rem_euclid ( self . workspaces . len ( ) as isize )
153+ as usize ;
155154
156- let dur = if debounce {
157- Duration :: from_millis ( 350 )
158- } else {
159- Duration :: from_millis ( 200 )
160- } ;
161- if self . last_scroll . elapsed ( ) > Duration :: from_millis ( 100 )
162- || self . scroll * delta < 0.0
163- {
164- self . next_scroll = None ;
165- self . scroll = 0.0 ;
166- }
167- self . last_scroll = Instant :: now ( ) ;
168-
169- self . scroll += delta;
170- if let Some ( next) = self . next_scroll {
171- if next > Instant :: now ( ) {
172- return cosmic:: iced:: Task :: none ( ) ;
173- }
174- self . next_scroll = None ;
175- }
176-
177- if self . scroll . abs ( ) < 1.0 {
178- return cosmic:: iced:: Task :: none ( ) ;
179- }
180- self . next_scroll = Some ( Instant :: now ( ) + dur) ;
181- if let Some ( w_i) = self
182- . workspaces
183- . iter ( )
184- . position ( |w| w. state . contains ( ext_workspace_handle_v1:: State :: Active ) )
185- {
186- let max_w = self . workspaces . len ( ) . wrapping_sub ( 1 ) ;
187- let d_i = if self . scroll > 0.0 {
188- if w_i == 0 { max_w } else { w_i. wrapping_sub ( 1 ) }
189- } else if w_i == max_w {
190- 0
191- } else {
192- w_i. wrapping_add ( 1 )
193- } ;
194- self . scroll = 0.0 ;
195- if let Some ( w) = self . workspaces . get ( d_i) {
196155 if let Some ( tx) = self . workspace_tx . as_mut ( ) {
197- let _ = tx. try_send ( WorkspaceEvent :: Activate ( w. handle . clone ( ) ) ) ;
156+ let _ = tx. try_send ( WorkspaceEvent :: Activate (
157+ self . workspaces [ d_i] . handle . clone ( ) ,
158+ ) ) ;
198159 }
199160 }
200161 }
0 commit comments