Files
Wire-Py/ui/menu_bar.py

180 lines
6.9 KiB
Python

import os
import subprocess
import tkinter as tk
import webbrowser
from functools import partial
from pathlib import Path
from tkinter import ttk
from logger import app_logger
from shared_libs.common_tools import ConfigManager, Tooltip
from shared_libs.gitea import GiteaUpdate
from shared_libs.message import MessageDialog
from shared_libs.wp_app_config import AppConfig, Msg
class MenuBar(ttk.Frame):
def __init__(self, container, image_manager, tooltip_state, on_theme_toggle, toggle_log_window, **kwargs):
super().__init__(container, **kwargs)
self.image_manager = image_manager
self.tooltip_state = tooltip_state
self.on_theme_toggle = on_theme_toggle
self.options_btn = ttk.Menubutton(self, text=Msg.STR["options"])
self.options_btn.grid(column=0, row=0)
Tooltip(self.options_btn, Msg.TTIP["settings"], state_var=self.tooltip_state)
self.set_update = tk.IntVar()
self.settings = tk.Menu(self, relief="flat")
self.options_btn.configure(menu=self.settings, style="Toolbutton")
self.settings.add_checkbutton(
label=Msg.STR["disable_updates"],
command=lambda: self.update_setting(self.set_update.get()),
variable=self.set_update,
)
self.update_label = tk.StringVar()
self.updates_lb = ttk.Label(self, textvariable=self.update_label)
self.updates_lb.grid(column=2, row=0)
self.updates_lb.grid_remove()
self.update_tooltip = tk.StringVar()
self.update_foreground = tk.StringVar(value="red")
self.update_label.trace_add("write", self.update_label_display)
self.update_foreground.trace_add("write", self.update_label_display)
res = GiteaUpdate.api_down(
AppConfig.UPDATE_URL, AppConfig.VERSION, ConfigManager.get("updates")
)
self.update_ui_for_update(res)
# Tooltip Menu
self.tooltip_label = tk.StringVar()
self.tooltip_update_label()
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()
self.settings.add_command(
label=self.theme_label.get(), command=self.on_theme_toggle
)
# About BTN Menu / Label
self.about_btn = ttk.Button(
self, text=Msg.STR["about"], style="Toolbutton", command=self.about
)
self.about_btn.grid(column=1, row=0)
self.columnconfigure(10, weight=1) # Add a column with weight to push button to the right
self.log_btn = ttk.Button(
self,
image=self.image_manager.get_icon("log_small"),
style="Toolbutton",
command=toggle_log_window,
)
self.log_btn.grid(column=11, row=0, sticky='e')
Tooltip(self.log_btn, "Show Log", state_var=self.tooltip_state)
def update_label_display(self, *args):
self.updates_lb.configure(foreground=self.update_foreground.get())
if self.update_label.get():
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
else:
self.updates_lb.grid_remove()
def updater(self):
tmp_dir = Path("/tmp/lxtools")
Path.mkdir(tmp_dir, exist_ok=True)
os.chdir(tmp_dir)
result = subprocess.run(["/usr/local/bin/lxtools_installer"], check=False)
if result.returncode != 0:
MessageDialog("error", result.stderr)
def update_ui_for_update(self, res):
if hasattr(self, "update_btn"):
self.update_btn.grid_forget()
delattr(self, "update_btn")
if res == "False":
self.set_update.set(value=1)
self.update_label.set(Msg.STR["update_search_off"])
self.update_tooltip.set(Msg.TTIP["updates_disabled"])
self.update_foreground.set("")
Tooltip(self.updates_lb, self.update_tooltip.get(), state_var=self.tooltip_state)
elif res == "No Internet Connection!":
self.update_label.set(Msg.STR["no_server_connection"])
self.update_foreground.set("red")
Tooltip(self.updates_lb, Msg.TTIP["no_server_conn_tt"], state_var=self.tooltip_state)
elif res == "No Updates":
self.update_label.set(Msg.STR["no_updates"])
self.update_tooltip.set(Msg.TTIP["up_to_date"])
self.update_foreground.set("")
Tooltip(self.updates_lb, self.update_tooltip.get(), state_var=self.tooltip_state)
else:
self.set_update.set(value=0)
self.update_label.set("")
self.update_btn = ttk.Button(
self,
image=self.image_manager.get_icon("settings"),
style="Toolbutton",
command=self.updater,
)
self.update_btn.grid(column=5, row=0, padx=0)
Tooltip(self.update_btn, Msg.TTIP["install_new_version"], state_var=self.tooltip_state)
@staticmethod
def about() -> None:
MessageDialog(
"info",
Msg.STR["about_msg"],
buttons=["OK", Msg.STR["goto_git"]],
title=Msg.STR["info"],
commands=[
None,
partial(webbrowser.open, "https://git.ilunix.de/punix/Wire-Py"),
],
icon="/usr/share/icons/lx-icons/64/wg_vpn.png",
wraplength=420,
)
def update_setting(self, update_res) -> None:
if update_res == 1:
ConfigManager.set("updates", "off")
self.update_ui_for_update("False")
else:
ConfigManager.set("updates", "on")
try:
res = GiteaUpdate.api_down(AppConfig.UPDATE_URL, AppConfig.VERSION, "on")
if hasattr(self, "update_btn"):
self.update_btn.grid_forget()
if hasattr(self, "updates_lb"):
self.updates_lb.grid_forget()
self.update_ui_for_update(res)
except Exception as e:
app_logger.log(f"Error checking for updates: {e}")
self.update_ui_for_update("No Internet Connection!")
def tooltip_update_label(self) -> None:
if self.tooltip_state.get():
self.tooltip_label.set(Msg.STR["disable_tooltips"])
else:
self.tooltip_label.set(Msg.STR["enable_tooltips"])
def tooltips_toggle(self):
new_bool_state = not self.tooltip_state.get()
ConfigManager.set("tooltips", str(new_bool_state))
self.tooltip_state.set(new_bool_state)
self.tooltip_update_label()
self.settings.entryconfigure(1, label=self.tooltip_label.get())
def update_theme_label(self) -> None:
current_theme = ConfigManager.get("theme")
if current_theme == "light":
self.theme_label.set(Msg.STR["dark"])
else:
self.theme_label.set(Msg.STR["light"])