b1f07a5170e83261c565db63da8b1df8c38f7aaa
[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 controls from mobiles apps
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 is set by `push_window`.
31 var window: Window is noinit
32
33 # Make `window` visible and push it on the top of the `window_stack`
34 #
35 # This method can be called at any times while the app is active.
36 fun push_window(window: Window)
37 do
38 window_stack.add window
39 self.window = window
40 window.on_create
41 end
42
43 # Pop the current `window` from the stack and show the previous one
44 #
45 # Require: `window_stack.not_empty`
46 fun pop_window
47 do
48 assert window_stack.not_empty
49 window_stack.pop
50 window = window_stack.last
51 window.on_resume
52 end
53
54 # Stack of active windows
55 var window_stack = new Array[Window]
56
57 redef fun on_create
58 do
59 var window = root_window
60 push_window window
61 end
62
63 redef fun on_resume do window.on_resume
64
65 redef fun on_pause do window.on_pause
66
67 redef fun on_stop do window.on_stop
68
69 redef fun on_restore_state do window.on_restore_state
70
71 redef fun on_save_state do window.on_save_state
72 end
73
74 # Hook to create the first window shown to the user
75 #
76 # By default, a `Window` is created, which can be refined to customize it.
77 # However, most apps should refine this method to return a different window,
78 # this way the app can have more than one window.
79 fun root_window: Window do return new Window
80
81 # An event created by an `AppComponent` and sent to `AppObserver`s
82 interface AppEvent
83 end
84
85 # Observer of `AppEvent`s raised by `AppComponent`s
86 interface AppObserver
87 # Notification of `event` raised by `sender`
88 #
89 # To be implemented in subclasses as needed.
90 fun on_event(event: AppEvent) do end
91 end
92
93 redef class AppComponent
94 super AppObserver
95
96 # All `AppObserver` notified of events raised by `self`
97 #
98 # By default, only `self` is an observer.
99 # Any other `AppObserver` can be added to this collection.
100 var observers = new HashSet[AppObserver].from([self: AppObserver])
101
102 # Propagate `event` to all `observers` by calling `AppObserver::on_event`
103 fun notify_observers(event: AppEvent)
104 do
105 for observer in observers do
106 observer.on_event(event)
107 end
108 end
109 end
110
111 # A control implementing the UI
112 class Control
113 super AppComponent
114
115 # Direct parent `Control` in the control tree
116 #
117 # The parents (direct and indirect) receive all events from `self`,
118 # like the `observers`.
119 #
120 # If `null` then `self` is at the root of the tree, or not yet attached.
121 var parent: nullable CompositeControl = null is private writable(set_parent)
122
123 # Direct parent `Control` in the control tree
124 #
125 # The parents (direct and indirect) receive all events from `self`,
126 # like the `observers`.
127 #
128 # Setting `parent` calls `remove` on the old parent and `add` on the new one.
129 fun parent=(parent: nullable CompositeControl)
130 is autoinit do
131 var old_parent = self.parent
132 if old_parent != null and old_parent != parent then
133 old_parent.remove self
134 end
135
136 if parent != null then parent.add self
137
138 set_parent parent
139 end
140
141 # Also notify the parents (both direct and indirect)
142 redef fun notify_observers(event)
143 do
144 super
145
146 var p = parent
147 while p != null do
148 p.on_event event
149 p = p.parent
150 end
151 end
152 end
153
154 # A `Control` grouping other controls
155 class CompositeControl
156 super Control
157
158 # Child controls composing this control
159 protected var items = new Array[Control]
160
161 # Add `item` as a child of `self`
162 protected fun add(item: Control) do items.add item
163
164 # Remove `item` from `self`
165 fun remove(item: Control) do if has(item) then items.remove item
166
167 # Is `item` in `self`?
168 fun has(item: Control): Bool do return items.has(item)
169
170 # Remove all items from `self`
171 fun clear do for item in items.to_a do remove item
172
173 redef fun on_create do for i in items do i.on_create
174
175 redef fun on_resume do for i in items do i.on_resume
176
177 redef fun on_pause do for i in items do i.on_pause
178
179 redef fun on_stop do for i in items do i.on_stop
180
181 redef fun on_restore_state do for i in items do i.on_restore_state
182
183 redef fun on_save_state do for i in items do i.on_save_state
184 end
185
186 # A window, root of the `Control` tree
187 #
188 # Each window should hold a single control, usually a `CompositeControl`,
189 # which in turn holds all the displayed controls.
190 class Window
191 super CompositeControl
192
193 # Should the back button be shown and used to go back to a previous window?
194 fun enable_back_button: Bool do return app.window_stack.length > 1
195
196 # The back button has been pressed, usually to open the previous window
197 fun on_back_button do app.pop_window
198 end
199
200 # A visible `Control`
201 abstract class View
202 super Control
203
204 # Is this control enabled so the user can interact with it?
205 #
206 # By default, or if set to `null`, the control is enabled.
207 var enabled: nullable Bool is writable, abstract, autoinit
208 end
209
210 # A control displaying some `text`
211 #
212 # For a text-only control, see `Label`.
213 abstract class TextView
214 super View
215
216 # Main `Text` of this control
217 #
218 # By default, or if set to `null`, no text is shown.
219 var text: nullable Text is writable, abstract, autoinit
220
221 # Set the relative size of the text
222 #
223 # A value of 1.0, the default, use the platform default text size.
224 # Values under 1.0 set a smaller text size, and over 1.0 a larger size.
225 #
226 # Implementation varies per platform, and some controls may be unaffected
227 # depending on the customization options of each platform.
228 # For consistent results, it is recommended to use only on instances
229 # of `Label` and `size` should be either 0.5, 1.0 or 1.5.
230 fun size=(size: nullable Float) is autoinit do end
231
232 # Align the text horizontally
233 #
234 # Use 0.0 to align left (the default), 0.5 to align in the center and
235 # 1.0 to align on the right.
236 #
237 # Implementation varies per platform, and some controls may be unaffected
238 # depending on the customization options of each platform.
239 # For consistent results, it is recommended to use only on instances
240 # of `Label` and `size` should be either 0.0, 0.5 or 1.0.
241 fun align=(align: nullable Float) is autoinit do end
242 end
243
244 # A control for the user to enter custom `text`
245 class TextInput
246 super TextView
247
248 # Hide password or any content entered in this view?
249 var is_password: nullable Bool is writable
250 end
251
252 # A pressable button, raises `ButtonPressEvent`
253 class Button
254 super TextView
255 end
256
257 # A simple text label
258 class Label
259 super TextView
260 end
261
262 # Toggle control between two states, also displays a label
263 class CheckBox
264 super TextView
265
266 # Is this control in the checked/on state?
267 var is_checked = false is writable
268 end
269
270 # Event sent from a `VIEW`
271 class ViewEvent
272 super AppEvent
273
274 # The `VIEW` that raised this event
275 var sender: VIEW
276
277 # Type of the `sender`
278 type VIEW: View
279 end
280
281 # A `Button` press event
282 class ButtonPressEvent
283 super ViewEvent
284
285 redef type VIEW: Button
286 end
287
288 # The `CheckBox` `sender` has been toggled
289 class ToggleEvent
290 super ViewEvent
291
292 redef type VIEW: CheckBox
293 end
294
295 # A layout to visually organize `Control`s
296 abstract class Layout
297 super View
298 super CompositeControl
299 end
300
301 # An horizontal linear organization
302 class HorizontalLayout
303 super Layout
304 end
305
306 # A vertical linear organization
307 class VerticalLayout
308 super Layout
309 end
310
311 # Scrollable list of views in a simple list
312 class ListLayout
313 super View
314 super CompositeControl
315 end
316
317 redef class Text
318 # Open the URL `self` with the default browser
319 fun open_in_browser do print_error "Text::open_in_browser not implemented on this platform."
320 end