benches/strings: add .gitignore and `make clean`
[nit.git] / lib / android / ui.nit
index b4ed822..6d0e0f8 100644 (file)
@@ -21,7 +21,7 @@
 # methods of the main thread to customize the response to a given event.
 #
 # This graph shows the path of a button click:
-# ~~~
+# ~~~raw
 #     UI Thread     #   Main thread
 #
 #       User
 # ~~~
 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;
@@ -78,73 +78,14 @@ end
 
 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
@@ -221,23 +162,18 @@ class Button
 
        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
@@ -248,7 +184,7 @@ class EditText
 
        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
@@ -331,22 +267,15 @@ extern class NativeTextView in "Java" `{ android.widget.TextView `}
 
        fun text=(value: JavaString) in "Java" `{
 
-               android.util.Log.d("Nity", "1");
                final TextView final_recv = recv;
                final String final_value = value;
 
-               android.util.Log.d("Nity", "4");
-               ((NativeActivity)recv.getContext()).runOnUiThread(new Runnable() {
+               ((Activity)recv.getContext()).runOnUiThread(new Runnable() {
                        @Override
                        public void run()  {
-                               android.util.Log.d("Nity", "-5");
-                               android.util.Log.d("Nity", final_value);
-                               android.util.Log.d("Nity", "-5.5");
                                final_recv.setText(final_value);
-                               android.util.Log.d("Nity", "-6");
                        }
                });
-               android.util.Log.d("Nity", "7");
        `}
 
        fun enabled: Bool in "Java" `{ return recv.isEnabled(); `}
@@ -354,7 +283,7 @@ extern class NativeTextView in "Java" `{ android.widget.TextView `}
                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);
@@ -394,14 +323,15 @@ extern class NativeButton in "Java" `{ android.widget.Button `}
 
        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;