Skip to content
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

stdweb support for eventloop 2.0 #797

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fd4db40
Create the type layout
ryanisaacg Feb 13, 2019
f44e98d
Implemented a few easy methods
ryanisaacg Feb 20, 2019
c088f8b
Create the outline of event input and handler calls
ryanisaacg Feb 26, 2019
f698d45
Add key and mouse event support that typechecks
ryanisaacg Mar 2, 2019
37d354c
Get to a state where a canvas is spawned
ryanisaacg Mar 10, 2019
283a8de
Refactor out the stdweb functionality into different modules
ryanisaacg Mar 10, 2019
3dd0e31
Merge eventloop-2.0 into stdweb-eventloop-2
ryanisaacg Mar 11, 2019
aaee724
Rearchitect to allow API compliance
ryanisaacg Mar 12, 2019
d1deba8
Rename modules
ryanisaacg Mar 12, 2019
a5166ba
Implement request_redraw
ryanisaacg Mar 16, 2019
96786bb
Implement focus event
ryanisaacg Mar 16, 2019
85446d8
Fix warnings
ryanisaacg Mar 16, 2019
b09629f
Handle ControlFlow::Exit
ryanisaacg Mar 19, 2019
7c6bdcc
Handle ControlFlow::Exit and dealing with events-in-events
ryanisaacg Mar 23, 2019
9e25561
Fix compile failures and add canvas positioning
ryanisaacg Apr 3, 2019
fe5e300
Clean up and document the core of stdweb event handling
ryanisaacg Apr 25, 2019
9f801cf
Only send the request-redraw on the next animation frame
ryanisaacg Apr 29, 2019
70c7382
Fix the request_animation_frame lifetimes
ryanisaacg May 2, 2019
37dadab
Add access to the canvas in the Window
ryanisaacg Jun 1, 2019
1409f83
Add support for mouse wheel
ryanisaacg Jun 1, 2019
f2b6ef2
Merge master into stdweb-eventloop-2
ryanisaacg Jun 1, 2019
b59e3c6
WIP
ryanisaacg Jun 15, 2019
2690306
Implement Poll and WaitUntil in the stdweb backend
ryanisaacg Jun 17, 2019
182beb4
Indicate that I will be maintaing the stdweb backend
ryanisaacg Jun 17, 2019
b571362
Fix a panic due to double-borrow
ryanisaacg Jun 21, 2019
cf28751
Remove unnecessary set-to-wait in example
ryanisaacg Jun 23, 2019
a0f280e
Update how timeouts are cleared to avoid possible double-clearing
ryanisaacg Jun 23, 2019
94387c4
Merge master Monitor APIs to stdweb
ryanisaacg Jun 27, 2019
e94a006
Merge remote-tracking branch 'upstream/master' into stdweb-eventloop-2
ryanisaacg Jul 9, 2019
7bbc829
Fix a few warnings
ryanisaacg Jul 9, 2019
c76ab65
Add on-close event listening
ryanisaacg Jul 9, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ categories = ["gui"]
features = ["serde"]

[dependencies]
instant = "0.1"
lazy_static = "1"
libc = "0.2"
log = "0.4"
Expand Down Expand Up @@ -79,3 +80,10 @@ percent-encoding = "1.0"

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "windows"))'.dependencies.parking_lot]
version = "0.8"

[target.'cfg(target_arch = "wasm32")'.dependencies]
stdweb = { path = "../stdweb", optional = true }
instant = { version = "0.1", features = ["stdweb"] }

