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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
import St from 'gi://St';
import Meta from 'gi://Meta';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as Background from 'resource:///org/gnome/shell/ui/background.js';
import * as uniforms from './shader_uniforms.js';
import * as utils from './utils.js';
/// A `Pipeline` object is a handy way to manage the effects attached to an actor. It only manages
/// one actor at a time (so blurring multiple widgets will need multiple `Pipeline`), and is
/// linked to a `pipeline_id` that has been (hopefully) defined in the settings.
///
/// It communicates with the settings through the `PipelinesManager` object, and receives different
/// signals (with `pipeline_id` being an unique string):
/// - `'pipeline_id'::pipeline-updated`, handing a new pipeline descriptor object, when the pipeline
/// has been changed enough that it needs to rebuild the effects configuration
/// - `'pipeline_id'::pipeline-destroyed`, when the pipeline has been destroyed; thus making the
/// `Pipeline` change its id to `pipeline_default`
///
/// And each effect, with an unique `id`, is connected to the `PipelinesManager` for the signals:
/// - `'pipeline_id'::effect-'id'-key-removed`, handing the key that was removed
/// - `'pipeline_id'::effect-'id'-key-updated`, handing the key that was changed and its new value
/// - `'pipeline_id'::effect-'id'-key-added`, handing the key that was added and its value
export const Pipeline = class Pipeline {
constructor(effects_manager, pipelines_manager, pipeline_id, actor = null, options = {}) {
this.effects_manager = effects_manager;
this.pipelines_manager = pipelines_manager;
this.effects = [];
this.effect_overrides = options.effect_overrides ?? {};
this.set_pipeline_id(pipeline_id);
this.attach_pipeline_to_actor(actor);
}
/// Create a background linked to the monitor with index `monitor_index`, with a
/// `BackgroundManager` that is appended to the list `background_managers`. The background actor
/// will be given the name `widget_name` and inserted into the given `background_group`.
/// If `use_absolute_position` is false, then the position used is at (0,0); useful when the
/// positioning is relative.
/// Note: exposed to public API.
create_background_with_effects(
monitor_index,
background_managers,
background_group,
widget_name,
use_absolute_position = true
) {
let monitor = Main.layoutManager.monitors[monitor_index];
this.remove_pipeline_from_actor();
// create the new actor
this.actor = new St.Widget({
name: widget_name,
x: use_absolute_position ? monitor.x : 0,
y: utils.subpixel_stage_offset() + (use_absolute_position ? monitor.y : 0),
z_position: 1, // seems to fix the multi-monitor glitch
width: monitor.width,
height: monitor.height
});
if (this.pipeline_id)
this.attach_pipeline_to_actor(this.actor);
let bg_manager = new Background.BackgroundManager({
container: this.actor,
monitorIndex: monitor_index,
controlPosition: false,
});
bg_manager._bms_pipeline = this;
// 'controlPosition: false' skips BackgroundManager's default layout pass, which also diables
// sibling re-ordering.
// Without it, new actors render on top while loading, causing a solid color flash through
// on the surface.
this.child_added_id = this.actor.connect(
'child-added', (container, child) => {
if (child instanceof Meta.BackgroundActor)
container.set_child_below_sibling(child, null);
}
);
background_managers.push(bg_manager);
background_group.insert_child_at_index(this.actor, 0);
return this.actor;
};
/// Set the pipeline id, correctly connecting the `Pipeline` object to listen the pipelines
/// manager for pipeline-wide changes. This does not update the effects in consequence, call
/// `change_pipeline_to` instead if you want to reconstruct the effects too.
set_pipeline_id(pipeline_id) {
// disconnect ancient signals
this.remove_connections();
// change the id
this.pipeline_id = pipeline_id;
// connect to settings changes
this._pipeline_changed_id = this.pipelines_manager.connect(
this.pipeline_id + '::pipeline-updated',
(_, new_pipeline) => this.update_effects_from_pipeline(new_pipeline)
);
this._pipeline_destroyed_id = this.pipelines_manager.connect(
this.pipeline_id + '::pipeline-destroyed',
_ => this.change_pipeline_to("pipeline_default")
);
}
/// Disconnect the signals for the pipeline changes. Please note that the signals related to the
/// effects are stored with them and removed with `remove_all_effects`.
remove_connections() {
if (this._pipeline_changed_id)
this.pipelines_manager.disconnect(this._pipeline_changed_id);
if (this._pipeline_destroyed_id)
this.pipelines_manager.disconnect(this._pipeline_destroyed_id);
this._pipeline_changed_id = null;
this._pipeline_destroyed_id = null;
}
/// Attach a Pipeline object with `pipeline_id` already set to an actor.
attach_pipeline_to_actor(actor) {
if (!actor) {
this.remove_pipeline_from_actor();
return;
}
this.disconnect_actor_destroy();
this.actor = actor;
// attach the pipeline
let pipeline = this.pipelines_manager.pipelines[this.pipeline_id];
if (!pipeline) {
this._warn(`could not attach pipeline to actor, pipeline "${this.pipeline_id}" not found`);
// do not recurse...
if ("pipeline_default" in this.pipelines_manager.pipelines) {
this.set_pipeline_id("pipeline_default");
pipeline = this.pipelines_manager.pipelines["pipeline_default"];
} else
return;
}
this.actor_destroy_id = this.actor.connect(
"destroy", () => this.remove_pipeline_from_actor()
);
// update the effects
this.update_effects_from_pipeline(pipeline);
}
remove_pipeline_from_actor() {
this.remove_all_effects();
this.disconnect_actor_destroy();
this.disconnect_child_added();
this.actor = null;
}
disconnect_actor_destroy() {
if (this.actor && this.actor_destroy_id) {
this.actor.disconnect(this.actor_destroy_id);
}
this.actor_destroy_id = null;
}
disconnect_child_added() {
if (this.actor && this.child_added_id) {
this.actor.disconnect(this.child_added_id);
}
this.child_added_id = null;
}
/// Update the effects from the given pipeline object, the hard way.
update_effects_from_pipeline(pipeline) {
// remove all effects
this.remove_all_effects();
// build the new effects to be added
pipeline.effects.forEach(effect => {
if (effect.type === 'pixelize')
this.build_pixelize_effect(effect);
else if ('new_' + effect.type + '_effect' in this.effects_manager)
this.build_effect(effect);
else
this._warn(`could not add effect to actor, effect "${effect.type}" not found`);
});
this.effects.reverse();
// add the effects to the actor
if (this.actor)
this.effects.forEach(effect => {
this.actor.add_effect(effect);
uniforms.mark_dirty(effect);
});
else
this._warn(`could not add effect to actor, actor does not exist anymore`);
}
build_pixelize_effect(effect_infos) {
const effect_params = this.get_effect_params(effect_infos);
const effect_overrides = this.get_effect_overrides(effect_infos.type, effect_params);
const params = {
...effect_params,
...effect_overrides,
};
const downscale = this.effects_manager.new_downscale_effect({
divider: params.factor,
downsampling_mode: params.downsampling_mode,
opacity_factor: params.opacity_factor,
});
const upscale = this.effects_manager.new_upscale_effect({
factor: params.factor,
opacity_factor: params.opacity_factor,
});
this.setup_effect(downscale, effect_infos, effect_params, 'downscale');
this.setup_effect(upscale, effect_infos, effect_params, 'upscale');
}
/// Given an `effect_infos` object containing the effect type, id and params, build an effect
/// and append it to the effects list
build_effect(effect_infos) {
const effect_params = this.get_effect_params(effect_infos);
const effect_overrides = this.get_effect_overrides(effect_infos.type, effect_params);
let effect = this.effects_manager['new_' + effect_infos.type + '_effect']({
...effect_params,
...effect_overrides,
});
this.setup_effect(effect, effect_infos, effect_params);
}
setup_effect(effect, effect_infos, effect_params, pixelize_role = null) {
effect._bms_effect_type = effect_infos.type;
effect._bms_effect_id = effect_infos.id;
effect._bms_effect_params = effect_params;
effect._bms_pixelize_role = pixelize_role;
this.effects.push(effect);
// connect to settings changes
effect._effect_key_removed_id = this.pipelines_manager.connect(
this.pipeline_id + '::effect-' + effect_infos.id + '-key-removed', (_, key) => {
const default_value = this.get_effect_default_param(effect, key);
effect._bms_effect_params[key] = default_value;
if (!this.apply_effect_override(effect, key))
this.set_effect_param(effect, key, default_value);
}
);
effect._effect_key_updated_id = this.pipelines_manager.connect(
this.pipeline_id + '::effect-' + effect_infos.id + '-key-updated', (_, key, value) => {
effect._bms_effect_params[key] = value;
if (!this.apply_effect_override(effect, key))
this.set_effect_param(effect, key, value);
}
);
effect._effect_key_added_id = this.pipelines_manager.connect(
this.pipeline_id + '::effect-' + effect_infos.id + '-key-added', (_, key, value) => {
effect._bms_effect_params[key] = value;
if (!this.apply_effect_override(effect, key))
this.set_effect_param(effect, key, value);
}
);
}
get_effect_params(effect_infos) {
const effect_class = this.effects_manager.SUPPORTED_EFFECTS[effect_infos.type]?.class;
return {
...(effect_class?.default_params ?? {}),
...(effect_infos.params ?? {}),
};
}
get_effect_default_param(effect, key) {
return this.effects_manager.SUPPORTED_EFFECTS[effect._bms_effect_type]
?.class
?.default_params
?.[key];
}
get_effect_overrides(effect_type, effect_params = {}) {
const overrides = this.effect_overrides[effect_type] ?? {};
return typeof overrides === 'function' ? overrides(effect_params) : overrides;
}
apply_effect_override(effect, key) {
const overrides = this.get_effect_overrides(effect._bms_effect_type, effect._bms_effect_params);
if (!(key in overrides))
return false;
this.set_effect_param(effect, key, overrides[key]);
return true;
}
apply_effect_overrides(effect_type = null) {
this.effects.forEach(effect => {
if (effect_type && effect._bms_effect_type !== effect_type)
return;
const overrides = this.get_effect_overrides(effect._bms_effect_type, effect._bms_effect_params);
Object.keys(overrides).forEach(key => this.set_effect_param(effect, key, overrides[key]));
});
}
set_effect_param(effect, key, value) {
if (effect._bms_effect_type !== 'pixelize') {
effect[key] = value;
return;
}
if (key === 'factor') {
if (effect._bms_pixelize_role === 'downscale')
effect.divider = value;
else if (effect._bms_pixelize_role === 'upscale')
effect.factor = value;
return;
}
if (key === 'downsampling_mode') {
if (effect._bms_pixelize_role === 'downscale')
effect.downsampling_mode = value;
return;
}
if (key in effect)
effect[key] = value;
}
/// Remove every effect from the actor it is attached to. Please note that they are not
/// destroyed, but rather stored (thanks to the `EffectManager` class) to be reused later.
remove_all_effects() {
this.effects.forEach(effect => {
this.effects_manager.remove(effect);
[
effect._effect_key_removed_id,
effect._effect_key_updated_id,
effect._effect_key_added_id
].forEach(
id => { if (id) this.pipelines_manager.disconnect(id); }
);
delete effect._effect_key_removed_id;
delete effect._effect_key_updated_id;
delete effect._effect_key_added_id;
delete effect._bms_effect_type;
delete effect._bms_effect_id;
delete effect._bms_effect_params;
delete effect._bms_pixelize_role;
});
this.effects = [];
}
/// Change the pipeline id, and update the effects according to this change.
/// Note: exposed to public API.
change_pipeline_to(pipeline_id) {
this.set_pipeline_id(pipeline_id);
this.attach_pipeline_to_actor(this.actor);
}
/// Resets the `Pipeline` object to a sane state, removing every effect and signal.
/// Note: exposed to public API.
destroy() {
this.remove_connections();
this.remove_pipeline_from_actor();
this.pipeline_id = null;
}
_warn(str) {
console.warn(`[Blur my Shell > pipeline] ${str}`);
}
};
|