-
Notifications
You must be signed in to change notification settings - Fork 946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP - wasm_bindgen backend for eventloop-2.0 #845
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0e5f1c5
all required types declared
spennydl e6d1a50
stubbed out interfaces. it builds!
spennydl 1a8b3c1
beginnings of an event loop
spennydl 5be877c
restructured, sketched out a few more types
spennydl 840ffde
added example, light cleanup
spennydl b93f0d4
exposed canvas, some work on window creation
spennydl f83bd53
wasm_bindgen example builds
spennydl 6946fb1
simple mouse click event fires
spennydl 52ed46c
cleanup of imports, added the event module
spennydl 63fab76
macro for installing simple events
spennydl 8b9280e
moved events back over to event_loop, sleeping works now
spennydl 7c144ae
Updated for changing interfaces
spennydl ed2c76f
fleshed out the window
spennydl d413a03
updated circlecli config with websys build
spennydl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
extern crate web_sys; | ||
extern crate wasm_bindgen; | ||
|
||
extern crate winit; | ||
use winit::window::WindowBuilder; | ||
use winit::event::{Event, WindowEvent}; | ||
use winit::event_loop::{EventLoop, ControlFlow}; | ||
use winit::platform::websys::WebsysWindowExt; | ||
use winit::platform::websys::WebsysWindowBuilderExt; | ||
|
||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen::JsCast; | ||
|
||
// A macro to provide `println!(..)`-style syntax for `console.log` logging. | ||
macro_rules! log { | ||
( $( $t:tt )* ) => { | ||
web_sys::console::log_1(&format!( $( $t )* ).into()); | ||
} | ||
} | ||
|
||
// use wee_alloc if it is available | ||
#[cfg(feature = "wee_alloc")] | ||
#[global_allocator] | ||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; | ||
|
||
#[wasm_bindgen] | ||
struct App {} | ||
|
||
#[wasm_bindgen] | ||
impl App { | ||
pub fn new() -> App { | ||
App{} | ||
} | ||
|
||
pub fn run(&self) { | ||
// create an event loop | ||
let event_loop = EventLoop::new(); | ||
|
||
// create a window and associate it with the event loop | ||
let window = WindowBuilder::new() | ||
.with_title("A fantastic window!") | ||
.with_canvas_id("test") | ||
.build(&event_loop) | ||
.unwrap(); | ||
|
||
// do some drawing | ||
let canvas = window.get_canvas(); | ||
let ctx = canvas.get_context("2d").unwrap().unwrap() | ||
.dyn_into::<web_sys::CanvasRenderingContext2d>().unwrap(); | ||
ctx.begin_path(); | ||
ctx.arc(95.0, 50.0, 40.0, 0.0, 2.0 * 3.14159).unwrap(); | ||
ctx.stroke(); | ||
|
||
// run forever | ||
// | ||
// when using wasm_bindgen, this will currently throw a js | ||
// exception once all browser event handlers have been installed. | ||
event_loop.run(|event, _, control_flow| { | ||
|
||
match event { | ||
Event::WindowEvent { | ||
event: WindowEvent::CloseRequested, | ||
.. | ||
} => *control_flow = ControlFlow::Exit, | ||
_ => *control_flow = ControlFlow::Poll, | ||
} | ||
}); | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let app = App::new(); | ||
app.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,6 @@ pub mod ios; | |
pub mod macos; | ||
pub mod unix; | ||
pub mod windows; | ||
pub mod websys; | ||
|
||
pub mod desktop; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
extern crate wasm_bindgen; | ||
extern crate web_sys; | ||
|
||
use platform_impl::window::ElementSelection; | ||
use window::{Window, WindowBuilder}; | ||
|
||
pub trait WebsysWindowExt { | ||
fn get_canvas<'a>(&'a self) -> &'a web_sys::HtmlCanvasElement; | ||
} | ||
|
||
impl WebsysWindowExt for Window { | ||
fn get_canvas<'a>(&'a self) -> &'a web_sys::HtmlCanvasElement { | ||
&self.window.canvas | ||
} | ||
} | ||
|
||
pub trait WebsysWindowBuilderExt { | ||
fn with_canvas_id(self, canvas_id: &str) -> WindowBuilder; | ||
|
||
fn with_container_id(self, container_id: &str) -> WindowBuilder; | ||
} | ||
|
||
impl WebsysWindowBuilderExt for WindowBuilder { | ||
fn with_canvas_id(mut self, canvas_id: &str) -> WindowBuilder { | ||
self.platform_specific.element = ElementSelection::CanvasId(canvas_id.to_string()); | ||
self | ||
} | ||
|
||
fn with_container_id(mut self, container_id: &str) -> WindowBuilder { | ||
self.platform_specific.element = ElementSelection::ContainerId(container_id.to_string()); | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::convert::From; | ||
|
||
use ::event::WindowEvent as WindowEvent; | ||
use ::event::DeviceId as WDeviceId; | ||
use ::event::{ElementState, MouseButton}; | ||
|
||
use ::web_sys::MouseEvent; | ||
use super::window::DeviceId; | ||
|
||
impl From<MouseEvent> for WindowEvent { | ||
fn from(event: MouseEvent) -> Self { | ||
WindowEvent::MouseInput { | ||
device_id: WDeviceId(DeviceId::dummy()), | ||
state: ElementState::Pressed, | ||
button: MouseButton::Left, | ||
modifiers: Default::default() | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Will emscripten conflict with websys with some targets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't believe so, the target triple used for websys is
wasm32-unknown-unknown
. i would assume that if someone is building forwasm32-unknown-emscripten
they wouldn't also have thewebsys
feature set. i could add something liketarget_os = "unknown"
to the cfg condition below (line21) to be safe