mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 06:41:14 +00:00
Use winit also when targeting wasm
This allows unifying the code paths more.
This commit is contained in:
parent
9be4c88b13
commit
5b313af9aa
2 changed files with 26 additions and 24 deletions
|
@ -12,11 +12,13 @@ sixtyfps_gl_backend = { package = "gl", path = "../../sixtyfps_runtime/rendering
|
||||||
kurbo = "0.5.11"
|
kurbo = "0.5.11"
|
||||||
image = { version = "0.23.4", default-features = false, features = [ "png" ] }
|
image = { version = "0.23.4", default-features = false, features = [ "png" ] }
|
||||||
cgmath = "0.17.0"
|
cgmath = "0.17.0"
|
||||||
|
instant = "0.1"
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
web_sys = { version = "0.3", package = "web-sys", features=["console"] }
|
web_sys = { version = "0.3", package = "web-sys", features=["console"] }
|
||||||
wasm-bindgen = { version = "0.2" }
|
wasm-bindgen = { version = "0.2" }
|
||||||
glow = { version = "0.4.0", default-features = false }
|
glow = { version = "0.4.0", default-features = false }
|
||||||
|
winit = { version = "0.22.1", features = ["web-sys"] }
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
glutin = { version = "0.24" }
|
glutin = { version = "0.24" }
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
use cgmath::{Matrix4, SquareMatrix, Vector3};
|
use cgmath::{Matrix4, SquareMatrix, Vector3};
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
use glow::HasRenderLoop;
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use glutin;
|
use glutin;
|
||||||
|
use instant;
|
||||||
use kurbo::{BezPath, Rect};
|
use kurbo::{BezPath, Rect};
|
||||||
use sixtyfps_corelib::graphics::{Color, FillStyle, GraphicsBackend, RenderTree};
|
use sixtyfps_corelib::graphics::{Color, FillStyle, GraphicsBackend, RenderTree};
|
||||||
use sixtyfps_gl_backend::{GLRenderer, OpaqueRenderingPrimitive};
|
use sixtyfps_gl_backend::{GLRenderer, OpaqueRenderingPrimitive};
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
use winit::{event, event_loop, window::WindowBuilder};
|
use winit::{event, event_loop, window::WindowBuilder};
|
||||||
|
|
||||||
fn create_rect(
|
fn create_rect(
|
||||||
|
@ -32,10 +30,11 @@ pub fn wasm_main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
let (event_loop, windowed_context, gl_context) = {
|
|
||||||
let event_loop = event_loop::EventLoop::new();
|
let event_loop = event_loop::EventLoop::new();
|
||||||
let wb = WindowBuilder::new();
|
let wb = WindowBuilder::new();
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
let (windowed_context, gl_context) = {
|
||||||
let windowed_context =
|
let windowed_context =
|
||||||
glutin::ContextBuilder::new().with_vsync(true).build_windowed(wb, &event_loop).unwrap();
|
glutin::ContextBuilder::new().with_vsync(true).build_windowed(wb, &event_loop).unwrap();
|
||||||
let windowed_context = unsafe { windowed_context.make_current().unwrap() };
|
let windowed_context = unsafe { windowed_context.make_current().unwrap() };
|
||||||
|
@ -44,12 +43,11 @@ fn main() {
|
||||||
windowed_context.get_proc_address(s) as *const _
|
windowed_context.get_proc_address(s) as *const _
|
||||||
});
|
});
|
||||||
|
|
||||||
(event_loop, windowed_context, gl_context)
|
(windowed_context, gl_context)
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
let (event_loop, windowed_context, gl_context) = {
|
let (window, gl_context) = {
|
||||||
use wasm_bindgen::JsCast;
|
|
||||||
let canvas = web_sys::window()
|
let canvas = web_sys::window()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.document()
|
.document()
|
||||||
|
@ -58,17 +56,21 @@ fn main() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.dyn_into::<web_sys::HtmlCanvasElement>()
|
.dyn_into::<web_sys::HtmlCanvasElement>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let webgl1_context = canvas
|
|
||||||
|
use winit::platform::web::WindowBuilderExtWebSys;
|
||||||
|
use winit::platform::web::WindowExtWebSys;
|
||||||
|
|
||||||
|
let window = wb.with_canvas(Some(canvas)).build(&event_loop).unwrap();
|
||||||
|
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
let webgl1_context = window
|
||||||
|
.canvas()
|
||||||
.get_context("webgl")
|
.get_context("webgl")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.dyn_into::<web_sys::WebGlRenderingContext>()
|
.dyn_into::<web_sys::WebGlRenderingContext>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
(
|
(window, glow::Context::from_webgl1_context(webgl1_context))
|
||||||
glow::RenderLoop::from_request_animation_frame(),
|
|
||||||
(canvas.width(), canvas.height()),
|
|
||||||
glow::Context::from_webgl1_context(webgl1_context),
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut renderer = GLRenderer::new(gl_context);
|
let mut renderer = GLRenderer::new(gl_context);
|
||||||
|
@ -124,10 +126,8 @@ fn main() {
|
||||||
|
|
||||||
render_tree.node_at_mut(root).append_child(image_node);
|
render_tree.node_at_mut(root).append_child(image_node);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
let next_frame_time =
|
let next_frame_time = instant::Instant::now() + std::time::Duration::from_nanos(16_666_667);
|
||||||
std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
|
|
||||||
*control_flow = event_loop::ControlFlow::WaitUntil(next_frame_time);
|
*control_flow = event_loop::ControlFlow::WaitUntil(next_frame_time);
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
|
@ -146,14 +146,14 @@ fn main() {
|
||||||
_ => return,
|
_ => return,
|
||||||
}
|
}
|
||||||
|
|
||||||
let size = windowed_context.window().inner_size();
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
let window = windowed_context.window();
|
||||||
|
|
||||||
|
let size = window.inner_size();
|
||||||
// TODO #4: ensure GO context is current -- see if this can be done within the runtime
|
// TODO #4: ensure GO context is current -- see if this can be done within the runtime
|
||||||
render_tree.render(&mut renderer, size.width, size.height, root);
|
render_tree.render(&mut renderer, size.width, size.height, root);
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
windowed_context.swap_buffers().unwrap();
|
windowed_context.swap_buffers().unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
event_loop.run(move |_running: &mut bool| {
|
|
||||||
render_tree.render(&mut renderer, windowed_context.0, windowed_context.1, root);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue