Compare commits

..

No commits in common. "659f2c8e5e0e907bc984336a2fbedb5dcc34e026" and "91d6c2be290e499935cb6d5a8532b6a0614c68cb" have entirely different histories.

4 changed files with 61 additions and 37 deletions

2
Cargo.lock generated
View File

@ -379,7 +379,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
[[package]] [[package]]
name = "winresz" name = "winresutil"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",

View File

@ -1,5 +1,5 @@
[package] [package]
name = "winresz" name = "winresutil"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View File

@ -14,10 +14,7 @@ fn filter_target_windows(hwnd: &HWND, q: &TargetInformation) -> bool {
return false; return false;
}; };
if q.title_contains if q.title_contains.iter().all(|s| !title.contains(s)) {
.iter()
.all(|s| !title.to_ascii_lowercase().contains(&s.to_ascii_lowercase()))
{
return false; return false;
} }
} }
@ -33,10 +30,7 @@ fn filter_target_windows(hwnd: &HWND, q: &TargetInformation) -> bool {
return false; return false;
}; };
if q.path_endswith if q.path_endswith.iter().all(|s| !path.ends_with(s)) {
.iter()
.all(|s| !path.to_ascii_lowercase().ends_with(&s.to_ascii_lowercase()))
{
return false; return false;
} }
} }
@ -44,33 +38,45 @@ fn filter_target_windows(hwnd: &HWND, q: &TargetInformation) -> bool {
true true
} }
fn window_callback(hwnd: HWND, op: &Cli) { fn window_callback(hwnd: HWND, op: &Commands) {
if !filter_target_windows(&hwnd, &op.target) { let t = match op {
Commands::Get { target } => target,
Commands::Set {
target,
resolution: _,
} => target,
};
if !filter_target_windows(&hwnd, t) {
return; return;
} }
let client_size = Size::from(hwnd.GetClientRect().unwrap()); let client_size = Size::from(hwnd.GetClientRect().unwrap());
let Some(size) = op.size else { match op {
println!("{}\t", client_size - op.target.offset); Commands::Get { target: _ } => {
return; println!("{}", client_size - t.offset);
}; }
Commands::Set {
target: _,
resolution,
} => {
let window_size = Size::from(hwnd.GetWindowRect().unwrap()); let window_size = Size::from(hwnd.GetWindowRect().unwrap());
let border = window_size - client_size; let border = window_size - client_size;
hwnd.SetWindowPos( hwnd.SetWindowPos(
HwndPlace::None, HwndPlace::None,
POINT::new(0, 0), POINT::new(0, 0),
(size + op.target.offset + border).into(), (*resolution + t.offset + border).into(),
SWP::NOMOVE, SWP::NOMOVE,
) )
.unwrap(); .unwrap();
}
}
} }
fn main() { fn main() {
let c = Cli::parse(); let c = Cli::parse().command;
EnumWindows(|hwnd: HWND| -> bool { EnumWindows(|hwnd: HWND| -> bool {
window_callback(hwnd, &c); window_callback(hwnd, &c);
true true

View File

@ -1,4 +1,4 @@
use clap::{Args, Parser}; use clap::{Args, Parser, Subcommand};
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
@ -6,23 +6,35 @@ use winsafe::{RECT, SIZE};
#[derive(Debug, Clone, Args)] #[derive(Debug, Clone, Args)]
pub struct TargetInformation { pub struct TargetInformation {
#[arg(short, long, help = "Filter by binary path")] #[arg(short, long)]
pub path_endswith: Vec<String>, pub path_endswith: Vec<String>,
#[arg(short, long, help = "Filter by title")] #[arg(short, long)]
pub title_contains: Vec<String>, pub title_contains: Vec<String>,
#[arg(short, long, value_parser = parse_size, default_value_t = Size::default(), help = "Additional offset for window")] #[arg(short, long, value_parser = parse_size, default_value_t = Size::default())]
pub offset: Size, pub offset: Size,
} }
#[derive(Debug, Clone, Subcommand)]
pub enum Commands {
Get {
#[command(flatten)]
target: TargetInformation,
},
Set {
#[command(flatten)]
target: TargetInformation,
#[arg(value_parser = parse_size)]
resolution: Size,
},
}
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct Cli { pub struct Cli {
#[command(flatten)] #[command(subcommand)]
pub target: TargetInformation, pub command: Commands,
#[arg(value_parser = parse_size, help = "Set the window size if it's set.")]
pub size: Option<Size>,
} }
#[derive(Debug, Copy, Clone, Parser, Default)] #[derive(Debug, Copy, Clone, Parser)]
pub struct Size { pub struct Size {
pub x: usize, pub x: usize,
pub y: usize, pub y: usize,
@ -66,6 +78,12 @@ impl From<Size> for SIZE {
} }
} }
impl Default for Size {
fn default() -> Self {
Self { x: 0, y: 0 }
}
}
impl Display for Size { impl Display for Size {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}", self.x, self.y) write!(f, "{}x{}", self.x, self.y)