| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of html; |
| 6 |
| 7 /** |
| 8 * Internal class that does the actual calculations to determine keyCode and |
| 9 * charCode for keydown, keypress, and keyup events for all browsers. |
| 10 */ |
| 11 class _KeyboardEventHandler implements EventStreamProvider<KeyEvent> { |
| 12 // This code inspired by Closure's KeyHandling library. |
| 13 // http://closure-library.googlecode.com/svn/docs/closure_goog_events_keyhandl
er.js.source.html |
| 14 |
| 15 /** |
| 16 * The set of keys that have been pressed down without seeing their |
| 17 * corresponding keyup event. |
| 18 */ |
| 19 List<KeyboardEvent> _keyDownList; |
| 20 |
| 21 /** The set of functions that wish to be notified when a KeyEvent happens. */ |
| 22 List<Function> _callbacks; |
| 23 |
| 24 /** The type of KeyEvent we are tracking (keyup, keydown, keypress). */ |
| 25 String _type; |
| 26 |
| 27 /** The element we are watching for events to happen on. */ |
| 28 EventTarget _target; |
| 29 |
| 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]; |
| 32 |
| 33 /** Controller to produce KeyEvents for the stream. */ |
| 34 StreamController _controller; |
| 35 |
| 36 /** |
| 37 * An enumeration of key identifiers currently part of the W3C draft for DOM3 |
| 38 * and their mappings to keyCodes. |
| 39 * http://www.w3.org/TR/DOM-Level-3-Events/keyset.html#KeySet-Set |
| 40 */ |
| 41 static Map<String, int> _keyIdentifier = { |
| 42 'Up': KeyCode.UP, |
| 43 'Down': KeyCode.DOWN, |
| 44 'Left': KeyCode.LEFT, |
| 45 'Right': KeyCode.RIGHT, |
| 46 'Enter': KeyCode.ENTER, |
| 47 'F1': KeyCode.F1, |
| 48 'F2': KeyCode.F2, |
| 49 'F3': KeyCode.F3, |
| 50 'F4': KeyCode.F4, |
| 51 'F5': KeyCode.F5, |
| 52 'F6': KeyCode.F6, |
| 53 'F7': KeyCode.F7, |
| 54 'F8': KeyCode.F8, |
| 55 'F9': KeyCode.F9, |
| 56 'F10': KeyCode.F10, |
| 57 'F11': KeyCode.F11, |
| 58 'F12': KeyCode.F12, |
| 59 'U+007F': KeyCode.DELETE, |
| 60 'Home': KeyCode.HOME, |
| 61 'End': KeyCode.END, |
| 62 'PageUp': KeyCode.PAGE_UP, |
| 63 'PageDown': KeyCode.PAGE_DOWN, |
| 64 'Insert': KeyCode.INSERT |
| 65 }; |
| 66 |
| 67 /** |
| 68 * Gets the type of the event which this would listen for on the specified |
| 69 * event target. |
| 70 */ |
| 71 String getEventType(EventTarget target) => 'KeyEvent'; |
| 72 |
| 73 /** Return a stream for KeyEvents for the specified target. */ |
| 74 Stream<KeyEvent> forTarget(EventTarget e, {bool useCapture: false}) { |
| 75 _initializeAllEventListeners(e); |
| 76 return _controller.stream; |
| 77 } |
| 78 |
| 79 /** |
| 80 * General constructor, performs basic initialization for our improved |
| 81 * KeyboardEvent controller. |
| 82 */ |
| 83 _KeyboardEventHandler(String type) { |
| 84 _type = type; |
| 85 _controller = new StreamController.broadcast(); |
| 86 _callbacks = []; |
| 87 } |
| 88 |
| 89 /** |
| 90 * Hook up all event listeners under the covers so we can estimate keycodes |
| 91 * and charcodes when they are not provided. |
| 92 */ |
| 93 _initializeAllEventListeners(EventTarget target) { |
| 94 _target = target; |
| 95 _keyDownList = []; |
| 96 Element.keyDownEvent.forTarget(_target, useCapture: true).listen( |
| 97 processKeyDown); |
| 98 Element.keyPressEvent.forTarget(_target, useCapture: true).listen( |
| 99 processKeyPress); |
| 100 Element.keyUpEvent.forTarget(_target, useCapture: true).listen( |
| 101 processKeyUp); |
| 102 } |
| 103 |
| 104 /** |
| 105 * Notify all callback listeners that a KeyEvent of the relevant type has |
| 106 * occurred. |
| 107 */ |
| 108 bool _dispatch(KeyEvent event) { |
| 109 if (event.type == _type) |
| 110 _controller.add(event); |
| 111 } |
| 112 |
| 113 /** Determine if caps lock is one of the currently depressed keys. */ |
| 114 bool get _capsLockOn => |
| 115 _keyDownList.any((var element) => element.keyCode == KeyCode.CAPS_LOCK); |
| 116 |
| 117 /** |
| 118 * Given the previously recorded keydown key codes, see if we can determine |
| 119 * the keycode of this keypress [event]. (Generally browsers only provide |
| 120 * charCode information for keypress events, but with a little |
| 121 * reverse-engineering, we can also determine the keyCode.) Returns |
| 122 * KeyCode.UNKNOWN if the keycode could not be determined. |
| 123 */ |
| 124 int _determineKeyCodeForKeypress(KeyboardEvent event) { |
| 125 // Note: This function is a work in progress. We'll expand this function |
| 126 // once we get more information about other keyboards. |
| 127 for (var prevEvent in _keyDownList) { |
| 128 if (prevEvent._shadowCharCode == event.charCode) { |
| 129 return prevEvent.keyCode; |
| 130 } |
| 131 if ((event.shiftKey || _capsLockOn) && event.charCode >= "A".codeUnits[0] |
| 132 && event.charCode <= "Z".codeUnits[0] && event.charCode + |
| 133 _ROMAN_ALPHABET_OFFSET == prevEvent._shadowCharCode) { |
| 134 return prevEvent.keyCode; |
| 135 } |
| 136 } |
| 137 return KeyCode.UNKNOWN; |
| 138 } |
| 139 |
| 140 /** |
| 141 * Given the charater code returned from a keyDown [event], try to ascertain |
| 142 * and return the corresponding charCode for the character that was pressed. |
| 143 * This information is not shown to the user, but used to help polyfill |
| 144 * keypress events. |
| 145 */ |
| 146 int _findCharCodeKeyDown(KeyboardEvent event) { |
| 147 if (event.keyLocation == 3) { // Numpad keys. |
| 148 switch (event.keyCode) { |
| 149 case KeyCode.NUM_ZERO: |
| 150 // Even though this function returns _charCodes_, for some cases the |
| 151 // KeyCode == the charCode we want, in which case we use the keycode |
| 152 // constant for readability. |
| 153 return KeyCode.ZERO; |
| 154 case KeyCode.NUM_ONE: |
| 155 return KeyCode.ONE; |
| 156 case KeyCode.NUM_TWO: |
| 157 return KeyCode.TWO; |
| 158 case KeyCode.NUM_THREE: |
| 159 return KeyCode.THREE; |
| 160 case KeyCode.NUM_FOUR: |
| 161 return KeyCode.FOUR; |
| 162 case KeyCode.NUM_FIVE: |
| 163 return KeyCode.FIVE; |
| 164 case KeyCode.NUM_SIX: |
| 165 return KeyCode.SIX; |
| 166 case KeyCode.NUM_SEVEN: |
| 167 return KeyCode.SEVEN; |
| 168 case KeyCode.NUM_EIGHT: |
| 169 return KeyCode.EIGHT; |
| 170 case KeyCode.NUM_NINE: |
| 171 return KeyCode.NINE; |
| 172 case KeyCode.NUM_MULTIPLY: |
| 173 return 42; // Char code for * |
| 174 case KeyCode.NUM_PLUS: |
| 175 return 43; // + |
| 176 case KeyCode.NUM_MINUS: |
| 177 return 45; // - |
| 178 case KeyCode.NUM_PERIOD: |
| 179 return 46; // . |
| 180 case KeyCode.NUM_DIVISION: |
| 181 return 47; // / |
| 182 } |
| 183 } else if (event.keyCode >= 65 && event.keyCode <= 90) { |
| 184 // Set the "char code" for key down as the lower case letter. Again, this |
| 185 // will not show up for the user, but will be helpful in estimating |
| 186 // keyCode locations and other information during the keyPress event. |
| 187 return event.keyCode + _ROMAN_ALPHABET_OFFSET; |
| 188 } |
| 189 switch(event.keyCode) { |
| 190 case KeyCode.SEMICOLON: |
| 191 return KeyCode.FF_SEMICOLON; |
| 192 case KeyCode.EQUALS: |
| 193 return KeyCode.FF_EQUALS; |
| 194 case KeyCode.COMMA: |
| 195 return 44; // Ascii value for , |
| 196 case KeyCode.DASH: |
| 197 return 45; // - |
| 198 case KeyCode.PERIOD: |
| 199 return 46; // . |
| 200 case KeyCode.SLASH: |
| 201 return 47; // / |
| 202 case KeyCode.APOSTROPHE: |
| 203 return 96; // ` |
| 204 case KeyCode.OPEN_SQUARE_BRACKET: |
| 205 return 91; // [ |
| 206 case KeyCode.BACKSLASH: |
| 207 return 92; // \ |
| 208 case KeyCode.CLOSE_SQUARE_BRACKET: |
| 209 return 93; // ] |
| 210 case KeyCode.SINGLE_QUOTE: |
| 211 return 39; // ' |
| 212 } |
| 213 return event.keyCode; |
| 214 } |
| 215 |
| 216 /** |
| 217 * Returns true if the key fires a keypress event in the current browser. |
| 218 */ |
| 219 bool _firesKeyPressEvent(KeyEvent event) { |
| 220 if (!Device.isIE && !Device.isWebKit) { |
| 221 return true; |
| 222 } |
| 223 |
| 224 if (Device.userAgent.contains('Mac') && event.altKey) { |
| 225 return KeyCode.isCharacterKey(event.keyCode); |
| 226 } |
| 227 |
| 228 // Alt but not AltGr which is represented as Alt+Ctrl. |
| 229 if (event.altKey && !event.ctrlKey) { |
| 230 return false; |
| 231 } |
| 232 |
| 233 // Saves Ctrl or Alt + key for IE and WebKit, which won't fire keypress. |
| 234 if (!event.shiftKey && |
| 235 (_keyDownList.last.keyCode == KeyCode.CTRL || |
| 236 _keyDownList.last.keyCode == KeyCode.ALT || |
| 237 Device.userAgent.contains('Mac') && |
| 238 _keyDownList.last.keyCode == KeyCode.META)) { |
| 239 return false; |
| 240 } |
| 241 |
| 242 // Some keys with Ctrl/Shift do not issue keypress in WebKit. |
| 243 if (Device.isWebKit && event.ctrlKey && event.shiftKey && ( |
| 244 event.keyCode == KeyCode.BACKSLASH || |
| 245 event.keyCode == KeyCode.OPEN_SQUARE_BRACKET || |
| 246 event.keyCode == KeyCode.CLOSE_SQUARE_BRACKET || |
| 247 event.keyCode == KeyCode.TILDE || |
| 248 event.keyCode == KeyCode.SEMICOLON || event.keyCode == KeyCode.DASH || |
| 249 event.keyCode == KeyCode.EQUALS || event.keyCode == KeyCode.COMMA || |
| 250 event.keyCode == KeyCode.PERIOD || event.keyCode == KeyCode.SLASH || |
| 251 event.keyCode == KeyCode.APOSTROPHE || |
| 252 event.keyCode == KeyCode.SINGLE_QUOTE)) { |
| 253 return false; |
| 254 } |
| 255 |
| 256 switch (event.keyCode) { |
| 257 case KeyCode.ENTER: |
| 258 // IE9 does not fire keypress on ENTER. |
| 259 return !Device.isIE; |
| 260 case KeyCode.ESC: |
| 261 return !Device.isWebKit; |
| 262 } |
| 263 |
| 264 return KeyCode.isCharacterKey(event.keyCode); |
| 265 } |
| 266 |
| 267 /** |
| 268 * Normalize the keycodes to the IE KeyCodes (this is what Chrome, IE, and |
| 269 * Opera all use). |
| 270 */ |
| 271 int _normalizeKeyCodes(KeyboardEvent event) { |
| 272 // Note: This may change once we get input about non-US keyboards. |
| 273 if (Device.isFirefox) { |
| 274 switch(event.keyCode) { |
| 275 case KeyCode.FF_EQUALS: |
| 276 return KeyCode.EQUALS; |
| 277 case KeyCode.FF_SEMICOLON: |
| 278 return KeyCode.SEMICOLON; |
| 279 case KeyCode.MAC_FF_META: |
| 280 return KeyCode.META; |
| 281 case KeyCode.WIN_KEY_FF_LINUX: |
| 282 return KeyCode.WIN_KEY; |
| 283 } |
| 284 } |
| 285 return event.keyCode; |
| 286 } |
| 287 |
| 288 /** Handle keydown events. */ |
| 289 void processKeyDown(KeyboardEvent e) { |
| 290 // Ctrl-Tab and Alt-Tab can cause the focus to be moved to another window |
| 291 // before we've caught a key-up event. If the last-key was one of these |
| 292 // we reset the state. |
| 293 if (_keyDownList.length > 0 && |
| 294 (_keyDownList.last.keyCode == KeyCode.CTRL && !e.ctrlKey || |
| 295 _keyDownList.last.keyCode == KeyCode.ALT && !e.altKey || |
| 296 Device.userAgent.contains('Mac') && |
| 297 _keyDownList.last.keyCode == KeyCode.META && !e.metaKey)) { |
| 298 _keyDownList = []; |
| 299 } |
| 300 |
| 301 var event = new KeyEvent(e); |
| 302 event._shadowKeyCode = _normalizeKeyCodes(event); |
| 303 // Technically a "keydown" event doesn't have a charCode. This is |
| 304 // calculated nonetheless to provide us with more information in giving |
| 305 // as much information as possible on keypress about keycode and also |
| 306 // charCode. |
| 307 event._shadowCharCode = _findCharCodeKeyDown(event); |
| 308 if (_keyDownList.length > 0 && event.keyCode != _keyDownList.last.keyCode && |
| 309 !_firesKeyPressEvent(event)) { |
| 310 // Some browsers have quirks not firing keypress events where all other |
| 311 // browsers do. This makes them more consistent. |
| 312 processKeyPress(event); |
| 313 } |
| 314 _keyDownList.add(event); |
| 315 _dispatch(event); |
| 316 } |
| 317 |
| 318 /** Handle keypress events. */ |
| 319 void processKeyPress(KeyboardEvent event) { |
| 320 var e = new KeyEvent(event); |
| 321 // IE reports the character code in the keyCode field for keypress events. |
| 322 // There are two exceptions however, Enter and Escape. |
| 323 if (Device.isIE) { |
| 324 if (e.keyCode == KeyCode.ENTER || e.keyCode == KeyCode.ESC) { |
| 325 e._shadowCharCode = 0; |
| 326 } else { |
| 327 e._shadowCharCode = e.keyCode; |
| 328 } |
| 329 } else if (Device.isOpera) { |
| 330 // Opera reports the character code in the keyCode field. |
| 331 e._shadowCharCode = KeyCode.isCharacterKey(e.keyCode) ? e.keyCode : 0; |
| 332 } |
| 333 // Now we guestimate about what the keycode is that was actually |
| 334 // pressed, given previous keydown information. |
| 335 e._shadowKeyCode = _determineKeyCodeForKeypress(e); |
| 336 |
| 337 // Correct the key value for certain browser-specific quirks. |
| 338 if (e._shadowKeyIdentifier != null && |
| 339 _keyIdentifier.containsKey(e._shadowKeyIdentifier)) { |
| 340 // This is needed for Safari Windows because it currently doesn't give a |
| 341 // keyCode/which for non printable keys. |
| 342 e._shadowKeyCode = _keyIdentifier[e._shadowKeyIdentifier]; |
| 343 } |
| 344 e._shadowAltKey = _keyDownList.any((var element) => element.altKey); |
| 345 _dispatch(e); |
| 346 } |
| 347 |
| 348 /** Handle keyup events. */ |
| 349 void processKeyUp(KeyboardEvent event) { |
| 350 var e = new KeyEvent(event); |
| 351 KeyboardEvent toRemove = null; |
| 352 for (var key in _keyDownList) { |
| 353 if (key.keyCode == e.keyCode) { |
| 354 toRemove = key; |
| 355 } |
| 356 } |
| 357 if (toRemove != null) { |
| 358 _keyDownList = |
| 359 _keyDownList.where((element) => element != toRemove).toList(); |
| 360 } else if (_keyDownList.length > 0) { |
| 361 // This happens when we've reached some international keyboard case we |
| 362 // haven't accounted for or we haven't correctly eliminated all browser |
| 363 // inconsistencies. Filing bugs on when this is reached is welcome! |
| 364 _keyDownList.removeLast(); |
| 365 } |
| 366 _dispatch(e); |
| 367 } |
| 368 } |
| 369 |
| 370 |
| 371 /** |
| 372 * Records KeyboardEvents that occur on a particular element, and provides a |
| 373 * stream of outgoing KeyEvents with cross-browser consistent keyCode and |
| 374 * charCode values despite the fact that a multitude of browsers that have |
| 375 * varying keyboard default behavior. |
| 376 * |
| 377 * Example usage: |
| 378 * |
| 379 * new KeyboardEventStream.onKeyDown(document.body).listen( |
| 380 * keydownHandlerTest); |
| 381 * |
| 382 * 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 |
| 384 * possible. Bugs welcome! |
| 385 */ |
| 386 class KeyboardEventStream implements Stream<KeyEvent> { |
| 387 _KeyboardEventHandler _handler; |
| 388 Stream<KeyEvent> _stream; |
| 389 |
| 390 /** Named constructor to produce a stream for onKeyPress events. */ |
| 391 KeyboardEventStream.onKeyPress(EventTarget target) { |
| 392 _handler = new _KeyboardEventHandler('keypress'); |
| 393 _stream = _handler.forTarget(target); |
| 394 } |
| 395 |
| 396 /** Named constructor to produce a stream for onKeyUp events. */ |
| 397 KeyboardEventStream.onKeyUp(EventTarget target) { |
| 398 _handler = new _KeyboardEventHandler('keyup'); |
| 399 _stream = _handler.forTarget(target); |
| 400 } |
| 401 |
| 402 /** Named constructor to produce a stream for onKeyDown events. */ |
| 403 KeyboardEventStream.onKeyDown(EventTarget target) { |
| 404 _handler = new _KeyboardEventHandler('keydown'); |
| 405 _stream = _handler.forTarget(target); |
| 406 } |
| 407 |
| 408 /** |
| 409 * Unlike regular KeyboardEvents, you can programmatically add KeyEvents to |
| 410 * this stream. Be careful, though! If you add a keyDown event without a |
| 411 * corresponding keyUp event later, the stream may have difficulty estimating |
| 412 * future key codes. |
| 413 */ |
| 414 void addKeyDown(KeyEvent e) { |
| 415 _handler.processKeyDown(e); |
| 416 } |
| 417 |
| 418 /** |
| 419 * Unlike regular KeyboardEvents, you can programmatically add KeyEvents to |
| 420 * this stream. Be careful, though! If you add a keyUp event without a |
| 421 * corresponding keyDown event previously, the stream may have difficulty |
| 422 * estimating future key codes. |
| 423 */ |
| 424 void addKeyUp(KeyEvent e) { |
| 425 _handler.processKeyUp(e); |
| 426 } |
| 427 |
| 428 /** |
| 429 * Unlike regular KeyboardEvents, you can programmatically add KeyEvents to |
| 430 * this stream. |
| 431 */ |
| 432 void addKeyPress(KeyEvent e) { |
| 433 _handler.processKeyPress(e); |
| 434 } |
| 435 |
| 436 // ---------------- Stream implementation methods: --------------- |
| 437 /** |
| 438 * Adds a subscription to this stream. |
| 439 * |
| 440 * On each data event from this stream, the subscriber's [onData] handler |
| 441 * is called. If [onData] is null, nothing happens. |
| 442 * |
| 443 * On errors from this stream, the [onError] handler is given a |
| 444 * [AsyncError] object describing the error. |
| 445 * |
| 446 * If this stream closes, the [onDone] handler is called. |
| 447 * |
| 448 * If [unsubscribeOnError] is true, the subscription is ended when |
| 449 * the first error is reported. The default is false. |
| 450 */ |
| 451 StreamSubscription<KeyEvent> listen(void onData(KeyEvent event), |
| 452 {void onError(AsyncError error), void onDone(), |
| 453 bool unsubscribeOnError}) => _stream.listen(onData, onError: |
| 454 onError, onDone: onDone, unsubscribeOnError: unsubscribeOnError); |
| 455 |
| 456 /** |
| 457 * Reports whether this stream is a broadcast stream. |
| 458 */ |
| 459 bool get isBroadcast => _stream.isBroadcast; |
| 460 |
| 461 /** Counts the elements in the stream. */ |
| 462 Future<int> get length => _stream.length; |
| 463 |
| 464 /** Reports whether this stream contains any elements. */ |
| 465 Future<bool> get isEmpty => _stream.isEmpty; |
| 466 |
| 467 /** |
| 468 * Returns the first element. |
| 469 * |
| 470 * If [this] is empty throws a [StateError]. Otherwise this method is |
| 471 * equivalent to [:this.elementAt(0):] |
| 472 */ |
| 473 Future<KeyEvent> get first => _stream.first; |
| 474 |
| 475 /** |
| 476 * Returns the last element. |
| 477 * |
| 478 * If [this] is empty throws a [StateError]. |
| 479 */ |
| 480 Future<KeyEvent> get last => _stream.last; |
| 481 |
| 482 /** |
| 483 * Returns the single element. |
| 484 * |
| 485 * If [this] is empty or has more than one element throws a [StateError]. |
| 486 */ |
| 487 Future<KeyEvent> get single => _stream.single; |
| 488 |
| 489 /** |
| 490 * Creates a new stream from this stream that converts each element |
| 491 * into zero or more events. |
| 492 * |
| 493 * Each incoming event is converted to an [Iterable] of new events, |
| 494 * and each of these new events are then sent by the returned stream |
| 495 * in order. |
| 496 */ |
| 497 Stream<dynamic> expand(Iterable convert(KeyEvent value)) => |
| 498 _stream.expand(convert); |
| 499 |
| 500 /** |
| 501 * Chains this stream as the input of the provided [StreamTransformer]. |
| 502 * |
| 503 * Returns the result of [:streamTransformer.bind:] itself. |
| 504 */ |
| 505 Stream<dynamic> transform(StreamTransformer<KeyEvent, dynamic> |
| 506 streamTransformer) => _stream.transform(streamTransformer); |
| 507 |
| 508 /** |
| 509 * Checks whether [test] accepts any element provided by this stream. |
| 510 * |
| 511 * Completes the [Future] when the answer is known. |
| 512 * If this stream reports an error, the [Future] will report that error. |
| 513 */ |
| 514 Future<bool> any(bool test(KeyEvent element)) => _stream.any(test); |
| 515 |
| 516 /** |
| 517 * Returns a multi-subscription stream that produces the same events as this. |
| 518 * |
| 519 * If this stream is single-subscription, return a new stream that allows |
| 520 * multiple subscribers. It will subscribe to this stream when its first |
| 521 * subscriber is added, and unsubscribe again when the last subscription is |
| 522 * cancelled. |
| 523 * |
| 524 * If this stream is already a broadcast stream, it is returned unmodified. |
| 525 */ |
| 526 Stream<KeyEvent> asBroadcastStream() => _stream; |
| 527 |
| 528 /** |
| 529 * Checks whether [match] occurs in the elements provided by this stream. |
| 530 * |
| 531 * Completes the [Future] when the answer is known. |
| 532 * If this stream reports an error, the [Future] will report that error. |
| 533 */ |
| 534 Future<bool> contains(KeyEvent match) => _stream.contains(match); |
| 535 |
| 536 /** |
| 537 * Skips data events if they are equal to the previous data event. |
| 538 * |
| 539 * The returned stream provides the same events as this stream, except |
| 540 * that it never provides two consequtive data events that are equal. |
| 541 * |
| 542 * Equality is determined by the provided [equals] method. If that is |
| 543 * omitted, the '==' operator on the last provided data element is used. |
| 544 */ |
| 545 Stream<KeyEvent> distinct([bool equals(KeyEvent previous, KeyEvent next)]) => |
| 546 _stream.distinct(equals); |
| 547 |
| 548 /** |
| 549 * Returns the value of the [index]th data event of this stream. |
| 550 * |
| 551 * If an error event occurs, the future will end with this error. |
| 552 * |
| 553 * If this stream provides fewer than [index] elements before closing, |
| 554 * an error is reported. |
| 555 */ |
| 556 Future<KeyEvent> elementAt(int index) => _stream.elementAt(index); |
| 557 |
| 558 /** |
| 559 * Checks whether [test] accepts all elements provided by this stream. |
| 560 * |
| 561 * Completes the [Future] when the answer is known. |
| 562 * If this stream reports an error, the [Future] will report that error. |
| 563 */ |
| 564 Future<bool> every(bool test(KeyEvent element)) => |
| 565 _stream.every(test); |
| 566 |
| 567 /** |
| 568 * Finds the first element of this stream matching [test]. |
| 569 * |
| 570 * Returns a future that is filled with the first element of this stream |
| 571 * that [test] returns true for. |
| 572 * |
| 573 * If no such element is found before this stream is done, and a |
| 574 * [defaultValue] function is provided, the result of calling [defaultValue] |
| 575 * becomes the value of the future. |
| 576 * |
| 577 * If an error occurs, or if this stream ends without finding a match and |
| 578 * with no [defaultValue] function provided, the future will receive an |
| 579 * error. |
| 580 */ |
| 581 Future<KeyEvent> firstWhere(bool test(KeyEvent value), |
| 582 {KeyEvent defaultValue()}) => _stream.firstWhere(test); |
| 583 |
| 584 /** |
| 585 * Creates a wrapper Stream that intercepts some errors from this stream. |
| 586 * |
| 587 * If this stream sends an error that matches [test], then it is intercepted |
| 588 * by the [handle] function. |
| 589 * |
| 590 * An [AsyncError] [:e:] is matched by a test function if [:test(e):] returns |
| 591 * true. If [test] is omitted, every error is considered matching. |
| 592 * |
| 593 * If the error is intercepted, the [handle] function can decide what to do |
| 594 * with it. It can throw if it wants to raise a new (or the same) error, |
| 595 * or simply return to make the stream forget the error. |
| 596 * |
| 597 * If you need to transform an error into a data event, use the more generic |
| 598 * [Stream.transformEvent] to handle the event by writing a data event to |
| 599 * the output sink |
| 600 */ |
| 601 Stream<KeyEvent> handleError(void handle(AsyncError error), |
| 602 {bool test(error)}) => _stream.handleError(handle); |
| 603 |
| 604 /** |
| 605 * Finds the last element in this stream matching [test]. |
| 606 * |
| 607 * As [firstWhere], except that the last matching element is found. |
| 608 * That means that the result cannot be provided before this stream |
| 609 * is done. |
| 610 */ |
| 611 Future<KeyEvent> lastWhere(bool test(KeyEvent value), |
| 612 {KeyEvent defaultValue()}) => _stream.lastWhere(test); |
| 613 |
| 614 /** |
| 615 * Creates a new stream that converts each element of this stream |
| 616 * to a new value using the [convert] function. |
| 617 */ |
| 618 Stream map(convert(KeyEvent event)) => _stream.map(convert); |
| 619 |
| 620 /** |
| 621 * Finds the largest element in the stream. |
| 622 * |
| 623 * If the stream is empty, the result is [:null:]. |
| 624 * Otherwise the result is an value from the stream that is not smaller |
| 625 * than any other value from the stream (according to [compare], which must |
| 626 * be a [Comparator]). |
| 627 * |
| 628 * If [compare] is omitted, it defaults to [Comparable.compare]. |
| 629 * |
| 630 * *Deprecated*. Use [reduce] with a binary max method if needed. |
| 631 */ |
| 632 Future<KeyEvent> max([int compare(KeyEvent a, KeyEvent b)]) => |
| 633 _stream.max(compare); |
| 634 |
| 635 /** |
| 636 * Finds the least element in the stream. |
| 637 * |
| 638 * If the stream is empty, the result is [:null:]. |
| 639 * Otherwise the result is a value from the stream that is not greater |
| 640 * than any other value from the stream (according to [compare], which must |
| 641 * be a [Comparator]). |
| 642 * |
| 643 * If [compare] is omitted, it defaults to [Comparable.compare]. |
| 644 * |
| 645 * *Deprecated*. Use [reduce] with a binary min method if needed. |
| 646 */ |
| 647 Future<KeyEvent> min([int compare(KeyEvent a, KeyEvent b)]) => |
| 648 _stream.min(compare); |
| 649 |
| 650 /** |
| 651 * Binds this stream as the input of the provided [StreamConsumer]. |
| 652 */ |
| 653 Future pipe(StreamConsumer<KeyEvent, dynamic> streamConsumer) => |
| 654 _stream.pipe(streamConsumer); |
| 655 |
| 656 Future pipeInto(EventSink<KeyEvent> sink, {void onError(AsyncError error), |
| 657 bool unsubscribeOnError}) => _stream.pipeInto(sink, onError: |
| 658 onError, unsubscribeOnError: unsubscribeOnError); |
| 659 |
| 660 /** Reduces a sequence of values by repeatedly applying [combine]. */ |
| 661 Future reduce(initialValue, combine(previous, KeyEvent element)) => |
| 662 _stream.reduce(initialValue, combine); |
| 663 |
| 664 /** |
| 665 * Finds the single element in this stream matching [test]. |
| 666 * |
| 667 * Like [lastMatch], except that it is an error if more than one |
| 668 * matching element occurs in the stream. |
| 669 */ |
| 670 Future<KeyEvent> singleWhere(bool test(KeyEvent value)) => |
| 671 _stream.singleWhere(test); |
| 672 |
| 673 /** |
| 674 * Skips the first [count] data events from this stream. |
| 675 */ |
| 676 Stream<KeyEvent> skip(int count) => _stream.skip(count); |
| 677 |
| 678 /** |
| 679 * Skip data events from this stream while they are matched by [test]. |
| 680 * |
| 681 * Error and done events are provided by the returned stream unmodified. |
| 682 * |
| 683 * Starting with the first data event where [test] returns true for the |
| 684 * event data, the returned stream will have the same events as this stream. |
| 685 */ |
| 686 Stream<KeyEvent> skipWhile(bool test(KeyEvent value)) => |
| 687 _stream.skipWhile(test); |
| 688 |
| 689 /** |
| 690 * Provides at most the first [n] values of this stream. |
| 691 * |
| 692 * Forwards the first [n] data events of this stream, and all error |
| 693 * events, to the returned stream, and ends with a done event. |
| 694 * |
| 695 * If this stream produces fewer than [count] values before it's done, |
| 696 * so will the returned stream. |
| 697 */ |
| 698 Stream<KeyEvent> take(int count) => _stream.take(count); |
| 699 |
| 700 /** |
| 701 * Forwards data events while [test] is successful. |
| 702 * |
| 703 * The returned stream provides the same events as this stream as long |
| 704 * as [test] returns [:true:] for the event data. The stream is done |
| 705 * when either this stream is done, or when this stream first provides |
| 706 * a value that [test] doesn't accept. |
| 707 */ |
| 708 Stream<KeyEvent> takeWhile(bool test(KeyEvent value)) => |
| 709 _stream.takeWhile(test); |
| 710 |
| 711 /** Collects the data of this stream in a [List]. */ |
| 712 Future<List<KeyEvent>> toList() => _stream.toList(); |
| 713 |
| 714 /** Collects the data of this stream in a [Set]. */ |
| 715 Future<Set<KeyEvent>> toSet() => _stream.toSet(); |
| 716 |
| 717 /** |
| 718 * Creates a new stream from this stream that discards some data events. |
| 719 * |
| 720 * The new stream sends the same error and done events as this stream, |
| 721 * but it only sends the data events that satisfy the [test]. |
| 722 */ |
| 723 Stream<KeyEvent> where(bool test(KeyEvent event)) => |
| 724 _stream.where(test); |
| 725 } |
| OLD | NEW |