From 82d00d495b816c237cbefac3ff2387907a7f8843 Mon Sep 17 00:00:00 2001 From: mathieui Date: Fri, 19 Aug 2016 01:00:06 +0200 Subject: Add a "confirm" tab type This tab will be used when an external events prompts a boolean choice to a user, like a new ssl cert, or a XEP (e.g. 0070) that needs an answer, and for which a command-line interface with an info message would be a pain. --- poezio/tabs/__init__.py | 1 + poezio/tabs/confirmtab.py | 105 ++++++++++++++++++++++++++++++++++++++++++++ poezio/windows/__init__.py | 3 +- poezio/windows/confirm.py | 38 ++++++++++++++++ poezio/windows/info_wins.py | 18 ++++++++ 5 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 poezio/tabs/confirmtab.py create mode 100644 poezio/windows/confirm.py (limited to 'poezio') diff --git a/poezio/tabs/__init__.py b/poezio/tabs/__init__.py index 4347e505..55a74457 100644 --- a/poezio/tabs/__init__.py +++ b/poezio/tabs/__init__.py @@ -3,6 +3,7 @@ from poezio.tabs.basetabs import STATE_PRIORITY from poezio.tabs.rostertab import RosterInfoTab from poezio.tabs.muctab import MucTab, NS_MUC_USER from poezio.tabs.privatetab import PrivateTab +from poezio.tabs.confirmtab import ConfirmTab from poezio.tabs.conversationtab import ConversationTab, StaticConversationTab,\ DynamicConversationTab from poezio.tabs.xmltab import XMLTab diff --git a/poezio/tabs/confirmtab.py b/poezio/tabs/confirmtab.py new file mode 100644 index 00000000..3d4bd2dc --- /dev/null +++ b/poezio/tabs/confirmtab.py @@ -0,0 +1,105 @@ +""" +A generic tab that displays a text and a boolean choice +""" + +import logging +log = logging.getLogger(__name__) + +import curses +import collections + +from poezio import windows +from poezio.decorators import refresh_wrapper + +from poezio.tabs import Tab + + +class ConfirmTab(Tab): + plugin_commands = {} + plugin_keys = {} + + def __init__(self, core, name, text, short_message, callback, critical=False): + """Parameters: + name: The name of the tab + text: the text shown in the tab + short_message: what will be displayed at the top and bottom of + the tab + callback: the function(bool) that will be called when the user + makes a choice + critical: if the message needs to be displayed in a flashy color + """ + Tab.__init__(self, core) + self.state = 'normal' + self.name = name + self.default_help_message = windows.HelpText("Choose with arrow keys and press enter") + self.input = self.default_help_message + self.infowin_top = windows.ConfirmStatusWin(short_message, critical) + self.infowin_bottom = windows.ConfirmStatusWin(short_message, critical) + self.dialog = windows.Dialog(text, critical) + self.key_func['^I'] = self.toggle_choice + self.key_func[' '] = self.toggle_choice + self.key_func['KEY_LEFT'] = self.toggle_choice + self.key_func['KEY_RIGHT'] = self.toggle_choice + self.key_func['^M'] = self.validate + self.resize() + self.update_keys() + self.update_commands() + self.completion_callback = callback + + def toggle_choice(self): + self.dialog.toggle_choice() + self.refresh() + self.core.doupdate() + + def validate(self): + self.completion_callback(self.dialog.accept) + self.close() + + def refresh(self): + if self.need_resize: + self.resize() + log.debug(' TAB Refresh: %s', self.__class__.__name__) + if self.size.tab_degrade_y: + display_info_win = False + else: + display_info_win = True + if display_info_win: + self.info_win.refresh() + self.refresh_tab_win() + self.infowin_top.refresh() + self.infowin_bottom.refresh() + self.dialog.refresh() + self.input.refresh() + + def resize(self): + if self.size.tab_degrade_y: + info_win_height = 0 + tab_win_height = 0 + else: + info_win_height = self.core.information_win_size + tab_win_height = Tab.tab_win_height() + + self.infowin_top.resize(1, self.width, 0, 0) + self.infowin_bottom.resize(1, self.width, self.height - 2 - info_win_height - tab_win_height, 0) + self.dialog.resize(self.height - 3 - info_win_height - tab_win_height, + self.width, 1, 0) + self.input.resize(1, self.width, self.height-1, 0) + + def close(self, arg=None): + self.core.close_tab(self) + + def on_input(self, key, raw): + res = self.input.do_command(key, raw=raw) + if res and not isinstance(self.input, windows.Input): + return True + elif res: + return False + if not raw and key in self.key_func: + return self.key_func[key]() + + def on_info_win_size_changed(self): + if self.core.information_win_size >= self.height-3: + return + self.dialog.resize(self.height-3-self.core.information_win_size - Tab.tab_win_height(), self.width, 1, 0) + self.infowin_bottom.resize(1, self.width, self.height-2-self.core.information_win_size - Tab.tab_win_height(), 0) + diff --git a/poezio/windows/__init__.py b/poezio/windows/__init__.py index 463c27e1..06200a41 100644 --- a/poezio/windows/__init__.py +++ b/poezio/windows/__init__.py @@ -6,10 +6,11 @@ used to display information on the screen from poezio.windows.base_wins import Win from poezio.windows.data_forms import FormWin from poezio.windows.bookmark_forms import BookmarksWin +from poezio.windows.confirm import Dialog from poezio.windows.info_bar import GlobalInfoBar, VerticalGlobalInfoBar from poezio.windows.info_wins import InfoWin, XMLInfoWin, PrivateInfoWin, MucListInfoWin, \ ConversationInfoWin, DynamicConversationInfoWin, MucInfoWin, \ - ConversationStatusMessageWin, BookmarksInfoWin + ConversationStatusMessageWin, BookmarksInfoWin, ConfirmStatusWin from poezio.windows.input_placeholders import HelpText, YesNoInput from poezio.windows.inputs import Input, HistoryInput, MessageInput, CommandInput from poezio.windows.list import ListWin, ColumnHeaderWin diff --git a/poezio/windows/confirm.py b/poezio/windows/confirm.py new file mode 100644 index 00000000..591ff89a --- /dev/null +++ b/poezio/windows/confirm.py @@ -0,0 +1,38 @@ +from poezio.windows.base_wins import Win + +from poezio.theming import get_theme, to_curses_attr + +class Dialog(Win): + str_accept = " Accept " + str_refuse = " Reject " + def __init__(self, helper_text, critical=False): + self.text = helper_text + self.accept = False + self.critical = critical + + def refresh(self): + self._win.erase() + self.addstr(self.text + "\n ") + + if self.critical: + col = to_curses_attr(get_theme().COLOR_WARNING_PROMPT) + else: + col = to_curses_attr(get_theme().COLOR_TAB_NORMAL) + + if self.accept: + self.addstr(self.str_accept, col) + else: + self.addstr(self.str_accept) + + self.addstr(" ") + + if self.accept: + self.addstr(self.str_refuse) + else: + self.addstr(self.str_refuse, col) + + self._refresh() + + def toggle_choice(self): + self.accept = not self.accept + diff --git a/poezio/windows/info_wins.py b/poezio/windows/info_wins.py index 4ca72449..da470cb5 100644 --- a/poezio/windows/info_wins.py +++ b/poezio/windows/info_wins.py @@ -304,3 +304,21 @@ class BookmarksInfoWin(InfoWin): def write_remote_status(self, preferred): self.addstr('Remote storage: %s' % preferred, to_curses_attr(get_theme().COLOR_INFORMATION_BAR)) +class ConfirmStatusWin(Win): + def __init__(self, text, critical=False): + Win.__init__(self) + self.text = text + self.critical = critical + + def refresh(self): + log.debug('Refresh: %s', self.__class__.__name__) + self._win.erase() + if self.critical: + color = get_theme().COLOR_WARNING_PROMPT + else: + color = get_theme().COLOR_INFORMATION_BAR + c_color = to_curses_attr(color) + self.addstr(self.text, c_color) + self.finish_line(color) + self._refresh() + -- cgit v1.2.3