Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Popups: Allow dynamic text updates
Browse files Browse the repository at this point in the history
Allow passing an object rather than a list for the popup message. The object
must implement __iter__ and __next__ to generate the lines of text for the message
body in the popup.

Use for the submission picker. The submission text updates after picking a new
submission.
  • Loading branch information
natecraddock committed Jul 21, 2020
1 parent 5c09414 commit d2a6cea
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
2 changes: 2 additions & 0 deletions zygrader/config/changelog.txt
Expand Up @@ -5,6 +5,8 @@
* Make light theme consistent.
* Draw borders on popups.
* Show version number in header.
* Add commandline args to skip updates and install specific
versions. Run with --help for more info.

3.6
* Reformat zygrader source as a proper Python package.
Expand Down
2 changes: 1 addition & 1 deletion zygrader/config/shared.py
Expand Up @@ -5,7 +5,7 @@

class SharedData:
# Zygrader version
VERSION = LooseVersion("4.0.0")
VERSION = LooseVersion("4.0.1")

# Current class code (shared)
# TODO: Could this be per-user?
Expand Down
14 changes: 14 additions & 0 deletions zygrader/data/model.py
Expand Up @@ -85,6 +85,20 @@ class SubmissionFlag(enum.Flag):


class Submission:
# Implement the iterator interface so the message can be updated
# Throughout the grader popup's lifetime.
def __iter__(self):
self.current_line = 0
return self

def __next__(self):
if self.current_line < len(self.msg):
line = self.msg[self.current_line]
self.current_line += 1
return line
else:
raise StopIteration

def get_part_identifier(self, part):
"""Some parts are not named, use ID in that case"""
if part["name"]:
Expand Down
2 changes: 1 addition & 1 deletion zygrader/grader.py
Expand Up @@ -263,7 +263,7 @@ def student_callback(context: WinContext, lab, use_locks=True):
if not (use_locks and submission.flag & data.model.SubmissionFlag.DIFF_PARTS):
del options["Diff Parts"]

window.create_options_popup("Submission", submission.msg,
window.create_options_popup("Submission", submission,
options, components.Popup.ALIGN_LEFT)

SharedData.running_process = None
Expand Down
23 changes: 14 additions & 9 deletions zygrader/ui/components.py
Expand Up @@ -47,22 +47,22 @@ def __calculate_size(self):
self.y = (self.available_rows - self.rows) // 2
self.x = (self.available_cols - self.cols) // 2

def __draw_message_left(self):
longest_line = max([len(l) for l in self.message])
def __draw_message_left(self, message: list):
longest_line = max([len(l) for l in message])

message_x = self.cols // 2 - longest_line // 2

message_y = self.rows // 2 - len(self.message) // 2
message_y = self.rows // 2 - len(message) // 2
message_row = 0
for line in self.message:
for line in message:
line = line[:self.cols - Popup.PADDING]
add_str(self.window, message_y + message_row, message_x, line)
message_row += 1

def __draw_message_center(self):
message_y = self.rows // 2 - len(self.message) // 2
def __draw_message_center(self, message: list):
message_y = self.rows // 2 - len(message) // 2
message_row = 0
for line in self.message:
for line in message:
line = line[:self.cols - Popup.PADDING]
message_x = self.cols // 2 - len(line) // 2
add_str(self.window, message_y + message_row, message_x, line)
Expand All @@ -75,11 +75,16 @@ def draw_title(self):
def draw_text(self):
self.window.erase()

if not isinstance(self.message, list):
message = list(self.message)
else:
message = self.message

# Draw lines of message
if self.align == Popup.ALIGN_CENTER:
self.__draw_message_center()
self.__draw_message_center(message)
elif self.align == Popup.ALIGN_LEFT:
self.__draw_message_left()
self.__draw_message_left(message)

self.draw_title()

Expand Down

0 comments on commit d2a6cea

Please sign in to comment.