This commit is contained in:
sankalp committed 2024-11-15 18:26:45 +05:45
1 parent 8cdd6d9797
commit 733819470e
29 files changed
-154

No files matched your search

-54
View File
@@ -1,54 +0,0 @@
import gi
from fabric.widgets.box import Box
from fabric.widgets.button import Button
from fabric.widgets.image import Image
from .icon_resolver import IconResolver
gi.require_version("Glace", "0.1")
from gi.repository import Glace
class OpenAppsBar(Box):
def __init__(self):
self.client_buttons = {}
super().__init__(spacing=10)
self.icon_resolver = IconResolver()
self._manager = Glace.Manager()
self._manager.connect("client-added", self.on_client_added)
def on_active_window(self, _, event):
print(event.data)
print(self.client_buttons)
# client_buttons[]
def on_client_added(self, _, client: Glace.Client):
client_image = Image()
client_button = Button(
name="panel-button",
image=client_image,
on_button_press_event=lambda *_: client.activate(),
)
self.client_buttons[client.get_id()] = client_button
client.connect(
"notify::app-id",
lambda *_: [
client_image.set_from_pixbuf(
self.icon_resolver.get_icon_pixbuf(client.get_app_id(), 24)
),
print(client.get_id()),
],
)
client.connect(
"notify::activated",
lambda *_: (
client_button.add_style_class("activated")
if client.get_activated()
else client_button.remove_style_class("activated")
),
)
client.bind_property("title", client_button, "tooltip-text", 0)
client.connect("close", lambda *_: self.remove(client_button))
self.add(client_button)
-100
View File
@@ -1,100 +0,0 @@
import json
import os
import re
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GLib, Gtk
from loguru import logger
# TODO WIP
# Idea: nearest string matching algorithm
# if already exists: retrieve from json
# if found: stor in json: app_id -> icon_name
# if not found: store in json -> misisng-icon
CACHE_DIR = str(GLib.get_user_cache_dir()) + "/fabric"
ICON_CACHE_FILE = CACHE_DIR + "/icons.json"
if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)
class IconResolver:
def __init__(self, default_applicaiton_icon: str = "application-x-symbolic"):
if os.path.exists(ICON_CACHE_FILE):
f = open(ICON_CACHE_FILE)
try:
self._icon_dict = json.load(f)
except json.JSONDecodeError:
logger.info("[ICONS] Cache file does not exist or is corrupted")
f.close()
else:
self._icon_dict = {}
self.default_applicaiton_icon = default_applicaiton_icon
def get_icon_name(self, app_id: str):
if app_id in self._icon_dict:
return self._icon_dict[app_id]
new_icon = self._compositor_find_icon(app_id)
logger.info(
f"[ICONS] found new icon: '{new_icon}' for app id: '{app_id}', storing..."
)
self._store_new_icon(app_id, new_icon)
return new_icon
def get_icon_pixbuf(self, app_id: str, size: int = 16):
return Gtk.IconTheme.get_default().load_icon(
self.get_icon_name(app_id),
size,
Gtk.IconLookupFlags.FORCE_SIZE,
)
def _store_new_icon(self, app_id: str, icon: str):
self._icon_dict[app_id] = icon
with open(ICON_CACHE_FILE, "w") as f:
json.dump(self._icon_dict, f)
f.close()
def _get_icon_from_desktop_file(self, desktop_file_path: str):
# TODO: get icon in the [Desktop Entry] section only
with open(desktop_file_path) as f:
for line in f.readlines():
if "Icon=" in line:
return "".join(line[5:].split())
return self.default_applicaiton_icon
def _get_desktop_file(self, app_id: str) -> str | None:
data_dirs = GLib.get_system_data_dirs()
for data_dir in data_dirs:
data_dir = data_dir + "/applications/"
if os.path.exists(data_dir):
# Do name resolving here
files = os.listdir(data_dir)
matching = [
s for s in files if "".join(app_id.lower().split()) in s.lower()
]
if matching:
return data_dir + matching[0]
for word in list(filter(None, re.split(r"-|\.|_|\s", app_id))):
matching = [s for s in files if word.lower() in s.lower()]
if matching:
return data_dir + matching[0]
return None
def _compositor_find_icon(self, app_id: str):
if Gtk.IconTheme.get_default().has_icon(app_id):
return app_id
if Gtk.IconTheme.get_default().has_icon(app_id + "-desktop"):
return app_id + "-desktop"
desktop_file = self._get_desktop_file(app_id)
return (
self._get_icon_from_desktop_file(desktop_file)
if desktop_file
else self.default_applicaiton_icon
)