Default winit output scale to window scale
Browse files at this commit7449fe896879c6a7e67e2ec3838b05ad0180abf2Parent: e38aa0a3 changed files +38 -11
- src/backend/winit.rs +19 -5
- src/niri.rs +11 -6
- src/utils/mod.rs +8 -0
src/backend/winit.rsmodified+19 -5
| @@ -1,4 +1,4 @@ | ||
| 1 | -use std::cell::RefCell; | |
| 1 | +use std::cell::{Cell, RefCell}; | |
| 2 | 2 | use std::collections::HashMap; |
| 3 | 3 | use std::mem; |
| 4 | 4 | use std::rc::Rc; |
| @@ -26,7 +26,7 @@ use super::{IpcOutputMap, OutputId, RenderResult}; | ||
| 26 | 26 | use crate::niri::{Niri, RedrawState, State}; |
| 27 | 27 | use crate::render_helpers::debug::draw_damage; |
| 28 | 28 | use crate::render_helpers::{resources, shaders, RenderCtx, RenderTarget}; |
| 29 | -use crate::utils::{get_monotonic_time, logical_output}; | |
| 29 | +use crate::utils::{get_monotonic_time, logical_output, WinitScale}; | |
| 30 | 30 | |
| 31 | 31 | pub struct Winit { |
| 32 | 32 | config: Rc<RefCell<Config>>, |
| @@ -81,6 +81,10 @@ impl Winit { | ||
| 81 | 81 | serial: None, |
| 82 | 82 | }); |
| 83 | 83 | |
| 84 | + output | |
| 85 | + .user_data() | |
| 86 | + .insert_if_missing(|| WinitScale(Cell::new(backend.scale_factor()))); | |
| 87 | + | |
| 84 | 88 | let physical_properties = output.physical_properties(); |
| 85 | 89 | let ipc_outputs = Arc::new(Mutex::new(HashMap::from([( |
| 86 | 90 | OutputId::next(), |
| @@ -109,9 +113,10 @@ impl Winit { | ||
| 109 | 113 | |
| 110 | 114 | event_loop |
| 111 | 115 | .insert_source(winit, move |event, _, state| match event { |
| 112 | - WinitEvent::Resized { size, .. } => { | |
| 116 | + WinitEvent::Resized { size, scale_factor } => { | |
| 113 | 117 | let winit = state.backend.winit(); |
| 114 | - winit.output.change_current_state( | |
| 118 | + let output = winit.output.clone(); | |
| 119 | + output.change_current_state( | |
| 115 | 120 | Some(Mode { |
| 116 | 121 | size, |
| 117 | 122 | refresh: 60_000, |
| @@ -121,6 +126,11 @@ impl Winit { | ||
| 121 | 126 | None, |
| 122 | 127 | ); |
| 123 | 128 | |
| 129 | + { | |
| 130 | + let scale = output.user_data().get_or_insert(WinitScale::default); | |
| 131 | + scale.0.set(scale_factor); | |
| 132 | + } | |
| 133 | + | |
| 124 | 134 | { |
| 125 | 135 | let mut ipc_outputs = winit.ipc_outputs.lock().unwrap(); |
| 126 | 136 | let output = ipc_outputs.values_mut().next().unwrap(); |
| @@ -130,7 +140,11 @@ impl Winit { | ||
| 130 | 140 | state.niri.ipc_outputs_changed = true; |
| 131 | 141 | } |
| 132 | 142 | |
| 133 | - state.niri.output_resized(&winit.output); | |
| 143 | + state.reload_output_config(); | |
| 144 | + | |
| 145 | + // reload_output_config() will call output_resized() for scale changes, | |
| 146 | + // but not for size-only changes. Even if this is the second call, it's fine. | |
| 147 | + state.niri.output_resized(&output); | |
| 134 | 148 | } |
| 135 | 149 | WinitEvent::Input(event) => state.process_input_event(event), |
| 136 | 150 | WinitEvent::Focus(_) => (), |
src/niri.rsmodified+11 -6
| @@ -188,7 +188,7 @@ use crate::utils::xwayland::satellite::Satellite; | ||
| 188 | 188 | use crate::utils::{ |
| 189 | 189 | center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, is_mapped, |
| 190 | 190 | logical_output, make_screenshot_path, output_matches_name, output_size, panel_orientation, |
| 191 | - send_scale_transform, write_png_rgba8, xwayland, | |
| 191 | + send_scale_transform, winit_scale, write_png_rgba8, xwayland, | |
| 192 | 192 | }; |
| 193 | 193 | use crate::window::mapped::MappedId; |
| 194 | 194 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; |
| @@ -1854,6 +1854,7 @@ impl State { | ||
| 1854 | 1854 | let scale = config |
| 1855 | 1855 | .and_then(|c| c.scale) |
| 1856 | 1856 | .map(|s| s.0) |
| 1857 | + .or_else(|| winit_scale(output)) | |
| 1857 | 1858 | .unwrap_or_else(|| { |
| 1858 | 1859 | let size_mm = output.physical_properties().size; |
| 1859 | 1860 | let resolution = output.current_mode().unwrap().size; |
| @@ -2960,11 +2961,15 @@ impl Niri { | ||
| 2960 | 2961 | |
| 2961 | 2962 | let config = self.config.borrow(); |
| 2962 | 2963 | let c = config.outputs.find(name); |
| 2963 | - let scale = c.and_then(|c| c.scale).map(|s| s.0).unwrap_or_else(|| { | |
| 2964 | - let size_mm = output.physical_properties().size; | |
| 2965 | - let resolution = output.current_mode().unwrap().size; | |
| 2966 | - guess_monitor_scale(size_mm, resolution) | |
| 2967 | - }); | |
| 2964 | + let scale = c | |
| 2965 | + .and_then(|c| c.scale) | |
| 2966 | + .map(|s| s.0) | |
| 2967 | + .or_else(|| winit_scale(&output)) | |
| 2968 | + .unwrap_or_else(|| { | |
| 2969 | + let size_mm = output.physical_properties().size; | |
| 2970 | + let resolution = output.current_mode().unwrap().size; | |
| 2971 | + guess_monitor_scale(size_mm, resolution) | |
| 2972 | + }); | |
| 2968 | 2973 | let scale = closest_representable_scale(scale.clamp(0.1, 10.)); |
| 2969 | 2974 | |
| 2970 | 2975 | let mut transform = panel_orientation(&output) |
src/utils/mod.rsmodified+8 -0
| @@ -1,3 +1,4 @@ | ||
| 1 | +use std::cell::Cell; | |
| 1 | 2 | use std::cmp::{max, min}; |
| 2 | 3 | use std::ffi::{CString, OsStr}; |
| 3 | 4 | use std::fmt::Display; |
| @@ -237,6 +238,13 @@ pub fn panel_orientation(output: &Output) -> Transform { | ||
| 237 | 238 | .unwrap_or(Transform::Normal) |
| 238 | 239 | } |
| 239 | 240 | |
| 241 | +// For winit-backed outputs. | |
| 242 | +#[derive(Default)] | |
| 243 | +pub struct WinitScale(pub Cell<f64>); | |
| 244 | +pub fn winit_scale(output: &Output) -> Option<f64> { | |
| 245 | + output.user_data().get::<WinitScale>().map(|x| x.0.get()) | |
| 246 | +} | |
| 247 | + | |
| 240 | 248 | pub fn ipc_transform_to_smithay(transform: niri_ipc::Transform) -> Transform { |
| 241 | 249 | match transform { |
| 242 | 250 | niri_ipc::Transform::Normal => Transform::Normal, |