Skip to content

Commit

Permalink
Update winit (#108)
Browse files Browse the repository at this point in the history
* Update winit

* Don't pull in winit's default features
  • Loading branch information
Ralith authored Nov 6, 2023
1 parent 6c939b9 commit 05cb678
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 199 deletions.
2 changes: 1 addition & 1 deletion crates/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ pollster = "0.3.0"
profiling = "1.0.6"
tracy-client = { version = "0.15.1", optional = true }
wgpu = "0.17.1"
winit = "0.28.1"
winit = "0.29.2"
121 changes: 61 additions & 60 deletions crates/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async fn run(body: impl ExampleBody) {
}

// Normal winit setup for an EventLoop and Window.
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
.with_title(title)
.with_inner_size(LogicalSize::new(800.0, 600.0))
Expand Down Expand Up @@ -137,78 +137,79 @@ async fn run(body: impl ExampleBody) {

let start = Instant::now();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
event_loop.set_control_flow(ControlFlow::Poll);
event_loop
.run(move |event, elwt| {
if app.handle_event(&mut yak, &event, elwt) {
return;
}

if app.handle_event(&mut yak, &event, control_flow) {
return;
}
match event {
Event::AboutToWait => {
state.time = (Instant::now() - start).as_secs_f32();

match event {
Event::MainEventsCleared => {
state.time = (Instant::now() - start).as_secs_f32();
{
profiling::scope!("Build UI");

{
profiling::scope!("Build UI");
// Every frame, call yak.start() to begin building the UI for
// this frame. Any yakui widget calls that happen on this thread
// between start() and finish() will be applied to this yakui
// State.
yak.start();

// Every frame, call yak.start() to begin building the UI for
// this frame. Any yakui widget calls that happen on this thread
// between start() and finish() will be applied to this yakui
// State.
yak.start();
// Call out to the body of the program, passing in a bit of
// shared state that all the examples can use.
body.run(&mut state);

// Call out to the body of the program, passing in a bit of
// shared state that all the examples can use.
body.run(&mut state);
// Finish building the UI and compute this frame's layout.
yak.finish();
}

// Finish building the UI and compute this frame's layout.
yak.finish();
// The example graphics abstraction calls yak.paint() to get
// access to the underlying PaintDom, which holds all the state
// about how to paint widgets.
app.paint(&mut yak, {
let bg = yakui::colors::BACKGROUND_1.to_linear();
wgpu::Color {
r: bg.x.into(),
g: bg.y.into(),
b: bg.z.into(),
a: 1.0,
}
});

profiling::finish_frame!();
}

// The example graphics abstraction calls yak.paint() to get
// access to the underlying PaintDom, which holds all the state
// about how to paint widgets.
app.paint(&mut yak, {
let bg = yakui::colors::BACKGROUND_1.to_linear();
wgpu::Color {
r: bg.x.into(),
g: bg.y.into(),
b: bg.z.into(),
a: 1.0,
Event::WindowEvent {
event: WindowEvent::MouseInput { state, button, .. },
..
} => {
// This print is a handy way to show which mouse events are
// handled by yakui, and which ones will make it to the
// underlying application.
if button == winit::event::MouseButton::Left {
println!("Left mouse button {state:?}");
}
});

profiling::finish_frame!();
}

Event::WindowEvent {
event: WindowEvent::MouseInput { state, button, .. },
..
} => {
// This print is a handy way to show which mouse events are
// handled by yakui, and which ones will make it to the
// underlying application.
if button == winit::event::MouseButton::Left {
println!("Left mouse button {state:?}");
}
}

Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
if let Some(inset) = inset {
let size = Vec2::new(size.width as f32, size.height as f32);
yak.set_unscaled_viewport(Rect::from_pos_size(
Vec2::splat(inset),
size - Vec2::splat(inset * 2.0),
));
Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
if let Some(inset) = inset {
let size = Vec2::new(size.width as f32, size.height as f32);
yak.set_unscaled_viewport(Rect::from_pos_size(
Vec2::splat(inset),
size - Vec2::splat(inset * 2.0),
));
}
}
}

_ => (),
}
});
_ => (),
}
})
.unwrap();
}

/// This function takes some bytes and turns it into a yakui `Texture` object so
Expand Down
2 changes: 1 addition & 1 deletion crates/demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ env_logger = "0.10.0"
log = "0.4.17"
pollster = "0.3.0"
wgpu = { version = "0.17.1", features = ["webgl"] }
winit = "0.28.2"
winit = "0.29.2"

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_log = "0.2.1"
Expand Down
73 changes: 38 additions & 35 deletions crates/demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use winit::{
event::Event,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::Window,
};
Expand All @@ -23,52 +23,55 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
let mut yak = Yakui::new();
let mut graphics = Graphics::new(&window).await;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
event_loop.set_control_flow(ControlFlow::Poll);
event_loop
.run(move |event, elwt| {
if graphics.handle_event(&mut yak, &event, elwt) {
return;
}

if graphics.handle_event(&mut yak, &event, control_flow) {
return;
}
match event {
Event::AboutToWait => {
#[cfg(target_arch = "wasm32")]
{
use winit::dpi::LogicalSize;
use winit::event::WindowEvent;
use winit::window::WindowId;

match event {
Event::MainEventsCleared => {
#[cfg(target_arch = "wasm32")]
{
use winit::dpi::LogicalSize;
use winit::event::WindowEvent;
use winit::window::WindowId;
let web_window = web_sys::window().unwrap();

let web_window = web_sys::window().unwrap();
let event: Event<'_, ()> = Event::WindowEvent {
window_id: unsafe { WindowId::dummy() },
event: WindowEvent::Resized(window.inner_size()),
};

let event: Event<'_, ()> = Event::WindowEvent {
window_id: unsafe { WindowId::dummy() },
event: WindowEvent::Resized(window.inner_size()),
};
let width = web_window.inner_width().unwrap().as_f64().unwrap();
let height = web_window.inner_height().unwrap().as_f64().unwrap();
window.set_inner_size(LogicalSize::new(width, height));
graphics.handle_event(&mut yak, &event, control_flow);
}

let width = web_window.inner_width().unwrap().as_f64().unwrap();
let height = web_window.inner_height().unwrap().as_f64().unwrap();
window.set_inner_size(LogicalSize::new(width, height));
graphics.handle_event(&mut yak, &event, control_flow);
window.request_redraw();
}

window.request_redraw();
}

Event::RedrawRequested(_) => {
yak.start();
app();
yak.finish();
Event::WindowEvent {
event: WindowEvent::RedrawRequested { .. },
..
} => {
yak.start();
app();
yak.finish();

graphics.paint(&mut yak, wgpu::Color::BLACK);
graphics.paint(&mut yak, wgpu::Color::BLACK);
}
_ => (),
}

_ => (),
}
});
})
.unwrap();
}

