Merge pull request #65 from nwg-piotr/images
Map "reverse DNS"-style named .desktop file names to relevant icon names
This commit is contained in:
@@ -16,6 +16,7 @@ workspaces_list = []
|
||||
controls_list = []
|
||||
config_dir = ""
|
||||
app_dirs = []
|
||||
name2icon_dict = {}
|
||||
|
||||
commands = {
|
||||
"light": False,
|
||||
|
@@ -239,6 +239,7 @@ def main():
|
||||
save_string("-c {} -s {}".format(args.config, args.style), os.path.join(local_dir(), "args"))
|
||||
|
||||
common.app_dirs = get_app_dirs()
|
||||
common.name2icon_dict = map_odd_desktop_files()
|
||||
|
||||
config_file = os.path.join(common.config_dir, args.config)
|
||||
|
||||
|
@@ -6,7 +6,7 @@ import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
|
||||
from nwg_panel.tools import check_key, get_icon, update_image
|
||||
from nwg_panel.tools import check_key, get_icon_name, update_image
|
||||
|
||||
|
||||
class Scratchpad(Gtk.Box):
|
||||
@@ -36,9 +36,9 @@ class Scratchpad(Gtk.Box):
|
||||
if aid:
|
||||
pid = node.pid
|
||||
if node.app_id:
|
||||
icon = get_icon(node.app_id)
|
||||
icon = get_icon_name(node.app_id)
|
||||
elif node.window_class:
|
||||
icon = get_icon(node.window_class)
|
||||
icon = get_icon_name(node.window_class)
|
||||
else:
|
||||
icon = "icon-missing"
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
import os
|
||||
from gi.repository import Gtk, Gdk, GdkPixbuf
|
||||
|
||||
from nwg_panel.tools import check_key, get_icon, update_image, load_autotiling, get_config_dir
|
||||
from nwg_panel.tools import check_key, get_icon_name, update_image, load_autotiling, get_config_dir
|
||||
import nwg_panel.common
|
||||
|
||||
|
||||
@@ -125,26 +125,29 @@ class WindowBox(Gtk.EventBox):
|
||||
if settings["show-app-icon"]:
|
||||
name = con.app_id if con.app_id else con.window_class
|
||||
|
||||
icon_from_desktop = get_icon(name)
|
||||
if icon_from_desktop:
|
||||
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(
|
||||
".svg") and not icon_from_desktop.endswith(".png"):
|
||||
image = Gtk.Image()
|
||||
update_image(image, icon_from_desktop, settings["image-size"], icons_path)
|
||||
else:
|
||||
try:
|
||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_from_desktop, settings["image-size"],
|
||||
settings["image-size"])
|
||||
except:
|
||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
|
||||
os.path.join(get_config_dir(), "icons_light/icon-missing.svg"), settings["image-size"], settings["image-size"])
|
||||
image = Gtk.Image.new_from_pixbuf(pixbuf)
|
||||
image = Gtk.Image()
|
||||
icon_theme = Gtk.IconTheme.get_default()
|
||||
try:
|
||||
# This should work if your icon theme provides the icon, or if it's placed in /usr/share/pixmaps
|
||||
pixbuf = icon_theme.load_icon(name, settings["image-size"], Gtk.IconLookupFlags.FORCE_SIZE)
|
||||
image.set_from_pixbuf(pixbuf)
|
||||
except:
|
||||
# If the above failed, let's search .desktop files to find the icon name
|
||||
icon_from_desktop = get_icon_name(name)
|
||||
if icon_from_desktop:
|
||||
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(
|
||||
".svg") and not icon_from_desktop.endswith(".png"):
|
||||
update_image(image, icon_from_desktop, settings["image-size"], icons_path)
|
||||
else:
|
||||
try:
|
||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_from_desktop, settings["image-size"],
|
||||
settings["image-size"])
|
||||
except:
|
||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
|
||||
os.path.join(get_config_dir(), "icons_light/icon-missing.svg"), settings["image-size"], settings["image-size"])
|
||||
image.set_from_pixbuf(pixbuf)
|
||||
|
||||
self.box.pack_start(image, False, False, 4)
|
||||
else:
|
||||
image = Gtk.Image()
|
||||
update_image(image, name, settings["image-size"], icons_path)
|
||||
self.box.pack_start(image, False, False, 4)
|
||||
self.box.pack_start(image, False, False, 4)
|
||||
|
||||
if con.name:
|
||||
check_key(settings, "show-app-name", True)
|
||||
|
@@ -3,7 +3,7 @@
|
||||
from gi.repository import Gtk, GdkPixbuf
|
||||
|
||||
import nwg_panel.common
|
||||
from nwg_panel.tools import check_key, get_icon, update_image,load_autotiling
|
||||
from nwg_panel.tools import check_key, get_icon_name, update_image,load_autotiling
|
||||
|
||||
|
||||
class SwayWorkspaces(Gtk.Box):
|
||||
@@ -130,7 +130,7 @@ class SwayWorkspaces(Gtk.Box):
|
||||
|
||||
def update_icon(self, win_id, win_name):
|
||||
if win_id and win_name:
|
||||
icon_from_desktop = get_icon(win_id)
|
||||
icon_from_desktop = get_icon_name(win_id)
|
||||
if icon_from_desktop:
|
||||
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(
|
||||
".svg") and not icon_from_desktop.endswith(".png"):
|
||||
|
@@ -8,6 +8,8 @@ import stat
|
||||
|
||||
import gi
|
||||
|
||||
import nwg_panel.common
|
||||
|
||||
gi.require_version('GdkPixbuf', '2.0')
|
||||
gi.require_version('Gtk', '3.0')
|
||||
gi.require_version('Gdk', '3.0')
|
||||
@@ -74,7 +76,25 @@ def get_app_dirs():
|
||||
return desktop_dirs
|
||||
|
||||
|
||||
def get_icon(app_name):
|
||||
def map_odd_desktop_files():
|
||||
name2icon_dict = {}
|
||||
for d in nwg_panel.common.app_dirs:
|
||||
if os.path.exists(d):
|
||||
for path in os.listdir(d):
|
||||
if os.path.isfile(os.path.join(d, path)):
|
||||
if path.count(".") > 1:
|
||||
content = load_text_file(os.path.join(d, path))
|
||||
for line in content.splitlines():
|
||||
if line.startswith("[") and not line == "[Desktop Entry]":
|
||||
break
|
||||
if line.upper().startswith("ICON="):
|
||||
icon = line.split("=")[1]
|
||||
name2icon_dict[path] = icon
|
||||
break
|
||||
return name2icon_dict
|
||||
|
||||
|
||||
def get_icon_name(app_name):
|
||||
if not app_name:
|
||||
return ""
|
||||
# GIMP returns "app_id": null and for some reason "class": "Gimp-2.10" instead of just "gimp".
|
||||
@@ -83,17 +103,24 @@ def get_icon(app_name):
|
||||
return "gimp"
|
||||
|
||||
for d in nwg_panel.common.app_dirs:
|
||||
# This will work if the .desktop file name is app_id.desktop or wm_class.desktop
|
||||
path = os.path.join(d, "{}.desktop".format(app_name))
|
||||
content = None
|
||||
if os.path.isfile(path):
|
||||
content = load_text_file(path)
|
||||
elif os.path.isfile(path.lower()):
|
||||
content = load_text_file(path.lower())
|
||||
content = load_text_file(path)
|
||||
if content:
|
||||
for line in content.splitlines():
|
||||
if line.upper().startswith("ICON"):
|
||||
return line.split("=")[1]
|
||||
|
||||
# Search .desktop files that use "reverse DNS"-style names
|
||||
# see: https://github.com/nwg-piotr/nwg-panel/issues/64
|
||||
for key in nwg_panel.common.name2icon_dict.keys():
|
||||
if app_name in key.split("."):
|
||||
return nwg_panel.common.name2icon_dict[key]
|
||||
|
||||
|
||||
def local_dir():
|
||||
local_dir = os.path.join(os.path.join(os.getenv("HOME"), ".local/share/nwg-panel"))
|
||||
|
Reference in New Issue
Block a user