Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add style variations to the progress bar widget #3143

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/examples/widgets/progress_bar_styled_rainbow.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Bar > .bar--indeterminate {
color: $primary;
background: $secondary;
}

Bar > .bar--bar {
color: blue;
background: $primary 30%;
}

Bar > .bar--complete {
color: red;
}

PercentageStatus {
text-style: reverse;
color: $secondary;
}

ETAStatus {
text-style: underline;
}
38 changes: 38 additions & 0 deletions docs/examples/widgets/progress_bar_styled_rainbow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from textual.app import App, ComposeResult
from textual.containers import Center, Middle
from textual.timer import Timer
from textual.widgets import Footer, ProgressBar


class StyledExtProgressBar(App[None]):
BINDINGS = [
("s", "start", "Start"),
]
CSS_PATH = "progress_bar_styled_rainbow.css"

progress_timer: Timer
"""Timer to simulate progress happening."""

def compose(self) -> ComposeResult:
with Center():
with Middle():
yield ProgressBar(color_scheme="rainbow")
yield Footer()

def on_mount(self) -> None:
"""Set up a timer to simulate progess happening."""
self.progress_timer = self.set_interval(1 / 10, self.make_progress, pause=True)

def make_progress(self) -> None:
"""Called automatically to advance the progress bar."""
self.query_one(ProgressBar).advance(1)

def action_start(self) -> None:
"""Start the progress tracking."""
self.query_one(ProgressBar).update(total=100)
self.progress_timer.resume()


if __name__ == "__main__":
app = StyledExtProgressBar()
app.run()
52 changes: 52 additions & 0 deletions docs/examples/widgets/progress_bar_styled_rainbow_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from textual.app import App, ComposeResult
from textual.containers import Center, Middle
from textual.timer import Timer
from textual.widgets import Footer, ProgressBar


class StyledExtProgressBar(App[None]):
BINDINGS = [
("s", "start", "Start"),
]
CSS_PATH = "progress_bar_styled_rainbow.css"

progress_timer: Timer
"""Timer to simulate progress happening."""

def compose(self) -> ComposeResult:
with Center():
with Middle():
yield ProgressBar(color_scheme="rainbow")
yield Footer()

def on_mount(self) -> None:
"""Set up a timer to simulate progess happening."""
self.progress_timer = self.set_interval(1 / 10, self.make_progress, pause=True)

def make_progress(self) -> None:
"""Called automatically to advance the progress bar."""
self.query_one(ProgressBar).advance(1)

def action_start(self) -> None:
"""Start the progress tracking."""
self.query_one(ProgressBar).update(total=100)
self.progress_timer.resume()

def key_1(self) -> None:
self._action_common_keypress(10)

def key_5(self) -> None:
self._action_common_keypress(50)

def key_9(self) -> None:
self._action_common_keypress(90)

def _action_common_keypress(self, progress: int) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit of a nit-pick, but given that this is intended for the docs, I think I'd be tempted to write this as slightly more idiomatic Textual code, using BINDINGS and calling on the action.

# Freeze time for the indeterminate progress bar.
self.query_one(ProgressBar).query_one("#eta")._get_elapsed_time = lambda: 0
self.query_one(ProgressBar).update(total=100, progress=progress)
self.progress_timer.pause()

if __name__ == "__main__":
app = StyledExtProgressBar()
app.run()
38 changes: 38 additions & 0 deletions docs/examples/widgets/progress_bar_styled_thickness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from textual.app import App, ComposeResult
from textual.containers import Center, Middle
from textual.timer import Timer
from textual.widgets import Footer, ProgressBar


class StyledExtProgressBar(App[None]):
BINDINGS = [
("s", "start", "Start"),
]
CSS_PATH = "progress_bar_styled.css"

progress_timer: Timer
"""Timer to simulate progress happening."""

def compose(self) -> ComposeResult:
with Center():
with Middle():
yield ProgressBar(thickness=2)
yield Footer()

def on_mount(self) -> None:
"""Set up a timer to simulate progess happening."""
self.progress_timer = self.set_interval(1 / 10, self.make_progress, pause=True)

def make_progress(self) -> None:
"""Called automatically to advance the progress bar."""
self.query_one(ProgressBar).advance(1)

