implements label cache

popup_rename implemented

button binds added.
This commit is contained in:
onyx-and-iris 2023-09-02 04:23:15 +01:00
parent 7ff9c9e33b
commit 2666d8081a
3 changed files with 119 additions and 20 deletions

View File

@ -239,8 +239,7 @@ class Builder:
outputs = list() outputs = list()
[step(outputs) for step in (add_strip_outputs,)] [step(outputs) for step in (add_strip_outputs,)]
label = self.vm.strip[i].label return psg.Frame(self.window.cache["labels"]["strip"][f"STRIP {i}||LABEL"], outputs, key=f"STRIP {i}||LABEL")
return psg.Frame(label if label else f"Hardware Input {i + 1}", outputs)
def make_tab1_rows(self) -> psg.Frame: def make_tab1_rows(self) -> psg.Frame:
layout = [[self.make_tab1_row(i)] for i in range(self.kind.phys_in)] layout = [[self.make_tab1_row(i)] for i in range(self.kind.phys_in)]
@ -265,8 +264,7 @@ class Builder:
outputs = list() outputs = list()
[step(outputs) for step in (add_strip_outputs,)] [step(outputs) for step in (add_strip_outputs,)]
label = self.vm.strip[i].label return psg.Frame(self.window.cache["labels"]["strip"][f"STRIP {i}||LABEL"], outputs, key=f"STRIP {i}||LABEL")
return psg.Frame(label if label else f"Virtual Input {i - self.kind.phys_out + 1}", outputs)
def make_tab2_rows(self) -> psg.Frame: def make_tab2_rows(self) -> psg.Frame:
layout = [[self.make_tab2_row(i)] for i in range(self.kind.phys_in, self.kind.phys_in + self.kind.virt_in)] layout = [[self.make_tab2_row(i)] for i in range(self.kind.phys_in, self.kind.phys_in + self.kind.virt_in)]
@ -280,11 +278,7 @@ class Builder:
buses = list() buses = list()
[step(buses) for step in (add_strip_outputs,)] [step(buses) for step in (add_strip_outputs,)]
label = self.vm.bus[i].label return psg.Frame(self.window.cache["labels"]["bus"][f"BUS {i}||LABEL"], buses)
if i < self.kind.phys_out:
return psg.Frame(label if label else f"Physical Bus {i + 1}", buses)
else:
return psg.Frame(label if label else f"Virtual Bus {i - self.kind.phys_out + 1}", buses)
def make_tab3_rows(self): def make_tab3_rows(self):
layout = [[self.make_tab3_row(i)] for i in range(self.kind.num_bus)] layout = [[self.make_tab3_row(i)] for i in range(self.kind.num_bus)]

View File

@ -28,3 +28,30 @@ def _make_output_cache(vm) -> dict:
def _make_bus_mode_cache(vm) -> dict: def _make_bus_mode_cache(vm) -> dict:
return {**{f"BUS {i}||MODE": vm.bus[i].mode.get() for i in range(vm.kind.num_bus)}} return {**{f"BUS {i}||MODE": vm.bus[i].mode.get() for i in range(vm.kind.num_bus)}}
def _make_label_cache(vm) -> dict:
return {
"strip": {
**{
f"STRIP {i}||LABEL": vm.strip[i].label if vm.strip[i].label else f"Hardware Input {i + 1}"
for i in range(vm.kind.phys_in)
},
**{
f"STRIP {i}||LABEL": vm.strip[i].label
if vm.strip[i].label
else f"Virtual Input {i - vm.kind.phys_in + 1}"
for i in range(vm.kind.phys_in, vm.kind.phys_in + vm.kind.virt_in)
},
},
"bus": {
**{
f"BUS {i}||LABEL": vm.bus[i].label if vm.bus[i].label else f"Physical Bus {i + 1}"
for i in range(vm.kind.phys_out)
},
**{
f"BUS {i}||LABEL": vm.bus[i].label if vm.bus[i].label else f"Virtual Bus {i - vm.kind.phys_out + 1}"
for i in range(vm.kind.phys_out, vm.kind.phys_out + vm.kind.virt_out)
},
},
}

