summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/blur-my-shell@aunetx/conveniences/effects_manager.js
blob: 8b99396ad528aa8ccebc9cee3c034db577c81df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { get_supported_effects } from '../effects/effects.js';

/// An object to manage effects (by not destroying them all the time)
export const EffectsManager = class EffectsManager {
    constructor(connections) {
        this.connections = connections;
        this.used = new Set();
        this.SUPPORTED_EFFECTS = get_supported_effects();

        Object.keys(this.SUPPORTED_EFFECTS).forEach(effect_name => {
            // init the arrays containing each unused effect
            this[effect_name + '_effects'] = [];

            // init the functions for each effect
            this['new_' + effect_name + '_effect'] = function (params) {
                let effect;
                if (this[effect_name + '_effects'].length > 0) {
                    effect = this[effect_name + '_effects'].pop();
                    effect.set({
                        ...this.SUPPORTED_EFFECTS[effect_name].class.default_params, ...params
                    });
                } else {
                    effect = new this.SUPPORTED_EFFECTS[effect_name].class({
                        ...this.SUPPORTED_EFFECTS[effect_name].class.default_params, ...params
                    });
                    this.connect_to_destroy(effect);
                }

                this.used.add(effect);
                return effect;
            };
        });
    }

    connect_to_destroy(effect) {
        const update_actor = () => {
            const actor = effect.get_actor();
            if (actor === effect._bms_actor)
                return;

            this.disconnect_actor_destroy(effect);
            effect._bms_actor = actor;
            if (actor)
                effect._bms_actor_destroy_id = actor.connect(
                    'destroy', () => this.remove(effect, true)
                );
        };

        this.connections.connect(effect, 'notify::actor', update_actor);
        update_actor();
    }

    disconnect_actor_destroy(effect) {
        if (effect._bms_actor && effect._bms_actor_destroy_id) {
            effect._bms_actor.disconnect(effect._bms_actor_destroy_id);
        }
        effect._bms_actor = null;
        effect._bms_actor_destroy_id = null;
    }

    remove(effect) {
        const actor = effect.get_actor();
        if (actor)
            actor.remove_effect(effect);
        this.disconnect_actor_destroy(effect);

        if (this.used.delete(effect)) {
            Object.keys(this.SUPPORTED_EFFECTS).forEach(effect_name => {
                if (effect instanceof this.SUPPORTED_EFFECTS[effect_name].class)
                    this[effect_name + '_effects'].push(effect);
            });
        }
    }

    destroy_all() {
        const immutable_used_list = [...this.used];
        immutable_used_list.forEach(effect => this.remove(effect));
        Object.keys(this.SUPPORTED_EFFECTS).forEach(effect_name => {
            this[effect_name + '_effects'].splice(0, this[effect_name + '_effects'].length);
        });
    }

    _warn(str) {
        console.warn(`[Blur my Shell > effects mng]  ${str}`);
    }
};