def action_start(self) -> None:
"""Start the progress tracking."""
self.query_one(ProgressBar).update(total=100)
self.progress_timer.resume()


if __name__ == "__main__":
app = StyledExtProgressBar()
app.run()
53 changes: 53 additions & 0 deletions docs/examples/widgets/progress_bar_styled_thickness_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from textual.app import App, ComposeResult
from textual.containers import Center, Middle
from textual.timer import Timer
from textual.widgets import Footer, ProgressBar


class StyledExtProgressBar(App[None]):
BINDINGS = [
("s", "start", "Start"),
]
CSS_PATH = "progress_bar_styled.css"

progress_timer: Timer
"""Timer to simulate progress happening."""

def compose(self) -> ComposeResult:
with Center():
with Middle():
yield ProgressBar()
yield Footer()

def on_mount(self) -> None:
"""Set up a timer to simulate progess happening."""
self.progress_timer = self.set_interval(1 / 10, self.make_progress, pause=True)

def make_progress(self) -> None:
"""Called automatically to advance the progress bar."""
self.query_one(ProgressBar).advance(1)

def action_start(self) -> None:
"""Start the progress tracking."""
self.query_one(ProgressBar).update(total=100)
self.progress_timer.resume()

def key_0(self) -> None:
self._action_common_keypress(0)

def key_1(self) -> None:
self._action_common_keypress(1)

def key_2(self) -> None:
self._action_common_keypress(2)

def _action_common_keypress(self, thickness: int) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in another file: I think I'd be tempted to do this with BINDINGS and the action, it feels like it would be more "normal" for Textual code.

# Freeze time for the indeterminate progress bar.
self.query_one(ProgressBar).query_one("#eta")._get_elapsed_time = lambda: 0
self.query_one(ProgressBar).query_one("#bar").thickness = thickness
self.query_one(ProgressBar).update(total=100, progress=50)
self.progress_timer.pause()

if __name__ == "__main__":
app = StyledExtProgressBar()
app.run()
71 changes: 67 additions & 4 deletions docs/widgets/progress_bar.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ It shows the progress bar in:

=== "39% done"

```{.textual path="docs/examples/widgets/progress_bar_isolated_.py" press="t"}
```{.textual path="docs/examples/widgets/progress_bar_styled_.py" press="t"}
rodrigogiraoserrao marked this conversation as resolved.
Show resolved Hide resolved
davep marked this conversation as resolved.
Show resolved Hide resolved
```

=== "Completed"
Expand Down Expand Up @@ -104,16 +104,79 @@ Refer to the [section below](#styling-the-progress-bar) for more information.
--8<-- "docs/examples/widgets/progress_bar_styled.tcss"
```

### Rainbow Color Schemes

The rainbow color scheme gradually changes the color of the bar as it progresses.
The initial color is defined by the component class `bar--bar` and the final color by the component class `bar--complete`.
(The gradient is computed in the HSL color space in the counterclockwise direction.)

=== "Rainbow at 10%"

```{.textual path="docs/examples/widgets/progress_bar_styled_rainbow_.py" press="1"}
davep marked this conversation as resolved.
Show resolved Hide resolved
```

=== "Rainbow at 50%"

```{.textual path="docs/examples/widgets/progress_bar_styled_rainbow_.py" press="5"}
davep marked this conversation as resolved.
Show resolved Hide resolved
```

=== "Rainbow at 90%"

```{.textual path="docs/examples/widgets/progress_bar_styled_rainbow_.py" press="9"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the trailing _ in the filename here?

```
=== "progress_bar_styled_rainbow.py"

```python
--8<-- "docs/examples/widgets/progress_bar_styled_rainbow.py"
```

=== "progress_bar_styled.css"

```sass
--8<-- "docs/examples/widgets/progress_bar_styled_rainbow.css"
rodrigogiraoserrao marked this conversation as resolved.
Show resolved Hide resolved
```

### Progress Bar Thickness Styling

This shows a progress bar with custom bar thickness: thin, normal or thick.

=== "Thin"

```{.textual path="docs/examples/widgets/progress_bar_styled_thickness_.py" press="0"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the trailing _ in the filename here?

```

