Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Nov 11, 2024
1 parent 04300c9 commit c426ece
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 122 deletions.
61 changes: 26 additions & 35 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
profiler::Profile,
view::{IntoView, View},
window::WindowConfig,
AnyView,
};

type AppEventCallback = dyn Fn(AppEvent);
Expand Down Expand Up @@ -52,32 +53,10 @@ pub enum AppEvent {
}

pub(crate) enum UserEvent {
AppUpdate,
Idle,
QuitApp,
NewWindow {
view_fn: Box<dyn FnOnce(WindowId) -> Box<dyn View> + Sync + Send>,
config: Option<WindowConfig>,
},
CloseWindow {
window_id: WindowId,
},
CaptureWindow {
window_id: WindowId,
capture: WriteSignal<Option<Arc<Capture>>>,
},
ProfileWindow {
window_id: WindowId,
end_profile: Option<WriteSignal<Option<Arc<Profile>>>>,
},
RequestTimer {
timer: Timer,
},
CancelTimer {
timer: TimerToken,
},
GpuResourcesUpdate {
window_id: WindowId,
},
GpuResourcesUpdate { window_id: WindowId },
}

pub(crate) enum AppUpdateEvent {
Expand Down Expand Up @@ -122,9 +101,10 @@ pub(crate) fn add_app_update_event(event: AppUpdateEvent) {
/// This is the entry point of the application.
pub struct Application {
receiver: Receiver<UserEvent>,
handle: Option<ApplicationHandle>,
handle: ApplicationHandle,
event_listener: Option<Box<AppEventCallback>>,
event_loop: Option<EventLoop>,
initial_windows: Vec<(Box<dyn FnOnce(WindowId) -> AnyView>, Option<WindowConfig>)>,
}

impl Default for Application {
Expand All @@ -135,7 +115,10 @@ impl Default for Application {

impl ApplicationHandler for Application {
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
todo!()
while let Some((view_fn, window_config)) = self.initial_windows.pop() {
self.handle
.new_window(event_loop, view_fn, window_config.unwrap_or_default());
}
}

fn window_event(
Expand All @@ -144,12 +127,20 @@ impl ApplicationHandler for Application {
window_id: WindowId,
event: WindowEvent,
) {
let handle = self.handle.as_mut().unwrap();
handle.handle_window_event(window_id, event, event_loop);
self.handle
.handle_window_event(window_id, event, event_loop);
}

fn proxy_wake_up(&mut self, event_loop: &dyn ActiveEventLoop) {
for event in self.receiver.try_iter() {}
for event in self.receiver.try_iter() {
self.handle.handle_user_event(event_loop, event);
}
}

fn exiting(&mut self, _event_loop: &dyn ActiveEventLoop) {
if let Some(action) = self.event_listener.as_ref() {
action(AppEvent::WillTerminate);
}
}
}

Expand All @@ -165,9 +156,10 @@ impl Application {
let handle = ApplicationHandle::new();
Self {
receiver,
handle: Some(handle),
handle,
event_listener: None,
event_loop: Some(event_loop),
initial_windows: Vec::new(),
}
}

Expand All @@ -188,11 +180,10 @@ impl Application {
app_view: impl FnOnce(WindowId) -> V + 'static,
config: Option<WindowConfig>,
) -> Self {
self.handle.as_mut().unwrap().new_window(
self.event_loop.as_ref().unwrap(),
Box::new(|window_id| app_view(window_id).into_any()),
config.unwrap_or_default(),
);
self.initial_windows.push((
Box::new(move |window_id: WindowId| app_view(window_id).into_any()),
config,
));
self
}

Expand Down
71 changes: 26 additions & 45 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{collections::HashMap, mem, rc::Rc};
use winit::{
dpi::{LogicalPosition, LogicalSize},
event::WindowEvent,
event_loop::{ActiveEventLoop, ControlFlow, EventLoopProxy},
event_loop::{ActiveEventLoop, ControlFlow},
window::WindowId,
};

Expand Down Expand Up @@ -41,15 +41,10 @@ impl ApplicationHandle {
}
}

pub(crate) fn handle_user_event(
&mut self,
event_loop: &ActiveEventLoop,
event_proxy: EventLoopProxy<UserEvent>,
event: UserEvent,
) {
pub(crate) fn handle_user_event(&mut self, event_loop: &dyn ActiveEventLoop, event: UserEvent) {
match event {
UserEvent::AppUpdate => {
self.handle_update_event(event_loop, event_proxy);
self.handle_update_event(event_loop);
}
UserEvent::Idle => {
self.idle();
Expand All @@ -66,23 +61,16 @@ impl ApplicationHandle {
}
}

pub(crate) fn handle_update_event(
&mut self,
event_loop: &EventLoopWindowTarget<UserEvent>,
event_proxy: EventLoopProxy<UserEvent>,
) {
pub(crate) fn handle_update_event(&mut self, event_loop: &dyn ActiveEventLoop) {
let events = APP_UPDATE_EVENTS.with(|events| {
let mut events = events.borrow_mut();
std::mem::take(&mut *events)
});
for event in events {
match event {
AppUpdateEvent::NewWindow { view_fn, config } => self.new_window(
event_loop,
event_proxy.clone(),
view_fn,
config.unwrap_or_default(),
),
AppUpdateEvent::NewWindow { view_fn, config } => {
self.new_window(event_loop, view_fn, config.unwrap_or_default())
}
AppUpdateEvent::CloseWindow { window_id } => {
self.close_window(window_id, event_loop);
}
Expand Down Expand Up @@ -140,7 +128,7 @@ impl ApplicationHandle {
let start = window_handle.profile.is_some().then(|| {
let name = match event {
WindowEvent::ActivationTokenDone { .. } => "ActivationTokenDone",
WindowEvent::Resized(..) => "Resized",
WindowEvent::SurfaceResized(..) => "Resized",
WindowEvent::Moved(..) => "Moved",
WindowEvent::CloseRequested => "CloseRequested",
WindowEvent::Destroyed => "Destroyed",
Expand All @@ -151,22 +139,21 @@ impl ApplicationHandle {
WindowEvent::KeyboardInput { .. } => "KeyboardInput",
WindowEvent::ModifiersChanged(..) => "ModifiersChanged",
WindowEvent::Ime(..) => "Ime",
WindowEvent::CursorMoved { .. } => "CursorMoved",
WindowEvent::CursorEntered { .. } => "CursorEntered",
WindowEvent::CursorLeft { .. } => "CursorLeft",
WindowEvent::PointerMoved { .. } => "PointerMoved",
WindowEvent::PointerEntered { .. } => "PointerEntered",
WindowEvent::PointerLeft { .. } => "PointerLeft",
WindowEvent::MouseWheel { .. } => "MouseWheel",
WindowEvent::MouseInput { .. } => "MouseInput",
WindowEvent::TouchpadMagnify { .. } => "TouchpadMagnify",
WindowEvent::SmartMagnify { .. } => "SmartMagnify",
WindowEvent::TouchpadRotate { .. } => "TouchpadRotate",
WindowEvent::PointerButton { .. } => "PointerButton",
WindowEvent::TouchpadPressure { .. } => "TouchpadPressure",
WindowEvent::AxisMotion { .. } => "AxisMotion",
WindowEvent::Touch(_) => "Touch",
WindowEvent::ScaleFactorChanged { .. } => "ScaleFactorChanged",
WindowEvent::ThemeChanged(..) => "ThemeChanged",
WindowEvent::Occluded(..) => "Occluded",
WindowEvent::MenuAction(..) => "MenuAction",
WindowEvent::RedrawRequested => "RedrawRequested",
WindowEvent::PinchGesture { .. } => "PinchGesture",
WindowEvent::PanGesture { .. } => "PanGesture",
WindowEvent::DoubleTapGesture { .. } => "DoubleTapGesture",
WindowEvent::RotationGesture { .. } => "RotationGesture",
// WindowEvent::MenuAction(..) => "MenuAction",
};
(
name,
Expand All @@ -177,7 +164,7 @@ impl ApplicationHandle {

match event {
WindowEvent::ActivationTokenDone { .. } => {}
WindowEvent::Resized(size) => {
WindowEvent::SurfaceResized(size) => {
let size: LogicalSize<f64> = size.to_logical(window_handle.scale);
let size = Size::new(size.width, size.height);
window_handle.size(size);
Expand Down Expand Up @@ -216,40 +203,34 @@ impl ApplicationHandle {
WindowEvent::Ime(ime) => {
window_handle.ime(ime);
}
WindowEvent::CursorMoved { position, .. } => {
WindowEvent::PointerMoved { position, .. } => {
let position: LogicalPosition<f64> = position.to_logical(window_handle.scale);
let point = Point::new(position.x, position.y);
window_handle.pointer_move(point);
}
WindowEvent::CursorEntered { .. } => {}
WindowEvent::CursorLeft { .. } => {
WindowEvent::PointerEntered { .. } => {}
WindowEvent::PointerLeft { .. } => {
window_handle.pointer_leave();
}
WindowEvent::MouseWheel { delta, .. } => {
window_handle.mouse_wheel(delta);
}
WindowEvent::MouseInput { state, button, .. } => {
window_handle.mouse_input(button, state);
WindowEvent::PointerButton { state, button, .. } => {
window_handle.pointer_button(button, state);
}
WindowEvent::TouchpadMagnify { .. } => {}
WindowEvent::SmartMagnify { .. } => {}
WindowEvent::TouchpadRotate { .. } => {}
WindowEvent::TouchpadPressure { .. } => {}
WindowEvent::AxisMotion { .. } => {}
WindowEvent::Touch(_) => {}
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
window_handle.scale(scale_factor);
}
WindowEvent::ThemeChanged(theme) => {
window_handle.os_theme_changed(theme);
}
WindowEvent::Occluded(_) => {}
WindowEvent::MenuAction(id) => {
window_handle.menu_action(id);
}
WindowEvent::RedrawRequested => {
window_handle.render_frame();
}
} // WindowEvent::MenuAction(id) => {
// window_handle.menu_action(id);
// }
}

if let Some((name, start, new_frame)) = start {
Expand Down
Loading

0 comments on commit c426ece

Please sign in to comment.