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

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
« no previous file with comments | « samples/swarm/Views.dart ('k') | samples/swarm/swarm_ui_lib/base/DomWrapper.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) 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; 44 Timer _timer;
45 bool _isMobileSafari = false; 45 bool _isMobileSafari = false;
46 CssStyleDeclaration _safariHackStyle; 46 CssStyleDeclaration _safariHackStyle;
47 int _frameCount = 0; 47 int _frameCount = 0;
48 bool _webkitAnimationFrameMaybeAvailable = true; 48 bool _webkitAnimationFrameMaybeAvailable = true;
49 49
50 AnimationScheduler() 50 AnimationScheduler()
51 : _callbacks = new List<CallbackData>() { 51 : _callbacks = new List<CallbackData>() {
52 if (_isMobileSafari) { 52 if (_isMobileSafari) {
53 // TODO(jacobr): find a better workaround for the issue that 3d transforms 53 // TODO(jacobr): find a better workaround for the issue that 3d transforms
54 // sometimes don't render on iOS without forcing a layout. 54 // sometimes don't render on iOS without forcing a layout.
(...skipping 22 matching lines...) Expand all
77 int requestAnimationFrame(AnimationCallback callback, 77 int requestAnimationFrame(AnimationCallback callback,
78 [Element element = null, 78 [Element element = null,
79 num minTime = null]) { 79 num minTime = null]) {
80 final callbackData = new CallbackData(callback, minTime); 80 final callbackData = new CallbackData(callback, minTime);
81 _requestAnimationFrameHelper(callbackData); 81 _requestAnimationFrameHelper(callbackData);
82 return callbackData.id; 82 return callbackData.id;
83 } 83 }
84 84
85 void _requestAnimationFrameHelper(CallbackData callbackData) { 85 void _requestAnimationFrameHelper(CallbackData callbackData) {
86 _callbacks.add(callbackData); 86 _callbacks.add(callbackData);
87 if (_intervalId == null) { 87 if (_timer == null) {
88 _setupInterval(); 88 _setupInterval();
89 } 89 }
90 } 90 }
91 91
92 void _setupInterval() { 92 void _setupInterval() {
93 // Assert that we never schedule multiple frames at once. 93 // Assert that we never schedule multiple frames at once.
94 assert(_intervalId == null); 94 assert(__timer == null);
95 if (USE_INTERVALS) { 95 if (USE_INTERVALS) {
96 _intervalId = window.setInterval(_step, MS_PER_FRAME); 96 _timer = new Timer.repeating(const Duration(milliseconds: MS_PER_FRAME),
97 (_) { _step(); });
97 } else { 98 } else {
98 if (_webkitAnimationFrameMaybeAvailable) { 99 if (_webkitAnimationFrameMaybeAvailable) {
99 try { 100 try {
100 // TODO(jacobr): passing in document should not be required. 101 // TODO(jacobr): passing in document should not be required.
101 _intervalId = window.webkitRequestAnimationFrame( 102 window.webkitRequestAnimationFrame(
102 (int ignored) { _step(); }); 103 (int ignored) { _step(); });
103 // TODO(jacobr) fix this odd type error. 104 // TODO(jacobr) fix this odd type error.
104 } catch (e) { 105 } catch (e) {
105 _webkitAnimationFrameMaybeAvailable = false; 106 _webkitAnimationFrameMaybeAvailable = false;
106 } 107 }
107 } 108 }
108 if (!_webkitAnimationFrameMaybeAvailable) { 109 if (!_webkitAnimationFrameMaybeAvailable) {
109 _intervalId = window.setTimeout(() { _step(); }, MS_PER_FRAME); 110 _timer = new Timer(const Duration(milliseconds: MS_PER_FRAME),
111 _step());
110 } 112 }
111 } 113 }
112 } 114 }
113 115
114 void _step() { 116 void _step() {
115 if (_callbacks.isEmpty) { 117 if (_callbacks.isEmpty) {
116 // Cancel the interval on the first frame where there aren't actually 118 // Cancel the interval on the first frame where there aren't actually
117 // any available callbacks. 119 // any available callbacks.
118 assert(_intervalId != null); 120 assert(_timer != null);
119 if (USE_INTERVALS) { 121 if (USE_INTERVALS) {
120 window.clearInterval(_intervalId); 122 _timer.cancel();
121 } 123 }
122 _intervalId = null; 124 _timer = null;
123 } else if (USE_INTERVALS == false) { 125 } else if (USE_INTERVALS == false) {
124 _intervalId = null; 126 _timer = null;
125 _setupInterval(); 127 _setupInterval();
126 } 128 }
127 int numRemaining = 0; 129 int numRemaining = 0;
128 int minTime = new DateTime.now().millisecondsSinceEpoch + MS_PER_FRAME; 130 int minTime = new DateTime.now().millisecondsSinceEpoch + MS_PER_FRAME;
129 131
130 int len = _callbacks.length; 132 int len = _callbacks.length;
131 for (final callback in _callbacks) { 133 for (final callback in _callbacks) {
132 if (!callback.ready(minTime)) { 134 if (!callback.ready(minTime)) {
133 numRemaining++; 135 numRemaining++;
134 } 136 }
(...skipping 24 matching lines...) Expand all
159 _frameCount++; 161 _frameCount++;
160 if (_isMobileSafari) { 162 if (_isMobileSafari) {
161 // Hack to work around an iOS bug where sometimes animations do not 163 // Hack to work around an iOS bug where sometimes animations do not
162 // render if only webkit transforms were modified. 164 // render if only webkit transforms were modified.
163 // TODO(jacobr): find a cleaner workaround. 165 // TODO(jacobr): find a cleaner workaround.
164 int offset = _frameCount % 2; 166 int offset = _frameCount % 2;
165 _safariHackStyle.left = '${offset}px'; 167 _safariHackStyle.left = '${offset}px';
166 } 168 }
167 } 169 }
168 } 170 }
OLDNEW
« no previous file with comments | « samples/swarm/Views.dart ('k') | samples/swarm/swarm_ui_lib/base/DomWrapper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698