Translate

Archives

Using an Extension to Monkey Patch the GNOME Shell

Wikipedia defines a monkey patch as a way to extend or modify the run-time code of dynamic languages without altering the original source code. JavaScript is one such dynamic language. The GNOME Shell UI is written in JavaScript so monkey patching of the GNOME Shell is possible.

Some time ago, I figured out why the Search Provider buttons in the Activities Overview screen did not display search providers icons. See here. I raised a new low priority bug report in GNOME bugzilla but recently decided to see if I could write a GNOME Shell extension which used monkey patching to provide a solution for this bug. It turned out to be a trivial exercise.

Here is how the search provider buttons look like at present in the GNOME Shell:

GNOME3 Shell screenshot

and here is how the search provider buttons look when two objects in the GNOME Shell are monkey patched using the extension that I wrote.

GNOME3 Shell screenshot

Note the colored icons displayed on the two search provider buttons.

Here is the source code for the extension:

const Lang = imports.lang;
const Main = imports.ui.main;
const Search = imports.ui.search;
const SearchDisplay = imports.ui.searchDisplay;
const St = imports.gi.St;

const  TEXTURE_CACHE_POLICY = 1;


function main() {

    // monkey patch
    Search.OpenSearchSystem.prototype.getProviders = function() {
        let res = [];
        for (let i = 0; i < this._providers.length; i++)
           res.push({ id: i,
                      name: this._providers[i].name,
                      icon_uri: this._providers[i].icon_uri });
        return res;
    };

   // monkey patch
    SearchDisplay.SearchResults.prototype._createOpenSearchProviderButton = function(provider) {

        let button = new St.Button({ style_class: 'dash-search-button',
                                     reactive: true,
                                     x_fill: true,
                                     y_align: St.Align.MIDDLE });
        button.connect('clicked', Lang.bind(this, function() {
            this._openSearchSystem.activateResult(provider.id);
        }));

        let title = new St.Label({ text: provider.name,
                                   style_class: 'dash-search-button-label' });

        let textureCache = St.TextureCache.get_default();
        let searchIcon = textureCache.load_uri_sync(TEXTURE_CACHE_POLICY,
                                                    provider.icon_uri, -1, -1);

        let iconBin = new St.Bin({ style_class: 'dash-search-button-icon',
                                   child: searchIcon });

        let box = new St.BoxLayout();
        box.add(iconBin, {expand: true, x_fill: false, x_align: St.Align.END });
        box.add(title, {expand: true, x_fill: false, x_align: St.Align.START });

        button.set_child(box);
        provider.actor = button;

        this._searchProvidersBox.add(button);
    };

}


Two objects in two different GNOME Shell source code files are monkey patched in this extension. The first object is OpenSearchSystem which is defined in search.js. The particular method that we wish to override via monkey patching is getProviders. This is done to add support for copying the search provider icon string data into the array returned by the getProviders method. The second object is SearchResults which is defined in searchDisplay.js. Here the extension overrides the createOpenSearchProviderButton method. This is done to provide support for creating an icon from the icon string data and displaying it appropriately on a search provider button.

Once you install this extension and reload your GNOME Shell, the icons will be displayed on all search provider buttons.

This extension demonstrates that it is possible to use a GNOME Shell extension to monkey patch the GNOME Shell. This may prove to be a useful mechanism for quickly patching the GNOME Shell prior to official source codes patches being released. You have now seen the world’s first patch extension for the GNOME Shell!

Keep experimenting!

P.S.   I have placed a tarball of this extension in my GNOME Shell Extensions download area on my website. Look for a tarball whose name starts with searchprovidericon. The current version number is 1.0 but this might change in the future if I revise the extension.

10 comments to Using an Extension to Monkey Patch the GNOME Shell

  • John

    Is there a way to Monkey Patch the indicator service. I’ve missed more meeting and IM’s because I don’t see the original notice. I have dual monitors and sometimes I don’t see the alerts. If the bottom of the screen could glow red to indicate a missed alert that would be great.

  • Jim

    Hi installed your searchprovideicon extension on susue 11.4, works fine. Great site I am learning alot.

    Thanks

  • Charles Bowman

    @James Hunt, as someone that has been using computers for about 30 years I completely disagree with your assessment of Gnome 3.0 shell. I find it extremely flexible & easy to use and think it’s going in the right direction, though mine has been modified & enhanced through the use of extensions and some additional applications I find indispensable, my favorite being Synapse. To me this is the DE that I have been waiting for and think that in time linux users will happily adopt it.

    You are entitled to your opinons though, as am I.

    (I do completely agree with you in regards to Unity though)

  • Scott

    @Charles Bowman

    I think you missed the point then, being that supposedly Gnome 3, “out of the box” is ready for use. Yet you have modified and enhanced it, including of all things, Synapse. A launcher – the one thing modern computer users use the most heavily.

    I find no productivity gains by going to the top left, selecting applications, then going all the way to the right to select my app group(or scrolling through all my apps), then moving back to the center or far left depending on the app. Honestly to me Gnome 3 looks like it was designed for a tablet or phone with touch screens in mind. This is great, except I know of no one who has a touch screen monitor for their desktop. On my Xoom, this would rock, on my desktop, not so much.

    Now I can get all that functionality back by adding extensions and get back to what I would consider a base, functional install. The launcher would eliminate 90% of the complaints I have seen.

  • Agustianes Suwardi

    @Charles Bowman, I agree with you. Gnome 3 is a huge step forward in Linux Desktop computing. I don’t know how people define a ‘power user’ is, but I found myself happily working with 7 terminal windows, 2 file manager windows, 1 web browser, and 2 openoffice documents in gnome shell. And the most important: I got my works done, in a relative same pace as in traditional Gnome 2 interface.

  • Charles Bowman

    @Scott

    Perhaps I did, but since I’ve been customizing my systems for over 20 years I am of the opinion that no one can assume what my needs are, or how I am “supposed” to use a system. I just read it as a rant about how he didn’t like Gnome shell, I obviously overlooked his emphasis on the “out of the box” usability. I don’t see it as not working out of the box, since it obviously does, any actual problems with functionality are more the fault of the system it runs on top of.

    There are easily a half dozen other desktop environments available just for linux, pick one that works “out of the box”.

  • Matt Heck

    James, regarding that article:

    “Do you expect companies to start purchasing expensive cards with 3D acceleration for their servers…?”

    Yes, actually, since it is no longer possible to do otherwise. The Intel Core series, and all higher currently available processors, include integrated graphics, which are multi-core and 3D accelerated. For that matter, the Intel Atom series does as well. All of these are a far cry better than the i815, i945, etc. CHIPSET graphics solutions that could barely be trusted to accelerate a 2D BITBLT operation.

    Progress has indeed marched along.

    That said, I’ll have to try a VNC session into a GNOME 3 system and see how it goes.

  • Pablo Mandiola

    Hey, I tried to do a monkey patch extension in gnome-shell 3.2 following this exaple and it seems it’s not working.

    Is it because this no longer works since 3.2 or am I doing something wrong?

    Thanks