forge
Explore
Log in

Don't you dare go Hollow

Default winit output scale to window scale

Browse files at this commit

7449fe896879c6a7e67e2ec3838b05ad0180abf2Ivan MolodetskikhParent: e38aa0a
src/backend/winit.rsmodified+19 -5
@@ -1,4 +1,4 @@
1-use std::cell::RefCell;
1+use std::cell::{Cell, RefCell};
22 use std::collections::HashMap;
33 use std::mem;
44 use std::rc::Rc;
@@ -26,7 +26,7 @@ use super::{IpcOutputMap, OutputId, RenderResult};
2626 use crate::niri::{Niri, RedrawState, State};
2727 use crate::render_helpers::debug::draw_damage;
2828 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};
3030
3131 pub struct Winit {
3232 config: Rc<RefCell<Config>>,
@@ -81,6 +81,10 @@ impl Winit {
8181 serial: None,
8282 });
8383
84+ output
85+ .user_data()
86+ .insert_if_missing(|| WinitScale(Cell::new(backend.scale_factor())));
87+
8488 let physical_properties = output.physical_properties();
8589 let ipc_outputs = Arc::new(Mutex::new(HashMap::from([(
8690 OutputId::next(),
@@ -109,9 +113,10 @@ impl Winit {
109113
110114 event_loop
111115 .insert_source(winit, move |event, _, state| match event {
112- WinitEvent::Resized { size, .. } => {
116+ WinitEvent::Resized { size, scale_factor } => {
113117 let winit = state.backend.winit();
114- winit.output.change_current_state(
118+ let output = winit.output.clone();
119+ output.change_current_state(
115120 Some(Mode {
116121 size,
117122 refresh: 60_000,
@@ -121,6 +126,11 @@ impl Winit {
121126 None,
122127 );
123128
129+ {
130+ let scale = output.user_data().get_or_insert(WinitScale::default);
131+ scale.0.set(scale_factor);
132+ }
133+
124134 {
125135 let mut ipc_outputs = winit.ipc_outputs.lock().unwrap();
126136 let output = ipc_outputs.values_mut().next().unwrap();
@@ -130,7 +140,11 @@ impl Winit {
130140 state.niri.ipc_outputs_changed = true;
131141 }
132142
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);
134148 }
135149 WinitEvent::Input(event) => state.process_input_event(event),
136150 WinitEvent::Focus(_) => (),
src/niri.rsmodified+11 -6
@@ -188,7 +188,7 @@ use crate::utils::xwayland::satellite::Satellite;
188188 use crate::utils::{
189189 center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, is_mapped,
190190 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,
192192 };
193193 use crate::window::mapped::MappedId;
194194 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef};
@@ -1854,6 +1854,7 @@ impl State {
18541854 let scale = config
18551855 .and_then(|c| c.scale)
18561856 .map(|s| s.0)
1857+ .or_else(|| winit_scale(output))
18571858 .unwrap_or_else(|| {
18581859 let size_mm = output.physical_properties().size;
18591860 let resolution = output.current_mode().unwrap().size;
@@ -2960,11 +2961,15 @@ impl Niri {
29602961
29612962 let config = self.config.borrow();
29622963 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+ });
29682973 let scale = closest_representable_scale(scale.clamp(0.1, 10.));
29692974
29702975 let mut transform = panel_orientation(&output)
src/utils/mod.rsmodified+8 -0
@@ -1,3 +1,4 @@
1+use std::cell::Cell;
12 use std::cmp::{max, min};
23 use std::ffi::{CString, OsStr};
34 use std::fmt::Display;
@@ -237,6 +238,13 @@ pub fn panel_orientation(output: &Output) -> Transform {
237238 .unwrap_or(Transform::Normal)
238239 }
239240
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+
240248 pub fn ipc_transform_to_smithay(transform: niri_ipc::Transform) -> Transform {
241249 match transform {
242250 niri_ipc::Transform::Normal => Transform::Normal,