Skip to content

Commit

Permalink
feat: scale inner and outer gaps by monitor DPI (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWischeropp authored Oct 4, 2024
1 parent 2e63e90 commit adc6d56
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
19 changes: 18 additions & 1 deletion packages/wm/src/common/length_value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{ops::Mul, str::FromStr};

use anyhow::{bail, Context};
use regex::Regex;
Expand Down Expand Up @@ -32,6 +32,13 @@ impl LengthValue {
}
}

pub fn to_px_scaled(&self, total_px: i32, scale_factor: f32) -> i32 {
match self.unit {
LengthUnit::Percentage => self.to_px(total_px),
LengthUnit::Pixel => (scale_factor * self.amount) as i32,
}
}

pub fn to_percentage(&self, total_px: i32) -> f32 {
match self.unit {
LengthUnit::Percentage => self.amount,
Expand Down Expand Up @@ -104,3 +111,13 @@ impl<'de> Deserialize<'de> for LengthValue {
}
}
}

impl Mul<f32> for LengthValue {
type Output = Self;
fn mul(self, rhs: f32) -> Self {
Self {
amount: self.amount * rhs,
unit: self.unit,
}
}
}
14 changes: 14 additions & 0 deletions packages/wm/src/common/rect_delta.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Mul;

use serde::{Deserialize, Serialize};

use super::LengthValue;
Expand Down Expand Up @@ -32,3 +34,15 @@ impl RectDelta {
}
}
}

impl Mul<f32> for RectDelta {
type Output = Self;
fn mul(self, rhs: f32) -> Self {
Self::new(
self.left * rhs,
self.top * rhs,
self.right * rhs,
self.bottom * rhs,
)
}
}
9 changes: 8 additions & 1 deletion packages/wm/src/containers/traits/tiling_size_getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ macro_rules! impl_tiling_size_getters {
}

fn inner_gap(&self) -> LengthValue {
self.0.borrow().inner_gap.clone()
let scale = match self.monitor() {
None => 1_f32,
Some(monitor) => match monitor.native().dpi() {
Ok(dpi) => dpi,
Err(_) => 1_f32,
},
};
self.0.borrow().inner_gap.clone() * scale
}

fn set_inner_gap(&self, inner_gap: LengthValue) {
Expand Down
8 changes: 6 additions & 2 deletions packages/wm/src/workspaces/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ impl PositionGetters for Workspace {
.cloned()
.context("Failed to get working area of parent monitor.")?;

let outer_gap = &self.0.borrow().outer_gap;
Ok(working_rect.apply_inverse_delta(outer_gap))
let scale = match self.monitor() {
None => 1_f32,
Some(monitor) => monitor.native().dpi()?,
};
let outer_gap = self.0.borrow().outer_gap.clone() * scale;
Ok(working_rect.apply_inverse_delta(&outer_gap))
}
}

0 comments on commit adc6d56

Please sign in to comment.