optimize performance 06-05-2025
This commit is contained in:
138
wirepy.py
138
wirepy.py
@ -14,7 +14,7 @@ from pathlib import Path
|
||||
from subprocess import check_call
|
||||
from tkinter import TclError, filedialog, ttk
|
||||
|
||||
from cls_mth_fc import (ConfigManager, ThemeManager, Create, GiteaUpdate, Tunnel, Tooltip, LxTools)
|
||||
from common_tools import (ConfigManager, ThemeManager, Create, GiteaUpdate, Tunnel, Tooltip, LxTools)
|
||||
from wp_app_config import AppConfig, Msg
|
||||
|
||||
LxTools.uos()
|
||||
@ -43,6 +43,7 @@ class Wirepy(tk.Tk):
|
||||
ConfigManager.init(AppConfig.SETTINGS_FILE)
|
||||
theme = ConfigManager.get("theme")
|
||||
ThemeManager.change_theme(self, theme)
|
||||
|
||||
# Load the image file from the disk
|
||||
self.wg_icon = tk.PhotoImage(file=AppConfig.IMAGE_PATHS["icon_vpn"])
|
||||
|
||||
@ -56,16 +57,15 @@ class FrameWidgets(ttk.Frame):
|
||||
"""
|
||||
ttk frame class for better structure
|
||||
"""
|
||||
def __init__(self, container, tips_enabled=None, **kwargs):
|
||||
def __init__(self, container, **kwargs):
|
||||
super().__init__(container, **kwargs)
|
||||
|
||||
|
||||
self.lb_tunnel = None
|
||||
self.btn_stst = None
|
||||
self.endpoint = None
|
||||
self.dns = None
|
||||
self.address = None
|
||||
self.auto_con = None
|
||||
self.tips_enabled = tips_enabled
|
||||
self.style = ttk.Style()
|
||||
self.wg_vpn_start = tk.PhotoImage(file=AppConfig.IMAGE_PATHS["icon_start"])
|
||||
self.wg_vpn_stop = tk.PhotoImage(file=AppConfig.IMAGE_PATHS["icon_stop"])
|
||||
@ -73,10 +73,26 @@ class FrameWidgets(ttk.Frame):
|
||||
self.tr_pic = tk.PhotoImage(file=AppConfig.IMAGE_PATHS["icon_trash"])
|
||||
self.exp_pic = tk.PhotoImage(file=AppConfig.IMAGE_PATHS["icon_export"])
|
||||
self.warning_pic = tk.PhotoImage(file=AppConfig.IMAGE_PATHS["icon_error"])
|
||||
self.tips_enabled = tips_enabled if tips_enabled is not None else ConfigManager.get("tooltip")
|
||||
|
||||
# StringVar-Variables initialization
|
||||
self.update_label = tk.StringVar()
|
||||
self.update_tooltip = tk.StringVar()
|
||||
self.tooltip_state = tk.BooleanVar()
|
||||
# Get value from configuration
|
||||
state = ConfigManager.get("tooltips")
|
||||
# NOTE: ConfigManager.get("tooltips") can return either a boolean value or a string,
|
||||
# depending on whether the value was loaded from the file (bool) or the default value is used (string).
|
||||
# The expression 'lines[5].strip() == "True"' in ConfigManager.load() converts the string to a boolean.
|
||||
# Convert to boolean and set
|
||||
if isinstance(state, bool):
|
||||
# If it's already a boolean, use directly
|
||||
self.tooltip_state.set(state)
|
||||
else:
|
||||
# If it's a string or something else
|
||||
self.tooltip_state.set(str(state) == "True")
|
||||
|
||||
self.tooltip_label = tk.StringVar() # StringVar-Variable for tooltip label for view Disabled/Enabled
|
||||
self.tooltip_update_label()
|
||||
self.update_label = tk.StringVar() # StringVar-Variable for update label
|
||||
self.update_tooltip = tk.StringVar() # StringVar-Variable for update tooltip please not remove!
|
||||
self.update_foreground = tk.StringVar(value="red")
|
||||
|
||||
# Frame for Menu
|
||||
@ -89,25 +105,25 @@ class FrameWidgets(ttk.Frame):
|
||||
self.version_lb.config(font=("Ubuntu", 11), foreground="#00c4ff")
|
||||
self.version_lb.grid(column=0, row=0, rowspan=4, padx=10)
|
||||
|
||||
Tooltip(self.version_lb, f"Version: {AppConfig.VERSION[2:]}", self.tips_enabled)
|
||||
Tooltip(self.version_lb, f"Version: {AppConfig.VERSION[2:]}", self.tooltip_state)
|
||||
|
||||
self.options_btn = ttk.Menubutton(self.menu_frame, text=_("Options"))
|
||||
self.options_btn.grid(column=1, columnspan=1, row=0)
|
||||
|
||||
Tooltip(self.options_btn, _("Click for Settings"), self.tips_enabled)
|
||||
Tooltip(self.options_btn, _("Click for Settings"), self.tooltip_state)
|
||||
|
||||
self.set_update = tk.IntVar()
|
||||
self.set_tip = tk.BooleanVar()
|
||||
self.settings = tk.Menu(self, relief="flat")
|
||||
self.options_btn.configure(menu=self.settings, style="Toolbutton")
|
||||
self.settings.add_checkbutton(label=_("Disable Updates"),
|
||||
command=lambda: self.update_setting(self.set_update.get()), variable=self.set_update)
|
||||
self.settings.add_command(label=_("Disable Tooltips"),
|
||||
command=lambda: self.tooltip(self.set_tip.get()), variable=self.set_tip)
|
||||
|
||||
self.updates_lb = ttk.Label(self.menu_frame)
|
||||
res = GiteaUpdate.api_down(AppConfig.UPDATE_URL, AppConfig.VERSION, ConfigManager.get("updates"))
|
||||
self.update_ui_for_update(res)
|
||||
|
||||
# Tooltip Menu
|
||||
self.settings.add_command(label=self.tooltip_label.get(), command=self.tooltips_toggle)
|
||||
# Label show dark or light
|
||||
self.theme_label = tk.StringVar()
|
||||
self.update_theme_label()
|
||||
@ -119,10 +135,6 @@ class FrameWidgets(ttk.Frame):
|
||||
self.about_btn.grid(column=2, columnspan=2, row=0)
|
||||
self.readme = tk.Menu(self)
|
||||
|
||||
|
||||
# View Checkbox to enable or disable Tooltip
|
||||
self.set_tip.set(value=not self.tips_enabled)
|
||||
|
||||
self.a = Tunnel.active()
|
||||
|
||||
# Label Frame 1
|
||||
@ -204,7 +216,7 @@ class FrameWidgets(ttk.Frame):
|
||||
self.btn_i = ttk.Button(self.lb_frame_btn_lbox, image=self.imp_pic, command=self.import_sl, padding=0)
|
||||
self.btn_i.grid(column=0, row=1, padx=15, pady=8)
|
||||
|
||||
Tooltip(self.btn_i, _("Click to import a Wireguard Tunnel"), self.tips_enabled)
|
||||
Tooltip(self.btn_i, _("Click to import a Wireguard Tunnel"), self.tooltip_state)
|
||||
|
||||
# Button Trash
|
||||
self.btn_tr = ttk.Button(self.lb_frame_btn_lbox, image=self.tr_pic, command=self.delete, padding=0,
|
||||
@ -212,9 +224,9 @@ class FrameWidgets(ttk.Frame):
|
||||
self.btn_tr.grid(column=0, row=2, padx=15, pady=8)
|
||||
|
||||
if self.l_box.size() == 0:
|
||||
Tooltip(self.btn_tr, _("No tunnels to delete in the list"), self.tips_enabled)
|
||||
Tooltip(self.btn_tr, _("No tunnels to delete in the list"), self.tooltip_state)
|
||||
else:
|
||||
Tooltip(self.btn_tr, _("Click to delete a Wireguard Tunnel\nSelect from the list!"), self.tips_enabled)
|
||||
Tooltip(self.btn_tr, _("Click to delete a Wireguard Tunnel\nSelect from the list!"), self.tooltip_state)
|
||||
|
||||
# Button Export
|
||||
self.btn_exp = ttk.Button(self.lb_frame_btn_lbox, image=self.exp_pic,
|
||||
@ -226,9 +238,9 @@ class FrameWidgets(ttk.Frame):
|
||||
self.btn_exp.grid(column=0, row=3, padx=15, pady=8)
|
||||
|
||||
if self.l_box.size() == 0:
|
||||
Tooltip(self.btn_exp, _("No Tunnels in List for Export"), self.tips_enabled)
|
||||
Tooltip(self.btn_exp, _("No Tunnels in List for Export"), self.tooltip_state)
|
||||
else:
|
||||
Tooltip(self.btn_exp, _("Click to export all\nWireguard Tunnel to Zipfile"), self.tips_enabled)
|
||||
Tooltip(self.btn_exp, _("Click to export all\nWireguard Tunnel to Zipfile"), self.tooltip_state)
|
||||
|
||||
# Label Entry
|
||||
self.lb_rename = ttk.Entry(self.lb_frame4, width=20)
|
||||
@ -237,9 +249,9 @@ class FrameWidgets(ttk.Frame):
|
||||
self.lb_rename.config(state="disable")
|
||||
|
||||
if self.l_box.size() != 0:
|
||||
Tooltip(self.lb_rename, _("To rename a tunnel, you need to\nselect a tunnel from the list"), self.tips_enabled)
|
||||
Tooltip(self.lb_rename, _("To rename a tunnel, you need to\nselect a tunnel from the list"), self.tooltip_state)
|
||||
else:
|
||||
Tooltip(self.lb_rename, _("To rename a tunnel, at least one must be in the list"), self.tips_enabled)
|
||||
Tooltip(self.lb_rename, _("To rename a tunnel, at least one must be in the list"), self.tooltip_state)
|
||||
|
||||
# Button Rename
|
||||
self.btn_rename = ttk.Button(self.lb_frame4, text=_("Rename"), state="disable", command=self.tl_rename,
|
||||
@ -260,14 +272,14 @@ class FrameWidgets(ttk.Frame):
|
||||
self.wg_autostart.grid(column=0, row=0, pady=15, padx=15, sticky="nw")
|
||||
|
||||
if self.l_box.size() >= 1 and len(self.l_box.curselection()) >= 1:
|
||||
Tooltip(self.wg_autostart, Msg.TTIP["autostart"], tself.tips_enabled)
|
||||
Tooltip(self.wg_autostart, Msg.TTIP["autostart"], self.tooltip_state)
|
||||
|
||||
if self.l_box.size() == 0:
|
||||
Tooltip(self.wg_autostart, Msg.TTIP["autostart_info"], self.tips_enabled)
|
||||
Tooltip(self.wg_autostart, Msg.TTIP["autostart_info"], self.tooltip_state)
|
||||
|
||||
else:
|
||||
|
||||
Tooltip(self.wg_autostart, Msg.TTIP["autostart"], self.tips_enabled)
|
||||
Tooltip(self.wg_autostart, Msg.TTIP["autostart"], self.tooltip_state)
|
||||
|
||||
self.on_off()
|
||||
|
||||
@ -290,7 +302,7 @@ class FrameWidgets(ttk.Frame):
|
||||
foreground=self.update_foreground.get()
|
||||
)
|
||||
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
|
||||
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tips_enabled)
|
||||
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tooltip_state)
|
||||
|
||||
elif res == "No Internet Connection!":
|
||||
self.update_label.set(_("No Server Connection!"))
|
||||
@ -322,7 +334,7 @@ class FrameWidgets(ttk.Frame):
|
||||
foreground=self.update_foreground.get()
|
||||
)
|
||||
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
|
||||
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tips_enabled)
|
||||
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tooltip_state)
|
||||
|
||||
else:
|
||||
self.set_update.set(value=0)
|
||||
@ -336,7 +348,7 @@ class FrameWidgets(ttk.Frame):
|
||||
# Create the update button if it doesn't exist yet
|
||||
self.update_btn = ttk.Menubutton(self.menu_frame, text=update_text)
|
||||
self.update_btn.grid(column=4, columnspan=3, row=0, padx=0)
|
||||
Tooltip(self.update_btn, _("Click to download new version"), self.tips_enabled)
|
||||
Tooltip(self.update_btn, _("Click to download new version"), self.tooltip_state)
|
||||
|
||||
self.download = tk.Menu(self, relief="flat")
|
||||
self.update_btn.configure(menu=self.download, style="Toolbutton")
|
||||
@ -350,8 +362,8 @@ class FrameWidgets(ttk.Frame):
|
||||
AppConfig.IMAGE_PATHS["icon_error"],
|
||||
AppConfig.IMAGE_PATHS["icon_msg"]
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
def tooltip(self, tip) -> None:
|
||||
"""
|
||||
Aktualisiert die Tooltip-Einstellung im ConfigManager
|
||||
@ -361,7 +373,7 @@ class FrameWidgets(ttk.Frame):
|
||||
# Beachten Sie die umgekehrte Logik: tip=True bedeutet Tooltips deaktivieren
|
||||
ConfigManager.set("tooltip", not tip)
|
||||
# Aktualisieren Sie die lokale Variable für sofortige Wirkung
|
||||
self.tips_enabled = not tip
|
||||
self.tooltip_state = not tip
|
||||
|
||||
@staticmethod
|
||||
def about() -> None:
|
||||
@ -403,7 +415,7 @@ class FrameWidgets(ttk.Frame):
|
||||
foreground=self.update_foreground.get()
|
||||
)
|
||||
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
|
||||
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tips_enabled)
|
||||
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tooltip_state)
|
||||
|
||||
elif res == "No Internet Connection!":
|
||||
self.update_label.set(_("No Server Connection!"))
|
||||
@ -427,7 +439,7 @@ class FrameWidgets(ttk.Frame):
|
||||
foreground=self.update_foreground.get()
|
||||
)
|
||||
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
|
||||
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tips_enabled)
|
||||
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tooltip_state)
|
||||
|
||||
else:
|
||||
# We have an update available
|
||||
@ -443,7 +455,7 @@ class FrameWidgets(ttk.Frame):
|
||||
# Create the update button if it doesn't exist yet
|
||||
self.update_btn = ttk.Menubutton(self.menu_frame, text=update_text)
|
||||
self.update_btn.grid(column=4, columnspan=3, row=0, padx=0)
|
||||
Tooltip(self.update_btn, _("Click to download new version"), self.tips_enabled)
|
||||
Tooltip(self.update_btn, _("Click to download new version"), self.tooltip_state)
|
||||
|
||||
self.download = tk.Menu(self, relief="flat")
|
||||
self.update_btn.configure(menu=self.download, style="Toolbutton")
|
||||
@ -510,13 +522,35 @@ class FrameWidgets(ttk.Frame):
|
||||
# Fallback to a default message if there's an error
|
||||
self.update_ui_for_update_status("No Internet Connection!")
|
||||
|
||||
def update_tooletip_label(self) -> str:
|
||||
"""Update the theme label based on current theme"""
|
||||
current_value = ConfigManager.get("tooletip")
|
||||
if current_value == "True":
|
||||
self.set_tip.set(_("Enable Tooltips"))
|
||||
def tooltip_update_label(self) -> None:
|
||||
"""Updates the tooltip menu label based on the current tooltip status"""
|
||||
# Set the menu text based on the current status
|
||||
if self.tooltip_state.get():
|
||||
# If tooltips are enabled, the menu option should be to disable them
|
||||
self.tooltip_label.set(_("Disable Tooltips"))
|
||||
else:
|
||||
self.set_tip.set(_("Disable Tooltips"))
|
||||
# If tooltips are disabled, the menu option should be to enable them
|
||||
self.tooltip_label.set(_("Enable Tooltips"))
|
||||
|
||||
|
||||
def tooltips_toggle(self):
|
||||
"""Toggles tooltips on/off and updates the menu label"""
|
||||
# Toggle the boolean state
|
||||
new_bool_state = not self.tooltip_state.get()
|
||||
|
||||
# Save the converted value in the configuration
|
||||
ConfigManager.set("tooltips", str(new_bool_state))
|
||||
print(f"Tooltips are now: {new_bool_state} in ConfigManager")
|
||||
# Update the tooltip_state variable for immediate effect
|
||||
self.tooltip_state.set(new_bool_state)
|
||||
|
||||
# Update the menu label
|
||||
self.tooltip_update_label()
|
||||
|
||||
# Update the menu entry - find the correct index
|
||||
# This assumes it's the third item (index 2) in your menu
|
||||
self.settings.entryconfigure(1, label=self.tooltip_label.get())
|
||||
|
||||
|
||||
def update_theme_label(self) -> str:
|
||||
"""Update the theme label based on current theme"""
|
||||
@ -546,9 +580,9 @@ class FrameWidgets(ttk.Frame):
|
||||
|
||||
tl = LxTools.get_file_name(AppConfig.TEMP_DIR)
|
||||
if len(self.tl) == 0:
|
||||
Tooltip(self.btn_stst, Msg.TTIP["empty_list"], self.tips_enabled)
|
||||
Tooltip(self.btn_stst, Msg.TTIP["empty_list"], self.tooltip_state)
|
||||
else:
|
||||
Tooltip(self.btn_stst, Msg.TTIP["start_tl"], self.tips_enabled)
|
||||
Tooltip(self.btn_stst, Msg.TTIP["start_tl"], self.tooltip_state)
|
||||
|
||||
def handle_tunnel_data(self, tunnel_name: str) -> tuple[str, str, str, str | None]:
|
||||
"""_summary_
|
||||
@ -588,7 +622,7 @@ class FrameWidgets(ttk.Frame):
|
||||
command=lambda: self.wg_switch("stop"), padding=0)
|
||||
self.btn_stst.grid(column=0, row=0, padx=5, pady=8)
|
||||
|
||||
Tooltip(self.btn_stst, Msg.TTIP["stop_tl"], self.tips_enabled)
|
||||
Tooltip(self.btn_stst, Msg.TTIP["stop_tl"], self.tooltip_state)
|
||||
|
||||
def reset_fields(self) -> None:
|
||||
"""
|
||||
@ -664,10 +698,10 @@ class FrameWidgets(ttk.Frame):
|
||||
self.l_box.update()
|
||||
self.l_box.selection_set(0)
|
||||
|
||||
Tooltip(self.wg_autostart, Msg.TTIP["autostart"], self.tips_enabled)
|
||||
Tooltip(self.btn_tr, Msg.TTIP["trash_tl"], self.tips_enabled)
|
||||
Tooltip(self.btn_exp, Msg.TTIP["export_tl"], self.tips_enabled)
|
||||
Tooltip(self.btn_rename, Msg.TTIP["rename_tl"], self.tips_enabled)
|
||||
Tooltip(self.wg_autostart, Msg.TTIP["autostart"], self.tooltip_state)
|
||||
Tooltip(self.btn_tr, Msg.TTIP["trash_tl"], self.tooltip_state)
|
||||
Tooltip(self.btn_exp, Msg.TTIP["export_tl"], self.tooltip_state)
|
||||
Tooltip(self.btn_rename, Msg.TTIP["rename_tl"], self.tooltip_state)
|
||||
|
||||
self.lb_rename.insert(0, "Max. 12 characters!")
|
||||
self.str_var = tk.StringVar()
|
||||
@ -730,11 +764,11 @@ class FrameWidgets(ttk.Frame):
|
||||
self.wg_autostart.configure(state="disabled")
|
||||
self.lb_rename.configure(state="disabled")
|
||||
Tooltip(self.wg_autostart, _("You must have at least one\ntunnel in the list,to use the autostart")
|
||||
, self.tips_enabled)
|
||||
, self.tooltip_state)
|
||||
|
||||
Tooltip(self.btn_exp, _("No Tunnels in List for Export"), self.tips_enabled)
|
||||
Tooltip(self.btn_stst, _("No tunnels to start in the list"), self.tips_enabled)
|
||||
Tooltip(self.lb_rename, _("To rename a tunnel, at least one must be in the list"), tips, )
|
||||
Tooltip(self.btn_exp, _("No Tunnels in List for Export"), self.tooltip_state)
|
||||
Tooltip(self.btn_stst, _("No tunnels to start in the list"), self.tooltip_state)
|
||||
Tooltip(self.lb_rename, _("To rename a tunnel, at least one must be in the list"), self.tooltip_state)
|
||||
self.lb_rename.insert(0, _("Max. 12 characters!"))
|
||||
|
||||
if self.a != "" and self.a == select_tl:
|
||||
|
Reference in New Issue
Block a user