# HG changeset patch # User Goffi # Date 1597390292 -7200 # Node ID 683e50799d6d17ff05550de7951ae0a07a112a51 # Parent e35de70f529520202ba10889469e2e6db4f5db9b browser (dialog): new class to handle notifications diff -r e35de70f5295 -r 683e50799d6d libervia/pages/_browser/dialog.py --- a/libervia/pages/_browser/dialog.py Fri Aug 14 09:31:32 2020 +0200 +++ b/libervia/pages/_browser/dialog.py Fri Aug 14 09:31:32 2020 +0200 @@ -30,3 +30,35 @@ cancel_elt.bind("click", lambda evt: cancel_cb(evt, notif_elt)) for cancel_elt in notif_elt.select(".click_to_ok"): cancel_elt.bind("click", lambda evt: ok_cb(evt, notif_elt)) + + +class Notification: + + def __init__(self): + self._tpl = Template("dialogs/notification.html") + + def close(self, notif_elt): + notif_elt.classList.remove('state_appended') + notif_elt.bind("transitionend", lambda __: notif_elt.remove()) + + def show( + self, + message: str, + level: str = "info", + delay: int = 5 + ) -> None: + # we log in console error messages, may be useful + if level in ("warning", "error"): + print(f"[{level}] {message}") + notif_elt = self._tpl.get_elt({ + "message": message, + "level": level, + }) + document["notifs_area"] <= notif_elt + timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) + timer.set_timeout(lambda: self.close(notif_elt), delay * 1000) + for elt in notif_elt.select('.click_to_close'): + elt.bind('click', lambda __: self.close(notif_elt)) + + +notification = Notification()