diff options
author | Florian Müllner <fmuellner@gnome.org> | 2018-07-18 01:34:59 +0200 |
---|---|---|
committer | Florian Müllner <fmuellner@gnome.org> | 2018-07-25 00:44:15 +0200 |
commit | a9eb838de2a6c69527db963101c5d72c1b34fb5f (patch) | |
tree | dc9f2b5be9ec066fafbd73362958cdd54f473937 | |
parent | fc53c7cf19760e2f6e542c87d0b32abb2e33ea23 (diff) | |
download | gnome-shell-wip/fmuellner/color-picker.tar.gz |
screenshot: Add PickColor() method to Screenshot interfacewip/fmuellner/color-picker
Use the newly added API to implement a color picker method in the
screenshot interface, so that the desktop portal can expose it
to applications.
https://gitlab.gnome.org/GNOME/gnome-shell/issues/286
-rw-r--r-- | data/org.gnome.Shell.Screenshot.xml | 10 | ||||
-rw-r--r-- | js/ui/screenshot.js | 77 |
2 files changed, 87 insertions, 0 deletions
diff --git a/data/org.gnome.Shell.Screenshot.xml b/data/org.gnome.Shell.Screenshot.xml index 9ffa5f61e..15045f8af 100644 --- a/data/org.gnome.Shell.Screenshot.xml +++ b/data/org.gnome.Shell.Screenshot.xml @@ -92,6 +92,16 @@ </method> <!-- + PickColor: + + Picks a color and returns the result in the "red", "green", + "blue" and "alpha" keys of the @result vardict. + --> + <method name="PickColor"> + <arg type="a{sv}" direction="out" name="result"/> + </method> + + <!-- FlashArea: @x: the X coordinate of the area to flash @y: the Y coordinate of the area to flash diff --git a/js/ui/screenshot.js b/js/ui/screenshot.js index 298a7a683..5b30d2fef 100644 --- a/js/ui/screenshot.js +++ b/js/ui/screenshot.js @@ -55,6 +55,9 @@ const ScreenshotIface = '<node> \ <arg type="i" direction="in" name="width"/> \ <arg type="i" direction="in" name="height"/> \ </method> \ +<method name="PickColor"> \ + <arg type="a{sv}" direction="out" name="color"/> \ +</method> \ </interface> \ </node>'; @@ -224,6 +227,32 @@ var ScreenshotService = new Lang.Class({ let flashspot = new Flashspot({ x : x, y : y, width: width, height: height}); flashspot.fire(); invocation.return_value(null); + }, + + PickColorAsync(params, invocation) { + let pickPixel = new PickPixel(); + pickPixel.show(); + pickPixel.connect('finished', (pickPixel, coords) => { + if (coords) { + let screenshot = this._createScreenshot(invocation, false); + if (!screenshot) + return; + screenshot.pick_color(...coords, (o, res) => { + let [success, color] = screenshot.pick_color_finish(res); + let retval = GLib.Variant.new('(a{sv})', [ + { red: GLib.Variant.new('d', color.red / 255.), + green: GLib.Variant.new('d', color.green / 255.), + blue: GLib.Variant.new('d', color.blue / 255.), + alpha: GLib.Variant.new('d', color.alpha / 255) } + ]); + this._removeShooterForSender(invocation.get_sender()); + invocation.return_value(retval); + }); + } else { + invocation.return_error_literal(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED, + "Operation was cancelled"); + } + }); } }); @@ -350,6 +379,54 @@ var SelectArea = new Lang.Class({ }); Signals.addSignalMethods(SelectArea.prototype); +var PickPixel = new Lang.Class({ + Name: 'PickPixel', + + _init() { + this._result = null; + + this._group = new St.Widget({ visible: false, + reactive: true }); + Main.uiGroup.add_actor(this._group); + + this._grabHelper = new GrabHelper.GrabHelper(this._group); + + this._group.connect('button-release-event', + this._onButtonRelease.bind(this)); + + let constraint = new Clutter.BindConstraint({ source: global.stage, + coordinate: Clutter.BindCoordinate.ALL }); + this._group.add_constraint(constraint); + }, + + show() { + if (!this._grabHelper.grab({ actor: this._group, + onUngrab: this._onUngrab.bind(this) })) + return; + + global.display.set_cursor(Meta.Cursor.CROSSHAIR); + Main.uiGroup.set_child_above_sibling(this._group, null); + this._group.visible = true; + }, + + _onButtonRelease(actor, event) { + this._result = event.get_coords(); + this._grabHelper.ungrab(); + return Clutter.EVENT_PROPAGATE; + }, + + _onUngrab() { + global.display.set_cursor(Meta.Cursor.DEFAULT); + this.emit('finished', this._result); + + GLib.idle_add(GLib.PRIORITY_DEFAULT, () => { + this._group.destroy(); + return GLib.SOURCE_REMOVE; + }); + } +}); +Signals.addSignalMethods(PickPixel.prototype); + var FLASHSPOT_ANIMATION_OUT_TIME = 0.5; // seconds var Flashspot = new Lang.Class({ |