aboutsummaryrefslogtreecommitdiff
path: root/src/layout/monitor.rs
diff options
context:
space:
mode:
authorFluxTape <fluxtape.contact@gmail.com>2024-03-19 14:27:52 +0000
committerGitHub <noreply@github.com>2024-03-19 07:27:52 -0700
commit23ac3d73232f307186212293713d6801d37cff2a (patch)
tree68d4e721a3c3e88cae2157a27cd28944d1eb3ea4 /src/layout/monitor.rs
parentc3327d36da25f37e86eb8f89bd74f2a4bc1ea744 (diff)
downloadniri-23ac3d73232f307186212293713d6801d37cff2a.tar.gz
niri-23ac3d73232f307186212293713d6801d37cff2a.tar.bz2
niri-23ac3d73232f307186212293713d6801d37cff2a.zip
Workspace back and forth (#253)
* implement workspace back and forth * Make our own ID counter instead of SerialCounter, use a newtype * Rename FocusWorkspaceBackAndForth to FocusWorkspacePrevious * Add focus-workspace-previous to tests * Don't special case in switch_workspace_previous * Minor clean up * Add switch_workspace_auto_back_and_forth to tests * Skip animation on switch_workspace_previous * Preserve previous_workspace_id on workspace movement * Make Workspace::id private with a getter Reduce the chance it gets overwritten. * Add test for workspace ID uniqueness * Update previous workspace ID upon moving workspace across monitors --------- Co-authored-by: Ivan Molodetskikh <yalterz@gmail.com>
Diffstat (limited to 'src/layout/monitor.rs')
-rw-r--r--src/layout/monitor.rs37
1 files changed, 36 insertions, 1 deletions
diff --git a/src/layout/monitor.rs b/src/layout/monitor.rs
index 6660d1b2..dd5709dd 100644
--- a/src/layout/monitor.rs
+++ b/src/layout/monitor.rs
@@ -10,7 +10,8 @@ use smithay::output::Output;
use smithay::utils::{Logical, Point, Rectangle, Scale};
use super::workspace::{
- compute_working_area, Column, ColumnWidth, OutputId, Workspace, WorkspaceRenderElement,
+ compute_working_area, Column, ColumnWidth, OutputId, Workspace, WorkspaceId,
+ WorkspaceRenderElement,
};
use super::{LayoutElement, Options};
use crate::animation::Animation;
@@ -35,6 +36,8 @@ pub struct Monitor<W: LayoutElement> {
pub workspaces: Vec<Workspace<W>>,
/// Index of the currently active workspace.
pub active_workspace_idx: usize,
+ /// ID of the previously active workspace.
+ pub previous_workspace_id: Option<WorkspaceId>,
/// In-progress switch between workspaces.
pub workspace_switch: Option<WorkspaceSwitch>,
/// Configurable properties of the layout.
@@ -89,6 +92,7 @@ impl<W: LayoutElement> Monitor<W> {
output,
workspaces,
active_workspace_idx: 0,
+ previous_workspace_id: None,
workspace_switch: None,
options,
}
@@ -114,6 +118,8 @@ impl<W: LayoutElement> Monitor<W> {
.map(|s| s.current_idx())
.unwrap_or(self.active_workspace_idx as f64);
+ self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id());
+
self.active_workspace_idx = idx;
self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
@@ -461,6 +467,11 @@ impl<W: LayoutElement> Monitor<W> {
));
}
+ fn previous_workspace_idx(&self) -> Option<usize> {
+ let id = self.previous_workspace_id?;
+ self.workspaces.iter().position(|w| w.id() == id)
+ }
+
pub fn switch_workspace(&mut self, idx: usize) {
self.activate_workspace(min(idx, self.workspaces.len() - 1));
// Don't animate this action.
@@ -469,6 +480,24 @@ impl<W: LayoutElement> Monitor<W> {
self.clean_up_workspaces();
}
+ pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) {
+ let idx = min(idx, self.workspaces.len() - 1);
+
+ if idx == self.active_workspace_idx {
+ if let Some(prev_idx) = self.previous_workspace_idx() {
+ self.switch_workspace(prev_idx);
+ }
+ } else {
+ self.switch_workspace(idx);
+ }
+ }
+
+ pub fn switch_workspace_previous(&mut self) {
+ if let Some(idx) = self.previous_workspace_idx() {
+ self.switch_workspace(idx);
+ }
+ }
+
pub fn consume_into_column(&mut self) {
self.active_workspace().consume_into_column();
}
@@ -564,8 +593,10 @@ impl<W: LayoutElement> Monitor<W> {
self.workspaces.push(ws);
}
+ let previous_workspace_id = self.previous_workspace_id;
self.activate_workspace(new_idx);
self.workspace_switch = None;
+ self.previous_workspace_id = previous_workspace_id;
self.clean_up_workspaces();
}
@@ -584,8 +615,10 @@ impl<W: LayoutElement> Monitor<W> {
self.workspaces.push(ws);
}
+ let previous_workspace_id = self.previous_workspace_id;
self.activate_workspace(new_idx);
self.workspace_switch = None;
+ self.previous_workspace_id = previous_workspace_id;
self.clean_up_workspaces();
}
@@ -833,6 +866,8 @@ impl<W: LayoutElement> Monitor<W> {
gesture.center_idx as f64 + current_pos,
);
+ self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id());
+
self.active_workspace_idx = new_idx;
self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
gesture.current_idx,