Browse Source

Don't completely refresh the list every second

Previously, we refreshed the list by completely erasing it and building
a new one.  While simple, it caused some obnoxious visual glitches, and
I could imagine some more serious problems it might have caused as well.

Now we remove only the devices that don't exist any more, and add only
those that we don't already know about.  Much better for the user
interface, but O(n^2).  Oh well, I doubt anyone will have enough PD
Buddies connected at once for that to matter much.
Clara Hobbs 7 years ago
parent
commit
b7a11e7cde
1 changed files with 23 additions and 6 deletions
  1. 23
    6
      pd-buddy-gtk.py

+ 23
- 6
pd-buddy-gtk.py View File

@@ -57,12 +57,29 @@ class SelectListStore(Gio.ListStore):
57 57
         GLib.timeout_add(1000, self.update_items)
58 58
 
59 59
     def update_items(self):
60
-        # Clear the old entries
61
-        self.remove_all()
62
-
63
-        # Search for the serial ports
64
-        for serport in serial.tools.list_ports.grep("1209:0001"):
65
-            self.append(ListRowModel(serport))
60
+        # Get a list of serial ports
61
+        serports = list(serial.tools.list_ports.grep("1209:0001"))
62
+
63
+        # Mark ports to remove or add
64
+        remove_list = []
65
+        list_len = self.get_n_items()
66
+        for i in range(list_len):
67
+            remove = True
68
+            for j in range(len(serports)):
69
+                if serports[j] is not None and self.get_item(i).serport == serports[j]:
70
+                    serports[j] = None
71
+                    remove = False
72
+            if remove:
73
+                remove_list.append(i)
74
+
75
+        # Remove the missing ones
76
+        for i in remove_list:
77
+            self.remove(i)
78
+
79
+        # Add any new ports
80
+        for port in serports:
81
+            if port is not None:
82
+                self.append(ListRowModel(port))
66 83
 
67 84
         # Set the visible child
68 85
         # FIXME: This is rather poor organization

Loading…
Cancel
Save