Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Unified Diff: tools/dom/src/KeyboardEventStream.dart

Issue 26334002: "Reverting 28321" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/dom/src/KeyName.dart ('k') | tools/dom/src/dart2js_KeyEvent.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/dom/src/KeyboardEventStream.dart
diff --git a/tools/dom/src/KeyboardEventStream.dart b/tools/dom/src/KeyboardEventStream.dart
index b7e16072246b552f464c01e6e477bdf4143ba7a0..4d01f614e4945433684e192d290ac6b9a6ec5ca8 100644
--- a/tools/dom/src/KeyboardEventStream.dart
+++ b/tools/dom/src/KeyboardEventStream.dart
@@ -27,8 +27,8 @@ class _KeyboardEventHandler extends EventStreamProvider<KeyEvent> {
// The distance to shift from upper case alphabet Roman letters to lower case.
static final int _ROMAN_ALPHABET_OFFSET = "a".codeUnits[0] - "A".codeUnits[0];
- /** Custom Stream (Controller) to produce KeyEvents for the stream. */
- _CustomKeyEventStreamImpl _stream;
+ /** Controller to produce KeyEvents for the stream. */
+ final StreamController _controller = new StreamController(sync: true);
static const _EVENT_TYPE = 'KeyEvent';
@@ -64,33 +64,56 @@ class _KeyboardEventHandler extends EventStreamProvider<KeyEvent> {
};
/** Return a stream for KeyEvents for the specified target. */
- // Note: this actually functions like a factory constructor.
- CustomStream<KeyEvent> forTarget(EventTarget e, {bool useCapture: false}) {
- var handler = new _KeyboardEventHandler.initializeAllEventListeners(
- _type, e);
- return handler._stream;
+ Stream<KeyEvent> forTarget(EventTarget e, {bool useCapture: false}) {
+ return new _KeyboardEventHandler.initializeAllEventListeners(
+ _type, e).stream;
+ }
+
+ /**
+ * Accessor to the stream associated with a particular KeyboardEvent
+ * EventTarget.
+ *
+ * [forTarget] must be called to initialize this stream to listen to a
+ * particular EventTarget.
+ */
+ Stream<KeyEvent> get stream {
+ if(_target != null) {
+ return _controller.stream;
+ } else {
+ throw new StateError("Not initialized. Call forTarget to access a stream "
+ "initialized with a particular EventTarget.");
+ }
}
/**
* General constructor, performs basic initialization for our improved
* KeyboardEvent controller.
*/
- _KeyboardEventHandler(this._type): super(_EVENT_TYPE),
- _stream = new _CustomKeyEventStreamImpl('event');
+ _KeyboardEventHandler(this._type) :
+ _target = null, super(_EVENT_TYPE) {
+ }
/**
* Hook up all event listeners under the covers so we can estimate keycodes
* and charcodes when they are not provided.
*/
_KeyboardEventHandler.initializeAllEventListeners(this._type, this._target) :
- super(_EVENT_TYPE) {
+ super(_EVENT_TYPE) {
Element.keyDownEvent.forTarget(_target, useCapture: true).listen(
processKeyDown);
Element.keyPressEvent.forTarget(_target, useCapture: true).listen(
processKeyPress);
Element.keyUpEvent.forTarget(_target, useCapture: true).listen(
processKeyUp);
- _stream = new _CustomKeyEventStreamImpl(_type);
+ }
+
+ /**
+ * Notify all callback listeners that a KeyEvent of the relevant type has
+ * occurred.
+ */
+ bool _dispatch(KeyEvent event) {
+ if (event.type == _type)
+ _controller.add(event);
}
/** Determine if caps lock is one of the currently depressed keys. */
@@ -281,7 +304,7 @@ class _KeyboardEventHandler extends EventStreamProvider<KeyEvent> {
_keyDownList.clear();
}
- var event = new KeyEvent.wrap(e);
+ var event = new KeyEvent(e);
event._shadowKeyCode = _normalizeKeyCodes(event);
// Technically a "keydown" event doesn't have a charCode. This is
// calculated nonetheless to provide us with more information in giving
@@ -295,12 +318,12 @@ class _KeyboardEventHandler extends EventStreamProvider<KeyEvent> {
processKeyPress(e);
}
_keyDownList.add(event);
- _stream.add(event);
+ _dispatch(event);
}
/** Handle keypress events. */
void processKeyPress(KeyboardEvent event) {
- var e = new KeyEvent.wrap(event);
+ var e = new KeyEvent(event);
// IE reports the character code in the keyCode field for keypress events.
// There are two exceptions however, Enter and Escape.
if (Device.isIE) {
@@ -325,12 +348,12 @@ class _KeyboardEventHandler extends EventStreamProvider<KeyEvent> {
e._shadowKeyCode = _keyIdentifier[e._shadowKeyIdentifier];
}
e._shadowAltKey = _keyDownList.any((var element) => element.altKey);
- _stream.add(e);
+ _dispatch(e);
}
/** Handle keyup events. */
void processKeyUp(KeyboardEvent event) {
- var e = new KeyEvent.wrap(event);
+ var e = new KeyEvent(event);
KeyboardEvent toRemove = null;
for (var key in _keyDownList) {
if (key.keyCode == e.keyCode) {
@@ -345,7 +368,7 @@ class _KeyboardEventHandler extends EventStreamProvider<KeyEvent> {
// inconsistencies. Filing bugs on when this is reached is welcome!
_keyDownList.removeLast();
}
- _stream.add(e);
+ _dispatch(e);
}
}
@@ -368,14 +391,14 @@ class _KeyboardEventHandler extends EventStreamProvider<KeyEvent> {
class KeyboardEventStream {
/** Named constructor to produce a stream for onKeyPress events. */
- static CustomStream<KeyEvent> onKeyPress(EventTarget target) =>
+ static Stream<KeyEvent> onKeyPress(EventTarget target) =>
new _KeyboardEventHandler('keypress').forTarget(target);
/** Named constructor to produce a stream for onKeyUp events. */
- static CustomStream<KeyEvent> onKeyUp(EventTarget target) =>
+ static Stream<KeyEvent> onKeyUp(EventTarget target) =>
new _KeyboardEventHandler('keyup').forTarget(target);
/** Named constructor to produce a stream for onKeyDown events. */
- static CustomStream<KeyEvent> onKeyDown(EventTarget target) =>
- new _KeyboardEventHandler('keydown').forTarget(target);
+ static Stream<KeyEvent> onKeyDown(EventTarget target) =>
+ new _KeyboardEventHandler('keydown').forTarget(target);
}
« no previous file with comments | « tools/dom/src/KeyName.dart ('k') | tools/dom/src/dart2js_KeyEvent.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698