@@ -17,10 +17,10 @@ use crate::styles::components::{
1717 AutoFocusableControls , CornerRadius , DefaultActiveBackgroundColor ,
1818 DefaultActiveForegroundColor , DefaultBackgroundColor , DefaultDisabledBackgroundColor ,
1919 DefaultDisabledForegroundColor , DefaultForegroundColor , DefaultHoveredBackgroundColor ,
20- DefaultHoveredForegroundColor , Easing , HighlightColor , IntrinsicPadding , OpaqueWidgetColor ,
21- OutlineColor , OutlineWidth , SurfaceColor , TextColor ,
20+ DefaultHoveredForegroundColor , Easing , HighlightColor , HorizontalAlignment , IntrinsicPadding ,
21+ OpaqueWidgetColor , OutlineColor , OutlineWidth , SurfaceColor , TextColor ,
2222} ;
23- use crate :: styles:: { ColorExt , Styles } ;
23+ use crate :: styles:: { ColorExt , HorizontalAlign , Styles } ;
2424use crate :: widget:: {
2525 EventHandling , MakeWidget , Notify , SharedCallback , Widget , WidgetLayout , WidgetRef , HANDLED ,
2626} ;
@@ -38,6 +38,7 @@ pub struct Button {
3838 pub kind : Value < ButtonKind > ,
3939 focusable : bool ,
4040 per_window : WindowLocal < PerWindow > ,
41+ label_align : Dynamic < HorizontalAlign > ,
4142}
4243
4344#[ derive( Debug , Default ) ]
@@ -139,12 +140,14 @@ pub struct ButtonColors {
139140impl Button {
140141 /// Returns a new button with the provided label.
141142 pub fn new ( content : impl MakeWidget ) -> Self {
143+ let label_align = Dynamic :: < HorizontalAlign > :: default ( ) ;
142144 Self {
143- content : content. into_ref ( ) ,
145+ content : content. with ( & HorizontalAlignment , & label_align ) . into_ref ( ) ,
144146 on_click : None ,
145147 per_window : WindowLocal :: default ( ) ,
146148 kind : Value :: Constant ( ButtonKind :: default ( ) ) ,
147149 focusable : true ,
150+ label_align,
148151 }
149152 }
150153
@@ -527,12 +530,19 @@ impl Widget for Button {
527530 let double_padding = padding * 2 ;
528531 let mounted = self . content . mounted ( context) ;
529532 let available_space = available_space. map ( |space| space - double_padding) ;
533+ let align = context. get ( & ButtonLabelAlignment ) ;
534+ self . label_align . set ( align) ;
530535 let layout = context. for_other ( & mounted) . layout ( available_space) ;
531536 let size = available_space. fit_measured ( layout. size ) ;
532- context. set_child_layout (
533- & mounted,
534- Rect :: new ( Point :: squared ( padding) , size) . into_signed ( ) ,
537+ let mut position = Rect :: new ( Point :: squared ( padding) , size) . into_signed ( ) ;
538+ position. origin . x += align. alignment_offset (
539+ layout. size . width . into_signed ( ) . min ( position. size . width ) ,
540+ available_space
541+ . width
542+ . fill_or_fit ( position. size . width )
543+ . into_signed ( ) ,
535544 ) ;
545+ context. set_child_layout ( & mounted, position) ;
536546 WidgetLayout {
537547 size : size + double_padding,
538548 baseline : layout. baseline . map ( |baseline| baseline + padding) ,
@@ -616,6 +626,8 @@ define_components! {
616626 /// The outline color of the button when the mouse cursor is hovering over
617627 /// it.
618628 ButtonDisabledOutline ( Color , "disabled_outline_color" , Color :: CLEAR_BLACK )
629+ /// The horizontal alignment to apply to the label widget of the button.
630+ ButtonLabelAlignment ( HorizontalAlign , "align" , HorizontalAlign :: Center )
619631 }
620632}
621633
0 commit comments