From 23ac3d73232f307186212293713d6801d37cff2a Mon Sep 17 00:00:00 2001 From: FluxTape Date: Tue, 19 Mar 2024 14:27:52 +0000 Subject: 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 --- src/layout/monitor.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src/layout/monitor.rs') 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 { pub workspaces: Vec>, /// Index of the currently active workspace. pub active_workspace_idx: usize, + /// ID of the previously active workspace. + pub previous_workspace_id: Option, /// In-progress switch between workspaces. pub workspace_switch: Option, /// Configurable properties of the layout. @@ -89,6 +92,7 @@ impl Monitor { output, workspaces, active_workspace_idx: 0, + previous_workspace_id: None, workspace_switch: None, options, } @@ -114,6 +118,8 @@ impl Monitor { .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 Monitor { )); } + fn previous_workspace_idx(&self) -> Option { + 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 Monitor { 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 Monitor { 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 Monitor { 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 Monitor { 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, -- cgit