@@ -666,20 +666,21 @@ def get_items(self) -> list:
666
666
def adjust_segment_radius (self , * args ) -> None :
667
667
"""Rounds off the first and last elements."""
668
668
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
683
684
684
685
def mark_item (self , segment_item : MDSegmentedButtonItem ) -> None :
685
686
"""Fired when a segment element is clicked (`on_release` event)."""
@@ -700,14 +701,28 @@ def add_widget(self, widget, *args, **kwargs):
700
701
widget ._segmented_button = self
701
702
widget .bind (on_release = self .mark_item )
702
703
self .ids .container .add_widget (widget )
703
- Clock . schedule_once ( self .adjust_segment_radius )
704
+ self .adjust_segment_radius ( )
704
705
elif isinstance (widget , MDSegmentedButtonContainer ):
705
706
return super ().add_widget (widget )
706
707
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
+
707
720
def _set_size_hint_min_x (
708
- self , widget : MDSegmentButtonLabel | MDSegmentButtonIcon
721
+ self , widget : MDSegmentButtonLabel | MDSegmentButtonIcon , sign : int = 1
709
722
):
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
+ )
711
726
712
727
713
728
class MDSegmentedButtonContainer (BoxLayout ):
0 commit comments