2012-12-10 15:26:31 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Gdk = imports.gi.Gdk;
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const GLib = imports.gi.GLib;
|
|
|
|
const Gtk = imports.gi.Gtk;
|
|
|
|
const Lang = imports.lang;
|
2013-09-11 15:35:58 +00:00
|
|
|
const Meta = imports.gi.Meta;
|
2012-12-10 15:26:31 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
2014-04-25 16:43:10 +00:00
|
|
|
const GrabHelper = imports.ui.grabHelper;
|
2012-12-10 15:26:31 +00:00
|
|
|
const Lightbox = imports.ui.lightbox;
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const Tweener = imports.ui.tweener;
|
|
|
|
|
2013-10-24 21:51:58 +00:00
|
|
|
const ScreenshotIface = '<node> \
|
|
|
|
<interface name="org.gnome.Shell.Screenshot"> \
|
|
|
|
<method name="ScreenshotArea"> \
|
|
|
|
<arg type="i" direction="in" name="x"/> \
|
|
|
|
<arg type="i" direction="in" name="y"/> \
|
|
|
|
<arg type="i" direction="in" name="width"/> \
|
|
|
|
<arg type="i" direction="in" name="height"/> \
|
|
|
|
<arg type="b" direction="in" name="flash"/> \
|
|
|
|
<arg type="s" direction="in" name="filename"/> \
|
|
|
|
<arg type="b" direction="out" name="success"/> \
|
|
|
|
<arg type="s" direction="out" name="filename_used"/> \
|
|
|
|
</method> \
|
|
|
|
<method name="ScreenshotWindow"> \
|
|
|
|
<arg type="b" direction="in" name="include_frame"/> \
|
|
|
|
<arg type="b" direction="in" name="include_cursor"/> \
|
|
|
|
<arg type="b" direction="in" name="flash"/> \
|
|
|
|
<arg type="s" direction="in" name="filename"/> \
|
|
|
|
<arg type="b" direction="out" name="success"/> \
|
|
|
|
<arg type="s" direction="out" name="filename_used"/> \
|
|
|
|
</method> \
|
|
|
|
<method name="Screenshot"> \
|
|
|
|
<arg type="b" direction="in" name="include_cursor"/> \
|
|
|
|
<arg type="b" direction="in" name="flash"/> \
|
|
|
|
<arg type="s" direction="in" name="filename"/> \
|
|
|
|
<arg type="b" direction="out" name="success"/> \
|
|
|
|
<arg type="s" direction="out" name="filename_used"/> \
|
|
|
|
</method> \
|
|
|
|
<method name="SelectArea"> \
|
|
|
|
<arg type="i" direction="out" name="x"/> \
|
|
|
|
<arg type="i" direction="out" name="y"/> \
|
|
|
|
<arg type="i" direction="out" name="width"/> \
|
|
|
|
<arg type="i" direction="out" name="height"/> \
|
|
|
|
</method> \
|
|
|
|
<method name="FlashArea"> \
|
|
|
|
<arg type="i" direction="in" name="x"/> \
|
|
|
|
<arg type="i" direction="in" name="y"/> \
|
|
|
|
<arg type="i" direction="in" name="width"/> \
|
|
|
|
<arg type="i" direction="in" name="height"/> \
|
|
|
|
</method> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var ScreenshotService = new Lang.Class({
|
2012-12-10 15:26:31 +00:00
|
|
|
Name: 'ScreenshotService',
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_init() {
|
2012-12-10 15:26:31 +00:00
|
|
|
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(ScreenshotIface, this);
|
|
|
|
this._dbusImpl.export(Gio.DBus.session, '/org/gnome/Shell/Screenshot');
|
|
|
|
|
2014-09-27 11:35:22 +00:00
|
|
|
this._screenShooter = new Map();
|
|
|
|
|
2014-10-03 14:33:37 +00:00
|
|
|
this._lockdownSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.lockdown' });
|
|
|
|
|
2012-12-10 15:26:31 +00:00
|
|
|
Gio.DBus.session.own_name('org.gnome.Shell.Screenshot', Gio.BusNameOwnerFlags.REPLACE, null, null);
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_createScreenshot(invocation) {
|
2014-09-27 11:35:22 +00:00
|
|
|
let sender = invocation.get_sender();
|
2014-10-03 14:33:37 +00:00
|
|
|
if (this._screenShooter.has(sender) ||
|
|
|
|
this._lockdownSettings.get_boolean('disable-save-to-disk')) {
|
2014-09-27 11:35:22 +00:00
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', [false, '']));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shooter = new Shell.Screenshot();
|
|
|
|
shooter._watchNameId =
|
|
|
|
Gio.bus_watch_name(Gio.BusType.SESSION, sender, 0, null,
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onNameVanished.bind(this));
|
2014-09-27 11:35:22 +00:00
|
|
|
|
|
|
|
this._screenShooter.set(sender, shooter);
|
|
|
|
|
|
|
|
return shooter;
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onNameVanished(connection, name) {
|
2014-09-27 11:35:22 +00:00
|
|
|
this._removeShooterForSender(name);
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_removeShooterForSender(sender) {
|
2014-09-27 11:35:22 +00:00
|
|
|
let shooter = this._screenShooter.get(sender);
|
|
|
|
if (!shooter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Gio.bus_unwatch_name(shooter._watchNameId);
|
|
|
|
this._screenShooter.delete(sender);
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_checkArea(x, y, width, height) {
|
2014-06-04 14:26:06 +00:00
|
|
|
return x >= 0 && y >= 0 &&
|
|
|
|
width > 0 && height > 0 &&
|
|
|
|
x + width <= global.screen_width &&
|
|
|
|
y + height <= global.screen_height;
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onScreenshotComplete(obj, result, area, filenameUsed, flash, invocation) {
|
2014-09-27 11:35:22 +00:00
|
|
|
if (result) {
|
|
|
|
if (flash) {
|
|
|
|
let flashspot = new Flashspot(area);
|
2017-10-31 00:38:18 +00:00
|
|
|
flashspot.fire(() => {
|
2014-09-27 11:35:22 +00:00
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2014-09-27 11:35:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
2012-12-10 15:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let retval = GLib.Variant.new('(bs)', [result, filenameUsed]);
|
|
|
|
invocation.return_value(retval);
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_scaleArea(x, y, width, height) {
|
2014-06-16 18:28:17 +00:00
|
|
|
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
|
|
|
|
x *= scaleFactor;
|
|
|
|
y *= scaleFactor;
|
|
|
|
width *= scaleFactor;
|
|
|
|
height *= scaleFactor;
|
|
|
|
return [x, y, width, height];
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_unscaleArea(x, y, width, height) {
|
2014-06-16 18:28:17 +00:00
|
|
|
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
|
|
|
|
x /= scaleFactor;
|
|
|
|
y /= scaleFactor;
|
|
|
|
width /= scaleFactor;
|
|
|
|
height /= scaleFactor;
|
|
|
|
return [x, y, width, height];
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
ScreenshotAreaAsync(params, invocation) {
|
2012-12-10 15:26:31 +00:00
|
|
|
let [x, y, width, height, flash, filename, callback] = params;
|
2014-06-16 18:28:17 +00:00
|
|
|
[x, y, width, height] = this._scaleArea(x, y, width, height);
|
2014-06-04 14:26:06 +00:00
|
|
|
if (!this._checkArea(x, y, width, height)) {
|
|
|
|
invocation.return_error_literal(Gio.IOErrorEnum,
|
|
|
|
Gio.IOErrorEnum.CANCELLED,
|
|
|
|
"Invalid params");
|
2013-03-02 13:59:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-27 11:35:22 +00:00
|
|
|
let screenshot = this._createScreenshot(invocation);
|
|
|
|
if (!screenshot)
|
|
|
|
return;
|
2012-12-10 15:26:31 +00:00
|
|
|
screenshot.screenshot_area (x, y, width, height, filename,
|
2017-12-02 00:27:35 +00:00
|
|
|
(obj, result, area, filenameUsed) => {
|
|
|
|
this._onScreenshotComplete(obj, result, area, filenameUsed,
|
|
|
|
flash, invocation);
|
|
|
|
});
|
2012-12-10 15:26:31 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
ScreenshotWindowAsync(params, invocation) {
|
2012-12-10 15:26:31 +00:00
|
|
|
let [include_frame, include_cursor, flash, filename] = params;
|
2014-09-27 11:35:22 +00:00
|
|
|
let screenshot = this._createScreenshot(invocation);
|
|
|
|
if (!screenshot)
|
|
|
|
return;
|
2012-12-10 15:26:31 +00:00
|
|
|
screenshot.screenshot_window (include_frame, include_cursor, filename,
|
2017-12-02 00:27:35 +00:00
|
|
|
(obj, result, area, filenameUsed) => {
|
|
|
|
this._onScreenshotComplete(obj, result, area, filenameUsed,
|
|
|
|
flash, invocation);
|
|
|
|
});
|
2012-12-10 15:26:31 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
ScreenshotAsync(params, invocation) {
|
2012-12-10 15:26:31 +00:00
|
|
|
let [include_cursor, flash, filename] = params;
|
2014-09-27 11:35:22 +00:00
|
|
|
let screenshot = this._createScreenshot(invocation);
|
|
|
|
if (!screenshot)
|
|
|
|
return;
|
2012-12-10 15:26:31 +00:00
|
|
|
screenshot.screenshot(include_cursor, filename,
|
2017-12-02 00:27:35 +00:00
|
|
|
(obj, result, area, filenameUsed) => {
|
|
|
|
this._onScreenshotComplete(obj, result, area, filenameUsed,
|
|
|
|
flash, invocation);
|
|
|
|
});
|
2012-12-10 15:26:31 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
SelectAreaAsync(params, invocation) {
|
2012-12-10 15:26:31 +00:00
|
|
|
let selectArea = new SelectArea();
|
|
|
|
selectArea.show();
|
2017-10-31 00:38:18 +00:00
|
|
|
selectArea.connect('finished', (selectArea, areaRectangle) => {
|
|
|
|
if (areaRectangle) {
|
|
|
|
let retRectangle = this._unscaleArea(areaRectangle.x, areaRectangle.y,
|
|
|
|
areaRectangle.width, areaRectangle.height);
|
|
|
|
let retval = GLib.Variant.new('(iiii)', retRectangle);
|
|
|
|
invocation.return_value(retval);
|
|
|
|
} else {
|
|
|
|
invocation.return_error_literal(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED,
|
|
|
|
"Operation was cancelled");
|
|
|
|
}
|
|
|
|
});
|
2012-12-10 15:26:31 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
FlashAreaAsync(params, invocation) {
|
2014-06-04 14:26:06 +00:00
|
|
|
let [x, y, width, height] = params;
|
2014-06-16 18:28:17 +00:00
|
|
|
[x, y, width, height] = this._scaleArea(x, y, width, height);
|
2014-06-04 14:26:06 +00:00
|
|
|
if (!this._checkArea(x, y, width, height)) {
|
|
|
|
invocation.return_error_literal(Gio.IOErrorEnum,
|
|
|
|
Gio.IOErrorEnum.CANCELLED,
|
|
|
|
"Invalid params");
|
|
|
|
return;
|
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
let flashspot = new Flashspot({ x : x, y : y, width: width, height: height});
|
|
|
|
flashspot.fire();
|
2014-06-04 14:26:06 +00:00
|
|
|
invocation.return_value(null);
|
2012-12-10 15:26:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var SelectArea = new Lang.Class({
|
2012-12-10 15:26:31 +00:00
|
|
|
Name: 'SelectArea',
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_init() {
|
2012-12-10 15:26:31 +00:00
|
|
|
this._startX = -1;
|
|
|
|
this._startY = -1;
|
|
|
|
this._lastX = 0;
|
|
|
|
this._lastY = 0;
|
2014-04-25 16:43:10 +00:00
|
|
|
this._result = null;
|
2012-12-10 15:26:31 +00:00
|
|
|
|
|
|
|
this._initRubberbandColors();
|
|
|
|
|
|
|
|
this._group = new St.Widget({ visible: false,
|
|
|
|
reactive: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0 });
|
|
|
|
Main.uiGroup.add_actor(this._group);
|
|
|
|
|
2014-04-25 16:43:10 +00:00
|
|
|
this._grabHelper = new GrabHelper.GrabHelper(this._group);
|
|
|
|
|
2012-12-10 15:26:31 +00:00
|
|
|
this._group.connect('button-press-event',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onButtonPress.bind(this));
|
2012-12-10 15:26:31 +00:00
|
|
|
this._group.connect('button-release-event',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onButtonRelease.bind(this));
|
2012-12-10 15:26:31 +00:00
|
|
|
this._group.connect('motion-event',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onMotionEvent.bind(this));
|
2012-12-10 15:26:31 +00:00
|
|
|
|
|
|
|
let constraint = new Clutter.BindConstraint({ source: global.stage,
|
|
|
|
coordinate: Clutter.BindCoordinate.ALL });
|
|
|
|
this._group.add_constraint(constraint);
|
|
|
|
|
|
|
|
this._rubberband = new Clutter.Rectangle({ color: this._background,
|
|
|
|
has_border: true,
|
|
|
|
border_width: 1,
|
|
|
|
border_color: this._border });
|
|
|
|
this._group.add_actor(this._rubberband);
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
show() {
|
2014-04-25 16:43:10 +00:00
|
|
|
if (!this._grabHelper.grab({ actor: this._group,
|
2017-12-02 00:27:35 +00:00
|
|
|
onUngrab: this._onUngrab.bind(this) }))
|
2012-12-10 15:26:31 +00:00
|
|
|
return;
|
|
|
|
|
2013-09-11 15:35:58 +00:00
|
|
|
global.screen.set_cursor(Meta.Cursor.CROSSHAIR);
|
2014-04-25 16:43:10 +00:00
|
|
|
Main.uiGroup.set_child_above_sibling(this._group, null);
|
2012-12-10 15:26:31 +00:00
|
|
|
this._group.visible = true;
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_initRubberbandColors() {
|
2012-12-10 15:26:31 +00:00
|
|
|
function colorFromRGBA(rgba) {
|
|
|
|
return new Clutter.Color({ red: rgba.red * 255,
|
|
|
|
green: rgba.green * 255,
|
|
|
|
blue: rgba.blue * 255,
|
|
|
|
alpha: rgba.alpha * 255 });
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = new Gtk.WidgetPath();
|
|
|
|
path.append_type(Gtk.IconView);
|
|
|
|
|
|
|
|
let context = new Gtk.StyleContext();
|
|
|
|
context.set_path(path);
|
|
|
|
context.add_class('rubberband');
|
|
|
|
|
|
|
|
this._background = colorFromRGBA(context.get_background_color(Gtk.StateFlags.NORMAL));
|
|
|
|
this._border = colorFromRGBA(context.get_border_color(Gtk.StateFlags.NORMAL));
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getGeometry() {
|
2012-12-10 15:26:31 +00:00
|
|
|
return { x: Math.min(this._startX, this._lastX),
|
|
|
|
y: Math.min(this._startY, this._lastY),
|
2015-03-14 22:48:41 +00:00
|
|
|
width: Math.abs(this._startX - this._lastX) + 1,
|
|
|
|
height: Math.abs(this._startY - this._lastY) + 1 };
|
2012-12-10 15:26:31 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onMotionEvent(actor, event) {
|
2012-12-10 15:26:31 +00:00
|
|
|
if (this._startX == -1 || this._startY == -1)
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2012-12-10 15:26:31 +00:00
|
|
|
|
|
|
|
[this._lastX, this._lastY] = event.get_coords();
|
2015-03-14 22:45:11 +00:00
|
|
|
this._lastX = Math.floor(this._lastX);
|
|
|
|
this._lastY = Math.floor(this._lastY);
|
2012-12-10 15:26:31 +00:00
|
|
|
let geometry = this._getGeometry();
|
|
|
|
|
|
|
|
this._rubberband.set_position(geometry.x, geometry.y);
|
|
|
|
this._rubberband.set_size(geometry.width, geometry.height);
|
|
|
|
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2012-12-10 15:26:31 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onButtonPress(actor, event) {
|
2012-12-10 15:26:31 +00:00
|
|
|
[this._startX, this._startY] = event.get_coords();
|
2015-03-14 22:45:11 +00:00
|
|
|
this._startX = Math.floor(this._startX);
|
|
|
|
this._startY = Math.floor(this._startY);
|
2012-12-10 15:26:31 +00:00
|
|
|
this._rubberband.set_position(this._startX, this._startY);
|
|
|
|
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2012-12-10 15:26:31 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onButtonRelease(actor, event) {
|
2014-04-25 16:43:10 +00:00
|
|
|
this._result = this._getGeometry();
|
2012-12-10 15:26:31 +00:00
|
|
|
Tweener.addTween(this._group,
|
|
|
|
{ opacity: 0,
|
2014-04-25 16:43:10 +00:00
|
|
|
time: 0.2,
|
2012-12-10 15:26:31 +00:00
|
|
|
transition: 'easeOutQuad',
|
2017-10-31 00:38:18 +00:00
|
|
|
onComplete: () => {
|
|
|
|
this._grabHelper.ungrab();
|
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
});
|
2014-04-25 16:43:10 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onUngrab() {
|
2014-04-25 16:43:10 +00:00
|
|
|
global.screen.set_cursor(Meta.Cursor.DEFAULT);
|
|
|
|
this.emit('finished', this._result);
|
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
|
|
this._group.destroy();
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
2012-12-10 15:26:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Signals.addSignalMethods(SelectArea.prototype);
|
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var FLASHSPOT_ANIMATION_OUT_TIME = 0.5; // seconds
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var Flashspot = new Lang.Class({
|
2012-12-10 15:26:31 +00:00
|
|
|
Name: 'Flashspot',
|
|
|
|
Extends: Lightbox.Lightbox,
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(area) {
|
2012-12-10 15:26:31 +00:00
|
|
|
this.parent(Main.uiGroup, { inhibitEvents: true,
|
|
|
|
width: area.width,
|
|
|
|
height: area.height });
|
|
|
|
|
|
|
|
this.actor.style_class = 'flashspot';
|
|
|
|
this.actor.set_position(area.x, area.y);
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
fire(doneCallback) {
|
2012-12-10 15:26:31 +00:00
|
|
|
this.actor.show();
|
2013-01-14 22:51:36 +00:00
|
|
|
this.actor.opacity = 255;
|
2012-12-10 15:26:31 +00:00
|
|
|
Tweener.addTween(this.actor,
|
|
|
|
{ opacity: 0,
|
2013-01-14 22:51:36 +00:00
|
|
|
time: FLASHSPOT_ANIMATION_OUT_TIME,
|
|
|
|
transition: 'easeOutQuad',
|
2017-10-31 00:38:18 +00:00
|
|
|
onComplete: () => {
|
2014-09-27 11:35:22 +00:00
|
|
|
if (doneCallback)
|
|
|
|
doneCallback();
|
2012-12-10 15:26:31 +00:00
|
|
|
this.destroy();
|
2017-10-31 00:38:18 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|