Skip to content

Commit d7d9f9e

Browse files
committed
TODO workspaces: Use libcosmic DiscreteScrollState helper
1 parent 91ae469 commit d7d9f9e

File tree

3 files changed

+48
-83
lines changed

3 files changed

+48
-83
lines changed

Cargo.lock

Lines changed: 30 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ sctk = { package = "smithay-client-toolkit", version = "0.20.0" }
8686
[patch."https://github.com/pop-os/cosmic-protocols"]
8787
cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols//", branch = "main" }
8888
cosmic-client-toolkit = { git = "https://github.com/pop-os/cosmic-protocols//", branch = "main" }
89+
90+
[patch."https://github.com/pop-os/libcosmic"]
91+
libcosmic = { git = "https://github.com/pop-os/libcosmic//", branch = "discrete-scroll" }
92+
cosmic-config = { git = "https://github.com/pop-os/libcosmic//", branch = "discrete-scroll" }
93+
iced_futures = { git = "https://github.com/pop-os/libcosmic//", branch = "discrete-scroll" }

cosmic-applet-workspaces/src/components/app.rs

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4037
static 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

6257
impl 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,54 +141,20 @@ 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-
};
155-
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);
144+
let discrete_delta = self.scroll.update(delta);
181145
if let Some(w_i) = self
182146
.workspaces
183147
.iter()
184148
.position(|w| w.state.contains(ext_workspace_handle_v1::State::Active))
185149
{
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) {
196-
if let Some(tx) = self.workspace_tx.as_mut() {
197-
let _ = tx.try_send(WorkspaceEvent::Activate(w.handle.clone()));
198-
}
150+
let d_i = (w_i as isize - discrete_delta.y)
151+
.rem_euclid(self.workspaces.len() as isize)
152+
as usize;
153+
154+
if let Some(tx) = self.workspace_tx.as_mut() {
155+
let _ = tx.try_send(WorkspaceEvent::Activate(
156+
self.workspaces[d_i].handle.clone(),
157+
));
199158
}
200159
}
201160
}

0 commit comments

Comments
 (0)