fix(window): Fix incorrect initial scaling (#784)

Also, replaced inverse scaling (like 1 / scaling) with natural scaling.
I think easier to work with natural scaling factor (like 1..2)
and multiplication, instead of inverse scaling factor and division.

Refs #771
macos-click-through
partizan 3 years ago committed by GitHub
parent 73902a6c7a
commit 9f7207feb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -255,7 +255,7 @@ impl Renderer {
#[allow(clippy::needless_collect)] #[allow(clippy::needless_collect)]
pub fn draw_frame(&mut self, root_canvas: &mut Canvas, dt: f32, scaling: f32) -> bool { pub fn draw_frame(&mut self, root_canvas: &mut Canvas, dt: f32, scaling: f32) -> bool {
trace!("Drawing Frame"); trace!("Drawing Frame at {} scale", scaling);
let mut font_changed = false; let mut font_changed = false;
let draw_commands: Vec<_> = self let draw_commands: Vec<_> = self
@ -277,7 +277,7 @@ impl Renderer {
root_canvas.save(); root_canvas.save();
root_canvas.reset_matrix(); root_canvas.reset_matrix();
root_canvas.scale((1.0 / scaling, 1.0 / scaling)); root_canvas.scale((scaling, scaling));
if let Some(root_window) = self.rendered_windows.get(&1) { if let Some(root_window) = self.rendered_windows.get(&1) {
let clip_rect = root_window.pixel_region(self.font_width, self.font_height); let clip_rect = root_window.pixel_region(self.font_width, self.font_height);

@ -49,8 +49,8 @@ fn build_window_surface_with_grid_size(
grid_height: u64, grid_height: u64,
scaling: f32, scaling: f32,
) -> Surface { ) -> Surface {
let pixel_width = ((grid_width * renderer.font_width) as f32 / scaling) as u64; let pixel_width = ((grid_width * renderer.font_width) as f32 * scaling) as u64;
let pixel_height = ((grid_height * renderer.font_height) as f32 / scaling) as u64; let pixel_height = ((grid_height * renderer.font_height) as f32 * scaling) as u64;
let mut surface = build_window_surface(parent_canvas, pixel_width, pixel_height); let mut surface = build_window_surface(parent_canvas, pixel_width, pixel_height);
let canvas = surface.canvas(); let canvas = surface.canvas();
@ -367,7 +367,7 @@ impl RenderedWindow {
let canvas = self.current_surface.surface.canvas(); let canvas = self.current_surface.surface.canvas();
canvas.save(); canvas.save();
canvas.scale((1.0 / scaling, 1.0 / scaling)); canvas.scale((scaling, scaling));
renderer.draw_background(canvas, grid_position, width, &style); renderer.draw_background(canvas, grid_position, width, &style);
renderer.draw_foreground(canvas, &cells, grid_position, width, &style); renderer.draw_foreground(canvas, &cells, grid_position, width, &style);
canvas.restore(); canvas.restore();
@ -381,16 +381,16 @@ impl RenderedWindow {
cols, cols,
} => { } => {
let scrolled_region = Rect::new( let scrolled_region = Rect::new(
(left * renderer.font_width) as f32 / scaling, (left * renderer.font_width) as f32 * scaling,
(top * renderer.font_height) as f32 / scaling, (top * renderer.font_height) as f32 * scaling,
(right * renderer.font_width) as f32 / scaling, (right * renderer.font_width) as f32 * scaling,
(bot * renderer.font_height) as f32 / scaling, (bot * renderer.font_height) as f32 * scaling,
); );
let mut translated_region = scrolled_region; let mut translated_region = scrolled_region;
translated_region.offset(( translated_region.offset((
(-cols * renderer.font_width as i64) as f32 / scaling, (-cols * renderer.font_width as i64) as f32 * scaling,
(-rows * renderer.font_height as i64) as f32 / scaling, (-rows * renderer.font_height as i64) as f32 * scaling,
)); ));
let snapshot = self.current_surface.surface.image_snapshot(); let snapshot = self.current_surface.surface.image_snapshot();

@ -10,6 +10,7 @@ use crate::{
settings::SETTINGS, settings::SETTINGS,
INITIAL_DIMENSIONS, INITIAL_DIMENSIONS,
}; };
use glutin::dpi::LogicalSize;
use std::sync::{atomic::AtomicBool, mpsc::Receiver, Arc}; use std::sync::{atomic::AtomicBool, mpsc::Receiver, Arc};
pub use window_wrapper::start_loop; pub use window_wrapper::start_loop;
@ -67,20 +68,16 @@ fn windows_fix_dpi() {
} }
fn handle_new_grid_size( fn handle_new_grid_size(
new_size: (u64, u64), new_size: LogicalSize<u32>,
renderer: &Renderer, renderer: &Renderer,
ui_command_sender: &LoggingTx<UiCommand>, ui_command_sender: &LoggingTx<UiCommand>,
) { ) {
let (new_width, new_height) = new_size; if new_size.width > 0 && new_size.height > 0 {
if new_width > 0 && new_height > 0 {
// Add 1 here to make sure resizing doesn't change the grid size on startup // Add 1 here to make sure resizing doesn't change the grid size on startup
let new_width = ((new_width + 1) / renderer.font_width) as u32; let width = ((new_size.width + 1) / renderer.font_width as u32) as u32;
let new_height = ((new_height + 1) / renderer.font_height) as u32; let height = ((new_size.height + 1) / renderer.font_height as u32) as u32;
ui_command_sender ui_command_sender
.send(UiCommand::Resize { .send(UiCommand::Resize { width, height })
width: new_width,
height: new_height,
})
.ok(); .ok();
} }
} }

@ -166,12 +166,13 @@ impl GlutinWindowWrapper {
pub fn draw_frame(&mut self, dt: f32) { pub fn draw_frame(&mut self, dt: f32) {
let window = self.windowed_context.window(); let window = self.windowed_context.window();
let scaling = window.scale_factor();
let new_size = window.inner_size(); let new_size = window.inner_size();
if self.previous_size != new_size { if self.previous_size != new_size {
self.previous_size = new_size; self.previous_size = new_size;
let new_size: LogicalSize<u32> = new_size.to_logical(window.scale_factor());
handle_new_grid_size( handle_new_grid_size(
(new_size.width as u64, new_size.height as u64), new_size.to_logical(scaling),
&self.renderer, &self.renderer,
&self.ui_command_sender, &self.ui_command_sender,
); );
@ -182,9 +183,8 @@ impl GlutinWindowWrapper {
let ui_command_sender = self.ui_command_sender.clone(); let ui_command_sender = self.ui_command_sender.clone();
if REDRAW_SCHEDULER.should_draw() || SETTINGS.get::<WindowSettings>().no_idle { if REDRAW_SCHEDULER.should_draw() || SETTINGS.get::<WindowSettings>().no_idle {
log::debug!("Render Triggered"); log::debug!("Render triggered using scale factor: {}", scaling);
let scaling = 1.0 / self.windowed_context.window().scale_factor();
let renderer = &mut self.renderer; let renderer = &mut self.renderer;
{ {
@ -192,7 +192,7 @@ impl GlutinWindowWrapper {
if renderer.draw_frame(canvas, dt, scaling as f32) { if renderer.draw_frame(canvas, dt, scaling as f32) {
handle_new_grid_size( handle_new_grid_size(
(current_size.width as u64, current_size.height as u64), current_size.to_logical(scaling),
renderer, renderer,
&ui_command_sender, &ui_command_sender,
); );

Loading…
Cancel
Save