add class Image for icon managment
This commit is contained in:
@ -3,6 +3,8 @@
|
||||
import logging
|
||||
import gettext
|
||||
import locale
|
||||
import tkinter as tk
|
||||
import os
|
||||
from pathlib import Path
|
||||
from subprocess import CompletedProcess, run
|
||||
from typing import Dict, Any
|
||||
@ -79,21 +81,6 @@ class AppConfig:
|
||||
"pkey_path": "/usr/local/etc/ssl/pwgk.pem",
|
||||
}
|
||||
|
||||
# Images and icons paths
|
||||
IMAGE_PATHS: Dict[str, Path] = {
|
||||
"icon_header": "/usr/share/icons/lx-icons/32/wg_vpn.png",
|
||||
"icon_vpn": "/usr/share/icons/lx-icons/48/wg_vpn.png",
|
||||
"icon_msg": "/usr/share/icons/lx-icons/48/wg_msg.png",
|
||||
"icon_import": "/usr/share/icons/lx-icons/48/wg_import.png",
|
||||
"icon_export": "/usr/share/icons/lx-icons/48/wg_export.png",
|
||||
"icon_trash": "/usr/share/icons/lx-icons/48/wg_trash.png",
|
||||
"icon_start": "/usr/share/icons/lx-icons/48/wg_vpn-start.png",
|
||||
"icon_stop": "/usr/share/icons/lx-icons/48/wg_vpn-stop.png",
|
||||
"icon_info": "/usr/share/icons/lx-icons/64/info.png",
|
||||
"icon_error": "/usr/share/icons/lx-icons/64/error.png",
|
||||
"icon_log": "/usr/share/icons/lx-icons/48/log.png",
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def setup_translations() -> gettext.gettext:
|
||||
"""
|
||||
@ -171,6 +158,65 @@ class AppConfig:
|
||||
_ = AppConfig.setup_translations()
|
||||
|
||||
|
||||
class Image:
|
||||
def __init__(self):
|
||||
self.images = {}
|
||||
|
||||
def load_image(self, image_key, fallback_paths=None) -> None | tk.PhotoImage:
|
||||
"""Load PNG image using tk.PhotoImage with fallback options"""
|
||||
if image_key in self.images:
|
||||
return self.images[image_key]
|
||||
|
||||
# Define image paths based on key
|
||||
image_paths = {
|
||||
"icon_header": [
|
||||
"/usr/share/icons/lx-icons/32/wg_vpn.png",
|
||||
],
|
||||
"icon_vpn": [
|
||||
"/usr/share/icons/lx-icons/48/wg_vpn.png",
|
||||
],
|
||||
"icon_start": [
|
||||
"/usr/share/icons/lx-icons/48/wg_vpn-start.png",
|
||||
],
|
||||
"icon_stop": [
|
||||
"/usr/share/icons/lx-icons/48/wg_vpn-stop.png",
|
||||
],
|
||||
"icon_import": [
|
||||
"/usr/share/icons/lx-icons/48/wg_import.png",
|
||||
],
|
||||
"icon_export": [
|
||||
"/usr/share/icons/lx-icons/48/wg_export.png",
|
||||
],
|
||||
"icon_log": [
|
||||
"/usr/share/icons/lx-icons/48/log.png",
|
||||
],
|
||||
"icon_trash": [
|
||||
"/usr/share/icons/lx-icons/48/wg_trash.png",
|
||||
],
|
||||
}
|
||||
|
||||
# Get paths to try
|
||||
paths_to_try = image_paths.get(image_key, [])
|
||||
|
||||
# Add fallback paths if provided
|
||||
if fallback_paths:
|
||||
paths_to_try.extend(fallback_paths)
|
||||
|
||||
# Try to load image from paths
|
||||
for path in paths_to_try:
|
||||
try:
|
||||
if os.path.exists(path):
|
||||
photo = tk.PhotoImage(file=path)
|
||||
self.images[image_key] = photo
|
||||
return photo
|
||||
except tk.TclError as e:
|
||||
# print(f"{LocaleStrings.MSGP["fail_load_image"]}{path}: {e}")
|
||||
continue
|
||||
|
||||
# Return None if no image found
|
||||
return None
|
||||
|
||||
|
||||
class Msg:
|
||||
"""
|
||||
A utility class that provides centralized access to translated message strings.
|
||||
|
Reference in New Issue
Block a user