From 8f2fae5d1e0524b68d100b7202bfa45b3718dad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Fri, 21 Apr 2023 15:21:49 +0100 Subject: [PATCH] Watch percentage changes directly. [skip ci] --- src/textual/widgets/_progress_bar.py | 31 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/textual/widgets/_progress_bar.py b/src/textual/widgets/_progress_bar.py index 3d8bf22b0b..b152ef683e 100644 --- a/src/textual/widgets/_progress_bar.py +++ b/src/textual/widgets/_progress_bar.py @@ -2,6 +2,7 @@ from __future__ import annotations +from functools import partial from math import ceil from time import monotonic from typing import Optional @@ -250,11 +251,30 @@ def key_space(self): def compose(self) -> ComposeResult: with Horizontal(): if not self.hide_bar: - yield self._bar + bar = Bar() + # Notify the bar when the percentage is updated. + self.watch( + self, "percentage", partial(setattr, bar, "_completion_percentage") + ) + yield bar if not self.hide_percentage: - yield self._percentage_status + percentage_status = PercentageStatus() + # Notify the percentage status label when the percentage is updated. + self.watch( + self, + "percentage", + partial(setattr, percentage_status, "_completion_percentage"), + ) + yield percentage_status if not self.hide_eta: - yield self._eta_status + eta_status = ETAStatus() + # Notify the ETA status label when the percentage is updated. + self.watch( + self, + "percentage", + partial(setattr, eta_status, "_completion_percentage"), + ) + yield eta_status def validate_progress(self, progress: float) -> float: if self.total is not None: @@ -266,11 +286,6 @@ def compute_percentage(self) -> float | None: return self.progress / self.total return None - def watch_percentage(self, percentage: float | None) -> None: - self._bar._completion_percentage = percentage - self._percentage_status._completion_percentage = percentage - self._eta_status._completion_percentage = percentage - def advance(self, advance: float = 1) -> None: """Advance the progress of the progress bar by the given amount.