Skip to content

Instantly share code, notes, and snippets.

@sanjarcode
Last active January 18, 2021 22:48
Show Gist options
  • Save sanjarcode/74f6d66750caacf59e3dd62fe83ae15e to your computer and use it in GitHub Desktop.
Save sanjarcode/74f6d66750caacf59e3dd62fe83ae15e to your computer and use it in GitHub Desktop.
gnome-shell-extensions snippets
// Use this in init()
function createTextButton(textP = "Click Here!", action) {
const St = imports.gi.St;
const Main = imports.ui.main;
// create button
button = new St.Bin({
style_class: "panel-button",
reactive: true,
can_focus: true,
x_fill: true,
y_fill: false,
track_hover: true,
});
// create text label
textLabel = new St.Label({ text: textP.toString() });
button.set_child(textLabel);
// event listener
button.connect("button-press-event", action);
// inject button to GUI
Main.panel._rightBox.insert_child_at_index(button, 0);
return button; // in disable() add this code // Main.panel._rightBox.remove_child(button);
}
// function for debugging gnome-shell-extensions by printing text to the desktop
function displayLog(textP="Hello, World!", fontSize=36) {
// get dependencies
const Main = imports.ui.main;
const Tweener = imports.ui.tweener;
// set style
textBox = new St.Label({style: `
font-size: ${fontSize}px;
font-weight: bold;
color: #ffffff;
background-color: rgba(10,10,10,0.7);
border-radius: 5px;
padding: .5em;
opacity: 255;
`, text: textP.toString().match(new RegExp('.{1,' + Math.floor(Main.layoutManager.primaryMonitor.width / fontSize) + '}', 'g')).join("\n")
// divide the text into smaller lines for displaying
});
// inject textBox to display
Main.uiGroup.add_actor(textBox);
// text disappears after 2 seconds
Tweener.addTween(textBox,
{ opacity: 0,
time: 2,
transition: 'easeOutQuad',
onComplete: function _hideHello() {
Main.uiGroup.remove_actor(textBox);
textBox = null;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment