summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/blur-my-shell@aunetx/components/popup/targets.js
blob: dd537b0bbeaccdfce188b73b0fc0d660a9ba756f (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
const POPUP_STYLE_CLASSES = ['popup-menu', 'quick-toggle-menu-container', 'candidate-popup-boxpointer'];
const POPUP_TARGET_STYLE_CLASSES = ['popup-menu-content', 'quick-settings', 'quick-toggle-menu', 'notification-banner', 'candidate-popup-content','screenshot-ui-panel'];
const POPUP_CHILD_STYLE_CLASSES = ['osd-window', 'resize-popup', 'switcher-list', 'workspace-switcher', 'modal-dialog', 'run-dialog'];
const POPUP_TARGET_STYLE_CLASSES_OSK = ['bms-keyboard-surface']
const POPUP_DESCENDANT_TARGET_STYLE_CLASSES = ['switcher-list'];

export const POPUP_BACKGROUND_STYLES = ['bms-popup-background-transparent', 'bms-popup-background-light', 'bms-popup-background-dark'];
export const POPUP_SURFACE_STYLES = POPUP_BACKGROUND_STYLES.map(
    style => style.replace('bms-popup-background-', 'bms-popup-surface-')
);
export const DEFAULT_CORNER_RADIUS = { key: 'corner-radius', property: 'CORNER_RADIUS' };
export const POPUP_CORNER_RADII = [
    {
        key: 'quick-settings-corner-radius',
        property: 'QUICK_SETTINGS_CORNER_RADIUS',
        style_classes: ['quick-settings', 'quick-toggle-menu', 'datemenu-popover','screenshot-ui-panel'],
    },
    {
        key: 'notification-corner-radius',
        property: 'NOTIFICATION_CORNER_RADIUS',
        style_classes: ['notification-banner', 'message', 'message-view', 'message-list'],
    },
    {
        key: 'menu-corner-radius',
        property: 'MENU_CORNER_RADIUS',
        style_classes: ['popup-menu-content', 'popup-menu', 'candidate-popup-content', 'candidate-popup-boxpointer'],
    },
    {
        key: 'osd-corner-radius',
        property: 'OSD_CORNER_RADIUS',
        style_classes: ['osd-window', 'resize-popup', 'switcher-list', 'workspace-switcher'],
    },
    {
        key: 'dialog-corner-radius',
        property: 'DIALOG_CORNER_RADIUS',
        style_classes: ['modal-dialog', 'run-dialog'],
    },
    {
        key: 'osk-corner-radius',
        property: 'OSK_CORNER_RADIUS',
        style_classes: ['bms-keyboard-surface'],
    },
];

export const PopupBlurTargets = class PopupBlurTargets {
    constructor(actor) {
        this.actor = actor;
        this.style_cache = null;
    }

    find(actor) {
        return this.with_style_cache(() => this.find_actor(actor));
    }

    find_actor(actor) {
        if (this.actor.is_internal_actor(actor) || !this.actor.watch_actor(actor))
            return [];

        const targets = this.create_targets();
        const delegate = this.actor.get_actor_delegate(actor);

        if (this.prefers_descendant_targets(actor)) {
            this.find_target_children(actor, targets);
            if (targets.actors.length > 0)
                return targets.actors;
        }

        if (this.actor.watch_actor(delegate?.box) && (this.has_any_style_class(delegate.box, POPUP_TARGET_STYLE_CLASSES) || this.has_any_style_class(delegate.box, POPUP_TARGET_STYLE_CLASSES_OSK)))
            this.add(targets, delegate.box);

        if (this.has_any_style_class(actor, POPUP_TARGET_STYLE_CLASSES))
            this.add(targets, actor);

        if (this.has_any_style_class(actor, POPUP_TARGET_STYLE_CLASSES_OSK))
            this.add(targets, actor);

        if (this.has_any_style_class(actor, POPUP_CHILD_STYLE_CLASSES))
            this.add(targets, actor);

        const dialog_layout = this.actor.get_actor_dialog_layout(actor);
        if (this.actor.watch_actor(dialog_layout) && this.has_any_style_class(dialog_layout, POPUP_CHILD_STYLE_CLASSES))
            this.add(targets, dialog_layout);

        if (targets.actors.length === 0)
            this.find_target_children(actor, targets);

        if (
            targets.actors.length === 0
            && this.has_any_style_class(actor, POPUP_STYLE_CLASSES)
        )
            this.add(targets, actor);

        return targets.actors;
    }

    find_target_children(actor, targets, seen = new WeakSet()) {
        const stack = this.get_children(actor)
            .slice()
            .reverse()
            .map(child => ({ actor: child, fallback_count: null }));

        while (stack.length > 0) {
            const { actor: child, fallback_count } = stack.pop();
            if (
                !child
                || this.actor.is_internal_actor(child)
            )
                continue;

            if (fallback_count !== null) {
                if (
                    targets.actors.length === fallback_count
                    && this.is_blur_target_actor(child)
                )
                    this.add(targets, child);
                continue;
            }

            if (seen.has(child))
                continue;

            seen.add(child);

            if (this.prefers_descendant_targets(child)) {
                const children = this.get_children(child);
                if (children.length > 0) {
                    stack.push({
                        actor: child,
                        fallback_count: targets.actors.length,
                    });
                    this.push_children(stack, children);
                    continue;
                }
            }

            if (this.is_blur_target_actor(child)) {
                this.add(targets, child);
                continue;
            }

            this.push_children(stack, this.get_children(child));
        }
    }

    prefers_descendant_targets(actor) {
        return this.has_any_style_class(actor, POPUP_DESCENDANT_TARGET_STYLE_CLASSES);
    }

    is_blur_target_actor(actor) {
        return (
            this.has_any_style_class(actor, POPUP_TARGET_STYLE_CLASSES)
            || this.has_any_style_class(actor, POPUP_TARGET_STYLE_CLASSES_OSK)
            || this.has_any_style_class(actor, POPUP_CHILD_STYLE_CLASSES)
        );
    }

    get_corner_radius(target, root_actor) {
        return POPUP_CORNER_RADII.find(radius =>
            this.has_any_style_class(target, radius.style_classes)
            || this.has_any_style_class(root_actor, radius.style_classes)
        ) ?? DEFAULT_CORNER_RADIUS;
    }

    with_style_cache(callback) {
        const previous_cache = this.style_cache;
        this.style_cache = new WeakMap();

        try {
            return callback();
        } finally {
            this.style_cache = previous_cache;
        }
    }

    has_any_style_class(actor, style_classes) {
        if (!actor)
            return false;

        const classes = this.get_style_classes(actor);
        if (classes)
            return style_classes.some(style_class => classes.has(style_class));

        return style_classes.some(style_class => this.has_style_class(actor, style_class));
    }

    has_style_class(actor, style_class) {
        return has_style_class(actor, style_class);
    }

    get_style_classes(actor) {
        if (!actor || !this.style_cache)
            return null;

        if (this.style_cache.has(actor))
            return this.style_cache.get(actor);

        if (!actor.get_style_class_name) {
            this.style_cache.set(actor, null);
            return null;
        }

        const class_names = actor.get_style_class_name();
        if (typeof class_names !== 'string') {
            this.style_cache.set(actor, null);
            return null;
        }

        const classes = new Set(class_names.split(/\s+/).filter(Boolean));
        this.style_cache.set(actor, classes);
        return classes;
    }

    create_targets() {
        return {
            actors: [],
            seen: new WeakSet(),
        };
    }

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

    push_children(stack, children) {
        children
            .slice()
            .reverse()
            .forEach(child => stack.push({ actor: child, fallback_count: null }));
    }

    add(targets, target) {
        if (
            this.actor.is_internal_actor(target)
            || !this.actor.watch_actor(target)
            || targets.seen.has(target)
        )
            return;

        targets.seen.add(target);
        targets.actors.push(target);
    }
};

export function has_style_class(actor, style_class) {
    if (!actor || !style_class)
        return false;

    if (actor.has_style_class_name)
        return actor.has_style_class_name(style_class);

    if (actor.get_style_class_name)
        return (actor.get_style_class_name() ?? '').split(/\s+/).includes(style_class);

    return false;
}