feat(move-floating-window): percentage change (#2371)

* feat: add percentage change to move-floating-window

* fixes

---------

Co-authored-by: Ivan Molodetskikh <yalterz@gmail.com>
This commit is contained in:
Mykyta Onipchenko
2025-10-18 01:39:50 -04:00
committed by GitHub
parent d31a90edb0
commit 79cdbc5748
4 changed files with 113 additions and 23 deletions
+48 -14
View File
@@ -813,14 +813,14 @@ pub enum Action {
/// How to change the X position.
#[cfg_attr(
feature = "clap",
arg(short, long, default_value = "+0", allow_negative_numbers = true)
arg(short, long, default_value = "+0", allow_hyphen_values = true)
)]
x: PositionChange,
/// How to change the Y position.
#[cfg_attr(
feature = "clap",
arg(short, long, default_value = "+0", allow_negative_numbers = true)
arg(short, long, default_value = "+0", allow_hyphen_values = true)
)]
y: PositionChange,
},
@@ -913,8 +913,12 @@ pub enum SizeChange {
pub enum PositionChange {
/// Set the position in logical pixels.
SetFixed(f64),
/// Set the position as a proportion of the working area.
SetProportion(f64),
/// Add or subtract to the current position in logical pixels.
AdjustFixed(f64),
/// Add or subtract to the current position as a proportion of the working area.
AdjustProportion(f64),
}
/// Workspace reference (id, index or name) to operate on.
@@ -1519,17 +1523,38 @@ impl FromStr for PositionChange {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = s;
match value.bytes().next() {
Some(b'-' | b'+') => {
let value = value.parse().map_err(|_| "error parsing value")?;
Ok(Self::AdjustFixed(value))
match s.split_once('%') {
Some((value, empty)) => {
if !empty.is_empty() {
return Err("trailing characters after '%' are not allowed");
}
match value.bytes().next() {
Some(b'-' | b'+') => {
let value = value.parse().map_err(|_| "error parsing value")?;
Ok(Self::AdjustProportion(value))
}
Some(_) => {
let value = value.parse().map_err(|_| "error parsing value")?;
Ok(Self::SetProportion(value))
}
None => Err("value is missing"),
}
}
Some(_) => {
let value = value.parse().map_err(|_| "error parsing value")?;
Ok(Self::SetFixed(value))
None => {
let value = s;
match value.bytes().next() {
Some(b'-' | b'+') => {
let value = value.parse().map_err(|_| "error parsing value")?;
Ok(Self::AdjustFixed(value))
}
Some(_) => {
let value = value.parse().map_err(|_| "error parsing value")?;
Ok(Self::SetFixed(value))
}
None => Err("value is missing"),
}
}
None => Err("value is missing"),
}
}
}
@@ -1686,9 +1711,18 @@ mod tests {
PositionChange::AdjustFixed(-10.),
);
assert!("10%".parse::<PositionChange>().is_err());
assert!("+10%".parse::<PositionChange>().is_err());
assert!("-10%".parse::<PositionChange>().is_err());
assert_eq!(
"10%".parse::<PositionChange>().unwrap(),
PositionChange::SetProportion(10.)
);
assert_eq!(
"+10%".parse::<PositionChange>().unwrap(),
PositionChange::AdjustProportion(10.)
);
assert_eq!(
"-10%".parse::<PositionChange>().unwrap(),
PositionChange::AdjustProportion(-10.)
);
assert!("-".parse::<PositionChange>().is_err());
assert!("10% ".parse::<PositionChange>().is_err());
}