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
23 # The current `Window` of this activity
25 # This attribute must be set by refinements of `App`.
26 var window
: Window is writable
28 redef fun on_create
do window
.on_create
30 redef fun on_start
do window
.on_start
32 redef fun on_resume
do window
.on_resume
34 redef fun on_pause
do window
.on_pause
36 redef fun on_stop
do window
.on_stop
38 redef fun on_destroy
do window
.on_destroy
40 redef fun on_restore_state
do window
.on_restore_state
42 redef fun on_save_state
do window
.on_save_state
45 # An event created by an `AppComponent` and sent to `AppObserver`s
49 # Observer of `AppEvent`s raised by `AppComponent`s
51 # Notification of `event` raised by `sender`
53 # To be implemented in subclasses as needed.
54 fun on_event
(event
: AppEvent) do end
57 redef class AppComponent
60 # All `AppObserver` notified of events raised by `self`
62 # By default, only `self` is an observer.
63 # Any other `AppObserver` can be added to this collection.
64 var observers
= new HashSet[AppObserver].from
([self: AppObserver])
66 # Propagate `event` to all `observers` by calling `AppObserver::on_event`
67 fun notify_observers
(event
: AppEvent)
69 for observer
in observers
do
70 observer
.on_event
(event
)
75 # A control implementing the UI
79 # Direct parent `Control` in the control tree
81 # If `null` then `self` is at the root of the tree, or not yet attached.
82 var parent
: nullable CompositeControl = null is private writable(set_parent
)
84 # Direct parent `Control` in the control tree
86 # Setting `parent` calls `remove` on the old parent and `add` on the new one.
87 fun parent
=(parent
: nullable CompositeControl)
89 var old_parent
= self.parent
90 if old_parent
!= null and old_parent
!= parent
then
91 old_parent
.remove
self
94 if parent
!= null then parent
.add
self
100 # A `Control` grouping other controls
101 class CompositeControl
104 private var items
= new HashSet[Control]
106 # Add `item` as a child of `self`
107 protected fun add
(item
: Control) do items
.add item
109 # Remove `item` from `self`
110 protected fun remove
(item
: Control) do if has
(item
) then items
.remove item
112 # Is `item` in `self`?
113 protected fun has
(item
: Control): Bool do return items
.has
(item
)
115 redef fun on_create
do for i
in items
do i
.on_create
117 redef fun on_start
do for i
in items
do i
.on_start
119 redef fun on_resume
do for i
in items
do i
.on_resume
121 redef fun on_pause
do for i
in items
do i
.on_pause
123 redef fun on_stop
do for i
in items
do i
.on_stop
125 redef fun on_destroy
do for i
in items
do i
.on_destroy
127 redef fun on_restore_state
do for i
in items
do i
.on_restore_state
129 redef fun on_save_state
do for i
in items
do i
.on_save_state
132 # A window, root of the `Control` tree
134 super CompositeControl
137 # A viewable `Control`
141 # Is this control enabled so the user can interact with it?
143 # By default, or if set to `null`, the control is enabled.
144 var enabled
: nullable Bool is writable #, abstract FIXME with #1311
147 # A control with some `text`
148 abstract class TextView
151 # Main `Text` of this control
153 # By default, or if set to `null`, no text is shown.
154 var text
: nullable Text is writable #, abstract FIXME with #1311
157 # A control for the user to enter custom `text`
162 # A pushable button, raises `ButtonPressEvent`
167 # A `Button` press event
168 class ButtonPressEvent
171 # The `Button` that raised this event
175 # A layout to visually organize `Control`s
176 abstract class Layout
178 super CompositeControl
181 # An horizontal linear organization
182 class HorizontalLayout
186 # A vertical linear organization