2661d0ee8b40b64ce601acb660e3e553b1b8acfa
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 # Views and services to use the Android native user interface
18 # Implementation note:
20 # We cannot rely on `Activity::on_restore_instance_state` to implement
21 # `on_restore_state` is it only invoked if there is a bundled state,
22 # and we don't use the Android bundled state.
29 private import data_store
32 # The Android element used to implement `self`
33 fun native
: NATIVE is abstract
36 type NATIVE: JavaObject
40 redef var native
= app
.native_activity
.new_global_ref
42 redef type NATIVE: NativeActivity
48 # FIXME abstract the Android restriction where `content_view` must be a layout
49 assert item
isa Layout
50 native
.content_view
= item
.native
55 redef type NATIVE: NativeView
57 redef fun enabled
=(enabled
) do native
.enabled
= enabled
or else true
58 redef fun enabled
do return native
.enabled
62 redef type NATIVE: NativeViewGroup
70 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
71 native
.add_view_with_weight
(item
.native
, 1.0)
74 redef fun remove
(item
)
77 if item
isa View then native
.remove_view item
.native
81 redef class HorizontalLayout
83 var layout
= new NativeLinearLayout(app
.native_activity
)
84 layout
= layout
.new_global_ref
90 redef class VerticalLayout
92 var layout
= new NativeLinearLayout(app
.native_activity
)
93 layout
= layout
.new_global_ref
100 redef type NATIVE: NativeTextView
102 redef fun text
do return native
.text
.to_s
103 redef fun text
=(value
) do
104 if value
== null then value
= ""
105 native
.text
= value
.to_java_string
109 fun text_size
: Float do return native
.text_size
112 fun text_size
=(text_size
: nullable Float) do
113 if text_size
!= null then native
.text_size
= text_size
117 redef class TextInput
118 redef type NATIVE: NativeEditText
119 redef var native
= (new NativeEditText(app
.native_activity
)).new_global_ref
125 redef type NATIVE: NativeButton
126 redef var native
= (new NativeButton(app
.native_activity
, self)).new_global_ref
128 private fun on_click
do notify_observers
new ButtonPressEvent(self)
130 redef fun finalize
do native
.delete_global_ref
133 redef class NativeButton
134 private new (context
: NativeActivity, sender_object
: Button)
135 import Button.on_click
in "Java" `{
136 final int final_sender_object = sender_object;
138 return new android.widget.Button(context){
140 public boolean onTouchEvent(android.view.MotionEvent event) {
141 if(event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
142 Button_on_click(final_sender_object);