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
22 # An event from the `app.nit` framework
24 # Reaction to this event
28 # A control click event
32 # Sender of this event
35 redef fun react
do sender
.click
self
38 # Receiver of events not handled directly by the sender
39 interface EventCatcher
40 fun catch_event
(event
: AppEvent) do end
47 # An `Object` that raises events
48 abstract class Eventful
49 var event_catcher
: EventCatcher = app
is lazy
, writable
53 ## Nity classes and services
56 # An Android control with text
57 abstract class TextView
61 # Native Java variant to this Nity class
62 type NATIVE: NativeTextView
64 # The native Java object encapsulated by `self`
65 var native
: NATIVE is noinit
67 # Get the text of this view
70 var jstr
= native
.text
76 # Set the text of this view
77 fun text
=(value
: Text)
79 var jstr
= value
.to_s
.to_java_string
84 # Get whether this view is enabled or not
85 fun enabled
: Bool do return native
.enabled
87 # Set if this view is enabled
88 fun enabled
=(val
: Bool) do native
.enabled
= val
90 # Set the size of the text in this view at `dpi`
91 fun text_size
=(dpi
: Numeric) do native
.text_size
= dpi
.to_f
93 private var finalized
= false
97 native
.delete_global_ref
107 redef type NATIVE: NativeButton
111 var native
= new NativeButton(app
.native_activity
, self)
112 self.native
= native
.new_global_ref
117 # By default, this method calls `app.catch_event`. It can be specialized
118 # with custom behavior or the receiver of `catch_event` can be changed
119 # with `event_catcher=`.
120 fun click
(event
: AppEvent) do event_catcher
.catch_event
(event
)
122 private fun click_from_native
do click
(new ClickEvent(self))
125 # An Android editable text field
129 redef type NATIVE: NativeEditText
133 var native
= new NativeEditText(app
.activities
.first
.native
)
134 self.native
= native
.new_global_ref
138 redef class NativeButton
139 new (context
: NativeActivity, sender_object
: Object)
140 import Button.click_from_native
in "Java" `{
141 final int final_sender_object = sender_object;
143 return new android.widget.Button(context){
145 public boolean onTouchEvent(android.view.MotionEvent event) {
146 if(event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
147 Button_click_from_native(final_sender_object);