|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use anyhow::{Context, Result, bail}; |
| 4 | +use std::process::Command; |
| 5 | +use which::which; |
| 6 | + |
| 7 | +use strum::IntoEnumIterator; |
| 8 | + |
| 9 | +use crate::{game_project::GameProject, utils::classname}; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +enum SwitcherType { |
| 13 | + Class(String), |
| 14 | + Title(String), |
| 15 | + AppName(String), |
| 16 | +} |
| 17 | + |
| 18 | +impl SwitcherType { |
| 19 | + fn value(&self) -> String { |
| 20 | + match self { |
| 21 | + SwitcherType::Class(c) => c.clone(), |
| 22 | + SwitcherType::Title(t) => t.clone(), |
| 23 | + SwitcherType::AppName(a) => a.clone(), |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, strum::EnumIter)] |
| 29 | +enum Switcher { |
| 30 | + #[cfg(target_os = "linux")] |
| 31 | + HyprCtl, |
| 32 | + |
| 33 | + #[cfg(target_os = "linux")] |
| 34 | + SwayMsg, |
| 35 | + |
| 36 | + #[cfg(target_os = "linux")] |
| 37 | + WmCtrl, |
| 38 | + |
| 39 | + #[cfg(target_os = "linux")] |
| 40 | + XDoTool, |
| 41 | + |
| 42 | + #[cfg(target_os = "macos")] |
| 43 | + OsaScript, |
| 44 | +} |
| 45 | + |
| 46 | +impl Switcher { |
| 47 | + fn path(&self) -> Option<PathBuf> { |
| 48 | + #[cfg(target_os = "linux")] |
| 49 | + match self { |
| 50 | + Switcher::HyprCtl => which("hyprctl").ok(), |
| 51 | + Switcher::SwayMsg => which("swaymsg").ok(), |
| 52 | + Switcher::WmCtrl => which("wmctrl").ok(), |
| 53 | + Switcher::XDoTool => which("xdotool").ok(), |
| 54 | + } |
| 55 | + #[cfg(target_os = "macos")] |
| 56 | + match self { |
| 57 | + Switcher::OsaScript => which("osascript").ok(), |
| 58 | + } |
| 59 | + #[cfg(target_os = "windows")] |
| 60 | + None |
| 61 | + } |
| 62 | + |
| 63 | + fn from_env() -> Option<Self> { |
| 64 | + Self::iter().find(|sw| sw.path().is_some()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +fn switch(switcher_type: SwitcherType) -> Result<()> { |
| 69 | + tracing::info!("Switching to {switcher_type:?}"); |
| 70 | + |
| 71 | + let Some(switcher) = Switcher::from_env() else { |
| 72 | + tracing::error!("No supported focus switcher found, do nothing..."); |
| 73 | + return Ok(()); |
| 74 | + }; |
| 75 | + |
| 76 | + #[cfg(target_os = "linux")] |
| 77 | + return match switcher { |
| 78 | + Switcher::HyprCtl => { |
| 79 | + Command::new(switcher.path().unwrap()) |
| 80 | + .arg("dispatch") |
| 81 | + .arg("focuswindow") |
| 82 | + .arg(match switcher_type { |
| 83 | + SwitcherType::Class(class) => format!("class:{class}"), |
| 84 | + SwitcherType::Title(title) => format!("title:{title}"), |
| 85 | + _ => bail!("Unsupported switcher type {switcher_type:?} for {switcher:?}"), |
| 86 | + }) |
| 87 | + .spawn()? |
| 88 | + .wait()?; |
| 89 | + |
| 90 | + Ok(()) |
| 91 | + } |
| 92 | + Switcher::SwayMsg => { |
| 93 | + Command::new(switcher.path().unwrap()) |
| 94 | + .arg(format!( |
| 95 | + "[{}={}] focus", |
| 96 | + match switcher_type { |
| 97 | + SwitcherType::Class(_) => format!("class"), |
| 98 | + SwitcherType::Title(_) => format!("title"), |
| 99 | + _ => bail!("Unsupported switcher type {switcher_type:?} for {switcher:?}"), |
| 100 | + }, |
| 101 | + switcher_type.value(), |
| 102 | + )) |
| 103 | + .spawn()? |
| 104 | + .wait()?; |
| 105 | + |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | + Switcher::WmCtrl => { |
| 109 | + let mut cmd = Command::new(switcher.path().unwrap()); |
| 110 | + |
| 111 | + if matches!(switcher_type, SwitcherType::Class(_)) { |
| 112 | + cmd.arg("-x"); |
| 113 | + } |
| 114 | + |
| 115 | + cmd.arg("-a").arg(switcher_type.value()).spawn()?.wait()?; |
| 116 | + |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | + Switcher::XDoTool => { |
| 120 | + Command::new(switcher.path().unwrap()) |
| 121 | + .arg("search") |
| 122 | + .arg(match switcher_type { |
| 123 | + SwitcherType::Class(_) => format!("--class"), |
| 124 | + SwitcherType::Title(_) => format!("--title"), |
| 125 | + _ => bail!("Unsupported switcher type {switcher_type:?} for {switcher:?}"), |
| 126 | + }) |
| 127 | + .arg(switcher_type.value()) |
| 128 | + .arg("windowactivate") |
| 129 | + .spawn()? |
| 130 | + .wait()?; |
| 131 | + |
| 132 | + Ok(()) |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | + #[cfg(target_os = "macos")] |
| 137 | + return match switcher { |
| 138 | + Switcher::OsaScript => { |
| 139 | + Command::new(switcher.path().unwrap()) |
| 140 | + .arg("-e") |
| 141 | + .arg(match switcher_type { |
| 142 | + SwitcherType::AppName(app_name) => format!("'tell application \"System Events\" to tell process \"{app_name}\" to set frontmost to true'"), |
| 143 | + _ => bail!("Unsupported switcher type {switcher_type:?} for {switcher:?}"), |
| 144 | + }) |
| 145 | + .spawn()? |
| 146 | + .wait()?; |
| 147 | + |
| 148 | + Ok(()) |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + #[cfg(target_os = "windows")] |
| 153 | + return Ok(()); |
| 154 | +} |
| 155 | + |
| 156 | +pub fn focus_neovim(root_dir: PathBuf) -> Result<()> { |
| 157 | + if !root_dir.join("game.project").exists() { |
| 158 | + bail!("Could not find game.project file in {root_dir:?}: Not a valid Defold directory"); |
| 159 | + } |
| 160 | + |
| 161 | + if cfg!(target_os = "linux") { |
| 162 | + let class = classname( |
| 163 | + root_dir |
| 164 | + .to_str() |
| 165 | + .context("could not convert path to string")?, |
| 166 | + )?; |
| 167 | + |
| 168 | + return switch(SwitcherType::Class(class)); |
| 169 | + } |
| 170 | + |
| 171 | + tracing::error!("Focus switching to Neovim is not support on current platform"); |
| 172 | + |
| 173 | + Ok(()) |
| 174 | +} |
| 175 | + |
| 176 | +pub fn focus_game(root_dir: PathBuf) -> Result<()> { |
| 177 | + if !root_dir.join("game.project").exists() { |
| 178 | + bail!("Could not find game.project file in {root_dir:?}: Not a valid Defold directory"); |
| 179 | + } |
| 180 | + |
| 181 | + let game_project = GameProject::load_from_path(root_dir.join("game.project"))?; |
| 182 | + |
| 183 | + if cfg!(target_os = "linux") { |
| 184 | + return switch(SwitcherType::Title(game_project.title)); |
| 185 | + } else if cfg!(target_os = "macos") { |
| 186 | + return switch(SwitcherType::AppName(game_project.title)); |
| 187 | + } |
| 188 | + |
| 189 | + tracing::error!("Focus switching to the Game is not support on current platform"); |
| 190 | + |
| 191 | + Ok(()) |
| 192 | +} |
0 commit comments