Skip to content

Commit 8de9393

Browse files
committed
Fix #1690
1 parent 0c260bc commit 8de9393

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

Diff for: kivymd/uix/segmentedbutton/segmentedbutton.py

+32-17
Original file line numberDiff line numberDiff line change
@@ -666,20 +666,21 @@ def get_items(self) -> list:
666666
def adjust_segment_radius(self, *args) -> None:
667667
"""Rounds off the first and last elements."""
668668

669-
if self.ids.container.children[0].radius == [0, 0, 0, 0]:
670-
self.ids.container.children[0].radius = (
671-
0,
672-
self.height / 2,
673-
self.height / 2,
674-
0,
675-
)
676-
if self.ids.container.children[-1].radius == [0, 0, 0, 0]:
677-
self.ids.container.children[-1].radius = (
678-
self.height / 2,
679-
0,
680-
0,
681-
self.height / 2,
682-
)
669+
_rad = self.height / 2
670+
671+
_last_radius = [0, _rad, _rad, 0]
672+
_first_radius = [_rad, 0, 0, _rad]
673+
_optimal_radius = [0, 0, 0, 0]
674+
675+
_child_count = len(self.ids.container.children)
676+
677+
for count, child in enumerate(self.ids.container.children):
678+
if count == 0:
679+
child.radius = _last_radius
680+
elif count == _child_count - 1:
681+
child.radius = _first_radius
682+
else:
683+
child.radius = _optimal_radius
683684

684685
def mark_item(self, segment_item: MDSegmentedButtonItem) -> None:
685686
"""Fired when a segment element is clicked (`on_release` event)."""
@@ -700,14 +701,28 @@ def add_widget(self, widget, *args, **kwargs):
700701
widget._segmented_button = self
701702
widget.bind(on_release=self.mark_item)
702703
self.ids.container.add_widget(widget)
703-
Clock.schedule_once(self.adjust_segment_radius)
704+
self.adjust_segment_radius()
704705
elif isinstance(widget, MDSegmentedButtonContainer):
705706
return super().add_widget(widget)
706707

708+
def remove_widget(self, widget, *args, **kwargs):
709+
if isinstance(widget, MDSegmentedButtonItem):
710+
for child in widget.children[0].children:
711+
if isinstance(child, MDSegmentButtonLabel) or isinstance(
712+
child, MDSegmentButtonIcon
713+
):
714+
self._set_size_hint_min_x(child, sign=-1)
715+
self.ids.container.remove_widget(widget)
716+
self.adjust_segment_radius()
717+
elif isinstance(widget, MDSegmentedButtonContainer):
718+
return super().remove_widget(widget)
719+
707720
def _set_size_hint_min_x(
708-
self, widget: MDSegmentButtonLabel | MDSegmentButtonIcon
721+
self, widget: MDSegmentButtonLabel | MDSegmentButtonIcon, sign: int = 1
709722
):
710-
self.ids.container.size_hint_min_x += widget.texture_size[0] + dp(36)
723+
self.ids.container.size_hint_min_x += sign * (
724+
widget.texture_size[0] + dp(36)
725+
)
711726

712727

713728
class MDSegmentedButtonContainer(BoxLayout):

0 commit comments

Comments
 (0)