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

Side by Side Diff: samples/swarm/swarm_ui_lib/base/AnimationScheduler.dart

Issue 12218131: Combine window.setTimeout/setInterval with Timer and Timer.repeating. Also (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 base; 5 part of base;
6 6
7 typedef void AnimationCallback(num currentTime); 7 typedef void AnimationCallback(num currentTime);
8 8
9 class CallbackData { 9 class CallbackData {
10 final AnimationCallback callback; 10 final AnimationCallback callback;
(...skipping 10 matching lines...) Expand all
21 _nextId = 1; 21 _nextId = 1;
22 } 22 }
23 id = _nextId++; 23 id = _nextId++;
24 } 24 }
25 } 25 }
26 26
27 /** 27 /**
28 * Animation scheduler implementing the functionality provided by 28 * Animation scheduler implementing the functionality provided by
29 * [:window.requestAnimationFrame:] for platforms that do not support it 29 * [:window.requestAnimationFrame:] for platforms that do not support it
30 * or support it badly. When multiple UI components are animating at once, 30 * or support it badly. When multiple UI components are animating at once,
31 * this approach yields superior performance to calling setTimeout directly as 31 * this approach yields superior performance to calling setTimeout/Timer
32 * all pieces of the UI will animate at the same time resulting in fewer 32 * directly as all pieces of the UI will animate at the same time resulting in
33 * layouts. 33 * fewer layouts.
34 */ 34 */
35 // TODO(jacobr): use window.requestAnimationFrame when it is available and 35 // TODO(jacobr): use window.requestAnimationFrame when it is available and
36 // 60fps for the current browser. 36 // 60fps for the current browser.
37 class AnimationScheduler { 37 class AnimationScheduler {
38 static const FRAMES_PER_SECOND = 60; 38 static const FRAMES_PER_SECOND = 60;
39 static const MS_PER_FRAME = 1000 ~/ FRAMES_PER_SECOND; 39 static const MS_PER_FRAME = 1000 ~/ FRAMES_PER_SECOND;
40 static const USE_INTERVALS = false; 40 static const USE_INTERVALS = false;
41 41
42 /** List of callbacks to be executed next animation frame. */ 42 /** List of callbacks to be executed next animation frame. */
43 List<CallbackData> _callbacks; 43 List<CallbackData> _callbacks;
44 int _intervalId;
45 bool _isMobileSafari = false; 44 bool _isMobileSafari = false;
46 CssStyleDeclaration _safariHackStyle; 45 CssStyleDeclaration _safariHackStyle;
47 int _frameCount = 0; 46 int _frameCount = 0;
48 bool _webkitAnimationFrameMaybeAvailable = true; 47 bool _webkitAnimationFrameMaybeAvailable = true;
49 48
50 AnimationScheduler() 49 AnimationScheduler()
51 : _callbacks = new List<CallbackData>() { 50 : _callbacks = new List<CallbackData>() {
52 if (_isMobileSafari) { 51 if (_isMobileSafari) {
53 // TODO(jacobr): find a better workaround for the issue that 3d transforms 52 // TODO(jacobr): find a better workaround for the issue that 3d transforms
54 // sometimes don't render on iOS without forcing a layout. 53 // sometimes don't render on iOS without forcing a layout.
(...skipping 22 matching lines...) Expand all
77 int requestAnimationFrame(AnimationCallback callback, 76 int requestAnimationFrame(AnimationCallback callback,
78 [Element element = null, 77 [Element element = null,
79 num minTime = null]) { 78 num minTime = null]) {
80 final callbackData = new CallbackData(callback, minTime); 79 final callbackData = new CallbackData(callback, minTime);
81 _requestAnimationFrameHelper(callbackData); 80 _requestAnimationFrameHelper(callbackData);
82 return callbackData.id; 81 return callbackData.id;
83 } 82 }
84 83
85 void _requestAnimationFrameHelper(CallbackData callbackData) { 84 void _requestAnimationFrameHelper(CallbackData callbackData) {
86 _callbacks.add(callbackData); 85 _callbacks.add(callbackData);
87 if (_intervalId == null) { 86 if (_timer == null) {
blois 2013/02/13 03:09:24 Is _timer declared somewhere?
Emily Fortuna 2013/02/13 20:19:44 Done.
88 _setupInterval(); 87 _setupInterval();
89 } 88 }
90 } 89 }
91 90
92 void _setupInterval() { 91 void _setupInterval() {
93 // Assert that we never schedule multiple frames at once. 92 // Assert that we never schedule multiple frames at once.
94 assert(_intervalId == null); 93 assert(__timer == null);
95 if (USE_INTERVALS) { 94 if (USE_INTERVALS) {
96 _intervalId = window.setInterval(_step, MS_PER_FRAME); 95 _timer = new Timer.repeating(const Duration(milliseconds: MS_PER_FRAME),
96 _step);
floitsch 2013/02/13 10:03:07 The original code doesn't look like it's repeating
Emily Fortuna 2013/02/13 20:19:44 setInterval fires repeatedly: https://developer.mo
97 } else { 97 } else {
98 if (_webkitAnimationFrameMaybeAvailable) { 98 if (_webkitAnimationFrameMaybeAvailable) {
99 try { 99 try {
100 // TODO(jacobr): passing in document should not be required. 100 // TODO(jacobr): passing in document should not be required.
101 _intervalId = window.webkitRequestAnimationFrame( 101 window.webkitRequestAnimationFrame(
102 (int ignored) { _step(); }); 102 (int ignored) { _step(); });
103 // TODO(jacobr) fix this odd type error. 103 // TODO(jacobr) fix this odd type error.
104 } catch (e) { 104 } catch (e) {
105 _webkitAnimationFrameMaybeAvailable = false; 105 _webkitAnimationFrameMaybeAvailable = false;
106 } 106 }
107 } 107 }
108 if (!_webkitAnimationFrameMaybeAvailable) { 108 if (!_webkitAnimationFrameMaybeAvailable) {
109 _intervalId = window.setTimeout(() { _step(); }, MS_PER_FRAME); 109 _timer = new Timer(const Duration(milliseconds: MS_PER_FRAME),
110 () { _step(); });
floitsch 2013/02/13 10:03:07 remove the closure. Just "_step".
Emily Fortuna 2013/02/13 20:19:44 Done.
110 } 111 }
111 } 112 }
112 } 113 }
113 114
114 void _step() { 115 void _step() {
115 if (_callbacks.isEmpty) { 116 if (_callbacks.isEmpty) {
116 // Cancel the interval on the first frame where there aren't actually 117 // Cancel the interval on the first frame where there aren't actually
117 // any available callbacks. 118 // any available callbacks.
118 assert(_intervalId != null); 119 assert(_timer != null);
119 if (USE_INTERVALS) { 120 if (USE_INTERVALS) {
120 window.clearInterval(_intervalId); 121 _timer.cancel();
121 } 122 }
122 _intervalId = null; 123 _timer = null;
123 } else if (USE_INTERVALS == false) { 124 } else if (USE_INTERVALS == false) {
124 _intervalId = null; 125 _timer = null;
125 _setupInterval(); 126 _setupInterval();
126 } 127 }
127 int numRemaining = 0; 128 int numRemaining = 0;
128 int minTime = new DateTime.now().millisecondsSinceEpoch + MS_PER_FRAME; 129 int minTime = new DateTime.now().millisecondsSinceEpoch + MS_PER_FRAME;
129 130
130 int len = _callbacks.length; 131 int len = _callbacks.length;
131 for (final callback in _callbacks) { 132 for (final callback in _callbacks) {
132 if (!callback.ready(minTime)) { 133 if (!callback.ready(minTime)) {
133 numRemaining++; 134 numRemaining++;
134 } 135 }
(...skipping 24 matching lines...) Expand all
159 _frameCount++; 160 _frameCount++;
160 if (_isMobileSafari) { 161 if (_isMobileSafari) {
161 // Hack to work around an iOS bug where sometimes animations do not 162 // Hack to work around an iOS bug where sometimes animations do not
162 // render if only webkit transforms were modified. 163 // render if only webkit transforms were modified.
163 // TODO(jacobr): find a cleaner workaround. 164 // TODO(jacobr): find a cleaner workaround.
164 int offset = _frameCount % 2; 165 int offset = _frameCount % 2;
165 _safariHackStyle.left = '${offset}px'; 166 _safariHackStyle.left = '${offset}px';
166 } 167 }
167 } 168 }
168 } 169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698