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