Initial support for workspace pinning and moving
Adds support for cosmic-workspace-v2 pin, unpin, move_after, and move_before requests. Both features need some work with workspaces span displays mode, so that will need more fixes later. We also want to generate a unique id for pinned workspaces to send in the ext-workspace-v1 protocol. But that isn't a strict requirement for anything. So I haven't yet fully implemented that. We'll also want to persist other things, like workspace naming when that's added. Overall, though, with separate workspaces per display, this is working pretty well.
This commit is contained in:
parent
d1f4e7b12d
commit
96e9bf3b81
14 changed files with 622 additions and 118 deletions
168
src/shell/mod.rs
168
src/shell/mod.rs
|
|
@ -6,15 +6,20 @@ use layout::TilingExceptions;
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{atomic::Ordering, Mutex},
|
||||
thread,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use wayland_backend::server::ClientId;
|
||||
|
||||
use crate::wayland::{handlers::data_device, protocols::workspace::WorkspaceCapabilities};
|
||||
use crate::wayland::{
|
||||
handlers::data_device,
|
||||
protocols::workspace::{State as WState, WorkspaceCapabilities},
|
||||
};
|
||||
use cosmic_comp_config::{
|
||||
workspace::{WorkspaceLayout, WorkspaceMode},
|
||||
workspace::{PinnedWorkspace, WorkspaceLayout, WorkspaceMode},
|
||||
TileBehavior, ZoomConfig, ZoomMovement,
|
||||
};
|
||||
use cosmic_config::ConfigSet;
|
||||
use cosmic_protocols::workspace::v2::server::zcosmic_workspace_handle_v2::TilingState;
|
||||
use cosmic_settings_config::shortcuts::action::{Direction, FocusDirection, ResizeDirection};
|
||||
use cosmic_settings_config::{shortcuts, window_rules::ApplicationException};
|
||||
|
|
@ -38,10 +43,7 @@ use smithay::{
|
|||
},
|
||||
output::{Output, WeakOutput},
|
||||
reexports::{
|
||||
wayland_protocols::ext::{
|
||||
session_lock::v1::server::ext_session_lock_v1::ExtSessionLockV1,
|
||||
workspace::v1::server::ext_workspace_handle_v1::State as WState,
|
||||
},
|
||||
wayland_protocols::ext::session_lock::v1::server::ext_session_lock_v1::ExtSessionLockV1,
|
||||
wayland_server::{protocol::wl_surface::WlSurface, Client},
|
||||
},
|
||||
utils::{IsAlive, Logical, Point, Rectangle, Serial, Size},
|
||||
|
|
@ -54,6 +56,7 @@ use smithay::{
|
|||
},
|
||||
xwayland::X11Surface,
|
||||
};
|
||||
use tracing::error;
|
||||
|
||||
use crate::{
|
||||
backend::render::animations::spring::{Spring, SpringParams},
|
||||
|
|
@ -368,11 +371,48 @@ fn create_workspace(
|
|||
}
|
||||
state.set_workspace_capabilities(
|
||||
&workspace_handle,
|
||||
WorkspaceCapabilities::Activate | WorkspaceCapabilities::SetTilingState,
|
||||
WorkspaceCapabilities::Activate
|
||||
| WorkspaceCapabilities::SetTilingState
|
||||
| WorkspaceCapabilities::Pin
|
||||
| WorkspaceCapabilities::Move,
|
||||
);
|
||||
Workspace::new(workspace_handle, output.clone(), tiling, theme.clone())
|
||||
}
|
||||
|
||||
fn create_workspace_from_pinned(
|
||||
pinned: &PinnedWorkspace,
|
||||
state: &mut WorkspaceUpdateGuard<'_, State>,
|
||||
output: &Output,
|
||||
group_handle: &WorkspaceGroupHandle,
|
||||
active: bool,
|
||||
theme: cosmic::Theme,
|
||||
) -> Workspace {
|
||||
let workspace_handle = state
|
||||
.create_workspace(
|
||||
&group_handle,
|
||||
if pinned.tiling_enabled {
|
||||
TilingState::TilingEnabled
|
||||
} else {
|
||||
TilingState::FloatingOnly
|
||||
},
|
||||
// TODO Set id for persistent workspaces
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
state.add_workspace_state(&workspace_handle, WState::Pinned);
|
||||
if active {
|
||||
state.add_workspace_state(&workspace_handle, WState::Active);
|
||||
}
|
||||
state.set_workspace_capabilities(
|
||||
&workspace_handle,
|
||||
WorkspaceCapabilities::Activate
|
||||
| WorkspaceCapabilities::SetTilingState
|
||||
| WorkspaceCapabilities::Pin
|
||||
| WorkspaceCapabilities::Move,
|
||||
);
|
||||
Workspace::from_pinned(pinned, workspace_handle, output.clone(), theme.clone())
|
||||
}
|
||||
|
||||
/* We will probably need this again at some point
|
||||
fn merge_workspaces(
|
||||
mut workspace: Workspace,
|
||||
|
|
@ -545,7 +585,11 @@ impl WorkspaceSet {
|
|||
xdg_activation_state: &XdgActivationState,
|
||||
) {
|
||||
// add empty at the end, if necessary
|
||||
if self.workspaces.last().map_or(true, |last| !last.is_empty()) {
|
||||
if self
|
||||
.workspaces
|
||||
.last()
|
||||
.map_or(true, |last| !last.is_empty() || last.pinned)
|
||||
{
|
||||
self.add_empty_workspace(state);
|
||||
}
|
||||
|
||||
|
|
@ -556,8 +600,11 @@ impl WorkspaceSet {
|
|||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, workspace)| {
|
||||
let previous_is_empty =
|
||||
i > 0 && self.workspaces.get(i - 1).map_or(false, |w| w.is_empty());
|
||||
let previous_is_empty = i > 0
|
||||
&& self
|
||||
.workspaces
|
||||
.get(i - 1)
|
||||
.map_or(false, |w| w.is_empty() && !w.pinned);
|
||||
let keep = if workspace.can_auto_remove(xdg_activation_state) {
|
||||
// Keep empty workspace if it's active, or it's the last workspace,
|
||||
// and the previous worspace is not both active and empty.
|
||||
|
|
@ -650,6 +697,8 @@ pub struct Workspaces {
|
|||
autotile: bool,
|
||||
autotile_behavior: TileBehavior,
|
||||
theme: cosmic::Theme,
|
||||
// Persisted workspace to add on first `output_add`
|
||||
persisted_workspaces: Vec<PinnedWorkspace>,
|
||||
}
|
||||
|
||||
impl Workspaces {
|
||||
|
|
@ -662,6 +711,7 @@ impl Workspaces {
|
|||
autotile: config.cosmic_conf.autotile,
|
||||
autotile_behavior: config.cosmic_conf.autotile_behavior,
|
||||
theme,
|
||||
persisted_workspaces: config.cosmic_conf.pinned_workspaces.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -686,6 +736,20 @@ impl Workspaces {
|
|||
});
|
||||
workspace_state.add_group_output(&set.group, &output);
|
||||
|
||||
// If this is the first output added, create workspaces for pinned workspaces from config
|
||||
for pinned in std::mem::take(&mut self.persisted_workspaces) {
|
||||
tracing::error!("pinned workspace: {:?}", pinned);
|
||||
let workspace = create_workspace_from_pinned(
|
||||
&pinned,
|
||||
workspace_state,
|
||||
output,
|
||||
&set.group,
|
||||
false,
|
||||
self.theme.clone(),
|
||||
);
|
||||
set.workspaces.push(workspace);
|
||||
}
|
||||
|
||||
// Remove workspaces that prefer this output from other sets
|
||||
let mut moved_workspaces = self
|
||||
.sets
|
||||
|
|
@ -836,6 +900,73 @@ impl Workspaces {
|
|||
}
|
||||
}
|
||||
|
||||
// Move a workspace before/after a different workspace
|
||||
pub fn move_workspace(
|
||||
&mut self,
|
||||
handle: &WorkspaceHandle,
|
||||
other_handle: &WorkspaceHandle,
|
||||
workspace_state: &mut WorkspaceUpdateGuard<'_, State>,
|
||||
after: bool,
|
||||
) {
|
||||
if handle == other_handle {
|
||||
return;
|
||||
}
|
||||
|
||||
let (Some(old_output), Some(new_output)) = (
|
||||
self.space_for_handle(handle).map(|w| w.output.clone()),
|
||||
self.space_for_handle(other_handle)
|
||||
.map(|w| w.output.clone()),
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Check which workspace is active on the new set; before removing from the
|
||||
// old set in cause we're moving an active workspace within the same set.
|
||||
let new_set = &mut self.sets[&new_output];
|
||||
let previous_active_handle = new_set.workspaces[new_set.active].handle;
|
||||
|
||||
// Remove workspace from old set
|
||||
let old_set = &mut self.sets[&old_output];
|
||||
let mut workspace = if new_output != old_output {
|
||||
old_set.remove_workspace(workspace_state, handle).unwrap()
|
||||
} else {
|
||||
// If set is the same, just remove it here without adding empty workspace,
|
||||
// updating `active`, etc.
|
||||
let idx = old_set
|
||||
.workspaces
|
||||
.iter()
|
||||
.position(|w| w.handle == *handle)
|
||||
.unwrap();
|
||||
old_set.workspaces.remove(idx)
|
||||
};
|
||||
|
||||
let new_set = &mut self.sets[&new_output];
|
||||
|
||||
if new_output != old_output {
|
||||
workspace_state.remove_workspace_state(&workspace.handle, WState::Active);
|
||||
workspace_state.move_workspace_to_group(new_set.group, workspace.handle);
|
||||
workspace.set_output(&new_output, true);
|
||||
workspace.refresh();
|
||||
}
|
||||
|
||||
// Insert workspace into new set, relative to `other_handle`
|
||||
let idx = new_set
|
||||
.workspaces
|
||||
.iter()
|
||||
.position(|w| w.handle == *other_handle)
|
||||
.unwrap();
|
||||
let insert_idx = if after { idx + 1 } else { idx };
|
||||
new_set.workspaces.insert(insert_idx, workspace);
|
||||
|
||||
new_set.active = new_set
|
||||
.workspaces
|
||||
.iter()
|
||||
.position(|w| w.handle == previous_active_handle)
|
||||
.unwrap();
|
||||
|
||||
new_set.update_workspace_idxs(workspace_state);
|
||||
}
|
||||
|
||||
pub fn update_config(
|
||||
&mut self,
|
||||
config: &Config,
|
||||
|
|
@ -937,7 +1068,7 @@ impl Workspaces {
|
|||
.sets
|
||||
.values()
|
||||
.flat_map(|set| set.workspaces.last())
|
||||
.any(|w| w.mapped().next().is_some())
|
||||
.any(|w| !w.is_empty() || w.pinned)
|
||||
{
|
||||
for set in self.sets.values_mut() {
|
||||
set.add_empty_workspace(workspace_state);
|
||||
|
|
@ -1156,6 +1287,21 @@ impl Workspaces {
|
|||
self.autotile = autotile;
|
||||
self.apply_tile_change(guard, seats);
|
||||
}
|
||||
|
||||
pub fn persist(&self, config: &Config) {
|
||||
let pinned_workspaces: Vec<PinnedWorkspace> = self
|
||||
.sets
|
||||
.values()
|
||||
.flat_map(|set| &set.workspaces)
|
||||
.flat_map(|w| w.to_pinned())
|
||||
.collect();
|
||||
let config = config.cosmic_helper.clone();
|
||||
thread::spawn(move || {
|
||||
if let Err(err) = config.set("pinned_workspaces", pinned_workspaces) {
|
||||
error!(?err, "Failed to update pinned_workspaces key");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ use crate::{
|
|||
element::{AsGlowRenderer, FromGlesError},
|
||||
BackdropShader,
|
||||
},
|
||||
config::EdidProduct,
|
||||
shell::{
|
||||
layout::{floating::FloatingLayout, tiling::TilingLayout},
|
||||
OverviewMode, ANIMATION_DURATION,
|
||||
|
|
@ -19,6 +18,7 @@ use crate::{
|
|||
},
|
||||
},
|
||||
};
|
||||
use cosmic_comp_config::workspace::{OutputMatch, PinnedWorkspace};
|
||||
|
||||
use cosmic::theme::CosmicTheme;
|
||||
use cosmic_protocols::workspace::v2::server::zcosmic_workspace_handle_v2::TilingState;
|
||||
|
|
@ -74,30 +74,30 @@ use super::{
|
|||
|
||||
const FULLSCREEN_ANIMATION_DURATION: Duration = Duration::from_millis(200);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct OutputMatch {
|
||||
name: String,
|
||||
edid: Option<EdidProduct>,
|
||||
// For stable workspace id, generate random 24-bit integer, as a hex string
|
||||
// Must be compared with existing workspaces work uniqueness.
|
||||
// TODO: Assign an id to any workspace that is pinned
|
||||
pub fn random_id() -> String {
|
||||
let id = rand::random_range(0..(2 << 24));
|
||||
format!("{:x}", id)
|
||||
}
|
||||
|
||||
impl OutputMatch {
|
||||
fn for_output(output: &Output) -> Self {
|
||||
Self {
|
||||
name: output.name(),
|
||||
edid: output.edid().cloned(),
|
||||
}
|
||||
fn output_match_for_output(output: &Output) -> OutputMatch {
|
||||
OutputMatch {
|
||||
name: output.name(),
|
||||
edid: output.edid().cloned(),
|
||||
}
|
||||
}
|
||||
|
||||
// If `disambguate` is true, check that edid *and* connector name match.
|
||||
// Otherwise, match only edid (if it exists)
|
||||
fn matches(&self, output: &Output, disambiguate: bool) -> bool {
|
||||
if self.edid.as_ref() != output.edid() {
|
||||
false
|
||||
} else if disambiguate || self.edid.is_none() {
|
||||
self.name == output.name()
|
||||
} else {
|
||||
true
|
||||
}
|
||||
// If `disambguate` is true, check that edid *and* connector name match.
|
||||
// Otherwise, match only edid (if it exists)
|
||||
fn output_matches(output_match: &OutputMatch, output: &Output, disambiguate: bool) -> bool {
|
||||
if output_match.edid.as_ref() != output.edid() {
|
||||
false
|
||||
} else if disambiguate || output_match.edid.is_none() {
|
||||
output_match.name == output.name()
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -109,6 +109,7 @@ pub struct Workspace {
|
|||
pub minimized_windows: Vec<MinimizedWindow>,
|
||||
pub tiling_enabled: bool,
|
||||
pub fullscreen: Option<FullscreenSurface>,
|
||||
pub pinned: bool,
|
||||
|
||||
pub handle: WorkspaceHandle,
|
||||
pub focus_stack: FocusStacks,
|
||||
|
|
@ -269,7 +270,7 @@ impl Workspace {
|
|||
) -> Workspace {
|
||||
let tiling_layer = TilingLayout::new(theme.clone(), &output);
|
||||
let floating_layer = FloatingLayout::new(theme, &output);
|
||||
let output_match = OutputMatch::for_output(&output);
|
||||
let output_match = output_match_for_output(&output);
|
||||
|
||||
Workspace {
|
||||
output,
|
||||
|
|
@ -278,6 +279,7 @@ impl Workspace {
|
|||
tiling_enabled,
|
||||
minimized_windows: Vec::new(),
|
||||
fullscreen: None,
|
||||
pinned: false,
|
||||
handle,
|
||||
focus_stack: FocusStacks::default(),
|
||||
screencopy: ScreencopySessions::default(),
|
||||
|
|
@ -291,6 +293,55 @@ impl Workspace {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_pinned(
|
||||
pinned: &PinnedWorkspace,
|
||||
handle: WorkspaceHandle,
|
||||
output: Output,
|
||||
theme: cosmic::Theme,
|
||||
) -> Self {
|
||||
let tiling_layer = TilingLayout::new(theme.clone(), &output);
|
||||
let floating_layer = FloatingLayout::new(theme, &output);
|
||||
let output_match = output_match_for_output(&output);
|
||||
|
||||
Workspace {
|
||||
output,
|
||||
tiling_layer,
|
||||
floating_layer,
|
||||
tiling_enabled: pinned.tiling_enabled,
|
||||
minimized_windows: Vec::new(),
|
||||
fullscreen: None,
|
||||
pinned: true,
|
||||
handle,
|
||||
focus_stack: FocusStacks::default(),
|
||||
screencopy: ScreencopySessions::default(),
|
||||
output_stack: {
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back(pinned.output.clone());
|
||||
if output_match != pinned.output {
|
||||
queue.push_back(output_match);
|
||||
}
|
||||
queue
|
||||
},
|
||||
backdrop_id: Id::new(),
|
||||
dirty: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_pinned(&self) -> Option<PinnedWorkspace> {
|
||||
let output = self.explicit_output().clone();
|
||||
if self.pinned {
|
||||
Some(PinnedWorkspace {
|
||||
output: cosmic_comp_config::workspace::OutputMatch {
|
||||
name: output.name,
|
||||
edid: output.edid,
|
||||
},
|
||||
tiling_enabled: self.tiling_enabled,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[profiling::function]
|
||||
pub fn refresh(&mut self) {
|
||||
// TODO: `Option::take_if` once stabilitized
|
||||
|
|
@ -316,9 +367,9 @@ impl Workspace {
|
|||
}
|
||||
|
||||
// Auto-removal of workspaces is allowed if empty, unless blocked by an
|
||||
// unused and unexpired activation token.
|
||||
// unused and unexpired activation token, or pinned.
|
||||
pub fn can_auto_remove(&self, xdg_activation_state: &XdgActivationState) -> bool {
|
||||
self.is_empty() && !self.has_activation_token(xdg_activation_state)
|
||||
self.is_empty() && !self.has_activation_token(xdg_activation_state) && !self.pinned
|
||||
}
|
||||
|
||||
pub fn refresh_focus_stack(&mut self) {
|
||||
|
|
@ -389,6 +440,11 @@ impl Workspace {
|
|||
&self.output
|
||||
}
|
||||
|
||||
/// Output workspace was originally created on, or explicitly moved to by the user
|
||||
fn explicit_output(&self) -> &OutputMatch {
|
||||
self.output_stack.front().unwrap()
|
||||
}
|
||||
|
||||
// Set output the workspace is on
|
||||
//
|
||||
// If `explicit` is `true`, the user has explicitly moved the workspace
|
||||
|
|
@ -414,21 +470,21 @@ impl Workspace {
|
|||
if let Some(pos) = self
|
||||
.output_stack
|
||||
.iter()
|
||||
.position(|i| i.matches(output, true))
|
||||
.position(|i| output_matches(i, output, true))
|
||||
{
|
||||
// Matched edid and connector name
|
||||
self.output_stack.truncate(pos + 1);
|
||||
} else if let Some(pos) = self
|
||||
.output_stack
|
||||
.iter()
|
||||
.position(|i| i.matches(output, false))
|
||||
.position(|i| output_matches(i, output, false))
|
||||
{
|
||||
// Matched edid but not connector name; truncate entries that don't match edid,
|
||||
// but keep old entry in case we see two outputs with the same edid.
|
||||
self.output_stack.truncate(pos + 1);
|
||||
self.output_stack.push_back(OutputMatch::for_output(output));
|
||||
self.output_stack.push_back(output_match_for_output(output));
|
||||
} else {
|
||||
self.output_stack.push_back(OutputMatch::for_output(output));
|
||||
self.output_stack.push_back(output_match_for_output(output));
|
||||
}
|
||||
self.output = output.clone();
|
||||
}
|
||||
|
|
@ -440,7 +496,7 @@ impl Workspace {
|
|||
.is_some_and(|edid| self.output().edid() == Some(edid));
|
||||
self.output_stack
|
||||
.iter()
|
||||
.any(|i| i.matches(output, disambiguate))
|
||||
.any(|i| output_matches(i, output, disambiguate))
|
||||
}
|
||||
|
||||
pub fn unmap(&mut self, mapped: &CosmicMapped) -> Option<ManagedState> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue