20
20
21
21
use druid:: lens:: Unit ;
22
22
use druid:: widget:: prelude:: * ;
23
- use druid:: widget:: { BackgroundBrush , Button , ClipBox , Controller , Flex , Label , List , Padding , Side , Slider , Tabs , TextBox , ViewportHeader } ;
24
- use druid:: { AppLauncher , Color , Data , Insets , Lens , LocalizedString , Point , Rect , RoundedRectRadii , Selector , Vec2 , WidgetExt , WidgetPod , WindowDesc } ;
23
+ use druid:: widget:: {
24
+ BackgroundBrush , Button , ClipBox , Controller , Flex , Label , List , Padding , Side , Slider , Tabs ,
25
+ TextBox , ViewportHeader ,
26
+ } ;
27
+ use druid:: {
28
+ AppLauncher , Color , Data , Insets , Lens , LocalizedString , Point , Rect , RoundedRectRadii ,
29
+ Selector , Vec2 , WidgetExt , WidgetPod , WindowDesc ,
30
+ } ;
25
31
use im:: Vector ;
26
32
use std:: sync:: Arc ;
27
33
@@ -67,7 +73,14 @@ fn build_widget() -> impl Widget<AppData> {
67
73
. with_default_spacer ( )
68
74
. with_child ( Label :: new ( "Info:" ) . align_left ( ) )
69
75
. with_default_spacer ( )
70
- . with_child ( List :: new ( || TextBox :: new ( ) . padding ( Insets :: new ( 15.0 , 0.0 , 0.0 , 10.0 ) ) . expand_width ( ) ) . lens ( Contact :: info) )
76
+ . with_child (
77
+ List :: new ( || {
78
+ TextBox :: new ( )
79
+ . padding ( Insets :: new ( 15.0 , 0.0 , 0.0 , 10.0 ) )
80
+ . expand_width ( )
81
+ } )
82
+ . lens ( Contact :: info) ,
83
+ )
71
84
. with_child (
72
85
Button :: new ( "Add Info" ) . on_click ( |_, data : & mut Contact , _| {
73
86
data. info . push_back ( Arc :: new ( String :: new ( ) ) )
@@ -80,22 +93,25 @@ fn build_widget() -> impl Widget<AppData> {
80
93
. rounded ( RoundedRectRadii :: new ( 0.0 , 0.0 , 10.0 , 10.0 ) ) ;
81
94
82
95
let header = Flex :: row ( )
83
- . with_flex_child ( Label :: dynamic ( |data : & Contact , _| format ! ( "Contact \" {}\" " , & data. name) ) . center ( ) , 1.0 )
84
- . with_child ( Button :: new ( "X" ) . on_click ( |ctx, data : & mut Contact , _|{
85
- ctx. submit_notification ( REMOVE_ID . with ( data. id ) )
86
- } ) . padding ( 5.0 ) )
96
+ . with_flex_child (
97
+ Label :: dynamic ( |data : & Contact , _| format ! ( "Contact \" {}\" " , & data. name) ) . center ( ) ,
98
+ 1.0 ,
99
+ )
100
+ . with_child (
101
+ Button :: new ( "X" )
102
+ . on_click ( |ctx, data : & mut Contact , _| {
103
+ ctx. submit_notification ( REMOVE_ID . with ( data. id ) )
104
+ } )
105
+ . padding ( 5.0 ) ,
106
+ )
87
107
. center ( )
88
108
. background ( Color :: grey8 ( 15 ) )
89
109
. rounded ( RoundedRectRadii :: new ( 10.0 , 10.0 , 0.0 , 0.0 ) ) ;
90
110
91
- ViewportHeader :: new (
92
- body,
93
- header,
94
- Side :: Top ,
95
- )
96
- . clipped_content ( true )
97
- . with_minimum_visible_content ( 20.0 )
98
- . padding ( Insets :: uniform_xy ( 0.0 , 5.0 ) )
111
+ ViewportHeader :: new ( body, header, Side :: Top )
112
+ . clipped_content ( true )
113
+ . with_minimum_visible_content ( 20.0 )
114
+ . padding ( Insets :: uniform_xy ( 0.0 , 5.0 ) )
99
115
} )
100
116
. lens ( AppData :: list)
101
117
. controller ( RemoveID )
@@ -128,14 +144,21 @@ const REMOVE_ID: Selector<usize> = Selector::new("org.druid.example.remove_id");
128
144
struct RemoveID ;
129
145
130
146
impl < W : Widget < AppData > > Controller < AppData , W > for RemoveID {
131
- fn event ( & mut self , child : & mut W , ctx : & mut EventCtx , event : & Event , data : & mut AppData , env : & Env ) {
147
+ fn event (
148
+ & mut self ,
149
+ child : & mut W ,
150
+ ctx : & mut EventCtx ,
151
+ event : & Event ,
152
+ data : & mut AppData ,
153
+ env : & Env ,
154
+ ) {
132
155
if let Event :: Notification ( notification) = event {
133
156
if let Some ( id) = notification. get ( REMOVE_ID ) {
134
157
ctx. set_handled ( ) ;
135
- data. list . retain ( |c|c. id != * id) ;
158
+ data. list . retain ( |c| c. id != * id) ;
136
159
}
137
160
} else {
138
161
child. event ( ctx, event, data, env) ;
139
162
}
140
163
}
141
- }
164
+ }
0 commit comments