|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from math import ceil |
4 | | -import logging |
5 | 3 |
|
| 4 | +import logging |
6 | 5 |
|
| 6 | +from rich.repr import rich_repr, RichReprResult |
7 | 7 | from rich.color import Color |
8 | 8 | from rich.style import Style |
9 | 9 | from rich.console import Console, ConsoleOptions, RenderResult, RenderableType |
|
12 | 12 |
|
13 | 13 | log = logging.getLogger("rich") |
14 | 14 |
|
15 | | -from .widget import Widget |
| 15 | +from .widget import Reactive, Widget |
16 | 16 |
|
17 | 17 |
|
18 | | -class ScrollBar: |
| 18 | +class ScrollBarRender: |
19 | 19 | def __init__( |
20 | 20 | self, |
21 | 21 | virtual_size: int = 100, |
@@ -65,34 +65,39 @@ def render_bar( |
65 | 65 | _Segment = Segment |
66 | 66 | _Style = Style |
67 | 67 | blank = " " * width_thickness |
68 | | - segments = [_Segment(blank, _Style(bgcolor=back))] * int(size) |
69 | | - |
70 | | - step_size = virtual_size / size |
71 | | - |
72 | | - start = int(position / step_size * 8) |
73 | | - end = start + max(8, int(window_size / step_size * 8)) |
74 | | - |
75 | | - start_index, start_bar = divmod(start, 8) |
76 | | - end_index, end_bar = divmod(end, 8) |
77 | 68 |
|
78 | | - segments[start_index:end_index] = [_Segment(blank, _Style(bgcolor=bar))] * ( |
79 | | - end_index - start_index |
80 | | - ) |
81 | | - |
82 | | - if start_index < len(segments): |
83 | | - segments[start_index] = _Segment( |
84 | | - bars[7 - start_bar] * width_thickness, |
85 | | - _Style(bgcolor=back, color=bar) |
86 | | - if vertical |
87 | | - else _Style(bgcolor=bar, color=back), |
88 | | - ) |
89 | | - if end_index < len(segments): |
90 | | - segments[end_index] = _Segment( |
91 | | - bars[7 - end_bar] * width_thickness, |
92 | | - _Style(bgcolor=bar, color=back) |
93 | | - if vertical |
94 | | - else _Style(bgcolor=back, color=bar), |
95 | | - ) |
| 69 | + background_meta = {"background": True} |
| 70 | + foreground_meta = {"background": False} |
| 71 | + |
| 72 | + back_segment = Segment(blank, _Style(bgcolor=back, meta=background_meta)) |
| 73 | + segments = [back_segment] * int(size) |
| 74 | + if window_size and size and virtual_size: |
| 75 | + step_size = virtual_size / size |
| 76 | + |
| 77 | + start = int(position / step_size * 8) |
| 78 | + end = start + max(8, int(window_size / step_size * 8)) |
| 79 | + |
| 80 | + start_index, start_bar = divmod(start, 8) |
| 81 | + end_index, end_bar = divmod(end, 8) |
| 82 | + |
| 83 | + segments[start_index:end_index] = [ |
| 84 | + _Segment(blank, _Style(bgcolor=bar, meta=foreground_meta)) |
| 85 | + ] * (end_index - start_index) |
| 86 | + |
| 87 | + if start_index < len(segments): |
| 88 | + segments[start_index] = _Segment( |
| 89 | + bars[7 - start_bar] * width_thickness, |
| 90 | + _Style(bgcolor=back, color=bar, meta=foreground_meta) |
| 91 | + if vertical |
| 92 | + else _Style(bgcolor=bar, color=back, meta=foreground_meta), |
| 93 | + ) |
| 94 | + if end_index < len(segments): |
| 95 | + segments[end_index] = _Segment( |
| 96 | + bars[7 - end_bar] * width_thickness, |
| 97 | + _Style(bgcolor=bar, color=back, meta=foreground_meta) |
| 98 | + if vertical |
| 99 | + else _Style(bgcolor=back, color=bar, meta=foreground_meta), |
| 100 | + ) |
96 | 101 | if vertical: |
97 | 102 | return Segments(segments, new_lines=True) |
98 | 103 | else: |
@@ -127,11 +132,37 @@ def __rich_console__( |
127 | 132 | yield bar |
128 | 133 |
|
129 | 134 |
|
| 135 | +@rich_repr |
| 136 | +class ScrollBar(Widget): |
| 137 | + def __init__(self, vertical: bool = True, name: str | None = None) -> None: |
| 138 | + self.vertical = vertical |
| 139 | + super().__init__(name=name) |
| 140 | + |
| 141 | + virtual_size: Reactive[int] = Reactive(100) |
| 142 | + window_size: Reactive[int] = Reactive(20) |
| 143 | + position: Reactive[int] = Reactive(0) |
| 144 | + |
| 145 | + def __rich_repr__(self) -> RichReprResult: |
| 146 | + yield "virtual_size", self.virtual_size |
| 147 | + yield "window_size", self.window_size |
| 148 | + yield "position", self.position |
| 149 | + |
| 150 | + __rich_repr__.angular = True |
| 151 | + |
| 152 | + def render(self) -> RenderableType: |
| 153 | + return ScrollBarRender( |
| 154 | + virtual_size=self.virtual_size, |
| 155 | + window_size=self.window_size, |
| 156 | + position=self.position, |
| 157 | + vertical=self.vertical, |
| 158 | + ) |
| 159 | + |
| 160 | + |
130 | 161 | if __name__ == "__main__": |
131 | 162 | from rich.console import Console |
132 | 163 | from rich.segment import Segments |
133 | 164 |
|
134 | 165 | console = Console() |
135 | | - bar = ScrollBar() |
| 166 | + bar = ScrollBarRender() |
136 | 167 |
|
137 | | - console.print(ScrollBar(position=15.3, thickness=5, vertical=False)) |
| 168 | + console.print(ScrollBarRender(position=15.3, thickness=5, vertical=False)) |
0 commit comments