macos-click-through
Keith Simmons 5 years ago committed by keith
parent 04b0b795f3
commit fef5d56106

@ -1,13 +1,9 @@
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use std::thread::sleep;
use image::{load_from_memory, GenericImageView, Pixel}; use image::{load_from_memory, GenericImageView, Pixel};
use skulpin::{CoordinateSystem, RendererBuilder, PresentMode};
use skulpin::winit::dpi::{LogicalSize, LogicalPosition};
use skulpin::winit::event::{ElementState, Event, MouseScrollDelta, StartCause, WindowEvent, ModifiersState};
use skulpin::winit::event_loop::{ControlFlow, EventLoop};
use skulpin::winit::window::{Icon, WindowBuilder};
use log::{info, debug, trace, error}; use log::{info, debug, trace, error};
use crate::bridge::{construct_keybinding_string, BRIDGE, UiCommand}; use crate::bridge::{construct_keybinding_string, BRIDGE, UiCommand};
@ -31,34 +27,37 @@ fn handle_new_grid_size(new_size: LogicalSize<f32>, renderer: &Renderer) {
} }
pub fn ui_loop() { pub fn ui_loop() {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let (width, height) = INITIAL_DIMENSIONS;
let event_loop = EventLoop::<()>::with_user_event(); let event_loop = EventLoop::<()>::with_user_event();
let mut renderer = Renderer::new(); let mut renderer = Renderer::new();
let (width, height) = INITIAL_DIMENSIONS;
let logical_size = LogicalSize::new( let logical_size = LogicalSize::new(
(width as f32 * renderer.font_width) as f64, (width as f32 * renderer.font_width) as f64,
(height as f32 * renderer.font_height + 1.0) as f64 (height as f32 * renderer.font_height + 1.0) as f64
); );
let icon = { // let icon = {
let icon_data = Asset::get("nvim.ico").expect("Failed to read icon data"); // let icon_data = Asset::get("nvim.ico").expect("Failed to read icon data");
let icon = load_from_memory(&icon_data).expect("Failed to parse icon data"); // let icon = load_from_memory(&icon_data).expect("Failed to parse icon data");
let (width, height) = icon.dimensions(); // let (width, height) = icon.dimensions();
let mut rgba = Vec::with_capacity((width * height) as usize * 4); // let mut rgba = Vec::with_capacity((width * height) as usize * 4);
for (_, _, pixel) in icon.pixels() { // for (_, _, pixel) in icon.pixels() {
rgba.extend_from_slice(&pixel.to_rgba().0); // rgba.extend_from_slice(&pixel.to_rgba().0);
} // }
Icon::from_rgba(rgba, width, height).expect("Failed to create icon object") // Icon::from_rgba(rgba, width, height).expect("Failed to create icon object")
}; // };
info!("icon created"); // info!("icon created");
let mut title = "Neovide".to_string(); let mut window = video_subsystem.window("Neovide", width, height)
let window = Arc::new(WindowBuilder::new() .position_centered()
.with_title(&title) .vulkan()
.with_inner_size(logical_size) .build()
.with_window_icon(Some(icon)) .expect("Failed to create window");
.build(&event_loop)
.expect("Failed to create window"));
info!("window created"); info!("window created");
let mut skulpin_renderer = RendererBuilder::new() let mut skulpin_renderer = RendererBuilder::new()
@ -77,6 +76,36 @@ pub fn ui_loop() {
let mut next_char_modifiers = ModifiersState::empty(); let mut next_char_modifiers = ModifiersState::empty();
info!("Starting window event loop"); info!("Starting window event loop");
let mut event_pump = sdl_context.event_pump()?;
loop {
let frame_start = Instant::now();
let editor_title = { EDITOR.lock().title.clone() };
if title != editor_title {
title = editor_title;
window.set_title(&title);
}
if REDRAW_SCHEDULER.should_draw() || SETTINGS.get("no_idle").read_bool() {
debug!("Render Triggered");
if skulpin_renderer.draw(&window, |canvas, coordinate_system_helper| {
if renderer.draw(canvas, coordinate_system_helper) {
handle_new_grid_size(window.inner_size().to_logical(window.scale_factor()), &renderer)
}
}).is_err() {
error!("Render failed. Closing");
*control_flow = ControlFlow::Exit;
return;
}
}
let elapsed = frame_start.since();
let frame_length = Duration::from_secs_f32(1.0 / 60.0);
if elapsed < frame_length {
sleep(frame_length - elapsed);
}
}
event_loop.run(move |event, _window_target, control_flow| { event_loop.run(move |event, _window_target, control_flow| {
trace!("Window Event: {:?}", event); trace!("Window Event: {:?}", event);
match event { match event {
@ -206,26 +235,6 @@ pub fn ui_loop() {
} }
Event::RedrawRequested { .. } => { Event::RedrawRequested { .. } => {
let frame_start = Instant::now();
let editor_title = { EDITOR.lock().title.clone() };
if title != editor_title {
title = editor_title;
window.set_title(&title);
}
if REDRAW_SCHEDULER.should_draw() || SETTINGS.get("no_idle").read_bool() {
debug!("Render Triggered");
if skulpin_renderer.draw(&window, |canvas, coordinate_system_helper| {
if renderer.draw(canvas, coordinate_system_helper) {
handle_new_grid_size(window.inner_size().to_logical(window.scale_factor()), &renderer)
}
}).is_err() {
error!("Render failed. Closing");
*control_flow = ControlFlow::Exit;
return;
}
}
*control_flow = ControlFlow::WaitUntil(frame_start + Duration::from_secs_f32(1.0 / 60.0)); *control_flow = ControlFlow::WaitUntil(frame_start + Duration::from_secs_f32(1.0 / 60.0));
}, },

Loading…
Cancel
Save