changeset 1563:e3449beac8d8

browser (calls): Add clear search + formatting
author Goffi <goffi@goffi.org>
date Thu, 17 Aug 2023 16:23:01 +0200
parents 4afafce0c4c9
children bd3c880f4a47
files libervia/web/pages/calls/_browser/__init__.py libervia/web/pages/calls/_browser/webrtc.py
diffstat 2 files changed, 39 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/web/pages/calls/_browser/__init__.py	Thu Aug 17 14:59:01 2023 +0200
+++ b/libervia/web/pages/calls/_browser/__init__.py	Thu Aug 17 16:23:01 2023 +0200
@@ -14,7 +14,16 @@
 profile = window.profile or ""
 bridge = Bridge()
 GATHER_TIMEOUT = 10000
-ALLOWED_STATUSES = (None, "dialing", "ringing", "in-call", "on-hold", "connecting", "connection-lost", "reconnecting")
+ALLOWED_STATUSES = (
+    None,
+    "dialing",
+    "ringing",
+    "in-call",
+    "on-hold",
+    "connecting",
+    "connection-lost",
+    "reconnecting",
+)
 AUDIO = "audio"
 VIDEO = "video"
 ALLOWED_CALL_MODES = {AUDIO, VIDEO}
@@ -49,15 +58,9 @@
 
         # call/hang up buttons
         self._call_mode = VIDEO
-        document["video_call_btn"].bind(
-            "click",
-            lambda __: aio.run(self.make_call())
-
-        )
+        document["video_call_btn"].bind("click", lambda __: aio.run(self.make_call()))
         document["audio_call_btn"].bind(
-            "click",
-            lambda __: aio.run(self.make_call(video=False))
-
+            "click", lambda __: aio.run(self.make_call(video=False))
         )
         document["hangup_btn"].bind("click", lambda __: aio.run(self.hang_up()))
 
@@ -77,8 +80,9 @@
             self.share_desktop_btn_elt.classList.add("is-hidden")
 
         # search
+        self.search_elt = document["search"]
         self.jid_search = JidSearch(
-            document["search"],
+            self.search_elt,
             document["contacts"],
             click_cb=self._on_entity_click,
             template="call/search_item.html",
@@ -94,9 +98,10 @@
                     ".click-to-audio": lambda evt, item: aio.run(
                         self.on_entity_action(evt, AUDIO, item)
                     ),
-                }
-            }
+                },
+            },
         )
+        document["clear_search_btn"].bind("click", self.on_clear_search)
 
         # incoming call dialog
         self.incoming_call_dialog_elt = None
@@ -151,7 +156,7 @@
         else:
             raise ValueError("Invalid call mode")
 
-    def set_avatar(self, entity_jid: JID|str) -> None:
+    def set_avatar(self, entity_jid: JID | str) -> None:
         """Set the avatar element from entity_jid
 
         @param entity_jid: bare jid of the entity
@@ -312,7 +317,7 @@
         """
         self.call_mode = VIDEO if video else AUDIO
         try:
-            callee_jid = JID(document["search"].value.strip())
+            callee_jid = JID(self.search_elt.value.strip())
             if not callee_jid.is_valid:
                 raise ValueError
         except ValueError:
@@ -431,11 +436,23 @@
         else:
             log.error(f"Internal Error: Unknown call mode: {mode}")
 
+    def on_clear_search(self, ev) -> None:
+        """Clear the search input and trigger its 'input' event.
+
+        @param ev: the event object from the button click.
+        """
+        if not self.search_elt.value:
+            return
+        # clear the search field
+        self.search_elt.value = ''
+        # and dispatch the input event so items are updated
+        self.search_elt.dispatchEvent(window.Event.new("input"))
+
     def toggle_fullscreen(self, fullscreen: bool | None = None):
         """Toggle fullscreen mode for video elements.
 
         @param fullscreen: if set, determine the fullscreen state; otherwise,
-        the fullscreen mode will be toggled.
+            the fullscreen mode will be toggled.
         """
         do_fullscreen = (
             document.fullscreenElement is None if fullscreen is None else fullscreen
@@ -506,7 +523,7 @@
 
     async def on_entity_click(self, item: dict) -> None:
         """Set entity JID to search bar, and start the call"""
-        document["search"].value = item["entity"]
+        self.search_elt.value = item["entity"]
 
         await self.make_call()
 
@@ -516,10 +533,10 @@
         if action == "menu":
             evt.currentTarget.parent.classList.toggle("is-active")
         elif action in (VIDEO, AUDIO):
-            document["search"].value = item["entity"]
+            self.search_elt.value = item["entity"]
             # we want the dropdown to be inactive
             evt.currentTarget.closest(".dropdown").classList.remove("is-active")
-            await self.make_call(video=action==VIDEO)
+            await self.make_call(video=action == VIDEO)
 
 
 CallUI()
--- a/libervia/web/pages/calls/_browser/webrtc.py	Thu Aug 17 14:59:01 2023 +0200
+++ b/libervia/web/pages/calls/_browser/webrtc.py	Thu Aug 17 16:23:01 2023 +0200
@@ -199,7 +199,9 @@
             if self.on_connection_established_cb is not None:
                 self.on_connection_established_cb()
         elif state == "failed":
-            log.error("ICE connection failed. Check network connectivity and ICE configurations.")
+            log.error(
+                "ICE connection failed. Check network connectivity and ICE configurations."
+            )
         elif state == "disconnected":
             log.warning("ICE connection was disconnected.")
             if self.on_connection_lost_cb is not None:
@@ -267,8 +269,7 @@
         peer_connection.addEventListener("track", self.on_track)
         peer_connection.addEventListener("negotiationneeded", self.on_negotiation_needed)
         peer_connection.addEventListener(
-            "iceconnectionstatechange",
-            self.on_ice_connection_state_change
+            "iceconnectionstatechange", self.on_ice_connection_state_change
         )
         peer_connection.addEventListener("icecandidate", self.on_ice_candidate)
         peer_connection.addEventListener("icecandidateerror", self.on_ice_candidate_error)