mirror of
https://gitlab.com/hyask/swaysome.git
synced 2025-12-10 07:44:43 +01:00
Merge branch 'master' into 'master'
Added constant for maximum workspaces per group instead of hardcoded 10 Hello creators of swaysome. I love this program, its quite nice, thank you for sharing it ^.^ However, I can't live with just 10 workspaces per group, I need more. So, instead of having the base 10 hard-coded all around, I created a constant, which I named `MAX_GROUP_WS` due to my super creative naming skills. This way, anyone could change the groups naming and the limit of workspaces with just modifying a simple constant. Examples: ``` MAX Naming 10 → 17, 27 100 → 107, 207 1000 → 1007, 2007 15 → 22, 37 ``` On my local setup I use 100, so its easy to know which group I'm in, and I recommend others to either use 10 or 100. That said, nothing prevents you from using other bases, such as hexadecimal (16), or 69... To keep backwards compatibility, and not break any setup, I kept the default as `10`. If you like my change, feel free to merge it! See merge request hyask/swaysome!12
This commit is contained in:
commit
cdd5a24dc6
1 changed files with 33 additions and 27 deletions
60
src/main.rs
60
src/main.rs
|
|
@ -14,6 +14,13 @@ use std::path::Path;
|
||||||
|
|
||||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||||
|
|
||||||
|
// Maximum workspaces per group. This will determine the naming.
|
||||||
|
// Examples:
|
||||||
|
// 10 → 17, 27
|
||||||
|
// 100 → 107, 207
|
||||||
|
// 15 → 22, 37
|
||||||
|
const MAX_GROUP_WS: usize = 10;
|
||||||
|
|
||||||
const RUN_COMMAND: u32 = 0;
|
const RUN_COMMAND: u32 = 0;
|
||||||
const GET_WORKSPACES: u32 = 1;
|
const GET_WORKSPACES: u32 = 1;
|
||||||
// const SUBSCRIBE: u32 = 2;
|
// const SUBSCRIBE: u32 = 2;
|
||||||
|
|
@ -240,7 +247,7 @@ fn get_current_workspace(stream: &UnixStream) -> Workspace {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_container_to_workspace(stream: &UnixStream, workspace_index: usize) {
|
fn move_container_to_workspace(stream: &UnixStream, workspace_index: usize) {
|
||||||
if workspace_index < 10 {
|
if workspace_index < MAX_GROUP_WS {
|
||||||
move_container_to_workspace_relative(stream, workspace_index);
|
move_container_to_workspace_relative(stream, workspace_index);
|
||||||
} else {
|
} else {
|
||||||
move_container_to_workspace_absolute(stream, workspace_index);
|
move_container_to_workspace_absolute(stream, workspace_index);
|
||||||
|
|
@ -249,18 +256,18 @@ fn move_container_to_workspace(stream: &UnixStream, workspace_index: usize) {
|
||||||
|
|
||||||
fn move_container_to_workspace_group(stream: &UnixStream, target_group: usize) {
|
fn move_container_to_workspace_group(stream: &UnixStream, target_group: usize) {
|
||||||
let current_workspace_index = get_current_workspace(stream).num;
|
let current_workspace_index = get_current_workspace(stream).num;
|
||||||
let current_workspace_index_relative = (current_workspace_index % 10) as usize;
|
let current_workspace_index_relative = (current_workspace_index % MAX_GROUP_WS) as usize;
|
||||||
move_container_to_workspace_absolute(
|
move_container_to_workspace_absolute(
|
||||||
stream,
|
stream,
|
||||||
current_workspace_index_relative + target_group * 10,
|
current_workspace_index_relative + target_group * MAX_GROUP_WS,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_container_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
|
fn move_container_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
|
||||||
let group_index = (workspace_index / 10) as usize;
|
let group_index = (workspace_index / MAX_GROUP_WS) as usize;
|
||||||
let outputs = get_outputs(stream);
|
let outputs = get_outputs(stream);
|
||||||
let workspaces = get_workspaces(stream);
|
let workspaces = get_workspaces(stream);
|
||||||
let full_ws_name = format!("{}{}", group_index, workspace_index % 10);
|
let full_ws_name = format!("{}", group_index * MAX_GROUP_WS + workspace_index % MAX_GROUP_WS);
|
||||||
|
|
||||||
// If the workspace already exists
|
// If the workspace already exists
|
||||||
match workspaces.iter().find(|w| w.num == workspace_index) {
|
match workspaces.iter().find(|w| w.num == workspace_index) {
|
||||||
|
|
@ -270,8 +277,8 @@ fn move_container_to_workspace_absolute(stream: &UnixStream, workspace_index: us
|
||||||
send_command(stream, &focus_cmd);
|
send_command(stream, &focus_cmd);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let target_group = workspace_index / 10;
|
let target_group = workspace_index / MAX_GROUP_WS;
|
||||||
let target_screen_index = match workspaces.iter().find(|w| w.num / 10 == target_group) {
|
let target_screen_index = match workspaces.iter().find(|w| w.num / MAX_GROUP_WS == target_group) {
|
||||||
// If other workspaces on the same group exists
|
// If other workspaces on the same group exists
|
||||||
Some(other_workspace) => Some(
|
Some(other_workspace) => Some(
|
||||||
outputs
|
outputs
|
||||||
|
|
@ -347,16 +354,16 @@ fn move_container_to_workspace_absolute(stream: &UnixStream, workspace_index: us
|
||||||
|
|
||||||
fn move_container_to_workspace_relative(stream: &UnixStream, workspace_index: usize) {
|
fn move_container_to_workspace_relative(stream: &UnixStream, workspace_index: usize) {
|
||||||
let current_workspace_index: usize = get_current_workspace(stream).num;
|
let current_workspace_index: usize = get_current_workspace(stream).num;
|
||||||
let focused_output_index = current_workspace_index / 10;
|
let focused_output_index = current_workspace_index / MAX_GROUP_WS;
|
||||||
|
|
||||||
let mut cmd: String = "move container to workspace number ".to_string();
|
let mut cmd: String = "move container to workspace number ".to_string();
|
||||||
let full_ws_name = format!("{}{}", focused_output_index, workspace_index);
|
let full_ws_name = format!("{}", focused_output_index * MAX_GROUP_WS + workspace_index);
|
||||||
cmd.push_str(&full_ws_name);
|
cmd.push_str(&full_ws_name);
|
||||||
send_command(stream, &cmd);
|
send_command(stream, &cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn focus_to_workspace(stream: &UnixStream, workspace_index: usize) {
|
fn focus_to_workspace(stream: &UnixStream, workspace_index: usize) {
|
||||||
if workspace_index < 10 {
|
if workspace_index < MAX_GROUP_WS {
|
||||||
focus_to_workspace_relative(stream, workspace_index);
|
focus_to_workspace_relative(stream, workspace_index);
|
||||||
} else {
|
} else {
|
||||||
focus_to_workspace_absolute(stream, workspace_index);
|
focus_to_workspace_absolute(stream, workspace_index);
|
||||||
|
|
@ -364,7 +371,7 @@ fn focus_to_workspace(stream: &UnixStream, workspace_index: usize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn focus_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
|
fn focus_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
|
||||||
let output_index = (workspace_index / 10) as usize;
|
let output_index = (workspace_index / MAX_GROUP_WS) as usize;
|
||||||
let outputs = get_outputs(stream);
|
let outputs = get_outputs(stream);
|
||||||
let workspaces = get_workspaces(stream);
|
let workspaces = get_workspaces(stream);
|
||||||
|
|
||||||
|
|
@ -376,8 +383,8 @@ fn focus_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
|
||||||
send_command(stream, &focus_cmd);
|
send_command(stream, &focus_cmd);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let target_group = workspace_index / 10;
|
let target_group = workspace_index / MAX_GROUP_WS;
|
||||||
let target_screen_index = match workspaces.iter().find(|w| w.num / 10 == target_group) {
|
let target_screen_index = match workspaces.iter().find(|w| w.num / MAX_GROUP_WS == target_group) {
|
||||||
// If other workspaces on the same group exists
|
// If other workspaces on the same group exists
|
||||||
Some(other_workspace) => Some(
|
Some(other_workspace) => Some(
|
||||||
outputs
|
outputs
|
||||||
|
|
@ -418,10 +425,10 @@ fn focus_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
|
||||||
|
|
||||||
fn focus_to_workspace_relative(stream: &UnixStream, workspace_index: usize) {
|
fn focus_to_workspace_relative(stream: &UnixStream, workspace_index: usize) {
|
||||||
let current_workspace_index: usize = get_current_workspace(stream).num;
|
let current_workspace_index: usize = get_current_workspace(stream).num;
|
||||||
let focused_output_index = current_workspace_index / 10;
|
let focused_output_index = current_workspace_index / MAX_GROUP_WS;
|
||||||
|
|
||||||
let mut cmd: String = "workspace number ".to_string();
|
let mut cmd: String = "workspace number ".to_string();
|
||||||
let full_ws_name = format!("{}{}", focused_output_index, &workspace_index);
|
let full_ws_name = format!("{}", focused_output_index * MAX_GROUP_WS + workspace_index);
|
||||||
cmd.push_str(&full_ws_name);
|
cmd.push_str(&full_ws_name);
|
||||||
send_command(stream, &cmd);
|
send_command(stream, &cmd);
|
||||||
}
|
}
|
||||||
|
|
@ -431,10 +438,10 @@ fn focus_to_group(stream: &UnixStream, group_index: usize) {
|
||||||
let workspaces = get_workspaces(stream);
|
let workspaces = get_workspaces(stream);
|
||||||
|
|
||||||
let current_workspace_index: usize = get_current_workspace(stream).num;
|
let current_workspace_index: usize = get_current_workspace(stream).num;
|
||||||
let target_workspace_relative_index = current_workspace_index % 10;
|
let target_workspace_relative_index = current_workspace_index % MAX_GROUP_WS;
|
||||||
|
|
||||||
let target_workspace_index = group_index * 10 + target_workspace_relative_index;
|
let target_workspace_index = group_index * MAX_GROUP_WS + target_workspace_relative_index;
|
||||||
let full_ws_name = format!("{}{}", group_index, target_workspace_relative_index);
|
let full_ws_name = format!("{}", group_index * MAX_GROUP_WS + target_workspace_relative_index);
|
||||||
|
|
||||||
// If the workspace already exists
|
// If the workspace already exists
|
||||||
match workspaces.iter().find(|w| w.num == target_workspace_index) {
|
match workspaces.iter().find(|w| w.num == target_workspace_index) {
|
||||||
|
|
@ -444,7 +451,7 @@ fn focus_to_group(stream: &UnixStream, group_index: usize) {
|
||||||
send_command(stream, &focus_cmd);
|
send_command(stream, &focus_cmd);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let target_screen_index = match workspaces.iter().find(|w| w.num / 10 == group_index) {
|
let target_screen_index = match workspaces.iter().find(|w| w.num / MAX_GROUP_WS == group_index) {
|
||||||
// If other workspaces on the same group exists
|
// If other workspaces on the same group exists
|
||||||
Some(other_workspace) => Some(
|
Some(other_workspace) => Some(
|
||||||
outputs
|
outputs
|
||||||
|
|
@ -457,7 +464,7 @@ fn focus_to_group(stream: &UnixStream, group_index: usize) {
|
||||||
),
|
),
|
||||||
None => {
|
None => {
|
||||||
// Or if the targeted output is currently connected
|
// Or if the targeted output is currently connected
|
||||||
if group_index != 0 && group_index <= outputs.len() {
|
if group_index > 0 && group_index <= outputs.len() {
|
||||||
Some(group_index)
|
Some(group_index)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
@ -526,8 +533,8 @@ fn move_container_to_next_or_prev_output(stream: &UnixStream, go_to_prev: bool)
|
||||||
.iter()
|
.iter()
|
||||||
.find(|x| x.output == target_output.name && x.visible)
|
.find(|x| x.output == target_output.name && x.visible)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let group_index = (target_workspace.num / 10) as usize;
|
let group_index = (target_workspace.num / MAX_GROUP_WS) as usize;
|
||||||
let full_ws_name = format!("{}{}", group_index, target_workspace.num % 10);
|
let full_ws_name = format!("{}", group_index * MAX_GROUP_WS + target_workspace.num % MAX_GROUP_WS);
|
||||||
|
|
||||||
// Move container to target workspace
|
// Move container to target workspace
|
||||||
let mut cmd: String = "move container to workspace number ".to_string();
|
let mut cmd: String = "move container to workspace number ".to_string();
|
||||||
|
|
@ -550,7 +557,7 @@ fn focus_container_to_prev_group(stream: &UnixStream) {
|
||||||
|
|
||||||
fn focus_container_to_next_or_prev_group(stream: &UnixStream, go_to_prev: bool) {
|
fn focus_container_to_next_or_prev_group(stream: &UnixStream, go_to_prev: bool) {
|
||||||
let current_workspace_index: usize = get_current_workspace(stream).num;
|
let current_workspace_index: usize = get_current_workspace(stream).num;
|
||||||
let focused_group_index = current_workspace_index / 10;
|
let focused_group_index = current_workspace_index / MAX_GROUP_WS;
|
||||||
|
|
||||||
if go_to_prev {
|
if go_to_prev {
|
||||||
focus_to_group(stream, focused_group_index - 1);
|
focus_to_group(stream, focused_group_index - 1);
|
||||||
|
|
@ -570,9 +577,8 @@ fn init_workspaces(stream: &UnixStream, workspace_index: usize) {
|
||||||
|
|
||||||
let mut cmd: String = "workspace number ".to_string();
|
let mut cmd: String = "workspace number ".to_string();
|
||||||
let full_ws_name = format!(
|
let full_ws_name = format!(
|
||||||
"{}{}",
|
"{}",
|
||||||
get_current_output_index(stream) + 1,
|
(get_current_output_index(stream) + 1) * MAX_GROUP_WS + workspace_index
|
||||||
&workspace_index
|
|
||||||
);
|
);
|
||||||
cmd.push_str(&full_ws_name);
|
cmd.push_str(&full_ws_name);
|
||||||
send_command(stream, &cmd);
|
send_command(stream, &cmd);
|
||||||
|
|
@ -590,7 +596,7 @@ fn rearrange_workspaces(stream: &UnixStream) {
|
||||||
focus_cmd.push_str(&workspace.num.to_string());
|
focus_cmd.push_str(&workspace.num.to_string());
|
||||||
send_command(stream, &focus_cmd);
|
send_command(stream, &focus_cmd);
|
||||||
|
|
||||||
let group_index = workspace.num / 10;
|
let group_index = workspace.num / MAX_GROUP_WS;
|
||||||
if group_index <= outputs.len() - 1 {
|
if group_index <= outputs.len() - 1 {
|
||||||
let mut move_cmd = move_cmd_prefix.clone();
|
let mut move_cmd = move_cmd_prefix.clone();
|
||||||
move_cmd.push_str(&outputs[group_index.max(1) - 1].name);
|
move_cmd.push_str(&outputs[group_index.max(1) - 1].name);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue