fixed
This commit is contained in:
1 parent
f5eaa6596d
commit
149cd94a44
6 files changed
+75
-118
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
from typing import TypedDict
|
||||
|
||||
from fabric.hyprland.widgets import get_hyprland_connection
|
||||
@@ -7,7 +8,7 @@ from fabric.widgets.box import Box
|
||||
from fabric.widgets.button import Button
|
||||
from fabric.widgets.image import Image
|
||||
from gi.repository import GdkPixbuf, GLib, Gtk
|
||||
from snippets import IconResolver
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class PagerClient(TypedDict):
|
||||
@@ -29,7 +30,6 @@ class TaskBar(Box):
|
||||
self.connection = get_hyprland_connection()
|
||||
self.icon_size = icon_size
|
||||
self.icon_theme = Gtk.IconTheme.get_default()
|
||||
self.icon_resolver = IconResolver()
|
||||
|
||||
self.set_visible(False)
|
||||
self._icon_cache = {}
|
||||
@@ -61,9 +61,7 @@ class TaskBar(Box):
|
||||
|
||||
button = Button(image=icon, tooltip_text=client["title"])
|
||||
if client["address"] == active_window_address:
|
||||
button.set_style(
|
||||
"background-color: @surfaceVariant; border-radius:100px;"
|
||||
)
|
||||
button.add_style_class("activated")
|
||||
button.connect(
|
||||
"button-press-event", self.on_icon_click, client["address"]
|
||||
)
|
||||
@@ -95,18 +93,47 @@ class TaskBar(Box):
|
||||
def bake_window_icon(
|
||||
self, window_class: str, fallback_icon: str = "image-missing"
|
||||
) -> Image:
|
||||
icon_name = self.icon_resolver.get_icon_name(window_class) or fallback_icon
|
||||
if icon_name not in self._icon_cache:
|
||||
pixbuf = self.load_icon(icon_name)
|
||||
self._icon_cache[icon_name] = pixbuf
|
||||
return Image(pixbuf=self._icon_cache[icon_name], size=self.icon_size)
|
||||
icon_name = self.get_icon_from_desktop_entry(window_class)
|
||||
|
||||
def load_icon(self, icon_name: str) -> GdkPixbuf.Pixbuf:
|
||||
if icon_name:
|
||||
pixbuf = self.load_icon(icon_name)
|
||||
else:
|
||||
pixbuf = self.load_icon(window_class, fallback_icon)
|
||||
|
||||
return Image(pixbuf=pixbuf, size=self.icon_size)
|
||||
|
||||
def get_icon_from_desktop_entry(self, window_class: str) -> str:
|
||||
for data_dir in GLib.get_system_data_dirs():
|
||||
applications_dir = os.path.join(data_dir, "applications")
|
||||
|
||||
if os.path.isdir(applications_dir):
|
||||
for desktop_file in os.listdir(applications_dir):
|
||||
if desktop_file.endswith(".desktop"):
|
||||
file_path = os.path.join(applications_dir, desktop_file)
|
||||
try:
|
||||
with open(file_path, "r") as f:
|
||||
app_name = None
|
||||
icon_name = None
|
||||
for line in f:
|
||||
if line.startswith("Name="):
|
||||
app_name = line.split("=", 2)[1].strip().lower()
|
||||
elif line.startswith("Icon=") and app_name:
|
||||
icon_name = line.split("=", 2)[1].strip()
|
||||
if window_class in app_name:
|
||||
return icon_name
|
||||
except Exception as e:
|
||||
logger.error(f"Error reading {file_path}: {e}")
|
||||
return None
|
||||
|
||||
def load_icon(
|
||||
self, icon_name: str, fallback_icon: str = "image-missing"
|
||||
) -> GdkPixbuf.Pixbuf:
|
||||
try:
|
||||
return self.icon_theme.load_icon(
|
||||
pixbuf = self.icon_theme.load_icon(
|
||||
icon_name, self.icon_size, Gtk.IconLookupFlags.FORCE_SIZE
|
||||
)
|
||||
except Exception:
|
||||
return self.icon_theme.load_icon(
|
||||
"image-missing", self.icon_size, Gtk.IconLookupFlags.FORCE_SIZE
|
||||
pixbuf = self.icon_theme.load_icon(
|
||||
fallback_icon, self.icon_size, Gtk.IconLookupFlags.FORCE_SIZE
|
||||
)
|
||||
return pixbuf
|
||||
Reference in new issue
Block a user