Skip to content

Commit

Permalink
Marking nodes with ws stack marks or main marks isn't necessary anymo…
Browse files Browse the repository at this point in the history
…re (#35)

Remove all node marking except in one instance (which is just a very brief marking at that). The marks aren't needed anymore and were mostly just leftovers from earlier experiments.
  • Loading branch information
johnae committed Feb 24, 2023
1 parent 7402044 commit d11e59c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 86 deletions.
28 changes: 10 additions & 18 deletions src/server/command_handlers/layout/stack_main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
node_ext::NodeExt,
utils::{get_focused_workspace, get_main_mark, get_stack_mark},
};
use crate::{node_ext::NodeExt, utils::get_focused_workspace};
use anyhow::Result;
use either::Either;
use swayipc_async::Connection;
Expand All @@ -20,9 +17,8 @@ impl StackMain {
let tree = self.connection.get_tree().await?;
let ws = get_focused_workspace(&mut self.connection).await?;
let wstree = tree.find_as_ref(|n| n.id == ws.id).unwrap();
let stack_mark = get_stack_mark(ws.id);

if let Some(stack) = wstree.find_as_ref(|n| n.marks.contains(&stack_mark)) {
if let Some(stack) = wstree.nodes.first() {
if stack.nodes.len() == 0 {
return Ok(());
}
Expand Down Expand Up @@ -76,14 +72,13 @@ impl StackMain {
let tree = self.connection.get_tree().await?;
let ws = get_focused_workspace(&mut self.connection).await?;
let wstree = tree.find_as_ref(|n| n.id == ws.id).unwrap();
let stack_mark = get_stack_mark(ws.id);
let main_mark = get_main_mark(ws.id);

if let Some(stack) = wstree.find_as_ref(|n| n.marks.contains(&stack_mark)) {
if let Some(stack) = wstree.nodes.first() {
if stack.nodes.len() == 0 {
return Ok(());
}

let main = wstree.nodes.last().expect("main window not found");
let stack_leaves = stack.iter().filter(|n| n.is_window());

let mut stack_leaves_next = stack_leaves.clone();
Expand All @@ -101,9 +96,9 @@ impl StackMain {
}
}
cmd.push_str(&format!(
"[con_id={}] focus; [con_mark={}] focus; ",
"[con_id={}] focus; [con_id={}] focus; ",
stack.nodes.last().unwrap().id,
main_mark
main.id
));
log::debug!("stack main controller, master cycle next 1: {}", cmd);
self.connection.run_command(cmd).await?;
Expand All @@ -123,9 +118,8 @@ impl StackMain {
.unwrap();

let cmd = format!(
"[con_id={}] focus; swap container with con_id {}; [con_id={}] focus; [con_mark={}] unmark {}; [con_id={}] mark --add {}",
"[con_id={}] focus; swap container with con_id {}; [con_id={}] focus",
main.id, stack_first, stack_first,
main_mark, main_mark, stack_first, main_mark
);
log::debug!("stack main controller, master cycle next 2: {}", cmd);
self.connection.run_command(cmd).await?;
Expand All @@ -138,10 +132,8 @@ impl StackMain {
let tree = self.connection.get_tree().await?;
let ws = get_focused_workspace(&mut self.connection).await?;
let wstree = tree.find_as_ref(|n| n.id == ws.id).unwrap();
let stack_mark = get_stack_mark(ws.id);
let main_mark = get_main_mark(ws.id);

if let Some(stack) = wstree.find_as_ref(|n| n.marks.contains(&stack_mark)) {
if let Some(stack) = wstree.nodes.first() {
if stack.nodes.len() == 0 {
return Ok(());
}
Expand All @@ -163,8 +155,8 @@ impl StackMain {
};

let cmd = format!(
"[con_id={}] focus; swap container with con_id {}; [con_id={}] focus; [con_mark={}] unmark {}; [con_id={}] mark --add {}",
main.id, stack_current.id, stack_current.id, main_mark, main_mark, stack_current.id, main_mark
"[con_id={}] focus; swap container with con_id {}; [con_id={}] focus",
main.id, stack_current.id, stack_current.id
);
log::debug!("stack main controller, swap visible: {}", cmd);
self.connection.run_command(cmd).await?;
Expand Down
58 changes: 26 additions & 32 deletions src/server/event_handlers/layout/stack_main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
layout::StackLayout,
node_ext::NodeExt,
utils::{self, get_focused_workspace, get_main_mark, get_stack_mark},
utils::{self, get_focused_workspace},
};

use anyhow::Result;
Expand Down Expand Up @@ -43,46 +43,39 @@ impl StackMain {
return Ok(());
}
let wstree = tree.find_as_ref(|n| n.id == ws.id).unwrap();
log::debug!("new_window: {:?}", event.container.id);
log::debug!("nodes_len: {}", wstree.nodes.len());
log::debug!("wstree: {:?}", wstree);
log::debug!("new_window id: {}", event.container.id);
log::debug!("workspace nodes len: {}", wstree.nodes.len());
let layout = match self.stack_layout {
StackLayout::Tabbed => "split v; layout tabbed",
StackLayout::Stacked => "split v; layout stacking",
StackLayout::Tiled => "split v",
};
match wstree.nodes.len() {
1 => {
let main_mark = get_main_mark(ws.id);
let cmd = format!(
"[con_mark={}] unmark {}; [con_id={}] focus; split h; [con_id={}] mark --add {}",
main_mark, main_mark, event.container.id, event.container.id, main_mark
);
let cmd = format!("[con_id={}] focus; split h", event.container.id);
self.connection.run_command(cmd).await?;
Ok(())
}
2 => {
let stack_mark = get_stack_mark(ws.id);
let main_mark = get_main_mark(ws.id);
let main = wstree.nodes.last().expect("main window not found");
let stack = wstree.nodes.first().expect("stack container not found");

let cmd = if stack.is_window() {
format!(
"[con_id={}] mark --add {}; [con_id={}] focus; {}; resize set width {}; [con_id={}] focus parent; mark --add {}; [con_id={}] focus",
main.id, main_mark, stack.id, layout, (100 - self.size), stack.id, stack_mark, main.id
"[con_id={}] focus; {}; resize set width {}; [con_id={}] focus",
stack.id,
layout,
(100 - self.size),
main.id
)
} else {
if let Some(node) = stack.find_as_ref(|n| n.id == event.container.id) {
format!(
"[con_id={}] mark --add {}; [con_id={}] focus; swap container with con_id {}; [con_id={}] mark --add {}; [con_id={}] focus",
stack.id, stack_mark, main.id, node.id, node.id, main_mark, node.id
"[con_id={}] focus; swap container with con_id {}; [con_id={}] focus",
main.id, node.id, node.id
)
} else {
format!(
"[con_id={}] mark --add {}; [con_id={}] mark --add {}",
stack.id, stack_mark, main.id, main_mark
)
String::from("nop event container not in stack")
}
};

Expand All @@ -97,14 +90,13 @@ impl StackMain {
.find(|n| n.is_window() && n.id != event.container.id)
.expect("main window not found");
let stack = wstree.nodes.first().expect("stack container not found");

let stack_mark = get_stack_mark(ws.id);
let main_mark = get_main_mark(ws.id);
let stack_mark = format!("_stack_{}", stack.id);

let cmd = format!(
"[con_mark={}] unmark {}; [con_mark={}] unmark {}; [con_id={}] mark --add {}; [con_id={}] mark --add {}; [con_id={}] focus; move container to mark {}; [con_id={}] focus; swap container with con_id {}; [con_id={}] focus",
stack_mark, stack_mark, main_mark, main_mark, stack.id, stack_mark, event.container.id, main_mark,
"[con_id={}] mark --add {}; [con_id={}] focus; move container to mark {}; [con_mark={}] unmark {}; [con_id={}] focus; swap container with con_id {}; [con_id={}] focus",
stack.id, stack_mark,
event.container.id, stack_mark,
stack_mark, stack_mark,
main.id, event.container.id, event.container.id
);

Expand All @@ -124,9 +116,8 @@ impl StackMain {
return Ok(());
}
let wstree = tree.find_as_ref(|n| n.id == ws.id).unwrap();
let main_mark = get_main_mark(ws.id);

if event.container.marks.contains(&main_mark) {
if wstree.nodes.len() == 1 {
if let Some(stack) = wstree
.nodes
.iter()
Expand All @@ -142,16 +133,19 @@ impl StackMain {
});

let cmd = if wstree.iter().filter(|n| n.is_window()).count() == 1 {
log::debug!("count is 1..., stack_current: {:?}", stack_current.id);
log::debug!("on_close_window, count 1, stack_id: {}", stack_current.id);
format!(
"[con_id={}] focus; layout splith; move up; [con_id={}] mark --add {}",
stack_current.id, stack_current.id, main_mark
"[con_id={}] focus; layout splith; move up",
stack_current.id
)
} else {
log::debug!("count is more than 1...");
log::debug!(
"on_close_window, count more than 1, stack_id: {}",
stack_current.id
);
format!(
"[con_id={}] focus; move right; resize set width {}; [con_id={}] mark --add {}",
stack_current.id, self.size, stack_current.id, main_mark
"[con_id={}] focus; move right; resize set width {}",
stack_current.id, self.size
)
};
log::debug!("close_window: {}", cmd);
Expand Down
25 changes: 2 additions & 23 deletions src/server/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ use swayipc_async::{Connection, WindowEvent};
use super::command_handlers;
use super::event_handlers;

use crate::{
commands::PerswayCommand,
layout::WorkspaceLayout,
utils::{self, get_main_mark},
};
use crate::{commands::PerswayCommand, layout::WorkspaceLayout, utils};

#[derive(Debug)]
pub struct WorkspaceConfig {
Expand Down Expand Up @@ -101,15 +97,8 @@ impl MessageHandler {
log::debug!("start relayout of ws {}", ws.num);
task::spawn(utils::relayout_workspace(
ws.num,
|mut conn, ws_num, old_ws_id, _output_id, windows| async move {
let main_mark = get_main_mark(old_ws_id);
let main_window = windows.iter().find(|n| n.marks.contains(&main_mark));
|mut conn, ws_num, _old_ws_id, _output_id, windows| async move {
for window in windows.iter().rev() {
if let Some(main_window) = main_window {
if window.id == main_window.id {
continue;
}
}
let cmd = format!(
"[con_id={}] move to workspace number {}; [con_id={}] focus",
window.id, ws_num, window.id
Expand All @@ -118,16 +107,6 @@ impl MessageHandler {
conn.run_command(cmd).await?;
task::sleep(Duration::from_millis(25)).await;
}
if let Some(main_window) = main_window {
let cmd = format!(
"[con_id={}] move to workspace number {}; [con_id={}] focus",
main_window.id, ws_num, main_window.id
);
log::debug!("relayout closure cmd: {}", cmd);
conn.run_command(cmd).await?;
} else {
log::debug!("no main window found via mark: {}", main_mark);
}
Ok(())
},
));
Expand Down
13 changes: 0 additions & 13 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ use std::{future::Future, time::Duration};
use swayipc_async::{Connection, Node, Workspace};

pub const PERSWAY_TMP_WORKSPACE: &str = "◕‿◕";
pub const MAIN_PREFIX: &str = "_main_";
pub const STACK_PREFIX: &str = "_stack_";
//pub const CURRENT_PREFIX: &str = "_stack_current_";
//pub const WS_PREFIX: &str = "_ws_";
//const CON_PREFIX: &str = "_con_";

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

pub fn get_main_mark(ws_id: i64) -> String {
format!("{}{}", MAIN_PREFIX, ws_id)
}

pub fn get_stack_mark(ws_id: i64) -> String {
format!("{}{}", STACK_PREFIX, ws_id)
}

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

0 comments on commit d11e59c

Please sign in to comment.