Skip to content

Commit

Permalink
experimental support for "reverse DNS" .desktop file names #64
Browse files Browse the repository at this point in the history
  • Loading branch information
nwg-piotr committed Jul 26, 2021
1 parent ad07191 commit f713383
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
6 changes: 3 additions & 3 deletions nwg_panel/modules/scratchpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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):
Expand Down Expand Up @@ -36,9 +36,9 @@ def check_scratchpad(self, tree):
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"

Expand Down
45 changes: 24 additions & 21 deletions nwg_panel/modules/sway_taskbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -125,26 +125,29 @@ def __init__(self, con, settings, position, icons_path, floating=False):
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)

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)
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)

if con.name:
check_key(settings, "show-app-name", True)
Expand Down
4 changes: 2 additions & 2 deletions nwg_panel/modules/sway_workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -130,7 +130,7 @@ def refresh(self):

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"):
Expand Down
12 changes: 11 additions & 1 deletion nwg_panel/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_app_dirs():
return desktop_dirs


def get_icon(app_name):
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".
Expand All @@ -83,6 +83,7 @@ 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):
Expand All @@ -93,6 +94,15 @@ def get_icon(app_name):
for line in content.splitlines():
if line.upper().startswith("ICON"):
return line.split("=")[1]
# Support for "reverse DNS" .desktop file names
# See: https://github.com/nwg-piotr/nwg-panel/issues/64
elif os.path.isdir(d):
for file in os.listdir(d):
if app_name in file.split("."):
content = load_text_file(os.path.join(d, file))
for line in content.splitlines():
if line.upper().startswith("ICON"):
return line.split("=")[1]


def local_dir():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def read(f_name):

setup(
name='nwg-panel',
version='0.4.0',
version='0.4.1',
description='GTK3-based panel for sway window manager',
packages=find_packages(),
include_package_data=True,
Expand Down

0 comments on commit f713383

Please sign in to comment.