@@ -729,6 +729,12 @@ class MDTopAppBar(
729
729
_trailing_button_container = ObjectProperty ()
730
730
_leading_button_container = ObjectProperty ()
731
731
_appbar_title = ObjectProperty ()
732
+ _right_padding = NumericProperty (0 )
733
+ _left_padding = NumericProperty (0 )
734
+
735
+ def __init__ (self , * args , ** kwargs ):
736
+ super ().__init__ (* args , ** kwargs )
737
+ Clock .schedule_once (lambda x : self .on_size (self , (0 , 0 )))
732
738
733
739
def on_type (self , instance , value ) -> None :
734
740
def on_type (* args ):
@@ -745,16 +751,17 @@ def on_size(self, instance, size) -> None:
745
751
self ._appbar_title ._title_width = (
746
752
self ._appbar_title .texture_size [0 ]
747
753
)
748
- Clock . schedule_once (
749
- lambda x : self ._appbar_title . on_pos_hint (
750
- self ._appbar_title , self . _appbar_title . pos_hint
751
- )
754
+ self . _right_padding = 0
755
+ self ._left_padding = 0
756
+ self ._appbar_title . on_pos_hint (
757
+ self . _appbar_title , self . _appbar_title . pos_hint
752
758
)
753
759
754
760
def add_widget (self , widget , * args , ** kwargs ):
755
761
if isinstance (widget , MDTopAppBarTitle ):
756
762
widget ._appbar = self
757
763
self ._appbar_title = widget
764
+ widget .bind (text = lambda * x : self .on_size (* x ))
758
765
Clock .schedule_once (lambda x : self ._add_title (widget ))
759
766
elif isinstance (widget , MDTopAppBarTrailingButtonContainer ):
760
767
self ._trailing_button_container = widget
@@ -779,81 +786,92 @@ def _set_padding_title(self, value):
779
786
not self ._trailing_button_container
780
787
and self ._leading_button_container
781
788
):
782
- left_padding = (self .width // 2 ) - (
783
- self ._leading_button_container .width
784
- + (self ._appbar_title ._title_width // 2 )
785
- )
786
- self ._appbar_title .padding = [left_padding , 0 , 0 , 0 ]
789
+ self ._left_padding = (
790
+ (self .width // 2 )
791
+ - (
792
+ self ._leading_button_container .width
793
+ + (self ._appbar_title ._title_width // 2 )
794
+ )
795
+ ) - self ._left_padding
787
796
elif (
788
797
self ._trailing_button_container
789
798
and not self ._leading_button_container
790
799
):
791
- left_padding = (self .width // 2 ) - (
792
- self ._appbar_title ._title_width // 2
793
- )
794
- right_padding = (self .width // 2 ) - (
795
- self ._trailing_button_container .width
796
- + (self ._appbar_title ._title_width // 2 )
797
- )
798
- self ._appbar_title .padding = [left_padding , 0 , right_padding , 0 ]
800
+ self ._left_padding = (
801
+ (self .width // 2 ) - (self ._appbar_title ._title_width // 2 )
802
+ ) - self ._left_padding
803
+ self ._right_padding = (
804
+ (self .width // 2 )
805
+ - (
806
+ self ._trailing_button_container .width
807
+ + (self ._appbar_title ._title_width // 2 )
808
+ )
809
+ ) - self ._right_padding
799
810
elif (
800
811
not self ._trailing_button_container
801
812
and not self ._leading_button_container
802
813
):
803
- left_padding = (self .width // 2 ) - (
804
- self ._appbar_title ._title_width // 2
805
- )
806
- right_padding = (self .width // 2 ) - (
807
- self ._appbar_title ._title_width // 2
808
- )
809
- self ._appbar_title .padding = [left_padding , 0 , right_padding , 0 ]
814
+ self ._left_padding = (
815
+ (self .width // 2 ) - (self ._appbar_title ._title_width // 2 )
816
+ ) - self ._left_padding
817
+ self ._right_padding = (
818
+ (self .width // 2 ) - (self ._appbar_title ._title_width // 2 )
819
+ ) - self ._right_padding
810
820
elif (
811
821
self ._trailing_button_container
812
822
and self ._leading_button_container
813
823
):
814
- left_padding = (self .width // 2 ) - (
815
- self ._leading_button_container .width
816
- + (self ._appbar_title ._title_width // 2 )
817
- )
818
- right_padding = (self .width // 2 ) - (
819
- self ._trailing_button_container .width
820
- + (self ._appbar_title ._title_width // 2 )
821
- )
822
- self ._appbar_title .padding = [left_padding , 0 , right_padding , 0 ]
824
+ self ._left_padding = (
825
+ (self .width // 2 )
826
+ - (
827
+ self ._leading_button_container .width
828
+ + (self ._appbar_title ._title_width // 2 )
829
+ )
830
+ ) - self ._left_padding
831
+ self ._right_padding = (
832
+ (self .width // 2 )
833
+ - (
834
+ self ._trailing_button_container .width
835
+ + (self ._appbar_title ._title_width // 2 )
836
+ )
837
+ ) - self ._right_padding
823
838
elif (
824
839
not value
825
840
and self ._trailing_button_container
826
841
and self ._leading_button_container
827
842
):
828
843
if self .type == "small" :
829
- right_padding = self .width - (
830
- self ._trailing_button_container .width
831
- + self ._leading_button_container .width
832
- + self ._appbar_title ._title_width
844
+
845
+ self ._right_padding = (
846
+ self .width
847
+ - (
848
+ self ._trailing_button_container .width
849
+ + self ._leading_button_container .width
850
+ + self ._appbar_title ._title_width
851
+ )
852
+ - self ._right_padding
833
853
)
834
- self ._appbar_title .padding = [0 , 0 , right_padding , 0 ]
835
854
elif (
836
855
not value
837
856
and self ._trailing_button_container
838
857
and not self ._leading_button_container
839
858
):
840
859
if self .type == "small" :
841
- right_padding = self .width - (
842
- self ._trailing_button_container .width
843
- + self ._appbar_title ._title_width
860
+ self ._right_padding = (
861
+ self .width
862
+ - (
863
+ self ._trailing_button_container .width
864
+ + self ._appbar_title ._title_width
865
+ )
866
+ - self ._right_padding
844
867
)
845
- self ._appbar_title .padding = [
846
- dp (16 ),
847
- 0 ,
848
- right_padding - dp (16 ),
849
- 0 ,
850
- ]
868
+ self ._left_padding = dp (16 )
851
869
elif (
852
870
not value
853
871
and not self ._trailing_button_container
854
872
and not self ._leading_button_container
855
873
):
856
- self ._appbar_title . padding_x = dp (16 )
874
+ self ._left_padding = dp (16 )
857
875
858
876
859
877
class MDBottomAppBar (
0 commit comments