Minor keyboard updates

Support combined character input from winit.
Make use of utility methods from keyboard-types.
Remove prinatable attribute of KeyboardEvent.
This commit is contained in:
Pyfisch 2018-10-13 20:30:30 +02:00
parent 0ccaa7e1a9
commit c8619424f2
14 changed files with 98 additions and 182 deletions

22
Cargo.lock generated
View file

@ -512,7 +512,7 @@ dependencies = [
"gleam 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "gleam 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
@ -549,7 +549,7 @@ dependencies = [
"gfx_traits 0.0.1", "gfx_traits 0.0.1",
"hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)",
"ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"layout_traits 0.0.1", "layout_traits 0.0.1",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"metrics 0.0.1", "metrics 0.0.1",
@ -902,7 +902,7 @@ name = "embedder_traits"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
@ -1736,7 +1736,7 @@ dependencies = [
[[package]] [[package]]
name = "keyboard-types" name = "keyboard-types"
version = "0.4.0-servo" version = "0.4.2-servo"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2030,7 +2030,7 @@ dependencies = [
"hashglobe 0.1.0", "hashglobe 0.1.0",
"hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper_serde 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "hyper_serde 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"mozjs 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "mozjs 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
"selectors 0.20.0", "selectors 0.20.0",
"serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
@ -3021,7 +3021,7 @@ dependencies = [
"ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
"jstraceable_derive 0.0.1", "jstraceable_derive 0.0.1",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -3124,7 +3124,7 @@ name = "script_tests"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"euclid 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"script 0.0.1", "script 0.0.1",
"servo_url 0.0.1", "servo_url 0.0.1",
] ]
@ -3143,7 +3143,7 @@ dependencies = [
"hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper_serde 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "hyper_serde 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
"malloc_size_of 0.0.1", "malloc_size_of 0.0.1",
"malloc_size_of_derive 0.0.1", "malloc_size_of_derive 0.0.1",
@ -3232,7 +3232,7 @@ dependencies = [
"gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gleam 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "gleam 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glutin 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", "glutin 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libservo 0.0.1", "libservo 0.0.1",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -4064,7 +4064,7 @@ dependencies = [
"hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)", "keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
@ -4492,7 +4492,7 @@ dependencies = [
"checksum jni-sys 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" "checksum jni-sys 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130"
"checksum jpeg-decoder 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "0dfe27a6c0dabd772d0f9b9f8701c4ca12c4d1eebcadf2be1f6f70396f6a1434" "checksum jpeg-decoder 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "0dfe27a6c0dabd772d0f9b9f8701c4ca12c4d1eebcadf2be1f6f70396f6a1434"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum keyboard-types 0.4.0-servo (registry+https://github.com/rust-lang/crates.io-index)" = "4bfd54c7c93e29a8a7ef177160b9898a479e31220918493fbfab49744008d172" "checksum keyboard-types 0.4.2-servo (registry+https://github.com/rust-lang/crates.io-index)" = "75082c134a78e0fc2232d2f30bf3dfdea1cd28591846b85a73b4b46cd776b482"
"checksum khronos_api 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9ef23fcc4059260c5936f638c9805ebfc87cb172fa6661d130cba7f97d58f55" "checksum khronos_api 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9ef23fcc4059260c5936f638c9805ebfc87cb172fa6661d130cba7f97d58f55"
"checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"

View file

@ -21,7 +21,7 @@ gleam = {version = "0.6", optional = true}
image = "0.19" image = "0.19"
ipc-channel = "0.11" ipc-channel = "0.11"
libc = "0.2" libc = "0.2"
keyboard-types = {version = "0.4.0-serde", features = ["serde"]} keyboard-types = {version = "0.4.2-servo", features = ["serde"]}
log = "0.4" log = "0.4"
msg = {path = "../msg"} msg = {path = "../msg"}
net_traits = {path = "../net_traits"} net_traits = {path = "../net_traits"}

View file

@ -25,7 +25,7 @@ gfx_traits = {path = "../gfx_traits"}
hyper = "0.10" hyper = "0.10"
ipc-channel = "0.11" ipc-channel = "0.11"
layout_traits = {path = "../layout_traits"} layout_traits = {path = "../layout_traits"}
keyboard-types = {version = "0.4.0-serde", features = ["serde"]} keyboard-types = {version = "0.4.2-servo", features = ["serde"]}
log = "0.4" log = "0.4"
metrics = {path = "../metrics"} metrics = {path = "../metrics"}
msg = {path = "../msg"} msg = {path = "../msg"}

View file

@ -14,7 +14,7 @@ tests = []
[dependencies] [dependencies]
ipc-channel = "0.11" ipc-channel = "0.11"
keyboard-types = {version = "0.4.0-serde", features = ["serde"]} keyboard-types = {version = "0.4.2-servo", features = ["serde"]}
lazy_static = "1" lazy_static = "1"
log = "0.4" log = "0.4"
msg = {path = "../msg"} msg = {path = "../msg"}

View file

@ -31,7 +31,7 @@ euclid = "0.19"
hashglobe = { path = "../hashglobe" } hashglobe = { path = "../hashglobe" }
hyper = { version = "0.10", optional = true } hyper = { version = "0.10", optional = true }
hyper_serde = { version = "0.8", optional = true } hyper_serde = { version = "0.8", optional = true }
keyboard-types = {version = "0.4.0-serde", features = ["serde"], optional = true} keyboard-types = {version = "0.4.2-servo", features = ["serde"], optional = true}
mozjs = { version = "0.9.0", optional = true } mozjs = { version = "0.9.0", optional = true }
selectors = { path = "../selectors" } selectors = { path = "../selectors" }
serde = { version = "1.0.27", optional = true } serde = { version = "1.0.27", optional = true }

View file

@ -59,7 +59,7 @@ image = "0.19"
ipc-channel = "0.11" ipc-channel = "0.11"
itertools = "0.7.6" itertools = "0.7.6"
jstraceable_derive = {path = "../jstraceable_derive"} jstraceable_derive = {path = "../jstraceable_derive"}
keyboard-types = {version = "0.4.0-serde", features = ["serde"]} keyboard-types = {version = "0.4.2-servo", features = ["serde"]}
lazy_static = "1" lazy_static = "1"
libc = "0.2" libc = "0.2"
log = "0.4" log = "0.4"

View file

@ -61,7 +61,7 @@ use dom::htmlimageelement::HTMLImageElement;
use dom::htmlmetaelement::HTMLMetaElement; use dom::htmlmetaelement::HTMLMetaElement;
use dom::htmlscriptelement::{HTMLScriptElement, ScriptResult}; use dom::htmlscriptelement::{HTMLScriptElement, ScriptResult};
use dom::htmltitleelement::HTMLTitleElement; use dom::htmltitleelement::HTMLTitleElement;
use dom::keyboardevent::{KeyboardEvent, key_keycode}; use dom::keyboardevent::KeyboardEvent;
use dom::location::Location; use dom::location::Location;
use dom::messageevent::MessageEvent; use dom::messageevent::MessageEvent;
use dom::mouseevent::MouseEvent; use dom::mouseevent::MouseEvent;
@ -770,10 +770,12 @@ impl Document {
// Step 1 is not handled here; the fragid is already obtained by the calling function // Step 1 is not handled here; the fragid is already obtained by the calling function
// Step 2: Simply use None to indicate the top of the document. // Step 2: Simply use None to indicate the top of the document.
// Step 3 & 4 // Step 3 & 4
percent_decode(fragid.as_bytes()).decode_utf8().ok() percent_decode(fragid.as_bytes())
// Step 5 .decode_utf8()
.ok()
// Step 5
.and_then(|decoded_fragid| self.get_element_by_id(&Atom::from(decoded_fragid))) .and_then(|decoded_fragid| self.get_element_by_id(&Atom::from(decoded_fragid)))
// Step 6 // Step 6
.or_else(|| self.get_anchor_by_name(fragid)) .or_else(|| self.get_anchor_by_name(fragid))
// Step 7 & 8 // Step 7 & 8
} }
@ -806,7 +808,8 @@ impl Document {
rect.origin.x.to_nearest_px() as f32, rect.origin.x.to_nearest_px() as f32,
rect.origin.y.to_nearest_px() as f32, rect.origin.y.to_nearest_px() as f32,
) )
}).or_else(|| { })
.or_else(|| {
if fragment.is_empty() || fragment.eq_ignore_ascii_case("top") { if fragment.is_empty() || fragment.eq_ignore_ascii_case("top") {
// FIXME(stshine): this should be the origin of the stacking context space, // FIXME(stshine): this should be the origin of the stacking context space,
// which may differ under the influence of writing mode. // which may differ under the influence of writing mode.
@ -1349,44 +1352,19 @@ impl Document {
} }
/// The entry point for all key processing for web content /// The entry point for all key processing for web content
pub fn dispatch_key_event( pub fn dispatch_key_event(&self, keyboard_event: ::keyboard_types::KeyboardEvent) {
&self,
keyboard_event: ::keyboard_types::KeyboardEvent,
) {
let focused = self.get_focused_element(); let focused = self.get_focused_element();
let body = self.GetBody(); let body = self.GetBody();
let printable = match keyboard_event.key {
Key::Character(_) => true,
_ => false,
};
let target = match (&focused, &body) { let target = match (&focused, &body) {
(&Some(ref focused), _) => focused.upcast(), (&Some(ref focused), _) => focused.upcast(),
(&None, &Some(ref body)) => body.upcast(), (&None, &Some(ref body)) => body.upcast(),
(&None, &None) => self.window.upcast(), (&None, &None) => self.window.upcast(),
}; };
let ev_type = DOMString::from(
match keyboard_event.state {
KeyState::Down => "keydown",
KeyState::Up => "keyup",
}.to_owned());
let char_code = if let Key::Character(ref c) = keyboard_event.key {
let mut chars = c.chars();
let n = chars.next().map(|c| c as u32);
if chars.next().is_some() {
None
} else {
n
}
} else {
None
};
let keyevent = KeyboardEvent::new( let keyevent = KeyboardEvent::new(
&self.window, &self.window,
ev_type, DOMString::from(keyboard_event.state.to_string()),
true, true,
true, true,
Some(&self.window), Some(&self.window),
@ -1397,15 +1375,18 @@ impl Document {
keyboard_event.repeat, keyboard_event.repeat,
keyboard_event.is_composing, keyboard_event.is_composing,
keyboard_event.modifiers, keyboard_event.modifiers,
char_code, 0,
key_keycode(&keyboard_event.key), keyboard_event.key.legacy_keycode(),
); );
let event = keyevent.upcast::<Event>(); let event = keyevent.upcast::<Event>();
event.fire(target); event.fire(target);
let mut cancel_state = event.get_cancel_state(); let mut cancel_state = event.get_cancel_state();
// https://w3c.github.io/uievents/#keys-cancelable-keys // https://w3c.github.io/uievents/#keys-cancelable-keys
if keyboard_event.state != KeyState::Up && printable && cancel_state != EventDefault::Prevented { if keyboard_event.state == KeyState::Down &&
keyboard_event.key.legacy_charcode() != 0 &&
cancel_state != EventDefault::Prevented
{
// https://w3c.github.io/uievents/#keypress-event-order // https://w3c.github.io/uievents/#keypress-event-order
let event = KeyboardEvent::new( let event = KeyboardEvent::new(
&self.window, &self.window,
@ -1420,7 +1401,7 @@ impl Document {
keyboard_event.repeat, keyboard_event.repeat,
keyboard_event.is_composing, keyboard_event.is_composing,
keyboard_event.modifiers, keyboard_event.modifiers,
None, keyboard_event.key.legacy_charcode(),
0, 0,
); );
let ev = event.upcast::<Event>(); let ev = event.upcast::<Event>();
@ -1438,7 +1419,9 @@ impl Document {
// Here, we're dispatching it after the key event so the script has a chance to cancel it // Here, we're dispatching it after the key event so the script has a chance to cancel it
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=27337 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27337
match keyboard_event.key { match keyboard_event.key {
Key::Character(ref letter) if letter == " " && keyboard_event.state == KeyState::Up => { Key::Character(ref letter)
if letter == " " && keyboard_event.state == KeyState::Up =>
{
let maybe_elem = target.downcast::<Element>(); let maybe_elem = target.downcast::<Element>();
if let Some(el) = maybe_elem { if let Some(el) = maybe_elem {
synthetic_click_activation( synthetic_click_activation(

View file

@ -31,9 +31,8 @@ pub struct KeyboardEvent {
modifiers: Cell<Modifiers>, modifiers: Cell<Modifiers>,
repeat: Cell<bool>, repeat: Cell<bool>,
is_composing: Cell<bool>, is_composing: Cell<bool>,
char_code: Cell<Option<u32>>, char_code: Cell<u32>,
key_code: Cell<u32>, key_code: Cell<u32>,
printable: Cell<Option<char>>,
} }
impl KeyboardEvent { impl KeyboardEvent {
@ -47,9 +46,8 @@ impl KeyboardEvent {
modifiers: Cell::new(Modifiers::empty()), modifiers: Cell::new(Modifiers::empty()),
repeat: Cell::new(false), repeat: Cell::new(false),
is_composing: Cell::new(false), is_composing: Cell::new(false),
char_code: Cell::new(None), char_code: Cell::new(0),
key_code: Cell::new(0), key_code: Cell::new(0),
printable: Cell::new(None),
} }
} }
@ -74,7 +72,7 @@ impl KeyboardEvent {
repeat: bool, repeat: bool,
is_composing: bool, is_composing: bool,
modifiers: Modifiers, modifiers: Modifiers,
char_code: Option<u32>, char_code: u32,
key_code: u32, key_code: u32,
) -> DomRoot<KeyboardEvent> { ) -> DomRoot<KeyboardEvent> {
let ev = KeyboardEvent::new_uninitialized(window); let ev = KeyboardEvent::new_uninitialized(window);
@ -121,7 +119,7 @@ impl KeyboardEvent {
init.repeat, init.repeat,
init.isComposing, init.isComposing,
modifiers, modifiers,
None, 0,
0, 0,
); );
*event.key.borrow_mut() = init.key.clone(); *event.key.borrow_mut() = init.key.clone();
@ -130,10 +128,6 @@ impl KeyboardEvent {
} }
impl KeyboardEvent { impl KeyboardEvent {
pub fn printable(&self) -> Option<char> {
self.printable.get()
}
pub fn key(&self) -> Key { pub fn key(&self) -> Key {
self.typed_key.borrow().clone() self.typed_key.borrow().clone()
} }
@ -143,54 +137,6 @@ impl KeyboardEvent {
} }
} }
// https://w3c.github.io/uievents/#legacy-key-models
pub fn key_keycode(key: &Key) -> u32 {
match key {
// https://w3c.github.io/uievents/#legacy-key-models
Key::Backspace => 8,
Key::Tab => 9,
Key::Enter => 13,
Key::Shift => 16,
Key::Control => 17,
Key::Alt => 18,
Key::CapsLock => 20,
Key::Escape => 27,
Key::PageUp => 33,
Key::PageDown => 34,
Key::End => 35,
Key::Home => 36,
Key::ArrowLeft => 37,
Key::ArrowUp => 38,
Key::ArrowRight => 39,
Key::ArrowDown => 40,
Key::Delete => 46,
Key::Character(ref c) if c.len() == 1 => match c.chars().next().unwrap() {
' ' => 32,
//§ B.2.1.3
'0'...'9' => c.as_bytes()[0] as u32,
//§ B.2.1.4
'a'...'z' => c.to_ascii_uppercase().as_bytes()[0] as u32,
'A'...'Z' => c.as_bytes()[0] as u32,
// https://w3c.github.io/uievents/#optionally-fixed-virtual-key-codes
';' | ':' => 186,
'=' | '+' => 187,
',' | '<' => 188,
'-' | '_' => 189,
'.' | '>' => 190,
'/' | '?' => 191,
'`' | '~' => 192,
'[' | '{' => 219,
'\\' | '|' => 220,
']' | '}' => 221,
'\'' | '\"' => 222,
_ => 0,
},
//§ B.2.1.8
_ => 0,
}
}
impl KeyboardEventMethods for KeyboardEvent { impl KeyboardEventMethods for KeyboardEvent {
// https://w3c.github.io/uievents/#widl-KeyboardEvent-initKeyboardEvent // https://w3c.github.io/uievents/#widl-KeyboardEvent-initKeyboardEvent
fn InitKeyboardEvent( fn InitKeyboardEvent(
@ -282,7 +228,7 @@ impl KeyboardEventMethods for KeyboardEvent {
// https://w3c.github.io/uievents/#widl-KeyboardEvent-charCode // https://w3c.github.io/uievents/#widl-KeyboardEvent-charCode
fn CharCode(&self) -> u32 { fn CharCode(&self) -> u32 {
self.char_code.get().unwrap_or(0) self.char_code.get()
} }
// https://w3c.github.io/uievents/#widl-KeyboardEvent-keyCode // https://w3c.github.io/uievents/#widl-KeyboardEvent-keyCode
@ -292,7 +238,11 @@ impl KeyboardEventMethods for KeyboardEvent {
// https://w3c.github.io/uievents/#widl-KeyboardEvent-which // https://w3c.github.io/uievents/#widl-KeyboardEvent-which
fn Which(&self) -> u32 { fn Which(&self) -> u32 {
self.char_code.get().unwrap_or(self.KeyCode()) if self.char_code.get() != 0 {
self.char_code.get()
} else {
self.key_code.get()
}
} }
// https://dom.spec.whatwg.org/#dom-event-istrusted // https://dom.spec.whatwg.org/#dom-event-istrusted

View file

@ -20,7 +20,7 @@ gfx_traits = {path = "../gfx_traits"}
hyper = "0.10" hyper = "0.10"
hyper_serde = "0.8" hyper_serde = "0.8"
ipc-channel = "0.11" ipc-channel = "0.11"
keyboard-types = {version = "0.4.0-serde", features = ["serde"]} keyboard-types = {version = "0.4.2-servo", features = ["serde"]}
libc = "0.2" libc = "0.2"
malloc_size_of = { path = "../malloc_size_of" } malloc_size_of = { path = "../malloc_size_of" }
malloc_size_of_derive = { path = "../malloc_size_of_derive" } malloc_size_of_derive = { path = "../malloc_size_of_derive" }

View file

@ -16,7 +16,7 @@ euclid = "0.19"
hyper = "0.10" hyper = "0.10"
image = "0.19" image = "0.19"
ipc-channel = "0.11" ipc-channel = "0.11"
keyboard-types = {version = "0.4.0-serde", features = ["serde"]} keyboard-types = {version = "0.4.2-servo", features = ["serde"]}
log = "0.4" log = "0.4"
msg = {path = "../msg"} msg = {path = "../msg"}
net_traits = {path = "../net_traits"} net_traits = {path = "../net_traits"}

View file

@ -40,7 +40,7 @@ crossbeam-channel = "0.2"
euclid = "0.19" euclid = "0.19"
gleam = "0.6" gleam = "0.6"
glutin = "0.18" glutin = "0.18"
keyboard-types = {version = "0.4.0-serde", features = ["serde"]} keyboard-types = {version = "0.4.2-servo", features = ["serde"]}
lazy_static = "1" lazy_static = "1"
libservo = {path = "../../components/servo"} libservo = {path = "../../components/servo"}
log = "0.4" log = "0.4"

View file

@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use keyboard_types::{Code, Key, Modifiers, Location}; use keyboard_types::{Code, Key, KeyboardEvent, KeyState, Modifiers, Location};
use winit::VirtualKeyCode; use winit::{ElementState, KeyboardInput, ModifiersState, VirtualKeyCode};
// Some shortcuts use Cmd on Mac and Control on other systems. // Some shortcuts use Cmd on Mac and Control on other systems.
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
@ -17,7 +17,7 @@ pub const CMD_OR_ALT: Modifiers = Modifiers::META;
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
pub const CMD_OR_ALT: Modifiers = Modifiers::ALT; pub const CMD_OR_ALT: Modifiers = Modifiers::ALT;
pub fn get_servo_key_from_winit_key(key: Option<VirtualKeyCode>) -> Key { fn get_servo_key_from_winit_key(key: Option<VirtualKeyCode>) -> Key {
use winit::VirtualKeyCode::*; use winit::VirtualKeyCode::*;
// TODO: figure out how to map NavigateForward, NavigateBackward // TODO: figure out how to map NavigateForward, NavigateBackward
// TODO: map the remaining keys if possible // TODO: map the remaining keys if possible
@ -126,7 +126,7 @@ pub fn get_servo_key_from_winit_key(key: Option<VirtualKeyCode>) -> Key {
} }
} }
pub fn get_servo_location_from_winit_key(key: Option<VirtualKeyCode>) -> Location { fn get_servo_location_from_winit_key(key: Option<VirtualKeyCode>) -> Location {
use winit::VirtualKeyCode::*; use winit::VirtualKeyCode::*;
// TODO: add more numpad keys // TODO: add more numpad keys
let key = if let Some(key) = key { let key = if let Some(key) = key {
@ -145,7 +145,7 @@ pub fn get_servo_location_from_winit_key(key: Option<VirtualKeyCode>) -> Locatio
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn get_servo_code_from_scancode(scancode: u32) -> Code { fn get_servo_code_from_scancode(scancode: u32) -> Code {
// TODO: Map more codes // TODO: Map more codes
use keyboard_types::Code::*; use keyboard_types::Code::*;
match scancode { match scancode {
@ -236,7 +236,32 @@ pub fn get_servo_code_from_scancode(scancode: u32) -> Code {
} }
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
pub fn get_servo_code_from_scancode(_scancode: u32) -> Code { fn get_servo_code_from_scancode(_scancode: u32) -> Code {
// TODO: Implement for Windows and Mac OS // TODO: Implement for Windows and Mac OS
Code::Unidentified Code::Unidentified
} }
fn get_modifiers(mods: ModifiersState) -> Modifiers {
let mut modifiers = Modifiers::empty();
modifiers.set(Modifiers::CONTROL, mods.ctrl);
modifiers.set(Modifiers::SHIFT, mods.shift);
modifiers.set(Modifiers::ALT, mods.alt);
modifiers.set(Modifiers::META, mods.logo);
modifiers
}
pub fn keyboard_event_from_winit(input: KeyboardInput) -> KeyboardEvent {
info!("winit keyboard input: {:?}", input);
KeyboardEvent {
state: match input.state {
ElementState::Pressed => KeyState::Down,
ElementState::Released => KeyState::Up,
},
key: get_servo_key_from_winit_key(input.virtual_keycode),
code: get_servo_code_from_scancode(input.scancode),
location: get_servo_location_from_winit_key(input.virtual_keycode),
modifiers: get_modifiers(input.modifiers),
repeat: false,
is_composing: false,
}
}

View file

@ -9,7 +9,7 @@ use euclid::{Length, TypedPoint2D, TypedVector2D, TypedScale, TypedSize2D};
use gdi32; use gdi32;
use gleam::gl; use gleam::gl;
use glutin::{Api, ContextBuilder, GlContext, GlRequest, GlWindow}; use glutin::{Api, ContextBuilder, GlContext, GlRequest, GlWindow};
use keyboard_types::{Key, KeyboardEvent, Modifiers as KeyModifiers, KeyState}; use keyboard_types::{Key, KeyboardEvent, KeyState};
#[cfg(any(target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "linux", target_os = "macos"))]
use osmesa_sys; use osmesa_sys;
use servo::compositing::windowing::{AnimationState, MouseWindowEvent, WindowEvent}; use servo::compositing::windowing::{AnimationState, MouseWindowEvent, WindowEvent};
@ -31,13 +31,13 @@ use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
use std::time; use std::time;
use super::keyutils; use super::keyutils::keyboard_event_from_winit;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use user32; use user32;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use winapi; use winapi;
use winit; use winit;
use winit::{ElementState, Event, ModifiersState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode}; use winit::{ElementState, Event, MouseButton, MouseScrollDelta, TouchPhase, KeyboardInput};
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize}; use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use winit::os::macos::{ActivationPolicy, WindowBuilderExt}; use winit::os::macos::{ActivationPolicy, WindowBuilderExt};
@ -150,7 +150,6 @@ pub struct Window {
mouse_down_point: Cell<TypedPoint2D<i32, DevicePixel>>, mouse_down_point: Cell<TypedPoint2D<i32, DevicePixel>>,
event_queue: RefCell<Vec<WindowEvent>>, event_queue: RefCell<Vec<WindowEvent>>,
mouse_pos: Cell<TypedPoint2D<i32, DevicePixel>>, mouse_pos: Cell<TypedPoint2D<i32, DevicePixel>>,
key_modifiers: Cell<KeyModifiers>,
last_pressed: Cell<Option<KeyboardEvent>>, last_pressed: Cell<Option<KeyboardEvent>>,
animation_state: Cell<AnimationState>, animation_state: Cell<AnimationState>,
fullscreen: Cell<bool>, fullscreen: Cell<bool>,
@ -276,10 +275,7 @@ impl Window {
event_queue: RefCell::new(vec![]), event_queue: RefCell::new(vec![]),
mouse_down_button: Cell::new(None), mouse_down_button: Cell::new(None),
mouse_down_point: Cell::new(TypedPoint2D::new(0, 0)), mouse_down_point: Cell::new(TypedPoint2D::new(0, 0)),
mouse_pos: Cell::new(TypedPoint2D::new(0, 0)), mouse_pos: Cell::new(TypedPoint2D::new(0, 0)),
key_modifiers: Cell::new(KeyModifiers::empty()),
last_pressed: Cell::new(None), last_pressed: Cell::new(None),
gl: gl.clone(), gl: gl.clone(),
animation_state: Cell::new(AnimationState::Idle), animation_state: Cell::new(AnimationState::Idle),
@ -419,8 +415,15 @@ impl Window {
} }
let mut event = if let Some(event) = self.last_pressed.replace(None) { let mut event = if let Some(event) = self.last_pressed.replace(None) {
event event
} else if ch.is_ascii() {
// Some keys like Backspace emit a control character in winit
// but they are already dealt with in handle_keyboard_input
// so just ignore the character.
return
} else { } else {
return; // For combined characters like the letter e with an acute accent
// no keyboard event is emitted. A dummy event is created in this case.
KeyboardEvent::default()
}; };
event.key = Key::Character(ch.to_string()); event.key = Key::Character(ch.to_string());
self.event_queue self.event_queue
@ -428,44 +431,15 @@ impl Window {
.push(WindowEvent::Keyboard(event)); .push(WindowEvent::Keyboard(event));
} }
fn toggle_keyboard_modifiers(&self, mods: ModifiersState) {
self.toggle_modifier(KeyModifiers::CONTROL, mods.ctrl);
self.toggle_modifier(KeyModifiers::SHIFT, mods.shift);
self.toggle_modifier(KeyModifiers::ALT, mods.alt);
self.toggle_modifier(KeyModifiers::META, mods.logo);
}
fn handle_keyboard_input( fn handle_keyboard_input(
&self, &self,
scancode: u32, input: KeyboardInput,
element_state: ElementState,
code: Option<VirtualKeyCode>,
mods: ModifiersState,
) { ) {
info!( let event = keyboard_event_from_winit(input);
"winit keyboard input: {}, {:?}, {:?}, {:?}", if event.state == KeyState::Down && event.key == Key::Unidentified {
scancode, element_state, code, mods
);
self.toggle_keyboard_modifiers(mods);
let key = keyutils::get_servo_key_from_winit_key(code);
let location = keyutils::get_servo_location_from_winit_key(code);
let state = match element_state {
ElementState::Pressed => KeyState::Down,
ElementState::Released => KeyState::Up,
};
let event = KeyboardEvent {
state,
key: key.clone(),
code: keyutils::get_servo_code_from_scancode(scancode),
location,
modifiers: self.key_modifiers.get(),
repeat: false,
is_composing: false,
};
if element_state == ElementState::Pressed && key == Key::Unidentified {
// If pressed and probably printable, we expect a ReceivedCharacter event. // If pressed and probably printable, we expect a ReceivedCharacter event.
self.last_pressed.set(Some(event)); self.last_pressed.set(Some(event));
} else if key != Key::Unidentified { } else if event.key != Key::Unidentified {
self.last_pressed.set(None); self.last_pressed.set(None);
self.event_queue self.event_queue
.borrow_mut() .borrow_mut()
@ -482,17 +456,11 @@ impl Window {
Event::WindowEvent { Event::WindowEvent {
event: event:
winit::WindowEvent::KeyboardInput { winit::WindowEvent::KeyboardInput {
input: input,
winit::KeyboardInput {
scancode,
state,
virtual_keycode,
modifiers,
},
.. ..
}, },
.. ..
} => self.handle_keyboard_input(scancode, state, virtual_keycode, modifiers), } => self.handle_keyboard_input(input),
Event::WindowEvent { Event::WindowEvent {
event: winit::WindowEvent::MouseInput { state, button, .. }, event: winit::WindowEvent::MouseInput { state, button, .. },
.. ..
@ -596,16 +564,6 @@ impl Window {
} }
} }
fn toggle_modifier(&self, modifier: KeyModifiers, pressed: bool) {
let mut modifiers = self.key_modifiers.get();
if pressed {
modifiers.insert(modifier);
} else {
modifiers.remove(modifier);
}
self.key_modifiers.set(modifiers);
}
/// Helper function to handle a click /// Helper function to handle a click
fn handle_mouse( fn handle_mouse(
&self, &self,

View file

@ -10,6 +10,6 @@ path = "lib.rs"
[dependencies] [dependencies]
euclid = "0.19" euclid = "0.19"
keyboard-types = "0.4.0-servo" keyboard-types = "0.4.2-servo"
script = {path = "../../../components/script"} script = {path = "../../../components/script"}
servo_url = {path = "../../../components/url"} servo_url = {path = "../../../components/url"}