share/libgc: add a feature to script to use a local version of the source pkgs
[nit.git] / lib / android / ui / ui.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Views and services to use the Android native user interface
18 module ui
19
20 import native_ui
21
22 # An event from the `app.nit` framework
23 interface AppEvent
24 # Reaction to this event
25 fun react do end
26 end
27
28 # A control click event
29 class ClickEvent
30 super AppEvent
31
32 # Sender of this event
33 var sender: Button
34
35 redef fun react do sender.click self
36 end
37
38 # Receiver of events not handled directly by the sender
39 interface EventCatcher
40 fun catch_event(event: AppEvent) do end
41 end
42
43 redef class App
44 super EventCatcher
45 end
46
47 # An `Object` that raises events
48 abstract class Eventful
49 var event_catcher: EventCatcher = app is lazy, writable
50 end
51
52 #
53 ## Nity classes and services
54 #
55
56 # An Android control with text
57 abstract class TextView
58 super Finalizable
59 super Eventful
60
61 # Native Java variant to this Nity class
62 type NATIVE: NativeTextView
63
64 # The native Java object encapsulated by `self`
65 var native: NATIVE is noinit
66
67 # Get the text of this view
68 fun text: String
69 do
70 var jstr = native.text
71 var str = jstr.to_s
72 jstr.delete_local_ref
73 return str
74 end
75
76 # Set the text of this view
77 fun text=(value: Text)
78 do
79 var jstr = value.to_s.to_java_string
80 native.text = jstr
81 jstr.delete_local_ref
82 end
83
84 # Get whether this view is enabled or not
85 fun enabled: Bool do return native.enabled
86
87 # Set if this view is enabled
88 fun enabled=(val: Bool) do native.enabled = val
89
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
92
93 private var finalized = false
94 redef fun finalize
95 do
96 if not finalized then
97 native.delete_global_ref
98 finalized = true
99 end
100 end
101 end
102
103 # An Android button
104 class Button
105 super TextView
106
107 redef type NATIVE: NativeButton
108
109 init
110 do
111 var native = new NativeButton(app.native_activity, self)
112 self.native = native.new_global_ref
113 end
114
115 # Click event
116 #
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)
121
122 private fun click_from_native do click(new ClickEvent(self))
123 end
124
125 # An Android editable text field
126 class EditText
127 super TextView
128
129 redef type NATIVE: NativeEditText
130
131 init
132 do
133 var native = new NativeEditText(app.activities.first.native)
134 self.native = native.new_global_ref
135 end
136 end
137
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;
142
143 return new android.widget.Button(context){
144 @Override
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);
148 return true;
149 }
150 return false;
151 }
152 };
153 `}
154 end