Skip to content

Commit

Permalink
Minor keyboard updates
Browse files Browse the repository at this point in the history
Support combined character input from winit.
Make use of utility methods from keyboard-types.
Remove prinatable attribute of KeyboardEvent.
  • Loading branch information
pyfisch committed Oct 13, 2018
1 parent 0ccaa7e commit c861942
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 182 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/compositing/Cargo.toml
Expand Up @@ -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"}
Expand Down
2 changes: 1 addition & 1 deletion components/constellation/Cargo.toml
Expand Up @@ -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"}
Expand Down
2 changes: 1 addition & 1 deletion components/embedder_traits/Cargo.toml
Expand Up @@ -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"}
Expand Down
2 changes: 1 addition & 1 deletion components/malloc_size_of/Cargo.toml
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion components/script/Cargo.toml
Expand Up @@ -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"
Expand Down
57 changes: 20 additions & 37 deletions components/script/dom/document.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand All @@ -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>();
Expand All @@ -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(
Expand Down
70 changes: 10 additions & 60 deletions components/script/dom/keyboardevent.rs
Expand Up @@ -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 {
Expand All @@ -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),
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -121,7 +119,7 @@ impl KeyboardEvent {
init.repeat,
init.isComposing,
modifiers,
None,
0,
0,
);
*event.key.borrow_mut() = init.key.clone();
Expand All @@ -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()
}
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c861942

Please sign in to comment.