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.
This commit is contained in:
Rafał Dzięgiel
2021-09-02 18:58:51 +02:00
parent 2e0a455f92
commit e34b164f5a

View File

@@ -1,4 +1,4 @@
const { Gio, GObject, Gtk, Gst } = imports.gi; const { Gdk, Gio, GObject, Gst, Gtk } = imports.gi;
const System = imports.system; const System = imports.system;
const Debug = imports.src.debug; const Debug = imports.src.debug;
const FileOps = imports.src.fileOps; const FileOps = imports.src.fileOps;
@@ -131,7 +131,7 @@ class ClapperUriDialog extends Gtk.Dialog
modal: true, modal: true,
use_header_bar: true, use_header_bar: true,
title: _('Open URI'), title: _('Open URI'),
default_width: 400, default_width: 420,
}); });
const contentBox = this.get_content_area(); const contentBox = this.get_content_area();
@@ -156,6 +156,12 @@ class ClapperUriDialog extends Gtk.Dialog
this.set_default_response(Gtk.ResponseType.OK); this.set_default_response(Gtk.ResponseType.OK);
this.set_response_sensitive(Gtk.ResponseType.OK, false); 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(); this.show();
} }
@@ -180,6 +186,18 @@ class ClapperUriDialog extends Gtk.Dialog
this.set_response_sensitive(Gtk.ResponseType.OK, isUriValid); 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( var ResumeDialog = GObject.registerClass(