Skip to content

Commit

Permalink
Manager: Rework list sorting
Browse files Browse the repository at this point in the history
Our list got very confusing with newly found device rows being inserted
in the list based on name or datetime added. When you have lot's of
devices it becomes a pain to find them so we need a better way.

We primarily now sort whether a device is paired. The logic behind this
is that these are the devices the user most likely is interested in. Next
we sort all connected devices below and the remaining ones at the bottom.
  • Loading branch information
infirit committed Feb 27, 2023
1 parent f946bc6 commit cce37cc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 131 deletions.
43 changes: 25 additions & 18 deletions blueman/gui/manager/ManagerDeviceList.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ def __init__(self, inst: "Blueman", adapter: Optional[str] = None) -> None:
self._batteries: Dict[str, Battery] = {}

self.Config = Gio.Settings(schema_id="org.blueman.general")
self.Config.connect('changed', self._on_settings_changed)
# Set the correct sorting
self._on_settings_changed(self.Config, "sort-by")
self._on_settings_changed(self.Config, "sort-type")

self.connect("query-tooltip", self.tooltip_query)
self.tooltip_row: Optional[Gtk.TreePath] = None
Expand All @@ -102,20 +98,11 @@ def __init__(self, inst: "Blueman", adapter: Optional[str] = None) -> None:
self.set_search_equal_func(self.search_func)
self.filter.set_visible_func(self.filter_func)

def _on_settings_changed(self, settings: Gio.Settings, key: str) -> None:
if key in ('sort-by', 'sort-order'):
sort_by = settings['sort-by']
sort_order = settings['sort-order']

if sort_order == 'ascending':
sort_type = Gtk.SortType.ASCENDING
else:
sort_type = Gtk.SortType.DESCENDING

column_id = self.ids.get(sort_by)

if column_id:
self.liststore.set_sort_column_id(column_id, sort_type)
# Setup sorting on paired. Function will hanlde further sorting
sort_column_id = self.ids.get("paired")
assert sort_column_id is not None
self.liststore.set_sort_column_id(sort_column_id, Gtk.SortType.ASCENDING)
self.liststore.set_sort_func(sort_column_id, self.sort_func)

