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`
28 # The current `Window` of this activity
30 # This attribute must be set by refinements of `App`.
31 var window
: Window is writable
33 redef fun on_create
do window
.on_create
35 redef fun on_start
do window
.on_start
37 redef fun on_resume
do window
.on_resume
39 redef fun on_pause
do window
.on_pause
41 redef fun on_stop
do window
.on_stop
43 redef fun on_destroy
do window
.on_destroy
45 redef fun on_restore_state
do window
.on_restore_state
47 redef fun on_save_state
do window
.on_save_state
50 # An event created by an `AppComponent` and sent to `AppObserver`s
54 # Observer of `AppEvent`s raised by `AppComponent`s
56 # Notification of `event` raised by `sender`
58 # To be implemented in subclasses as needed.
59 fun on_event
(event
: AppEvent) do end
62 redef class AppComponent
65 # All `AppObserver` notified of events raised by `self`
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])
71 # Propagate `event` to all `observers` by calling `AppObserver::on_event`
72 fun notify_observers
(event
: AppEvent)
74 for observer
in observers
do
75 observer
.on_event
(event
)
80 # A control implementing the UI
84 # Direct parent `Control` in the control tree
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
)
89 # Direct parent `Control` in the control tree
91 # Setting `parent` calls `remove` on the old parent and `add` on the new one.
92 fun parent
=(parent
: nullable CompositeControl)
94 var old_parent
= self.parent
95 if old_parent
!= null and old_parent
!= parent
then
96 old_parent
.remove
self
99 if parent
!= null then parent
.add
self
105 # A `Control` grouping other controls
106 class CompositeControl
109 private var items
= new HashSet[Control]
111 # Add `item` as a child of `self`
112 protected fun add
(item
: Control) do items
.add item
114 # Remove `item` from `self`
115 protected fun remove
(item
: Control) do if has
(item
) then items
.remove item
117 # Is `item` in `self`?
118 protected fun has
(item
: Control): Bool do return items
.has
(item
)
120 redef fun on_create
do for i
in items
do i
.on_create
122 redef fun on_start
do for i
in items
do i
.on_start
124 redef fun on_resume
do for i
in items
do i
.on_resume
126 redef fun on_pause
do for i
in items
do i
.on_pause
128 redef fun on_stop
do for i
in items
do i
.on_stop
130 redef fun on_destroy
do for i
in items
do i
.on_destroy
132 redef fun on_restore_state
do for i
in items
do i
.on_restore_state
134 redef fun on_save_state
do for i
in items
do i
.on_save_state
137 # A window, root of the `Control` tree
139 super CompositeControl
142 # A viewable `Control`
146 # Is this control enabled so the user can interact with it?
148 # By default, or if set to `null`, the control is enabled.
149 var enabled
: nullable Bool is writable #, abstract FIXME with #1311
152 # A control with some `text`
153 abstract class TextView
156 # Main `Text` of this control
158 # By default, or if set to `null`, no text is shown.
159 var text
: nullable Text is writable #, abstract FIXME with #1311
162 # A control for the user to enter custom `text`
167 # A pushable button, raises `ButtonPressEvent`
172 # A `Button` press event
173 class ButtonPressEvent
176 # The `Button` that raised this event
180 # A layout to visually organize `Control`s
181 abstract class Layout
183 super CompositeControl
186 # An horizontal linear organization
187 class HorizontalLayout
191 # A vertical linear organization