From e129c135900f8b47cab37e1ed5f494211e5cd730 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 18 Mar 2024 21:10:19 +0100 Subject: [PATCH] view: add way to redraw screen if the view is visible Views can now redraw themselves using the request_redraw_if_visible() method. Behind the scenes, it will look if the view is the currently visible view and skip the redraw otherwise. Signed-off-by: Olivier Gayot --- subiquitycore/view.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/subiquitycore/view.py b/subiquitycore/view.py index 993888d5..41acc6f5 100644 --- a/subiquitycore/view.py +++ b/subiquitycore/view.py @@ -19,6 +19,7 @@ Contains some default key navigations """ import asyncio +import contextlib import logging from urwid import Overlay, Text, emit_signal @@ -152,3 +153,16 @@ class BaseView(WidgetWrap): self.cancel() return None return key + + def is_visible(self) -> bool: + with contextlib.suppress(AttributeError): + return self.controller.app.ui.body is self + return False + + def request_redraw(self, *, only_if_visible=False) -> None: + if only_if_visible and not self.is_visible(): + return + self.controller.app.request_screen_redraw() + + def request_redraw_if_visible(self) -> None: + self.request_redraw(only_if_visible=True)