1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- #![ no_std] ////
15+ #![ no_std] // This program will run on embedded platforms
1616use druid:: widget:: { Align , Button , Column , Label , Padding } ;
1717use druid:: { AppLauncher , LocalizedString , Widget , WindowDesc } ;
1818
1919pub fn launch ( ) {
2020 // Build a new window
2121 let main_window = WindowDesc :: new ( ui_builder) ;
22+ // Application state is initially 0
2223 let data = 0_u32 ;
23- // Launch the window
24+ // Launch the window with the initial application state
2425 AppLauncher :: with_window ( main_window)
2526 . use_simple_logger ( )
2627 . launch ( data)
2728 . expect ( "launch failed" ) ;
2829}
2930
30- /// Build the UI for the window. The window state consists of 1 value: `count` of type `u32`.
31- fn ui_builder ( ) -> impl Widget < u32 > { // `u32` is the window state
31+ /// Build the UI for the window. The application state consists of 1 value: `count` of type `u32`.
32+ fn ui_builder ( ) -> impl Widget < u32 > { // `u32` is the application state
3233 // Create a line of text based on a counter value
3334 let text =
3435 LocalizedString :: new ( "hello-counter" )
3536 . with_arg (
3637 "count" ,
37- |data : & u32 , _env| ( * data) . into ( )
38+ // Closure that will be run to fetch the counter value
39+ | data : & u32 , _env |
40+ ( * data) . into ( ) // We return the counter value in the application state
3841 ) ;
3942 // Create a label widget to display the text
4043 let label = Label :: new ( text) ;
4144 // Create a button widget to increment the counter
42- // let button = Button::<u32>::new( ////
4345 let button = Button :: new (
44- "increment" ,
45- |_ctx, data, _env| * data += 1
46+ "increment" , // Text to be shown
47+ // Closure that will be run when button is tapped
48+ | _ctx, data, _env |
49+ * data += 1 // We increment the counter
4650 ) ;
4751
4852 // Create a column for the UI
4953 let mut col = Column :: new ( ) ;
50- // Add the label and button widgets to the column
51- // col.add_child(label, 1.0); ////
54+ // Add the label widget to the column, centered with padding
5255 col. add_child (
5356 Align :: centered (
5457 Padding :: new ( 5.0 , label)
5558 ) ,
5659 1.0
5760 ) ;
61+ // Add the button widget to the column, with padding
5862 col. add_child (
5963 Padding :: new ( 5.0 , button) ,
6064 1.0
6165 ) ;
66+ // Return the column containing the label and button widgets
6267 col
63- }
68+ }
0 commit comments