examples: annotate examples
[nit.git] / lib / app / examples / ui_example.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # User interface example using `app::ui`
16 module ui_example is
17 example
18 app_name "app.nit UI"
19 app_namespace "org.nitlanguage.ui_example"
20 android_api_min 21
21 android_api_target 21
22 android_manifest_activity "android:theme=\"@android:style/Theme.Material\""
23 end
24
25 import app::ui
26 import app::data_store
27 import android::aware # for android_api_target
28
29 # Window showing off some the available controls
30 class UiExampleWindow
31 super Window
32
33 # Root layout
34 var layout = new ListLayout(parent=self)
35
36 # Some label
37 var some_label = new Label(parent=layout, text="Sample Window using a ListLayout.")
38
39 # A checkbox
40 var checkbox = new CheckBox(parent=layout, text="A CheckBox")
41
42 # Horizontal organization
43 var h_layout = new HorizontalLayout(parent=layout)
44
45 # Description for the `user_input`
46 var user_input_label = new Label(parent=h_layout, text="Input some text:", align=0.5)
47
48 # Field for the user to enter data
49 var user_input = new TextInput(parent=h_layout, text="Default text")
50
51 # Button to open a new window with a ListLayout
52 var button_window = new Button(parent=layout, text="Open a new window")
53
54 # URL to open
55 var example_url = "http://nitlanguage.org/"
56
57 # Button to open the browser
58 var button_browser = new Button(parent=layout, text="Open {example_url}")
59
60 redef fun on_event(event)
61 do
62 if event isa ButtonPressEvent then
63 if event.sender == button_browser then
64 example_url.open_in_browser
65 else if event.sender == button_window then
66 app.push_window new SecondWindow
67 end
68 else if event isa ToggleEvent then
69 if event.sender == checkbox then checkbox.text = if checkbox.is_checked then "Checked" else "Unchecked"
70 end
71 end
72 end
73
74 # Another window with a small `VerticalLayout`
75 class SecondWindow
76 super Window
77
78 # Root layout
79 var layout = new VerticalLayout(parent=self)
80
81 # Some label
82 var a_label = new Label(parent=layout, text="This window uses a VerticalLayout.")
83
84 # Another label
85 var another_label = new Label(parent=layout, text="Close it by tapping the back button.")
86 end
87
88 redef fun root_window do return new UiExampleWindow