Dispatch keystrokes and fix rerendering when window invalidated

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Nathan Sobo
2021-03-21 11:38:23 -06:00
co-authored by Antonio Scandurra
parent f5df3681f8
commit 046fe3fff9
9 changed files with 55 additions and 27 deletions
+1
View File
@@ -97,6 +97,7 @@ impl Renderer {
for quad_batch in layer.quads().chunks(batch_size) {
for (ix, quad) in quad_batch.iter().enumerate() {
let bounds = quad.bounds * scene.scale_factor();
log::info!("render quad {:?}", quad);
let shader_quad = shaders::GPUIQuad {
origin: bounds.origin().to_float2(),
size: bounds.size().to_float2(),
+23 -18
View File
@@ -2,7 +2,6 @@ use crate::{
executor,
geometry::vector::Vector2F,
platform::{self, Event, WindowContext},
util::post_inc,
Scene,
};
use anyhow::{anyhow, Result};
@@ -12,7 +11,7 @@ use cocoa::{
NSWindow, NSWindowStyleMask,
},
base::{id, nil},
foundation::{NSAutoreleasePool, NSSize, NSString},
foundation::{NSAutoreleasePool, NSInteger, NSSize, NSString},
quartzcore::AutoresizingMask,
};
use ctor::ctor;
@@ -42,6 +41,9 @@ const WINDOW_STATE_IVAR: &'static str = "windowState";
static mut WINDOW_CLASS: *const Class = ptr::null();
static mut VIEW_CLASS: *const Class = ptr::null();
#[allow(non_upper_case_globals)]
const NSViewLayerContentsRedrawDuringViewResize: NSInteger = 2;
#[ctor]
unsafe fn build_classes() {
WINDOW_CLASS = {
@@ -117,7 +119,7 @@ pub struct Window(Rc<RefCell<WindowState>>);
struct WindowState {
native_window: id,
event_callback: Option<Box<dyn FnMut(Event, &mut dyn platform::WindowContext)>>,
event_callback: Option<Box<dyn FnMut(Event)>>,
resize_callback: Option<Box<dyn FnMut(&mut dyn platform::WindowContext)>>,
synthetic_drag_counter: usize,
executor: Rc<executor::Foreground>,
@@ -219,10 +221,10 @@ impl Window {
// on we explicitly make the view layer-backed up front so that AppKit doesn't do it
// itself and break the association with its context.
native_view.setWantsLayer(YES);
native_view.layer().setBackgroundColor_(
msg_send![class!(NSColor), colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0],
);
let _: () = msg_send![
native_view,
setLayerContentsRedrawPolicy: NSViewLayerContentsRedrawDuringViewResize
];
native_window.setContentView_(native_view.autorelease());
native_window.makeFirstResponder_(native_view);
@@ -252,7 +254,7 @@ impl Drop for Window {
}
impl platform::Window for Window {
fn on_event(&mut self, callback: Box<dyn FnMut(Event, &mut dyn platform::WindowContext)>) {
fn on_event(&mut self, callback: Box<dyn FnMut(Event)>) {
self.0.as_ref().borrow_mut().event_callback = Some(callback);
}
@@ -290,6 +292,7 @@ impl platform::WindowContext for WindowState {
}
fn present_scene(&mut self, scene: Scene) {
log::info!("present scene");
self.scene_to_render = Some(scene);
unsafe {
let _: () = msg_send![self.native_window.contentView(), setNeedsDisplay: YES];
@@ -331,32 +334,33 @@ extern "C" fn dealloc_view(this: &Object, _: Sel) {
extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) {
let window_state = unsafe { get_window_state(this) };
let weak_window_state = Rc::downgrade(&window_state);
let mut window_state = window_state.as_ref().borrow_mut();
let mut window_state_borrow = window_state.as_ref().borrow_mut();
let event = unsafe { Event::from_native(native_event, Some(window_state.size().y())) };
let event = unsafe { Event::from_native(native_event, Some(window_state_borrow.size().y())) };
if let Some(event) = event {
match event {
Event::LeftMouseDragged { position } => {
window_state.synthetic_drag_counter += 1;
window_state
window_state_borrow.synthetic_drag_counter += 1;
window_state_borrow
.executor
.spawn(synthetic_drag(
weak_window_state,
window_state.synthetic_drag_counter,
window_state_borrow.synthetic_drag_counter,
position,
))
.detach();
}
Event::LeftMouseUp { .. } => {
window_state.synthetic_drag_counter += 1;
window_state_borrow.synthetic_drag_counter += 1;
}
_ => {}
}
if let Some(mut callback) = window_state.event_callback.take() {
callback(event, &mut *window_state);
window_state.event_callback = Some(callback);
if let Some(mut callback) = window_state_borrow.event_callback.take() {
drop(window_state_borrow);
callback(event);
window_state.borrow_mut().event_callback = Some(callback);
}
}
}
@@ -417,6 +421,7 @@ extern "C" fn set_frame_size(this: &Object, _: Sel, size: NSSize) {
}
extern "C" fn display_layer(this: &Object, _: Sel, _: id) {
log::info!("display layer");
unsafe {
let window_state = get_window_state(this);
let mut window_state = window_state.as_ref().borrow_mut();
@@ -470,7 +475,7 @@ async fn synthetic_drag(
let mut window_state = window_state.borrow_mut();
if window_state.synthetic_drag_counter == drag_id {
if let Some(mut callback) = window_state.event_callback.take() {
callback(Event::LeftMouseDragged { position }, &mut *window_state);
callback(Event::LeftMouseDragged { position });
window_state.event_callback = Some(callback);
}
} else {
+1 -1
View File
@@ -41,7 +41,7 @@ pub trait Dispatcher: Send + Sync {
}
pub trait Window: WindowContext {
fn on_event(&mut self, callback: Box<dyn FnMut(Event, &mut dyn WindowContext)>);
fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn WindowContext)>);
}