Extensions, InjectionManager

I am following extensios porting guide for 45 Extension (ESModule) | GNOME JavaScript

I found the InjectionManager part and tried the given example.
Based on the example, I tweak my code as below.

import St from 'gi://St';
import { Extension, InjectionManager } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as UnlockDialog from 'resource:///org/gnome/shell/ui/unlockDialog.js';

export default class ControlBlurExtension extends Extension {
    constructor(metadata) {
        super(metadata);

        this._injectionManager = new InjectionManager();
    }

    enable() {
        this._settings = this.getSettings();

        // Overriding a method with an *arrow function*
        this._injectionManager.overrideMethod(UnlockDialog.UnlockDialog.prototype, '_updateBackgroundEffects',
            originalMethod => {
                return () => {
                    const themeContext = St.ThemeContext.get_for_stage(global.stage);

                    let BRIGHTNESS_VALUE = this._settings.get_double('brightness');
                    let SIGMA_VALUE = this._settings.get_int('sigma');

                    for (const widget of this._backgroundGroup) {
                        const effect = widget.get_effect('blur');

                        if (effect) {
                            effect.set({
                                brightness: BRIGHTNESS_VALUE,
                                sigma: SIGMA_VALUE * themeContext.scale_factor,
                            });
                        }
                        
                    }
                    originalMethod.call();
                }
            });
    }

    disable() {
        this._injectionManager.clear();
        this._settings = null;
    }
}

However, the code is not working and I have two concerns

  1. I did not understand what is originalMethod.call() function is for
  2. in the return function, for (const widget of this._backgroundGroup) { is it the rite way calling this._backgroundGroup?

Any help is much appreciated. Thanks

The parameter is for the case where you want to extend the original method rather than replace it.

It doesn’t look like this is what you want to do here, so you can just ignore the parameter (i.e. never call the original method).

No. You are using an arrow function, so this is the instance of you extension class, not the unlock dialog.

It’s probably more convenient to do something like

            () => {
                const settings = this.getSettings();
                return function () {
                    const themeContext = St.ThemeContext.get_for_stage(global.stage);

                    let BRIGHTNESS_VALUE = settings.get_double('brightness');
                    let SIGMA_VALUE = settings.get_int('sigma');

                    for (const widget of this._backgroundGroup) {
                        const effect = widget.get_effect('blur');

                        if (effect) {
                            effect.set({
                                brightness: BRIGHTNESS_VALUE,
                                sigma: SIGMA_VALUE * themeContext.scale_factor,
                            });
                        }
                    }
                };
            }

That worked. Great explanation @fmullner.
Thank you so-much for your quick response.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.