From e34b164f5a876d1b3e3a5b15a489bb562f00d2af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Thu, 2 Sep 2021 18:58:51 +0200 Subject: [PATCH] Auto fill open URI entry with clipboard content If clipboard contains a valid URI, prefill the entry with it. Also select all of it to make it easier to remove this text. --- src/dialogs.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/dialogs.js b/src/dialogs.js index 2e444c24..8a504e14 100644 --- a/src/dialogs.js +++ b/src/dialogs.js @@ -1,4 +1,4 @@ -const { Gio, GObject, Gtk, Gst } = imports.gi; +const { Gdk, Gio, GObject, Gst, Gtk } = imports.gi; const System = imports.system; const Debug = imports.src.debug; const FileOps = imports.src.fileOps; @@ -131,7 +131,7 @@ class ClapperUriDialog extends Gtk.Dialog modal: true, use_header_bar: true, title: _('Open URI'), - default_width: 400, + default_width: 420, }); const contentBox = this.get_content_area(); @@ -156,6 +156,12 @@ class ClapperUriDialog extends Gtk.Dialog this.set_default_response(Gtk.ResponseType.OK); this.set_response_sensitive(Gtk.ResponseType.OK, false); + const display = Gdk.Display.get_default(); + const clipboard = (display) ? display.get_clipboard() : null; + + if(clipboard) + clipboard.read_text_async(null, this._readTextAsyncCb.bind(this)); + this.show(); } @@ -180,6 +186,18 @@ class ClapperUriDialog extends Gtk.Dialog this.set_response_sensitive(Gtk.ResponseType.OK, isUriValid); } + + _readTextAsyncCb(clipboard, result) + { + const uri = clipboard.read_text_finish(result); + if(!uri || !Gst.uri_is_valid(uri)) return; + + const contentBox = this.get_content_area(); + const linkEntry = contentBox.get_last_child(); + + linkEntry.set_text(uri); + linkEntry.select_region(0, -1); + } }); var ResumeDialog = GObject.registerClass(