[patch.crates-io]
stdweb = { path = "../stdweb" }
2 changes: 0 additions & 2 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ fn main() {
.unwrap();

event_loop.run(move |event, _, control_flow| {
println!("{:?}", event);

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
6 changes: 4 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//! processed and used to modify the program state. For more details, see the root-level documentation.
//!
//! [event_loop_run]: ../event_loop/struct.EventLoop.html#method.run
use std::{path::PathBuf, time::Instant};

use instant::Instant;
use std::path::PathBuf;

use crate::{
dpi::{LogicalPosition, LogicalSize},
Expand Down Expand Up @@ -61,7 +63,7 @@ impl<T> Event<T> {
}

/// Describes the reason the event loop is resuming.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StartCause {
/// Sent if the time specified by `ControlFlow::WaitUntil` has been reached. Contains the
/// moment the timeout was requested and the requested resume time. The actual resume time is
Expand Down
6 changes: 4 additions & 2 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
//! [create_proxy]: ./struct.EventLoop.html#method.create_proxy
//! [event_loop_proxy]: ./struct.EventLoopProxy.html
//! [send_event]: ./struct.EventLoopProxy.html#method.send_event
use std::{error, fmt, ops::Deref, time::Instant};

use instant::Instant;
use std::{error, fmt, ops::Deref};

use crate::{
event::Event,
Expand Down Expand Up @@ -69,7 +71,7 @@ impl<T> fmt::Debug for EventLoopWindowTarget<T> {
/// the control flow to `Poll`.
///
/// [events_cleared]: ../event/enum.Event.html#variant.EventsCleared
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ControlFlow {
/// When the current loop iteration finishes, immediately begin a new iteration regardless of
/// whether or not new events are available to process.
Expand Down
20 changes: 0 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,6 @@
//! [`LoopDestroyed`]: ./event/enum.Event.html#variant.LoopDestroyed
//! [`platform`]: ./platform/index.html

#![deny(rust_2018_idioms)]

#[allow(unused_imports)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
#[macro_use]
#[cfg(target_os = "windows")]
extern crate derivative;
#[macro_use]
#[cfg(target_os = "windows")]
extern crate bitflags;
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[macro_use]
extern crate objc;

pub mod dpi;
#[macro_use]
pub mod error;
Expand Down
2 changes: 2 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ pub mod macos;
pub mod unix;
pub mod windows;

pub mod stdweb;

pub mod desktop;
8 changes: 8 additions & 0 deletions src/platform/stdweb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg(feature = "stdweb")]

use stdweb::web::html_element::CanvasElement;

pub trait WindowExtStdweb {
fn canvas(&self) -> CanvasElement;
}

6 changes: 5 additions & 1 deletion src/platform_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ mod platform;
#[cfg(target_os = "emscripten")]
#[path = "emscripten/mod.rs"]
mod platform;
#[cfg(feature = "stdweb")]
#[path="stdweb/mod.rs"]
mod platform;

#[cfg(all(
not(target_os = "ios"),
Expand All @@ -35,6 +38,7 @@ mod platform;
not(target_os = "freebsd"),
not(target_os = "netbsd"),
not(target_os = "openbsd"),
not(target_os = "emscripten")
not(target_os = "emscripten"),
not(feature = "stdweb")
))]
compile_error!("The platform you're compiling for is not supported by winit");
203 changes: 203 additions & 0 deletions src/platform_impl/stdweb/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
use stdweb::{
js,
JsSerialize,
web::event::{IKeyboardEvent, IMouseEvent},
unstable::TryInto
};
use crate::event::{MouseButton, ModifiersState, ScanCode, VirtualKeyCode};

pub fn button_mapping(event: &impl IKeyboardEvent) -> Option<VirtualKeyCode> {
Some(match &event.code()[..] {
"Digit1" => VirtualKeyCode::Key1,
"Digit2" => VirtualKeyCode::Key2,
"Digit3" => VirtualKeyCode::Key3,
"Digit4" => VirtualKeyCode::Key4,
"Digit5" => VirtualKeyCode::Key5,
"Digit6" => VirtualKeyCode::Key6,
"Digit7" => VirtualKeyCode::Key7,
"Digit8" => VirtualKeyCode::Key8,
"Digit9" => VirtualKeyCode::Key9,
"Digit0" => VirtualKeyCode::Key0,
"KeyA" => VirtualKeyCode::A,
"KeyB" => VirtualKeyCode::B,
"KeyC" => VirtualKeyCode::C,
"KeyD" => VirtualKeyCode::D,
"KeyE" => VirtualKeyCode::E,
"KeyF" => VirtualKeyCode::F,
"KeyG" => VirtualKeyCode::G,
"KeyH" => VirtualKeyCode::H,
"KeyI" => VirtualKeyCode::I,
"KeyJ" => VirtualKeyCode::J,
"KeyK" => VirtualKeyCode::K,
"KeyL" => VirtualKeyCode::L,
"KeyM" => VirtualKeyCode::M,
"KeyN" => VirtualKeyCode::N,
"KeyO" => VirtualKeyCode::O,
"KeyP" => VirtualKeyCode::P,
"KeyQ" => VirtualKeyCode::Q,
"KeyR" => VirtualKeyCode::R,
"KeyS" => VirtualKeyCode::S,
"KeyT" => VirtualKeyCode::T,
"KeyU" => VirtualKeyCode::U,
"KeyV" => VirtualKeyCode::V,
"KeyW" => VirtualKeyCode::W,
"KeyX" => VirtualKeyCode::X,
"KeyY" => VirtualKeyCode::Y,
"KeyZ" => VirtualKeyCode::Z,
"Escape" => VirtualKeyCode::Escape,
"F1" => VirtualKeyCode::F1,
"F2" => VirtualKeyCode::F2,
"F3" => VirtualKeyCode::F3,
"F4" => VirtualKeyCode::F4,
"F5" => VirtualKeyCode::F5,
"F6" => VirtualKeyCode::F6,
"F7" => VirtualKeyCode::F7,
"F8" => VirtualKeyCode::F8,
"F9" => VirtualKeyCode::F9,
"F10" => VirtualKeyCode::F10,
"F11" => VirtualKeyCode::F11,
"F12" => VirtualKeyCode::F12,
"F13" => VirtualKeyCode::F13,
"F14" => VirtualKeyCode::F14,
"F15" => VirtualKeyCode::F15,
"F16" => VirtualKeyCode::F16,
"F17" => VirtualKeyCode::F17,
"F18" => VirtualKeyCode::F18,
"F19" => VirtualKeyCode::F19,
"F20" => VirtualKeyCode::F20,
"F21" => VirtualKeyCode::F21,
"F22" => VirtualKeyCode::F22,
"F23" => VirtualKeyCode::F23,
"F24" => VirtualKeyCode::F24,
"PrintScreen" => VirtualKeyCode::Snapshot,
"ScrollLock" => VirtualKeyCode::Scroll,
"Pause" => VirtualKeyCode::Pause,
"Insert" => VirtualKeyCode::Insert,
"Home" => VirtualKeyCode::Home,
"Delete" => VirtualKeyCode::Delete,
"End" => VirtualKeyCode::End,
"PageDown" => VirtualKeyCode::PageDown,
"PageUp" => VirtualKeyCode::PageUp,
"ArrowLeft" => VirtualKeyCode::Left,
"ArrowUp" => VirtualKeyCode::Up,
"ArrowRight" => VirtualKeyCode::Right,
"ArrowDown" => VirtualKeyCode::Down,
"Backspace" => VirtualKeyCode::Back,
"Enter" => VirtualKeyCode::Return,
"Space" => VirtualKeyCode::Space,
"Compose" => VirtualKeyCode::Compose,
"Caret" => VirtualKeyCode::Caret,
"NumLock" => VirtualKeyCode::Numlock,
"Numpad0" => VirtualKeyCode::Numpad0,
"Numpad1" => VirtualKeyCode::Numpad1,
"Numpad2" => VirtualKeyCode::Numpad2,
"Numpad3" => VirtualKeyCode::Numpad3,
"Numpad4" => VirtualKeyCode::Numpad4,
"Numpad5" => VirtualKeyCode::Numpad5,
"Numpad6" => VirtualKeyCode::Numpad6,
"Numpad7" => VirtualKeyCode::Numpad7,
"Numpad8" => VirtualKeyCode::Numpad8,
"Numpad9" => VirtualKeyCode::Numpad9,
"AbntC1" => VirtualKeyCode::AbntC1,
"AbntC2" => VirtualKeyCode::AbntC2,
"NumpadAdd" => VirtualKeyCode::Add,
"Quote" => VirtualKeyCode::Apostrophe,
"Apps" => VirtualKeyCode::Apps,
"At" => VirtualKeyCode::At,
"Ax" => VirtualKeyCode::Ax,
"Backslash" => VirtualKeyCode::Backslash,
"Calculator" => VirtualKeyCode::Calculator,
"Capital" => VirtualKeyCode::Capital,
"Semicolon" => VirtualKeyCode::Semicolon,
"Comma" => VirtualKeyCode::Comma,
"Convert" => VirtualKeyCode::Convert,
"NumpadDecimal" => VirtualKeyCode::Decimal,
"NumpadDivide" => VirtualKeyCode::Divide,
"Equal" => VirtualKeyCode::Equals,
"Backquote" => VirtualKeyCode::Grave,
"Kana" => VirtualKeyCode::Kana,
"Kanji" => VirtualKeyCode::Kanji,
"AltLeft" => VirtualKeyCode::LAlt,
"BracketLeft" => VirtualKeyCode::LBracket,
"ControlLeft" => VirtualKeyCode::LControl,
"ShiftLeft" => VirtualKeyCode::LShift,
"MetaLeft" => VirtualKeyCode::LWin,
"Mail" => VirtualKeyCode::Mail,
"MediaSelect" => VirtualKeyCode::MediaSelect,
"MediaStop" => VirtualKeyCode::MediaStop,
"Minus" => VirtualKeyCode::Minus,
"NumpadMultiply" => VirtualKeyCode::Multiply,
"Mute" => VirtualKeyCode::Mute,
"LaunchMyComputer" => VirtualKeyCode::MyComputer,
"NavigateForward" => VirtualKeyCode::NavigateForward,
"NavigateBackward" => VirtualKeyCode::NavigateBackward,
"NextTrack" => VirtualKeyCode::NextTrack,
"NoConvert" => VirtualKeyCode::NoConvert,
"NumpadComma" => VirtualKeyCode::NumpadComma,
"NumpadEnter" => VirtualKeyCode::NumpadEnter,
"NumpadEquals" => VirtualKeyCode::NumpadEquals,
"OEM102" => VirtualKeyCode::OEM102,
"Period" => VirtualKeyCode::Period,
"PlayPause" => VirtualKeyCode::PlayPause,
"Power" => VirtualKeyCode::Power,
"PrevTrack" => VirtualKeyCode::PrevTrack,
"AltRight" => VirtualKeyCode::RAlt,
"BracketRight" => VirtualKeyCode::RBracket,
"ControlRight" => VirtualKeyCode::RControl,
"ShiftRight" => VirtualKeyCode::RShift,
"MetaRight" => VirtualKeyCode::RWin,
"Slash" => VirtualKeyCode::Slash,
"Sleep" => VirtualKeyCode::Sleep,
"Stop" => VirtualKeyCode::Stop,
"NumpadSubtract" => VirtualKeyCode::Subtract,
"Sysrq" => VirtualKeyCode::Sysrq,
"Tab" => VirtualKeyCode::Tab,
"Underline" => VirtualKeyCode::Underline,
"Unlabeled" => VirtualKeyCode::Unlabeled,
"AudioVolumeDown" => VirtualKeyCode::VolumeDown,
"AudioVolumeUp" => VirtualKeyCode::VolumeUp,
"Wake" => VirtualKeyCode::Wake,
"WebBack" => VirtualKeyCode::WebBack,
"WebFavorites" => VirtualKeyCode::WebFavorites,
"WebForward" => VirtualKeyCode::WebForward,
"WebHome" => VirtualKeyCode::WebHome,
"WebRefresh" => VirtualKeyCode::WebRefresh,
"WebSearch" => VirtualKeyCode::WebSearch,
"WebStop" => VirtualKeyCode::WebStop,
"Yen" => VirtualKeyCode::Yen,
_ => return None
})
}

pub fn mouse_modifiers_state(event: &impl IMouseEvent) -> ModifiersState {
ModifiersState {
shift: event.shift_key(),
ctrl: event.ctrl_key(),
alt: event.alt_key(),
logo: event.meta_key(),
}
}

pub fn mouse_button(event: &impl IMouseEvent) -> MouseButton {
match event.button() {
stdweb::web::event::MouseButton::Left => MouseButton::Left,
stdweb::web::event::MouseButton::Right => MouseButton::Right,
stdweb::web::event::MouseButton::Wheel => MouseButton::Middle,
stdweb::web::event::MouseButton::Button4 => MouseButton::Other(0),
stdweb::web::event::MouseButton::Button5 => MouseButton::Other(1),
}
}

pub fn keyboard_modifiers_state(event: &impl IKeyboardEvent) -> ModifiersState {
ModifiersState {
shift: event.shift_key(),
ctrl: event.ctrl_key(),
alt: event.alt_key(),
logo: event.meta_key(),
}
}

pub fn scancode<T: JsSerialize>(event: &T) -> ScanCode {
let which = js! ( return @{event}.which; );
which.try_into().expect("The which value should be a number")
}
Loading