Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi monitor fixes #15

Open
wants to merge 2 commits into
base: multi_monitor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use tokio::{

use crate::{Message, State};

use regex::Regex;

#[derive(Subcommand, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum InfoCommand {
WaybarActivityStatus,
Expand Down Expand Up @@ -450,6 +452,11 @@ impl InfoCommand {
focused: false,
named_focus: focii.get(w).cloned().unwrap_or_default(),
};
// w comes with a $ in the monitor position
// name comes with a number there
// so to compare them, we have to... do something about that
let re = Regex::new(r"\d+\)").unwrap();
let name = re.replace(&name,"$)");
if w == &name {
ws.focused = true;
}
Expand Down
75 changes: 43 additions & 32 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::{
Message,
};

use regex::Regex;

#[derive(Debug)]
pub struct State {
pub focused: HashMap<String, String>,
Expand Down Expand Up @@ -51,7 +53,7 @@ impl State {
.map(|name| {
raw_workspaces
.clone()
.map(|(x, y)| format!("{name}:({x} {y}$)"))
.map(|(x, y)| format!("{name}:({x} {y} $)"))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
Expand All @@ -75,6 +77,12 @@ impl State {
pub fn get_indices(&self, name: impl AsRef<str>) -> Option<(usize, Option<usize>)> {
let name = name.as_ref();
let activity_index = self.get_activity_index(name)?;
// w comes with a $ in the monitor position
// name comes with a number there
// so to compare them, we have to... do something about that
let re = Regex::new(r"\d+\)").unwrap();
let binding = re.replace(&name,"$)");
let name = binding.as_ref();
let workspace_index = self.workspaces[activity_index]
.iter()
.position(|w| w == name);
Expand Down Expand Up @@ -113,39 +121,50 @@ impl State {
let res = set_workspace_anim(anim).await;
let name = name.as_ref();

let mut window = None;
let monitors = Monitors::get_async().await?;
let mut monitors = monitors.into_iter().collect::<Vec<_>>();
monitors.sort_by(|a, b| {
if a.focused {
Ordering::Greater
} else if b.focused {
Ordering::Less
} else {
Ordering::Equal
}
});

if move_window {
window = Client::get_active_async().await?;
}
if window.is_some() {
Dispatch::call_async(DispatchType::MoveToWorkspaceSilent(
WorkspaceIdentifierWithSpecial::Name(name),
None,
))
.await?;
for m in monitors.iter() {
if m.focused {
let wsname = name.replace("$", &m.id.to_string());
Dispatch::call_async(DispatchType::MoveToWorkspaceSilent(
WorkspaceIdentifierWithSpecial::Name(&wsname),
None,
))
.await?;
}
}
}
let re = Regex::new(r"\d+\)").unwrap();
let name = re.replace(name,"$)");
match self.config.multi_monitor_strategy {
MultiMonitorStrategy::SeparateWorkspaces => {
// get x y for current monitor and switch all monitors to x y w
let monitors = Monitors::get_async().await?;
let mut monitors = monitors.into_iter().collect::<Vec<_>>();
monitors.sort_by(|a, b| {
if a.focused {
Ordering::Greater
} else if b.focused {
Ordering::Less
} else {
Ordering::Equal
}
});
let cursor = CursorPosition::get_async().await?;
for m in monitors.iter() {
let name = name.replace("$", &m.id.to_string());
let wsname = name.replace("$", &m.id.to_string());
Dispatch::call_async(DispatchType::Custom(
"moveworkspacetomonitor",
&format!("special:{} {}", name, m.id),
"focusmonitor",
&m.id.to_string()
))
.await?;
Dispatch::call_async(DispatchType::Custom(
"workspace",
&format!("name:{}", wsname)
))
.await?;
}
Dispatch::call_async(DispatchType::MoveCursor(cursor.x, cursor.y)).await?;
}
MultiMonitorStrategy::SharedWorkspacesSyncActivities => {
// switch all monitors to their corresponding ws in activity mentioned in their current ws
Expand Down Expand Up @@ -201,14 +220,6 @@ impl State {
.await?;
}
}
if let Some(w) = window {
let cursor = CursorPosition::get_async().await?;
Dispatch::call_async(DispatchType::FocusWindow(WindowIdentifier::Address(
w.address,
)))
.await?;
Dispatch::call_async(DispatchType::MoveCursor(cursor.x, cursor.y)).await?;
}
res
}

Expand Down