class ShamirNumInput(ui.Control): SET_SHARES = object() SET_THRESHOLD = object() def __init__(self, step, count, min_count, max_count): self.step = step self.input = NumInput(count, min_count=min_count, max_count=max_count) self.input.on_change = self.on_change self.repaint = True def dispatch(self, event, x, y): self.input.dispatch(event, x, y) if event is ui.RENDER: self.on_render() def on_render(self): if self.repaint: count = self.input.count # render the headline if self.step is ShamirNumInput.SET_SHARES: header = "Set num. of shares" elif self.step is ShamirNumInput.SET_THRESHOLD: header = "Set threshold" ui.header(header, ui.ICON_RESET, ui.TITLE_GREY, ui.BG, ui.ORANGE_ICON) # render the counter if self.step is ShamirNumInput.SET_SHARES: ui.display.text( 12, 130, "%s people or locations" % count, ui.BOLD, ui.FG, ui.BG, ui.WIDTH - 12, ) ui.display.text( 12, 156, "will each hold one share.", ui.NORMAL, ui.FG, ui.BG ) elif self.step is ShamirNumInput.SET_THRESHOLD: ui.display.text( 12, 130, "For recovery you need", ui.NORMAL, ui.FG, ui.BG ) ui.display.text( 12, 156, "any %s of the shares." % count, ui.BOLD, ui.FG, ui.BG, ui.WIDTH - 12, ) self.repaint = False def on_change(self, count): self.repaint = True
class ShamirNumInput(ui.Component): SET_SHARES = object() SET_THRESHOLD = object() SET_GROUPS = object() SET_GROUP_THRESHOLD = object() def __init__(self, step, count, min_count, max_count, group_id=None): self.step = step self.input = NumInput(count, min_count=min_count, max_count=max_count) self.input.on_change = self.on_change self.repaint = True self.group_id = group_id def dispatch(self, event, x, y): self.input.dispatch(event, x, y) if event is ui.RENDER: self.on_render() def on_render(self): if self.repaint: count = self.input.count # render the headline if self.step is ShamirNumInput.SET_SHARES: header = "Set num. of shares" elif self.step is ShamirNumInput.SET_THRESHOLD: header = "Set threshold" elif self.step is ShamirNumInput.SET_GROUPS: header = "Set num. of groups" elif self.step is ShamirNumInput.SET_GROUP_THRESHOLD: header = "Set group threshold" ui.header(header, ui.ICON_RESET, ui.TITLE_GREY, ui.BG, ui.ORANGE_ICON) # render the counter if self.step is ShamirNumInput.SET_SHARES: if self.group_id is None: first_line_text = "%s people or locations" % count second_line_text = "will each hold one share." else: first_line_text = "Set the total number of" second_line_text = "shares in Group %s." % (self.group_id + 1) ui.display.text(12, 130, first_line_text, ui.NORMAL, ui.FG, ui.BG, ui.WIDTH - 12) ui.display.text(12, 156, second_line_text, ui.NORMAL, ui.FG, ui.BG) elif self.step is ShamirNumInput.SET_THRESHOLD: if self.group_id is None: first_line_text = "For recovery you need" second_line_text = "any %s of the shares." % count else: first_line_text = "The required number of " second_line_text = "shares to form Group %s." % ( self.group_id + 1) ui.display.text(12, 130, first_line_text, ui.NORMAL, ui.FG, ui.BG) ui.display.text(12, 156, second_line_text, ui.NORMAL, ui.FG, ui.BG, ui.WIDTH - 12) elif self.step is ShamirNumInput.SET_GROUPS: ui.display.text(12, 130, "A group is made up of", ui.NORMAL, ui.FG, ui.BG) ui.display.text(12, 156, "recovery shares.", ui.NORMAL, ui.FG, ui.BG, ui.WIDTH - 12) elif self.step is ShamirNumInput.SET_GROUP_THRESHOLD: ui.display.text(12, 130, "The required number of", ui.NORMAL, ui.FG, ui.BG) ui.display.text( 12, 156, "groups for recovery.", ui.NORMAL, ui.FG, ui.BG, ui.WIDTH - 12, ) self.repaint = False def on_change(self, count): self.repaint = True