summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/blur-my-shell@aunetx/conveniences/dummy_pipeline.js
blob: 4d71d468251e8d3c294111435a6a7ab911933038 (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
import St from 'gi://St';
import Clutter from 'gi://Clutter';

/// A dummy `Pipeline`, for dynamic blur only.
/// Instead of a pipeline id, we take the settings of the component we want to blur.
export const DummyPipeline = class DummyPipeline {
    constructor(effects_manager, settings, actor = null, options = {}) {
        this.effects_manager = effects_manager;
        this.settings = settings;
        this.corner_radius_key = options.corner_radius_key ?? 'corner-radius';
        this.corner_radius_getter = options.corner_radius_getter ?? (() => this.settings.CORNER_RADIUS);
        this.opacity_factor = 1;
        this.effect = null;
        this.attach_effect_to_actor(actor);
    }

    create_background_with_effect(
        background_group,
        widget_name
    ) {
        // create the new actor
        this.actor = new St.Widget({ name: widget_name });

        this.attach_effect_to_actor(this.actor);

        // a dummy `BackgroundManager`, just to access the pipeline easily
        let bg_manager = new Clutter.Actor;
        bg_manager.backgroundActor = this.actor;
        bg_manager._bms_pipeline = this;

        background_group.insert_child_at_index(this.actor, 0);

        return [this.actor, bg_manager];
    };

    attach_effect_to_actor(actor) {
        // set the actor
        if (actor)
            this.actor = actor;
        else {
            this.remove_pipeline_from_actor();
            return;
        }

        // build the new effect to be added
        this.build_effect({
            unscaled_radius: this.get_unscaled_radius(),
            brightness: this.get_brightness(),
            unscaled_corner_radius: this.corner_radius_getter(),
        });

        this.actor_destroy_id = this.actor.connect(
            "destroy", () => this.remove_pipeline_from_actor()
        );

        // add the effect to the actor
        if (this.actor)
            this.actor.add_effect(this.effect);
        else
            this._warn(`could not add effect to actor, actor does not exist anymore`);
    }

    remove_pipeline_from_actor() {
        this.remove_effect();
        if (this.actor && this.actor_destroy_id)
            this.actor.disconnect(this.actor_destroy_id);
        this.actor_destroy_id = null;
        this.actor = null;
    }

    build_effect(params) {
        // create the effect
        this.effect = this.effects_manager.new_native_dynamic_gaussian_blur_effect(params);

        // connect to settings changes, using the true gsettings object
        this._sigma_changed_id = this.settings.settings.connect('changed::sigma', () => this.update_effect());
        this._brightness_changed_id = this.settings.settings.connect('changed::brightness', () => this.update_effect());
        this._corner_radius_changed_id = this.settings.settings.connect(
            `changed::${this.corner_radius_key}`,
            () => this.update_effect()
        );
    }

    set_opacity_factor(opacity_factor) {
        opacity_factor = Math.max(0, Math.min(1, opacity_factor));
        if (this.opacity_factor === opacity_factor)
            return;

        this.opacity_factor = opacity_factor;
        this.update_effect();
    }

    update_effect() {
        if (!this.effect)
            return;

        this.effect.unscaled_radius = this.get_unscaled_radius();
        this.effect.brightness = this.get_brightness();
        this.effect.unscaled_corner_radius = this.corner_radius_getter();
    }

    get_unscaled_radius() {
        return 2 * this.settings.SIGMA * this.opacity_factor;
    }

    get_brightness() {
        return 1 - (1 - this.settings.BRIGHTNESS) * this.opacity_factor;
    }

    repaint_effect() {
        this.effect?.queue_repaint();
    }

    /// 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_effect() {
        if (this.effect)
            this.effects_manager.remove(this.effect);
        this.effect = null;

        if (this._sigma_changed_id)
            this.settings.settings.disconnect(this._sigma_changed_id);
        if (this._brightness_changed_id)
            this.settings.settings.disconnect(this._brightness_changed_id);
        if (this._corner_radius_changed_id)
            this.settings.settings.disconnect(this._corner_radius_changed_id);
        delete this._sigma_changed_id;
        delete this._brightness_changed_id;
        delete this._corner_radius_changed_id;
    }

    /// Do nothing for this dummy pipeline.
    /// Note: exposed to public API.
    change_pipeline_to() { return; }

    /// Note: exposed to public API.
    destroy() {
        this.remove_pipeline_from_actor();
    }

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