Skip to content

Commit

Permalink
buttons/core/views: Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rsashank committed Jul 2, 2024
1 parent f0cd8bc commit 0ae1e06
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 12 deletions.
22 changes: 20 additions & 2 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,27 @@ def report_warning(
"""
self.view.set_footer_text(text, "task:warning", duration)

def show_spoiler(self, content: str) -> None:
def show_spoiler(
self,
content: str,
message: Message,
topic_links: Dict[str, Tuple[str, int, bool]],
message_links: Dict[str, Tuple[str, int, bool]],
time_mentions: List[Tuple[str, str]],
spoilers: List[Tuple[int, List[Any], List[Any]]],
) -> None:
self.show_pop_up(
SpoilerView(self, "Spoiler (up/down scrolls)", content), "area:msg"
SpoilerView(
self,
"Spoiler (up/down scrolls)",
content,
message,
topic_links,
message_links,
time_mentions,
spoilers,
),
"area:msg",
)

def show_media_confirmation_popup(
Expand Down
19 changes: 18 additions & 1 deletion zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,20 @@ def __init__(
header_len: int,
header: List[Any],
content: List[Any],
message: Message,
topic_links: Dict[str, Tuple[str, int, bool]],
message_links: Dict[str, Tuple[str, int, bool]],
time_mentions: List[Tuple[str, str]],
spoilers: List[Tuple[int, List[Any], List[Any]]],
display_attr: Optional[str],
) -> None:
self.controller = controller
self.content = content
self.message = message
self.topic_links = topic_links
self.message_links = message_links
self.time_mentions = time_mentions
self.spoilers = spoilers

super().__init__("")
self.update_widget(header_len, header, display_attr)
Expand All @@ -344,7 +354,14 @@ def update_widget(
self._w = urwid.AttrMap(icon, display_attr, focus_map="selected")

def show_spoiler(self, *_: Any) -> None:
self.controller.show_spoiler(self.content)
self.controller.show_spoiler(
self.content,
self.message,
self.topic_links,
self.message_links,
self.time_mentions,
self.spoilers,
)


class TopicButton(TopButton):
Expand Down
73 changes: 64 additions & 9 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,12 +1078,39 @@ def __init__(


class SpoilerView(PopUpView):
def __init__(self, controller: Any, title: str, content: str) -> None:
def __init__(
self,
controller: Any,
title: str,
content: str,
message: Message,
topic_links: Dict[str, Tuple[str, int, bool]],
message_links: Dict[str, Tuple[str, int, bool]],
time_mentions: List[Tuple[str, str]],
spoilers: List[Tuple[int, List[Any], List[Any]]],
) -> None:
self.message = message
self.topic_links = topic_links
self.message_links = message_links
self.time_mentions = time_mentions
self.spoilers = spoilers
width, _ = controller.maximum_popup_dimensions()
widget = [urwid.Text(content)]

super().__init__(controller, widget, "ENTER", width, title)

def keypress(self, size: urwid_Size, key: str) -> str:
if is_command_key("GO_BACK", key):
self.controller.show_msg_info(
msg=self.message,
topic_links=self.topic_links,
message_links=self.message_links,
time_mentions=self.time_mentions,
spoilers=self.spoilers,
)
return key
return super().keypress(size, key)


class AboutView(PopUpView):
def __init__(
Expand Down Expand Up @@ -1687,14 +1714,9 @@ def __init__(
popup_width = max(popup_width, topic_link_width)

if spoilers:
spoiler_buttons = []
spoiler_width = 0
for index, (header_len, header, content) in enumerate(spoilers):
spoiler_width = max(header_len, spoiler_width)
display_attr = None if index % 2 else "popup_contrast"
spoiler_buttons.append(
SpoilerButton(controller, header_len, header, content, display_attr)
)
spoiler_buttons, spoiler_width = self.create_spoiler_buttons(
controller, spoilers
)

# slice_index = Number of labels before message links + 1 newline
# + 1 'Spoilers' category label.
Expand Down Expand Up @@ -1735,6 +1757,39 @@ def create_link_buttons(

return link_widgets, link_width

def create_spoiler_buttons(
self, controller: Any, spoilers: List[Tuple[int, List[Any], List[Any]]]
) -> Tuple[List[SpoilerButton], int]:
spoiler_buttons = []
spoiler_width = 0

for index, (header_len, header, content) in enumerate(spoilers):
spoiler_width = max(header_len, spoiler_width)

display_attr = None if index % 2 else "popup_contrast"

processed_header = [f"{index+1}: "] + header
header_len = sum(
len(part[1]) if isinstance(part, tuple) else len(part)
for part in processed_header
)

spoiler_buttons.append(
SpoilerButton(
controller,
header_len,
processed_header,
header+["\n\n"]+content,
self.msg,
self.topic_links,
self.message_links,
self.time_mentions,
self.spoilers,
display_attr,
)
)
return spoiler_buttons, spoiler_width

def keypress(self, size: urwid_Size, key: str) -> str:
if is_command_key("EDIT_HISTORY", key) and self.show_edit_history_label:
self.controller.show_edit_history(
Expand Down

0 comments on commit 0ae1e06

Please sign in to comment.