ui/screenshot_ui: Correct fractional scaled behavior
Browse files at this commit4b830ee7ff2a96fc7bf05176c9da0095f890e1adParent: 8e415682 changed files +74 -71
- src/input/mod.rs +25 -26
- src/ui/screenshot_ui.rs +49 -45
src/input/mod.rsmodified+25 -26
| @@ -1,4 +1,5 @@ | |||
| 1 | use std::any::Any; | 1 | use std::any::Any; |
| 2 | +use std::cmp::min; | ||
| 2 | use std::collections::hash_map::Entry; | 3 | use std::collections::hash_map::Entry; |
| 3 | use std::collections::HashSet; | 4 | use std::collections::HashSet; |
| 4 | use std::time::Duration; | 5 | use std::time::Duration; |
| @@ -1009,16 +1010,16 @@ impl State { | |||
| 1009 | 1010 | ||
| 1010 | if let Some(output) = self.niri.screenshot_ui.selection_output() { | 1011 | if let Some(output) = self.niri.screenshot_ui.selection_output() { |
| 1011 | let geom = self.niri.global_space.output_geometry(output).unwrap(); | 1012 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 1012 | - let mut point = new_pos; | 1013 | + let mut point = (new_pos - geom.loc.to_f64()) |
| 1013 | - point.x = point | ||
| 1014 | - .x | ||
| 1015 | - .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 1016 | - point.y = point | ||
| 1017 | - .y | ||
| 1018 | - .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 1019 | - let point = (point - geom.loc.to_f64()) | ||
| 1020 | .to_physical(output.current_scale().fractional_scale()) | 1014 | .to_physical(output.current_scale().fractional_scale()) |
| 1021 | .to_i32_round(); | 1015 | .to_i32_round(); |
| 1016 | + | ||
| 1017 | + let size = output.current_mode().unwrap().size; | ||
| 1018 | + let transform = output.current_transform(); | ||
| 1019 | + let size = transform.transform_size(size); | ||
| 1020 | + point.x = min(size.w - 1, point.x); | ||
| 1021 | + point.y = min(size.h - 1, point.y); | ||
| 1022 | + | ||
| 1022 | self.niri.screenshot_ui.pointer_motion(point); | 1023 | self.niri.screenshot_ui.pointer_motion(point); |
| 1023 | } | 1024 | } |
| 1024 | 1025 | ||
| @@ -1106,16 +1107,16 @@ impl State { | |||
| 1106 | 1107 | ||
| 1107 | if let Some(output) = self.niri.screenshot_ui.selection_output() { | 1108 | if let Some(output) = self.niri.screenshot_ui.selection_output() { |
| 1108 | let geom = self.niri.global_space.output_geometry(output).unwrap(); | 1109 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 1109 | - let mut point = pos; | 1110 | + let mut point = (pos - geom.loc.to_f64()) |
| 1110 | - point.x = point | ||
| 1111 | - .x | ||
| 1112 | - .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 1113 | - point.y = point | ||
| 1114 | - .y | ||
| 1115 | - .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 1116 | - let point = (point - geom.loc.to_f64()) | ||
| 1117 | .to_physical(output.current_scale().fractional_scale()) | 1111 | .to_physical(output.current_scale().fractional_scale()) |
| 1118 | .to_i32_round(); | 1112 | .to_i32_round(); |
| 1113 | + | ||
| 1114 | + let size = output.current_mode().unwrap().size; | ||
| 1115 | + let transform = output.current_transform(); | ||
| 1116 | + let size = transform.transform_size(size); | ||
| 1117 | + point.x = min(size.w - 1, point.x); | ||
| 1118 | + point.y = min(size.h - 1, point.y); | ||
| 1119 | + | ||
| 1119 | self.niri.screenshot_ui.pointer_motion(point); | 1120 | self.niri.screenshot_ui.pointer_motion(point); |
| 1120 | } | 1121 | } |
| 1121 | 1122 | ||
| @@ -1277,18 +1278,16 @@ impl State { | |||
| 1277 | if let Some((output, _)) = self.niri.output_under(pos) { | 1278 | if let Some((output, _)) = self.niri.output_under(pos) { |
| 1278 | let output = output.clone(); | 1279 | let output = output.clone(); |
| 1279 | let geom = self.niri.global_space.output_geometry(&output).unwrap(); | 1280 | let geom = self.niri.global_space.output_geometry(&output).unwrap(); |
| 1280 | - let mut point = pos; | 1281 | + let mut point = (pos - geom.loc.to_f64()) |
| 1281 | - // Re-clamp as pointer can be within 0.5 from the limit which will round up | ||
| 1282 | - // to a wrong value. | ||
| 1283 | - point.x = point | ||
| 1284 | - .x | ||
| 1285 | - .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 1286 | - point.y = point | ||
| 1287 | - .y | ||
| 1288 | - .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 1289 | - let point = (point - geom.loc.to_f64()) | ||
| 1290 | .to_physical(output.current_scale().fractional_scale()) | 1282 | .to_physical(output.current_scale().fractional_scale()) |
| 1291 | .to_i32_round(); | 1283 | .to_i32_round(); |
| 1284 | + | ||
| 1285 | + let size = output.current_mode().unwrap().size; | ||
| 1286 | + let transform = output.current_transform(); | ||
| 1287 | + let size = transform.transform_size(size); | ||
| 1288 | + point.x = min(size.w - 1, point.x); | ||
| 1289 | + point.y = min(size.h - 1, point.y); | ||
| 1290 | + | ||
| 1292 | if self | 1291 | if self |
| 1293 | .niri | 1292 | .niri |
| 1294 | .screenshot_ui | 1293 | .screenshot_ui |
src/ui/screenshot_ui.rsmodified+49 -45
| @@ -8,7 +8,6 @@ use arrayvec::ArrayVec; | |||
| 8 | use niri_config::Action; | 8 | use niri_config::Action; |
| 9 | use smithay::backend::allocator::Fourcc; | 9 | use smithay::backend::allocator::Fourcc; |
| 10 | use smithay::backend::input::{ButtonState, MouseButton}; | 10 | use smithay::backend::input::{ButtonState, MouseButton}; |
| 11 | -use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; | ||
| 12 | use smithay::backend::renderer::element::Kind; | 11 | use smithay::backend::renderer::element::Kind; |
| 13 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | 12 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 14 | use smithay::backend::renderer::ExportMem; | 13 | use smithay::backend::renderer::ExportMem; |
| @@ -18,8 +17,10 @@ use smithay::utils::{Physical, Point, Rectangle, Size, Transform}; | |||
| 18 | 17 | ||
| 19 | use crate::niri_render_elements; | 18 | use crate::niri_render_elements; |
| 20 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | 19 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 20 | +use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; | ||
| 21 | use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; | 21 | use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; |
| 22 | use crate::render_helpers::RenderTarget; | 22 | use crate::render_helpers::RenderTarget; |
| 23 | +use crate::utils::apply_scale; | ||
| 23 | 24 | ||
| 24 | const BORDER: i32 = 2; | 25 | const BORDER: i32 = 2; |
| 25 | 26 | ||
| @@ -123,14 +124,14 @@ impl ScreenshotUi { | |||
| 123 | ) | 124 | ) |
| 124 | }); | 125 | }); |
| 125 | let buffers = [ | 126 | let buffers = [ |
| 126 | - SolidColorBuffer::new((0, 0), [1., 1., 1., 1.]), | 127 | + SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 127 | - SolidColorBuffer::new((0, 0), [1., 1., 1., 1.]), | 128 | + SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 128 | - SolidColorBuffer::new((0, 0), [1., 1., 1., 1.]), | 129 | + SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 129 | - SolidColorBuffer::new((0, 0), [1., 1., 1., 1.]), | 130 | + SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 130 | - SolidColorBuffer::new((0, 0), [0., 0., 0., 0.5]), | 131 | + SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 131 | - SolidColorBuffer::new((0, 0), [0., 0., 0., 0.5]), | 132 | + SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 132 | - SolidColorBuffer::new((0, 0), [0., 0., 0., 0.5]), | 133 | + SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 133 | - SolidColorBuffer::new((0, 0), [0., 0., 0., 0.5]), | 134 | + SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 134 | ]; | 135 | ]; |
| 135 | let locations = [Default::default(); 8]; | 136 | let locations = [Default::default(); 8]; |
| 136 | let data = OutputData { | 137 | let data = OutputData { |
| @@ -167,10 +168,9 @@ impl ScreenshotUi { | |||
| 167 | } | 168 | } |
| 168 | }; | 169 | }; |
| 169 | 170 | ||
| 170 | - let scale = selection.0.current_scale().integer_scale(); | ||
| 171 | let last_selection = Some(( | 171 | let last_selection = Some(( |
| 172 | selection.0.downgrade(), | 172 | selection.0.downgrade(), |
| 173 | - rect_from_corner_points(selection.1, selection.2, scale), | 173 | + rect_from_corner_points(selection.1, selection.2), |
| 174 | )); | 174 | )); |
| 175 | 175 | ||
| 176 | *self = Self::Closed { last_selection }; | 176 | *self = Self::Closed { last_selection }; |
| @@ -193,17 +193,15 @@ impl ScreenshotUi { | |||
| 193 | }; | 193 | }; |
| 194 | 194 | ||
| 195 | let (selection_output, a, b) = selection; | 195 | let (selection_output, a, b) = selection; |
| 196 | - let scale = selection_output.current_scale().integer_scale(); | 196 | + let mut rect = rect_from_corner_points(*a, *b); |
| 197 | - let mut rect = rect_from_corner_points(*a, *b, scale); | ||
| 198 | 197 | ||
| 199 | for (output, data) in output_data { | 198 | for (output, data) in output_data { |
| 200 | let buffers = &mut data.buffers; | 199 | let buffers = &mut data.buffers; |
| 201 | let locations = &mut data.locations; | 200 | let locations = &mut data.locations; |
| 202 | let size = data.size; | 201 | let size = data.size; |
| 202 | + let scale = data.scale; | ||
| 203 | 203 | ||
| 204 | if output == selection_output { | 204 | if output == selection_output { |
| 205 | - let scale = output.current_scale().integer_scale(); | ||
| 206 | - | ||
| 207 | // Check if the selection is still valid. If not, reset it back to default. | 205 | // Check if the selection is still valid. If not, reset it back to default. |
| 208 | if !Rectangle::from_loc_and_size((0, 0), size).contains_rect(rect) { | 206 | if !Rectangle::from_loc_and_size((0, 0), size).contains_rect(rect) { |
| 209 | rect = Rectangle::from_loc_and_size( | 207 | rect = Rectangle::from_loc_and_size( |
| @@ -211,20 +209,29 @@ impl ScreenshotUi { | |||
| 211 | (size.w / 2, size.h / 2), | 209 | (size.w / 2, size.h / 2), |
| 212 | ); | 210 | ); |
| 213 | *a = rect.loc; | 211 | *a = rect.loc; |
| 214 | - *b = rect.loc + rect.size - Size::from((scale, scale)); | 212 | + *b = rect.loc + rect.size - Size::from((1, 1)); |
| 215 | } | 213 | } |
| 216 | 214 | ||
| 217 | - let border = BORDER * scale; | 215 | + let border = apply_scale(scale, BORDER); |
| 218 | 216 | ||
| 219 | - buffers[0].resize((rect.size.w + border * 2, border)); | 217 | + let resize = move |buffer: &mut SolidColorBuffer, w: i32, h: i32| { |
| 220 | - buffers[1].resize((rect.size.w + border * 2, border)); | 218 | + let size = Size::<_, Physical>::from((w, h)); |
| 221 | - buffers[2].resize((border, rect.size.h)); | 219 | + buffer.resize(size.to_f64().to_logical(scale)); |
| 222 | - buffers[3].resize((border, rect.size.h)); | 220 | + }; |
| 223 | 221 | ||
| 224 | - buffers[4].resize((size.w, rect.loc.y)); | 222 | + resize(&mut buffers[0], rect.size.w + border * 2, border); |
| 225 | - buffers[5].resize((size.w, size.h - rect.loc.y - rect.size.h)); | 223 | + resize(&mut buffers[1], rect.size.w + border * 2, border); |
| 226 | - buffers[6].resize((rect.loc.x, rect.size.h)); | 224 | + resize(&mut buffers[2], border, rect.size.h); |
| 227 | - buffers[7].resize((size.w - rect.loc.x - rect.size.w, rect.size.h)); | 225 | + resize(&mut buffers[3], border, rect.size.h); |
| 226 | + | ||
| 227 | + resize(&mut buffers[4], size.w, rect.loc.y); | ||
| 228 | + resize(&mut buffers[5], size.w, size.h - rect.loc.y - rect.size.h); | ||
| 229 | + resize(&mut buffers[6], rect.loc.x, rect.size.h); | ||
| 230 | + resize( | ||
| 231 | + &mut buffers[7], | ||
| 232 | + size.w - rect.loc.x - rect.size.w, | ||
| 233 | + rect.size.h, | ||
| 234 | + ); | ||
| 228 | 235 | ||
| 229 | locations[0] = Point::from((rect.loc.x - border, rect.loc.y - border)); | 236 | locations[0] = Point::from((rect.loc.x - border, rect.loc.y - border)); |
| 230 | locations[1] = Point::from((rect.loc.x - border, rect.loc.y + rect.size.h)); | 237 | locations[1] = Point::from((rect.loc.x - border, rect.loc.y + rect.size.h)); |
| @@ -235,15 +242,15 @@ impl ScreenshotUi { | |||
| 235 | locations[6] = Point::from((0, rect.loc.y)); | 242 | locations[6] = Point::from((0, rect.loc.y)); |
| 236 | locations[7] = Point::from((rect.loc.x + rect.size.w, rect.loc.y)); | 243 | locations[7] = Point::from((rect.loc.x + rect.size.w, rect.loc.y)); |
| 237 | } else { | 244 | } else { |
| 238 | - buffers[0].resize((0, 0)); | 245 | + buffers[0].resize((0., 0.)); |
| 239 | - buffers[1].resize((0, 0)); | 246 | + buffers[1].resize((0., 0.)); |
| 240 | - buffers[2].resize((0, 0)); | 247 | + buffers[2].resize((0., 0.)); |
| 241 | - buffers[3].resize((0, 0)); | 248 | + buffers[3].resize((0., 0.)); |
| 242 | - | 249 | + |
| 243 | - buffers[4].resize(size.to_logical(1)); | 250 | + buffers[4].resize(size.to_f64().to_logical(data.scale)); |
| 244 | - buffers[5].resize((0, 0)); | 251 | + buffers[5].resize((0., 0.)); |
| 245 | - buffers[6].resize((0, 0)); | 252 | + buffers[6].resize((0., 0.)); |
| 246 | - buffers[7].resize((0, 0)); | 253 | + buffers[7].resize((0., 0.)); |
| 247 | } | 254 | } |
| 248 | } | 255 | } |
| 249 | } | 256 | } |
| @@ -269,8 +276,7 @@ impl ScreenshotUi { | |||
| 269 | elements.extend(buf_loc.map(|(buffer, loc)| { | 276 | elements.extend(buf_loc.map(|(buffer, loc)| { |
| 270 | SolidColorRenderElement::from_buffer( | 277 | SolidColorRenderElement::from_buffer( |
| 271 | buffer, | 278 | buffer, |
| 272 | - *loc, | 279 | + loc.to_f64().to_logical(output_data.scale), |
| 273 | - 1., // We treat these as physical coordinates. | ||
| 274 | 1., | 280 | 1., |
| 275 | Kind::Unspecified, | 281 | Kind::Unspecified, |
| 276 | ) | 282 | ) |
| @@ -314,8 +320,7 @@ impl ScreenshotUi { | |||
| 314 | }; | 320 | }; |
| 315 | 321 | ||
| 316 | let data = &output_data[&selection.0]; | 322 | let data = &output_data[&selection.0]; |
| 317 | - let scale = selection.0.current_scale().integer_scale(); | 323 | + let rect = rect_from_corner_points(selection.1, selection.2); |
| 318 | - let rect = rect_from_corner_points(selection.1, selection.2, scale); | ||
| 319 | let buf_rect = rect | 324 | let buf_rect = rect |
| 320 | .to_logical(1) | 325 | .to_logical(1) |
| 321 | .to_buffer(1, Transform::Normal, &data.size.to_logical(1)); | 326 | .to_buffer(1, Transform::Normal, &data.size.to_logical(1)); |
| @@ -411,16 +416,14 @@ impl ScreenshotUi { | |||
| 411 | // Check if the resulting selection is zero-sized, and try to come up with a small | 416 | // Check if the resulting selection is zero-sized, and try to come up with a small |
| 412 | // default rectangle. | 417 | // default rectangle. |
| 413 | let (output, a, b) = selection; | 418 | let (output, a, b) = selection; |
| 414 | - let scale = output.current_scale().integer_scale(); | 419 | + let mut rect = rect_from_corner_points(*a, *b); |
| 415 | - let mut rect = rect_from_corner_points(*a, *b, scale); | 420 | + if rect.size.is_empty() || rect.size == Size::from((1, 1)) { |
| 416 | - if rect.size.is_empty() || rect.size == Size::from((scale, scale)) { | ||
| 417 | let data = &output_data[output]; | 421 | let data = &output_data[output]; |
| 418 | rect = Rectangle::from_loc_and_size((rect.loc.x - 16, rect.loc.y - 16), (32, 32)) | 422 | rect = Rectangle::from_loc_and_size((rect.loc.x - 16, rect.loc.y - 16), (32, 32)) |
| 419 | .intersection(Rectangle::from_loc_and_size((0, 0), data.size)) | 423 | .intersection(Rectangle::from_loc_and_size((0, 0), data.size)) |
| 420 | .unwrap_or_default(); | 424 | .unwrap_or_default(); |
| 421 | - let scale = output.current_scale().integer_scale(); | ||
| 422 | *a = rect.loc; | 425 | *a = rect.loc; |
| 423 | - *b = rect.loc + rect.size - Size::from((scale, scale)); | 426 | + *b = rect.loc + rect.size - Size::from((1, 1)); |
| 424 | } | 427 | } |
| 425 | } | 428 | } |
| 426 | 429 | ||
| @@ -457,11 +460,12 @@ fn action(raw: Keysym, mods: ModifiersState) -> Option<Action> { | |||
| 457 | pub fn rect_from_corner_points( | 460 | pub fn rect_from_corner_points( |
| 458 | a: Point<i32, Physical>, | 461 | a: Point<i32, Physical>, |
| 459 | b: Point<i32, Physical>, | 462 | b: Point<i32, Physical>, |
| 460 | - scale: i32, | ||
| 461 | ) -> Rectangle<i32, Physical> { | 463 | ) -> Rectangle<i32, Physical> { |
| 462 | let x1 = min(a.x, b.x); | 464 | let x1 = min(a.x, b.x); |
| 463 | let y1 = min(a.y, b.y); | 465 | let y1 = min(a.y, b.y); |
| 464 | let x2 = max(a.x, b.x); | 466 | let x2 = max(a.x, b.x); |
| 465 | let y2 = max(a.y, b.y); | 467 | let y2 = max(a.y, b.y); |
| 466 | - Rectangle::from_extemities((x1, y1), (x2 + scale, y2 + scale)) | 468 | + // We're adding + 1 because the pointer is clamped to output size - 1, so to get the full |
| 469 | + // screen worth of selection we must add back that + 1. | ||
| 470 | + Rectangle::from_extemities((x1, y1), (x2 + 1, y2 + 1)) | ||
| 467 | } | 471 | } |