Skip to content

Commit

Permalink
Keep last entered data in injection widget
Browse files Browse the repository at this point in the history
  • Loading branch information
bjhardcastle committed Aug 2, 2024
1 parent f2e4ce6 commit ff5de66
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/npc_shields/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,9 @@ def get_hint(name, field) -> str:
if name not in ("shield")
}
self._apply_default_injection_values()
self._apply_previous_injection_values()
self.text_entry_grid = ipw.GridBox(
[v for v in self.text_entry_boxes.values()],
list(self.text_entry_boxes.values()),
)
hbox_elements = [self.text_entry_grid]
if self.shield:
Expand All @@ -299,6 +300,7 @@ def get_hint(name, field) -> str:
)
)
hbox = ipw.HBox(hbox_elements)
self.vbox_kwargs = vbox_kwargs

self.add_injection_button = ipw.Button(
description="Add this injection",
Expand All @@ -307,14 +309,24 @@ def get_hint(name, field) -> str:
tooltip="Append the current set of parameters as a unique injection for this session",
)
self.add_injection_button.on_click(lambda _: self.add_injection())

self.add_reset_button = ipw.Button(
description="Reset form",
button_style="warning",
layout=ipw.Layout(width="30%"),
tooltip="Clear all data entered in fields and reset to default values",
)
self.add_reset_button.on_click(lambda _: self._apply_default_injection_values())

self.console = ipw.Output()

if self.injections:
with self.console:
print(
f"Loaded existing injections [total: {len(self.injections)} injections]"
)
super().__init__([hbox, self.add_injection_button, self.console], **vbox_kwargs)

super().__init__([hbox, self.add_injection_button, self.add_reset_button, self.console], **vbox_kwargs)

@property
def save_paths(self) -> tuple[pathlib.Path, ...]:
Expand All @@ -341,6 +353,15 @@ def _apply_default_injection_values(self) -> None:
self.text_entry_boxes[name].value = ""
else:
self.text_entry_boxes[name].value = str(getattr(field, "default", ""))

def _apply_previous_injection_values(self) -> None:
latest_injection: npc_shields.types.Injection | None = max(self.injections, key=lambda x: x.start_time, default=None)
latest_injection_data = latest_injection.to_json() if latest_injection else {}
for name in self.text_entry_boxes:
value = latest_injection_data.get(name)
if value is not None:
self.text_entry_boxes[name].value = str(value)


def add_injection(self) -> None:
try:
Expand Down

0 comments on commit ff5de66

Please sign in to comment.