def on_icon_theme_changed(self, _icon_them: Gtk.IconTheme) -> None:
for row in self.liststore:
Expand Down Expand Up @@ -148,6 +135,26 @@ def filter_func(self, _model: Gtk.TreeModel, tree_iter: Gtk.TreeIter, _data: Any
else:
return True

def sort_func(self, _model: Gtk.TreeModel, rowa_iter: Gtk.TreeIter, rowb_iter: Gtk.TreeIter, _data: Any) -> int:
if not self.liststore.iter_is_valid(rowa_iter) or not self.liststore.iter_is_valid(rowb_iter):
return 0

rowa = self.get(rowa_iter, "alias", "connected", "paired")
rowb = self.get(rowb_iter, "alias", "connected", "paired")

if rowa["alias"] is None or rowb["alias"] is None:
return 0

key1 = (rowa["paired"], not rowa["connected"], rowa["alias"])
key2 = (rowb["paired"], not rowb["connected"], rowb["alias"])

if key1 > key2:
return 1
elif key1 < key2:
return -1
else:
return 0

def drag_recv(self, _widget: Gtk.Widget, context: Gdk.DragContext, x: int, y: int, selection: Gtk.SelectionData,
_info: int, time: int) -> None:

Expand Down
52 changes: 3 additions & 49 deletions blueman/gui/manager/ManagerMenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,6 @@ def __init__(self, blueman: "Blueman"):
item_unnamed = blueman.builder.get_widget("hide_unnamed_item", Gtk.CheckMenuItem)
self.blueman.Config.bind("hide-unnamed", item_unnamed, "active", Gio.SettingsBindFlags.DEFAULT)

self._sort_alias_item = blueman.builder.get_widget("sort_name_item", Gtk.CheckMenuItem)
self._sort_timestamp_item = blueman.builder.get_widget("sort_added_item", Gtk.CheckMenuItem)

sort_config = self.Config['sort-by']
if sort_config == "alias":
self._sort_alias_item.props.active = True
else:
self._sort_timestamp_item.props.active = True

self._sort_type_item = blueman.builder.get_widget("sort_descending_item", Gtk.CheckMenuItem)

if self.Config['sort-order'] == "ascending":
self._sort_type_item.props.active = False
else:
self._sort_type_item.props.active = True

item_plugins = blueman.builder.get_widget("plugins_item", Gtk.ImageMenuItem)
item_plugins.connect('activate', self._on_plugin_dialog_activate)

Expand Down Expand Up @@ -98,39 +82,9 @@ def __init__(self, blueman: "Blueman"):
self.device_menu: Optional[ManagerDeviceMenu] = None

self.Config.connect("changed", self._on_settings_changed)
self._sort_alias_item.connect("activate", self._on_sorting_changed, "alias")
self._sort_timestamp_item.connect("activate", self._on_sorting_changed, "timestamp")
self._sort_type_item.connect("activate", self._on_sorting_changed, "sort-type")

def _on_sorting_changed(self, btn: Gtk.CheckMenuItem, sort_opt: str) -> None:
if sort_opt == 'alias' and btn.props.active:
self.Config['sort-by'] = "alias"
elif sort_opt == "timestamp" and btn.props.active:
self.Config['sort-by'] = "timestamp"
elif sort_opt == 'sort-type':
# FIXME bind widget to gsetting
if btn.props.active:
self.Config["sort-order"] = "descending"
else:
self.Config["sort-order"] = "ascending"

def _on_settings_changed(self, settings: Gio.Settings, key: str) -> None:
value = settings[key]
if key == 'sort-by':
if value == "alias":
if not self._sort_alias_item.props.active:
self._sort_alias_item.props.active = True
elif value == "timestamp":
if not self._sort_timestamp_item.props.active:
self._sort_timestamp_item.props.active = True
elif key == "sort-type":
if value == "ascending":
if not self._sort_type_item.props.active:
self._sort_type_item.props.active = True
else:
if not self._sort_type_item.props.active:
self._sort_type_item.props.active = False
elif key == "hide-unnamed":

def _on_settings_changed(self, _settings: Gio.Settings, key: str) -> None:
if key == "hide-unnamed":
logging.debug("refilter")
self.blueman.List.filter.refilter()

Expand Down
17 changes: 0 additions & 17 deletions data/org.blueman.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,6 @@
<summary>Show Manager's StatusBar</summary>
<description>Show or hide the Manager's StatusBar</description>
</key>
<key type="s" name="sort-by">
<choices>
<choice value="timestamp"/>
<choice value="alias"/>
</choices>
<default>"timestamp"</default>
<summary>Sort device list</summary>
<description>Sort the device list by column, possible values are timestamp and alias</description>
</key>
<key type="s" name="sort-order">
<choices>
<choice value="ascending"/>
<choice value="descending"/>
</choices>
<default>"ascending"</default>
<summary>Sort ascending or descending</summary>
</key>
<key type="b" name="hide-unnamed">
<summary>Hide devices with no name</summary>
<default>true</default>
Expand Down
47 changes: 0 additions & 47 deletions data/ui/manager-main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -167,53 +167,6 @@
<property name="can-focus">False</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="sort_by_item">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">S_ort By</property>
<property name="use-underline">True</property>
<child type="submenu">
<object class="GtkMenu">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkRadioMenuItem" id="sort_name_item">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">_Name</property>
<property name="use-underline">True</property>
<property name="draw-as-radio">True</property>
</object>
</child>
<child>
<object class="GtkRadioMenuItem" id="sort_added_item">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">_Added</property>
<property name="use-underline">True</property>
<property name="draw-as-radio">True</property>
<property name="group">sort_name_item</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="sort_descending_item">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">_Descending</property>
<property name="use-underline">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="plugins_item">
<property name="label" translatable="yes">_Plugins</property>
Expand Down

0 comments on commit cce37cc

Please sign in to comment.