flutter_flutter/sdk/lib/framework/scheduler.dart
Adam Barth 6eba4a04f9 Add a simple inksplash example
We'll eventually turn this into a full fn2 component, but for now it's just an
example.

To make this work, I created a schedule.dart as a start to implementing
scheduler.md. For now, I've kept the API similar to the web platform so that
the old world can continue use it backed by sky.window.requestAnimationFrame.

R=eseidel@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1145973009
2015-05-29 13:55:12 -07:00

62 lines
1.5 KiB
Dart

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:sky' as sky;
typedef void Callback(double timeStamp);
bool _haveScheduledVisualUpdate = false;
int _nextCallbackId = 1;
final List<Callback> _persistentCallbacks = new List<Callback>();
Map<int, Callback> _transientCallbacks = new Map<int, Callback>();
void _beginFrame(double timeStamp) {
_haveScheduledVisualUpdate = false;
Map<int, Callback> callbacks = _transientCallbacks;
_transientCallbacks = new Map<int, Callback>();
callbacks.forEach((id, callback) {
callback(timeStamp);
});
for (Callback callback in _persistentCallbacks)
callback(timeStamp);
}
void init() {
assert(sky.window == null);
sky.view.setBeginFrameCallback(_beginFrame);
}
void addPersistentFrameCallback(Callback callback) {
assert(sky.window == null);
_persistentCallbacks.add(callback);
}
int requestAnimationFrame(Callback callback) {
if (sky.window != null)
return sky.window.requestAnimationFrame(callback);
int id = _nextCallbackId++;
_transientCallbacks[id] = callback;
ensureVisualUpdate();
return id;
}
void cancelAnimationFrame(int id) {
if (sky.window != null)
return sky.window.cancelAnimationFrame(id);
_transientCallbacks.remove(id);
}
void ensureVisualUpdate() {
assert(sky.window == null);
if (_haveScheduledVisualUpdate)
return;
sky.view.scheduleFrame();
_haveScheduledVisualUpdate = true;
}