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

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

Issue 12670017: Incorporate Pete's comments on KeyboardEventStream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 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 | « tests/html/keyboard_event_test.dart ('k') | no next file » | 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 50ebeae8e9f2a701a8b5ef5d63c0cb7d755159fd..5495d7f3e34b75815b09e097329e6fadddc8216e 100644
--- a/tools/dom/src/KeyboardEventStream.dart
+++ b/tools/dom/src/KeyboardEventStream.dart
@@ -33,6 +33,8 @@ class _KeyboardEventHandler implements EventStreamProvider<KeyEvent> {
/** Controller to produce KeyEvents for the stream. */
StreamController _controller;
+ String _eventType = 'KeyEvent';
+
/**
* An enumeration of key identifiers currently part of the W3C draft for DOM3
* and their mappings to keyCodes.
@@ -68,12 +70,29 @@ class _KeyboardEventHandler implements EventStreamProvider<KeyEvent> {
* Gets the type of the event which this would listen for on the specified
* event target.
*/
- String getEventType(EventTarget target) => 'KeyEvent';
+ String getEventType(EventTarget target) => _eventType;
/** Return a stream for KeyEvents for the specified target. */
Stream<KeyEvent> forTarget(EventTarget e, {bool useCapture: false}) {
- _initializeAllEventListeners(e);
- return _controller.stream;
+ var newController = new _KeyboardEventHandler.initializeAllEventListeners(
Andrei Mouravski 2013/03/27 00:03:35 I'd inline this.
+ _type, e);
+ return newController.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.");
+ }
}
/**
@@ -81,16 +100,23 @@ class _KeyboardEventHandler implements EventStreamProvider<KeyEvent> {
* KeyboardEvent controller.
*/
_KeyboardEventHandler(String type) {
+ _commonInit(type);
+ }
+
+ void _commonInit(String type) {
_type = type;
_controller = new StreamController.broadcast();
_callbacks = [];
+ _target = null;
}
/**
* Hook up all event listeners under the covers so we can estimate keycodes
* and charcodes when they are not provided.
*/
- _initializeAllEventListeners(EventTarget target) {
+ _KeyboardEventHandler.initializeAllEventListeners(String type,
+ EventTarget target) {
+ _commonInit(type);
_target = target;
_keyDownList = [];
Element.keyDownEvent.forTarget(_target, useCapture: true).listen(
@@ -376,24 +402,24 @@ class _KeyboardEventHandler implements EventStreamProvider<KeyEvent> {
*
* Example usage:
*
- * new KeyboardEventStream.onKeyDown(document.body).listen(
+ * KeyboardEventStream.onKeyDown(document.body).listen(
* keydownHandlerTest);
*
* This class is very much a work in progress, and we'd love to get information
* on how we can make this class work with as many international keyboards as
* possible. Bugs welcome!
*/
-class KeyboardEventStream extends Stream<KeyEvent> {
+class KeyboardEventStream {
/** Named constructor to produce a stream for onKeyPress events. */
- factory KeyboardEventStream.onKeyPress(EventTarget target) =>
+ static Stream<KeyEvent> onKeyPress(EventTarget target) =>
new _KeyboardEventHandler('keypress').forTarget(target);
/** Named constructor to produce a stream for onKeyUp events. */
- factory KeyboardEventStream.onKeyUp(EventTarget target) =>
+ static Stream<KeyEvent> onKeyUp(EventTarget target) =>
new _KeyboardEventHandler('keyup').forTarget(target);
/** Named constructor to produce a stream for onKeyDown events. */
- factory KeyboardEventStream.onKeyDown(EventTarget target) =>
+ static Stream<KeyEvent> onKeyDown(EventTarget target) =>
new _KeyboardEventHandler('keydown').forTarget(target);
}
« no previous file with comments | « tests/html/keyboard_event_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698