Save geometry to win registry

This commit is contained in:
faroukbmiled 2023-09-24 21:19:19 +01:00
parent 71bf316076
commit 211f319f75
3 changed files with 36 additions and 9 deletions

View File

@ -393,4 +393,25 @@ def check_item_date(down_date, date_updated):
except:
return False
def get_window_size_from_registry():
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, REGISTRY_KEY_PATH, 0, winreg.KEY_READ) as key:
width, _ = winreg.QueryValueEx(key, "WindowWidth")
height, _ = winreg.QueryValueEx(key, "WindowHeight")
x, _ = winreg.QueryValueEx(key, "WindowX")
y, _ = winreg.QueryValueEx(key, "WindowY")
return int(width), int(height), int(x), int(y)
except (FileNotFoundError, OSError, ValueError, FileNotFoundError):
return None, None, None, None
def save_window_size_to_registry(width, height, x, y):
try:
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, REGISTRY_KEY_PATH) as key:
winreg.SetValueEx(key, "WindowWidth", 0, winreg.REG_SZ, str(width))
winreg.SetValueEx(key, "WindowHeight", 0, winreg.REG_SZ, str(height))
winreg.SetValueEx(key, "WindowX", 0, winreg.REG_SZ, str(x))
winreg.SetValueEx(key, "WindowY", 0, winreg.REG_SZ, str(y))
except Exception as e:
print(f"Error saving to registry: {e}")
# End helper functions

View File

@ -18,6 +18,7 @@ from tkinter import END, Event, Menu
import customtkinter as ctk
import psutil
import requests
import winreg
from bs4 import BeautifulSoup
from CTkMessagebox import CTkMessagebox
from PIL import Image
@ -41,4 +42,5 @@ LATEST_RELEASE_URL = "https://github.com/faroukbmiled/BOIIIWD/releases/latest/do
LIBRARY_FILE = "boiiiwd_library.json"
RESOURCES_DIR = os.path.join(os.path.dirname(__file__), '..', 'resources')
UPDATER_FOLDER = "update"
REGISTRY_KEY_PATH = r"Software\BOIIIWD"
VERSION = "v0.3.1"

View File

@ -1,4 +1,3 @@
from turtle import title
from src.update_window import check_for_updates_func
from src.helpers import *
@ -13,11 +12,11 @@ class BOIIIWD(ctk.CTk):
# configure window
self.title("BOIII Workshop Downloader - Main")
window_info = get_window_size_from_registry()
try:
geometry_file = os.path.join(application_path, "boiiiwd_dont_touch.conf")
if os.path.isfile(geometry_file):
with open(geometry_file, "r") as conf:
self.geometry(conf.read())
if all(window_info):
width, height, x, y = window_info
self.geometry(f"{width}x{height}+{x}+{y}")
else:
self.geometry(f"{920}x{560}")
except:
@ -195,7 +194,7 @@ class BOIIIWD(ctk.CTk):
self.sidebar_settings_tooltip = CTkToolTip(self.sidebar_settings, message="Settings")
self.sidebar_library_tooltip = CTkToolTip(self.sidebar_library, message="Experimental")
self.sidebar_queue_tooltip = CTkToolTip(self.sidebar_queue, message="Experimental")
self.bind("<Configure>", self.save_window_size)
self.bind("<Configure>", lambda e: self.save_window_size_position())
# context_menus
self.create_context_menu(self.edit_workshop_id)
@ -322,9 +321,14 @@ class BOIIIWD(ctk.CTk):
if library:
self.library_tab.filter_items(self.cevent)
def save_window_size(self, event):
with open("boiiiwd_dont_touch.conf", "w") as conf:
conf.write(self.geometry())
def save_window_size_position(self):
geometry = self.geometry()
match = re.match(r"(\d+)x(\d+)\+(\d+)\+(\d+)", geometry)
if match:
width, height, x, y = map(int, match.groups())
save_window_size_to_registry(width, height, x, y)
else:
print("Invalid geometry format:", geometry)
def on_closing(self):
save_config("DestinationFolder" ,self.edit_destination_folder.get())