Skip to content

Instantly share code, notes, and snippets.

@naddeoa
Created May 15, 2017 02:45
Show Gist options
  • Save naddeoa/9dd6e6c408dbd450d8ecb5560e5b6c47 to your computer and use it in GitHub Desktop.
Save naddeoa/9dd6e6c408dbd450d8ecb5560e5b6c47 to your computer and use it in GitHub Desktop.
Minimal example of how to create a gnome shell extension that has a menu, menu items, and a label that updates on a timer
// Basic stuff
const St = imports.gi.St;
const Lang = imports.lang;
// UI specific components
const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu;
const PanelMenu = imports.ui.panelMenu;
const Mainloop = imports.mainloop;
const Indicator = new Lang.Class({
Name: 'GnomeBitBarIndicator', // TODO does this even matter?
Extends: PanelMenu.Button,
_init: function() {
this.parent(0.0, "GnomeBitBar", false); // TODO does this even matter?
// Set up click handlers
this.actor.connect('enter_event', Lang.bind(this, this.resetCounter, true));
this.actor.connect('leave_event', Lang.bind(this, this.resetCounter, false));
this.actor.connect('button_press_event', Lang.bind(this, this.resetCounter, false));
this.i = 0;
this.text = new St.Label({
text: "loading..."
});
this._refresh();
this.actor.add_actor(this.text);
// add menu items
let item = new PopupMenu.PopupSubMenuMenuItem('Hello', true);
this.menu.addMenuItem(item);
// make the first menu item do something on click
item.actor.connect('button_press_event', Lang.bind(this, function(){
this.menu.close();
this.resetCounter();
}, false));
// Add some extra items
this.menu.addMenuItem(new PopupMenu.PopupSubMenuMenuItem('hello 2', false));
this.menu.addMenuItem(new PopupMenu.PopupSubMenuMenuItem('hello 3', false));
this.menu.addMenuItem(new PopupMenu.PopupSubMenuMenuItem('hello 4', false));
this.menu.addMenuItem(new PopupMenu.PopupSubMenuMenuItem('hello 5', false));
//add seperator menu
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem);
},
resetCounter: function(){
this.i = 0;
},
updateUI: function() {
this.text.set_text('i = ' + this.i);
},
_refresh: function () {
this.i++;
this.updateUI();
this._removeTimeout();
this._timeout = Mainloop.timeout_add_seconds(2, Lang.bind(this, this._refresh));
return true;
},
_removeTimeout: function () {
if (this._timeout) {
Mainloop.source_remove(this._timeout);
this._timeout = null;
}
}
});
let menu;
function init() {
// One time startup initialization
}
function enable() {
menu = new Indicator();
Main.panel.addToStatusArea('indicator', menu);
}
function disable() {
menu.destroy();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment