Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Nov 12, 2024
1 parent 06c2dc5 commit efbe3d2
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 191 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ raw-window-handle = "0.6.0"
wgpu = { version = "22.0.0" }
parking_lot = { version = "0.12.1" }
swash = { version = "0.1.17" }
winit = { git = "https://github.com/rust-windowing/winit", rev = "ae4c449670674d8ac0d6d8754caf3fe5f4954c25" }
muda = { version = "0.15.3" }
winit = { git = "https://github.com/rust-windowing/winit", rev = "3a60cbaba5fd96d9244a1f316ae562d7032b2b98" }

[dependencies]
slotmap = "1.0.7"
Expand Down Expand Up @@ -68,6 +69,7 @@ parking_lot = { workspace = true }
image = { workspace = true }
im = { workspace = true }
wgpu = { workspace = true }
muda = { workspace = true }
winit = { workspace = true }
futures = { version = "0.3.30", optional = true }
crossbeam = "0.8"
Expand Down
80 changes: 25 additions & 55 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, rc::Rc, sync::Arc};

use crossbeam_channel::{Receiver, Sender};
use floem_reactive::WriteSignal;
use floem_reactive::{Trigger, WriteSignal};
use parking_lot::Mutex;
use raw_window_handle::HasDisplayHandle;
use winit::{
Expand Down Expand Up @@ -53,27 +53,27 @@ pub enum AppEvent {
}

pub(crate) enum UserEvent {
AppUpdate,
Idle,
AppUpdate(AppUpdateEvent),
Idle(Trigger),
QuitApp,
GpuResourcesUpdate { window_id: WindowId },
}

pub(crate) enum AppUpdateEvent {
NewWindow {
view_fn: Box<dyn FnOnce(WindowId) -> Box<dyn View>>,
view_fn: Box<dyn FnOnce(WindowId) -> Box<dyn View> + Send + Sync>,
config: Option<WindowConfig>,
},
CloseWindow {
window_id: WindowId,
},
CaptureWindow {
window_id: WindowId,
capture: WriteSignal<Option<Rc<Capture>>>,
capture: WriteSignal<Option<Arc<Capture>>>,
},
ProfileWindow {
window_id: WindowId,
end_profile: Option<WriteSignal<Option<Rc<Profile>>>>,
end_profile: Option<WriteSignal<Option<Arc<Profile>>>>,
},
RequestTimer {
timer: Timer,
Expand All @@ -89,12 +89,7 @@ pub(crate) enum AppUpdateEvent {
}

pub(crate) fn add_app_update_event(event: AppUpdateEvent) {
APP_UPDATE_EVENTS.with(|events| {
events.borrow_mut().push(event);
});
Application::with_event_loop_proxy(|proxy| {
proxy.wake_up();
});
Application::send_proxy_event(UserEvent::AppUpdate(event));
}

/// Floem top level application
Expand All @@ -115,10 +110,12 @@ impl Default for Application {

impl ApplicationHandler for Application {
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
println!("can create surfaces");
while let Some((view_fn, window_config)) = self.initial_windows.pop() {
self.handle
.new_window(event_loop, view_fn, window_config.unwrap_or_default());
}
println!("window creation done");
}

fn window_event(
Expand All @@ -127,21 +124,30 @@ impl ApplicationHandler for Application {
window_id: WindowId,
event: WindowEvent,
) {
println!("window event {event:?}");
self.handle.handle_timer(event_loop);
self.handle
.handle_window_event(window_id, event, event_loop);
}

fn proxy_wake_up(&mut self, event_loop: &dyn ActiveEventLoop) {
println!("proxy wake up");
self.handle.handle_timer(event_loop);
for event in self.receiver.try_iter() {
self.handle.handle_user_event(event_loop, event);
}
self.handle.handle_updates_for_all_windows();
}

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

fn about_to_wait(&mut self, event_loop: &dyn ActiveEventLoop) {
self.handle.handle_timer(event_loop);
}
}

impl Application {
Expand Down Expand Up @@ -189,60 +195,24 @@ impl Application {

pub fn run(mut self) {
let event_loop = self.event_loop.take().unwrap();
println!("now run app");
event_loop.run_app(self);
}

pub fn old_run(mut self) {
let mut handle = self.handle.take().unwrap();
handle.idle();
let event_loop_proxy = self.event_loop.create_proxy();
let _ = self.event_loop.run(|event, event_loop| {
event_loop.set_control_flow(ControlFlow::Wait);
handle.handle_timer(event_loop);

match event {
floem_winit::event::Event::NewEvents(_) => {}
floem_winit::event::Event::WindowEvent { window_id, event } => {
handle.handle_window_event(window_id, event, event_loop);
}
floem_winit::event::Event::DeviceEvent { .. } => {}
floem_winit::event::Event::UserEvent(event) => {
handle.handle_user_event(event_loop, event_loop_proxy.clone(), event);
}
floem_winit::event::Event::Suspended => {}
floem_winit::event::Event::Resumed => {}
floem_winit::event::Event::AboutToWait => {}
floem_winit::event::Event::LoopExiting => {
if let Some(action) = self.event_listener.as_ref() {
action(AppEvent::WillTerminate);
}
}
floem_winit::event::Event::MemoryWarning => {}
floem_winit::event::Event::Reopen => {}
}
});
}

pub(crate) fn with_event_loop_proxy(f: impl FnOnce(&EventLoopProxy)) {
if let Some(proxy) = EVENT_LOOP_PROXY.lock().as_ref() {
f(proxy);
}
}

pub(crate) fn send_proxy_event(event: UserEvent) {
if let Some((proxy, sender)) = EVENT_LOOP_PROXY.lock().as_ref() {
let _ = sender.send(event);
proxy.wake_up();
}
}

pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
self.event_loop.as_ref().unwrap().available_monitors()
}
// pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
// self.event_loop.as_ref().unwrap().available_monitors()
// }

pub fn primary_monitor(&self) -> Option<MonitorHandle> {
self.event_loop.as_ref().unwrap().primary_monitor()
}
// pub fn primary_monitor(&self) -> Option<MonitorHandle> {
// self.event_loop.as_ref().unwrap().primary_monitor()
// }
}

/// Initiates the application shutdown process.
Expand Down
128 changes: 63 additions & 65 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use web_time::Instant;
#[cfg(target_arch = "wasm32")]
use wgpu::web_sys;

use floem_reactive::SignalUpdate;
use floem_reactive::{SignalUpdate, Trigger};
use peniko::kurbo::{Point, Size};
use std::{collections::HashMap, mem, rc::Rc};
use std::{collections::HashMap, mem, rc::Rc, sync::Arc};
use winit::{
dpi::{LogicalPosition, LogicalSize},
event::WindowEvent,
Expand Down Expand Up @@ -43,11 +43,11 @@ impl ApplicationHandle {

pub(crate) fn handle_user_event(&mut self, event_loop: &dyn ActiveEventLoop, event: UserEvent) {
match event {
UserEvent::AppUpdate => {
self.handle_update_event(event_loop);
UserEvent::AppUpdate(event) => {
self.handle_update_event(event_loop, event);
}
UserEvent::Idle => {
self.idle();
UserEvent::Idle(trigger) => {
self.idle(trigger);
}
UserEvent::QuitApp => {
event_loop.exit();
Expand All @@ -61,55 +61,53 @@ impl ApplicationHandle {
}
}

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, view_fn, config.unwrap_or_default())
}
AppUpdateEvent::CloseWindow { window_id } => {
self.close_window(window_id, event_loop);
}
AppUpdateEvent::RequestTimer { timer } => {
self.request_timer(timer, event_loop);
}
AppUpdateEvent::CancelTimer { timer } => {
self.remove_timer(&timer);
}
AppUpdateEvent::CaptureWindow { window_id, capture } => {
capture.set(self.capture_window(window_id).map(Rc::new));
}
AppUpdateEvent::ProfileWindow {
window_id,
end_profile,
} => {
let handle = self.window_handles.get_mut(&window_id);
if let Some(handle) = handle {
if let Some(profile) = end_profile {
profile.set(handle.profile.take().map(|mut profile| {
profile.next_frame();
Rc::new(profile)
}));
} else {
handle.profile = Some(Profile::default());
}
pub(crate) fn handle_update_event(
&mut self,
event_loop: &dyn ActiveEventLoop,
event: AppUpdateEvent,
) {
match event {
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);
}
AppUpdateEvent::RequestTimer { timer } => {
self.request_timer(timer, event_loop);
}
AppUpdateEvent::CancelTimer { timer } => {
self.remove_timer(&timer);
}
AppUpdateEvent::CaptureWindow { window_id, capture } => {
capture.set(self.capture_window(window_id).map(Arc::new));
}
AppUpdateEvent::ProfileWindow {
window_id,
end_profile,
} => {
let handle = self.window_handles.get_mut(&window_id);
if let Some(handle) = handle {
if let Some(profile) = end_profile {
profile.set(handle.profile.take().map(|mut profile| {
profile.next_frame();
Arc::new(profile)
}));
} else {
handle.profile = Some(Profile::default());
}
}
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
AppUpdateEvent::MenuAction {
window_id,
action_id,
} => {
let window_handle = match self.window_handles.get_mut(&window_id) {
Some(window_handle) => window_handle,
None => return,
};
window_handle.menu_action(action_id);
}
}
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
AppUpdateEvent::MenuAction {
window_id,
action_id,
} => {
let window_handle = match self.window_handles.get_mut(&window_id) {
Some(window_handle) => window_handle,
None => return,
};
window_handle.menu_action(action_id);
}
}
}
Expand Down Expand Up @@ -228,9 +226,13 @@ impl ApplicationHandle {
WindowEvent::Occluded(_) => {}
WindowEvent::RedrawRequested => {
window_handle.render_frame();
} // WindowEvent::MenuAction(id) => {
// window_handle.menu_action(id);
// }
}
WindowEvent::PinchGesture { .. } => {}
WindowEvent::PanGesture { .. } => {}
WindowEvent::DoubleTapGesture { .. } => {}
WindowEvent::RotationGesture { .. } => {} // WindowEvent::MenuAction(id) => {
// window_handle.menu_action(id);
// }
}

if let Some((name, start, new_frame)) = start {
Expand Down Expand Up @@ -275,6 +277,7 @@ impl ApplicationHandle {
font_embolden,
}: WindowConfig,
) {
println!("new window");
let logical_size = size.map(|size| LogicalSize::new(size.width, size.height));

let mut window_attributes = winit::window::WindowAttributes::default()
Expand Down Expand Up @@ -394,6 +397,7 @@ impl ApplicationHandle {
}
}

println!("window attributes {window_attributes:?}");
let Ok(window) = event_loop.create_window(window_attributes) else {
return;
};
Expand Down Expand Up @@ -432,17 +436,11 @@ impl ApplicationHandle {
.map(|handle| handle.capture())
}

pub(crate) fn idle(&mut self) {
let ext_events = { mem::take(&mut *EXT_EVENT_HANDLER.queue.lock()) };

for trigger in ext_events {
trigger.notify();
}

self.handle_updates_for_all_windows();
pub(crate) fn idle(&mut self, trigger: Trigger) {
trigger.notify();
}

fn handle_updates_for_all_windows(&mut self) {
pub(crate) fn handle_updates_for_all_windows(&mut self) {
for (window_id, handle) in self.window_handles.iter_mut() {
handle.process_update();
while process_window_updates(window_id) {}
Expand Down
9 changes: 1 addition & 8 deletions src/ext_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ impl ExtEventHandler {
}

pub fn add_trigger(&self, trigger: Trigger) {
{
// Run this in a short block to prevent any deadlock if running the trigger effects
// causes another trigger to be registered
EXT_EVENT_HANDLER.queue.lock().push_back(trigger);
}
Application::with_event_loop_proxy(|proxy| {
let _ = proxy.send_event(UserEvent::Idle);
});
Application::send_proxy_event(UserEvent::Idle(trigger));
}
}

Expand Down
Loading

0 comments on commit efbe3d2

Please sign in to comment.