Add option to set status visibility
This commit is contained in:
parent
4f30c177d6
commit
9aadec6cfb
|
@ -1,6 +1,8 @@
|
||||||
import urwid
|
import urwid
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from .constants import VISIBILITY_OPTIONS
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,59 +24,102 @@ class Button(urwid.AttrWrap):
|
||||||
|
|
||||||
|
|
||||||
class StatusComposer(urwid.Frame):
|
class StatusComposer(urwid.Frame):
|
||||||
|
"""
|
||||||
|
UI for compose and posting a status message.
|
||||||
|
"""
|
||||||
signals = ["close", "post"]
|
signals = ["close", "post"]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# This can be added by button press
|
self.content_caption = urwid.Text("Status message")
|
||||||
self.content = EditBox()
|
self.content_edit = EditBox()
|
||||||
self.content_warning = None
|
|
||||||
self.cw_button = Button("Add content warning", on_press=self.toggle_cw)
|
|
||||||
|
|
||||||
contents = [
|
self.cw_caption = urwid.Text("Content warning")
|
||||||
urwid.Text("Status message"),
|
self.cw_edit = None
|
||||||
self.content,
|
self.cw_add_button = Button("Add content warning",
|
||||||
urwid.Divider(),
|
on_press=self.add_content_warning)
|
||||||
self.cw_button,
|
self.cw_remove_button = Button("Remove content warning",
|
||||||
Button("Post", on_press=self.post),
|
on_press=self.remove_content_warning)
|
||||||
Button("Cancel", on_press=self.close),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
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.walker = urwid.SimpleListWalker(contents)
|
||||||
self.listbox = urwid.ListBox(self.walker)
|
self.listbox = urwid.ListBox(self.walker)
|
||||||
return super().__init__(self.listbox)
|
return super().__init__(self.listbox)
|
||||||
|
|
||||||
def toggle_cw(self, button):
|
def generate_list_items(self):
|
||||||
if self.content_warning:
|
yield self.content_caption
|
||||||
self.cw_button.set_label("Add content warning")
|
yield self.content_edit
|
||||||
self.walker.pop(2)
|
yield urwid.Divider()
|
||||||
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 clear_error_message(self):
|
if self.cw_edit:
|
||||||
self.footer = None
|
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):
|
def set_error_message(self, msg):
|
||||||
self.footer = urwid.Text(("footer_message_error", msg))
|
self.footer = urwid.Text(("footer_message_error", msg))
|
||||||
|
|
||||||
|
def clear_error_message(self):
|
||||||
|
self.footer = None
|
||||||
|
|
||||||
def post(self, button):
|
def post(self, button):
|
||||||
self.clear_error_message()
|
self.clear_error_message()
|
||||||
|
|
||||||
# Don't lstrip content to avoid removing intentional leading whitespace
|
# Don't lstrip content to avoid removing intentional leading whitespace
|
||||||
# However, do strip both sides to check if there is any content there
|
# 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
|
content = None if not content.strip() else content
|
||||||
|
|
||||||
warning = (self.content_warning.edit_text.rstrip()
|
warning = self.cw_edit.edit_text.rstrip() if self.cw_edit else ""
|
||||||
if self.content_warning else "")
|
|
||||||
warning = None if not warning.strip() else warning
|
warning = None if not warning.strip() else warning
|
||||||
|
|
||||||
if not content:
|
if not content:
|
||||||
|
|
|
@ -30,3 +30,10 @@ PALETTE = [
|
||||||
('yellow', 'yellow', ''),
|
('yellow', 'yellow', ''),
|
||||||
('yellow_selected', 'yellow', 'dark blue'),
|
('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"),
|
||||||
|
]
|
||||||
|
|
Loading…
Reference in New Issue