Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const Util = imports.misc.util;
const GLib = imports.gi.GLib;
const St = imports.gi.St;
const Mainloop = imports.mainloop;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const Tweener = imports.ui.tweener;
const Clutter = imports.gi.Clutter;
const Lang = imports.lang;
const STATE_NORMAL = 0;
const STATE_COUNTING = 1;
const STATE_SHOTTING = 2;
const STATE_HAS_SCREENSHOT = 3;
function ScreenshotPanelButton() {
this._init();
}
ScreenshotPanelButton.prototype = {
__proto__: PanelMenu.Button.prototype,
_init: function() {
PanelMenu.Button.prototype._init.call(this, 0.0);
this._counting = 0;
this._state = STATE_NORMAL;
let button = new St.Icon({icon_name: "applets-screenshooter",
icon_type: St.IconType.FULLCOLOR,
icon_size: 24});
this._vbox;
this._screen = null;
let item;
this.actor.set_child(button);
this._vbox = new St.BoxLayout({vertical: true});
this.menu.addActor(this._vbox);
item = new PopupMenu.PopupMenuItem("Take a shot");
this._vbox.add(item.actor);
item.connect('activate', Lang.bind(this, this._onClick));
},
_onClick: function() {
this._counting = 3;
this._state = STATE_COUNTING;
this.menu.close()
this._icon = this._createText(this._counting);
global.stage.add_actor(this._icon);
Tweener.addTween (this._icon, {time: 1.0, transition: "easeInQuad",
onComplete: this._countingDown,
onCompleteScope: this,
opacity: 255,
scale_x: 0.0,
scale_y: 0.0});
},
_countingDown: function() {
this._counting--;
global.log("counting: " + this._counting);
global.stage.remove_actor(this._icon);
this._icon.destroy();
if (this._counting > 0) {
this._icon = this._createText(this._counting);
global.stage.add_actor(this._icon);
Tweener.addTween (this._icon, {time: 1.0,
transition: "easeInQuad",
onComplete: this._countingDown,
onCompleteScope: this,
opacity: 255,
scale_x: 0.0,
scale_y: 0.0});
}
else {
this._shotting();
}
},
_shotting: function() {
global.log("shotting");
this._state = STATE_SHOTTING;
let m = global.get_primary_monitor();
let color = new Clutter.Color();
color.from_string("White");
this._rect = new Clutter.Rectangle({
color: color,
width: m.width,
height: m.height});
this._rect.set_position(0, 0);
global.stage.add_actor(this._rect);
Tweener.addTween (this._rect, {time: 2.0,
transition: "easeInQuad",
onComplete: this._shotted,
onCompleteScope: this,
opacity: 0});
},
_shotted: function() {
let x, y, width;
global.stage.remove_actor(this._rect);
this._rect.destroy();
this._rect = null;
this._clone = new Clutter.Clone({source:Main.uiGroup});
global.stage.add_actor(this._clone);
width = this.actor.get_width();
[x, y] = this.actor.get_transformed_position();
Tweener.addTween (this._clone, {time:1.0,
transition: "easeInQuad",
onComplete: this._onShotComplete,
onCompleteScope: this,
x: (x + width/2),
y: y,
opacity: 0,
scale_x: 0.0,
scale_y: 0.0});
},
_run: function(command) {
let success, argc, argv;
try {
[success, argc, argv] = GLib.shell_parse_argv(command);
GLib.spawn_sync(null, argv, null, null, null);
} catch (err) {
global.log("error execute import");
}
},
_onShotComplete: function() {
global.stage.remove_actor(this._clone);
this._clone.destroy();
let filename = GLib.get_home_dir() + "/screenshot.png";
this._run("import -window root " + filename);
if (this._screen != null) {
this._vbox.remove(this._screen);
this._screen.destroy();
}
this._screen = new Clutter.Texture({filename: filename,
width: 320, height: 240});
this._vbox.add(this._screen);
this._state = STATE_HAS_SCREENSHOT;
},
_createText: function(counting) {
let icon = new Clutter.Text({ use_markup: true,
text: "<span font='Sans 1000'>" + counting + "</span>",
opacity: 0} );
icon.set_anchor_point_from_gravity(Clutter.Gravity.CENTER);
let m = global.get_primary_monitor();
icon.set_position (Math.floor(m.width/2),
Math.floor(m.height/2));
return icon;
},
};
function main() {
let button = new ScreenshotPanelButton();
Main.panel._leftBox.add(button.actor);
Main.panel._menus.addMenu(button.menu);
Main.screenshot = button;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.