1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Views and services to use the Android native user interface
19 # Events, such as a button click, come from the UI thread and are then
20 # passed to the main thread. It is recommended to specialize one of the
21 # methods of the main thread to customize the response to a given event.
23 # This graph shows the path of a button click:
25 # UI Thread # Main thread
30 # Button::click_ui --> Button::click
35 module ui
is min_api_version
14
38 import pthreads
::concurrent_collections
41 import android.app.Activity;
43 import android.view.Gravity;
44 import android.view.MotionEvent;
45 import android.view.ViewGroup;
46 import android.view.ViewGroup.MarginLayoutParams;
48 import android.widget.Button;
49 import android.widget.LinearLayout;
50 import android.widget.GridLayout;
51 import android.widget.PopupWindow;
52 import android.widget.TextView;
58 # An event from the `app.nit` framework
60 # Reaction to this event
64 # A control click event
68 # Sender of this event
71 redef fun react
do sender
.click
self
74 # Receiver of events not handled directly by the sender
75 interface EventCatcher
76 fun catch_event
(event
: AppEvent) do end
83 redef extern class NativeActivity
85 # Set the main layout of this activity
86 fun content_view
=(layout
: NativeViewGroup) in "Java" `{
87 final ViewGroup final_layout = layout;
88 final Activity final_recv = recv;
90 recv.runOnUiThread(new Runnable() {
93 final_recv.setContentView(final_layout);
95 final_layout.requestFocus();
101 # An `Object` that raises events
102 abstract class Eventful
103 var event_catcher
: EventCatcher = app
is lazy
, writable
107 ## Nity classes and services
110 # An Android control with text
111 abstract class TextView
115 # Native Java variant to this Nity class
116 type NATIVE: NativeTextView
118 # The native Java object encapsulated by `self`
119 var native
: NATIVE is noinit
121 # Get the text of this view
124 var jstr
= native
.text
126 jstr
.delete_local_ref
130 # Set the text of this view
131 fun text
=(value
: Text)
133 var jstr
= value
.to_s
.to_java_string
135 jstr
.delete_local_ref
138 # Get whether this view is enabled or not
139 fun enabled
: Bool do return native
.enabled
141 # Set if this view is enabled
142 fun enabled
=(val
: Bool) do native
.enabled
= val
144 # Set the size of the text in this view at `dpi`
145 fun text_size
=(dpi
: Numeric) do native
.text_size
= dpi
.to_f
147 private var finalized
= false
150 if not finalized
then
151 native
.delete_global_ref
161 redef type NATIVE: NativeButton
165 var native
= new NativeButton(app
.native_activity
, self)
166 self.native
= native
.new_global_ref
171 # By default, this method calls `app.catch_event`. It can be specialized
172 # with custom behavior or the receiver of `catch_event` can be changed
173 # with `event_catcher=`.
174 fun click
(event
: AppEvent) do event_catcher
.catch_event
(event
)
176 private fun click_from_native
do click
(new ClickEvent(self))
179 # An Android editable text field
183 redef type NATIVE: NativeEditText
187 var native
= new NativeEditText(app
.activities
.first
.native
)
188 self.native
= native
.new_global_ref
196 # A `View` for Android
197 extern class NativeView in "Java" `{ android.view.View `}
200 fun minimum_width=(val: Int) in "Java" `{ recv.setMinimumWidth((int)val); `}
201 fun minimum_height
=(val
: Int) in "Java" `{ recv.setMinimumHeight((int)val); `}
204 # A collection of `NativeView`
205 extern class NativeViewGroup in "Java" `{ android.view.ViewGroup `}
208 fun add_view
(view
: NativeView) in "Java" `{ recv.addView(view); `}
211 # A `NativeViewGroup` organized in a line
212 extern class NativeLinearLayout in "Java" `{ android.widget.LinearLayout `}
213 super NativeViewGroup
215 new(context
: NativeActivity) in "Java" `{ return new LinearLayout(context); `}
217 fun set_vertical in "Java" `{ recv.setOrientation(LinearLayout.VERTICAL); `}
218 fun set_horizontal
in "Java" `{ recv.setOrientation(LinearLayout.HORIZONTAL); `}
220 redef fun add_view(view) in "Java"
222 MarginLayoutParams params
= new MarginLayoutParams(
223 LinearLayout.LayoutParams.MATCH_PARENT,
224 LinearLayout.LayoutParams.WRAP_CONTENT);
225 recv
.addView
(view
, params
);
228 fun add_view_with_weight(view: NativeView, weight: Float)
230 recv
.addView
(view
, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, (float
)weight
));
234 # A `NativeViewGroup` organized as a grid
235 extern class NativeGridLayout in "Java" `{ android.widget.GridLayout `}
236 super NativeViewGroup
238 new(context
: NativeActivity) in "Java" `{ return new android.widget.GridLayout(context); `}
240 fun row_count=(val: Int) in "Java" `{ recv.setRowCount((int)val); `}
242 fun column_count
=(val
: Int) in "Java" `{ recv.setColumnCount((int)val); `}
244 redef fun add_view(view) in "Java" `{ recv.addView(view); `}
247 extern class NativePopupWindow in "Java" `{ android.widget.PopupWindow `}
250 new (context: NativeActivity) in "Java" `{
251 PopupWindow recv
= new PopupWindow(context
);
252 recv
.setWindowLayoutMode
(LinearLayout.LayoutParams.MATCH_PARENT,
253 LinearLayout.LayoutParams.MATCH_PARENT);
254 recv
.setClippingEnabled
(false);
258 fun content_view=(layout: NativeViewGroup) in "Java" `{ recv.setContentView(layout); `}
261 extern class NativeTextView in "Java" `{ android.widget.TextView `}
264 new (context: NativeActivity) in "Java" `{ return new TextView(context); `}
266 fun text
: JavaString in "Java" `{ return recv.getText().toString(); `}
268 fun text=(value: JavaString) in "Java" `{
270 final
TextView final_recv
= recv
;
271 final
String final_value
= value
;
273 ((Activity)recv
.getContext
()).runOnUiThread
(new Runnable() {
276 final_recv
.setText
(final_value
);
281 fun enabled: Bool in "Java" `{ return recv.isEnabled(); `}
282 fun enabled
=(value
: Bool) in "Java" `{
283 final TextView final_recv = recv;
284 final boolean final_value = value;
286 ((Activity)recv.getContext()).runOnUiThread(new Runnable() {
289 final_recv.setEnabled(final_value);
294 fun gravity_center
in "Java" `{
295 recv.setGravity(Gravity.CENTER);
298 fun text_size
=(dpi
: Float) in "Java" `{
299 recv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, (float)dpi);
303 extern class NativeEditText in "Java" `{ android.widget.EditText `}
306 redef type SELF: NativeEditText
308 new (context: NativeActivity) in "Java" `{ return new android.widget.EditText(context); `}
310 fun width
=(val
: Int) in "Java" `{ recv.setWidth((int)val); `}
312 fun input_type_text in "Java" `{ recv.setInputType(android.text.InputType.TYPE_CLASS_TEXT); `}
314 redef fun new_global_ref
: SELF import sys
, Sys.jni_env
`{
315 Sys sys = NativeEditText_sys(recv);
316 JNIEnv *env = Sys_jni_env(sys);
317 return (*env)->NewGlobalRef(env, recv);
321 extern class NativeButton in "Java" `{ android.widget.Button `}
324 redef type SELF: NativeButton
326 new (context: NativeActivity, sender_object: Object)
327 import Button.click_from_native in "Java" `{
328 final int final_sender_object
= sender_object
;
330 return new Button(context
){
332 public boolean onTouchEvent
(MotionEvent event
) {
333 if(event
.getAction
() == MotionEvent.ACTION_DOWN) {
334 Button_click_from_native(final_sender_object
);
342 redef fun new_global_ref: SELF import sys, Sys.jni_env `{
343 Sys sys
= NativeButton_sys(recv
);
344 JNIEnv *env
= Sys_jni_env(sys
);
345 return (*env
)->NewGlobalRef(env
, recv
);