lib/android: revamp entrypoint
[nit.git] / lib / app / ui.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 # Portable UI API for the _app.nit_ framework
16 module ui
17
18 import app_base
19
20 # Platform variations
21 import linux::ui is conditional(linux)
22 import android::ui is conditional(android)
23 import ios::ui is conditional(ios)
24
25 redef class App
26 super AppComponent
27
28 # The current `Window` of this activity
29 #
30 # This attribute must be set by refinements of `App`.
31 var window: Window is writable
32
33 redef fun on_create do window.on_create
34
35 redef fun on_start do window.on_start
36
37 redef fun on_resume do window.on_resume
38
39 redef fun on_pause do window.on_pause
40
41 redef fun on_stop do window.on_stop
42
43 redef fun on_destroy do window.on_destroy
44
45 redef fun on_restore_state do window.on_restore_state
46
47 redef fun on_save_state do window.on_save_state
48 end
49
50 # An event created by an `AppComponent` and sent to `AppObserver`s
51 interface AppEvent
52 end
53
54 # Observer of `AppEvent`s raised by `AppComponent`s
55 interface AppObserver
56 # Notification of `event` raised by `sender`
57 #
58 # To be implemented in subclasses as needed.
59 fun on_event(event: AppEvent) do end
60 end
61
62 redef class AppComponent
63 super AppObserver
64
65 # All `AppObserver` notified of events raised by `self`
66 #
67 # By default, only `self` is an observer.
68 # Any other `AppObserver` can be added to this collection.
69 var observers = new HashSet[AppObserver].from([self: AppObserver])
70
71 # Propagate `event` to all `observers` by calling `AppObserver::on_event`
72 fun notify_observers(event: AppEvent)
73 do
74 for observer in observers do
75 observer.on_event(event)
76 end
77 end
78 end
79
80 # A control implementing the UI
81 class Control
82 super AppComponent
83
84 # Direct parent `Control` in the control tree
85 #
86 # If `null` then `self` is at the root of the tree, or not yet attached.
87 var parent: nullable CompositeControl = null is private writable(set_parent)
88
89 # Direct parent `Control` in the control tree
90 #
91 # Setting `parent` calls `remove` on the old parent and `add` on the new one.
92 fun parent=(parent: nullable CompositeControl)
93 is autoinit do
94 var old_parent = self.parent
95 if old_parent != null and old_parent != parent then
96 old_parent.remove self
97 end
98
99 if parent != null then parent.add self
100
101 set_parent parent
102 end
103 end
104
105 # A `Control` grouping other controls
106 class CompositeControl
107 super Control
108
109 protected var items = new Array[Control]
110
111 # Add `item` as a child of `self`
112 protected fun add(item: Control) do items.add item
113
114 # Remove `item` from `self`
115 fun remove(item: Control) do if has(item) then items.remove item
116
117 # Is `item` in `self`?
118 fun has(item: Control): Bool do return items.has(item)
119
120 # Remove all items from `self`
121 fun clear do for item in items.to_a do remove item
122
123 redef fun on_create do for i in items do i.on_create
124
125 redef fun on_start do for i in items do i.on_start
126
127 redef fun on_resume do for i in items do i.on_resume
128
129 redef fun on_pause do for i in items do i.on_pause
130
131 redef fun on_stop do for i in items do i.on_stop
132
133 redef fun on_destroy do for i in items do i.on_destroy
134
135 redef fun on_restore_state do for i in items do i.on_restore_state
136
137 redef fun on_save_state do for i in items do i.on_save_state
138 end
139
140 # A window, root of the `Control` tree
141 class Window
142 super CompositeControl
143 end
144
145 # A viewable `Control`
146 abstract class View
147 super Control
148
149 # Is this control enabled so the user can interact with it?
150 #
151 # By default, or if set to `null`, the control is enabled.
152 var enabled: nullable Bool is writable, abstract, autoinit
153 end
154
155 # A control with some `text`
156 abstract class TextView
157 super View
158
159 # Main `Text` of this control
160 #
161 # By default, or if set to `null`, no text is shown.
162 var text: nullable Text is writable, abstract, autoinit
163
164 # Set the relative size of the text
165 #
166 # A value of 1.0, the default, use the platform default text size.
167 # Values under 1.0 set a smaller text size, and over 1.0 a larger size.
168 #
169 # Implementation varies per platform, and some controls may be unaffected
170 # depending on the customization options of each platform.
171 # For consistent results, it is recommended to use only on instances
172 # of `Label` and `size` should be either 0.5, 1.0 or 1.5.
173 fun size=(size: nullable Float) is autoinit do end
174
175 # Align the text horizontally
176 #
177 # Use 0.0 to align left (the default), 0.5 to align in the center and
178 # 1.0 to align on the right.
179 #
180 # Implementation varies per platform, and some controls may be unaffected
181 # depending on the customization options of each platform.
182 # For consistent results, it is recommended to use only on instances
183 # of `Label` and `size` should be either 0.0, 0.5 or 1.0.
184 fun align=(center: nullable Float) is autoinit do end
185 end
186
187 # A control for the user to enter custom `text`
188 class TextInput
189 super TextView
190
191 # Hide password or any content entered in this view?
192 var is_password: nullable Bool is writable
193 end
194
195 # A pushable button, raises `ButtonPressEvent`
196 class Button
197 super TextView
198 end
199
200 # A text label
201 class Label
202 super TextView
203 end
204
205 # Toggle control with two states and a label
206 class CheckBox
207 super TextView
208
209 # Is this control in the checked/on state?
210 var is_checked = false is writable
211 end
212
213 # A `Button` press event
214 class ButtonPressEvent
215 super AppEvent
216
217 # The `Button` that raised this event
218 var sender: Button
219 end
220
221 # A layout to visually organize `Control`s
222 abstract class Layout
223 super View
224 super CompositeControl
225 end
226
227 # An horizontal linear organization
228 class HorizontalLayout
229 super Layout
230 end
231
232 # A vertical linear organization
233 class VerticalLayout
234 super Layout
235 end
236
237 # Scrollable list of views in a simple list
238 class ListLayout
239 super View
240 super CompositeControl
241 end