b6555294a8efd85278c5967fdf3393a8c2fdf458
[nit.git] / lib / android / ui / ui.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Views and services to use the Android native user interface
16 module ui
17
18 # Implementation note:
19 #
20 # We cannot rely on `Activity::on_restore_instance_state` to implement
21 # `on_restore_state` is it only invoked if there is a bundled state,
22 # and we don't use the Android bundled state.
23
24 import native_ui
25 import log
26 import nit_activity
27
28 import app::ui
29 private import data_store
30 import assets
31
32 redef class Control
33 # The Android element used to implement `self`
34 fun native: NATIVE is abstract
35
36 # Type of `native`
37 type NATIVE: JavaObject
38 end
39
40 redef class Window
41 redef var native = app.native_activity.new_global_ref
42
43 redef type NATIVE: NativeActivity
44
45 redef fun add(item)
46 do
47 super
48
49 # FIXME abstract the Android restriction where `content_view` must be a layout
50 assert item isa Layout
51 native.content_view = item.native
52 end
53 end
54
55 redef class View
56 redef type NATIVE: NativeView
57
58 redef fun enabled=(enabled) do native.enabled = enabled or else true
59 redef fun enabled do return native.enabled
60 end
61
62 redef class Layout
63 redef type NATIVE: NativeViewGroup
64
65 redef fun add(item)
66 do
67 super
68
69 assert item isa View
70
71 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
72 native.add_view_with_weight(item.native, 1.0)
73 end
74
75 redef fun remove(item)
76 do
77 super
78 if item isa View then native.remove_view item.native
79 end
80 end
81
82 redef class HorizontalLayout
83 redef var native do
84 var layout = new NativeLinearLayout(app.native_activity)
85 layout = layout.new_global_ref
86 layout.set_horizontal
87 return layout
88 end
89 end
90
91 redef class VerticalLayout
92 redef var native do
93 var layout = new NativeLinearLayout(app.native_activity)
94 layout = layout.new_global_ref
95 layout.set_vertical
96 return layout
97 end
98 end
99
100 redef class ListLayout
101 redef type NATIVE: Android_widget_ListView
102
103 redef var native do
104 var layout = new Android_widget_ListView(app.native_activity)
105 layout = layout.new_global_ref
106 return layout
107 end
108
109 private var adapter: Android_widget_ArrayAdapter do
110 var adapter = new Android_widget_ArrayAdapter(app.native_activity,
111 android_r_layout_simple_list_item_1, self)
112 native.set_adapter adapter
113 return adapter.new_global_ref
114 end
115
116 redef fun add(item)
117 do
118 super
119 if item isa View then adapter.add item.native
120 end
121
122 private fun create_view(position: Int): NativeView
123 do
124 var ctrl = items[position]
125 assert ctrl isa View
126 return ctrl.native
127 end
128 end
129
130 redef class Android_widget_ArrayAdapter
131 private new (context: NativeContext, res: Int, sender: ListLayout)
132 import ListLayout.create_view in "Java" `{
133 final int final_sender_object = sender;
134
135 return new android.widget.ArrayAdapter(context, (int)res) {
136 @Override
137 public android.view.View getView(int position, android.view.View convertView, android.view.ViewGroup parent) {
138 return ListLayout_create_view(final_sender_object, position);
139 }
140 };
141 `}
142 end
143
144 redef class TextView
145 redef type NATIVE: NativeTextView
146
147 redef fun text do return native.text.to_s
148 redef fun text=(value) do
149 if value == null then value = ""
150 native.text = value.to_java_string
151 end
152
153 # Size of the text
154 fun text_size: Float do return native.text_size
155
156 # Size of the text
157 fun text_size=(text_size: nullable Float) do
158 if text_size != null then native.text_size = text_size
159 end
160 end
161
162 redef class Label
163 redef type NATIVE: NativeTextView
164 redef var native do return (new NativeTextView(app.native_activity)).new_global_ref
165
166 init do native.set_text_appearance(app.native_activity, android_r_style_text_appearance_medium)
167 end
168
169 redef class TextInput
170 redef type NATIVE: NativeEditText
171 redef var native = (new NativeEditText(app.native_activity)).new_global_ref
172 end
173
174 redef class Button
175 super Finalizable
176
177 redef type NATIVE: NativeButton
178 redef var native = (new NativeButton(app.native_activity, self)).new_global_ref
179
180 private fun on_click do notify_observers new ButtonPressEvent(self)
181
182 redef fun finalize do native.delete_global_ref
183 end
184
185 redef class NativeButton
186 private new (context: NativeActivity, sender_object: Button)
187 import Button.on_click in "Java" `{
188 final int final_sender_object = sender_object;
189
190 return new android.widget.Button(context){
191 @Override
192 public boolean onTouchEvent(android.view.MotionEvent event) {
193 if(event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
194 Button_on_click(final_sender_object);
195 return true;
196 }
197 return false;
198 }
199 };
200 `}
201 end