summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/blur-my-shell@aunetx/components/popup/surface_transitions.js
blob: 70fd520bc96320ed019a13dbad5d08358c3f2690 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const GEOMETRY_TRANSITION_PROPERTIES = [
    'x',
    'y',
    'width',
    'height',
    'clip-rect',
    'clip_rect',
    'translation-x',
    'translation-y',
    'translation_x',
    'translation_y',
    'scale-x',
    'scale-y',
    'scale_x',
    'scale_y',
];

export const PopupBlurSurfaceTransitions = class PopupBlurSurfaceTransitions {
    constructor(surface) {
        this.surface = surface;
    }

    has_running() {
        return this.get_state().running;
    }

    get_state(include_descendants = false) {
        const surface_actors = this.get_surface_actors();
        const actors = include_descendants
            ? this.get_actors(surface_actors)
            : surface_actors;
        const geometry_properties = this.get_running_properties(
            GEOMETRY_TRANSITION_PROPERTIES,
            actors
        );
        const opacity = this.has_running_for(['opacity'], surface_actors);

        return {
            complete: include_descendants,
            running: opacity || geometry_properties.size > 0,
            geometry: geometry_properties.size > 0,
            geometry_properties,
            opacity,
        };
    }

    complete_state(transition_state) {
        if (transition_state.complete)
            return transition_state;

        return this.get_state(true);
    }

    has_running_for(properties, actors) {
        return actors.some(actor =>
            properties.some(property => this.has_transition(actor, property))
        );
    }

    get_running_properties(properties, actors) {
        const running_properties = new Set();

        actors.forEach(actor => {
            properties.forEach(property => {
                if (this.has_transition(actor, property))
                    running_properties.add(this.normalize_property(property));
            });
        });

        return running_properties;
    }

    normalize_property(property) {
        return property.replace(/_/g, '-');
    }

    get_actors(surface_actors = this.get_surface_actors()) {
        const actors = [...surface_actors];
        const seen = new WeakSet();
        actors.forEach(actor => seen.add(actor));

        if (!this.surface.is_quick_settings()) {
            this.add_descendants(actors, seen, this.surface.target);
            this.add_descendants(actors, seen, this.surface.root_actor);
        }

        return actors;
    }

    get_surface_actors() {
        const actors = [];
        const seen = new WeakSet();

        this.add_actor(actors, seen, this.surface.target);
        this.add_actor(actors, seen, this.surface.root_actor);
        this.add_actor(actors, seen, this.surface.get_geometry_actor());
        this.add_ancestors(actors, seen, this.surface.target);
        this.add_ancestors(actors, seen, this.surface.root_actor);

        return actors;
    }

    add_actor(actors, seen, actor) {
        if (!actor || seen.has(actor))
            return;

        seen.add(actor);
        actors.push(actor);
    }

    add_ancestors(actors, seen, actor) {
        actor = actor?.get_parent();
        while (actor && actor !== this.surface.parent) {
            this.add_actor(actors, seen, actor);
            actor = actor.get_parent();
        }
    }

    add_descendants(actors, seen, actor) {
        const stack = this.get_children(actor).slice().reverse();

        while (stack.length > 0) {
            const child = stack.pop();
            if (seen.has(child))
                continue;

            this.add_actor(actors, seen, child);
            stack.push(...this.get_children(child).slice().reverse());
        }
    }

    has_transition(actor, property) {
        return !!actor?.get_transition(property);
    }

    get_children(actor) {
        return actor ? actor.get_children() : [];
    }
};