refactor(bluetooth): resolve device icons from GTK icon theme
_check_icon_to_type now queries Gtk.IconTheme for the icon name (with -symbolic suffix) before falling back to a hardcoded mapping. _get_icon_for_device simplified since type property now returns GTK icon names directly.
This commit is contained in:
2 files changed
+25
-22
No files matched your search
+24
-11
@@ -2,7 +2,7 @@ from collections.abc import Callable
|
|||||||
from typing import Any, Concatenate, ParamSpec
|
from typing import Any, Concatenate, ParamSpec
|
||||||
|
|
||||||
from fabric.core.service import Property, Service, Signal
|
from fabric.core.service import Property, Service, Signal
|
||||||
from fabric.utils import Gio, GLib, logger
|
from fabric.utils import Gio, GLib, Gtk, logger
|
||||||
|
|
||||||
P = ParamSpec("P")
|
P = ParamSpec("P")
|
||||||
|
|
||||||
@@ -784,14 +784,27 @@ def _path_to_addr(object_path: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def _icon_to_type(icon: str) -> str:
|
def _icon_to_type(icon: str) -> str:
|
||||||
|
"""Resolve a Bluez icon name to a GTK symbolic icon via the current theme.
|
||||||
|
|
||||||
|
Checks the icon theme for the icon (with ``-symbolic`` suffix) and falls
|
||||||
|
back to a hardcoded mapping when the theme doesn't provide it.
|
||||||
|
"""
|
||||||
|
if icon:
|
||||||
|
icon_theme = Gtk.IconTheme.get_default()
|
||||||
|
symbolic = f"{icon}-symbolic"
|
||||||
|
if icon_theme.has_icon(symbolic):
|
||||||
|
return symbolic
|
||||||
|
if icon_theme.has_icon(icon):
|
||||||
|
return icon
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"audio-headset": "Headset",
|
"audio-headset": "audio-headset-symbolic",
|
||||||
"audio-headphones": "Headphones",
|
"audio-headphones": "audio-headphones-symbolic",
|
||||||
"audio-speakers": "Speaker",
|
"audio-speakers": "audio-speakers-symbolic",
|
||||||
"audio-card": "Speaker",
|
"audio-card": "audio-speakers-symbolic",
|
||||||
"input-keyboard": "Keyboard",
|
"input-keyboard": "input-keyboard-symbolic",
|
||||||
"input-mouse": "Mouse",
|
"input-mouse": "input-mouse-symbolic",
|
||||||
"input-gaming": "Joypad",
|
"input-gaming": "input-gaming-symbolic",
|
||||||
"phone": "Phone",
|
"phone": "phone-symbolic",
|
||||||
"printer": "Printer",
|
"printer": "printer-symbolic",
|
||||||
}.get(icon, icon)
|
}.get(icon, "bluetooth-symbolic")
|
||||||
@@ -143,19 +143,9 @@ class BluetoothDeviceSlot(Box):
|
|||||||
self.device.emit("changed")
|
self.device.emit("changed")
|
||||||
|
|
||||||
def _get_icon_for_device(self, device) -> str:
|
def _get_icon_for_device(self, device) -> str:
|
||||||
dtype = getattr(device, "type", None) or getattr(
|
return getattr(device, "type", None) or getattr(
|
||||||
device, "icon_name", "bluetooth"
|
device, "icon_name", "bluetooth"
|
||||||
)
|
)
|
||||||
return {
|
|
||||||
"Headset": "audio-headset-symbolic",
|
|
||||||
"Headphones": "audio-headphones-symbolic",
|
|
||||||
"Speaker": "audio-speakers-symbolic",
|
|
||||||
"Keyboard": "input-keyboard-symbolic",
|
|
||||||
"Mouse": "input-mouse-symbolic",
|
|
||||||
"Joypad": "input-gaming-symbolic",
|
|
||||||
"Phone": "phone-symbolic",
|
|
||||||
"Printer": "printer-symbolic",
|
|
||||||
}.get(dtype, dtype + "-symbolic" if not dtype.endswith("-symbolic") else dtype)
|
|
||||||
|
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
"""Clean up device signal connections to prevent memory leaks."""
|
"""Clean up device signal connections to prevent memory leaks."""
|
||||||
|
|||||||
Reference in new issue
Block a user