async def prompt_multiline_text_output(self, title, text=''): result = ImbTuiResult() input_done = asyncio.Event() def ok_handler() -> None: input_done.set() def back_handler() -> None: result.back_selected = True input_done.set() ok_button = Button(text='Ok', handler=ok_handler) back_button = Button(text='Back', handler=back_handler) dialog = Dialog( title=title, body=TextArea(text=text, wrap_lines=True, multiline=True, scrollbar=True, read_only=True), buttons=[ok_button, back_button], modal=False, ) dialog.container.container.content.style = "" self.app_frame.body = HSplit([ dialog, ]) self.app.invalidate() self.app.layout.focus(self.app_frame) await input_done.wait() return result
def yes_no_dialog(title='', text='', yes_text='Yes', no_text='No', style=None, async_=False): """ Display a Yes/No dialog. Return a boolean. """ def yes_handler(): get_app().exit(result=True) def no_handler(): get_app().exit(result=False) dialog = Dialog(title=title, body=Label(text=text, dont_extend_height=True), buttons=[ Button(text=yes_text, handler=yes_handler), Button(text=no_text, handler=no_handler), ], with_background=True) return _run_dialog(dialog, style, async_=async_)
def input_dialog(title='', text='', ok_text='OK', cancel_text='Cancel', completer=None, password=False, style=None, async_=False): """ Display a text input box. Return the given text, or None when cancelled. """ def accept(buf): get_app().layout.focus(ok_button) return True # Keep text. def ok_handler(): get_app().exit(result=textfield.text) ok_button = Button(text=ok_text, handler=ok_handler) cancel_button = Button(text=cancel_text, handler=_return_none) textfield = TextArea( multiline=False, password=password, completer=completer, accept_handler=accept) dialog = Dialog( title=title, body=HSplit([ Label(text=text, dont_extend_height=True), textfield, ], padding=D(preferred=1, max=1)), buttons=[ok_button, cancel_button], with_background=True) return _run_dialog(dialog, style, async_=async_)
def show_message(self, title, message, callback=None): cancel_button = Button('Cancel', handler=lambda: response_received(False)) ok_button = Button('OK', handler=lambda: response_received(True)) def response_received(is_ok): if callback: callback(is_ok) self._float_message_layout = None self.layout.focus_next() self.invalidate() message_frame = Frame( HSplit([ Window(FormattedTextControl(HTML(message + '\n\n')), align=WindowAlign.CENTER), VSplit([ cancel_button, ok_button ], padding=3, align=WindowAlign.CENTER) ], padding=1), title=title, ) self._float_message_layout = message_frame self.layout.focus(cancel_button) self.invalidate()
def yes_no_dialog( title: AnyFormattedText = "", text: AnyFormattedText = "", yes_text: str = "Yes", no_text: str = "No", style: Optional[BaseStyle] = None, ) -> Application[bool]: """ Display a Yes/No dialog. Return a boolean. """ def yes_handler() -> None: get_app().exit(result=True) def no_handler() -> None: get_app().exit(result=False) dialog = Dialog( title=title, body=Label(text=text, dont_extend_height=True), buttons=[ Button(text=yes_text, handler=yes_handler), Button(text=no_text, handler=no_handler), ], with_background=True, ) return _create_app(dialog, style)
def radiolist_dialog(title: AnyFormattedText = '', text: AnyFormattedText = '', ok_text: str = 'Ok', cancel_text: str = 'Cancel', values: Optional[List[Tuple[_T, AnyFormattedText]]] = None, style: Optional[BaseStyle] = None) -> Application[_T]: """ Display a simple list of element the user can choose amongst. Only one element can be selected at a time using Arrow keys and Enter. The focus can be moved between the list and the Ok/Cancel button with tab. """ if values is None: values = [] def ok_handler() -> None: get_app().exit(result=radio_list.current_value) radio_list = RadioList(values) dialog = Dialog(title=title, body=HSplit([ Label(text=text, dont_extend_height=True), radio_list, ], padding=1), buttons=[ Button(text=ok_text, handler=ok_handler), Button(text=cancel_text, handler=_return_none), ], with_background=True) return _create_app(dialog, style)
def get_root_container(self) -> Dialog: difficulty_store = DifficultyPresets() with difficulty_store: difficulty_names = difficulty_store.get_difficulty_names( self._selected_game_getter().game_name) difficulty_radiolist = RadioList([(difficulty, difficulty) for difficulty in difficulty_names]) def ok_handler() -> None: self._selected_difficulty_setter( difficulty_radiolist.current_value) self.multi_screen.clear_floating() def cancel_handler() -> None: self.multi_screen.clear_floating() return Dialog( title="Difficulty", body=HSplit( [ Label(text="Select a difficulty", dont_extend_height=True), difficulty_radiolist, ], padding=1, ), buttons=[ Button(text="Okay", handler=ok_handler), Button(text="Cancel", handler=cancel_handler), ], with_background=True, )
def get_root_container(self) -> Dialog: sort_radiolist = RadioList([(sort_method, sort_method.column_name) for sort_method in ScoreSort]) def ok_handler() -> None: self._selected_sort_setter(sort_radiolist.current_value) self.multi_screen.clear_floating() def cancel_handler() -> None: self.multi_screen.clear_floating() return Dialog( title="Sort By", body=HSplit( [ Label(text="Select the column to sort by", dont_extend_height=True), sort_radiolist, ], padding=1, ), buttons=[ Button(text="Okay", handler=ok_handler), Button(text="Cancel", handler=cancel_handler), ], with_background=True, )
def radiolist_dialog(title='', text='', ok_text='Ok', cancel_text='Cancel', values=None, style=None, async_=False): """ Display a simple message box and wait until the user presses enter. """ def ok_handler(): get_app().exit(result=radio_list.current_value) radio_list = RadioList(values) dialog = Dialog(title=title, body=HSplit([ Label(text=text, dont_extend_height=True), radio_list, ], padding=1), buttons=[ Button(text=ok_text, handler=ok_handler), Button(text=cancel_text, handler=_return_none), ], with_background=True) return _run_dialog(dialog, style, async_=async_)
def __init__(self, event): def ok_handler(): # if len(ButtonManager.buttons) > 0: delete_server(event, name) root_container.floats.pop() def cancel_handler(): root_container.floats.pop() event.app.layout.focus(ButtonManager.prev_button) # Get data about server currently editing name = ButtonManager.current_button # Dialog configuration ok_button = Button(text='OK', handler=ok_handler) cancel_button = Button(text='Cancel', handler=cancel_handler) self.dialog = Dialog( title='Delete confirmation', body=Label( text='Are you sure you want to delete {}?'.format(name)), buttons=[cancel_button, ok_button], width=D(preferred=80), with_background=True) root_container.floats.append(Float(self.dialog)) event.app.layout.focus(self.dialog)
def __init__(self, title="", label_text="", completer=None): self.future = Future() def accept_text(buf): app.layout.focus(ok_button) buf.complete_state = None return True def accept(): self.future.set_result(self.text_area.text) def cancel(): self.future.set_result(None) self.text_area = TextArea( completer=completer, multiline=False, width=D(preferred=40), accept_handler=accept_text, ) ok_button = Button(text="OK", handler=accept) cancel_button = Button(text="Cancel", handler=cancel) self.dialog = Dialog( title=title, body=HSplit([Label(text=label_text), self.text_area]), buttons=[ok_button, cancel_button], width=D(preferred=80), modal=True, )
def __init__(self, title, text, yes_text, no_text, button=None): self.future = Future() def yes_handler(): self.future.set_result(True) def no_handler(): self.future.set_result(None) self.buttons = [ Button(text=yes_text, handler=yes_handler), Button(text=no_text, handler=no_handler), ] if button: self.buttons.insert( 1, Button(text=button[0], handler=lambda: button[1](self))) self.dialog = Dialog( title=title, body=HSplit([Label(text=text)]), buttons=self.buttons, width=D(preferred=50), modal=True, )
def init_layout(self): for i in range(len(self.top_screens)): s = self.top_screens[i] s.app = self s.screen_idx = i self.screen_sel_buttons.append( Button(text="%s %s" % (s.shortcut.upper(), s.shortcut_label), handler=s.screen_sel)) self.lbl_screen = Label(text=self.top_screens[0].title) self.top_screen = DynamicContainer(self.get_top_screen_container) btn_action = Button(text="F8 Action", handler=self.f_action) btn_exit = Button(text="F10 Exit", handler=self.f_exit) self.root_container = FloatContainer( HSplit([ Box( body=VSplit([self.lbl_screen], align="CENTER", padding=3), style="class:button-bar", height=1, ), self.top_screen, Box( body=VSplit(self.screen_sel_buttons + [btn_action, btn_exit], align="CENTER", padding=3), style="class:button-bar", height=1, ), ]), self.floats) self.top_layout = Layout(self.root_container, focused_element=self.screen_sel_buttons[0])
def __init__(self, title, text, asking=False): self.future = Future() def set_done(): self.future.set_result(None) def accept(): self.future.set_result(True) def cancel(): self.future.set_result(False) if asking: buttons = [ Button(text="Yes", handler=accept), Button(text="No", handler=cancel), ] else: buttons = [Button(text="OK", handler=set_done)] text = "\n".join( [textwrap.fill(line, width=71) for line in text.splitlines()]) self.dialog = Dialog( title=title, body=HSplit([Label(text=text)]), buttons=buttons, width=D(preferred=75), modal=True, )
def __init__(self, title='', label_text='', completer=None): self.future = Future() def accept_text(): get_app().layout.focus(ok_button) self.text_area.buffer.complete_state = None def accept(): self.future.set_result(self.text_area.text) def cancel(): self.future.set_result(None) self.text_area = TextArea( completer=completer, multiline=False, width=D(preferred=40), accept_handler=accept_text) ok_button = Button(text='OK', handler=accept) cancel_button = Button(text='Cancel', handler=cancel) self.dialog = Dialog( title=title, body=HSplit([ Label(text=label_text), self.text_area ]), buttons=[ok_button, cancel_button], width=D(preferred=80), modal=True)
def __init__(self, event, dialog): def ok_handler(): root_container.floats.pop() db.update_one(values={'name': name, 'auth': None}) event.app.layout.focus(ButtonManager.prev_button) select_item(event) def cancel_handler(): root_container.floats.pop() root_container.floats.append(self.auth_float) event.app.layout.focus(dialog) ok_button = Button(text='OK', handler=ok_handler) cancel_button = Button(text='Cancel', handler=cancel_handler) name = ButtonManager.current_button self.dialog = Dialog( title='Delete confirmation', body=Label( text='Are you sure you want to delete authentication for {}?'. format(name)), buttons=[cancel_button, ok_button], width=D(preferred=80), with_background=True) self.auth_float = root_container.floats.pop() root_container.floats.append(Float(self.dialog)) event.app.layout.focus(self.dialog)
def disconnect_dialog(my_app: "sqlApp"): def yes_handler() -> None: obj = my_app.selected_object obj.conn.close() if my_app.active_conn is obj.conn: my_app.active_conn = None my_app.show_disconnect_dialog = False my_app.show_sidebar = True my_app.application.layout.focus("sidebarbuffer") def no_handler() -> None: my_app.show_disconnect_dialog = False my_app.show_sidebar = True my_app.application.layout.focus("sidebarbuffer") dialog = Dialog( title=lambda: my_app.selected_object.name, body=Label(text="Are you sure you want to disconnect?", dont_extend_height=True), buttons=[ Button(text="OK", handler=yes_handler), Button(text="Cancel", handler=no_handler), ], with_background=False, ) return ConditionalContainer(content=dialog, filter=ShowDisconnectDialog(my_app) & ~is_done)
def radiolist_dialog(title='', text='', ok_text='Ok', cancel_text='Cancel', values=None, style=None, async_=False): """ Display a simple list of element the user can choose amongst. Only one element can be selected at a time using Arrow keys and Enter. The focus can be moved between the list and the Ok/Cancel button with tab. """ def ok_handler(): get_app().exit(result=radio_list.current_value) radio_list = RadioList(values) dialog = Dialog( title=title, body=HSplit([ Label(text=text, dont_extend_height=True), radio_list, ], padding=1), buttons=[ Button(text=ok_text, handler=ok_handler), Button(text=cancel_text, handler=_return_none), ], with_background=True) return _run_dialog(dialog, style, async_=async_)
async def long_prompt_text_input(self, title, prompt: Union[str, Iterable[str]], initial_text='', allow_other=False): 'prompt for single text input with a multi-line prompt' result = ImbTuiResult() input_done = asyncio.Event() def accept(buf) -> bool: get_app().layout.focus(ok_button) return True # Keep text. dialog_body = [] if isinstance(prompt, str): dialog_body.append( Window(FormattedTextControl(prompt), height=1, align=WindowAlign.CENTER)) else: for line in prompt: dialog_body.append( Window(FormattedTextControl(line), height=1, align=WindowAlign.CENTER)) text_field = TextArea(text=initial_text, multiline=False, accept_handler=accept) dialog_body.append(text_field) def ok_handler(): result.value = text_field.text input_done.set() def back_handler(): result.back_selected = True input_done.set() ok_button = Button(text='Ok', handler=ok_handler) dialog = Dialog( title=title, body=HSplit(dialog_body), buttons=[ ok_button, Button(text="Back", handler=back_handler), ], modal=False, ) # disable a_reverse style applied to dialogs dialog.container.container.content.style = "" self.app_frame.body = HSplit([ Window(), dialog, Window(), ]) self.app.invalidate() self.app.layout.focus(self.app_frame) await input_done.wait() return result
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.riitag_info = user.RiitagInfo() # placeholder self.menu_settings_button = Button( 'Settings', handler=lambda: self._set_state('Settings')) self.menu_exit_button = Button('Exit', handler=self.quit_app) self.menu_logout_button = Button('Logout', handler=self._logout) self.settings_back_button = Button( 'Back...', width=12, handler=lambda: self._set_state('Menu')) self.settings_reset_button = Button('Reset...', width=12, handler=self._reset_preferences) self.settings_pres_timeout_button = PreferenceButton( value=self.app.preferences.presence_timeout, increments=10, limits=(10, 120)) self.settings_check_interval_button = PreferenceButton( value=self.app.preferences.check_interval, increments=10, limits=(10, 60)) self.right_panel_state = 'Menu' self.menu_layout = Frame(Box(HSplit([ self.menu_settings_button, Label(''), self.menu_exit_button, self.menu_logout_button, ]), padding_left=3, padding_top=2), title='Menu') self.settings_layout = Frame(Box(HSplit([ Window(FormattedTextControl( HTML( 'This is where you can modify settings\nregarding the underlying presence\nwatcher.' )), wrap_lines=True, width=25), Label(''), VSplit([ Label('Presence Timeout (min.):'), self.settings_pres_timeout_button ], width=15), VSplit([ Label('Refresh Interval (sec.):'), self.settings_check_interval_button ], padding=3), Label(''), VSplit([self.settings_back_button, self.settings_reset_button], align=WindowAlign.CENTER) ]), padding_left=3, padding_top=2), title='Settings')
def get_questions_widgets(self): question_widgets = [ Frame(body=Label(text=unicode(self.header))), ] question_widgets += self.questions.get_widgets_list() create_button = Button(u"Create", handler=self.accept_create) exit_button = Button(u"Exit", handler=self.accept_exit) question_widgets.append(Box(VSplit([create_button, exit_button]))) return question_widgets
async def prompt_yn(self, title, prompt, disable_back=False, allow_other=False, other_button_text="Other"): result = ImbTuiResult() input_done = asyncio.Event() def yes_handler(): result.value = True input_done.set() def no_handler(): result.value = False input_done.set() def back_handler(): result.back_selected = True input_done.set() def other_handler(): result.other_selected = True input_done.set() buttons = [ Button(text="Yes", handler=yes_handler), Button(text="No", handler=no_handler) ] if not disable_back: buttons.append(Button(text="Back", handler=back_handler)) if allow_other: buttons.append( Button(text=other_button_text, handler=other_handler)) yn_dialog = Dialog( title=title, body=Window(FormattedTextControl(prompt), height=1, align=WindowAlign.CENTER), buttons=buttons, modal=False, ) # disable a_reverse style applied to dialogs yn_dialog.container.container.content.style = "" self.app_frame.body = HSplit([ Window(), yn_dialog, Window(), ]) self.app.invalidate() self.app.layout.focus(self.app_frame) await input_done.wait() return result
def input_dialog( title: AnyFormattedText = "", text: AnyFormattedText = "", ok_text: str = "OK", cancel_text: str = "Cancel", completer: Optional[Completer] = None, validator: Optional[Validator] = None, password: FilterOrBool = False, style: Optional[BaseStyle] = None, value: str = "", ) -> Application[str]: """ Display a text input box. Return the given text, or None when cancelled. """ def accept(buf: Buffer) -> bool: get_app().layout.focus(ok_button) return True # Keep text. def ok_handler() -> None: if textfield.buffer.validate(): get_app().exit(result=textfield.text) else: get_app().layout.focus(textfield) ok_button = Button(text=ok_text, handler=ok_handler) cancel_button = Button(text=cancel_text, handler=_return_none) textfield = TextArea( text=value, multiline=False, password=password, completer=completer, validator=validator, accept_handler=accept, ) dialog = Dialog( title=title, body=HSplit( [ Label(text=text, dont_extend_height=True), textfield, ValidationToolbar(), ], padding=D(preferred=1, max=1), ), buttons=[ok_button, cancel_button], with_background=True, ) return _create_app(dialog, style)
def _create_ui(self): btn_start = Button("Start", handler=self.tomato.start) btn_pause = Button("Pause", handler=self.tomato.pause) btn_reset = Button("Reset", handler=self.tomato.reset) btn_reset_all = Button("Reset All", handler=self.tomato.reset_all) btn_exit = Button("Exit", handler=self._exit_clicked) # All the widgets for the UI. self.text_area = FormattedTextControl(focusable=False, show_cursor=False) text_window = Window( content=self.text_area, dont_extend_height=True, height=11, style="bg:#ffffff #000000", ) root_container = Box( HSplit([ Label(text="Press `Tab` to move the focus."), HSplit([ VSplit( [ btn_start, btn_pause, btn_reset, btn_reset_all, btn_exit, ], padding=1, style="bg:#cccccc", ), text_window, ]), ])) layout = Layout(container=root_container, focused_element=btn_start) self._set_key_bindings() # Styling. style = Style([ ("left-pane", "bg:#888800 #000000"), ("right-pane", "bg:#00aa00 #000000"), ("button", "#000000"), ("button-arrow", "#000000"), ("button focused", "bg:#ff0000"), ("red", "#ff0000"), ("green", "#00ff00"), ]) self.application = Application(layout=layout, key_bindings=self.kb, style=style, full_screen=True)
async def prompt_multiline_text_input(self, title, prompt: Union[str, Iterable[str]], initial_text=''): result = ImbTuiResult() input_done = asyncio.Event() dialog_body = [] if isinstance(prompt, str): dialog_body.append( Window(FormattedTextControl(prompt), height=1, align=WindowAlign.CENTER)) else: for line in prompt: dialog_body.append( Window(FormattedTextControl(line), height=1, align=WindowAlign.CENTER)) textfield = TextArea(text=initial_text, multiline=True, scrollbar=True) dialog_body.append(textfield) def ok_handler() -> None: result.value = textfield.text input_done.set() def back_handler() -> None: result.back_selected = True input_done.set() ok_button = Button(text='Ok', handler=ok_handler) back_button = Button(text='Back', handler=back_handler) dialog = Dialog( title=title, body=HSplit(dialog_body, padding=Dimension(preferred=1, max=1)), buttons=[ok_button, back_button], modal=False, ) dialog.container.container.content.style = "" self.app_frame.body = HSplit([ dialog, ]) self.app.invalidate() self.app.layout.focus(self.app_frame) await input_done.wait() return result
def __init__(self): self.name = ButtonManager.current_button self.ok_button = Button(text='OK', handler=self.ok_handler) self.cancel_button = Button(text='Cancel', handler=self.cancel_handler) self.delete_button = Button(text='Delete', handler=self.delete_handler) self.authuser = TextArea(multiline=False) self.authpass_one = TextArea(multiline=False, password=True) self.authpass_two = TextArea(multiline=False, password=True)
def __init__(self, event, title='', text=''): def ok_handler(): root_container.floats.pop() # If there was an original dialog, insert it back into layout if self.orig_dialog: root_container.floats.append(self.orig_dialog) event.app.layout.focus(root_container.float_container) else: event.app.layout.focus(ButtonManager.prev_button) ok_button = Button(text='OK', handler=ok_handler) dialog = Dialog(Window(wrap_lines=True, content=FormattedTextControl(text=text), always_hide_cursor=True), title=title, width=D(preferred=80), buttons=[ok_button], with_background=True) try: # If a dialog was already up, save it self.orig_dialog = root_container.floats.pop() except IndexError: self.orig_dialog = None root_container.floats.append(Float(content=dialog)) event.app.layout.focus(ok_button)
def button_dialog( title: AnyFormattedText = "", text: AnyFormattedText = "", buttons: List[Tuple[str, _T]] = [], style: Optional[BaseStyle] = None, ) -> Application[_T]: """ Display a dialog with button choices (given as a list of tuples). Return the value associated with button. """ def button_handler(v: _T) -> None: get_app().exit(result=v) dialog = Dialog( title=title, body=Label(text=text, dont_extend_height=True), buttons=[ Button(text=t, handler=functools.partial(button_handler, v)) for t, v in buttons ], with_background=True, ) return _create_app(dialog, style)
def get_root_container(self) -> FloatContainer: buttons = [ *self._game_buttons.keys(), HorizontalLine(), Button("Quit", width=MENU_BUTTON_WIDTH, handler=self._exit), ] menu_keybindings = _create_menu_keybindings(buttons) game_buttons_container = Frame( HSplit( buttons, width=Dimension(min=MENU_BUTTON_WIDTH, max=40), height=Dimension(), ), title="Games", key_bindings=menu_keybindings, ) game_description_container = Frame( Box( Label( text=self._get_game_description, dont_extend_height=False, width=Dimension(min=40), ), padding=0, padding_left=1, ), ) return FloatContainer(VSplit([ game_buttons_container, game_description_container, ]), floats=[])
def get_root_container(self): solution_containers = [ HSplit([ self._get_grid_container(grid), Button( "Select", functools.partial(self.multi_screen.app.exit, result=grid)), ]) for grid in self.solution_grids ] challenge_grid_container = Box( self._get_grid_container(self.challenge_grid)) return FloatContainer( Box( Frame( HSplit( [ challenge_grid_container, VSplit( solution_containers, padding=2, ), ], padding=1, ), ), ), floats=[], )