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 # 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
)
29 # The current `Window` of this activity
31 # This attribute must be set by refinements of `App`.
32 var window
: Window is writable
34 redef fun on_create
do window
.on_create
36 redef fun on_start
do window
.on_start
38 redef fun on_resume
do window
.on_resume
40 redef fun on_pause
do window
.on_pause
42 redef fun on_stop
do window
.on_stop
44 redef fun on_destroy
do window
.on_destroy
46 redef fun on_restore_state
do window
.on_restore_state
48 redef fun on_save_state
do window
.on_save_state
51 # An event created by an `AppComponent` and sent to `AppObserver`s
55 # Observer of `AppEvent`s raised by `AppComponent`s
57 # Notification of `event` raised by `sender`
59 # To be implemented in subclasses as needed.
60 fun on_event
(event
: AppEvent) do end
63 redef class AppComponent
66 # All `AppObserver` notified of events raised by `self`
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])
72 # Propagate `event` to all `observers` by calling `AppObserver::on_event`
73 fun notify_observers
(event
: AppEvent)
75 for observer
in observers
do
76 observer
.on_event
(event
)
81 # A control implementing the UI
85 # Direct parent `Control` in the control tree
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
)
90 # Direct parent `Control` in the control tree
92 # Setting `parent` calls `remove` on the old parent and `add` on the new one.
93 fun parent
=(parent
: nullable CompositeControl)
95 var old_parent
= self.parent
96 if old_parent
!= null and old_parent
!= parent
then
97 old_parent
.remove
self
100 if parent
!= null then parent
.add
self
106 # A `Control` grouping other controls
107 class CompositeControl
110 protected var items
= new Array[Control]
112 # Add `item` as a child of `self`
113 protected fun add
(item
: Control) do items
.add item
115 # Remove `item` from `self`
116 fun remove
(item
: Control) do if has
(item
) then items
.remove item
118 # Is `item` in `self`?
119 fun has
(item
: Control): Bool do return items
.has
(item
)
121 redef fun on_create
do for i
in items
do i
.on_create
123 redef fun on_start
do for i
in items
do i
.on_start
125 redef fun on_resume
do for i
in items
do i
.on_resume
127 redef fun on_pause
do for i
in items
do i
.on_pause
129 redef fun on_stop
do for i
in items
do i
.on_stop
131 redef fun on_destroy
do for i
in items
do i
.on_destroy
133 redef fun on_restore_state
do for i
in items
do i
.on_restore_state
135 redef fun on_save_state
do for i
in items
do i
.on_save_state
138 # A window, root of the `Control` tree
140 super CompositeControl
143 # A viewable `Control`
147 # Is this control enabled so the user can interact with it?
149 # By default, or if set to `null`, the control is enabled.
150 var enabled
: nullable Bool is writable, abstract, autoinit
153 # A control with some `text`
154 abstract class TextView
157 # Main `Text` of this control
159 # By default, or if set to `null`, no text is shown.
160 var text
: nullable Text is writable, abstract, autoinit
163 # A control for the user to enter custom `text`
167 # Hide password or any content entered in this view?
168 var is_password
: nullable Bool is writable
171 # A pushable button, raises `ButtonPressEvent`
181 # Toggle control with two states and a label
185 # Is this control in the checked/on state?
186 var is_checked
= false is writable
189 # A `Button` press event
190 class ButtonPressEvent
193 # The `Button` that raised this event
197 # A layout to visually organize `Control`s
198 abstract class Layout
200 super CompositeControl
203 # An horizontal linear organization
204 class HorizontalLayout
208 # A vertical linear organization
213 # Scrollable list of views in a simple list
216 super CompositeControl