Allow for floating-point percentages in window geometry (fixes #601) (#602)

This commit is contained in:
ElKowar 2022-10-24 19:08:50 +02:00 committed by GitHub
parent 0cccd9d74f
commit 471f1a117d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 14 deletions

View file

@ -7,6 +7,7 @@ All notable changes to eww will be listed here, starting at changes since versio
### Features
- Add support for safe access (`?.`) in simplexpr (By: oldwomanjosiah)
- Allow floating-point numbers in percentages for window-geometry
## [0.4.0] (04.09.2022)

View file

@ -23,7 +23,7 @@ mod backend {
use super::*;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct BackendWindowOptions {
pub wm_ignore: bool,
pub sticky: bool,
@ -94,7 +94,7 @@ mod backend {
}
// Surface definition if the backend for X11 is enable
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, serde::Serialize)]
#[derive(Debug, Clone, Copy, PartialEq, Default, serde::Serialize)]
pub struct StrutDefinition {
pub side: Side,
pub dist: NumWithUnit,

View file

@ -89,7 +89,7 @@ impl FromAst for TopLevel {
}
}
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize)]
#[derive(Debug, PartialEq, Clone, serde::Serialize)]
pub struct Config {
pub widget_definitions: HashMap<String, WidgetDefinition>,
pub window_definitions: HashMap<String, WindowDefinition>,

View file

@ -17,7 +17,7 @@ use eww_shared_util::{AttrName, Span, VarName};
use super::{backend_window_options::BackendWindowOptions, widget_use::WidgetUse, window_geometry::WindowGeometry};
#[derive(Debug, Clone, serde::Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, serde::Serialize, PartialEq)]
pub struct WindowDefinition {
pub name: String,
pub geometry: Option<WindowGeometry>,

View file

@ -110,7 +110,7 @@ impl std::str::FromStr for AnchorPoint {
}
}
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Serialize)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize)]
pub struct WindowGeometry {
pub anchor_point: AnchorPoint,
pub offset: Coords,

View file

@ -14,11 +14,11 @@ pub enum Error {
MalformedCoords,
}
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Display, DebugCustom, SmartDefault)]
#[derive(Clone, Copy, PartialEq, Deserialize, Serialize, Display, DebugCustom, SmartDefault)]
pub enum NumWithUnit {
#[display(fmt = "{}%", .0)]
#[debug(fmt = "{}%", .0)]
Percent(i32),
Percent(f32),
#[display(fmt = "{}px", .0)]
#[debug(fmt = "{}px", .0)]
#[default]
@ -33,10 +33,10 @@ impl NumWithUnit {
}
}
pub fn perc_relative_to(&self, max: i32) -> i32 {
pub fn perc_relative_to(&self, max: i32) -> f32 {
match *self {
NumWithUnit::Percent(n) => n,
NumWithUnit::Pixels(n) => ((n as f64 / max as f64) * 100.0) as i32,
NumWithUnit::Pixels(n) => ((n as f64 / max as f64) * 100.0) as f32,
}
}
}
@ -45,19 +45,19 @@ impl FromStr for NumWithUnit {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
static PATTERN: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new("^(-?\\d+)(.*)$").unwrap());
static PATTERN: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new("^(-?\\d+(?:.\\d+)?)(.*)$").unwrap());
let captures = PATTERN.captures(s).ok_or_else(|| Error::NumParseFailed(s.to_string()))?;
let value = captures.get(1).unwrap().as_str().parse::<i32>().map_err(|_| Error::NumParseFailed(s.to_string()))?;
let value = captures.get(1).unwrap().as_str().parse::<f32>().map_err(|_| Error::NumParseFailed(s.to_string()))?;
match captures.get(2).unwrap().as_str() {
"px" | "" => Ok(NumWithUnit::Pixels(value)),
"px" | "" => Ok(NumWithUnit::Pixels(value.floor() as i32)),
"%" => Ok(NumWithUnit::Percent(value)),
unit => Err(Error::InvalidUnit(unit.to_string())),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Display, Default)]
#[derive(Clone, Copy, PartialEq, Deserialize, Serialize, Display, Default)]
#[display(fmt = "{}*{}", x, y)]
pub struct Coords {
pub x: NumWithUnit,
@ -106,7 +106,8 @@ mod test {
fn test_parse_num_with_unit() {
assert_eq!(NumWithUnit::Pixels(55), NumWithUnit::from_str("55").unwrap());
assert_eq!(NumWithUnit::Pixels(55), NumWithUnit::from_str("55px").unwrap());
assert_eq!(NumWithUnit::Percent(55), NumWithUnit::from_str("55%").unwrap());
assert_eq!(NumWithUnit::Percent(55.0), NumWithUnit::from_str("55%").unwrap());
assert_eq!(NumWithUnit::Percent(55.5), NumWithUnit::from_str("55.5%").unwrap());
assert!(NumWithUnit::from_str("55pp").is_err());
}