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

Side by Side Diff: runtime/embedders/openglui/common/gl.dart

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

Powered by Google App Engine
This is Rietveld 408576698