View File

@ -5,7 +5,7 @@ from pathlib import Path
import PySimpleGUI as psg import PySimpleGUI as psg
from .builder import Builder from .builder import Builder
from .models import _make_bus_mode_cache, _make_output_cache from .models import _make_bus_mode_cache, _make_label_cache, _make_output_cache
from .nvda import Nvda from .nvda import Nvda
from .parser import Parser from .parser import Parser
from .util import ( from .util import (
@ -30,7 +30,11 @@ class NVDAVMWindow(psg.Window):
self.vm = vm self.vm = vm
self.kind = self.vm.kind self.kind = self.vm.kind
self.logger = logger.getChild(type(self).__name__) self.logger = logger.getChild(type(self).__name__)
self.cache = {"outputs": _make_output_cache(self.vm), "busmode": _make_bus_mode_cache(self.vm)} self.cache = {
"outputs": _make_output_cache(self.vm),
"busmode": _make_bus_mode_cache(self.vm),
"labels": _make_label_cache(self.vm),
}
self.nvda = Nvda() self.nvda = Nvda()
self.parser = Parser() self.parser = Parser()
self.builder = Builder(self) self.builder = Builder(self)
@ -139,7 +143,7 @@ class NVDAVMWindow(psg.Window):
event, values = window.read() event, values = window.read()
if event in (psg.WIN_CLOSED, "Cancel"): if event in (psg.WIN_CLOSED, "Cancel"):
break break
if event.endswith("||FOCUS IN"): elif event.endswith("||FOCUS IN"):
if values["Browse"]: if values["Browse"]:
filepath = values["Browse"] filepath = values["Browse"]
break break
@ -155,6 +159,61 @@ class NVDAVMWindow(psg.Window):
if filepath: if filepath:
return Path(filepath) return Path(filepath)
def popup_rename(self, message, title=None, tab=None):
if tab == "Physical Strip":
upper = self.kind.phys_out + 1
elif tab == "Virtual Strip":
upper = self.kind.virt_out + 1
elif tab == "Buses":
upper = self.kind.num_bus + 1
layout = [
[psg.Text(message)],
[
[
psg.Spin(
list(range(1, upper)), initial_value=1, size=2, enable_events=True, key=f"Index", readonly=True
),
psg.Input(key="Edit"),
],
[psg.Button("Ok"), psg.Button("Cancel")],
],
]
window = psg.Window(title, layout, finalize=True)
window["Index"].bind("<FocusIn>", "||FOCUS IN")
window["Edit"].bind("<FocusIn>", "||FOCUS IN")
window["Ok"].bind("<FocusIn>", "||FOCUS IN")
window["Ok"].bind("<Return>", "||KEY ENTER")
window["Cancel"].bind("<FocusIn>", "||FOCUS IN")
window["Cancel"].bind("<Return>", "||KEY ENTER")
data = {}
while True:
event, values = window.read()
print(f"event: {event}")
print(f"values: {values}")
if event in (psg.WIN_CLOSED, "Cancel"):
break
elif event.endswith("||KEY ENTER"):
window.find_element_with_focus().click()
elif event == "Index":
val = values["Index"]
self.nvda.speak(f"Index {val}")
elif event.startswith("Index") and event.endswith("||FOCUS IN"):
val = values["Index"]
self.nvda.speak(f"Index {val}")
elif event.startswith("Edit") and event.endswith("||FOCUS IN"):
self.nvda.speak("Edit")
elif event == "Ok":
data = values
break
elif event.startswith("Ok") and event.endswith("||FOCUS IN"):
self.nvda.speak("Ok")
elif event.startswith("Cancel") and event.endswith("||FOCUS IN"):
self.nvda.speak("Cancel")
window.close()
return data
def run(self): def run(self):
""" """
Parses the event string and matches it to events Parses the event string and matches it to events
@ -176,6 +235,27 @@ class NVDAVMWindow(psg.Window):
case ["CTRL-TAB"] | ["CTRL-SHIFT-TAB"]: case ["CTRL-TAB"] | ["CTRL-SHIFT-TAB"]:
self["tabs"].set_focus() self["tabs"].set_focus()
# Rename popups
case ["F2:113"]:
tab = values["tabs"]
if tab in ("Physical Strip", "Virtual Strip", "Buses"):
data = self.popup_rename("Label", title=f"Rename {tab}", tab=tab)
if not data:
continue
index = int(data["Index"]) - 1
match tab:
case "Physical Strip" | "Virtual Strip":
print(self.cache["labels"]["strip"][f"STRIP {index}||LABEL"])
label = data.get("Edit") or self.cache["labels"]["strip"][f"STRIP {index}||LABEL"]
self.vm.strip[index].label = label
self[f"STRIP {index}||LABEL"].update(value=label)
self.cache["labels"]["strip"][f"STRIP {index}||LABEL"] = label
case "Buses":
label = data.get("Edit") or self.cache["labels"]["bus"][f"BUS {index}||LABEL"]
self.vm.bus[index].label = label
self[f"BUS {index}||LABEL"].update(value=label)
self.cache["labels"]["bus"][f"BUS {index}||LABEL"] = label
# Menus # Menus
case [["Restart", "Audio", "Engine"], ["MENU"]]: case [["Restart", "Audio", "Engine"], ["MENU"]]:
self.perform_long_operation(self.vm.command.restart, "ENGINE RESTART||END") self.perform_long_operation(self.vm.command.restart, "ENGINE RESTART||END")
@ -335,8 +415,8 @@ class NVDAVMWindow(psg.Window):
self.nvda.speak(f"STRIP {index} {output} {label if label else ''} {'on' if val else 'off'}") self.nvda.speak(f"STRIP {index} {output} {label if label else ''} {'on' if val else 'off'}")
case [["STRIP", index], [output], ["FOCUS", "IN"]]: case [["STRIP", index], [output], ["FOCUS", "IN"]]:
val = self.cache["outputs"][f"STRIP {index}||{output}"] val = self.cache["outputs"][f"STRIP {index}||{output}"]
label = self.vm.strip[int(index)].label label = self.cache["labels"]["strip"][f"STRIP {index}||LABEL"]
self.nvda.speak(f"STRIP {index} {output} {label if label else ''} {'on' if val else 'off'}") self.nvda.speak(f"{label} {output} {'on' if val else 'off'}")
case [["STRIP", index], [output], ["KEY", "ENTER"]]: case [["STRIP", index], [output], ["KEY", "ENTER"]]:
self.find_element_with_focus().click() self.find_element_with_focus().click()
@ -349,17 +429,15 @@ class NVDAVMWindow(psg.Window):
else: else:
self.vm.bus[int(index)].mode.composite = True self.vm.bus[int(index)].mode.composite = True
self.cache["busmode"][event] = "composite" self.cache["busmode"][event] = "composite"
label = self.vm.bus[int(index)].label label = self.cache["labels"]["bus"][f"BUS {index}||LABEL"]
self.TKroot.after( self.TKroot.after(
200, 200,
self.nvda.speak, self.nvda.speak,
f"BUS {index} {label if label else ''} bus mode {self.cache['busmode'][event]}", f"{label} bus mode {self.cache['busmode'][event]}",
) )
case [["BUS", index], ["MODE"], ["FOCUS", "IN"]]: case [["BUS", index], ["MODE"], ["FOCUS", "IN"]]:
label = self.vm.bus[int(index)].label label = self.cache["labels"]["bus"][f"BUS {index}||LABEL"]
self.nvda.speak( self.nvda.speak(f"{label} bus mode {self.cache['busmode'][f'BUS {index}||MODE']}")
f"BUS {index} {label if label else ''} bus mode {self.cache['busmode'][f'BUS {index}||MODE']}"
)
# Unknown # Unknown
case _: case _: