From 9aadec6cfbeadad5fb995b17a0e8450842192158 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Tue, 27 Aug 2019 14:16:14 +0200 Subject: [PATCH] Add option to set status visibility --- toot/tui/compose.py | 109 +++++++++++++++++++++++++++++------------- toot/tui/constants.py | 7 +++ 2 files changed, 84 insertions(+), 32 deletions(-) diff --git a/toot/tui/compose.py b/toot/tui/compose.py index cffb52d..61f6e19 100644 --- a/toot/tui/compose.py +++ b/toot/tui/compose.py @@ -1,6 +1,8 @@ import urwid import logging +from .constants import VISIBILITY_OPTIONS + logger = logging.getLogger(__name__) @@ -22,59 +24,102 @@ class Button(urwid.AttrWrap): class StatusComposer(urwid.Frame): + """ + UI for compose and posting a status message. + """ signals = ["close", "post"] def __init__(self): - # This can be added by button press - self.content = EditBox() - self.content_warning = None - self.cw_button = Button("Add content warning", on_press=self.toggle_cw) + self.content_caption = urwid.Text("Status message") + self.content_edit = EditBox() - contents = [ - urwid.Text("Status message"), - self.content, - urwid.Divider(), - self.cw_button, - Button("Post", on_press=self.post), - Button("Cancel", on_press=self.close), - ] + self.cw_caption = urwid.Text("Content warning") + self.cw_edit = None + self.cw_add_button = Button("Add content warning", + on_press=self.add_content_warning) + self.cw_remove_button = Button("Remove content warning", + on_press=self.remove_content_warning) + self.visibility = "public" + self.visibility_button = Button("Visibility: {}".format(self.visibility), + on_press=self.choose_visibility) + + self.post_button = Button("Post", on_press=self.post) + self.cancel_button = Button("Cancel", on_press=self.close) + + contents = list(self.generate_list_items()) + logger.info(contents) self.walker = urwid.SimpleListWalker(contents) self.listbox = urwid.ListBox(self.walker) return super().__init__(self.listbox) - def toggle_cw(self, button): - if self.content_warning: - self.cw_button.set_label("Add content warning") - self.walker.pop(2) - self.walker.pop(2) - self.walker.pop(2) - self.walker.set_focus(3) - self.content_warning = None - else: - self.cw_button.set_label("Remove content warning") - self.content_warning = EditBox() - self.walker.insert(2, self.content_warning) - self.walker.insert(2, urwid.Text("Content warning")) - self.walker.insert(2, urwid.Divider()) - self.walker.set_focus(4) + def generate_list_items(self): + yield self.content_caption + yield self.content_edit + yield urwid.Divider() - def clear_error_message(self): - self.footer = None + if self.cw_edit: + yield self.cw_caption + yield self.cw_edit + yield urwid.Divider() + yield self.cw_remove_button + else: + yield self.cw_add_button + + yield self.visibility_button + yield self.post_button + yield self.cancel_button + + def refresh(self): + self.walker = urwid.SimpleListWalker(list(self.generate_list_items())) + self.listbox.body = self.walker + + def choose_visibility(self, *args): + list_items = [urwid.Text("Choose status visibility:")] + for visibility, caption, description in VISIBILITY_OPTIONS: + text = "{} - {}".format(caption, description) + button = Button(text, on_press=self.set_visibility, user_data=visibility) + list_items.append(button) + + self.walker = urwid.SimpleListWalker(list_items) + self.listbox.body = self.walker + + # Initially focus currently chosen visibility + focus_map = {v[0]: n + 1 for n, v in enumerate(VISIBILITY_OPTIONS)} + focus = focus_map.get(self.visibility, 1) + self.walker.set_focus(focus) + + def set_visibility(self, widget, visibility): + self.visibility = visibility + self.visibility_button.set_label("Visibility: {}".format(self.visibility)) + self.refresh() + self.walker.set_focus(7 if self.cw_edit else 4) + + def add_content_warning(self, button): + self.cw_edit = EditBox() + self.refresh() + self.walker.set_focus(4) + + def remove_content_warning(self, button): + self.cw_edit = None + self.refresh() + self.walker.set_focus(3) def set_error_message(self, msg): self.footer = urwid.Text(("footer_message_error", msg)) + def clear_error_message(self): + self.footer = None + def post(self, button): self.clear_error_message() # Don't lstrip content to avoid removing intentional leading whitespace # However, do strip both sides to check if there is any content there - content = self.content.edit_text.rstrip() + content = self.content_edit.edit_text.rstrip() content = None if not content.strip() else content - warning = (self.content_warning.edit_text.rstrip() - if self.content_warning else "") + warning = self.cw_edit.edit_text.rstrip() if self.cw_edit else "" warning = None if not warning.strip() else warning if not content: diff --git a/toot/tui/constants.py b/toot/tui/constants.py index 89e8bcf..6824667 100644 --- a/toot/tui/constants.py +++ b/toot/tui/constants.py @@ -30,3 +30,10 @@ PALETTE = [ ('yellow', 'yellow', ''), ('yellow_selected', 'yellow', 'dark blue'), ] + +VISIBILITY_OPTIONS = [ + ("public", "Public", "Post to public timelines"), + ("private", "Private", "Do not post to public timelines"), + ("unlisted", "Unlisted", "Post to followers only"), + ("direct", "Direct", "Post to mentioned users only"), +]