diff options
| -rw-r--r-- | src/niri.rs | 7 | ||||
| -rw-r--r-- | src/utils/scale.rs | 17 |
2 files changed, 21 insertions, 3 deletions
diff --git a/src/niri.rs b/src/niri.rs index b8309f65..13e46b08 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -124,7 +124,7 @@ use crate::ui::exit_confirm_dialog::ExitConfirmDialog; use crate::ui::hotkey_overlay::HotkeyOverlay; use crate::ui::screen_transition::{self, ScreenTransition}; use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement}; -use crate::utils::scale::guess_monitor_scale; +use crate::utils::scale::{closest_representable_scale, guess_monitor_scale}; use crate::utils::spawning::CHILD_ENV; use crate::utils::{ center, center_f64, get_monotonic_time, ipc_transform_to_smithay, logical_output, @@ -1053,7 +1053,7 @@ impl State { let resolution = output.current_mode().unwrap().size; guess_monitor_scale(size_mm, resolution) }); - let scale = scale.clamp(1., 10.); + let scale = closest_representable_scale(scale.clamp(1., 10.)); let mut transform = config .map(|c| ipc_transform_to_smithay(c.transform)) @@ -1736,7 +1736,8 @@ impl Niri { let resolution = output.current_mode().unwrap().size; guess_monitor_scale(size_mm, resolution) }); - let scale = scale.clamp(1., 10.); + let scale = closest_representable_scale(scale.clamp(1., 10.)); + let mut transform = c .map(|c| ipc_transform_to_smithay(c.transform)) .unwrap_or(Transform::Normal); diff --git a/src/utils/scale.rs b/src/utils/scale.rs index 339d0678..6db88248 100644 --- a/src/utils/scale.rs +++ b/src/utils/scale.rs @@ -51,6 +51,13 @@ fn is_valid_for_resolution(resolution: Size<i32, Physical>, scale: i32) -> bool logical.w * logical.h >= MIN_LOGICAL_AREA } +/// Adjusts the scale to the closest exactly-representable value. +pub fn closest_representable_scale(scale: f64) -> f64 { + // Current fractional-scale Wayland protocol can only represent N / 120 scales. + const FRACTIONAL_SCALE_DENOM: f64 = 120.; + + (scale * FRACTIONAL_SCALE_DENOM).round() / FRACTIONAL_SCALE_DENOM +} #[cfg(test)] mod tests { use k9::snapshot; @@ -101,4 +108,14 @@ mod tests { fn guess_monitor_scale_unknown_size() { assert_eq!(check((0, 0), (1920, 1080)), 1.); } + + #[test] + fn test_round_scale() { + snapshot!(closest_representable_scale(1.3), "1.3"); + snapshot!(closest_representable_scale(1.31), "1.3083333333333333"); + snapshot!(closest_representable_scale(1.32), "1.3166666666666667"); + snapshot!(closest_representable_scale(1.33), "1.3333333333333333"); + snapshot!(closest_representable_scale(1.34), "1.3416666666666666"); + snapshot!(closest_representable_scale(1.35), "1.35"); + } } |
