Skip to content

Commit

Permalink
Better support for scratchpad
Browse files Browse the repository at this point in the history
  • Loading branch information
johnae committed May 24, 2023
1 parent 07b70cb commit e0f5252
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
15 changes: 11 additions & 4 deletions src/server/event_handlers/layout/spiral.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use super::super::traits::WindowEventHandler;
use crate::{node_ext::NodeExt, utils};
use crate::{
node_ext::NodeExt,
utils::{is_persway_tmp_workspace, is_scratchpad_workspace},
};

use anyhow::Result;
use async_trait::async_trait;
use swayipc_async::{Connection, WindowChange, WindowEvent};
use swayipc_async::{Connection, WindowChange, WindowEvent, Workspace};

pub struct Spiral {
connection: Connection,
}

fn should_skip_layout_of_workspace(workspace: &Workspace) -> bool {
is_persway_tmp_workspace(workspace) || is_scratchpad_workspace(workspace)
}

impl Spiral {
pub async fn handle(event: Box<WindowEvent>) {
if let Ok(mut manager) = Self::new().await {
Expand All @@ -28,8 +35,8 @@ impl Spiral {
.find_as_ref(|n| n.id == event.container.id)
.expect(&format!("no node found with id {}", event.container.id));
let ws = node.get_workspace().await?;
if ws.name == utils::PERSWAY_TMP_WORKSPACE {
log::debug!("skip spiral layout of tmp workspace");
if should_skip_layout_of_workspace(&ws) {
log::debug!("skip spiral layout of \"special\" workspace");
return Ok(());
}
if !(node.is_floating_window()
Expand Down
41 changes: 29 additions & 12 deletions src/server/event_handlers/layout/stack_main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use crate::{
layout::StackLayout,
node_ext::NodeExt,
utils::{self, get_focused_workspace},
utils::{get_focused_workspace, is_persway_tmp_workspace, is_scratchpad_workspace},
};

use anyhow::Result;
use async_trait::async_trait;
use swayipc_async::{Connection, WindowChange, WindowEvent};
use swayipc_async::{Connection, WindowChange, WindowEvent, Workspace};

use super::super::traits::WindowEventHandler;

fn should_skip_layout_of_workspace(workspace: &Workspace) -> bool {
is_persway_tmp_workspace(workspace) || is_scratchpad_workspace(workspace)
}

pub struct StackMain {
connection: Connection,
size: u8,
Expand Down Expand Up @@ -38,8 +42,8 @@ impl StackMain {
.find_as_ref(|n| n.id == event.container.id)
.expect(&format!("no node found with id {}", event.container.id));
let ws = node.get_workspace().await?;
if ws.name == utils::PERSWAY_TMP_WORKSPACE {
log::debug!("skip stack_main layout of tmp workspace");
if should_skip_layout_of_workspace(&ws) {
log::debug!("skip stack_main layout of \"special\" workspace");
return Ok(());
}
let wstree = tree.find_as_ref(|n| n.id == ws.id).unwrap();
Expand Down Expand Up @@ -111,10 +115,11 @@ impl StackMain {
async fn on_close_window(&mut self, event: &WindowEvent) -> Result<()> {
let tree = self.connection.get_tree().await?;
let ws = get_focused_workspace(&mut self.connection).await?;
if ws.name == utils::PERSWAY_TMP_WORKSPACE {
log::debug!("skip stack_main layout of tmp workspace");
if should_skip_layout_of_workspace(&ws) {
log::debug!("skip stack_main layout of \"special\" workspace");
return Ok(());
}

let wstree = tree.find_as_ref(|n| n.id == ws.id).unwrap();

if wstree.nodes.len() == 1 {
Expand Down Expand Up @@ -156,14 +161,26 @@ impl StackMain {
}
async fn on_move_window(&mut self, event: &WindowEvent) -> Result<()> {
let tree = self.connection.get_tree().await?;
let node = tree
.find_as_ref(|n| n.id == event.container.id)
.expect(&format!("no node found with id {}", event.container.id));
let ws = node.get_workspace().await?;
if ws.name == utils::PERSWAY_TMP_WORKSPACE {
log::debug!("skip stack_main layout of tmp workspace");

let node = if let Some(node) = tree.find_as_ref(|n| n.id == event.container.id) {
node
} else {
log::warn!("no node found with id {}", event.container.id);
return Ok(());
};

let ws = if let Ok(ws) = node.get_workspace().await {
ws
} else {
log::warn!("node had no workspace");
return self.on_close_window(event).await;
};

if should_skip_layout_of_workspace(&ws) {
log::debug!("skip stack_main layout of \"special\" workspace");
return Ok(());
}

let focused_ws = get_focused_workspace(&mut self.connection).await?;

if ws.id == focused_ws.id {
Expand Down
10 changes: 7 additions & 3 deletions src/server/event_handlers/misc/workspace_renamer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ use crate::utils;

use anyhow::Result;
use async_trait::async_trait;
use swayipc_async::{Connection, WindowChange, WindowEvent};
use swayipc_async::{Connection, WindowChange, WindowEvent, Workspace};

pub struct WorkspaceRenamer {
connection: Connection,
}

fn should_skip_rename_of_workspace(workspace: &Workspace) -> bool {
utils::is_persway_tmp_workspace(workspace) || utils::is_scratchpad_workspace(workspace)
}

fn get_app_name(event: &WindowEvent) -> Option<String> {
let app_id =
event
Expand Down Expand Up @@ -56,8 +60,8 @@ impl WorkspaceRenamer {
async fn rename_workspace(&mut self, event: WindowEvent) -> Result<()> {
log::debug!("workspace name manager handling event: {:?}", event.change);
let focused_ws = utils::get_focused_workspace(&mut self.connection).await?;
if focused_ws.name == utils::PERSWAY_TMP_WORKSPACE {
log::debug!("workspace name manager skip tmp workspace");
if should_skip_rename_of_workspace(&focused_ws) {
log::debug!("workspace name manager skip renaming workspace");
return Ok(());
}

Expand Down
9 changes: 9 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{future::Future, time::Duration};
use swayipc_async::{Connection, Node, Workspace};

pub const PERSWAY_TMP_WORKSPACE: &str = "◕‿◕";
pub const SCRATCHPAD_WORKSPACE: &str = "__i3_scratch";

pub async fn get_focused_workspace(conn: &mut Connection) -> Result<Workspace> {
let mut ws = conn.get_workspaces().await?.into_iter();
Expand Down Expand Up @@ -35,6 +36,14 @@ pub fn get_socket_path(socket_path: Option<String>) -> String {
})
}

pub fn is_scratchpad_workspace(ws: &Workspace) -> bool {
ws.name == SCRATCHPAD_WORKSPACE
}

pub fn is_persway_tmp_workspace(ws: &Workspace) -> bool {
ws.name == PERSWAY_TMP_WORKSPACE
}

pub async fn relayout_workspace<F, C>(ws_num: i32, f: C) -> Result<()>
where
F: Future<Output = Result<()>>,
Expand Down

0 comments on commit e0f5252

Please sign in to comment.