class SnapCheckBox(CheckBox): states = { True: SelectableIcon("[*]"), False: SelectableIcon("[ ]"), } def __init__(self, parent, snap, state): self.parent = parent self.snap = snap super().__init__(snap.name, state=state, on_state_change=self.state_change) def loaded(self): if len(self.snap.channels) == 0: # or other indication of failure ff = FetchingFailed(self, self.snap) self.parent.show_overlay(ff, width=ff.width) else: cur_chan = None if self.snap.name in self.parent.to_install: cur_chan = self.parent.to_install[self.snap.name].channel siv = SnapInfoView(self.parent, self.snap, cur_chan) self.parent.show_screen( screen(siv, [ other_btn(label=_("Close"), on_press=self.parent.show_main_screen) ], focus_buttons=False)) async def wait(self, t, fi): await t fi.close() self.loaded() def load_info(self): t = self.parent.controller.get_snap_info_task(self.snap) if t.done(): self.loaded() return fi = FetchingInfo(self.parent, self.snap, self.parent.controller.app.aio_loop) self.parent.show_overlay(fi, width=fi.width) schedule_task(self.wait(t, fi)) def keypress(self, size, key): if key.startswith("enter"): self.load_info() else: return super().keypress(size, key) def state_change(self, sender, new_state): if new_state: log.debug("selecting %s", self.snap.name) self.parent.to_install[self.snap.name] = SnapSelection( channel='stable', is_classic=self.snap.confinement == "classic") else: log.debug("unselecting %s", self.snap.name) self.parent.to_install.pop(self.snap.name, None)
class SnapCheckBox(CheckBox): states = { True: SelectableIcon("(*)"), False: SelectableIcon("( )"), } def __init__(self, parent, snap): self.parent = parent self.snap = snap super().__init__(snap.name, on_state_change=self.state_change) def load_info(self): called = False fi = None def callback(): nonlocal called called = True if fi is not None: fi.close() if len(self.snap.channels) == 0: # or other indication of failure ff = FetchingFailed(self, self.snap) self.parent.show_overlay(ff, width=ff.width) else: cur_chan = None if self.snap.name in self.parent.to_install: cur_chan = self.parent.to_install[self.snap.name].channel siv = SnapInfoView(self.parent, self.snap, cur_chan) self.parent.show_screen( screen(siv, [ other_btn(label=_("Close"), on_press=self.parent.show_main_screen) ], focus_buttons=False)) self.parent.controller.get_snap_info(self.snap, callback) # If we didn't get callback synchronously, display a dialog # while the info loads. if not called: fi = FetchingInfo(self.parent, self.snap, self.parent.controller.loop) self.parent.show_overlay(fi, width=fi.width) def keypress(self, size, key): if key.startswith("enter"): self.load_info() else: return super().keypress(size, key) def state_change(self, sender, new_state): if new_state: log.debug("selecting %s", self.snap.name) self.parent.to_install[self.snap.name] = SnapSelection( channel='stable', is_classic=self.snap.confinement == "classic") else: log.debug("unselecting %s", self.snap.name) self.parent.to_install.pop(self.snap.name, None)
def __init__(self, opts, index=0): self._options = [] for opt in opts: if not isinstance(opt, Option): opt = Option(opt) self._options.append(opt) self._button = SelectableIcon(self._prefix, len(self._prefix)) self._set_index(index) super().__init__(self._button)
def __init__(self, icon, label, on_press=None, user_data=None): self._icon = AttrMap(SelectableIcon(icon, 0), 'clip_dim') self._label = SelectableIcon(label, 0) cols = Columns([('fixed', len(icon), self._icon), self._label], dividechars=1) WidgetWrap.__init__(self, cols) connect_signal(self, 'click', on_press, user_data)
class SnapCheckBox(CheckBox): states = { True: SelectableIcon("[*]"), False: SelectableIcon("[ ]"), } def __init__(self, parent, snap, state): self.parent = parent self.snap = snap super().__init__( snap.name, state=state, on_state_change=self.state_change) async def load_info(self): app = self.parent.controller.app await app.wait_with_text_dialog( asyncio.shield( self.parent.controller.get_snap_info(self.snap)), _("Fetching info for {snap}").format(snap=self.snap.name), can_cancel=True) if len(self.snap.channels) == 0: # or other indication of failure ff = FetchingFailed(self, self.snap) self.parent.show_overlay(ff, width=ff.width) else: cur_chan = None selection = self.parent.selections_by_name.get(self.snap.name) if selection is not None: cur_chan = selection.channel siv = SnapInfoView(self.parent, self.snap, cur_chan) self.parent.show_screen(screen( siv, [other_btn( label=_("Close"), on_press=self.parent.show_main_screen)], focus_buttons=False)) def keypress(self, size, key): if key.startswith("enter"): schedule_task(self.load_info()) else: return super().keypress(size, key) def state_change(self, sender, new_state): if new_state: log.debug("selecting %s", self.snap.name) self.parent.selections_by_name[self.snap.name] = SnapSelection( name=self.snap.name, channel='stable', is_classic=self.snap.confinement == "classic") else: log.debug("unselecting %s", self.snap.name) self.parent.selections_by_name.pop(self.snap.name, None)
def __init__(self, opts, index=0): self._options = [] for opt in opts: if not isinstance(opt, tuple): if not isinstance(opt, str): raise SelectorError("invalid option %r", opt) opt = (opt, True, opt) elif len(opt) == 1: opt = (opt[0], True, opt[0]) elif len(opt) == 2: opt = (opt[0], opt[1], opt[0]) elif len(opt) != 3: raise SelectorError("invalid option %r", opt) self._options.append(opt) self._button = SelectableIcon(self._prefix, len(self._prefix)) self._set_index(index) super().__init__(self._button)
def __init__(self, opts): self._options = [] for opt in opts: if not isinstance(opt, Option): opt = Option(opt) self._options.append(opt) self._button = SelectableIcon(self.icon, 0) super().__init__(self._button) self._dialog = _ActionMenuDialog(self)
def __init__(self, opts, index=0): self._icon = SelectableIcon("", 0) self._padding = UrwidPadding( AttrWrap( Columns([ (1, Text('[')), self._icon, (3, Text('\N{BLACK DOWN-POINTING SMALL TRIANGLE} ]')), ], dividechars=1), 'menu_button', 'menu_button focus')) options = [] for opt in opts: options.append(Option(opt)) self.options = options self._set_index(index) super().__init__(_Launcher(self, self._padding))
def __init__(self, opts, icon="\N{BLACK RIGHT-POINTING SMALL TRIANGLE}"): self._actions = [] for opt in opts: if not isinstance(opt, Action): opt = Action(*opt) self._actions.append(opt) self.icon = icon self._button = SelectableIcon(self.icon, 0) super().__init__(self._button) self._dialog = _ActionMenuDialog(self)
def update_buttons(self): buttons = [(AttrMap(Button("Cancel", self.handle_cancel), 'button_primary', 'button_primary focus'), self.button_grid.options())] n_assigned = len(self.pc.assigned_services) if n_assigned > 0 and self.pc.can_deploy(): b = AttrMap(Button("Deploy", self.handle_deploy), 'button_primary', 'button_primary focus') else: b = AttrMap(SelectableIcon("(Deploy)"), 'disabled_button', 'disabled_button_focus') buttons.append((b, self.button_grid.options())) self.button_grid.contents = buttons
def update(self): self.deploy_view.update() self.required_services_list.update() self.additional_services_list.update() top_buttons = [] unplaced = self.placement_controller.unassigned_undeployed_services() if len(unplaced) == 0: icon = SelectableIcon(" (Auto-place Remaining Services) ") top_buttons.append((AttrMap(icon, 'disabled_button', 'disabled_button_focus'), self.top_button_grid.options())) else: top_buttons.append( (self.autoplace_button, self.top_button_grid.options())) top_buttons.append( (self.clear_all_button, self.top_button_grid.options())) self.top_button_grid.contents = top_buttons
def update(self): msg = ("Before continuing, ensure that at least one machine is " "enlisted into MAAS:") self.message = Text(self.spinner.next_frame() + ['\n', msg, '\n'], align='center') contents = [(self.message, self.main_pile.options())] global_ok, statuses = self.get_status() status_map = {True: ('success_icon', "\u2713 "), False: ('error_icon', "<!> ")} contents += [(Text([status_map[status], condition], align='center'), self.main_pile.options()) for status, condition in statuses] contents += [(Divider(), self.main_pile.options()), (self.button_pile, self.main_pile.options())] self.main_pile.contents = contents if not global_ok: b = AttrMap(SelectableIcon(" ( Can't Continue ) "), 'disabled_button', 'disabled_button_focus') else: b = AttrMap(Button("Continue", on_press=self.do_continue), 'button_primary', 'button_primary focus') cancel_b = AttrMap(Button("Cancel", on_press=self.do_cancel), 'button_secondary', 'button_secondary focus') self.button_pile.contents = [(Padding(cancel_b, width=24, align='center'), self.button_pile.options()), (Padding(b, width=24, align='center'), self.button_pile.options())] # ensure that the button is always focused: self.main_pile.focus_position = len(self.main_pile.contents) - 1
def update_buttons(self): buttons = [] for at in self.actions: if len(at) == 2: def predicate(x): return True label, func = at else: predicate, label, func = at if not predicate(self.machine): b = AttrMap(SelectableIcon(" (" + label + ")"), 'disabled_button', 'disabled_button_focus') else: b = AttrMap( Button(label, on_press=func, user_data=self.machine), 'button_secondary', 'button_secondary focus') buttons.append((b, self.button_grid.options())) self.button_grid.contents = buttons
class StarRadioButton(RadioButton): states = { True: SelectableIcon(" *"), False: SelectableIcon(" "), } reserve_columns = 3
def __init__(self, view, help_text): self._view = view self._help_text = help_text self._button = Padding.fixed_3(Color.button(SelectableIcon("[?]", 1))) super().__init__(self._button)
class Selector(PopUpLauncher): """A widget that allows the user to chose between options by popping up a list of options. (A bit like <select> in an HTML form). """ _prefix = "(+) " signals = ['select'] def __init__(self, opts, index=0): self._options = [] for opt in opts: if not isinstance(opt, Option): opt = Option(opt) self._options.append(opt) self._button = SelectableIcon(self._prefix, len(self._prefix)) self._set_index(index) super().__init__(self._button) def keypress(self, size, key): if self._command_map[key] != ACTIVATE: return key self.open_pop_up() def _set_index(self, val): self._button.set_text(self._prefix + self._options[val].label) self._index = val @property def index(self): return self._index @index.setter def index(self, val): self._emit('select', self._options[val].value) self._set_index(val) def option_by_label(self, label): for opt in self._options: if opt.label == label: return opt def option_by_value(self, value): for opt in self._options: if opt.value == value: return opt def option_by_index(self, index): return self._options[index] @property def value(self): return self._options[self._index].value @value.setter def value(self, val): for i, opt in enumerate(self._options): if opt.value == val: self.index = i return raise AttributeError("cannot set value to %r", val) def create_pop_up(self): return _PopUpSelectDialog(self, self.index) def get_pop_up_parameters(self): # line on left, space, line on right width = (max([len(o.label) for o in self._options]) + len(self._prefix) + 3) return {'left': -1, 'top': -self.index - 1, 'overlay_width': width, 'overlay_height': len(self._options) + 2}
class Selector(PopUpLauncher): """A widget that allows the user to chose between options by popping up this list of options. (A bit like <select> in an HTML form). """ _prefix = "(+) " signals = ['select'] def __init__(self, opts, index=0): self._options = [] for opt in opts: if not isinstance(opt, tuple): if not isinstance(opt, str): raise SelectorError("invalid option %r", opt) opt = (opt, True, opt) elif len(opt) == 1: opt = (opt[0], True, opt[0]) elif len(opt) == 2: opt = (opt[0], opt[1], opt[0]) elif len(opt) != 3: raise SelectorError("invalid option %r", opt) self._options.append(opt) self._button = SelectableIcon(self._prefix, len(self._prefix)) self._set_index(index) super().__init__(self._button) def keypress(self, size, key): if self._command_map[key] != ACTIVATE: return key self.open_pop_up() def _set_index(self, val): self._button.set_text(self._prefix + self._options[val][0]) self._index = val @property def index(self): return self._index @index.setter def index(self, val): self._emit('select', self._options[val][2]) self._set_index(val) @property def value(self): return self._options[self._index][2] @value.setter def value(self, val): for i, (label, enabled, value) in enumerate(self._options): if value == val: self.index = i return raise AttributeError("cannot set value to %r", val) def create_pop_up(self): return _PopUpSelectDialog(self, self.index) def get_pop_up_parameters(self): width = max([len(o[0]) for o in self._options]) \ + len(self._prefix) + 3 # line on left, space, line on right return {'left':-1, 'top':-self.index-1, 'overlay_width':width, 'overlay_height':len(self._options) + 2}
class Selector(WidgetWrap): """A widget that allows the user to chose between options by popping up a list of options. (A bit like <select> in an HTML form). """ signals = ['select'] def __init__(self, opts, index=0): self._icon = SelectableIcon("", 0) self._padding = UrwidPadding( AttrWrap( Columns([ (1, Text('[')), self._icon, (3, Text('\N{BLACK DOWN-POINTING SMALL TRIANGLE} ]')), ], dividechars=1), 'menu_button', 'menu_button focus')) options = [] for opt in opts: options.append(Option(opt)) self.options = options self._set_index(index) super().__init__(_Launcher(self, self._padding)) def get_natural_width(self): return self._padding.width def keypress(self, size, key): if self._command_map[key] != ACTIVATE: return key self.open_pop_up() def _set_index(self, val): self._icon.set_text(self._options[val].label) self._index = val @property def index(self): return self._index @index.setter def index(self, val): self._emit('select', self._options[val].value) self._set_index(val) @property def options(self): return self._options[:] @options.setter def options(self, val): self._options = val self._padding.width = max([len(o.label) for o in self._options]) + 6 def option_by_label(self, label): for opt in self._options: if opt.label == label: return opt def option_by_value(self, value): for opt in self._options: if opt.value == value: return opt def option_by_index(self, index): return self._options[index] @property def value(self): return self._options[self._index].value @value.setter def value(self, val): for i, opt in enumerate(self._options): if opt.value == val: self._set_index(i) return raise AttributeError("cannot set value to %r", val) def create_pop_up(self): return _PopUpSelectDialog(self, self.index) def get_pop_up_parameters(self): # line on left, space, line on right return { 'left': 0, 'top': -self.index - 1, 'overlay_width': self._padding.width, 'overlay_height': len(self._options) + 2 } def open_pop_up(self): self._w.open_pop_up() def close_pop_up(self): self._w.close_pop_up()
class StarCheckBox(CheckBox): states = { True: SelectableIcon(" *"), False: SelectableIcon(" "), } reserve_columns = 3
class StarRadioButton(RadioButton): states = { True: SelectableIcon("(*)"), False: SelectableIcon("( )"), }