use enigo::{Direction, Enigo, Keyboard, Settings};
use std::thread;
use std::time::Duration;
use crate::config::Keybindings;
use crate::keybind::KeyCombo;
use crate::layout::Layout;
const SPLIT_DELAY_MS: u64 = 200;
const NAV_DELAY_MS: u64 = 50;
fn press_key_combo(enigo: &mut Enigo, combo: &KeyCombo) -> Result<(), enigo::InputError> {
for modifier in &combo.modifiers {
enigo.key(*modifier, Direction::Press)?;
}
enigo.key(combo.key, Direction::Click)?;
for modifier in combo.modifiers.iter().rev() {
enigo.key(*modifier, Direction::Release)?;
}
Ok(())
}
pub fn execute_splits(keybindings: &Keybindings, layout: &Layout) -> Result<(), String> {
let mut enigo = Enigo::new(&Settings::default())
.map_err(|e| format!("Failed to initialize enigo: {}", e))?;
let split_delay = Duration::from_millis(SPLIT_DELAY_MS);
let nav_delay = Duration::from_millis(NAV_DELAY_MS);
let num_cols = layout.num_cols();
for _ in 0..(num_cols - 1) {
press_key_combo(&mut enigo, &keybindings.split_right)
.map_err(|e| format!("Failed to send split_right: {}", e))?;
thread::sleep(split_delay);
}
for _ in 0..(num_cols - 1) {
press_key_combo(&mut enigo, &keybindings.goto_previous)
.map_err(|e| format!("Failed to send goto_previous: {}", e))?;
thread::sleep(nav_delay);
}
for col in 0..num_cols {
for _ in 0..(layout.columns[col] - 1) {
press_key_combo(&mut enigo, &keybindings.split_down)
.map_err(|e| format!("Failed to send split_down: {}", e))?;
thread::sleep(split_delay);
}
if col < num_cols - 1 {
press_key_combo(&mut enigo, &keybindings.goto_next)
.map_err(|e| format!("Failed to send goto_next: {}", e))?;
thread::sleep(nav_delay);
}
}
press_key_combo(&mut enigo, &keybindings.equalize)
.map_err(|e| format!("Failed to send equalize: {}", e))?;
thread::sleep(split_delay);
for _ in 0..(layout.total_panes() - 1) {
press_key_combo(&mut enigo, &keybindings.goto_previous)
.map_err(|e| format!("Failed to send goto_previous: {}", e))?;
thread::sleep(nav_delay);
}
Ok(())
}