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 = []
|
controls_list = []
|
||||||
config_dir = ""
|
config_dir = ""
|
||||||
app_dirs = []
|
app_dirs = []
|
||||||
|
name2icon_dict = {}
|
||||||
|
|
||||||
commands = {
|
commands = {
|
||||||
"light": False,
|
"light": False,
|
||||||
|
@@ -239,6 +239,7 @@ def main():
|
|||||||
save_string("-c {} -s {}".format(args.config, args.style), os.path.join(local_dir(), "args"))
|
save_string("-c {} -s {}".format(args.config, args.style), os.path.join(local_dir(), "args"))
|
||||||
|
|
||||||
common.app_dirs = get_app_dirs()
|
common.app_dirs = get_app_dirs()
|
||||||
|
common.name2icon_dict = map_odd_desktop_files()
|
||||||
|
|
||||||
config_file = os.path.join(common.config_dir, args.config)
|
config_file = os.path.join(common.config_dir, args.config)
|
||||||
|
|
||||||
|
@@ -6,7 +6,7 @@ import gi
|
|||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
from gi.repository import Gtk
|
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):
|
class Scratchpad(Gtk.Box):
|
||||||
@@ -36,9 +36,9 @@ class Scratchpad(Gtk.Box):
|
|||||||
if aid:
|
if aid:
|
||||||
pid = node.pid
|
pid = node.pid
|
||||||
if node.app_id:
|
if node.app_id:
|
||||||
icon = get_icon(node.app_id)
|
icon = get_icon_name(node.app_id)
|
||||||
elif node.window_class:
|
elif node.window_class:
|
||||||
icon = get_icon(node.window_class)
|
icon = get_icon_name(node.window_class)
|
||||||
else:
|
else:
|
||||||
icon = "icon-missing"
|
icon = "icon-missing"
|
||||||
|
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
import os
|
import os
|
||||||
from gi.repository import Gtk, Gdk, GdkPixbuf
|
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
|
import nwg_panel.common
|
||||||
|
|
||||||
|
|
||||||
@@ -125,26 +125,29 @@ class WindowBox(Gtk.EventBox):
|
|||||||
if settings["show-app-icon"]:
|
if settings["show-app-icon"]:
|
||||||
name = con.app_id if con.app_id else con.window_class
|
name = con.app_id if con.app_id else con.window_class
|
||||||
|
|
||||||
icon_from_desktop = get_icon(name)
|
image = Gtk.Image()
|
||||||
if icon_from_desktop:
|
icon_theme = Gtk.IconTheme.get_default()
|
||||||
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(
|
try:
|
||||||
".svg") and not icon_from_desktop.endswith(".png"):
|
# This should work if your icon theme provides the icon, or if it's placed in /usr/share/pixmaps
|
||||||
image = Gtk.Image()
|
pixbuf = icon_theme.load_icon(name, settings["image-size"], Gtk.IconLookupFlags.FORCE_SIZE)
|
||||||
update_image(image, icon_from_desktop, settings["image-size"], icons_path)
|
image.set_from_pixbuf(pixbuf)
|
||||||
else:
|
except:
|
||||||
try:
|
# If the above failed, let's search .desktop files to find the icon name
|
||||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_from_desktop, settings["image-size"],
|
icon_from_desktop = get_icon_name(name)
|
||||||
settings["image-size"])
|
if icon_from_desktop:
|
||||||
except:
|
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(
|
||||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
|
".svg") and not icon_from_desktop.endswith(".png"):
|
||||||
os.path.join(get_config_dir(), "icons_light/icon-missing.svg"), settings["image-size"], settings["image-size"])
|
update_image(image, icon_from_desktop, settings["image-size"], icons_path)
|
||||||
image = Gtk.Image.new_from_pixbuf(pixbuf)
|
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)
|
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)
|
|
||||||
|
|
||||||
if con.name:
|
if con.name:
|
||||||
check_key(settings, "show-app-name", True)
|
check_key(settings, "show-app-name", True)
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
from gi.repository import Gtk, GdkPixbuf
|
from gi.repository import Gtk, GdkPixbuf
|
||||||
|
|
||||||
import nwg_panel.common
|
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):
|
class SwayWorkspaces(Gtk.Box):
|
||||||
@@ -130,7 +130,7 @@ class SwayWorkspaces(Gtk.Box):
|
|||||||
|
|
||||||
def update_icon(self, win_id, win_name):
|
def update_icon(self, win_id, win_name):
|
||||||
if win_id and 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 icon_from_desktop:
|
||||||
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(
|
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(
|
||||||
".svg") and not icon_from_desktop.endswith(".png"):
|
".svg") and not icon_from_desktop.endswith(".png"):
|
||||||
|
@@ -8,6 +8,8 @@ import stat
|
|||||||
|
|
||||||
import gi
|
import gi
|
||||||
|
|
||||||
|
import nwg_panel.common
|
||||||
|
|
||||||
gi.require_version('GdkPixbuf', '2.0')
|
gi.require_version('GdkPixbuf', '2.0')
|
||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
gi.require_version('Gdk', '3.0')
|
gi.require_version('Gdk', '3.0')
|
||||||
@@ -74,7 +76,25 @@ def get_app_dirs():
|
|||||||
return desktop_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:
|
if not app_name:
|
||||||
return ""
|
return ""
|
||||||
# GIMP returns "app_id": null and for some reason "class": "Gimp-2.10" instead of just "gimp".
|
# 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"
|
return "gimp"
|
||||||
|
|
||||||
for d in nwg_panel.common.app_dirs:
|
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))
|
path = os.path.join(d, "{}.desktop".format(app_name))
|
||||||
content = None
|
content = None
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
content = load_text_file(path)
|
content = load_text_file(path)
|
||||||
elif os.path.isfile(path.lower()):
|
elif os.path.isfile(path.lower()):
|
||||||
content = load_text_file(path.lower())
|
content = load_text_file(path)
|
||||||
if content:
|
if content:
|
||||||
for line in content.splitlines():
|
for line in content.splitlines():
|
||||||
if line.upper().startswith("ICON"):
|
if line.upper().startswith("ICON"):
|
||||||
return line.split("=")[1]
|
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():
|
def local_dir():
|
||||||
local_dir = os.path.join(os.path.join(os.getenv("HOME"), ".local/share/nwg-panel"))
|
local_dir = os.path.join(os.path.join(os.getenv("HOME"), ".local/share/nwg-panel"))
|
||||||
|
2
setup.py
2
setup.py
@@ -8,7 +8,7 @@ def read(f_name):
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='nwg-panel',
|
name='nwg-panel',
|
||||||
version='0.4.0',
|
version='0.4.1',
|
||||||
description='GTK3-based panel for sway window manager',
|
description='GTK3-based panel for sway window manager',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
|
Reference in New Issue
Block a user