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 <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2024-03-18 21:10:19 +01:00
parent 020dcc0b88
commit e129c13590
1 changed files with 14 additions and 0 deletions

View File

@ -19,6 +19,7 @@ Contains some default key navigations
""" """
import asyncio import asyncio
import contextlib
import logging import logging
from urwid import Overlay, Text, emit_signal from urwid import Overlay, Text, emit_signal
@ -152,3 +153,16 @@ class BaseView(WidgetWrap):
self.cancel() self.cancel()
return None return None
return key 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)