1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Portable UI API for the _app.nit_ framework
21 import linux
::ui
is conditional
(linux
)
22 import android
::ui
is conditional
(android
)
23 import ios
::ui
is conditional
(ios
)
28 # The current `Window` of this activity
30 # This attribute is set by `push_window`.
31 var window
: Window is noinit
33 # Make visible and push `window` on the top of `pop_window`
35 # This method must be called at least once within `App::on_create`.
36 # It can be called at any times while the app is active.
37 fun push_window
(window
: Window)
39 window_stack
.add window
43 # Pop the current `window` from the stack and show the previous one
45 # Require: `window_stack.not_empty`
48 assert window_stack
.not_empty
50 window
= window_stack
.last
54 # Stack of active windows
55 var window_stack
= new Array[Window]
57 redef fun on_create
do window
.on_create
59 redef fun on_start
do window
.on_start
61 redef fun on_resume
do window
.on_resume
63 redef fun on_pause
do window
.on_pause
65 redef fun on_stop
do window
.on_stop
67 redef fun on_destroy
do window
.on_destroy
69 redef fun on_restore_state
do window
.on_restore_state
71 redef fun on_save_state
do window
.on_save_state
74 # An event created by an `AppComponent` and sent to `AppObserver`s
78 # Observer of `AppEvent`s raised by `AppComponent`s
80 # Notification of `event` raised by `sender`
82 # To be implemented in subclasses as needed.
83 fun on_event
(event
: AppEvent) do end
86 redef class AppComponent
89 # All `AppObserver` notified of events raised by `self`
91 # By default, only `self` is an observer.
92 # Any other `AppObserver` can be added to this collection.
93 var observers
= new HashSet[AppObserver].from
([self: AppObserver])
95 # Propagate `event` to all `observers` by calling `AppObserver::on_event`
96 fun notify_observers
(event
: AppEvent)
98 for observer
in observers
do
99 observer
.on_event
(event
)
104 # A control implementing the UI
108 # Direct parent `Control` in the control tree
110 # If `null` then `self` is at the root of the tree, or not yet attached.
111 var parent
: nullable CompositeControl = null is private writable(set_parent
)
113 # Direct parent `Control` in the control tree
115 # Setting `parent` calls `remove` on the old parent and `add` on the new one.
116 fun parent
=(parent
: nullable CompositeControl)
118 var old_parent
= self.parent
119 if old_parent
!= null and old_parent
!= parent
then
120 old_parent
.remove
self
123 if parent
!= null then parent
.add
self
129 # A `Control` grouping other controls
130 class CompositeControl
133 protected var items
= new Array[Control]
135 # Add `item` as a child of `self`
136 protected fun add
(item
: Control) do items
.add item
138 # Remove `item` from `self`
139 fun remove
(item
: Control) do if has
(item
) then items
.remove item
141 # Is `item` in `self`?
142 fun has
(item
: Control): Bool do return items
.has
(item
)
144 # Remove all items from `self`
145 fun clear
do for item
in items
.to_a
do remove item
147 redef fun on_create
do for i
in items
do i
.on_create
149 redef fun on_start
do for i
in items
do i
.on_start
151 redef fun on_resume
do for i
in items
do i
.on_resume
153 redef fun on_pause
do for i
in items
do i
.on_pause
155 redef fun on_stop
do for i
in items
do i
.on_stop
157 redef fun on_destroy
do for i
in items
do i
.on_destroy
159 redef fun on_restore_state
do for i
in items
do i
.on_restore_state
161 redef fun on_save_state
do for i
in items
do i
.on_save_state
164 # A window, root of the `Control` tree
166 super CompositeControl
168 # Should the back button be shown and used to go back to a previous window?
169 fun enable_back_button
: Bool do return app
.window_stack
.length
> 1
171 # The back button has been pressed, usually to open the previous window
172 fun on_back_button
do app
.pop_window
175 # A viewable `Control`
179 # Is this control enabled so the user can interact with it?
181 # By default, or if set to `null`, the control is enabled.
182 var enabled
: nullable Bool is writable, abstract, autoinit
185 # A control with some `text`
186 abstract class TextView
189 # Main `Text` of this control
191 # By default, or if set to `null`, no text is shown.
192 var text
: nullable Text is writable, abstract, autoinit
194 # Set the relative size of the text
196 # A value of 1.0, the default, use the platform default text size.
197 # Values under 1.0 set a smaller text size, and over 1.0 a larger size.
199 # Implementation varies per platform, and some controls may be unaffected
200 # depending on the customization options of each platform.
201 # For consistent results, it is recommended to use only on instances
202 # of `Label` and `size` should be either 0.5, 1.0 or 1.5.
203 fun size
=(size
: nullable Float) is autoinit
do end
205 # Align the text horizontally
207 # Use 0.0 to align left (the default), 0.5 to align in the center and
208 # 1.0 to align on the right.
210 # Implementation varies per platform, and some controls may be unaffected
211 # depending on the customization options of each platform.
212 # For consistent results, it is recommended to use only on instances
213 # of `Label` and `size` should be either 0.0, 0.5 or 1.0.
214 fun align
=(center
: nullable Float) is autoinit
do end
217 # A control for the user to enter custom `text`
221 # Hide password or any content entered in this view?
222 var is_password
: nullable Bool is writable
225 # A pushable button, raises `ButtonPressEvent`
235 # Toggle control with two states and a label
239 # Is this control in the checked/on state?
240 var is_checked
= false is writable
243 # Event sent from a `VIEW`
247 # The `VIEW` that raised this event
250 # Type of the `sender`
254 # A `Button` press event
255 class ButtonPressEvent
258 redef type VIEW: Button
261 # The `CheckBox` `sender` has been toggled
265 redef type VIEW: CheckBox
268 # A layout to visually organize `Control`s
269 abstract class Layout
271 super CompositeControl
274 # An horizontal linear organization
275 class HorizontalLayout
279 # A vertical linear organization
284 # Scrollable list of views in a simple list
287 super CompositeControl