Implement support for Windows
This commit is contained in:
@@ -1,70 +1,70 @@
|
||||
use regex::Regex;
|
||||
use std::{process::Command, process::Output, str};
|
||||
|
||||
pub fn get_title_clean() -> String {
|
||||
let title = get_window_info().title;
|
||||
let re = Regex::new(r"-?\d+([:.]\d+)+%?").unwrap();
|
||||
re.replace_all(&title, "").to_string()
|
||||
}
|
||||
|
||||
pub fn minimize_other(title: &str) {
|
||||
let window_info = get_window_info();
|
||||
if &window_info.title != title {
|
||||
run(&format!("xdotool windowminimize {}", window_info.wid));
|
||||
}
|
||||
}
|
||||
|
||||
struct WindowInfo {
|
||||
title: String,
|
||||
_class: String,
|
||||
wid: String,
|
||||
}
|
||||
|
||||
fn run(cmd: &str) -> Option<String> {
|
||||
let output = Command::new("sh").arg("-c").arg(cmd).output();
|
||||
|
||||
let Ok(Output {
|
||||
status,
|
||||
stdout,
|
||||
stderr: _,
|
||||
}) = output
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if status.code() != Some(0) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let Ok(output_str) = str::from_utf8(&stdout) else {
|
||||
return None;
|
||||
};
|
||||
|
||||
Some(output_str.trim().to_string())
|
||||
}
|
||||
|
||||
fn get_window_info() -> WindowInfo {
|
||||
let none = WindowInfo {
|
||||
title: "none".to_string(),
|
||||
_class: "none".to_string(),
|
||||
wid: "".to_string(),
|
||||
};
|
||||
|
||||
let Some(wid) = run("xdotool getactivewindow") else {
|
||||
return none;
|
||||
};
|
||||
|
||||
let Some(class) = run(&format!("xdotool getwindowclassname {wid}")) else {
|
||||
return none;
|
||||
};
|
||||
|
||||
let Some(title) = run(&format!("xdotool getwindowname {wid}")) else {
|
||||
return none;
|
||||
};
|
||||
|
||||
WindowInfo {
|
||||
title,
|
||||
_class: class,
|
||||
wid,
|
||||
}
|
||||
}
|
||||
use regex::Regex;
|
||||
use std::{process::Command, process::Output, str};
|
||||
|
||||
pub fn get_title_clean() -> String {
|
||||
let title = get_window_info().title;
|
||||
let re = Regex::new(r"-?\d+([:.]\d+)+%?").unwrap();
|
||||
re.replace_all(&title, "").to_string()
|
||||
}
|
||||
|
||||
pub fn minimize_other(title: &str) {
|
||||
let window_info = get_window_info();
|
||||
if &window_info.title != title {
|
||||
run(&format!("xdotool windowminimize {}", window_info.wid));
|
||||
}
|
||||
}
|
||||
|
||||
struct WindowInfo {
|
||||
title: String,
|
||||
_class: String,
|
||||
wid: String,
|
||||
}
|
||||
|
||||
fn run(cmd: &str) -> Option<String> {
|
||||
let output = Command::new("sh").arg("-c").arg(cmd).output();
|
||||
|
||||
let Ok(Output {
|
||||
status,
|
||||
stdout,
|
||||
stderr: _,
|
||||
}) = output
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if status.code() != Some(0) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let Ok(output_str) = str::from_utf8(&stdout) else {
|
||||
return None;
|
||||
};
|
||||
|
||||
Some(output_str.trim().to_string())
|
||||
}
|
||||
|
||||
fn get_window_info() -> WindowInfo {
|
||||
let none = WindowInfo {
|
||||
title: "none".to_string(),
|
||||
_class: "none".to_string(),
|
||||
wid: "".to_string(),
|
||||
};
|
||||
|
||||
let Some(wid) = run("xdotool getactivewindow") else {
|
||||
return none;
|
||||
};
|
||||
|
||||
let Some(class) = run(&format!("xdotool getwindowclassname {wid}")) else {
|
||||
return none;
|
||||
};
|
||||
|
||||
let Some(title) = run(&format!("xdotool getwindowname {wid}")) else {
|
||||
return none;
|
||||
};
|
||||
|
||||
WindowInfo {
|
||||
title,
|
||||
_class: class,
|
||||
wid,
|
||||
}
|
||||
}
|
||||
9
src/window/mod.rs
Normal file
9
src/window/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use windows::*;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod linux;
|
||||
#[cfg(target_os = "linux")]
|
||||
pub use linux::*;
|
||||
38
src/window/windows.rs
Normal file
38
src/window/windows.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use regex::Regex;
|
||||
use std::{ffi::OsString, os::windows::ffi::OsStringExt};
|
||||
use winapi::shared::windef::HWND;
|
||||
use winapi::um::winuser::{ShowWindow, SW_MINIMIZE, GetForegroundWindow, GetWindowTextW};
|
||||
|
||||
|
||||
pub fn get_title_clean() -> String {
|
||||
let title = get_window_info().title;
|
||||
let re = Regex::new(r"-?\d+([:.]\d+)+%?").unwrap();
|
||||
re.replace_all(&title, "").to_string()
|
||||
}
|
||||
|
||||
pub fn minimize_other(title: &str) {
|
||||
let window_info = get_window_info();
|
||||
if window_info.title != title {
|
||||
unsafe {
|
||||
ShowWindow(window_info.hwnd, SW_MINIMIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct WindowInfo {
|
||||
title: String,
|
||||
hwnd: HWND,
|
||||
}
|
||||
|
||||
fn get_window_info() -> WindowInfo {
|
||||
unsafe {
|
||||
let hwnd = GetForegroundWindow();
|
||||
let mut text: [u16; 512] = [0; 512];
|
||||
let len = GetWindowTextW(hwnd, text.as_mut_ptr(), text.len() as i32) as usize;
|
||||
let title = OsString::from_wide(&text[..len]).to_string_lossy().into_owned();
|
||||
WindowInfo {
|
||||
title,
|
||||
hwnd,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user