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