Get active window on Hyprland using subprocess instead of relying on external library

This commit is contained in:
cyber-sushi 2024-05-09 18:36:08 +02:00
parent 9dba3c564f
commit 75b5699785

View file

@ -1,22 +1,25 @@
use crate::Config; use crate::Config;
use hyprland::{data::Client, prelude::*}; use serde_json;
use swayipc_async::Connection; use swayipc_async::Connection;
use std::collections::HashMap; use std::{collections::HashMap, process::Command};
use x11rb::protocol::xproto::{get_property, get_input_focus, Atom, AtomEnum}; use x11rb::protocol::xproto::{get_property, get_input_focus, Atom, AtomEnum};
pub async fn get_active_window(current_desktop: &Option<String>, config: &HashMap<String, Config>) -> String { pub async fn get_active_window(current_desktop: &Option<String>, config: &HashMap<String, Config>) -> String {
let active_client = current_desktop.clone().unwrap_or(String::from("default")); let active_client = current_desktop.clone().unwrap_or(String::from("default"));
match active_client.as_str() { match active_client.as_str() {
"Hyprland" => { "Hyprland" => {
let active_window: String = match Client::get_active_async().await.unwrap() { let query = Command::new("hyprctl").args(["activewindow", "-j"]).output().unwrap();
Some(window) => window.class, if let Ok(reply) = serde_json::from_str::<serde_json::Value>(std::str::from_utf8(query.stdout.as_slice()).unwrap()) {
None => String::from("default") let active_window = reply["class"].to_string().replace("\"", "");
};
if config.contains_key(&active_window) { if config.contains_key(&active_window) {
active_window active_window
} else { } else {
String::from("default") String::from("default")
} }
} else {
println!("Unable to connect to the compositor. Running without user environment?");
String::from("default")
}
}, },
"sway" => { "sway" => {
let mut connection = Connection::new().await.unwrap(); let mut connection = Connection::new().await.unwrap();
@ -61,3 +64,4 @@ pub async fn get_active_window(current_desktop: &Option<String>, config: &HashMa
_ => String::from("default") _ => String::from("default")
} }
} }