From a7af20a384f7ab37984c83ead5d7b10b87506bea Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Tue, 27 Jan 2015 17:32:12 -0500 Subject: [PATCH] lib/android: update `ui` to use NitActivity and be on the UI thread MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/android/ui.nit | 44 ++++++++------------------------------------ 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/lib/android/ui.nit b/lib/android/ui.nit index 9bd4fc0..6d0e0f8 100644 --- a/lib/android/ui.nit +++ b/lib/android/ui.nit @@ -34,7 +34,7 @@ # ~~~ module ui is min_api_version 14 -import native_app_glue +import nit_activity import pthreads::concurrent_collections in "Java" `{ @@ -78,30 +78,6 @@ 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 @@ -186,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 @@ -213,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 @@ -352,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; -- 1.7.9.5