summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/blur-my-shell@aunetx/components/popup/surface_geometry.js
blob: 84832d9a6b752147795e608f4059a96916236875 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import St from 'gi://St';

export function transform_to_actor_space(actor, { x, y, width, height }) {
    if (!actor)
        return null;
    const [actor_x, actor_y] = actor.get_transformed_position();
    const [actor_width, actor_height] = actor.get_size();
    const [transformed_width, transformed_height] = actor.get_transformed_size();
    const scale_x = actor_width > 0 ? transformed_width / actor_width : 1;
    const scale_y = actor_height > 0 ? transformed_height / actor_height : 1;

    return {
        x: (x - actor_x) / scale_x,
        y: (y - actor_y) / scale_y,
        width: width / scale_x,
        height: height / scale_y,
    };
}

export const PopupBlurSurfaceGeometry = class PopupBlurSurfaceGeometry {
    get(actor, { use_content = true, use_margins = true } = {}) {
        const actor_geometry = this.get_transformed_actor(actor);
        if (!this.has_valid_geometry(actor_geometry))
            return actor_geometry;

        const margin_geometry = use_margins
            ? this.get_margin_adjusted(actor, actor_geometry)
            : actor_geometry;
        if (!use_content)
            return margin_geometry;

        const content_geometry = this.get_transformed_content(actor, margin_geometry);

        if (!this.should_use_content(margin_geometry, content_geometry))
            return margin_geometry;

        return content_geometry;
    }

    get_transformed_actor(actor) {
        const extents = actor.get_transformed_extents();
        const top_left = extents?.get_top_left();
        const bottom_right = extents?.get_bottom_right();

        if (top_left && bottom_right) {
            return {
                x: top_left.x,
                y: top_left.y,
                width: bottom_right.x - top_left.x,
                height: bottom_right.y - top_left.y,
            };
        }

        if (!actor)
            return null;
        const [x, y] = actor.get_transformed_position();
        const [width, height] = actor.get_transformed_size();
        return { x, y, width, height };
    }

    get_transformed_clip(actor) {
        if (!actor?.has_clip)
            return null;

        const [clip_x, clip_y, clip_width, clip_height] = actor.get_clip();
        if (clip_width <= 0 || clip_height <= 0)
            return null;

        const [actor_x, actor_y] = actor.get_transformed_position();
        const [actor_width, actor_height] = actor.get_transformed_size();
        const scale_x = this.get_actor_scale(actor, actor_width, 'width');
        const scale_y = this.get_actor_scale(actor, actor_height, 'height');

        return {
            x: actor_x + clip_x * scale_x,
                y: actor_y + clip_y * scale_y,
                width: clip_width * scale_x,
                height: clip_height * scale_y,
        };
    }

    get_margin_adjusted(actor, geometry) {
        const margins = this.get_margins(actor);
        if (!margins)
            return geometry;

        const scale_x = this.get_actor_scale(actor, geometry.width, 'width');
        const scale_y = this.get_actor_scale(actor, geometry.height, 'height');

        return this.shrink(geometry,
            margins.top * scale_y,
            margins.right * scale_x,
            margins.bottom * scale_y,
            margins.left * scale_x
        ) ?? geometry;
    }

    get_transformed_content(actor, boundary) {
        const children = this.get_transformed_children(actor);
        if (!this.has_valid_geometry(children))
            return null;

        const insets = this.get_paint_insets(actor, boundary);
        const geometry = this.inflate(
            children,
            insets.top,
            insets.right,
            insets.bottom,
            insets.left
        );

        return this.intersect(geometry, boundary);
    }

    get_transformed_children(actor) {
        let geometry = null;

        this.get_children(actor).forEach(child => {
            if (!this.is_visible(child))
                return;

            const child_geometry = this.get_transformed_actor(child);
            if (!this.has_valid_geometry(child_geometry))
                return;

            geometry = this.union(geometry, child_geometry);
        });

        return geometry;
    }

    get_margins(actor) {
        const margins = {
            top: this.get_margin(actor, 'top', St.Side.TOP),
            right: this.get_margin(actor, 'right', St.Side.RIGHT),
            bottom: this.get_margin(actor, 'bottom', St.Side.BOTTOM),
            left: this.get_margin(actor, 'left', St.Side.LEFT),
        };

        return Object.values(margins).some(margin => margin > 0) ? margins : null;
    }

    get_margin(actor, side_name, side) {
        const property_names = [
            `margin_${side_name}`,
            `margin${side_name[0].toUpperCase()}${side_name.slice(1)}`,
        ];

        for (const property of property_names) {
            const value = actor[property];
            if (typeof value === 'number')
                return value;
        }

        const getter = actor[`get_margin_${side_name}`];
        if (getter) {
            const value = getter.call(actor);
            if (typeof value === 'number')
                return value;
        }

        try {
            const theme_node = actor.get_theme_node();
            if (!theme_node?.get_margin)
                return 0;

            return theme_node.get_margin(side);
        } catch (e) {
            // get_theme_node() may throw when actor is not yet mapped to a stage
            return 0;
        }
    }

    get_paint_insets(actor, geometry) {
        const scale_x = this.get_actor_scale(actor, geometry.width, 'width');
        const scale_y = this.get_actor_scale(actor, geometry.height, 'height');

        return {
            top: this.get_inset(actor, St.Side.TOP) * scale_y,
            right: this.get_inset(actor, St.Side.RIGHT) * scale_x,
            bottom: this.get_inset(actor, St.Side.BOTTOM) * scale_y,
            left: this.get_inset(actor, St.Side.LEFT) * scale_x,
        };
    }

    get_inset(actor, side) {
        try {
            const theme_node = actor.get_theme_node();
            if (!theme_node)
                return 0;

            let inset = 0;
            if (theme_node.get_padding)
                inset += theme_node.get_padding(side);
            if (theme_node.get_border_width)
                inset += theme_node.get_border_width(side);
            return inset;
        } catch (e) {
            // get_theme_node() may throw when actor is not yet mapped to a stage
            return 0;
        }
    }

    get_actor_scale(actor, transformed_size, property) {
        const size = actor ? (actor[property] ?? (actor[`get_${property}`] ? actor[`get_${property}`]() : 0)) : 0;
        return size > 0 ? transformed_size / size : 1;
    }

    should_use_content(outer, content) {
        if (!this.has_valid_geometry(content))
            return false;

        if (
            content.width > outer.width + 1
            || content.height > outer.height + 1
        )
            return false;

        return outer.width - content.width > 2 || outer.height - content.height > 2;
    }

    is_visible(actor) {
        return actor?.visible && actor.mapped;
    }

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

    has_valid_geometry(geometry) {
        return geometry?.width > 0 && geometry?.height > 0;
    }

    shrink(geometry, top, right, bottom, left) {
        const width = geometry.width - left - right;
        const height = geometry.height - top - bottom;

        if (width <= 0 || height <= 0)
            return null;

        return {
            x: geometry.x + left,
            y: geometry.y + top,
            width,
            height,
        };
    }

    inflate(geometry, top, right, bottom, left) {
        return {
            x: geometry.x - left,
            y: geometry.y - top,
            width: geometry.width + left + right,
            height: geometry.height + top + bottom,
        };
    }

    intersect(a, b) {
        const x1 = Math.max(a.x, b.x);
        const y1 = Math.max(a.y, b.y);
        const x2 = Math.min(a.x + a.width, b.x + b.width);
        const y2 = Math.min(a.y + a.height, b.y + b.height);

        if (x2 <= x1 || y2 <= y1)
            return null;

        return {
            x: x1,
            y: y1,
            width: x2 - x1,
            height: y2 - y1,
        };
    }

    union(a, b) {
        if (!a)
            return b;

        const x1 = Math.min(a.x, b.x);
        const y1 = Math.min(a.y, b.y);
        const x2 = Math.max(a.x + a.width, b.x + b.width);
        const y2 = Math.max(a.y + a.height, b.y + b.height);

        return {
            x: x1,
            y: y1,
            width: x2 - x1,
            height: y2 - y1,
        };
    }
};