[gnome-weather] City: clean up the animation code
- From: Giovanni Campagna <gcampagna src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-weather] City: clean up the animation code
- Date: Sat, 7 Mar 2015 04:40:46 +0000 (UTC)
commit 819c1e250f32b679289f4b29ac495611729dd86e
Author: Giovanni Campagna <gcampagna src gnome org>
Date: Fri Mar 6 20:04:55 2015 -0800
City: clean up the animation code
By removing JS Date, which is ugly, and by removing magic numbers.
src/app/city.js | 63 +++++++++++++++++++++++++----------------------------
src/misc/util.js | 5 ++++
2 files changed, 35 insertions(+), 33 deletions(-)
---
diff --git a/src/app/city.js b/src/app/city.js
index e5ace08..939933c 100644
--- a/src/app/city.js
+++ b/src/app/city.js
@@ -28,6 +28,8 @@ const Util = imports.misc.util;
const SPINNER_SIZE = 128;
+const SCROLLING_ANIMATION_TIME = 400000; //us
+
const WeatherWidget = new Lang.Class({
Name: 'WeatherWidget',
Extends: Gtk.Frame,
@@ -95,32 +97,20 @@ const WeatherWidget = new Lang.Class({
}
}));
+ this._tickId = 0;
+
this._leftButton.connect('clicked', Lang.bind(this, function() {
let hadjustment = this._dayStack.visible_child.get_hadjustment();
- this._target = hadjustment.value - hadjustment.page_size;
- if (this._target <= hadjustment.get_lower()) {
- this._leftButton.set_sensitive(false);
- this._rightButton.set_sensitive(true);
- } else
- this._rightButton.set_sensitive(true);
-
- this._start = new Date().getTime();
- this._end = this._start + 328;
- this._tickId = this.add_tick_callback(Lang.bind(this, this._animate));
+ let target = hadjustment.value - hadjustment.page_size;
+
+ this._beginScrollAnimation(target);
}));
this._rightButton.connect('clicked', Lang.bind(this, function() {
let hadjustment = this._dayStack.visible_child.get_hadjustment();
- this._target = hadjustment.value + hadjustment.page_size;
- if (this._target >= hadjustment.get_upper() - hadjustment.page_size) {
- this._rightButton.set_sensitive(false);
- this._leftButton.set_sensitive(true);
- } else
- this._leftButton.set_sensitive(true);
-
- this._start = new Date().getTime();
- this._end = this._start + 328;
- this._tickId = this.add_tick_callback(Lang.bind(this, this._animate));
+ let target = hadjustment.value + hadjustment.page_size;
+
+ this._beginScrollAnimation(target);
}));
this.add(outerBox);
@@ -143,29 +133,36 @@ const WeatherWidget = new Lang.Class({
}
},
- _animate: function() {
+ _beginScrollAnimation: function(target) {
+ let start = this.get_frame_clock().get_frame_time();
+ let end = start + SCROLLING_ANIMATION_TIME;
+
+ if (this._tickId != 0)
+ this.remove_tick_callback(this._tickId);
+
+ this._tickId = this.add_tick_callback(Lang.bind(this, function() {
+ return this._animate(target, start, end);
+ }));
+ },
+
+ _animate: function(target, start, end) {
let hadjustment = this._dayStack.visible_child.get_hadjustment();
let value = hadjustment.value;
let t = 1.0;
- let now = new Date().getTime();
- if (now < this._end) {
- t = (now - this._start) / 700;
- t = this._easeOutCubic (t);
- hadjustment.value = value + t * (this._target - value);
+ let now = this.get_frame_clock().get_frame_time();
+
+ if (now < end) {
+ t = (now - start) / SCROLLING_ANIMATION_TIME;
+ t = Util.easeOutCubic (t);
+ hadjustment.value = value + t * (target - value);
return true;
} else {
- hadjustment.value = value + t * (this._target - value);
- this.remove_tick_callback(this._tickId);
+ hadjustment.value = value + t * (target - value);
this._tickId = 0;
return false;
}
},
- _easeOutCubic: function(value) {
- let temp = value - 1;
- return temp * temp * temp + 1;
- },
-
clear: function() {
for (let t of ['today', 'tomorrow'])
this._forecasts[t].clear();
diff --git a/src/misc/util.js b/src/misc/util.js
index 73b37d2..c46aedb 100644
--- a/src/misc/util.js
+++ b/src/misc/util.js
@@ -170,3 +170,8 @@ function getEnabledProviders() {
return (GWeather.Provider.METAR | GWeather.Provider.YR_NO | GWeather.Provider.OWM);
}
}
+
+function easeOutCubic(value) {
+ let t = value - 1;
+ return t * t * t + 1;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]