fn main() {
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let window = winit::window::Window::new(&event_loop).unwrap();
#[cfg(not(target_arch = "wasm32"))]
{
Expand Down
2 changes: 1 addition & 1 deletion crates/yakui-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ yakui-wgpu = { path = "../yakui-wgpu" }

profiling = { version = "1.0.6", optional = true }
wgpu = "0.17.1"
winit = "0.28.1"
winit = { version = "0.29.2", features = ["rwh_05"] }
13 changes: 3 additions & 10 deletions crates/yakui-app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use winit::{
dpi::PhysicalSize,
event::{Event, StartCause, WindowEvent},
event_loop::ControlFlow,
event_loop::EventLoopWindowTarget,
window::Window,
};
use yakui_wgpu::SurfaceInfo;
Expand Down Expand Up @@ -166,7 +166,7 @@ impl Graphics {
&mut self,
yak: &mut yakui::Yakui,
event: &Event<T>,
control_flow: &mut ControlFlow,
elwt: &EventLoopWindowTarget<T>,
) -> bool {
// yakui_winit will return whether it handled an event. This means that
// yakui believes it should handle that event exclusively, like if a
Expand All @@ -180,7 +180,7 @@ impl Graphics {
event: WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
elwt.exit();
}

Event::NewEvents(cause) => {
Expand All @@ -207,13 +207,6 @@ impl Graphics {
self.resize(*size);
}

Event::WindowEvent {
event: WindowEvent::ScaleFactorChanged { new_inner_size, .. },
..
} => {
self.resize(**new_inner_size);
}

_ => (),
}

Expand Down
3 changes: 2 additions & 1 deletion crates/yakui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ edition = "2021"
[dependencies]
yakui-core = { path = "../yakui-core", version = "0.2.0" }

winit = "0.28.1"
# TODO: Disable all default features once supported (https://github.com/rust-windowing/winit/issues/3174)
winit = { version = "0.29.2", default-features = false, features = ["x11"] }
Loading

0 comments on commit 05cb678

Please sign in to comment.