refactor: Refactor window module (#849)

- Split platform-dependent code into separate function
- Remove wrapper function `create_window`
- Move handle_new_grid_size where it belongs
macos-click-through
partizan 3 years ago committed by GitHub
parent 21a8adc4a5
commit 257ec70a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -124,41 +124,11 @@ fn main() {
#[cfg(not(test))] #[cfg(not(test))]
init_logger(); init_logger();
#[cfg(target_os = "macos")] #[cfg(target_os = "windows")]
{ windows_fix_dpi();
// incase of app bundle, we can just pass --disowned option straight away to bypass this check
#[cfg(not(debug_assertions))]
if !SETTINGS.get::<CmdLineSettings>().disowned {
if let Ok(curr_exe) = std::env::current_exe() {
assert!(std::process::Command::new(curr_exe)
.args(std::env::args().skip(1))
.arg("--disowned")
.spawn()
.is_ok());
return;
} else {
eprintln!("error in disowning process, cannot obtain the path for the current executable, continuing without disowning...");
}
}
use std::env; #[cfg(target_os = "macos")]
if env::var_os("TERM").is_none() { handle_macos();
let mut profile_path = dirs::home_dir().unwrap();
profile_path.push(".profile");
let shell = env::var("SHELL").unwrap();
let cmd = format!(
"(source /etc/profile && source {} && echo $PATH)",
profile_path.to_str().unwrap()
);
if let Ok(path) = std::process::Command::new(shell)
.arg("-c")
.arg(cmd)
.output()
{
env::set_var("PATH", std::str::from_utf8(&path.stdout).unwrap());
}
}
}
WindowSettings::register(); WindowSettings::register();
RendererSettings::register(); RendererSettings::register();
@ -230,3 +200,48 @@ pub fn init_logger() {
.expect("Could not start logger"); .expect("Could not start logger");
} }
} }
#[cfg(target_os = "windows")]
fn windows_fix_dpi() {
use winapi::shared::windef::DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
use winapi::um::winuser::SetProcessDpiAwarenessContext;
unsafe {
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
}
#[cfg(target_os = "macos")]
fn handle_macos() {
// incase of app bundle, we can just pass --disowned option straight away to bypass this check
#[cfg(not(debug_assertions))]
if !SETTINGS.get::<CmdLineSettings>().disowned {
if let Ok(curr_exe) = std::env::current_exe() {
assert!(std::process::Command::new(curr_exe)
.args(std::env::args().skip(1))
.arg("--disowned")
.spawn()
.is_ok());
std::process::exit(0);
} else {
eprintln!("error in disowning process, cannot obtain the path for the current executable, continuing without disowning...");
}
}
use std::env;
if env::var_os("TERM").is_none() {
let mut profile_path = dirs::home_dir().unwrap();
profile_path.push(".profile");
let shell = env::var("SHELL").unwrap();
let cmd = format!(
"(source /etc/profile && source {} && echo $PATH)",
profile_path.to_str().unwrap()
);
if let Ok(path) = std::process::Command::new(shell)
.arg("-c")
.arg(cmd)
.output()
{
env::set_var("PATH", std::str::from_utf8(&path.stdout).unwrap());
}
}
}

@ -1,56 +1,5 @@
mod settings; mod settings;
mod window_wrapper; mod window_wrapper;
use crate::{
bridge::UiCommand,
channel_utils::*,
editor::{DrawCommand, WindowCommand},
renderer::Renderer,
};
use glutin::dpi::PhysicalSize;
use std::sync::{atomic::AtomicBool, mpsc::Receiver, Arc};
pub use window_wrapper::start_loop;
pub use settings::*; pub use settings::*;
pub use window_wrapper::create_window;
#[cfg(target_os = "windows")]
fn windows_fix_dpi() {
use winapi::shared::windef::DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
use winapi::um::winuser::SetProcessDpiAwarenessContext;
unsafe {
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
}
fn handle_new_grid_size(
new_size: PhysicalSize<u32>,
renderer: &Renderer,
ui_command_sender: &LoggingTx<UiCommand>,
) {
if new_size.width > 0 && new_size.height > 0 {
// Add 1 here to make sure resizing doesn't change the grid size on startup
let width = ((new_size.width + 1) / renderer.font_width as u32) as u32;
let height = ((new_size.height + 1) / renderer.font_height as u32) as u32;
ui_command_sender
.send(UiCommand::Resize { width, height })
.ok();
}
}
pub fn create_window(
batched_draw_command_receiver: Receiver<Vec<DrawCommand>>,
window_command_receiver: Receiver<WindowCommand>,
ui_command_sender: LoggingTx<UiCommand>,
running: Arc<AtomicBool>,
) {
#[cfg(target_os = "windows")]
windows_fix_dpi();
start_loop(
batched_draw_command_receiver,
window_command_receiver,
ui_command_sender,
running,
);
}

@ -35,7 +35,7 @@ impl KeyboardManager {
event: WindowEvent::Focused(_focused), event: WindowEvent::Focused(_focused),
.. ..
} => { } => {
// When window is just focused or lost it's focus, ignore keyboard events // When window is just focused or lost it's focus, ignore keyboard events
// that were submitted this frame // that were submitted this frame
self.ignore_input_this_frame = true; self.ignore_input_this_frame = true;
} }

@ -24,7 +24,7 @@ use log::trace;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
use glutin::platform::unix::WindowBuilderExtUnix; use glutin::platform::unix::WindowBuilderExtUnix;
use super::{handle_new_grid_size, settings::WindowSettings}; use super::settings::WindowSettings;
use crate::{ use crate::{
bridge::UiCommand, bridge::UiCommand,
channel_utils::*, channel_utils::*,
@ -168,12 +168,10 @@ impl GlutinWindowWrapper {
if previous_size != current_size { if previous_size != current_size {
trace!("Updating grid size: {:#?}", current_size); trace!("Updating grid size: {:#?}", current_size);
self.saved_inner_size = current_size; self.saved_inner_size = current_size;
handle_new_grid_size(current_size, &self.renderer, &self.ui_command_sender); self.handle_new_grid_size(current_size);
self.skia_renderer.resize(&self.windowed_context); self.skia_renderer.resize(&self.windowed_context);
} }
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 {
let renderer = &mut self.renderer; let renderer = &mut self.renderer;
@ -181,7 +179,7 @@ impl GlutinWindowWrapper {
let canvas = self.skia_renderer.canvas(); let canvas = self.skia_renderer.canvas();
if renderer.draw_frame(canvas, dt) { if renderer.draw_frame(canvas, dt) {
handle_new_grid_size(current_size, renderer, &ui_command_sender); self.handle_new_grid_size(current_size);
} }
} }
@ -190,12 +188,23 @@ impl GlutinWindowWrapper {
} }
} }
fn handle_new_grid_size(&self, new_size: PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
// Add 1 here to make sure resizing doesn't change the grid size on startup
let width = ((new_size.width + 1) / self.renderer.font_width as u32) as u32;
let height = ((new_size.height + 1) / self.renderer.font_height as u32) as u32;
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) {
self.renderer.handle_scale_factor_update(scale_factor); self.renderer.handle_scale_factor_update(scale_factor);
} }
} }
pub fn start_loop( pub fn create_window(
batched_draw_command_receiver: Receiver<Vec<DrawCommand>>, batched_draw_command_receiver: Receiver<Vec<DrawCommand>>,
window_command_receiver: Receiver<WindowCommand>, window_command_receiver: Receiver<WindowCommand>,
ui_command_sender: LoggingTx<UiCommand>, ui_command_sender: LoggingTx<UiCommand>,

Loading…
Cancel
Save