Skip to content

Commit

Permalink
ManagerMenu: Use Gio.Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
infirit committed Dec 10, 2022
1 parent 33e7554 commit a224912
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 159 deletions.
11 changes: 5 additions & 6 deletions blueman/gui/manager/ManagerDeviceList.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, inst: "Blueman", adapter: Optional[str] = None) -> None:
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._on_settings_changed(self.Config, "sort-descending")

self.connect("query-tooltip", self.tooltip_query)
self.tooltip_row: Optional[Gtk.TreePath] = None
Expand All @@ -103,14 +103,13 @@ def __init__(self, inst: "Blueman", adapter: Optional[str] = None) -> None:
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'):
if key in ('sort-by', 'sort-descending'):
sort_by = settings['sort-by']
sort_order = settings['sort-order']

if sort_order == 'ascending':
sort_type = Gtk.SortType.ASCENDING
else:
if settings['sort-descending']:
sort_type = Gtk.SortType.DESCENDING
else:
sort_type = Gtk.SortType.ASCENDING

column_id = self.ids.get(sort_by)

Expand Down
205 changes: 63 additions & 142 deletions blueman/gui/manager/ManagerMenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,143 +32,63 @@ def __init__(self, blueman: "Blueman"):
self.Search = None

self.item_adapter = self.blueman.builder.get_widget("item_adapter", Gtk.MenuItem)
self.adapter_menu = self.blueman.builder.get_widget("adapter_menu", Gtk.Menu)
self.item_device = self.blueman.builder.get_widget("item_device", Gtk.MenuItem)

self.item_view = self.blueman.builder.get_widget("item_view", Gtk.MenuItem)
self.item_help = self.blueman.builder.get_widget("item_help", Gtk.MenuItem)
report_action = Gio.SimpleAction.new("report", None)
report_action.connect("activate", self._simple_actions)
self.blueman.add_action(report_action)

help_menu = Gtk.Menu()

self.item_help.set_submenu(help_menu)
help_menu.show()
assert self.blueman.window is not None
window = self.blueman.window.get_toplevel()
assert isinstance(window, Gtk.Window)

report_item = create_menuitem(_("_Report a Problem"), "dialog-warning-symbolic")
report_item.show()
help_menu.append(report_item)
help_action = Gio.SimpleAction.new("help", None)
help_action.connect("activate", self._simple_actions)
self.blueman.add_action(help_action)

report_item.connect("activate", lambda x: launch(f"xdg-open {WEBSITE}/issues"))
show_toolbar_action = self.blueman.Config.create_action("show-toolbar")
self.blueman.add_action(show_toolbar_action)

sep = Gtk.SeparatorMenuItem()
sep.show()
help_menu.append(sep)
show_statusbar_action = self.blueman.Config.create_action("show-statusbar")
self.blueman.add_action(show_statusbar_action)

help_item = create_menuitem(_("_Help"), "help-about-symbolic")
help_item.show()
help_menu.append(help_item)
assert self.blueman.window is not None
widget = self.blueman.window.get_toplevel()
assert isinstance(widget, Gtk.Window)
window = widget
help_item.connect("activate", lambda x: show_about_dialog('Blueman ' + _('Device Manager'), parent=window))

view_menu = Gtk.Menu()
self.item_view.set_submenu(view_menu)
view_menu.show()

item_toolbar = Gtk.CheckMenuItem.new_with_mnemonic(_("Show _Toolbar"))
item_toolbar.show()
view_menu.append(item_toolbar)
self.blueman.Config.bind("show-toolbar", item_toolbar, "active", Gio.SettingsBindFlags.DEFAULT)

item_statusbar = Gtk.CheckMenuItem.new_with_mnemonic(_("Show _Statusbar"))
item_statusbar.show()
view_menu.append(item_statusbar)
self.blueman.Config.bind("show-statusbar", item_statusbar, "active", Gio.SettingsBindFlags.DEFAULT)

