From 50d2fa3de8ef613e51c056a845128cd076c4e803 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 1 Sep 2023 03:04:39 +0100 Subject: [PATCH] save,load and load on startup implemented --- .gitignore | 3 ++ src/nvda_voicemeeter/builder.py | 7 ++-- src/nvda_voicemeeter/window.py | 59 ++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index a3825f1..8d284c8 100644 --- a/.gitignore +++ b/.gitignore @@ -170,5 +170,8 @@ basic.py banana.py potato.py +# persistent storage +default.bin + # quick test quick.py \ No newline at end of file diff --git a/src/nvda_voicemeeter/builder.py b/src/nvda_voicemeeter/builder.py index 1218dda..b34352b 100644 --- a/src/nvda_voicemeeter/builder.py +++ b/src/nvda_voicemeeter/builder.py @@ -63,6 +63,9 @@ class Builder: "&Voicemeeter", [ "Restart Audio Engine::MENU", + "Save Settings::MENU", + "Load Settings::MENU", + "Load Settings on Startup ::MENU", ], ], ] @@ -209,7 +212,7 @@ class Builder: [ psg.ButtonMenu( "ASIO BUFFER", - size=(12, 2), + size=(14, 2), menu_def=["", samples], key="ASIO BUFFER", ) @@ -271,7 +274,7 @@ class Builder: """tab3 row represents bus composite toggle""" def add_strip_outputs(layout): - layout.append([psg.Button(f"BUSMODE", size=(16, 2), key=f"BUS {i}||MODE")]) + layout.append([psg.Button(f"BUSMODE", size=(12, 2), key=f"BUS {i}||MODE")]) buses = list() [step(buses) for step in (add_strip_outputs,)] diff --git a/src/nvda_voicemeeter/window.py b/src/nvda_voicemeeter/window.py index 19f4f54..7177238 100644 --- a/src/nvda_voicemeeter/window.py +++ b/src/nvda_voicemeeter/window.py @@ -1,4 +1,6 @@ import logging +import pickle +from pathlib import Path import PySimpleGUI as psg @@ -22,6 +24,8 @@ psg.theme("Dark Blue 3") class NVDAVMWindow(psg.Window): """Represents the main window of the Voicemeeter NVDA application""" + DEFAULT_BIN = "default.bin" + def __init__(self, title, vm): self.vm = vm self.kind = self.vm.kind @@ -41,6 +45,21 @@ class NVDAVMWindow(psg.Window): self.register_events() def __enter__(self): + default_config = Path.cwd() / self.DEFAULT_BIN + if default_config.exists(): + try: + with open(default_config, "rb") as f: + if config := pickle.load(f): + self.vm.set("command.load", config) + self.logger.debug(f"config {config} loaded") + self.TKroot.after( + 200, + self.nvda.speak, + f"config {Path(config).stem} has been loaded", + ) + except EOFError: + self.logger.debug("no data in default bin. silently continuing...") + return self def __exit__(self, exc_type, exc_value, traceback): @@ -129,8 +148,46 @@ class NVDAVMWindow(psg.Window): self.TKroot.after( 200, self.nvda.speak, - f"Audio Engine restarted", + "Audio Engine restarted", ) + case [["Save", "Settings"], ["MENU"]]: + filename = psg.popup_get_text("Filename", title="Save As") + if filename: + configpath = Path.home() / "Documents" / "Voicemeeter" / f"{filename.removesuffix('xml')}.xml" + self.vm.set("command.save", str(configpath)) + self.logger.debug(f"saving config file to {configpath}") + self.TKroot.after( + 200, + self.nvda.speak, + f"config file {filename} has been saved", + ) + case [["Load", "Settings"], ["MENU"]]: + configpath = Path.home() / "Documents" / "Voicemeeter" + filename = psg.popup_get_file("Load Settings", default_path=str(configpath)) + if filename: + configpath = configpath / filename + self.vm.set("command.load", str(configpath)) + self.logger.debug(f"loading config file from {configpath}") + self.TKroot.after( + 200, + self.nvda.speak, + f"config file {Path(filename).stem} has been loaded", + ) + case [["Load", "Settings", "on", "Startup"], ["MENU"]]: + filename = psg.popup_get_text("Filename", title="Load on startup") + if filename: + configpath = Path.home() / "Documents" / "Voicemeeter" / f"{filename.removesuffix('xml')}.xml" + with open(self.DEFAULT_BIN, "wb") as f: + pickle.dump(str(configpath), f) + self.TKroot.after( + 200, + self.nvda.speak, + f"config {filename} set as default on startup", + ) + else: + with open(self.DEFAULT_BIN, "wb") as f: + f.truncate() + self.logger.debug("default bin was truncated") # Tabs case [["tabs"], ["FOCUS", "IN"]]: