Migrate swarm's swarm_ui_lib/base

Change-Id: Ida800ca2ab841b3f0077a02b5796289d14176642
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/248260
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Vijay Menon <vsm@google.com>
This commit is contained in:
Vijay Menon 2022-06-13 21:53:53 +00:00 committed by Commit Bot
parent 5930e0c54d
commit e1b9705aee
6 changed files with 22 additions and 49 deletions

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
part of base;
typedef void AnimationCallback(num currentTime);
@ -11,19 +9,13 @@ typedef void AnimationCallback(num currentTime);
class CallbackData {
final AnimationCallback callback;
final num minTime;
int id;
final int id;
static int _nextId;
static int _nextId = 1;
bool ready(num time) => minTime == null || minTime <= time;
CallbackData(this.callback, this.minTime) {
// TODO(jacobr): static init needs cleanup, see http://b/4161827
if (_nextId == null) {
_nextId = 1;
}
id = _nextId++;
}
CallbackData(this.callback, this.minTime) : id = _nextId++;
}
/**
@ -43,15 +35,15 @@ class AnimationScheduler {
/** List of callbacks to be executed next animation frame. */
List<CallbackData> _callbacks;
bool _isMobileSafari = false;
CssStyleDeclaration _safariHackStyle;
late CssStyleDeclaration _safariHackStyle;
int _frameCount = 0;
AnimationScheduler() : _callbacks = new List<CallbackData>() {
AnimationScheduler() : _callbacks = List<CallbackData>.empty() {
if (_isMobileSafari) {
// TODO(jacobr): find a better workaround for the issue that 3d transforms
// sometimes don't render on iOS without forcing a layout.
final element = new Element.tag('div');
document.body.nodes.add(element);
final element = Element.tag('div');
document.body!.nodes.add(element);
_safariHackStyle = element.style;
_safariHackStyle.position = 'absolute';
}
@ -73,8 +65,8 @@ class AnimationScheduler {
* pending callback.
*/
int requestAnimationFrame(AnimationCallback callback,
[Element element = null, num minTime = null]) {
final callbackData = new CallbackData(callback, minTime);
[Element? element = null, num? minTime = null]) {
final callbackData = CallbackData(callback, minTime!);
_requestAnimationFrameHelper(callbackData);
return callbackData.id;
}
@ -98,7 +90,7 @@ class AnimationScheduler {
_setupInterval();
}
int numRemaining = 0;
int minTime = new DateTime.now().millisecondsSinceEpoch + MS_PER_FRAME;
int minTime = DateTime.now().millisecondsSinceEpoch + MS_PER_FRAME;
int len = _callbacks.length;
for (final callback in _callbacks) {
@ -114,7 +106,7 @@ class AnimationScheduler {
}
// Some callbacks need to be executed.
final currentCallbacks = _callbacks;
_callbacks = new List<CallbackData>();
_callbacks = List<CallbackData>.empty();
for (final callbackData in currentCallbacks) {
if (callbackData.ready(minTime)) {

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
part of base;
// TODO(jacobr): cache these results.
@ -15,17 +13,17 @@ class Device {
/**
* The regular expression for detecting an iPhone or iPod.
*/
static final _IPHONE_REGEX = new RegExp('iPhone|iPod');
static final _IPHONE_REGEX = RegExp('iPhone|iPod');
/**
* The regular expression for detecting an iPhone or iPod or iPad.
*/
static final _MOBILE_SAFARI_REGEX = new RegExp('iPhone|iPod|iPad');
static final _MOBILE_SAFARI_REGEX = RegExp('iPhone|iPod|iPad');
/**
* The regular expression for detecting an iPhone or iPod or iPad simulator.
*/
static final _APPLE_SIM_REGEX = new RegExp('iP.*Simulator');
static final _APPLE_SIM_REGEX = RegExp('iP.*Simulator');
/**
* Gets the browser's user agent. Using this function allows tests to inject
@ -75,11 +73,5 @@ class Device {
*/
static bool get isWebOs => userAgent.contains("webOS", 0);
static bool _supportsTouch;
static bool get supportsTouch {
if (_supportsTouch == null) {
_supportsTouch = isMobileSafari || isAndroid;
}
return _supportsTouch;
}
static late bool supportsTouch = isMobileSafari || isAndroid;
}

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
part of base;
/**
@ -24,9 +22,9 @@ class Dom {
/** Adds the given <style> text to the page. */
static void addStyle(String cssText) {
StyleElement style = new Element.tag('style');
var style = Element.tag('style') as StyleElement;
style.type = 'text/css';
style.text = cssText;
document.head.nodes.add(style);
document.head!.nodes.add(style);
}
}

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
part of base;
/**
@ -13,7 +11,7 @@ part of base;
* of the browser, the window object is available.
*/
class Env {
static AnimationScheduler _animationScheduler;
static AnimationScheduler _animationScheduler = AnimationScheduler();
/**
* Provides functionality similar to [:window.requestAnimationFrame:] for
@ -23,10 +21,7 @@ class Env {
* cancel the pending callback.
*/
static int requestAnimationFrame(AnimationCallback callback,
[Element element = null, num minTime = null]) {
if (_animationScheduler == null) {
_animationScheduler = new AnimationScheduler();
}
[Element? element = null, num? minTime = null]) {
return _animationScheduler.requestAnimationFrame(
callback, element, minTime);
}

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
part of base;
/**
@ -19,7 +17,7 @@ class Size {
return other != null && width == other.width && height == other.height;
}
int get hashCode => throw new UnimplementedError();
int get hashCode => throw UnimplementedError();
/**
* Returns the area of the size (width * height).
@ -46,10 +44,10 @@ class Size {
}
/**
* Returns a new copy of the Size.
* Returns a copy of the Size.
*/
Size clone() {
return new Size(width, height);
return Size(width, height);
}
/**

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
library base;
import 'dart:html';