item_unnamed = Gtk.CheckMenuItem.new_with_mnemonic(_("Hide _unnamed devices"))
item_unnamed.show()
view_menu.append(item_unnamed)
self.blueman.Config.bind("hide-unnamed", item_unnamed, "active", Gio.SettingsBindFlags.DEFAULT)

item_services: Gtk.MenuItem = Gtk.SeparatorMenuItem()
view_menu.append(item_services)
item_services.show()

sorting_group: Sequence[Gtk.RadioMenuItem] = []
item_sort = Gtk.MenuItem.new_with_mnemonic(_("S_ort By"))
view_menu.append(item_sort)
item_sort.show()

sorting_menu = Gtk.Menu()
item_sort.set_submenu(sorting_menu)

self._sort_alias_item = Gtk.RadioMenuItem.new_with_mnemonic(sorting_group, _("_Name"))
self._sort_alias_item.show()
sorting_group = self._sort_alias_item.get_group()
sorting_menu.append(self._sort_alias_item)

self._sort_timestamp_item = Gtk.RadioMenuItem.new_with_mnemonic(sorting_group, _("_Added"))
self._sort_timestamp_item.show()
sorting_menu.append(self._sort_timestamp_item)
hide_unnamed_action = self.blueman.Config.create_action("hide-unnamed")
self.blueman.add_action(hide_unnamed_action)

sort_config = self.Config['sort-by']
self._sort_alias_item = self.blueman.builder.get_widget("sort_name", Gtk.RadioMenuItem)
self._sort_timestamp_item = self.blueman.builder.get_widget("sort_added", Gtk.RadioMenuItem)

if sort_config == "alias":
self._sort_alias_item.props.active = True
else:
self._sort_timestamp_item.props.active = True

sort_sep = Gtk.SeparatorMenuItem()
sort_sep.show()
sorting_menu.append(sort_sep)
sort_type_action = self.blueman.Config.create_action("sort-descending")
self.blueman.add_action(sort_type_action)

self._sort_type_item = Gtk.CheckMenuItem.new_with_mnemonic(_("_Descending"))
self._sort_type_item.show()
sorting_menu.append(self._sort_type_item)
plugins_action = Gio.SimpleAction.new("plugins", None)
self.blueman.add_action(plugins_action)
plugins_action.connect("activate", self._on_plugin_dialog_activate)

services_action = Gio.SimpleAction.new("services", None)
self.blueman.add_action(services_action)
services_action.connect("activate", self._simple_actions)

search_action = Gio.SimpleAction.new("search", None)
self.blueman.add_action(search_action)
search_action.connect("activate", self._simple_actions)
self.Search = self.blueman.builder.get_widget("adapter_search", Gtk.ImageMenuItem)

preferences_action = Gio.SimpleAction.new("preferences", None)
self.blueman.add_action(preferences_action)
preferences_action.connect("activate", self._simple_actions)

exit_action = Gio.SimpleAction.new("exit", None)
self.blueman.add_action(exit_action)
exit_action.connect("activate", self._simple_actions)

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

sep = Gtk.SeparatorMenuItem()
sep.show()
view_menu.append(sep)

item_plugins = create_menuitem(_("_Plugins"), 'application-x-addon-symbolic')
item_plugins.show()
view_menu.append(item_plugins)
item_plugins.connect('activate', self._on_plugin_dialog_activate)

item_services = create_menuitem(_("_Local Services") + "…", "document-properties-symbolic")
item_services.connect('activate', lambda *args: launch("blueman-services", name=_("Service Preferences")))
view_menu.append(item_services)
item_services.show()

adapter_menu = Gtk.Menu()
self.item_adapter.set_submenu(adapter_menu)
self.item_adapter.props.sensitive = False

search_item = create_menuitem(_("_Search"), "edit-find-symbolic")
search_item.connect("activate", lambda x: self.blueman.inquiry())
search_item.show()
adapter_menu.prepend(search_item)
self.Search = search_item

sep = Gtk.SeparatorMenuItem()
sep.show()
adapter_menu.append(sep)

sep = Gtk.SeparatorMenuItem()
sep.show()
adapter_menu.append(sep)

