2012-12-10 15:26:31 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported ScreenshotService */
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2018-07-30 16:31:17 +00:00
|
|
|
const { Clutter, Gio, GObject, GLib, Meta, Shell, St } = imports.gi;
|
2012-12-10 15:26:31 +00:00
|
|
|
|
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;
|
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
Gio._promisify(Shell.Screenshot.prototype, 'pick_color', 'pick_color_finish');
|
|
|
|
Gio._promisify(Shell.Screenshot.prototype, 'screenshot', 'screenshot_finish');
|
|
|
|
Gio._promisify(Shell.Screenshot.prototype,
|
|
|
|
'screenshot_window', 'screenshot_window_finish');
|
|
|
|
Gio._promisify(Shell.Screenshot.prototype,
|
|
|
|
'screenshot_area', 'screenshot_area_finish');
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
|
|
|
const ScreenshotIface = loadInterfaceXML('org.gnome.Shell.Screenshot');
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var ScreenshotService = class {
|
|
|
|
constructor() {
|
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 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-01-29 01:27:05 +00:00
|
|
|
_createScreenshot(invocation, needsDisk = true) {
|
2018-07-19 17:23:46 +00:00
|
|
|
let lockedDown = false;
|
|
|
|
if (needsDisk)
|
2019-01-29 01:18:52 +00:00
|
|
|
lockedDown = this._lockdownSettings.get_boolean('disable-save-to-disk');
|
2018-07-19 17:23:46 +00:00
|
|
|
|
2014-09-27 11:35:22 +00:00
|
|
|
let sender = invocation.get_sender();
|
2021-01-25 11:38:51 +00:00
|
|
|
if (this._screenShooter.has(sender)) {
|
2020-05-28 22:35:27 +00:00
|
|
|
invocation.return_error_literal(
|
|
|
|
Gio.IOErrorEnum, Gio.IOErrorEnum.BUSY,
|
|
|
|
'There is an ongoing operation for this sender');
|
2014-09-27 11:35:22 +00:00
|
|
|
return null;
|
2021-01-25 11:38:51 +00:00
|
|
|
} else if (lockedDown) {
|
|
|
|
invocation.return_error_literal(
|
|
|
|
Gio.IOErrorEnum, Gio.IOErrorEnum.PERMISSION_DENIED,
|
|
|
|
'Saving to disk is disabled');
|
|
|
|
return null;
|
2014-09-27 11:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 01:19:44 +00:00
|
|
|
}
|
2014-09-27 11:35:22 +00:00
|
|
|
|
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 01:19:44 +00:00
|
|
|
}
|
2014-09-27 11:35:22 +00:00
|
|
|
|
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 01:19:44 +00:00
|
|
|
}
|
2014-09-27 11:35:22 +00:00
|
|
|
|
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 01:19:44 +00:00
|
|
|
}
|
2014-06-04 14:26:06 +00:00
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
*_resolveRelativeFilename(filename) {
|
2020-01-10 18:26:05 +00:00
|
|
|
filename = filename.replace(/\.png$/, '');
|
2019-11-05 23:46:41 +00:00
|
|
|
|
|
|
|
let path = [
|
|
|
|
GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES),
|
|
|
|
GLib.get_home_dir(),
|
|
|
|
].find(p => GLib.file_test(p, GLib.FileTest.EXISTS));
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
yield Gio.File.new_for_path(
|
|
|
|
GLib.build_filenamev([path, `${filename}.png`]));
|
|
|
|
|
|
|
|
for (let idx = 1; ; idx++) {
|
|
|
|
yield Gio.File.new_for_path(
|
|
|
|
GLib.build_filenamev([path, `${filename}-${idx}.png`]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 06:44:31 +00:00
|
|
|
_createStream(filename, invocation) {
|
2019-11-05 23:48:25 +00:00
|
|
|
if (filename == '')
|
|
|
|
return [Gio.MemoryOutputStream.new_resizable(), null];
|
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
if (GLib.path_is_absolute(filename)) {
|
|
|
|
try {
|
|
|
|
let file = Gio.File.new_for_path(filename);
|
|
|
|
let stream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);
|
|
|
|
return [stream, file];
|
|
|
|
} catch (e) {
|
2021-01-25 11:32:26 +00:00
|
|
|
invocation.return_gerror(e);
|
2021-01-25 11:12:44 +00:00
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
2019-11-05 23:46:41 +00:00
|
|
|
return [null, null];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 11:32:26 +00:00
|
|
|
let err;
|
2019-11-05 23:46:41 +00:00
|
|
|
for (let file of this._resolveRelativeFilename(filename)) {
|
|
|
|
try {
|
|
|
|
let stream = file.create(Gio.FileCreateFlags.NONE, null);
|
|
|
|
return [stream, file];
|
|
|
|
} catch (e) {
|
2021-01-25 11:32:26 +00:00
|
|
|
err = e;
|
2019-11-05 23:46:41 +00:00
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
|
2020-03-24 06:44:31 +00:00
|
|
|
break;
|
2019-11-05 23:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 11:32:26 +00:00
|
|
|
invocation.return_gerror(err);
|
2021-01-25 11:12:44 +00:00
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
2019-11-05 23:46:41 +00:00
|
|
|
return [null, null];
|
|
|
|
}
|
|
|
|
|
2020-03-24 06:15:26 +00:00
|
|
|
_onScreenshotComplete(area, stream, file, flash, invocation) {
|
|
|
|
if (flash) {
|
|
|
|
let flashspot = new Flashspot(area);
|
|
|
|
flashspot.fire(() => {
|
2014-09-27 11:35:22 +00:00
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
2020-03-24 06:15:26 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
2012-12-10 15:26:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
stream.close(null);
|
|
|
|
|
2019-11-05 23:48:25 +00:00
|
|
|
let filenameUsed = '';
|
|
|
|
if (file) {
|
|
|
|
filenameUsed = file.get_path();
|
|
|
|
} else {
|
|
|
|
let bytes = stream.steal_as_bytes();
|
|
|
|
let clipboard = St.Clipboard.get_default();
|
|
|
|
clipboard.set_content(St.ClipboardType.CLIPBOARD, 'image/png', bytes);
|
|
|
|
}
|
|
|
|
|
2020-03-24 06:15:26 +00:00
|
|
|
let retval = GLib.Variant.new('(bs)', [true, filenameUsed]);
|
2012-12-10 15:26:31 +00:00
|
|
|
invocation.return_value(retval);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
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 01:19:44 +00:00
|
|
|
}
|
2014-06-16 18:28:17 +00:00
|
|
|
|
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 01:19:44 +00:00
|
|
|
}
|
2014-06-16 18:28:17 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
async ScreenshotAreaAsync(params, invocation) {
|
2019-02-04 10:58:00 +00:00
|
|
|
let [x, y, width, height, flash, filename] = 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;
|
2019-11-05 23:46:41 +00:00
|
|
|
|
2020-03-24 06:44:31 +00:00
|
|
|
let [stream, file] = this._createStream(filename, invocation);
|
|
|
|
if (!stream)
|
|
|
|
return;
|
2019-11-05 23:46:41 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
try {
|
|
|
|
let [area] =
|
|
|
|
await screenshot.screenshot_area(x, y, width, height, stream);
|
|
|
|
this._onScreenshotComplete(area, stream, file, flash, invocation);
|
|
|
|
} catch (e) {
|
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
|
|
|
invocation.return_value(new GLib.Variant('(bs)', [false, '']));
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
async ScreenshotWindowAsync(params, invocation) {
|
2019-01-31 13:43:52 +00:00
|
|
|
let [includeFrame, includeCursor, flash, filename] = params;
|
2014-09-27 11:35:22 +00:00
|
|
|
let screenshot = this._createScreenshot(invocation);
|
|
|
|
if (!screenshot)
|
|
|
|
return;
|
2019-11-05 23:46:41 +00:00
|
|
|
|
2020-03-24 06:44:31 +00:00
|
|
|
let [stream, file] = this._createStream(filename, invocation);
|
|
|
|
if (!stream)
|
|
|
|
return;
|
2019-11-05 23:46:41 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
try {
|
|
|
|
let [area] =
|
|
|
|
await screenshot.screenshot_window(includeFrame, includeCursor, stream);
|
|
|
|
this._onScreenshotComplete(area, stream, file, flash, invocation);
|
|
|
|
} catch (e) {
|
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
|
|
|
invocation.return_value(new GLib.Variant('(bs)', [false, '']));
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
async ScreenshotAsync(params, invocation) {
|
2019-01-31 13:43:52 +00:00
|
|
|
let [includeCursor, flash, filename] = params;
|
2014-09-27 11:35:22 +00:00
|
|
|
let screenshot = this._createScreenshot(invocation);
|
|
|
|
if (!screenshot)
|
|
|
|
return;
|
2019-11-05 23:46:41 +00:00
|
|
|
|
2020-03-24 06:44:31 +00:00
|
|
|
let [stream, file] = this._createStream(filename, invocation);
|
|
|
|
if (!stream)
|
|
|
|
return;
|
2019-11-05 23:46:41 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
try {
|
|
|
|
let [area] = await screenshot.screenshot(includeCursor, stream);
|
|
|
|
this._onScreenshotComplete(area, stream, file, flash, invocation);
|
|
|
|
} catch (e) {
|
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
|
|
|
invocation.return_value(new GLib.Variant('(bs)', [false, '']));
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-12-19 02:39:39 +00:00
|
|
|
async SelectAreaAsync(params, invocation) {
|
2012-12-10 15:26:31 +00:00
|
|
|
let selectArea = new SelectArea();
|
2019-12-19 02:39:39 +00:00
|
|
|
try {
|
|
|
|
let areaRectangle = await selectArea.selectAsync();
|
|
|
|
let retRectangle = this._unscaleArea(
|
|
|
|
areaRectangle.x, areaRectangle.y,
|
|
|
|
areaRectangle.width, areaRectangle.height);
|
|
|
|
invocation.return_value(GLib.Variant.new('(iiii)', retRectangle));
|
|
|
|
} catch (e) {
|
|
|
|
invocation.return_error_literal(
|
|
|
|
Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED,
|
|
|
|
'Operation was cancelled');
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2019-08-19 19:06:04 +00:00
|
|
|
let flashspot = new Flashspot({ x, y, width, height });
|
2012-12-10 15:26:31 +00:00
|
|
|
flashspot.fire();
|
2014-06-04 14:26:06 +00:00
|
|
|
invocation.return_value(null);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2018-07-17 23:34:59 +00:00
|
|
|
|
2019-12-19 02:53:51 +00:00
|
|
|
async PickColorAsync(params, invocation) {
|
2018-07-30 16:31:17 +00:00
|
|
|
const screenshot = this._createScreenshot(invocation, false);
|
|
|
|
if (!screenshot)
|
|
|
|
return;
|
2019-12-19 02:53:51 +00:00
|
|
|
|
2018-07-30 16:31:17 +00:00
|
|
|
const pickPixel = new PickPixel(screenshot);
|
|
|
|
try {
|
|
|
|
const color = await pickPixel.pickAsync();
|
2019-12-19 19:50:37 +00:00
|
|
|
const { red, green, blue } = color;
|
|
|
|
const retval = GLib.Variant.new('(a{sv})', [{
|
|
|
|
color: GLib.Variant.new('(ddd)', [
|
|
|
|
red / 255.0,
|
|
|
|
green / 255.0,
|
|
|
|
blue / 255.0,
|
|
|
|
]),
|
|
|
|
}]);
|
|
|
|
invocation.return_value(retval);
|
2019-12-19 02:53:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
invocation.return_error_literal(
|
|
|
|
Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED,
|
|
|
|
'Operation was cancelled');
|
2020-05-28 22:27:34 +00:00
|
|
|
} finally {
|
|
|
|
this._removeShooterForSender(invocation.get_sender());
|
2019-12-19 02:53:51 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-12-19 02:39:39 +00:00
|
|
|
var SelectArea = GObject.registerClass(
|
|
|
|
class SelectArea extends St.Widget {
|
2019-07-16 09:24:13 +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
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
super._init({
|
|
|
|
visible: false,
|
|
|
|
reactive: true,
|
|
|
|
x: 0,
|
2019-08-20 21:43:54 +00:00
|
|
|
y: 0,
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|
|
|
|
Main.uiGroup.add_actor(this);
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
this._grabHelper = new GrabHelper.GrabHelper(this);
|
2014-04-25 16:43:10 +00:00
|
|
|
|
2012-12-10 15:26:31 +00:00
|
|
|
let constraint = new Clutter.BindConstraint({ source: global.stage,
|
|
|
|
coordinate: Clutter.BindCoordinate.ALL });
|
2019-07-16 09:24:13 +00:00
|
|
|
this.add_constraint(constraint);
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-04-04 14:03:54 +00:00
|
|
|
this._rubberband = new St.Widget({
|
|
|
|
style_class: 'select-area-rubberband',
|
2019-08-20 21:43:54 +00:00
|
|
|
visible: false,
|
2019-04-04 14:03:54 +00:00
|
|
|
});
|
2019-07-16 09:24:13 +00:00
|
|
|
this.add_actor(this._rubberband);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-12-19 02:39:39 +00:00
|
|
|
async selectAsync() {
|
2018-01-03 07:55:38 +00:00
|
|
|
global.display.set_cursor(Meta.Cursor.CROSSHAIR);
|
2019-07-16 09:24:13 +00:00
|
|
|
Main.uiGroup.set_child_above_sibling(this, null);
|
2019-12-19 02:39:39 +00:00
|
|
|
this.show();
|
|
|
|
|
2021-01-27 22:27:31 +00:00
|
|
|
try {
|
|
|
|
await this._grabHelper.grabAsync({ actor: this });
|
|
|
|
} finally {
|
|
|
|
global.display.set_cursor(Meta.Cursor.DEFAULT);
|
2019-12-19 02:39:39 +00:00
|
|
|
|
2021-01-27 22:27:31 +00:00
|
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
|
|
this.destroy();
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
|
|
|
}
|
2019-12-19 02:39:39 +00:00
|
|
|
|
|
|
|
return this._result;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getGeometry() {
|
2019-07-13 00:17:20 +00:00
|
|
|
return new Meta.Rectangle({
|
|
|
|
x: Math.min(this._startX, this._lastX),
|
|
|
|
y: Math.min(this._startY, this._lastY),
|
|
|
|
width: Math.abs(this._startX - this._lastX) + 1,
|
2019-08-20 21:43:54 +00:00
|
|
|
height: Math.abs(this._startY - this._lastY) + 1,
|
2019-07-13 00:17:20 +00:00
|
|
|
});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-09-10 05:42:48 +00:00
|
|
|
vfunc_motion_event(motionEvent) {
|
2019-09-10 06:49:50 +00:00
|
|
|
if (this._startX == -1 || this._startY == -1 || this._result)
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-09-10 05:42:48 +00:00
|
|
|
[this._lastX, this._lastY] = [motionEvent.x, motionEvent.y];
|
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);
|
2019-04-04 14:03:54 +00:00
|
|
|
this._rubberband.show();
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-09-10 05:42:48 +00:00
|
|
|
vfunc_button_press_event(buttonEvent) {
|
|
|
|
[this._startX, this._startY] = [buttonEvent.x, buttonEvent.y];
|
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;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-09-10 05:42:48 +00:00
|
|
|
vfunc_button_release_event() {
|
2014-04-25 16:43:10 +00:00
|
|
|
this._result = this._getGeometry();
|
2019-07-16 09:24:13 +00:00
|
|
|
this.ease({
|
2018-07-20 19:46:19 +00:00
|
|
|
opacity: 0,
|
|
|
|
duration: 200,
|
|
|
|
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
2019-08-20 21:43:54 +00:00
|
|
|
onComplete: () => this._grabHelper.ungrab(),
|
2018-07-20 19:46:19 +00:00
|
|
|
});
|
2014-04-25 16:43:10 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|
|
|
|
|
2018-07-30 16:31:17 +00:00
|
|
|
var RecolorEffect = GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
color: GObject.ParamSpec.boxed(
|
|
|
|
'color', 'color', 'replacement color',
|
|
|
|
GObject.ParamFlags.WRITABLE,
|
|
|
|
Clutter.Color.$gtype),
|
|
|
|
chroma: GObject.ParamSpec.boxed(
|
|
|
|
'chroma', 'chroma', 'color to replace',
|
|
|
|
GObject.ParamFlags.WRITABLE,
|
|
|
|
Clutter.Color.$gtype),
|
|
|
|
threshold: GObject.ParamSpec.float(
|
|
|
|
'threshold', 'threshold', 'threshold',
|
|
|
|
GObject.ParamFlags.WRITABLE,
|
|
|
|
0.0, 1.0, 0.0),
|
|
|
|
smoothing: GObject.ParamSpec.float(
|
|
|
|
'smoothing', 'smoothing', 'smoothing',
|
|
|
|
GObject.ParamFlags.WRITABLE,
|
|
|
|
0.0, 1.0, 0.0),
|
|
|
|
},
|
|
|
|
}, class RecolorEffect extends Shell.GLSLEffect {
|
|
|
|
_init(params) {
|
|
|
|
this._color = new Clutter.Color();
|
|
|
|
this._chroma = new Clutter.Color();
|
|
|
|
this._threshold = 0;
|
|
|
|
this._smoothing = 0;
|
|
|
|
|
|
|
|
this._colorLocation = null;
|
|
|
|
this._chromaLocation = null;
|
|
|
|
this._thresholdLocation = null;
|
|
|
|
this._smoothingLocation = null;
|
|
|
|
|
|
|
|
super._init(params);
|
|
|
|
|
|
|
|
this._colorLocation = this.get_uniform_location('recolor_color');
|
|
|
|
this._chromaLocation = this.get_uniform_location('chroma_color');
|
|
|
|
this._thresholdLocation = this.get_uniform_location('threshold');
|
|
|
|
this._smoothingLocation = this.get_uniform_location('smoothing');
|
|
|
|
|
|
|
|
this._updateColorUniform(this._colorLocation, this._color);
|
|
|
|
this._updateColorUniform(this._chromaLocation, this._chroma);
|
|
|
|
this._updateFloatUniform(this._thresholdLocation, this._threshold);
|
|
|
|
this._updateFloatUniform(this._smoothingLocation, this._smoothing);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateColorUniform(location, color) {
|
|
|
|
if (!location)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.set_uniform_float(location,
|
|
|
|
3, [color.red / 255, color.green / 255, color.blue / 255]);
|
|
|
|
this.queue_repaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateFloatUniform(location, value) {
|
|
|
|
if (!location)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.set_uniform_float(location, 1, [value]);
|
|
|
|
this.queue_repaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
set color(c) {
|
|
|
|
if (this._color.equal(c))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._color = c;
|
|
|
|
this.notify('color');
|
|
|
|
|
|
|
|
this._updateColorUniform(this._colorLocation, this._color);
|
|
|
|
}
|
|
|
|
|
|
|
|
set chroma(c) {
|
|
|
|
if (this._chroma.equal(c))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._chroma = c;
|
|
|
|
this.notify('chroma');
|
|
|
|
|
|
|
|
this._updateColorUniform(this._chromaLocation, this._chroma);
|
|
|
|
}
|
|
|
|
|
|
|
|
set threshold(value) {
|
|
|
|
if (this._threshold === value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._threshold = value;
|
|
|
|
this.notify('threshold');
|
|
|
|
|
|
|
|
this._updateFloatUniform(this._thresholdLocation, this._threshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
set smoothing(value) {
|
|
|
|
if (this._smoothing === value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._smoothing = value;
|
|
|
|
this.notify('smoothing');
|
|
|
|
|
|
|
|
this._updateFloatUniform(this._smoothingLocation, this._smoothing);
|
|
|
|
}
|
|
|
|
|
|
|
|
vfunc_build_pipeline() {
|
|
|
|
// Conversion parameters from https://en.wikipedia.org/wiki/YCbCr
|
|
|
|
const decl = `
|
|
|
|
vec3 rgb2yCrCb(vec3 c) { \n
|
|
|
|
float y = 0.299 * c.r + 0.587 * c.g + 0.114 * c.b; \n
|
|
|
|
float cr = 0.7133 * (c.r - y); \n
|
|
|
|
float cb = 0.5643 * (c.b - y); \n
|
|
|
|
return vec3(y, cr, cb); \n
|
|
|
|
} \n
|
|
|
|
\n
|
|
|
|
uniform vec3 chroma_color; \n
|
|
|
|
uniform vec3 recolor_color; \n
|
|
|
|
uniform float threshold; \n
|
|
|
|
uniform float smoothing; \n`;
|
|
|
|
const src = `
|
|
|
|
vec3 mask = rgb2yCrCb(chroma_color.rgb); \n
|
|
|
|
vec3 yCrCb = rgb2yCrCb(cogl_color_out.rgb); \n
|
|
|
|
float blend = \n
|
|
|
|
smoothstep(threshold, \n
|
|
|
|
threshold + smoothing, \n
|
|
|
|
distance(yCrCb.gb, mask.gb)); \n
|
|
|
|
cogl_color_out.rgb = \n
|
|
|
|
mix(recolor_color, cogl_color_out.rgb, blend); \n`;
|
|
|
|
|
|
|
|
this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, decl, src, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-12-19 02:53:51 +00:00
|
|
|
var PickPixel = GObject.registerClass(
|
|
|
|
class PickPixel extends St.Widget {
|
2018-07-30 16:31:17 +00:00
|
|
|
_init(screenshot) {
|
2019-07-16 09:24:13 +00:00
|
|
|
super._init({ visible: false, reactive: true });
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2018-07-30 16:31:17 +00:00
|
|
|
this._screenshot = screenshot;
|
|
|
|
|
2018-07-17 23:34:59 +00:00
|
|
|
this._result = null;
|
2018-07-30 16:31:17 +00:00
|
|
|
this._color = null;
|
|
|
|
this._inPick = false;
|
2018-07-17 23:34:59 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
Main.uiGroup.add_actor(this);
|
2018-07-17 23:34:59 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
this._grabHelper = new GrabHelper.GrabHelper(this);
|
2018-07-17 23:34:59 +00:00
|
|
|
|
|
|
|
let constraint = new Clutter.BindConstraint({ source: global.stage,
|
|
|
|
coordinate: Clutter.BindCoordinate.ALL });
|
2019-07-16 09:24:13 +00:00
|
|
|
this.add_constraint(constraint);
|
2018-07-30 16:31:17 +00:00
|
|
|
|
|
|
|
const action = new Clutter.ClickAction();
|
|
|
|
action.connect('clicked', async () => {
|
|
|
|
await this._pickColor(...action.get_coords());
|
|
|
|
this._result = this._color;
|
|
|
|
this._grabHelper.ungrab();
|
|
|
|
});
|
|
|
|
this.add_action(action);
|
|
|
|
|
|
|
|
this._recolorEffect = new RecolorEffect({
|
|
|
|
chroma: new Clutter.Color({
|
|
|
|
red: 80,
|
|
|
|
green: 219,
|
|
|
|
blue: 181,
|
|
|
|
}),
|
|
|
|
threshold: 0.04,
|
|
|
|
smoothing: 0.07,
|
|
|
|
});
|
|
|
|
this._previewCursor = new St.Icon({
|
|
|
|
icon_name: 'color-pick',
|
|
|
|
icon_size: Meta.prefs_get_cursor_size(),
|
|
|
|
effect: this._recolorEffect,
|
|
|
|
visible: false,
|
|
|
|
});
|
|
|
|
Main.uiGroup.add_actor(this._previewCursor);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2018-07-17 23:34:59 +00:00
|
|
|
|
2019-12-19 02:53:51 +00:00
|
|
|
async pickAsync() {
|
2018-07-30 16:31:17 +00:00
|
|
|
global.display.set_cursor(Meta.Cursor.BLANK);
|
2019-07-16 09:24:13 +00:00
|
|
|
Main.uiGroup.set_child_above_sibling(this, null);
|
2019-12-19 02:53:51 +00:00
|
|
|
this.show();
|
2018-07-17 23:34:59 +00:00
|
|
|
|
2018-07-30 16:31:17 +00:00
|
|
|
this._pickColor(...global.get_pointer());
|
|
|
|
|
2021-01-27 22:27:31 +00:00
|
|
|
try {
|
|
|
|
await this._grabHelper.grabAsync({ actor: this });
|
|
|
|
} finally {
|
|
|
|
global.display.set_cursor(Meta.Cursor.DEFAULT);
|
|
|
|
this._previewCursor.destroy();
|
2018-07-17 23:34:59 +00:00
|
|
|
|
2021-01-27 22:27:31 +00:00
|
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
|
|
this.destroy();
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
|
|
|
}
|
2019-12-19 02:53:51 +00:00
|
|
|
|
|
|
|
return this._result;
|
|
|
|
}
|
|
|
|
|
2018-07-30 16:31:17 +00:00
|
|
|
async _pickColor(x, y) {
|
|
|
|
if (this._inPick)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._inPick = true;
|
|
|
|
this._previewCursor.set_position(x, y);
|
|
|
|
[this._color] = await this._screenshot.pick_color(x, y);
|
|
|
|
this._inPick = false;
|
|
|
|
|
|
|
|
if (!this._color)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._recolorEffect.color = this._color;
|
|
|
|
this._previewCursor.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
vfunc_motion_event(motionEvent) {
|
|
|
|
const { x, y } = motionEvent;
|
|
|
|
this._pickColor(x, y);
|
2019-12-19 02:53:51 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2018-07-17 23:34:59 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|
2018-07-17 23:34:59 +00:00
|
|
|
|
2019-08-01 23:13:10 +00:00
|
|
|
var FLASHSPOT_ANIMATION_OUT_TIME = 500; // milliseconds
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
var Flashspot = GObject.registerClass(
|
|
|
|
class Flashspot extends Lightbox.Lightbox {
|
|
|
|
_init(area) {
|
|
|
|
super._init(Main.uiGroup, {
|
|
|
|
inhibitEvents: true,
|
|
|
|
width: area.width,
|
2019-08-20 21:43:54 +00:00
|
|
|
height: area.height,
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|
|
|
|
this.style_class = 'flashspot';
|
|
|
|
this.set_position(area.x, area.y);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-12-10 15:26:31 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
fire(doneCallback) {
|
2019-07-16 09:24:13 +00:00
|
|
|
this.set({ visible: true, opacity: 255 });
|
|
|
|
this.ease({
|
2018-07-20 19:46:19 +00:00
|
|
|
opacity: 0,
|
|
|
|
duration: FLASHSPOT_ANIMATION_OUT_TIME,
|
|
|
|
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
|
|
|
onComplete: () => {
|
|
|
|
if (doneCallback)
|
|
|
|
doneCallback();
|
|
|
|
this.destroy();
|
2019-08-20 21:43:54 +00:00
|
|
|
},
|
2018-07-20 19:46:19 +00:00
|
|
|
});
|
2012-12-10 15:26:31 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|