Skip to content

Commit

Permalink
Replace tao with winit (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz authored Jun 20, 2024
1 parent fdd9a59 commit bb6eb9d
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 156 deletions.
2 changes: 1 addition & 1 deletion packages/dioxus-blitz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tao = { version = "0.26.1", features = ["serde"] }
winit = "0.30.2"
muda = { version = "0.11.5", features = ["serde"] }
tokio = { workspace = true, features = ["full"] }
dioxus = { workspace = true }
Expand Down
166 changes: 85 additions & 81 deletions packages/dioxus-blitz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use dioxus::prelude::*;
use documents::DioxusDocument;
use muda::{MenuEvent, MenuId};
use std::collections::HashMap;
use tao::event_loop::EventLoopBuilder;
use tao::window::WindowId;
use tao::{
use url::Url;
use winit::event_loop::{EventLoop, EventLoopBuilder};
use winit::window::WindowId;
use winit::{
event::{Event, WindowEvent},
event_loop::ControlFlow,
};
use url::Url;

#[derive(Default)]
pub struct Config {
Expand Down Expand Up @@ -93,7 +93,9 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
let _guard = rt.enter();

// Build an event loop for the application
let event_loop = EventLoopBuilder::<UserWindowEvent>::with_user_event().build();
let event_loop = EventLoop::<UserWindowEvent>::with_user_event()
.build()
.unwrap();
let proxy = event_loop.create_proxy();

// Multiwindow ftw
Expand All @@ -106,94 +108,96 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let mut initial = true;

event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;
event_loop
.run(move |event, event_loop| {
event_loop.set_control_flow(ControlFlow::Wait);

let mut on_resume = || {
for (_, view) in windows.iter_mut() {
view.resume(event_loop, &proxy, &rt);
}
let mut on_resume = || {
for (_, view) in windows.iter_mut() {
view.resume(event_loop, &proxy, &rt);
}

for view in pending_windows.iter_mut() {
view.resume(event_loop, &proxy, &rt);
}
for view in pending_windows.iter_mut() {
view.resume(event_loop, &proxy, &rt);
}

for window in pending_windows.drain(..) {
let RenderState::Active(state) = &window.renderer.render_state else {
continue;
};
windows.insert(state.window.id(), window);
}
};

#[cfg(not(any(target_os = "android", target_os = "ios")))]
if initial {
on_resume();
initial = false;
}

match event {
// Exit the app when close is request
// Not always necessary
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,

// Nothing else to do, try redrawing?
Event::MainEventsCleared => {}

Event::UserEvent(UserWindowEvent(EventData::Poll, id)) => {
if let Some(view) = windows.get_mut(&id) {
if view.poll() {
view.request_redraw();
}
};
}
// Event::UserEvent(_redraw) => {
// for (_, view) in windows.iter() {
// view.request_redraw();
// }
// }
Event::NewEvents(_) => {
for id in windows.keys() {
_ = proxy.send_event(UserWindowEvent(EventData::Poll, *id));
for window in pending_windows.drain(..) {
let RenderState::Active(state) = &window.renderer.render_state else {
continue;
};
windows.insert(state.window.id(), window);
}
}
};

Event::RedrawRequested(window_id) => {
if let Some(window) = windows.get_mut(&window_id) {
window.renderer.dom.as_mut().resolve();
window.renderer.render(&mut window.scene);
};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if initial {
on_resume();
initial = false;
}

Event::Suspended => {
for (_, view) in windows.iter_mut() {
view.suspend();
match event {
// Exit the app when close is request
// Not always necessary
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => event_loop.exit(),

Event::WindowEvent {
window_id,
event: winit::event::WindowEvent::RedrawRequested,
} => {
if let Some(window) = windows.get_mut(&window_id) {
window.renderer.dom.as_mut().resolve();
window.renderer.render(&mut window.scene);
};
}
}

Event::Resumed => on_resume(),
Event::UserEvent(UserWindowEvent(EventData::Poll, id)) => {
if let Some(view) = windows.get_mut(&id) {
if view.poll() {
view.request_redraw();
}
};
}
// Event::UserEvent(_redraw) => {
// for (_, view) in windows.iter() {
// view.request_redraw();
// }
// }
Event::NewEvents(_) => {
for id in windows.keys() {
_ = proxy.send_event(UserWindowEvent(EventData::Poll, *id));
}
}

Event::WindowEvent {
window_id, event, ..
} => {
if let Some(window) = windows.get_mut(&window_id) {
window.handle_window_event(event);
};
}
Event::Suspended => {
for (_, view) in windows.iter_mut() {
view.suspend();
}
}

_ => (),
}
Event::Resumed => on_resume(),

if let Ok(event) = menu_channel.try_recv() {
if event.id == MenuId::new("dev.show_layout") {
for (_, view) in windows.iter_mut() {
view.renderer.devtools.show_layout = !view.renderer.devtools.show_layout;
view.request_redraw();
Event::WindowEvent {
window_id, event, ..
} => {
if let Some(window) = windows.get_mut(&window_id) {
window.handle_window_event(event);
};
}

_ => (),
}
}
});

if let Ok(event) = menu_channel.try_recv() {
if event.id == MenuId::new("dev.show_layout") {
for (_, view) in windows.iter_mut() {
view.renderer.devtools.show_layout = !view.renderer.devtools.show_layout;
view.request_redraw();
}
}
}
})
.unwrap();
}
2 changes: 1 addition & 1 deletion packages/dioxus-blitz/src/waker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures_util::task::ArcWake;
use std::sync::Arc;
use tao::{event_loop::EventLoopProxy, window::WindowId};
use winit::{event_loop::EventLoopProxy, window::WindowId};

#[derive(Debug, Clone)]
pub struct UserWindowEvent(pub EventData, pub WindowId);
Expand Down
Loading

0 comments on commit bb6eb9d

Please sign in to comment.