Implement support for Windows
This commit is contained in:
@@ -7,3 +7,6 @@ edition = "2021"
|
|||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
ratatui = "0.27.0"
|
ratatui = "0.27.0"
|
||||||
regex = "1.10.5"
|
regex = "1.10.5"
|
||||||
|
|
||||||
|
[target.'cfg(windows)'.dependencies]
|
||||||
|
winapi = { version = "0.3", features = ["winuser", "processthreadsapi"] }
|
||||||
@@ -9,5 +9,5 @@ window was and calculates a session score from that.
|
|||||||
To use AntiDrift, run `cargo run --release` directly, or `cargo build --release`
|
To use AntiDrift, run `cargo run --release` directly, or `cargo build --release`
|
||||||
and copy the binary into your `PATH`.
|
and copy the binary into your `PATH`.
|
||||||
|
|
||||||
Uses `xdotool` to get window titles and minimize windows. Currently Linux with
|
Under Linux, we use `xdotool` to get window titles and minimize windows. Under
|
||||||
X11 is the only supported platform.
|
Windows, we use the package `winapi` for the same functionality.
|
||||||
|
|||||||
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