| OLD | NEW |
| 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 library android_extension; | 5 library android_extension; |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 | 7 |
| 8 // A VERY simplified DOM. | 8 // A VERY simplified DOM. |
| 9 | 9 |
| 10 class BodyElement { | 10 class BodyElement { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 Document document = new Document._internal(); | 60 Document document = new Document._internal(); |
| 61 | 61 |
| 62 // TODO(gram): make private and call from within library context. | 62 // TODO(gram): make private and call from within library context. |
| 63 update_() { | 63 update_() { |
| 64 window._dispatch(); | 64 window._dispatch(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Event handling. This is very kludgy for now, especially the | 67 // Event handling. This is very kludgy for now, especially the |
| 68 // bare-bones Stream stuff! | 68 // bare-bones Stream stuff! |
| 69 | 69 |
| 70 typedef void EventListener(Event event); |
| 71 |
| 72 class EventTarget { |
| 73 static Map<EventTarget, Map<String, List<EventListener>>> |
| 74 _listeners = new Map(); |
| 75 |
| 76 static get listeners => _listeners; |
| 77 |
| 78 bool dispatchEvent(Event event) { |
| 79 if (!_listeners.containsKey(this)) return false; |
| 80 var listeners = _listeners[this]; |
| 81 if (!listeners.containsKey(event.type)) return false; |
| 82 var eventListeners = listeners[event.type]; |
| 83 for (var eventListener in eventListeners) { |
| 84 if (eventListener != null) { |
| 85 eventListener(event); |
| 86 } |
| 87 } |
| 88 return true; |
| 89 } |
| 90 |
| 91 void addListener(String eventType, EventListener handler) { |
| 92 if (!_listeners.containsKey(this)) { |
| 93 _listeners[this] = new Map(); |
| 94 } |
| 95 var listeners = _listeners[this]; |
| 96 if (!listeners.containsKey(eventType)) { |
| 97 listeners[eventType] = new List(); |
| 98 } |
| 99 var event_listeners = listeners[eventType]; |
| 100 for (var i = 0; i < event_listeners.length; i++) { |
| 101 if (event_listeners[i] == null) { |
| 102 event_listeners[i] = handler; |
| 103 return; |
| 104 } |
| 105 } |
| 106 event_listeners.add(handler); |
| 107 } |
| 108 |
| 109 void removeListener(String eventType, EventListener handler) { |
| 110 if (_listeners.containsKey(this)) { |
| 111 var listeners = _listeners[this]; |
| 112 if (listeners.containsKey(eventType)) { |
| 113 var event_listeners = listeners[eventType]; |
| 114 for (var i = 0; i < event_listeners.length; i++) { |
| 115 if (event_listeners[i] == handler) { |
| 116 event_listeners[i] = null; |
| 117 break; |
| 118 } |
| 119 } |
| 120 } |
| 121 } |
| 122 } |
| 123 } |
| 124 |
| 70 class Event { | 125 class Event { |
| 71 final String type; | 126 final String type; |
| 127 EventTarget target; |
| 72 Event(String type) : this.type = type; | 128 Event(String type) : this.type = type; |
| 73 } | 129 } |
| 74 | 130 |
| 75 class KeyEvent extends Event { | 131 class KeyEvent extends Event { |
| 76 final bool altKey; | 132 final bool altKey; |
| 77 final bool ctrlKey; | 133 final bool ctrlKey; |
| 78 final bool shiftKey; | 134 final bool shiftKey; |
| 79 final int keyCode; | 135 final int keyCode; |
| 80 | 136 |
| 81 KeyEvent(String type, int keycode, bool alt, bool ctrl, bool shift) | 137 KeyEvent(String type, int keycode, bool alt, bool ctrl, bool shift) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 93 | 149 |
| 94 MouseEvent(String type, int x, int y) | 150 MouseEvent(String type, int x, int y) |
| 95 : super(type), | 151 : super(type), |
| 96 screenX = x, | 152 screenX = x, |
| 97 screenY = y, | 153 screenY = y, |
| 98 clientX = x, | 154 clientX = x, |
| 99 clientY = y { | 155 clientY = y { |
| 100 } | 156 } |
| 101 } | 157 } |
| 102 | 158 |
| 103 typedef void EventListener(Event event); | |
| 104 | |
| 105 Map<String,List<Function>> _listeners = new Map(); | |
| 106 | 159 |
| 107 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> { | 160 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> { |
| 108 int _pauseCount = 0; | 161 int _pauseCount = 0; |
| 109 Object _target; | 162 EventTarget _target; |
| 110 final String _eventType; | 163 final String _eventType; |
| 111 var _onData; | 164 var _onData; |
| 112 | 165 |
| 113 _EventStreamSubscription(this._target, this._eventType, this._onData) { | 166 _EventStreamSubscription(this._target, this._eventType, this._onData) { |
| 114 _tryResume(); | 167 _tryResume(); |
| 115 } | 168 } |
| 116 | 169 |
| 117 void cancel() { | 170 void cancel() { |
| 118 if (_canceled) { | 171 if (_canceled) { |
| 119 throw new StateError("Subscription has been canceled."); | 172 throw new StateError("Subscription has been canceled."); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 } | 217 } |
| 165 if (!_paused) { | 218 if (!_paused) { |
| 166 throw new StateError("Subscription is not paused."); | 219 throw new StateError("Subscription is not paused."); |
| 167 } | 220 } |
| 168 --_pauseCount; | 221 --_pauseCount; |
| 169 _tryResume(); | 222 _tryResume(); |
| 170 } | 223 } |
| 171 | 224 |
| 172 void _tryResume() { | 225 void _tryResume() { |
| 173 if (_onData != null && !_paused) { | 226 if (_onData != null && !_paused) { |
| 174 if (!_listeners.containsKey(_eventType)) { | 227 _target.addListener(_eventType, _onData); |
| 175 _listeners[_eventType] = new List(); | |
| 176 } | |
| 177 var event_listeners = _listeners[_eventType]; | |
| 178 for (var i = 0; i < event_listeners.length; i++) { | |
| 179 if (event_listeners[i] == null) { | |
| 180 event_listeners[i] = _onData; | |
| 181 return; | |
| 182 } | |
| 183 } | |
| 184 event_listeners.add(_onData); | |
| 185 } | 228 } |
| 186 } | 229 } |
| 187 | 230 |
| 188 void _unlisten() { | 231 void _unlisten() { |
| 189 if (_onData != null) { | 232 if (_onData != null) { |
| 190 if (!_listeners.containsKey(_eventType)) { | 233 _target.removeListener(_eventType, _onData); |
| 191 var event_listeners = _listeners[_eventType]; | |
| 192 for (var i = 0; i < event_listeners.length; i++) { | |
| 193 if (event_listeners[i] == _onData) { | |
| 194 event_listeners[i] = null; | |
| 195 break; | |
| 196 } | |
| 197 } | |
| 198 } | |
| 199 } | 234 } |
| 200 } | 235 } |
| 201 } | 236 } |
| 202 | 237 |
| 203 class _EventStream<T extends Event> extends Stream<T> { | 238 class _EventStream<T extends Event> extends Stream<T> { |
| 204 final Object _target; | 239 final Object _target; |
| 205 final String _eventType; | 240 final String _eventType; |
| 206 | 241 |
| 207 _EventStream(this._target, this._eventType); | 242 _EventStream(this._target, this._eventType); |
| 208 | 243 |
| 209 // DOM events are inherently multi-subscribers. | 244 // DOM events are inherently multi-subscribers. |
| 210 Stream<T> asBroadcastStream() => this; | 245 Stream<T> asBroadcastStream() => this; |
| 211 bool get isBroadcast => true; | 246 bool get isBroadcast => true; |
| 212 | 247 |
| 213 StreamSubscription<T> listen(void onData(T event), | 248 StreamSubscription<T> listen(void onData(T event), |
| 214 { void onError(AsyncError error), | 249 { void onError(AsyncError error), |
| 215 void onDone(), | 250 void onDone(), |
| 216 bool unsubscribeOnError}) { | 251 bool unsubscribeOnError}) { |
| 217 | 252 |
| 218 return new _EventStreamSubscription<T>( | 253 return new _EventStreamSubscription<T>( |
| 219 this._target, this._eventType, onData); | 254 this._target, this._eventType, onData); |
| 220 } | 255 } |
| 221 } | 256 } |
| 222 | 257 |
| 223 class Node { | 258 class Node extends EventTarget { |
| 224 Stream<KeyEvent> get onKeyDown => new _EventStream(this, 'keydown'); | 259 Stream<KeyEvent> get onKeyDown => new _EventStream(this, 'keydown'); |
| 225 Stream<KeyEvent> get onKeyUp => new _EventStream(this, 'keyup'); | 260 Stream<KeyEvent> get onKeyUp => new _EventStream(this, 'keyup'); |
| 226 Stream<MouseEvent> get onMouseDown => new _EventStream(this, 'mousedown'); | 261 Stream<MouseEvent> get onMouseDown => new _EventStream(this, 'mousedown'); |
| 227 Stream<MouseEvent> get onMouseMove => new _EventStream(this, 'mousemove'); | 262 Stream<MouseEvent> get onMouseMove => new _EventStream(this, 'mousemove'); |
| 228 Stream<MouseEvent> get onMouseUp => new _EventStream(this, 'mouseup'); | 263 Stream<MouseEvent> get onMouseUp => new _EventStream(this, 'mouseup'); |
| 229 } | 264 } |
| 230 | 265 |
| 231 // TODO(gram): If we support more than one on-screen canvas, we will | 266 // TODO(gram): If we support more than one on-screen canvas, we will |
| 232 // need to filter dispatched events by the target Node. | 267 // need to filter dispatched mouse and key events by the target Node |
| 233 _dispatchEvent(String event_type, Event e) { | 268 // with more granularity; right now we just iterate through DOM nodes |
| 234 if (!_listeners.containsKey(event_type)) return; | 269 // until we find one that handles the event. |
| 235 var event_listeners = _listeners[event_type]; | 270 _dispatchEvent(Event event) { |
| 236 for (var i = 0; i < event_listeners.length; i++) { | 271 assert(document.body.nodes.length <= 1); |
| 237 if (event_listeners[i] != null) { | 272 for (var target in document.body.nodes) { |
| 238 event_listeners[i](e); | 273 event.target = target; |
| 274 if (target.dispatchEvent(event)) { |
| 275 break; |
| 239 } | 276 } |
| 240 } | 277 } |
| 241 } | 278 } |
| 242 | 279 |
| 243 _dispatchKeyEvent(String type, int keyCode, bool alt, bool ctrl, bool shift) => | 280 _dispatchKeyEvent(String type, int keyCode, bool alt, bool ctrl, bool shift) { |
| 244 _dispatchEvent(type, new KeyEvent(type, keyCode, alt, ctrl, shift)); | 281 _dispatchEvent(new KeyEvent(type, keyCode, alt, ctrl, shift)); |
| 282 } |
| 245 | 283 |
| 246 _dispatchMouseEvent(String type, double x, double y) => | 284 _dispatchMouseEvent(String type, double x, double y) { |
| 247 _dispatchEvent(type, new MouseEvent(type, x.toInt(), y.toInt())); | 285 _dispatchEvent(new MouseEvent(type, x.toInt(), y.toInt())); |
| 286 } |
| 248 | 287 |
| 249 // These next few are called by vmglue.cc. | 288 // These next few are called by vmglue.cc. |
| 250 onKeyDown_(int when, int keyCode, bool alt, bool ctrl, bool shift, int repeat) | 289 onKeyDown_(int when, int keyCode, bool alt, bool ctrl, bool shift, int repeat) |
| 251 => _dispatchKeyEvent('keydown', keyCode, alt, ctrl, shift); | 290 => _dispatchKeyEvent('keydown', keyCode, alt, ctrl, shift); |
| 252 | 291 |
| 253 onKeyUp_(int when, int keyCode, bool alt, bool ctrl, bool shift, int repeat) => | 292 onKeyUp_(int when, int keyCode, bool alt, bool ctrl, bool shift, int repeat) => |
| 254 _dispatchKeyEvent('keyup', keyCode, alt, ctrl, shift); | 293 _dispatchKeyEvent('keyup', keyCode, alt, ctrl, shift); |
| 255 | 294 |
| 256 onMouseDown_(int when, double x, double y) => | 295 onMouseDown_(int when, double x, double y) => |
| 257 _dispatchMouseEvent('mousedown', x, y); | 296 _dispatchMouseEvent('mousedown', x, y); |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 native "C2DStrokeText"; | 639 native "C2DStrokeText"; |
| 601 void C2DTransform(int handle, double m11, double m12, | 640 void C2DTransform(int handle, double m11, double m12, |
| 602 double m21, double m22, double dx, double dy) | 641 double m21, double m22, double dx, double dy) |
| 603 native "C2DTransform"; | 642 native "C2DTransform"; |
| 604 void C2DTranslate(int handle, double x, double y) | 643 void C2DTranslate(int handle, double x, double y) |
| 605 native "C2DTranslate"; | 644 native "C2DTranslate"; |
| 606 | 645 |
| 607 void C2DCreateNativeContext(int handle, int width, int height) | 646 void C2DCreateNativeContext(int handle, int width, int height) |
| 608 native "C2DCreateNativeContext"; | 647 native "C2DCreateNativeContext"; |
| 609 | 648 |
| 610 class ElementEvents { | 649 class ImageElement extends Node { |
| 611 final List load; | 650 Stream<Event> get onLoad => new _EventStream(this, 'load'); |
| 612 ElementEvents() | |
| 613 : load =new List() { | |
| 614 } | |
| 615 } | |
| 616 | 651 |
| 617 class ImageElement { | |
| 618 ElementEvents on; | |
| 619 String _src; | 652 String _src; |
| 620 int _width; | 653 int _width; |
| 621 int _height; | 654 int _height; |
| 622 | 655 |
| 623 get src => _src; | 656 get src => _src; |
| 624 set src(String v) { | 657 set src(String v) { |
| 625 _src = v; | 658 _src = v; |
| 626 for (var e in on.load) { | 659 var e = new Event('load'); |
| 627 e(this); | 660 e.target = this; |
| 628 } | 661 dispatchEvent(e); |
| 629 } | 662 } |
| 630 | 663 |
| 631 get width => _width; | 664 get width => _width; |
| 632 set width(int widthp) => _width = widthp; | 665 set width(int widthp) => _width = widthp; |
| 633 | 666 |
| 634 get height => _height; | 667 get height => _height; |
| 635 set height(int heightp) => _height = heightp; | 668 set height(int heightp) => _height = heightp; |
| 636 | 669 |
| 637 ImageElement({String srcp, int widthp, int heightp}) | 670 ImageElement({String srcp, int widthp, int heightp}) |
| 638 : on = new ElementEvents(), | 671 : _src = srcp, |
| 639 _src = srcp, | |
| 640 _width = widthp, | 672 _width = widthp, |
| 641 _height = heightp { | 673 _height = heightp { |
| 642 } | 674 } |
| 643 } | 675 } |
| 644 | 676 |
| 645 class ImageData { | 677 class ImageData { |
| 646 final Uint8ClampedArray data; | 678 final Uint8ClampedArray data; |
| 647 final int height; | 679 final int height; |
| 648 final int width; | 680 final int width; |
| 649 ImageData(this.height, this.width, this.data); | 681 ImageData(this.height, this.width, this.data); |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 num dirtyWidth, num dirtyHeight]) { | 1027 num dirtyWidth, num dirtyHeight]) { |
| 996 throw new Exception('Unimplemented webkitGetImageDataHD'); | 1028 throw new Exception('Unimplemented webkitGetImageDataHD'); |
| 997 } | 1029 } |
| 998 | 1030 |
| 999 // TODO(vsm): Kill. | 1031 // TODO(vsm): Kill. |
| 1000 noSuchMethod(invocation) { | 1032 noSuchMethod(invocation) { |
| 1001 throw new Exception('Unimplemented/unknown ${invocation.memberName}'); | 1033 throw new Exception('Unimplemented/unknown ${invocation.memberName}'); |
| 1002 } | 1034 } |
| 1003 } | 1035 } |
| 1004 | 1036 |
| OLD | NEW |