adapter_settings = create_menuitem(_("_Preferences"), "document-properties-symbolic")
adapter_settings.connect("activate", lambda x: self.blueman.adapter_properties())
adapter_settings.show()
adapter_menu.append(adapter_settings)

sep = Gtk.SeparatorMenuItem()
sep.show()
adapter_menu.append(sep)

exit_item = create_menuitem(_("_Exit"), "application-exit-symbolic")
exit_item.connect("activate", lambda x: self.blueman.quit())
exit_item.show()
adapter_menu.append(exit_item)

self.item_adapter.show()
self.item_view.show()
self.item_help.show()
self.item_device.show()
self.item_device.props.sensitive = False

Expand All @@ -186,19 +106,12 @@ def __init__(self, blueman: "Blueman"):
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]
Expand All @@ -209,13 +122,6 @@ def _on_settings_changed(self, settings: Gio.Settings, key: str) -> None:
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":
logging.debug("refilter")
self.blueman.List.filter.refilter()
Expand Down Expand Up @@ -258,8 +164,6 @@ def on_adapter_selected(self, menuitem: Gtk.CheckMenuItem, adapter_path: str) ->

def on_adapter_added(self, _manager: Optional[Manager], adapter_path: str) -> None:
adapter = Adapter(obj_path=adapter_path)
menu = self.item_adapter.get_submenu()
assert isinstance(menu, Gtk.Menu)

item = Gtk.RadioMenuItem.new_with_label(self._adapters_group, adapter.get_name())
item.show()
Expand All @@ -268,7 +172,7 @@ def on_adapter_added(self, _manager: Optional[Manager], adapter_path: str) -> No
self._itemhandler = item.connect("activate", self.on_adapter_selected, adapter_path)
self._adapterhandler = adapter.connect_signal("property-changed", self.on_adapter_property_changed)

menu.insert(item, self._insert_adapter_item_pos)
self.adapter_menu.insert(item, self._insert_adapter_item_pos)
self._insert_adapter_item_pos += 1

self.adapter_items[adapter_path] = (item, adapter)
Expand All @@ -282,19 +186,36 @@ def on_adapter_added(self, _manager: Optional[Manager], adapter_path: str) -> No

def on_adapter_removed(self, _manager: Manager, adapter_path: str) -> None:
item, adapter = self.adapter_items.pop(adapter_path)
menu = self.item_adapter.get_submenu()
assert isinstance(menu, Gtk.Menu)

item.disconnect(self._itemhandler)
adapter.disconnect(self._adapterhandler)

menu.remove(item)
self.adapter_menu.remove(item)
self._insert_adapter_item_pos -= 1

if len(self.adapter_items) == 0:
self.item_adapter.props.sensitive = False

def _on_plugin_dialog_activate(self, _item: Gtk.MenuItem) -> None:
def _on_plugin_dialog_activate(self, _action: Gio.Action, _value: Any) -> None:
def cb(_proxy: Gio.DBusProxy, _res: Any, _userdata: Any) -> None:
pass

self.blueman.Applet.OpenPluginDialog(result_handler=cb)

def _simple_actions(self, action: Gio.Action, _val: Optional[Any]) -> None:
name = action.get_name()
if name == "report":
launch(f"xdg-open {WEBSITE}/issues")
elif name == "services":
launch("blueman-services", name=_("Service Preferences"))
elif name == "search":
self.blueman.inquiry()
elif name == "preferences":
self.blueman.adapter_properties()
elif name == "exit":
self.blueman.quit()
elif name == "help":
assert self.blueman.window is not None
window = self.blueman.window.get_toplevel()
assert isinstance(window, Gtk.Window)
show_about_dialog('Blueman ' + _('Device Manager'), parent=window)
10 changes: 3 additions & 7 deletions data/org.blueman.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@
<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 type="b" name="sort-descending">
<default>false</default>
<summary>Sort descending</summary>
</key>
<key type="b" name="hide-unnamed">
<summary>Hide devices with no name</summary>
Expand Down
Loading

0 comments on commit a224912

Please sign in to comment.