diff options
-rw-r--r-- | Cargo.lock | 22 | ||||
-rw-r--r-- | components/compositing/Cargo.toml | 2 | ||||
-rw-r--r-- | components/constellation/Cargo.toml | 2 | ||||
-rw-r--r-- | components/embedder_traits/Cargo.toml | 2 | ||||
-rw-r--r-- | components/malloc_size_of/Cargo.toml | 2 | ||||
-rw-r--r-- | components/script/Cargo.toml | 2 | ||||
-rw-r--r-- | components/script/dom/document.rs | 57 | ||||
-rw-r--r-- | components/script/dom/keyboardevent.rs | 70 | ||||
-rw-r--r-- | components/script_traits/Cargo.toml | 2 | ||||
-rw-r--r-- | components/webdriver_server/Cargo.toml | 2 | ||||
-rw-r--r-- | ports/servo/Cargo.toml | 2 | ||||
-rw-r--r-- | ports/servo/glutin_app/keyutils.rs | 37 | ||||
-rw-r--r-- | ports/servo/glutin_app/window.rs | 76 | ||||
-rw-r--r-- | tests/unit/script/Cargo.toml | 2 |
14 files changed, 98 insertions, 182 deletions
diff --git a/Cargo.lock b/Cargo.lock index 7c2eb5a4c63..9a7388d7656 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -512,7 +512,7 @@ dependencies = [ "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)", "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)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "msg 0.0.1", @@ -549,7 +549,7 @@ dependencies = [ "gfx_traits 0.0.1", "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)", - "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", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "metrics 0.0.1", @@ -902,7 +902,7 @@ name = "embedder_traits" version = "0.0.1" dependencies = [ "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)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "msg 0.0.1", @@ -1736,7 +1736,7 @@ dependencies = [ [[package]] name = "keyboard-types" -version = "0.4.0-servo" +version = "0.4.2-servo" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2030,7 +2030,7 @@ dependencies = [ "hashglobe 0.1.0", "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)", - "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)", "selectors 0.20.0", "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)", "itertools 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", @@ -3124,7 +3124,7 @@ name = "script_tests" version = "0.0.1" dependencies = [ "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", "servo_url 0.0.1", ] @@ -3143,7 +3143,7 @@ dependencies = [ "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)", "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)", "malloc_size_of 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)", "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)", - "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)", "libservo 0.0.1", "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)", "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)", - "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)", "msg 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 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 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 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" diff --git a/components/compositing/Cargo.toml b/components/compositing/Cargo.toml index 994599ed095..e8e4b092bda 100644 --- a/components/compositing/Cargo.toml +++ b/components/compositing/Cargo.toml @@ -21,7 +21,7 @@ gleam = {version = "0.6", optional = true} image = "0.19" ipc-channel = "0.11" libc = "0.2" -keyboard-types = {version = "0.4.0-serde", features = ["serde"]} +keyboard-types = {version = "0.4.2-servo", features = ["serde"]} log = "0.4" msg = {path = "../msg"} net_traits = {path = "../net_traits"} diff --git a/components/constellation/Cargo.toml b/components/constellation/Cargo.toml index d3569f3b7fe..21da4b05300 100644 --- a/components/constellation/Cargo.toml +++ b/components/constellation/Cargo.toml @@ -25,7 +25,7 @@ gfx_traits = {path = "../gfx_traits"} hyper = "0.10" ipc-channel = "0.11" 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" metrics = {path = "../metrics"} msg = {path = "../msg"} diff --git a/components/embedder_traits/Cargo.toml b/components/embedder_traits/Cargo.toml index e125376e8c2..2eeae19b352 100644 --- a/components/embedder_traits/Cargo.toml +++ b/components/embedder_traits/Cargo.toml @@ -14,7 +14,7 @@ tests = [] [dependencies] 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" log = "0.4" msg = {path = "../msg"} diff --git a/components/malloc_size_of/Cargo.toml b/components/malloc_size_of/Cargo.toml index 6907118f58a..e982925e57a 100644 --- a/components/malloc_size_of/Cargo.toml +++ b/components/malloc_size_of/Cargo.toml @@ -31,7 +31,7 @@ euclid = "0.19" hashglobe = { path = "../hashglobe" } hyper = { version = "0.10", 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 } selectors = { path = "../selectors" } serde = { version = "1.0.27", optional = true } diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index 88353ab9023..a2f974c0a79 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -59,7 +59,7 @@ image = "0.19" ipc-channel = "0.11" itertools = "0.7.6" 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" libc = "0.2" log = "0.4" diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 72d74b61ae4..ec34a39dba4 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -61,7 +61,7 @@ use dom::htmlimageelement::HTMLImageElement; use dom::htmlmetaelement::HTMLMetaElement; use dom::htmlscriptelement::{HTMLScriptElement, ScriptResult}; use dom::htmltitleelement::HTMLTitleElement; -use dom::keyboardevent::{KeyboardEvent, key_keycode}; +use dom::keyboardevent::KeyboardEvent; use dom::location::Location; use dom::messageevent::MessageEvent; 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 2: Simply use None to indicate the top of the document. // Step 3 & 4 - percent_decode(fragid.as_bytes()).decode_utf8().ok() - // Step 5 + percent_decode(fragid.as_bytes()) + .decode_utf8() + .ok() + // Step 5 .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)) // Step 7 & 8 } @@ -806,7 +808,8 @@ impl Document { rect.origin.x.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") { // FIXME(stshine): this should be the origin of the stacking context space, // 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 - pub fn dispatch_key_event( - &self, - keyboard_event: ::keyboard_types::KeyboardEvent, - ) { + pub fn dispatch_key_event(&self, keyboard_event: ::keyboard_types::KeyboardEvent) { let focused = self.get_focused_element(); let body = self.GetBody(); - let printable = match keyboard_event.key { - Key::Character(_) => true, - _ => false, - }; - let target = match (&focused, &body) { (&Some(ref focused), _) => focused.upcast(), (&None, &Some(ref body)) => body.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( &self.window, - ev_type, + DOMString::from(keyboard_event.state.to_string()), true, true, Some(&self.window), @@ -1397,15 +1375,18 @@ impl Document { keyboard_event.repeat, keyboard_event.is_composing, keyboard_event.modifiers, - char_code, - key_keycode(&keyboard_event.key), + 0, + keyboard_event.key.legacy_keycode(), ); let event = keyevent.upcast::<Event>(); event.fire(target); let mut cancel_state = event.get_cancel_state(); // 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 let event = KeyboardEvent::new( &self.window, @@ -1420,7 +1401,7 @@ impl Document { keyboard_event.repeat, keyboard_event.is_composing, keyboard_event.modifiers, - None, + keyboard_event.key.legacy_charcode(), 0, ); 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 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27337 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>(); if let Some(el) = maybe_elem { synthetic_click_activation( diff --git a/components/script/dom/keyboardevent.rs b/components/script/dom/keyboardevent.rs index 46345ea739e..5f755e98369 100644 --- a/components/script/dom/keyboardevent.rs +++ b/components/script/dom/keyboardevent.rs @@ -31,9 +31,8 @@ pub struct KeyboardEvent { modifiers: Cell<Modifiers>, repeat: Cell<bool>, is_composing: Cell<bool>, - char_code: Cell<Option<u32>>, + char_code: Cell<u32>, key_code: Cell<u32>, - printable: Cell<Option<char>>, } impl KeyboardEvent { @@ -47,9 +46,8 @@ impl KeyboardEvent { modifiers: Cell::new(Modifiers::empty()), repeat: Cell::new(false), is_composing: Cell::new(false), - char_code: Cell::new(None), + char_code: Cell::new(0), key_code: Cell::new(0), - printable: Cell::new(None), } } @@ -74,7 +72,7 @@ impl KeyboardEvent { repeat: bool, is_composing: bool, modifiers: Modifiers, - char_code: Option<u32>, + char_code: u32, key_code: u32, ) -> DomRoot<KeyboardEvent> { let ev = KeyboardEvent::new_uninitialized(window); @@ -121,7 +119,7 @@ impl KeyboardEvent { init.repeat, init.isComposing, modifiers, - None, + 0, 0, ); *event.key.borrow_mut() = init.key.clone(); @@ -130,10 +128,6 @@ impl KeyboardEvent { } impl KeyboardEvent { - pub fn printable(&self) -> Option<char> { - self.printable.get() - } - pub fn key(&self) -> Key { 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 { // https://w3c.github.io/uievents/#widl-KeyboardEvent-initKeyboardEvent fn InitKeyboardEvent( @@ -282,7 +228,7 @@ impl KeyboardEventMethods for KeyboardEvent { // https://w3c.github.io/uievents/#widl-KeyboardEvent-charCode fn CharCode(&self) -> u32 { - self.char_code.get().unwrap_or(0) + self.char_code.get() } // https://w3c.github.io/uievents/#widl-KeyboardEvent-keyCode @@ -292,7 +238,11 @@ impl KeyboardEventMethods for KeyboardEvent { // https://w3c.github.io/uievents/#widl-KeyboardEvent-which 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 diff --git a/components/script_traits/Cargo.toml b/components/script_traits/Cargo.toml index 9b47eb9a7b2..1fbf4004377 100644 --- a/components/script_traits/Cargo.toml +++ b/components/script_traits/Cargo.toml @@ -20,7 +20,7 @@ gfx_traits = {path = "../gfx_traits"} hyper = "0.10" hyper_serde = "0.8" 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" malloc_size_of = { path = "../malloc_size_of" } malloc_size_of_derive = { path = "../malloc_size_of_derive" } diff --git a/components/webdriver_server/Cargo.toml b/components/webdriver_server/Cargo.toml index 3cbe306cbfa..d5f4c6fd76c 100644 --- a/components/webdriver_server/Cargo.toml +++ b/components/webdriver_server/Cargo.toml @@ -16,7 +16,7 @@ euclid = "0.19" hyper = "0.10" image = "0.19" 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" msg = {path = "../msg"} net_traits = {path = "../net_traits"} diff --git a/ports/servo/Cargo.toml b/ports/servo/Cargo.toml index f26d470ef9e..335a2a8a5b0 100644 --- a/ports/servo/Cargo.toml +++ b/ports/servo/Cargo.toml @@ -40,7 +40,7 @@ crossbeam-channel = "0.2" euclid = "0.19" gleam = "0.6" glutin = "0.18" -keyboard-types = {version = "0.4.0-serde", features = ["serde"]} +keyboard-types = {version = "0.4.2-servo", features = ["serde"]} lazy_static = "1" libservo = {path = "../../components/servo"} log = "0.4" diff --git a/ports/servo/glutin_app/keyutils.rs b/ports/servo/glutin_app/keyutils.rs index a1700e71f23..08193afc0a0 100644 --- a/ports/servo/glutin_app/keyutils.rs +++ b/ports/servo/glutin_app/keyutils.rs @@ -2,8 +2,8 @@ * 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/. */ -use keyboard_types::{Code, Key, Modifiers, Location}; -use winit::VirtualKeyCode; +use keyboard_types::{Code, Key, KeyboardEvent, KeyState, Modifiers, Location}; +use winit::{ElementState, KeyboardInput, ModifiersState, VirtualKeyCode}; // Some shortcuts use Cmd on Mac and Control on other systems. #[cfg(target_os = "macos")] @@ -17,7 +17,7 @@ pub const CMD_OR_ALT: Modifiers = Modifiers::META; #[cfg(not(target_os = "macos"))] 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::*; // TODO: figure out how to map NavigateForward, NavigateBackward // 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::*; // TODO: add more numpad keys 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")] -pub fn get_servo_code_from_scancode(scancode: u32) -> Code { +fn get_servo_code_from_scancode(scancode: u32) -> Code { // TODO: Map more codes use keyboard_types::Code::*; match scancode { @@ -236,7 +236,32 @@ pub fn get_servo_code_from_scancode(scancode: u32) -> Code { } #[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 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, + } +} diff --git a/ports/servo/glutin_app/window.rs b/ports/servo/glutin_app/window.rs index c47d313cc9b..3c1680645cb 100644 --- a/ports/servo/glutin_app/window.rs +++ b/ports/servo/glutin_app/window.rs @@ -9,7 +9,7 @@ use euclid::{Length, TypedPoint2D, TypedVector2D, TypedScale, TypedSize2D}; use gdi32; use gleam::gl; 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"))] use osmesa_sys; use servo::compositing::windowing::{AnimationState, MouseWindowEvent, WindowEvent}; @@ -31,13 +31,13 @@ use std::rc::Rc; use std::sync::Arc; use std::thread; use std::time; -use super::keyutils; +use super::keyutils::keyboard_event_from_winit; #[cfg(target_os = "windows")] use user32; #[cfg(target_os = "windows")] use winapi; 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}; #[cfg(target_os = "macos")] use winit::os::macos::{ActivationPolicy, WindowBuilderExt}; @@ -150,7 +150,6 @@ pub struct Window { mouse_down_point: Cell<TypedPoint2D<i32, DevicePixel>>, event_queue: RefCell<Vec<WindowEvent>>, mouse_pos: Cell<TypedPoint2D<i32, DevicePixel>>, - key_modifiers: Cell<KeyModifiers>, last_pressed: Cell<Option<KeyboardEvent>>, animation_state: Cell<AnimationState>, fullscreen: Cell<bool>, @@ -276,10 +275,7 @@ impl Window { event_queue: RefCell::new(vec![]), mouse_down_button: Cell::new(None), mouse_down_point: 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), gl: gl.clone(), animation_state: Cell::new(AnimationState::Idle), @@ -419,8 +415,15 @@ impl Window { } let mut event = if let Some(event) = self.last_pressed.replace(None) { 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 { - 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()); self.event_queue @@ -428,44 +431,15 @@ impl Window { .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( &self, - scancode: u32, - element_state: ElementState, - code: Option<VirtualKeyCode>, - mods: ModifiersState, + input: KeyboardInput, ) { - info!( - "winit keyboard input: {}, {:?}, {:?}, {:?}", - 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 { + let event = keyboard_event_from_winit(input); + if event.state == KeyState::Down && event.key == Key::Unidentified { // If pressed and probably printable, we expect a ReceivedCharacter event. self.last_pressed.set(Some(event)); - } else if key != Key::Unidentified { + } else if event.key != Key::Unidentified { self.last_pressed.set(None); self.event_queue .borrow_mut() @@ -482,17 +456,11 @@ impl Window { Event::WindowEvent { event: winit::WindowEvent::KeyboardInput { - input: - winit::KeyboardInput { - scancode, - state, - virtual_keycode, - modifiers, - }, + input, .. }, .. - } => self.handle_keyboard_input(scancode, state, virtual_keycode, modifiers), + } => self.handle_keyboard_input(input), Event::WindowEvent { 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 fn handle_mouse( &self, diff --git a/tests/unit/script/Cargo.toml b/tests/unit/script/Cargo.toml index a0d1418fb84..4a5d7f346b9 100644 --- a/tests/unit/script/Cargo.toml +++ b/tests/unit/script/Cargo.toml @@ -10,6 +10,6 @@ path = "lib.rs" [dependencies] euclid = "0.19" -keyboard-types = "0.4.0-servo" +keyboard-types = "0.4.2-servo" script = {path = "../../../components/script"} servo_url = {path = "../../../components/url"} |