Philipp Hörist pushed to branch master at gajim / gajim


Commits:
655d41cf by lovetox at 2022-03-06T17:29:15+01:00
StartChat: Refactor showing new contact rows

- Create the rows only once and use _filter_func() to show/hide
- Fix searching for text which is not a valid JID

- - - - -


1 changed file:

- gajim/gtk/start_chat.py


Changes:

=====================================
gajim/gtk/start_chat.py
=====================================
@@ -92,8 +92,8 @@ def __init__(self) -> None:
 
         # Helper for the case where we don't receive a disco info
         self._new_chat_row: Optional[ContactRow] = None
+        self._search_is_valid_jid = False
 
-        self.new_contact_row_visible = False
         self.new_contact_rows: dict[str, Optional[ContactRow]] = {}
         self._accounts = app.get_enabled_accounts_with_labels()
 
@@ -101,6 +101,7 @@ def __init__(self) -> None:
         self._add_accounts()
         self._add_contacts(rows)
         self._add_groupchats(rows)
+        self._add_new_contact_rows(rows)
 
         self._ui.search_entry.connect(
             'search-changed', self._on_search_changed)
@@ -160,7 +161,6 @@ def _add_accounts(self) -> None:
     def _add_contacts(self, rows: list[ContactRow]):
         show_account = len(self._accounts) > 1
         for account, _label in self._accounts:
-            self.new_contact_rows[account] = None
             client = app.get_client(account)
             for jid, _data in client.get_module('Roster').iter():
                 contact = client.get_module('Contacts').get_contact(jid)
@@ -192,6 +192,13 @@ def _add_groupchats(self, rows: list[ContactRow]) -> None:
                                        show_account,
                                        groupchat=True))
 
+    def _add_new_contact_rows(self, rows: list[ContactRow]) -> None:
+        for account, _ in self._accounts:
+            show_account = len(self._accounts) > 1
+            row = ContactRow(account, None, None, None, show_account)
+            self.new_contact_rows[account] = row
+            rows.append(row)
+
     def _load_contacts(self, rows: list[ContactRow]) -> None:
         for row in rows:
             self._ui.listbox.add(row)
@@ -514,7 +521,6 @@ def _on_global_search_toggle(self, button: 
Gtk.ToggleButton) -> None:
             self._set_listbox(self._global_search_listbox)
             if self._ui.search_entry.get_text():
                 self._start_search()
-            self._remove_new_jid_row()
             self._ui.listbox.invalidate_filter()
         else:
             self._ui.filter_bar_toggle.set_sensitive(True)
@@ -525,28 +531,21 @@ def _on_global_search_toggle(self, button: 
Gtk.ToggleButton) -> None:
 
     def _on_search_changed(self, search_entry: Gtk.SearchEntry) -> None:
         self._show_search_entry_error(False)
+        self._search_is_valid_jid = False
 
         if self._global_search_active():
             return
 
         search_text = search_entry.get_text()
-        if not search_text:
-            self._remove_new_jid_row()
-            self._ui.listbox.invalidate_filter()
-            return
-
-        try:
-            validate_jid(search_text)
-        except ValueError:
-            self._show_search_entry_error(True)
-            self._remove_new_jid_row()
-            return
-
         if '@' in search_text:
-            self._add_new_jid_row()
-            self._update_new_jid_rows(search_text)
-        else:
-            self._remove_new_jid_row()
+            try:
+                validate_jid(search_text)
+            except ValueError:
+                self._show_search_entry_error(True)
+            else:
+                self._update_new_contact_rows(search_text)
+                self._search_is_valid_jid = True
+
         self._ui.listbox.invalidate_filter()
 
     def _show_search_entry_error(self, state: bool):
@@ -558,28 +557,7 @@ def _show_search_entry_error(self, state: bool):
             Gtk.EntryIconPosition.SECONDARY,
             _('Invalid Address'))
 
-    def _add_new_jid_row(self) -> None:
-        if self.new_contact_row_visible:
-            return
-        for account in self.new_contact_rows:
-            show_account = len(self._accounts) > 1
-            row = ContactRow(account, None, None, None, show_account)
-            self.new_contact_rows[account] = row
-            self._ui.listbox.add(row)
-            listbox = cast(Gtk.ListBox, row.get_parent())
-            listbox.show_all()
-        self.new_contact_row_visible = True
-
-    def _remove_new_jid_row(self) -> None:
-        if not self.new_contact_row_visible:
-            return
-
-        for row in self.new_contact_rows.values():
-            if row is not None:
-                row.destroy()
-        self.new_contact_row_visible = False
-
-    def _update_new_jid_rows(self, search_text: str) -> None:
+    def _update_new_contact_rows(self, search_text: str) -> None:
         for row in self.new_contact_rows.values():
             if row is not None:
                 row.update_jid(JID.from_string(search_text))
@@ -620,6 +598,10 @@ def _scroll_to_first_row(self) -> None:
         self._ui.scrolledwindow.get_vadjustment().set_value(0)
 
     def _filter_func(self, row: ContactRow, _user_data: Any) -> bool:
+        if row.contact is None:
+            # new contact row
+            return self._search_is_valid_jid
+
         search_text = self._ui.search_entry.get_text().lower()
         search_text_list = search_text.split()
         row_text = row.get_search_text().lower()



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/655d41cf2a5f553a9977284d01f85e64c6c1606b

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/655d41cf2a5f553a9977284d01f85e64c6c1606b
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to