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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/html/keyboard_event_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of html; 5 part of html;
6 6
7 /** 7 /**
8 * Internal class that does the actual calculations to determine keyCode and 8 * Internal class that does the actual calculations to determine keyCode and
9 * charCode for keydown, keypress, and keyup events for all browsers. 9 * charCode for keydown, keypress, and keyup events for all browsers.
10 */ 10 */
(...skipping 15 matching lines...) Expand all
26 26
27 /** The element we are watching for events to happen on. */ 27 /** The element we are watching for events to happen on. */
28 EventTarget _target; 28 EventTarget _target;
29 29
30 // The distance to shift from upper case alphabet Roman letters to lower case. 30 // The distance to shift from upper case alphabet Roman letters to lower case.
31 final int _ROMAN_ALPHABET_OFFSET = "a".codeUnits[0] - "A".codeUnits[0]; 31 final int _ROMAN_ALPHABET_OFFSET = "a".codeUnits[0] - "A".codeUnits[0];
32 32
33 /** Controller to produce KeyEvents for the stream. */ 33 /** Controller to produce KeyEvents for the stream. */
34 StreamController _controller; 34 StreamController _controller;
35 35
36 String _eventType = 'KeyEvent';
37
36 /** 38 /**
37 * An enumeration of key identifiers currently part of the W3C draft for DOM3 39 * An enumeration of key identifiers currently part of the W3C draft for DOM3
38 * and their mappings to keyCodes. 40 * and their mappings to keyCodes.
39 * http://www.w3.org/TR/DOM-Level-3-Events/keyset.html#KeySet-Set 41 * http://www.w3.org/TR/DOM-Level-3-Events/keyset.html#KeySet-Set
40 */ 42 */
41 static Map<String, int> _keyIdentifier = { 43 static Map<String, int> _keyIdentifier = {
42 'Up': KeyCode.UP, 44 'Up': KeyCode.UP,
43 'Down': KeyCode.DOWN, 45 'Down': KeyCode.DOWN,
44 'Left': KeyCode.LEFT, 46 'Left': KeyCode.LEFT,
45 'Right': KeyCode.RIGHT, 47 'Right': KeyCode.RIGHT,
(...skipping 15 matching lines...) Expand all
61 'End': KeyCode.END, 63 'End': KeyCode.END,
62 'PageUp': KeyCode.PAGE_UP, 64 'PageUp': KeyCode.PAGE_UP,
63 'PageDown': KeyCode.PAGE_DOWN, 65 'PageDown': KeyCode.PAGE_DOWN,
64 'Insert': KeyCode.INSERT 66 'Insert': KeyCode.INSERT
65 }; 67 };
66 68
67 /** 69 /**
68 * Gets the type of the event which this would listen for on the specified 70 * Gets the type of the event which this would listen for on the specified
69 * event target. 71 * event target.
70 */ 72 */
71 String getEventType(EventTarget target) => 'KeyEvent'; 73 String getEventType(EventTarget target) => _eventType;
72 74
73 /** Return a stream for KeyEvents for the specified target. */ 75 /** Return a stream for KeyEvents for the specified target. */
74 Stream<KeyEvent> forTarget(EventTarget e, {bool useCapture: false}) { 76 Stream<KeyEvent> forTarget(EventTarget e, {bool useCapture: false}) {
75 _initializeAllEventListeners(e); 77 var newController = new _KeyboardEventHandler.initializeAllEventListeners(
Andrei Mouravski 2013/03/27 00:03:35 I'd inline this.
76 return _controller.stream; 78 _type, e);
79 return newController.stream;
77 } 80 }
78 81
79 /** 82 /**
83 * Accessor to the stream associated with a particular KeyboardEvent
84 * EventTarget.
85 *
86 * [forTarget] must be called to initialize this stream to listen to a
87 * particular EventTarget.
88 */
89 Stream<KeyEvent> get stream {
90 if(_target != null) {
91 return _controller.stream;
92 } else {
93 throw new StateError("Not initialized. Call forTarget to access a stream "
94 "initialized with a particular EventTarget.");
95 }
96 }
97
98 /**
80 * General constructor, performs basic initialization for our improved 99 * General constructor, performs basic initialization for our improved
81 * KeyboardEvent controller. 100 * KeyboardEvent controller.
82 */ 101 */
83 _KeyboardEventHandler(String type) { 102 _KeyboardEventHandler(String type) {
103 _commonInit(type);
104 }
105
106 void _commonInit(String type) {
84 _type = type; 107 _type = type;
85 _controller = new StreamController.broadcast(); 108 _controller = new StreamController.broadcast();
86 _callbacks = []; 109 _callbacks = [];
110 _target = null;
87 } 111 }
88 112
89 /** 113 /**
90 * Hook up all event listeners under the covers so we can estimate keycodes 114 * Hook up all event listeners under the covers so we can estimate keycodes
91 * and charcodes when they are not provided. 115 * and charcodes when they are not provided.
92 */ 116 */
93 _initializeAllEventListeners(EventTarget target) { 117 _KeyboardEventHandler.initializeAllEventListeners(String type,
118 EventTarget target) {
119 _commonInit(type);
94 _target = target; 120 _target = target;
95 _keyDownList = []; 121 _keyDownList = [];
96 Element.keyDownEvent.forTarget(_target, useCapture: true).listen( 122 Element.keyDownEvent.forTarget(_target, useCapture: true).listen(
97 processKeyDown); 123 processKeyDown);
98 Element.keyPressEvent.forTarget(_target, useCapture: true).listen( 124 Element.keyPressEvent.forTarget(_target, useCapture: true).listen(
99 processKeyPress); 125 processKeyPress);
100 Element.keyUpEvent.forTarget(_target, useCapture: true).listen( 126 Element.keyUpEvent.forTarget(_target, useCapture: true).listen(
101 processKeyUp); 127 processKeyUp);
102 } 128 }
103 129
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 395
370 396
371 /** 397 /**
372 * Records KeyboardEvents that occur on a particular element, and provides a 398 * Records KeyboardEvents that occur on a particular element, and provides a
373 * stream of outgoing KeyEvents with cross-browser consistent keyCode and 399 * stream of outgoing KeyEvents with cross-browser consistent keyCode and
374 * charCode values despite the fact that a multitude of browsers that have 400 * charCode values despite the fact that a multitude of browsers that have
375 * varying keyboard default behavior. 401 * varying keyboard default behavior.
376 * 402 *
377 * Example usage: 403 * Example usage:
378 * 404 *
379 * new KeyboardEventStream.onKeyDown(document.body).listen( 405 * KeyboardEventStream.onKeyDown(document.body).listen(
380 * keydownHandlerTest); 406 * keydownHandlerTest);
381 * 407 *
382 * This class is very much a work in progress, and we'd love to get information 408 * This class is very much a work in progress, and we'd love to get information
383 * on how we can make this class work with as many international keyboards as 409 * on how we can make this class work with as many international keyboards as
384 * possible. Bugs welcome! 410 * possible. Bugs welcome!
385 */ 411 */
386 class KeyboardEventStream extends Stream<KeyEvent> { 412 class KeyboardEventStream {
387 413
388 /** Named constructor to produce a stream for onKeyPress events. */ 414 /** Named constructor to produce a stream for onKeyPress events. */
389 factory KeyboardEventStream.onKeyPress(EventTarget target) => 415 static Stream<KeyEvent> onKeyPress(EventTarget target) =>
390 new _KeyboardEventHandler('keypress').forTarget(target); 416 new _KeyboardEventHandler('keypress').forTarget(target);
391 417
392 /** Named constructor to produce a stream for onKeyUp events. */ 418 /** Named constructor to produce a stream for onKeyUp events. */
393 factory KeyboardEventStream.onKeyUp(EventTarget target) => 419 static Stream<KeyEvent> onKeyUp(EventTarget target) =>
394 new _KeyboardEventHandler('keyup').forTarget(target); 420 new _KeyboardEventHandler('keyup').forTarget(target);
395 421
396 /** Named constructor to produce a stream for onKeyDown events. */ 422 /** Named constructor to produce a stream for onKeyDown events. */
397 factory KeyboardEventStream.onKeyDown(EventTarget target) => 423 static Stream<KeyEvent> onKeyDown(EventTarget target) =>
398 new _KeyboardEventHandler('keydown').forTarget(target); 424 new _KeyboardEventHandler('keydown').forTarget(target);
399 } 425 }
OLDNEW
« 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