refactor: Refactor startup process to delay setting window size.

Setting initial window size moved into draw_frame, and called
after fonts are loaded. So initial window size matches requested
geometry.

This also fixes missing startup screen, as UiCommand::Resize is not
called on startup.
macos-click-through
Serg Tereshchenko 3 years ago committed by Keith Simmons
parent d12ab7e33a
commit a8c6e1707b

@ -16,8 +16,8 @@ use crate::windows_utils::{
pub enum UiCommand { pub enum UiCommand {
Quit, Quit,
Resize { Resize {
width: u32, width: u64,
height: u32, height: u64,
}, },
Keyboard(String), Keyboard(String),
MouseButton { MouseButton {

@ -236,7 +236,6 @@ impl Editor {
} }
fn resize_window(&mut self, grid: u64, width: u64, height: u64) { fn resize_window(&mut self, grid: u64, width: u64, height: u64) {
trace!("editor resize {}", grid);
if let Some(window) = self.windows.get_mut(&grid) { if let Some(window) = self.windows.get_mut(&grid) {
window.resize((width, height)); window.resize((width, height));
} else { } else {

@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
use std::sync::Arc; use std::sync::Arc;
use glutin::dpi::PhysicalSize;
use log::{error, trace}; use log::{error, trace};
use skia_safe::{colors, dash_path_effect, BlendMode, Canvas, Color, Paint, Rect, HSV}; use skia_safe::{colors, dash_path_effect, BlendMode, Canvas, Color, Paint, Rect, HSV};
@ -51,6 +52,7 @@ pub struct Renderer {
pub font_height: u64, pub font_height: u64,
pub window_regions: Vec<WindowDrawDetails>, pub window_regions: Vec<WindowDrawDetails>,
pub batched_draw_command_receiver: Receiver<Vec<DrawCommand>>, pub batched_draw_command_receiver: Receiver<Vec<DrawCommand>>,
pub is_ready: bool,
} }
impl Renderer { impl Renderer {
@ -84,6 +86,24 @@ impl Renderer {
font_height, font_height,
window_regions, window_regions,
batched_draw_command_receiver, batched_draw_command_receiver,
is_ready: false,
}
}
// TODO: Refactor code to use these two functions instead of multiplication.
/// Convert PhysicalSize to grid size
pub fn to_grid_size(&self, new_size: PhysicalSize<u32>) -> (u64, u64) {
let width = new_size.width as u64 / self.font_width;
let height = new_size.height as u64 / self.font_height;
(width, height)
}
/// Convert grid size to PhysicalSize
pub fn to_physical_size(&self, new_size: (u64, u64)) -> PhysicalSize<u32> {
let (width, height) = new_size;
PhysicalSize {
width: (width * self.font_width) as u32,
height: (height * self.font_height) as u32,
} }
} }
@ -101,10 +121,11 @@ impl Renderer {
let (font_width, font_height) = self.shaper.font_base_dimensions(); let (font_width, font_height) = self.shaper.font_base_dimensions();
self.font_width = font_width; self.font_width = font_width;
self.font_height = font_height; self.font_height = font_height;
self.is_ready = true;
trace!( trace!(
"Updating font dimensions: {}x{}", "Updating font dimensions: {}x{}",
self.font_height,
self.font_width, self.font_width,
self.font_height,
); );
} }

@ -1,7 +1,5 @@
use crate::renderer::Renderer;
use crate::settings::SETTINGS; use crate::settings::SETTINGS;
use crate::window::WindowSettings; use crate::window::WindowSettings;
use glutin::dpi::PhysicalSize;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::path::PathBuf; use std::path::PathBuf;
@ -36,12 +34,10 @@ pub const DEFAULT_WINDOW_GEOMETRY: WindowGeometry = WindowGeometry {
height: 50, height: 50,
}; };
pub fn maybe_save_window_size(window_size: PhysicalSize<u32>, renderer: &Renderer) { pub fn maybe_save_window_size(grid_size: (u64, u64)) {
let saved_window_size = if SETTINGS.get::<WindowSettings>().remember_window_size { let saved_window_size = if SETTINGS.get::<WindowSettings>().remember_window_size {
WindowGeometry { let (width, height) = grid_size;
width: (window_size.width as f32 / renderer.font_width as f32) as u64, WindowGeometry { width, height }
height: (window_size.height as f32 / renderer.font_height as f32) as u64,
}
} else { } else {
WindowGeometry { WindowGeometry {
width: DEFAULT_WINDOW_GEOMETRY.width as u64, width: DEFAULT_WINDOW_GEOMETRY.width as u64,

@ -52,7 +52,9 @@ pub struct GlutinWindowWrapper {
mouse_manager: MouseManager, mouse_manager: MouseManager,
title: String, title: String,
fullscreen: bool, fullscreen: bool,
first_render_pass: bool,
saved_inner_size: PhysicalSize<u32>, saved_inner_size: PhysicalSize<u32>,
saved_grid_size: (u64, u64),
ui_command_sender: LoggingTx<UiCommand>, ui_command_sender: LoggingTx<UiCommand>,
window_command_receiver: Receiver<WindowCommand>, window_command_receiver: Receiver<WindowCommand>,
} }
@ -162,41 +164,44 @@ 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 current_size = window.inner_size();
let previous_size = self.saved_inner_size;
if previous_size != current_size {
trace!("Updating grid size: {:#?}", current_size);
self.saved_inner_size = current_size;
self.handle_new_grid_size(current_size);
self.skia_renderer.resize(&self.windowed_context);
}
if REDRAW_SCHEDULER.should_draw() || SETTINGS.get::<WindowSettings>().no_idle { if REDRAW_SCHEDULER.should_draw() || SETTINGS.get::<WindowSettings>().no_idle {
let renderer = &mut self.renderer; self.renderer.draw_frame(self.skia_renderer.canvas(), dt);
self.skia_renderer.gr_context.flush(None);
self.windowed_context.swap_buffers().unwrap();
}
{ // Wait until fonts are loaded, so we can set proper window size.
let canvas = self.skia_renderer.canvas(); if !self.renderer.is_ready {
return;
}
if renderer.draw_frame(canvas, dt) { if self.first_render_pass {
self.handle_new_grid_size(current_size); self.first_render_pass = false;
} if !window.is_maximized() {
window.set_inner_size(self.renderer.to_physical_size(self.saved_grid_size));
} }
}
self.skia_renderer.gr_context.flush(None); let new_size = window.inner_size();
self.windowed_context.swap_buffers().unwrap();
if self.saved_inner_size != new_size {
self.saved_inner_size = new_size;
self.handle_new_grid_size(new_size);
self.skia_renderer.resize(&self.windowed_context);
} }
} }
fn handle_new_grid_size(&self, new_size: PhysicalSize<u32>) { fn handle_new_grid_size(&mut self, new_size: PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 { let (width, height) = self.renderer.to_grid_size(new_size);
// Add 1 here to make sure resizing doesn't change the grid size on startup if self.saved_grid_size == (width, height) {
let width = ((new_size.width + 1) / self.renderer.font_width as u32) as u32; trace!("Grid matched saved size, skip update.");
let height = ((new_size.height + 1) / self.renderer.font_height as u32) as u32; return;
self.ui_command_sender
.send(UiCommand::Resize { width, height })
.ok();
} }
self.saved_grid_size = (width, height);
self.ui_command_sender
.send(UiCommand::Resize { width, height })
.ok();
} }
fn handle_scale_factor_update(&mut self, scale_factor: f64) { fn handle_scale_factor_update(&mut self, scale_factor: f64) {
@ -252,14 +257,6 @@ pub fn create_window(
let scale_factor = windowed_context.window().scale_factor(); let scale_factor = windowed_context.window().scale_factor();
let renderer = Renderer::new(batched_draw_command_receiver, scale_factor); let renderer = Renderer::new(batched_draw_command_receiver, scale_factor);
if !window.is_maximized() {
window.set_inner_size(get_initial_window_size((
renderer.font_width,
renderer.font_height,
)));
}
let saved_inner_size = window.inner_size(); let saved_inner_size = window.inner_size();
let skia_renderer = SkiaRenderer::new(&windowed_context); let skia_renderer = SkiaRenderer::new(&windowed_context);
@ -271,6 +268,8 @@ pub fn create_window(
renderer.font_height, renderer.font_height,
); );
let WindowGeometry { width, height } = SETTINGS.get::<CmdLineSettings>().geometry;
let mut window_wrapper = GlutinWindowWrapper { let mut window_wrapper = GlutinWindowWrapper {
windowed_context, windowed_context,
skia_renderer, skia_renderer,
@ -279,7 +278,9 @@ pub fn create_window(
mouse_manager: MouseManager::new(ui_command_sender.clone()), mouse_manager: MouseManager::new(ui_command_sender.clone()),
title: String::from("Neovide"), title: String::from("Neovide"),
fullscreen: false, fullscreen: false,
first_render_pass: true,
saved_inner_size, saved_inner_size,
saved_grid_size: (width, height),
ui_command_sender, ui_command_sender,
window_command_receiver, window_command_receiver,
}; };
@ -288,7 +289,7 @@ pub fn create_window(
event_loop.run(move |e, _window_target, control_flow| { event_loop.run(move |e, _window_target, control_flow| {
if !running.load(Ordering::Relaxed) { if !running.load(Ordering::Relaxed) {
maybe_save_window_size(window_wrapper.saved_inner_size, &window_wrapper.renderer); maybe_save_window_size(window_wrapper.saved_grid_size);
std::process::exit(0); std::process::exit(0);
} }
@ -311,9 +312,3 @@ pub fn create_window(
*control_flow = ControlFlow::WaitUntil(previous_frame_start + frame_duration) *control_flow = ControlFlow::WaitUntil(previous_frame_start + frame_duration)
}); });
} }
fn get_initial_window_size(font_dimesions: (u64, u64)) -> PhysicalSize<u32> {
let WindowGeometry { width, height } = SETTINGS.get::<CmdLineSettings>().geometry;
let (font_width, font_height) = font_dimesions;
PhysicalSize::new((width * font_width) as u32, (height * font_height) as u32)
}

Loading…
Cancel
Save