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

Implement single-line text input #3585

Merged
merged 10 commits into from Nov 13, 2014

Fill in KeyboardEvent.

  • Loading branch information
jdm committed Nov 13, 2014
commit e999843183f27286337705a032ed3d88d361b372
@@ -4,19 +4,34 @@

use dom::bindings::codegen::Bindings::KeyboardEventBinding;
use dom::bindings::codegen::Bindings::KeyboardEventBinding::KeyboardEventMethods;
use dom::bindings::codegen::InheritTypes::KeyboardEventDerived;
use dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods;
use dom::bindings::codegen::InheritTypes::{UIEventCast, KeyboardEventDerived};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector/*, reflect_dom_object*/};
use dom::bindings::global;
use dom::bindings::js::{JSRef, Temporary, RootedReference};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::event::{Event, KeyboardEventTypeId};
use dom::uievent::UIEvent;
use dom::window::Window;
use servo_util::str::DOMString;
use std::cell::{RefCell, Cell};

#[jstraceable]
#[must_root]
pub struct KeyboardEvent {
uievent: UIEvent,
key: RefCell<DOMString>,
code: RefCell<DOMString>,
location: Cell<u32>,
ctrl: Cell<bool>,
alt: Cell<bool>,
shift: Cell<bool>,
meta: Cell<bool>,
repeat: Cell<bool>,
is_composing: Cell<bool>,
char_code: Cell<Option<u32>>,
key_code: Cell<u32>,
}

