Add option to set status visibility

This commit is contained in:
Ivan Habunek 2019-08-27 14:16:14 +02:00
parent 4f30c177d6
commit 9aadec6cfb
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
2 changed files with 84 additions and 32 deletions

View File

@ -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:

View File

@ -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"),
]