mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-05-18 09:46:58 +02:00
feat(wm): add named workspace commands
This commit introduces three new commands, ensure-named-workspaces, named-workspace-rule, and focus-named-workspace, which email to reduce the configuration complexity by allowing users to refer to workspace names instead of monitor and workspace indices.
This commit is contained in:
@@ -208,6 +208,18 @@ impl WindowManager {
|
||||
|
||||
self.enforce_workspace_rules()?;
|
||||
}
|
||||
SocketMessage::NamedWorkspaceRule(_, ref id, ref workspace) => {
|
||||
if let Some((monitor_idx, workspace_idx)) =
|
||||
self.monitor_workspace_index_by_name(workspace)
|
||||
{
|
||||
{
|
||||
let mut workspace_rules = WORKSPACE_RULES.lock();
|
||||
workspace_rules.insert(id.to_string(), (monitor_idx, workspace_idx));
|
||||
}
|
||||
|
||||
self.enforce_workspace_rules()?;
|
||||
}
|
||||
}
|
||||
SocketMessage::ManageRule(_, ref id) => {
|
||||
let mut manage_identifiers = MANAGE_IDENTIFIERS.lock();
|
||||
if !manage_identifiers.contains(id) {
|
||||
@@ -453,6 +465,25 @@ impl WindowManager {
|
||||
self.focus_monitor(monitor_idx)?;
|
||||
self.focus_workspace(workspace_idx)?;
|
||||
}
|
||||
SocketMessage::FocusNamedWorkspace(ref name) => {
|
||||
let reenable_border = if BORDER_ENABLED.load(Ordering::SeqCst) {
|
||||
self.hide_border()?;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if let Some((monitor_idx, workspace_idx)) =
|
||||
self.monitor_workspace_index_by_name(name)
|
||||
{
|
||||
self.focus_monitor(monitor_idx)?;
|
||||
self.focus_workspace(workspace_idx)?;
|
||||
}
|
||||
|
||||
if reenable_border {
|
||||
self.show_border()?;
|
||||
}
|
||||
}
|
||||
SocketMessage::Stop => {
|
||||
tracing::info!(
|
||||
"received stop command, restoring all hidden windows and terminating process"
|
||||
@@ -480,6 +511,9 @@ impl WindowManager {
|
||||
SocketMessage::EnsureWorkspaces(monitor_idx, workspace_count) => {
|
||||
self.ensure_workspaces_for_monitor(monitor_idx, workspace_count)?;
|
||||
}
|
||||
SocketMessage::EnsureNamedWorkspaces(monitor_idx, ref names) => {
|
||||
self.ensure_named_workspaces_for_monitor(monitor_idx, names)?;
|
||||
}
|
||||
SocketMessage::NewWorkspace => {
|
||||
self.new_workspace()?;
|
||||
}
|
||||
|
||||
@@ -1876,6 +1876,30 @@ impl WindowManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn ensure_named_workspaces_for_monitor(
|
||||
&mut self,
|
||||
monitor_idx: usize,
|
||||
names: &Vec<String>,
|
||||
) -> Result<()> {
|
||||
tracing::info!("ensuring workspace count");
|
||||
|
||||
let monitor = self
|
||||
.monitors_mut()
|
||||
.get_mut(monitor_idx)
|
||||
.ok_or_else(|| anyhow!("there is no monitor"))?;
|
||||
|
||||
monitor.ensure_workspace_count(names.len());
|
||||
|
||||
for (workspace_idx, name) in names.iter().enumerate() {
|
||||
if let Some(workspace) = monitor.workspaces_mut().get_mut(workspace_idx) {
|
||||
workspace.set_name(Option::from(name.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn set_workspace_padding(
|
||||
&mut self,
|
||||
@@ -2045,6 +2069,23 @@ impl WindowManager {
|
||||
self.update_focused_workspace(false)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn monitor_workspace_index_by_name(&mut self, name: &str) -> Option<(usize, usize)> {
|
||||
tracing::info!("looking up workspace by name");
|
||||
|
||||
for (monitor_idx, monitor) in self.monitors().iter().enumerate() {
|
||||
for (workspace_idx, workspace) in monitor.workspaces().iter().enumerate() {
|
||||
if let Some(workspace_name) = workspace.name() {
|
||||
if workspace_name == name {
|
||||
return Option::from((monitor_idx, workspace_idx));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn new_workspace(&mut self) -> Result<()> {
|
||||
tracing::info!("adding new workspace");
|
||||
|
||||
@@ -26,7 +26,7 @@ use crate::INITIAL_CONFIGURATION_LOADED;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Getters, CopyGetters, MutGetters, Setters, JsonSchema)]
|
||||
pub struct Workspace {
|
||||
#[getset(set = "pub")]
|
||||
#[getset(get = "pub", set = "pub")]
|
||||
name: Option<String>,
|
||||
containers: Ring<Container>,
|
||||
#[getset(get = "pub", get_mut = "pub", set = "pub")]
|
||||
|
||||
Reference in New Issue
Block a user