impl KeyboardEventDerived for Event {
@@ -26,14 +41,152 @@ impl KeyboardEventDerived for Event {
}

impl KeyboardEvent {
pub fn Constructor(_global: &GlobalRef,
_type_: DOMString,
_init: &KeyboardEventBinding::KeyboardEventInit) -> Fallible<Temporary<KeyboardEvent>> {
fail!()
fn new_inherited() -> KeyboardEvent {
KeyboardEvent {
uievent: UIEvent::new_inherited(KeyboardEventTypeId),
key: RefCell::new("".to_string()),
code: RefCell::new("".to_string()),
location: Cell::new(0),
ctrl: Cell::new(false),
alt: Cell::new(false),
shift: Cell::new(false),
meta: Cell::new(false),
repeat: Cell::new(false),
is_composing: Cell::new(false),
char_code: Cell::new(None),
key_code: Cell::new(0),
}
}

fn new_uninitialized(window: JSRef<Window>) -> Temporary<KeyboardEvent> {
reflect_dom_object(box KeyboardEvent::new_inherited(),
&global::Window(window),
KeyboardEventBinding::Wrap)
}

pub fn new(window: JSRef<Window>,
type_: DOMString,
canBubble: bool,
cancelable: bool,
view: Option<JSRef<Window>>,
_detail: i32,
key: DOMString,
code: DOMString,
location: u32,
repeat: bool,
isComposing: bool,
ctrlKey: bool,
altKey: bool,
shiftKey: bool,
metaKey: bool,
char_code: Option<u32>,
key_code: u32) -> Temporary<KeyboardEvent> {
let ev = KeyboardEvent::new_uninitialized(window).root();
ev.deref().InitKeyboardEvent(type_, canBubble, cancelable, view, key, location,
"".to_string(), repeat, "".to_string());
*ev.code.borrow_mut() = code;
ev.ctrl.set(ctrlKey);
ev.alt.set(altKey);
ev.shift.set(shiftKey);
ev.meta.set(metaKey);
ev.char_code.set(char_code);
ev.key_code.set(key_code);
ev.is_composing.set(isComposing);
Temporary::from_rooted(*ev)
}

pub fn Constructor(global: &GlobalRef,
type_: DOMString,
init: &KeyboardEventBinding::KeyboardEventInit) -> Fallible<Temporary<KeyboardEvent>> {
let event = KeyboardEvent::new(global.as_window(), type_,
init.parent.parent.parent.bubbles,
init.parent.parent.parent.cancelable,
init.parent.parent.view.root_ref(),
init.parent.parent.detail,
init.key.clone(), init.code.clone(), init.location,
init.repeat, init.isComposing, init.parent.ctrlKey,
init.parent.altKey, init.parent.shiftKey, init.parent.metaKey,
None, 0);
Ok(event)
}
}

impl<'a> KeyboardEventMethods for JSRef<'a, KeyboardEvent> {
fn InitKeyboardEvent(self,
typeArg: DOMString,
canBubbleArg: bool,
cancelableArg: bool,
viewArg: Option<JSRef<Window>>,
keyArg: DOMString,
locationArg: u32,
_modifiersListArg: DOMString,
repeat: bool,
_locale: DOMString) {
let uievent: JSRef<UIEvent> = UIEventCast::from_ref(self);
uievent.InitUIEvent(typeArg, canBubbleArg, cancelableArg, viewArg, 0);
*self.key.borrow_mut() = keyArg;
self.location.set(locationArg);
self.repeat.set(repeat);
}

fn Key(self) -> DOMString {
self.key.borrow().clone()
}

fn Code(self) -> DOMString {
self.code.borrow().clone()
}

fn Location(self) -> u32 {
self.location.get()
}

fn CtrlKey(self) -> bool {
self.ctrl.get()
}

fn ShiftKey(self) -> bool {
self.shift.get()
}

fn AltKey(self) -> bool {
self.alt.get()
}

fn MetaKey(self) -> bool {
self.meta.get()
}

fn Repeat(self) -> bool {
self.repeat.get()
}

fn IsComposing(self) -> bool {
self.is_composing.get()
}

fn GetModifierState(self, keyArg: DOMString) -> bool {
match keyArg.as_slice() {
"Ctrl" => self.CtrlKey(),
"Alt" => self.AltKey(),
"Shift" => self.ShiftKey(),
"Meta" => self.MetaKey(),
"AltGraph" | "CapsLock" | "NumLock" | "ScrollLock" => false, //FIXME
_ => false,
}
}

fn CharCode(self) -> u32 {
self.char_code.get().unwrap_or(0)
}

fn KeyCode(self) -> u32 {
self.key_code.get()
}

fn Which(self) -> u32 {
self.char_code.get().unwrap_or(self.KeyCode())
}
}

impl Reflectable for KeyboardEvent {
@@ -175,7 +175,6 @@ impl<'a> MouseEventMethods for JSRef<'a, MouseEvent> {
}
}


impl Reflectable for MouseEvent {
fn reflector<'a>(&'a self) -> &'a Reflector {
self.uievent.reflector()
@@ -15,22 +15,44 @@ interface KeyboardEvent : UIEvent {
const unsigned long DOM_KEY_LOCATION_LEFT = 0x01;
const unsigned long DOM_KEY_LOCATION_RIGHT = 0x02;
const unsigned long DOM_KEY_LOCATION_NUMPAD = 0x03;
//readonly attribute DOMString key;
//readonly attribute DOMString code;
//readonly attribute unsigned long location;
//readonly attribute boolean ctrlKey;
//readonly attribute boolean shiftKey;
//readonly attribute boolean altKey;
//readonly attribute boolean metaKey;
//readonly attribute boolean repeat;
//readonly attribute boolean isComposing;
//boolean getModifierState (DOMString keyArg);
readonly attribute DOMString key;
readonly attribute DOMString code;
readonly attribute unsigned long location;
readonly attribute boolean ctrlKey;
readonly attribute boolean shiftKey;
readonly attribute boolean altKey;
readonly attribute boolean metaKey;
readonly attribute boolean repeat;
readonly attribute boolean isComposing;
boolean getModifierState (DOMString keyArg);
};

// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#idl-interface-KeyboardEvent-initializers
partial interface KeyboardEvent {
// Originally introduced (and deprecated) in DOM Level 3
void initKeyboardEvent (DOMString typeArg, boolean bubblesArg, boolean cancelableArg, Window? viewArg, DOMString keyArg, unsigned long locationArg, DOMString modifiersListArg, boolean repeat, DOMString locale);
};

// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#KeyboardEvent-supplemental-interface
partial interface KeyboardEvent {
// The following support legacy user agents
readonly attribute unsigned long charCode;
readonly attribute unsigned long keyCode;
readonly attribute unsigned long which;
};

// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#interface-KeyboardEvent
dictionary KeyboardEventInit : SharedKeyboardAndMouseEventInit {
DOMString key = "";
DOMString code = "";
unsigned long location = 0;
boolean repeat = false;
boolean isComposing = false;
};

// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#events-KeyboardEventInit-supplemental
/*partial dictionary KeyboardEventInit {
unsigned long charCode = 0;
unsigned long keyCode = 0;
unsigned long which = 0;
};*/
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.