=== "Normal"

```{.textual path="docs/examples/widgets/progress_bar_styled_thickness_.py" press="1"}
davep marked this conversation as resolved.
Show resolved Hide resolved
```

=== "Thick"

```{.textual path="docs/examples/widgets/progress_bar_styled_thickness_.py" press="2"}
```
=== "progress_bar_styled_thickness.py"

```python
--8<-- "docs/examples/widgets/progress_bar_styled_thickness.py"
```

=== "progress_bar_styled.css"

```sass
--8<-- "docs/examples/widgets/progress_bar_styled.css"
```

## Styling the Progress Bar

The progress bar is composed of three sub-widgets that can be styled independently:

| Widget name | ID | Description |
| ------------------ | ------------- | ---------------------------------------------------------------- |
| `Bar` | `#bar` | The bar that visually represents the progress made. |
| `Bar` | `#bar` | Bar that visually represents the progress made. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a specific reason for changing this? I feel it reads better before the change.

| `PercentageStatus` | `#percentage` | [Label](./label.md) that shows the percentage of completion. |
| `ETAStatus` | `#eta` | [Label](./label.md) that shows the estimated time to completion. |


### Bar Component Classes

::: textual.widgets._progress_bar.Bar.COMPONENT_CLASSES
Expand All @@ -125,9 +188,9 @@ The progress bar is composed of three sub-widgets that can be styled independent

| Name | Type | Default | Description |
| ------------ | ------- | ------- | ------------------------------------------------------------------------------------------------------- |
| `percentage` | `float | None` | The read-only percentage of progress that has been made. This is `None` if the `total` hasn't been set. |
| `percentage` | `float` | `None` | The read-only percentage of progress that has been made. This is `None` if the `total` hasn't been set. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type looks to be wrong here, if percentage can be None then the type should be float | None.

| `progress` | `float` | `0` | The number of steps of progress already made. |
| `total` | `float | None` | The total number of steps that we are keeping track of. |
| `total` | `float` | `None` | The total number of steps that we are keeping track of. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type looks wrong here, of total can be None then the type should be float | None.


## Messages

Expand Down
47 changes: 47 additions & 0 deletions src/textual/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,53 @@ def blend(
new_alpha,
)

@lru_cache(maxsize=1024)
def hsl_blend(
self, destination: Color, factor: float, alpha: float | None = None
) -> Color:
"""Generate a new color between two colors in the HSL color space.

This method calculates a new color on a gradient using the HSL color space.
The position on the gradient is given by `factor`, which is a float between -1 and 1, where 0 is the original color, and 1 or -1 is the `destination` color.
The sign of `factor` affects the direction of the hue angle, a positive number is in the clockwise direction, a negative number is in the counter-clockwise direction.
For lightness and saturation, only the absolute value of `factor` is used.
A value of `factor` between the two extremes produces a color somewhere between the two end points.

Args:
destination: Another color.
factor: A blend factor, -1 -> 1.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick: I'd spell out that it's "between -1 and 1", and perhaps make clear if it's exclusive or inclusive.

alpha: New alpha for result.

Returns:
A new color.
"""
abs_factor = abs(factor)
if factor == 0:
return self
elif abs_factor >= 1:
return destination

hsl_1 = self.hsl
a1 = self.a
hsl_2 = destination.hsl
a2 = destination.a

if alpha is None:
new_alpha = a1 + (a2 - a1) * abs_factor
else:
new_alpha = alpha

sign = 1 if factor > 0 else -1
if (sign * hsl_1.h) <= (sign * hsl_2.h):
new_h = hsl_1.h + (hsl_2.h - hsl_1.h) * abs_factor
else:
new_h = (hsl_1.h + (hsl_2.h + sign - hsl_1.h) * abs_factor) % 1

new_s = hsl_1.s + (hsl_2.s - hsl_1.s) * abs_factor
new_l = hsl_1.l + (hsl_2.l - hsl_1.l) * abs_factor

return Color.from_hsl(new_h, new_s, new_l).with_alpha(new_alpha)

def __add__(self, other: object) -> Color:
if isinstance(other, Color):
return self.blend(other, other.a, 1.0)
Expand Down
Loading
Loading