From f78d30d502adc40cbcc8fa7568cd63a87c6b6b7a Mon Sep 17 00:00:00 2001 From: piotr Date: Wed, 25 Oct 2023 01:08:40 +0200 Subject: [PATCH] buffer once found icon names #259 --- nwg_panel/common.py | 1 + nwg_panel/main.py | 2 +- nwg_panel/tools.py | 60 +++++++++++++++++++++++++-------------------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/nwg_panel/common.py b/nwg_panel/common.py index 482652e..36afef9 100644 --- a/nwg_panel/common.py +++ b/nwg_panel/common.py @@ -18,6 +18,7 @@ dwl_instances = [] app_dirs = [] name2icon_dict = {} scratchpad_cons = {} +app_name2icon_name = {} commands = { "light": False, diff --git a/nwg_panel/main.py b/nwg_panel/main.py index 82863cf..19f870c 100644 --- a/nwg_panel/main.py +++ b/nwg_panel/main.py @@ -544,7 +544,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() + # common.name2icon_dict = map_odd_desktop_files() config_file = os.path.join(common.config_dir, args.config) diff --git a/nwg_panel/tools.py b/nwg_panel/tools.py index bc7ba37..8dc40ac 100644 --- a/nwg_panel/tools.py +++ b/nwg_panel/tools.py @@ -72,32 +72,36 @@ def get_app_dirs(): return desktop_dirs -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.endswith(".desktop") and path.count(".") > 1: - try: - content = load_text_file(os.path.join(d, path)) - except Exception as e: - eprint(e) - if content: - 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 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.endswith(".desktop") and path.count(".") > 1: +# try: +# content = load_text_file(os.path.join(d, path)) +# except Exception as e: +# eprint(e) +# if content: +# 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 "" + # the name might've been already found in .desktop files and stored + if app_name in nwg_panel.common.app_name2icon_name: + return nwg_panel.common.app_name2icon_name[app_name] + # GIMP returns "app_id": null and for some reason "class": "Gimp-2.10" instead of just "gimp". # Until the GTK3 version is released, let's make an exception for GIMP. if "GIMP" in app_name.upper(): @@ -114,7 +118,9 @@ def get_icon_name(app_name): if content: for line in content.splitlines(): if line.upper().startswith("ICON"): - return line.split("=")[1] + icon_name = line.split("=")[1] + nwg_panel.common.app_name2icon_name[app_name] = icon_name + return icon_name # Otherwise, we need to search all .desktop files if os.path.isdir(d): @@ -125,13 +131,15 @@ def get_icon_name(app_name): if content: for line in content.splitlines(): if line.upper().startswith("ICON"): - return line.split("=")[1] + icon_name = line.split("=")[1] + nwg_panel.common.app_name2icon_name[app_name] = icon_name + return icon_name # Search the dictionary made of .desktop files that use "reverse DNS"-style names, prepared on startup. # 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] + # for key in nwg_panel.common.name2icon_dict.keys(): + # if app_name in key.split("."): + # return nwg_panel.common.name2icon_dict[key] # if all above fails return app_name