Skip to content

Commit

Permalink
feat(workspace-rule): add ability to only apply workspace rule on first
Browse files Browse the repository at this point in the history
show of app
  • Loading branch information
alvint91 authored and LGUG2Z committed Apr 30, 2023
1 parent b647fdf commit 4306a7b
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 42 deletions.
4 changes: 2 additions & 2 deletions komorebi-core/src/lib.rs
Expand Up @@ -127,8 +127,8 @@ pub enum SocketMessage {
WorkAreaOffset(Rect),
MonitorWorkAreaOffset(usize, Rect),
ResizeDelta(i32),
WorkspaceRule(ApplicationIdentifier, String, usize, usize),
NamedWorkspaceRule(ApplicationIdentifier, String, String),
WorkspaceRule(ApplicationIdentifier, String, usize, usize, bool),
NamedWorkspaceRule(ApplicationIdentifier, String, String, bool),
FloatRule(ApplicationIdentifier, String),
ManageRule(ApplicationIdentifier, String),
IdentifyObjectNameChangeApplication(ApplicationIdentifier, String),
Expand Down
2 changes: 1 addition & 1 deletion komorebi/src/main.rs
Expand Up @@ -94,7 +94,7 @@ lazy_static! {
]));
static ref MONITOR_INDEX_PREFERENCES: Arc<Mutex<HashMap<usize, Rect>>> =
Arc::new(Mutex::new(HashMap::new()));
static ref WORKSPACE_RULES: Arc<Mutex<HashMap<String, (usize, usize)>>> =
static ref WORKSPACE_RULES: Arc<Mutex<HashMap<String, (usize, usize, bool)>>> =
Arc::new(Mutex::new(HashMap::new()));
static ref MANAGE_IDENTIFIERS: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
static ref FLOAT_IDENTIFIERS: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![
Expand Down
25 changes: 21 additions & 4 deletions komorebi/src/process_command.rs
Expand Up @@ -215,21 +215,38 @@ impl WindowManager {
self.set_workspace_padding(monitor_idx, workspace_idx, size)?;
}
}
SocketMessage::WorkspaceRule(_, ref id, monitor_idx, workspace_idx) => {
SocketMessage::WorkspaceRule(
_,
ref id,
monitor_idx,
workspace_idx,
apply_on_first_show_only,
) => {
{
let mut workspace_rules = WORKSPACE_RULES.lock();
workspace_rules.insert(id.to_string(), (monitor_idx, workspace_idx));
workspace_rules.insert(
id.to_string(),
(monitor_idx, workspace_idx, apply_on_first_show_only),
);
}

self.enforce_workspace_rules()?;
}
SocketMessage::NamedWorkspaceRule(_, ref id, ref workspace) => {
SocketMessage::NamedWorkspaceRule(
_,
ref id,
ref workspace,
apply_on_first_show_only,
) => {
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));
workspace_rules.insert(
id.to_string(),
(monitor_idx, workspace_idx, apply_on_first_show_only),
);
}

self.enforce_workspace_rules()?;
Expand Down
9 changes: 9 additions & 0 deletions komorebi/src/process_event.rs
Expand Up @@ -150,6 +150,10 @@ impl WindowManager {
WindowManagerEvent::Destroy(_, window) | WindowManagerEvent::Unmanage(window) => {
self.focused_workspace_mut()?.remove_window(window.hwnd)?;
self.update_focused_workspace(false)?;

let mut already_moved_window_handles = self.already_moved_window_handles.lock();

already_moved_window_handles.remove(&window.hwnd);
}
WindowManagerEvent::Minimize(_, window) => {
let mut hide = false;
Expand Down Expand Up @@ -193,6 +197,11 @@ impl WindowManager {
self.focused_workspace_mut()?.remove_window(window.hwnd)?;
self.update_focused_workspace(false)?;
}

let mut already_moved_window_handles = self.already_moved_window_handles.lock();

already_moved_window_handles.remove(&window.hwnd);

}
WindowManagerEvent::FocusChange(_, window) => {
let workspace = self.focused_workspace_mut()?;
Expand Down
113 changes: 82 additions & 31 deletions komorebi/src/window_manager.rs
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::io::ErrorKind;
use std::num::NonZeroUsize;
Expand Down Expand Up @@ -71,6 +72,7 @@ pub struct WindowManager {
pub virtual_desktop_id: Option<Vec<u8>>,
pub has_pending_raise_op: bool,
pub pending_move_op: Option<(usize, usize, usize)>,
pub already_moved_window_handles: Arc<Mutex<HashSet<isize>>>,
}

#[derive(Debug, Serialize, JsonSchema)]
Expand Down Expand Up @@ -189,6 +191,7 @@ impl WindowManager {
hotwatch: Hotwatch::new()?,
has_pending_raise_op: false,
pending_move_op: None,
already_moved_window_handles: Arc::new(Mutex::new(HashSet::new())),
})
}

