comparison libervia/web/pages/chat/_browser/__init__.py @ 1542:fb31d3dba0c3

browser (chat): add marker for new messages when page is not visible
author Goffi <goffi@goffi.org>
date Wed, 28 Jun 2023 10:09:15 +0200
parents 3c384b51b2a1
children 9ba532041a8e
comparison
equal deleted inserted replaced
1541:3c384b51b2a1 1542:fb31d3dba0c3
22 class LiberviaWebChat: 22 class LiberviaWebChat:
23 def __init__(self): 23 def __init__(self):
24 self.message_tpl = Template("chat/message.html") 24 self.message_tpl = Template("chat/message.html")
25 self.url_preview_control_tpl = Template("components/url_preview_control.html") 25 self.url_preview_control_tpl = Template("components/url_preview_control.html")
26 self.url_preview_tpl = Template("components/url_preview.html") 26 self.url_preview_tpl = Template("components/url_preview.html")
27 self.new_messages_marker_elt = Template("chat/new_messages_marker.html").get_elt()
27 28
28 self.messages_elt = document["messages"] 29 self.messages_elt = document["messages"]
29 30
30 # attachments 31 # attachments
31 self.file_uploader = FileUploader( 32 self.file_uploader = FileUploader(
124 subject_data: dict, 125 subject_data: dict,
125 mess_type: str, 126 mess_type: str,
126 extra: dict, 127 extra: dict,
127 profile: str, 128 profile: str,
128 ) -> None: 129 ) -> None:
129 log.info(f"on_message_new: [{from_jid} -> {to_jid}] {message_data}, {extra=}") 130 # FIXME: visibilityState doesn't detect OS events such as `Alt + Tab`, using focus
131 # event may help to get those use cases, but it gives false positives.
132 if document.visibilityState == "hidden" and self.new_messages_marker_elt.parent is None:
133 # the page is not visible, and we have no new messages marker yet, so we add
134 # it
135 self.messages_elt <= self.new_messages_marker_elt
130 xhtml_data = extra.get("xhtml") 136 xhtml_data = extra.get("xhtml")
131 if not xhtml_data: 137 if not xhtml_data:
132 xhtml = None 138 xhtml = None
133 else: 139 else:
134 try: 140 try:
337 def close_modal(self, evt): 343 def close_modal(self, evt):
338 modal = document.select_one("#modal") 344 modal = document.select_one("#modal")
339 modal.classList.remove("is-active") 345 modal.classList.remove("is-active")
340 346
341 347
348 def handle_visibility_change(self, evt):
349 if (
350 document.visibilityState == "hidden"
351 and self.new_messages_marker_elt.parent is not None
352 ):
353 # if there is a new messages marker, we remove it
354 self.new_messages_marker_elt.remove()
355
356
342 libervia_web_chat = LiberviaWebChat() 357 libervia_web_chat = LiberviaWebChat()
343 358
344 document["message_input"].bind( 359 document["message_input"].bind(
345 "input", lambda __: libervia_web_chat.auto_resize_message_input() 360 "input", lambda __: libervia_web_chat.auto_resize_message_input()
346 ) 361 )
347 document["message_input"].bind("keydown", libervia_web_chat.on_message_keydown) 362 document["message_input"].bind("keydown", libervia_web_chat.on_message_keydown)
348 document["send_button"].bind("click", lambda __: libervia_web_chat.send_message()) 363 document["send_button"].bind("click", lambda __: libervia_web_chat.send_message())
349 document["attach_button"].bind("click", libervia_web_chat.on_attach_button_click) 364 document["attach_button"].bind("click", libervia_web_chat.on_attach_button_click)
350 document["file_input"].bind("change", libervia_web_chat.on_file_selected) 365 document["file_input"].bind("change", libervia_web_chat.on_file_selected)
366
367 document.bind("visibilitychange", libervia_web_chat.handle_visibility_change)
368
351 bridge.register_signal("message_new", libervia_web_chat._on_message_new) 369 bridge.register_signal("message_new", libervia_web_chat._on_message_new)
352 370
353 libervia_web_chat.make_attachments_dynamic() 371 libervia_web_chat.make_attachments_dynamic()
354 libervia_web_chat.handle_url_previews() 372 libervia_web_chat.handle_url_previews()