Skip to content

Commit 2313cd8

Browse files
A few improvements to the Checkbox implementation.
- Hide the scrollbar for a single checkbox. - Added a "checked" setter to the checkbox.
1 parent 77a8477 commit 2313cd8

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

prompt_toolkit/widgets/base.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@
5959
from prompt_toolkit.layout.dimension import AnyDimension
6060
from prompt_toolkit.layout.dimension import Dimension as D
6161
from prompt_toolkit.layout.dimension import to_dimension
62-
from prompt_toolkit.layout.margins import NumberedMargin, ScrollbarMargin
62+
from prompt_toolkit.layout.margins import (
63+
ConditionalMargin,
64+
NumberedMargin,
65+
ScrollbarMargin,
66+
)
6367
from prompt_toolkit.layout.processors import (
6468
AppendAutoSuggestion,
6569
BeforeInput,
@@ -639,6 +643,7 @@ class _DialogList(Generic[_T]):
639643
selected_style: str = ""
640644
checked_style: str = ""
641645
multiple_selection: bool = False
646+
show_scrollbar: bool = True
642647

643648
def __init__(self, values: Sequence[Tuple[_T, AnyFormattedText]]) -> None:
644649
assert len(values) > 0
@@ -702,7 +707,12 @@ def _find(event: E) -> None:
702707
self.window = Window(
703708
content=self.control,
704709
style=self.container_style,
705-
right_margins=[ScrollbarMargin(display_arrows=True),],
710+
right_margins=[
711+
ConditionalMargin(
712+
margin=ScrollbarMargin(display_arrows=True),
713+
filter=Condition(lambda: self.show_scrollbar),
714+
),
715+
],
706716
dont_extend_height=True,
707717
)
708718

@@ -804,14 +814,24 @@ class Checkbox(CheckboxList[str]):
804814
:param text: the text
805815
"""
806816

807-
def __init__(self, text: AnyFormattedText = "") -> None:
817+
show_scrollbar = False
818+
819+
def __init__(self, text: AnyFormattedText = "", checked: bool = False) -> None:
808820
values = [("value", text)]
809821
CheckboxList.__init__(self, values)
822+
self.checked = checked
810823

811824
@property
812825
def checked(self) -> bool:
813826
return "value" in self.current_values
814827

828+
@checked.setter
829+
def checked(self, value: bool) -> None:
830+
if value:
831+
self.current_values = ["value"]
832+
else:
833+
self.current_values = []
834+
815835

816836
class VerticalLine(object):
817837
"""

0 commit comments

Comments
 (0)