# ~~~
module ui is min_api_version 14
-import native_app_glue
+import nit_activity
import pthreads::concurrent_collections
in "Java" `{
- import android.app.NativeActivity;
+ import android.app.Activity;
import android.view.Gravity;
import android.view.MotionEvent;
redef class App
super EventCatcher
-
- # Queue of events to be received by the main thread
- var event_queue = new ConcurrentList[AppEvent]
-
- # Call `react` on all `AppEvent` available in `event_queue`
- protected fun loop_on_ui_callbacks
- do
- var queue = event_queue
- while not queue.is_empty do
- var event = queue.pop
- event.react
- end
- end
-
- redef fun run
- do
- loop
- # Process Android events
- poll_looper 100
-
- # Process app.nit events
- loop_on_ui_callbacks
- end
- end
end
redef extern class NativeActivity
- # Fill this entire `NativeActivity` with `popup`
- #
- # This is a workaround for the use on `takeSurface` in `NativeActivity.java`
- #
- # TODO replace NativeActivity by our own NitActivity
- private fun dedicate_to_popup(popup: NativePopupWindow, popup_layout: NativeViewGroup) in "Java" `{
- final LinearLayout final_main_layout = new LinearLayout(recv);
- final ViewGroup final_popup_layout = popup_layout;
- final PopupWindow final_popup = popup;
- final NativeActivity final_recv = recv;
-
- recv.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- MarginLayoutParams params = new MarginLayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.MATCH_PARENT);
-
- final_recv.setContentView(final_main_layout, params);
-
- final_popup.showAtLocation(final_popup_layout, Gravity.TOP, 0, 40);
- }
- });
- `}
-
# Set the main layout of this activity
- fun content_view=(layout: NativeViewGroup)
- do
- var popup = new NativePopupWindow(self)
- popup.content_view = layout
- dedicate_to_popup(popup, layout)
- end
-
- # Set the real content view of this activity, without hack
- #
- # TODO bring use this instead of the hack with `dedicate_to_pupup`
- private fun real_content_view=(layout: NativeViewGroup) in "Java" `{
+ fun content_view=(layout: NativeViewGroup) in "Java" `{
final ViewGroup final_layout = layout;
- final NativeActivity final_recv = recv;
+ final Activity final_recv = recv;
recv.runOnUiThread(new Runnable() {
@Override
init
do
- var native = new NativeButton(app.native_activity, app.event_queue, self)
+ var native = new NativeButton(app.native_activity, self)
self.native = native.new_global_ref
end
- # Click event on the Main thread
+ # Click event
#
# By default, this method calls `app.catch_event`. It can be specialized
# with custom behavior or the receiver of `catch_event` can be changed
# with `event_catcher=`.
fun click(event: AppEvent) do event_catcher.catch_event(event)
- # Click event on the UI thread
- #
- # This method is called on the UI thread and redirects the event to `click`
- # throught `App::event_queue`. In most cases, you should implement `click`
- # and leave `click_ui` as is.
- fun click_ui do app.event_queue.add(new ClickEvent(self))
+ private fun click_from_native do click(new ClickEvent(self))
end
# An Android editable text field
init
do
- var native = new NativeEditText(app.native_activity)
+ var native = new NativeEditText(app.activities.first.native)
self.native = native.new_global_ref
end
end
final TextView final_recv = recv;
final String final_value = value;
- ((NativeActivity)recv.getContext()).runOnUiThread(new Runnable() {
+ ((Activity)recv.getContext()).runOnUiThread(new Runnable() {
@Override
public void run() {
final_recv.setText(final_value);
final TextView final_recv = recv;
final boolean final_value = value;
- ((NativeActivity)recv.getContext()).runOnUiThread(new Runnable() {
+ ((Activity)recv.getContext()).runOnUiThread(new Runnable() {
@Override
public void run() {
final_recv.setEnabled(final_value);
redef type SELF: NativeButton
- new (context: NativeActivity, queue: ConcurrentList[AppEvent], sender_object: Object) import Button.click_ui in "Java" `{
+ new (context: NativeActivity, sender_object: Object)
+ import Button.click_from_native in "Java" `{
final int final_sender_object = sender_object;
return new Button(context){
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
- Button_click_ui(final_sender_object);
+ Button_click_from_native(final_sender_object);
return true;
}
return false;