# ~~~
module ui is min_api_version 14
-import native_app_glue
+import nit_activity
import pthreads::concurrent_collections
in "Java" `{
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
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
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;