[polari] connections: Use custom realname completion



commit bd4bc059cf180c18c082e62c5be0e00ceb253240
Author: Florian Müllner <fmuellner gnome org>
Date:   Mon Jul 11 14:01:28 2022 +0200

    connections: Use custom realname completion
    
    EntryCompletion is tied to the Entry widget, so our realname
    completion is currently preventing us from moving to libadwaita's
    new EntryRow widget.
    
    Luckily we don't use most of EntryCompletion's functionality: We
    only have a single completion that is inserted directly into the
    entry.
    
    That use is simple enough to reimplement it ourselves.
    
    Part-of: <https://gitlab.gnome.org/GNOME/polari/-/merge_requests/258>

 src/connections.js | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
---
diff --git a/src/connections.js b/src/connections.js
index 1835a317..1ee9c151 100644
--- a/src/connections.js
+++ b/src/connections.js
@@ -297,17 +297,25 @@ class ConnectionDetails extends Gtk.Box {
         this._sslSwitch.connect('notify::active',
             this._onCanConfirmChanged.bind(this));
 
-        let realnameStore = new Gtk.ListStore();
-        realnameStore.set_column_types([GObject.TYPE_STRING]);
-        realnameStore.insert_with_values(-1, [0], [GLib.get_real_name()]);
-
-        let completion = new Gtk.EntryCompletion({
-            model: realnameStore,
-            text_column: 0,
-            inline_completion: true,
-            popup_completion: false,
+        const buffer = this._realnameEntry.get_buffer();
+        const insertedTextId = buffer.connect('inserted-text', () => {
+            const text = this._realnameEntry.get_text();
+            const realname = GLib.get_real_name();
+            if (!realname.startsWith(text))
+                return;
+
+            GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
+                buffer.block_signal_handler(insertedTextId);
+
+                const startPos = GLib.utf8_strlen(text, -1);
+                this._realnameEntry.insert_text(
+                    realname.substring(text.length), -1, startPos);
+                this._realnameEntry.select_region(startPos, -1);
+
+                buffer.unblock_signal_handler(insertedTextId);
+                return GLib.SOURCE_REMOVE;
+            });
         });
-        this._realnameEntry.set_completion(completion);
 
         this.reset();
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]