Introduce on_request_frame

This commit is contained in:
Antonio Scandurra 2024-01-09 18:00:48 +01:00
parent 3bb29acd26
commit 0c6d107740
6 changed files with 37 additions and 42 deletions

View file

@ -1,6 +1,6 @@
use super::{display_bounds_from_native, ns_string, MacDisplay, MetalRenderer, NSRange};
use crate::{
display_bounds_to_native, point, px, size, AnyWindowHandle, Bounds, DrawWindow, ExternalPaths,
display_bounds_to_native, point, px, size, AnyWindowHandle, Bounds, ExternalPaths,
FileDropEvent, ForegroundExecutor, GlobalPixels, InputEvent, KeyDownEvent, Keystroke,
Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler, PlatformWindow, Point,
@ -46,7 +46,6 @@ use std::{
sync::{Arc, Weak},
time::Duration,
};
use util::ResultExt;
const WINDOW_STATE_IVAR: &str = "windowState";
@ -317,8 +316,8 @@ struct MacWindowState {
executor: ForegroundExecutor,
native_window: id,
renderer: MetalRenderer,
draw: Option<DrawWindow>,
kind: WindowKind,
request_frame_callback: Option<Box<dyn FnMut()>>,
event_callback: Option<Box<dyn FnMut(InputEvent) -> bool>>,
activate_callback: Option<Box<dyn FnMut(bool)>>,
resize_callback: Option<Box<dyn FnMut(Size<Pixels>, f32)>>,
@ -453,7 +452,6 @@ impl MacWindow {
pub fn open(
handle: AnyWindowHandle,
options: WindowOptions,
draw: DrawWindow,
executor: ForegroundExecutor,
) -> Self {
unsafe {
@ -545,8 +543,8 @@ impl MacWindow {
executor,
native_window,
renderer: MetalRenderer::new(true),
draw: Some(draw),
kind: options.kind,
request_frame_callback: None,
event_callback: None,
activate_callback: None,
resize_callback: None,
@ -926,6 +924,10 @@ impl PlatformWindow for MacWindow {
.detach();
}
fn on_request_frame(&self, callback: Box<dyn FnMut()>) {
self.0.as_ref().lock().request_frame_callback = Some(callback);
}
fn on_input(&self, callback: Box<dyn FnMut(InputEvent) -> bool>) {
self.0.as_ref().lock().event_callback = Some(callback);
}
@ -990,6 +992,11 @@ impl PlatformWindow for MacWindow {
}
}
fn draw(&self, scene: &crate::Scene) {
let mut this = self.0.lock();
this.renderer.draw(scene);
}
fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> {
self.0.lock().renderer.sprite_atlas().clone()
}
@ -1462,15 +1469,12 @@ extern "C" fn set_frame_size(this: &Object, _: Sel, size: NSSize) {
}
extern "C" fn display_layer(this: &Object, _: Sel, _: id) {
unsafe {
let window_state = get_window_state(this);
let mut draw = window_state.lock().draw.take().unwrap();
let scene = draw().log_err();
let mut window_state = window_state.lock();
window_state.draw = Some(draw);
if let Some(scene) = scene {
window_state.renderer.draw(&scene);
}
let window_state = unsafe { get_window_state(this) };
let mut lock = window_state.lock();
if let Some(mut callback) = lock.request_frame_callback.take() {
drop(lock);
callback();
window_state.lock().request_frame_callback = Some(callback);
}
}