Expand Down Expand Up @@ -494,41 +497,89 @@ impl WindowManager {
for (j, workspace) in monitor.workspaces().iter().enumerate() {
// And all the visible windows (at the top of a container)
for window in workspace.visible_windows().into_iter().flatten() {
// TODO this function needs some TLC
let mut already_moved_window_handles =
self.already_moved_window_handles.lock();
// If the executable names or titles of any of those windows are in our rules map
if let Some((monitor_idx, workspace_idx)) = workspace_rules.get(&window.exe()?)
if let Some((monitor_idx, workspace_idx, apply_on_first_show_only)) =
workspace_rules.get(&window.exe()?)
{
tracing::info!(
"{} should be on monitor {}, workspace {}",
window.title()?,
*monitor_idx,
*workspace_idx
);

// Create an operation outline and save it for later in the fn
to_move.push(EnforceWorkspaceRuleOp {
hwnd: window.hwnd,
origin_monitor_idx: i,
origin_workspace_idx: j,
target_monitor_idx: *monitor_idx,
target_workspace_idx: *workspace_idx,
});
} else if let Some((monitor_idx, workspace_idx)) =
if *apply_on_first_show_only {
if !already_moved_window_handles.contains(&window.hwnd) {
tracing::info!(
"{} should be on monitor {}, workspace {}",
window.title()?,
*monitor_idx,
*workspace_idx
);

already_moved_window_handles
.insert(window.hwnd);

// Create an operation outline and save it for later in the fn
to_move.push(EnforceWorkspaceRuleOp {
hwnd: window.hwnd,
origin_monitor_idx: i,
origin_workspace_idx: j,
target_monitor_idx: *monitor_idx,
target_workspace_idx: *workspace_idx,
});
}
} else {
tracing::info!(
"{} should be on monitor {}, workspace {}",
window.title()?,
*monitor_idx,
*workspace_idx
);

// Create an operation outline and save it for later in the fn
to_move.push(EnforceWorkspaceRuleOp {
hwnd: window.hwnd,
origin_monitor_idx: i,
origin_workspace_idx: j,
target_monitor_idx: *monitor_idx,
target_workspace_idx: *workspace_idx,
});
}
} else if let Some((monitor_idx, workspace_idx, apply_on_first_show_only)) =
workspace_rules.get(&window.title()?)
{
tracing::info!(
"{} should be on monitor {}, workspace {}",
window.title()?,
*monitor_idx,
*workspace_idx
);

to_move.push(EnforceWorkspaceRuleOp {
hwnd: window.hwnd,
origin_monitor_idx: i,
origin_workspace_idx: j,
target_monitor_idx: *monitor_idx,
target_workspace_idx: *workspace_idx,
});
if *apply_on_first_show_only {
if !already_moved_window_handles.contains(&window.hwnd) {
tracing::info!(
"{} should be on monitor {}, workspace {}",
window.title()?,
*monitor_idx,
*workspace_idx
);

already_moved_window_handles
.insert(window.hwnd);
to_move.push(EnforceWorkspaceRuleOp {
hwnd: window.hwnd,
origin_monitor_idx: i,
origin_workspace_idx: j,
target_monitor_idx: *monitor_idx,
target_workspace_idx: *workspace_idx,
});
}
} else {
tracing::info!(
"{} should be on monitor {}, workspace {}",
window.title()?,
*monitor_idx,
*workspace_idx
);

to_move.push(EnforceWorkspaceRuleOp {
hwnd: window.hwnd,
origin_monitor_idx: i,
origin_workspace_idx: j,
target_monitor_idx: *monitor_idx,
target_workspace_idx: *workspace_idx,
});
}
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions komorebic/src/main.rs
Expand Up @@ -525,6 +525,9 @@ struct WorkspaceRule {
monitor: usize,
/// Workspace index on the specified monitor (zero-indexed)
workspace: usize,
#[clap(short,long)]
/// Only apply once on first app load
apply_on_first_show_only: bool,
}

#[derive(Parser, AhkFunction)]
Expand All @@ -535,6 +538,9 @@ struct NamedWorkspaceRule {
id: String,
/// Name of a workspace
workspace: String,
#[clap(short,long)]
/// Only apply once on first app load
apply_on_first_show_only: bool,
}

#[derive(Parser, AhkFunction)]
Expand Down Expand Up @@ -1449,14 +1455,25 @@ fn main() -> Result<()> {
}
SubCommand::WorkspaceRule(arg) => {
send_message(
&SocketMessage::WorkspaceRule(arg.identifier, arg.id, arg.monitor, arg.workspace)
.as_bytes()?,
&SocketMessage::WorkspaceRule(
arg.identifier,
arg.id,
arg.monitor,
arg.workspace,
arg.apply_on_first_show_only,
)
.as_bytes()?,
)?;
}
SubCommand::NamedWorkspaceRule(arg) => {
send_message(
&SocketMessage::NamedWorkspaceRule(arg.identifier, arg.id, arg.workspace)
.as_bytes()?,
&SocketMessage::NamedWorkspaceRule(
arg.identifier,
arg.id,
arg.workspace,
arg.apply_on_first_show_only,
)
.as_bytes()?,
)?;
}
SubCommand::Stack(arg) => {
Expand Down

0 comments on commit 4306a7b

Please sign in to comment.