Round scale to closest representable
Browse files at this commit33b5beaeee62e47ecaa33fc3175ef9e22931f116Parent: 1dae45c2 changed files +21 -3
- src/niri.rs +4 -3
- src/utils/scale.rs +17 -0
src/niri.rsmodified+4 -3
| @@ -124,7 +124,7 @@ use crate::ui::exit_confirm_dialog::ExitConfirmDialog; | |||
| 124 | use crate::ui::hotkey_overlay::HotkeyOverlay; | 124 | use crate::ui::hotkey_overlay::HotkeyOverlay; |
| 125 | use crate::ui::screen_transition::{self, ScreenTransition}; | 125 | use crate::ui::screen_transition::{self, ScreenTransition}; |
| 126 | use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement}; | 126 | use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement}; |
| 127 | -use crate::utils::scale::guess_monitor_scale; | 127 | +use crate::utils::scale::{closest_representable_scale, guess_monitor_scale}; |
| 128 | use crate::utils::spawning::CHILD_ENV; | 128 | use crate::utils::spawning::CHILD_ENV; |
| 129 | use crate::utils::{ | 129 | use crate::utils::{ |
| 130 | center, center_f64, get_monotonic_time, ipc_transform_to_smithay, logical_output, | 130 | center, center_f64, get_monotonic_time, ipc_transform_to_smithay, logical_output, |
| @@ -1053,7 +1053,7 @@ impl State { | |||
| 1053 | let resolution = output.current_mode().unwrap().size; | 1053 | let resolution = output.current_mode().unwrap().size; |
| 1054 | guess_monitor_scale(size_mm, resolution) | 1054 | guess_monitor_scale(size_mm, resolution) |
| 1055 | }); | 1055 | }); |
| 1056 | - let scale = scale.clamp(1., 10.); | 1056 | + let scale = closest_representable_scale(scale.clamp(1., 10.)); |
| 1057 | 1057 | ||
| 1058 | let mut transform = config | 1058 | let mut transform = config |
| 1059 | .map(|c| ipc_transform_to_smithay(c.transform)) | 1059 | .map(|c| ipc_transform_to_smithay(c.transform)) |
| @@ -1736,7 +1736,8 @@ impl Niri { | |||
| 1736 | let resolution = output.current_mode().unwrap().size; | 1736 | let resolution = output.current_mode().unwrap().size; |
| 1737 | guess_monitor_scale(size_mm, resolution) | 1737 | guess_monitor_scale(size_mm, resolution) |
| 1738 | }); | 1738 | }); |
| 1739 | - let scale = scale.clamp(1., 10.); | 1739 | + let scale = closest_representable_scale(scale.clamp(1., 10.)); |
| 1740 | + | ||
| 1740 | let mut transform = c | 1741 | let mut transform = c |
| 1741 | .map(|c| ipc_transform_to_smithay(c.transform)) | 1742 | .map(|c| ipc_transform_to_smithay(c.transform)) |
| 1742 | .unwrap_or(Transform::Normal); | 1743 | .unwrap_or(Transform::Normal); |
src/utils/scale.rsmodified+17 -0
| @@ -51,6 +51,13 @@ fn is_valid_for_resolution(resolution: Size<i32, Physical>, scale: i32) -> bool | |||
| 51 | logical.w * logical.h >= MIN_LOGICAL_AREA | 51 | logical.w * logical.h >= MIN_LOGICAL_AREA |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | +/// Adjusts the scale to the closest exactly-representable value. | ||
| 55 | +pub fn closest_representable_scale(scale: f64) -> f64 { | ||
| 56 | + // Current fractional-scale Wayland protocol can only represent N / 120 scales. | ||
| 57 | + const FRACTIONAL_SCALE_DENOM: f64 = 120.; | ||
| 58 | + | ||
| 59 | + (scale * FRACTIONAL_SCALE_DENOM).round() / FRACTIONAL_SCALE_DENOM | ||
| 60 | +} | ||
| 54 | #[cfg(test)] | 61 | #[cfg(test)] |
| 55 | mod tests { | 62 | mod tests { |
| 56 | use k9::snapshot; | 63 | use k9::snapshot; |
| @@ -101,4 +108,14 @@ mod tests { | |||
| 101 | fn guess_monitor_scale_unknown_size() { | 108 | fn guess_monitor_scale_unknown_size() { |
| 102 | assert_eq!(check((0, 0), (1920, 1080)), 1.); | 109 | assert_eq!(check((0, 0), (1920, 1080)), 1.); |
| 103 | } | 110 | } |
| 111 | + | ||
| 112 | + #[test] | ||
| 113 | + fn test_round_scale() { | ||
| 114 | + snapshot!(closest_representable_scale(1.3), "1.3"); | ||
| 115 | + snapshot!(closest_representable_scale(1.31), "1.3083333333333333"); | ||
| 116 | + snapshot!(closest_representable_scale(1.32), "1.3166666666666667"); | ||
| 117 | + snapshot!(closest_representable_scale(1.33), "1.3333333333333333"); | ||
| 118 | + snapshot!(closest_representable_scale(1.34), "1.3416666666666666"); | ||
| 119 | + snapshot!(closest_representable_scale(1.35), "1.35"); | ||
| 120 | + } | ||
| 104 | } | 121 | } |