2013-06-18 07:35:41 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported Animation, AnimatedIcon, Spinner */
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
const { Clutter, GLib, GObject, Gio, St } = imports.gi;
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2019-11-18 15:18:29 -05:00
|
|
|
const Params = imports.misc.params;
|
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var ANIMATED_ICON_UPDATE_TIMEOUT = 16;
|
2019-08-01 19:13:10 -04:00
|
|
|
var SPINNER_ANIMATION_TIME = 300;
|
|
|
|
var SPINNER_ANIMATION_DELAY = 1000;
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
var Animation = GObject.registerClass(
|
|
|
|
class Animation extends St.Bin {
|
|
|
|
_init(file, width, height, speed) {
|
2019-08-19 15:06:04 -04:00
|
|
|
super._init({ width, height });
|
2019-07-16 05:24:13 -04:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
|
|
|
this.connect('resource-scale-changed',
|
2017-11-29 20:36:05 -05:00
|
|
|
this._loadFile.bind(this, file, width, height));
|
|
|
|
|
2019-02-26 20:08:26 -05:00
|
|
|
let themeContext = St.ThemeContext.get_for_stage(global.stage);
|
|
|
|
this._scaleChangedId = themeContext.connect('notify::scale-factor',
|
|
|
|
this._loadFile.bind(this, file, width, height));
|
|
|
|
|
2013-06-18 07:35:41 -04:00
|
|
|
this._speed = speed;
|
|
|
|
|
|
|
|
this._isLoaded = false;
|
|
|
|
this._isPlaying = false;
|
|
|
|
this._timeoutId = 0;
|
|
|
|
this._frame = 0;
|
2014-03-22 22:20:50 -04:00
|
|
|
|
2017-11-29 20:36:05 -05:00
|
|
|
this._loadFile(file, width, height);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
play() {
|
2013-06-18 07:35:41 -04:00
|
|
|
if (this._isLoaded && this._timeoutId == 0) {
|
|
|
|
if (this._frame == 0)
|
|
|
|
this._showFrame(0);
|
|
|
|
|
2017-12-01 19:27:35 -05:00
|
|
|
this._timeoutId = GLib.timeout_add(GLib.PRIORITY_LOW, this._speed, this._update.bind(this));
|
2014-04-10 13:26:52 -04:00
|
|
|
GLib.Source.set_name_by_id(this._timeoutId, '[gnome-shell] this._update');
|
2013-06-18 07:35:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this._isPlaying = true;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
stop() {
|
2013-06-18 07:35:41 -04:00
|
|
|
if (this._timeoutId > 0) {
|
2019-08-19 14:50:33 -04:00
|
|
|
GLib.source_remove(this._timeoutId);
|
2013-06-18 07:35:41 -04:00
|
|
|
this._timeoutId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._isPlaying = false;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2017-11-29 20:36:05 -05:00
|
|
|
_loadFile(file, width, height) {
|
2019-07-16 05:24:13 -04:00
|
|
|
let [validResourceScale, resourceScale] = this.get_resource_scale();
|
2019-06-14 10:48:16 -04:00
|
|
|
let wasPlaying = this._isPlaying;
|
|
|
|
|
|
|
|
if (this._isPlaying)
|
|
|
|
this.stop();
|
2017-11-29 20:36:05 -05:00
|
|
|
|
|
|
|
this._isLoaded = false;
|
2019-07-16 05:24:13 -04:00
|
|
|
this.destroy_all_children();
|
2017-11-29 20:36:05 -05:00
|
|
|
|
2019-06-14 10:48:16 -04:00
|
|
|
if (!validResourceScale) {
|
|
|
|
if (wasPlaying)
|
|
|
|
this.play();
|
2017-11-29 20:36:05 -05:00
|
|
|
return;
|
2019-06-14 10:48:16 -04:00
|
|
|
}
|
2017-11-29 20:36:05 -05:00
|
|
|
|
2019-01-31 08:43:52 -05:00
|
|
|
let textureCache = St.TextureCache.get_default();
|
2017-11-29 20:36:05 -05:00
|
|
|
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
|
2019-01-31 08:43:52 -05:00
|
|
|
this._animations = textureCache.load_sliced_image(file, width, height,
|
|
|
|
scaleFactor, resourceScale,
|
|
|
|
this._animationsLoaded.bind(this));
|
2019-11-06 06:05:56 -05:00
|
|
|
this._animations.set({
|
|
|
|
x_align: Clutter.ActorAlign.CENTER,
|
|
|
|
y_align: Clutter.ActorAlign.CENTER,
|
|
|
|
});
|
2019-07-16 05:24:13 -04:00
|
|
|
this.set_child(this._animations);
|
2019-06-14 10:48:16 -04:00
|
|
|
|
|
|
|
if (wasPlaying)
|
|
|
|
this.play();
|
2017-11-29 20:36:05 -05:00
|
|
|
}
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_showFrame(frame) {
|
2013-06-18 07:35:41 -04:00
|
|
|
let oldFrameActor = this._animations.get_child_at_index(this._frame);
|
|
|
|
if (oldFrameActor)
|
|
|
|
oldFrameActor.hide();
|
|
|
|
|
2019-08-19 15:38:51 -04:00
|
|
|
this._frame = frame % this._animations.get_n_children();
|
2013-06-18 07:35:41 -04:00
|
|
|
|
|
|
|
let newFrameActor = this._animations.get_child_at_index(this._frame);
|
|
|
|
if (newFrameActor)
|
|
|
|
newFrameActor.show();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_update() {
|
2013-06-18 07:35:41 -04:00
|
|
|
this._showFrame(this._frame + 1);
|
2013-11-28 19:45:39 -05:00
|
|
|
return GLib.SOURCE_CONTINUE;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2017-11-29 20:36:05 -05:00
|
|
|
_syncAnimationSize() {
|
|
|
|
if (!this._isLoaded)
|
|
|
|
return;
|
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
let [width, height] = this.get_size();
|
2017-11-29 20:36:05 -05:00
|
|
|
|
|
|
|
for (let i = 0; i < this._animations.get_n_children(); ++i)
|
|
|
|
this._animations.get_child_at_index(i).set_size(width, height);
|
|
|
|
}
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_animationsLoaded() {
|
2016-11-21 12:09:42 -05:00
|
|
|
this._isLoaded = this._animations.get_n_children() > 0;
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2017-11-29 20:36:05 -05:00
|
|
|
this._syncAnimationSize();
|
|
|
|
|
2013-06-18 07:35:41 -04:00
|
|
|
if (this._isPlaying)
|
|
|
|
this.play();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDestroy() {
|
2013-06-18 07:35:41 -04:00
|
|
|
this.stop();
|
2019-02-26 20:08:26 -05:00
|
|
|
|
|
|
|
let themeContext = St.ThemeContext.get_for_stage(global.stage);
|
|
|
|
if (this._scaleChangedId)
|
|
|
|
themeContext.disconnect(this._scaleChangedId);
|
|
|
|
this._scaleChangedId = 0;
|
2013-06-18 07:35:41 -04:00
|
|
|
}
|
2019-07-16 05:24:13 -04:00
|
|
|
});
|
2013-06-18 07:35:41 -04:00
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
var AnimatedIcon = GObject.registerClass(
|
|
|
|
class AnimatedIcon extends Animation {
|
|
|
|
_init(file, size) {
|
|
|
|
super._init(file, size, size, ANIMATED_ICON_UPDATE_TIMEOUT);
|
2013-06-18 07:35:41 -04:00
|
|
|
}
|
2019-07-16 05:24:13 -04:00
|
|
|
});
|
2018-11-28 10:41:09 -05:00
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
var Spinner = GObject.registerClass(
|
|
|
|
class Spinner extends AnimatedIcon {
|
2019-11-18 15:18:29 -05:00
|
|
|
_init(size, params) {
|
|
|
|
params = Params.parse(params, {
|
|
|
|
animate: false,
|
2019-11-18 15:24:05 -05:00
|
|
|
hideOnStop: false,
|
2019-11-18 15:18:29 -05:00
|
|
|
});
|
2018-11-28 10:41:09 -05:00
|
|
|
let file = Gio.File.new_for_uri('resource:///org/gnome/shell/theme/process-working.svg');
|
2019-07-16 05:24:13 -04:00
|
|
|
super._init(file, size);
|
2018-11-28 11:34:48 -05:00
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
this.opacity = 0;
|
2019-11-18 15:18:29 -05:00
|
|
|
this._animate = params.animate;
|
2019-11-18 15:24:05 -05:00
|
|
|
this._hideOnStop = params.hideOnStop;
|
|
|
|
this.visible = !this._hideOnStop;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-11-28 11:34:48 -05:00
|
|
|
|
2019-01-23 17:40:18 -05:00
|
|
|
_onDestroy() {
|
|
|
|
this._animate = false;
|
2017-10-30 21:19:44 -04:00
|
|
|
super._onDestroy();
|
|
|
|
}
|
2019-01-23 17:40:18 -05:00
|
|
|
|
2018-11-28 11:34:48 -05:00
|
|
|
play() {
|
2019-07-16 05:24:13 -04:00
|
|
|
this.remove_all_transitions();
|
2019-11-18 15:24:05 -05:00
|
|
|
this.show();
|
2018-11-28 11:34:48 -05:00
|
|
|
|
|
|
|
if (this._animate) {
|
2017-10-30 21:19:44 -04:00
|
|
|
super.play();
|
2019-07-16 05:24:13 -04:00
|
|
|
this.ease({
|
2018-11-28 11:34:48 -05:00
|
|
|
opacity: 255,
|
2018-07-20 15:46:19 -04:00
|
|
|
delay: SPINNER_ANIMATION_DELAY,
|
|
|
|
duration: SPINNER_ANIMATION_TIME,
|
2019-08-20 17:43:54 -04:00
|
|
|
mode: Clutter.AnimationMode.LINEAR,
|
2018-11-28 11:34:48 -05:00
|
|
|
});
|
|
|
|
} else {
|
2019-07-16 05:24:13 -04:00
|
|
|
this.opacity = 255;
|
2017-10-30 21:19:44 -04:00
|
|
|
super.play();
|
2018-11-28 11:34:48 -05:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-11-28 11:34:48 -05:00
|
|
|
|
|
|
|
stop() {
|
2019-07-16 05:24:13 -04:00
|
|
|
this.remove_all_transitions();
|
2018-11-28 11:34:48 -05:00
|
|
|
|
|
|
|
if (this._animate) {
|
2019-07-16 05:24:13 -04:00
|
|
|
this.ease({
|
2018-11-28 11:34:48 -05:00
|
|
|
opacity: 0,
|
2019-10-08 23:14:15 -04:00
|
|
|
duration: SPINNER_ANIMATION_TIME,
|
|
|
|
mode: Clutter.AnimationMode.LINEAR,
|
2019-11-18 15:24:05 -05:00
|
|
|
onComplete: () => {
|
|
|
|
super.stop();
|
|
|
|
if (this._hideOnStop)
|
|
|
|
this.hide();
|
|
|
|
},
|
2018-11-28 11:34:48 -05:00
|
|
|
});
|
|
|
|
} else {
|
2019-07-16 05:24:13 -04:00
|
|
|
this.opacity = 0;
|
2017-10-30 21:19:44 -04:00
|
|
|
super.stop();
|
2019-11-18 15:24:05 -05:00
|
|
|
|
|
|
|
if (this._hideOnStop)
|
|
|
|
this.hide();
|
2018-11-28 11:34:48 -05:00
|
|
|
}
|
2018-11-28 10:41:09 -05:00
|
|
|
}
|
2019-07-16 05:24:13 -04:00
|
|
|
});
|