Skip to content

Commit b2defda

Browse files
committed
model/views: Support event-triggered callback from views.
This commit adds two new functions in model.py: - register_callback(): That registers callbacks for UI elements and stores them in model.callbacks data-structure. - process_callback(): That processes the corresponding callback that are already registered after getting triggered from events. These combined allow us to dynamically update the checkboxes in stream_info popup after receiving events. Fixes #747.
1 parent 4cf1215 commit b2defda

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

zulipterminal/model.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ def __init__(self, controller: Any) -> None:
9898

9999
self._notified_user_of_notification_failure = False
100100

101+
self.callbacks: Dict[str, Any] = {}
102+
101103
# Events fetched once at startup
102104
self.initial_data_to_fetch: List[str] = [
103105
"realm",
@@ -178,6 +180,28 @@ def __init__(self, controller: Any) -> None:
178180
self.new_user_input = True
179181
self._start_presence_updates()
180182

183+
def register_callback(
184+
self, obj: Any, event: str, callback: Callable[[Any], None]
185+
) -> None:
186+
"""
187+
Registers a callback from UI elements that needs
188+
to be triggered on events.
189+
"""
190+
if event not in self.callbacks:
191+
self.callbacks[event] = {}
192+
self.callbacks[event] = (obj, callback)
193+
194+
def process_callback(self, event: str, stream_id: Optional[int]) -> None:
195+
"""
196+
This function processes and calls the callback function that
197+
was registered in self.callbacks and is recently triggered.
198+
"""
199+
if event in self.callbacks and event == "stream_info_open":
200+
caller = self.callbacks[event]
201+
if caller[0].stream_id == stream_id:
202+
callback = caller[1]
203+
callback()
204+
181205
def get_focus_in_current_narrow(self) -> Union[int, Set[None]]:
182206
"""
183207
Returns the focus in the current narrow.
@@ -954,7 +978,6 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int) -> StreamData:
954978
self.muted_streams.add(stream_id)
955979
self.unread_counts["all_msg"] -= unread_count
956980
stream_button.mark_muted()
957-
self.controller.update_screen()
958981
elif event.get("property", None) == "pin_to_top":
959982
stream_id = event["stream_id"]
960983

@@ -974,7 +997,8 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int) -> StreamData:
974997
sort_streams(self.unpinned_streams)
975998
sort_streams(self.pinned_streams)
976999
self.controller.view.left_panel.update_stream_view()
977-
self.controller.update_screen()
1000+
self.process_callback("stream_info_open", event.get("stream_id", None))
1001+
self.controller.update_screen()
9781002
elif event["op"] in ("peer_add", "peer_remove"):
9791003
# NOTE: ZFL 35 commit was not atomic with API change
9801004
# (ZFL >=35 can use new plural style)

zulipterminal/ui_tools/views.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,10 @@ def __init__(self, controller: Any, stream_id: int) -> None:
12421242

12431243
self.widgets.append(muted_setting)
12441244
self.widgets.append(pinned_setting)
1245+
1246+
self.controller.model.register_callback(
1247+
self, "stream_info_open", self.update_checkboxes_from_events
1248+
)
12451249
super().__init__(controller, self.widgets, "STREAM_DESC", popup_width, title)
12461250

12471251
def toggle_mute_status(self, button: Any, new_state: bool) -> None:

0 commit comments

Comments
 (0)