Skip to content

Commit 0ae1e06

Browse files
committed
buttons/core/views: Improvements
1 parent f0cd8bc commit 0ae1e06

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed

zulipterminal/core.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,27 @@ def report_warning(
490490
"""
491491
self.view.set_footer_text(text, "task:warning", duration)
492492

493-
def show_spoiler(self, content: str) -> None:
493+
def show_spoiler(
494+
self,
495+
content: str,
496+
message: Message,
497+
topic_links: Dict[str, Tuple[str, int, bool]],
498+
message_links: Dict[str, Tuple[str, int, bool]],
499+
time_mentions: List[Tuple[str, str]],
500+
spoilers: List[Tuple[int, List[Any], List[Any]]],
501+
) -> None:
494502
self.show_pop_up(
495-
SpoilerView(self, "Spoiler (up/down scrolls)", content), "area:msg"
503+
SpoilerView(
504+
self,
505+
"Spoiler (up/down scrolls)",
506+
content,
507+
message,
508+
topic_links,
509+
message_links,
510+
time_mentions,
511+
spoilers,
512+
),
513+
"area:msg",
496514
)
497515

498516
def show_media_confirmation_popup(

zulipterminal/ui_tools/buttons.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,20 @@ def __init__(
324324
header_len: int,
325325
header: List[Any],
326326
content: List[Any],
327+
message: Message,
328+
topic_links: Dict[str, Tuple[str, int, bool]],
329+
message_links: Dict[str, Tuple[str, int, bool]],
330+
time_mentions: List[Tuple[str, str]],
331+
spoilers: List[Tuple[int, List[Any], List[Any]]],
327332
display_attr: Optional[str],
328333
) -> None:
329334
self.controller = controller
330335
self.content = content
336+
self.message = message
337+
self.topic_links = topic_links
338+
self.message_links = message_links
339+
self.time_mentions = time_mentions
340+
self.spoilers = spoilers
331341

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

346356
def show_spoiler(self, *_: Any) -> None:
347-
self.controller.show_spoiler(self.content)
357+
self.controller.show_spoiler(
358+
self.content,
359+
self.message,
360+
self.topic_links,
361+
self.message_links,
362+
self.time_mentions,
363+
self.spoilers,
364+
)
348365

349366

350367
class TopicButton(TopButton):

zulipterminal/ui_tools/views.py

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,12 +1078,39 @@ def __init__(
10781078

10791079

10801080
class SpoilerView(PopUpView):
1081-
def __init__(self, controller: Any, title: str, content: str) -> None:
1081+
def __init__(
1082+
self,
1083+
controller: Any,
1084+
title: str,
1085+
content: str,
1086+
message: Message,
1087+
topic_links: Dict[str, Tuple[str, int, bool]],
1088+
message_links: Dict[str, Tuple[str, int, bool]],
1089+
time_mentions: List[Tuple[str, str]],
1090+
spoilers: List[Tuple[int, List[Any], List[Any]]],
1091+
) -> None:
1092+
self.message = message
1093+
self.topic_links = topic_links
1094+
self.message_links = message_links
1095+
self.time_mentions = time_mentions
1096+
self.spoilers = spoilers
10821097
width, _ = controller.maximum_popup_dimensions()
10831098
widget = [urwid.Text(content)]
10841099

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

1102+
def keypress(self, size: urwid_Size, key: str) -> str:
1103+
if is_command_key("GO_BACK", key):
1104+
self.controller.show_msg_info(
1105+
msg=self.message,
1106+
topic_links=self.topic_links,
1107+
message_links=self.message_links,
1108+
time_mentions=self.time_mentions,
1109+
spoilers=self.spoilers,
1110+
)
1111+
return key
1112+
return super().keypress(size, key)
1113+
10871114

10881115
class AboutView(PopUpView):
10891116
def __init__(
@@ -1687,14 +1714,9 @@ def __init__(
16871714
popup_width = max(popup_width, topic_link_width)
16881715

16891716
if spoilers:
1690-
spoiler_buttons = []
1691-
spoiler_width = 0
1692-
for index, (header_len, header, content) in enumerate(spoilers):
1693-
spoiler_width = max(header_len, spoiler_width)
1694-
display_attr = None if index % 2 else "popup_contrast"
1695-
spoiler_buttons.append(
1696-
SpoilerButton(controller, header_len, header, content, display_attr)
1697-
)
1717+
spoiler_buttons, spoiler_width = self.create_spoiler_buttons(
1718+
controller, spoilers
1719+
)
16981720

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

17361758
return link_widgets, link_width
17371759

1760+
def create_spoiler_buttons(
1761+
self, controller: Any, spoilers: List[Tuple[int, List[Any], List[Any]]]
1762+
) -> Tuple[List[SpoilerButton], int]:
1763+
spoiler_buttons = []
1764+
spoiler_width = 0
1765+
1766+
for index, (header_len, header, content) in enumerate(spoilers):
1767+
spoiler_width = max(header_len, spoiler_width)
1768+
1769+
display_attr = None if index % 2 else "popup_contrast"
1770+
1771+
processed_header = [f"{index+1}: "] + header
1772+
header_len = sum(
1773+
len(part[1]) if isinstance(part, tuple) else len(part)
1774+
for part in processed_header
1775+
)
1776+
1777+
spoiler_buttons.append(
1778+
SpoilerButton(
1779+
controller,
1780+
header_len,
1781+
processed_header,
1782+
header+["\n\n"]+content,
1783+
self.msg,
1784+
self.topic_links,
1785+
self.message_links,
1786+
self.time_mentions,
1787+
self.spoilers,
1788+
display_attr,
1789+
)
1790+
)
1791+
return spoiler_buttons, spoiler_width
1792+
17381793
def keypress(self, size: urwid_Size, key: str) -> str:
17391794
if is_command_key("EDIT_HISTORY", key) and self.show_edit_history_label:
17401795
self.controller.show_edit_history(

0 commit comments

Comments
 (0)