656e20988310b88450592d210f217985b127692d
[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
31 redef class Control
32 # The Android element used to implement `self`
33 fun native: NATIVE is abstract
34
35 # Type of `native`
36 type NATIVE: JavaObject
37 end
38
39 redef class Window
40 redef var native = app.native_activity.new_global_ref
41
42 redef type NATIVE: NativeActivity
43
44 redef fun add(item)
45 do
46 super
47
48 # FIXME abstract the Android restriction where `content_view` must be a layout
49 assert item isa Layout
50 native.content_view = item.native
51 end
52 end
53
54 redef class View
55 redef type NATIVE: NativeView
56
57 redef fun enabled=(enabled) do native.enabled = enabled or else true
58 redef fun enabled do return native.enabled
59 end
60
61 redef class Layout
62 redef type NATIVE: NativeViewGroup
63
64 redef fun add(item)
65 do
66 super
67
68 assert item isa View
69
70 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
71 native.add_view_with_weight(item.native, 1.0)
72 end
73
74 redef fun remove(item)
75 do
76 super
77 if item isa View then native.remove_view item.native
78 end
79 end
80
81 redef class HorizontalLayout
82 redef var native do
83 var layout = new NativeLinearLayout(app.native_activity)
84 layout = layout.new_global_ref
85 layout.set_horizontal
86 return layout
87 end
88 end
89
90 redef class VerticalLayout
91 redef var native do
92 var layout = new NativeLinearLayout(app.native_activity)
93 layout = layout.new_global_ref
94 layout.set_vertical
95 return layout
96 end
97 end
98
99 redef class ListLayout
100 redef type NATIVE: Android_widget_ListView
101
102 redef var native do
103 var layout = new Android_widget_ListView(app.native_activity)
104 layout = layout.new_global_ref
105 return layout
106 end
107
108 private var adapter: Android_widget_ArrayAdapter do
109 var adapter = new Android_widget_ArrayAdapter(app.native_activity,
110 android_r_layout_simple_list_item_1, self)
111 native.set_adapter adapter
112 return adapter
113 end
114
115 redef fun add(item)
116 do
117 super
118 if item isa View then adapter.add item.native
119 end
120
121 private fun create_view(position: Int): NativeView
122 do
123 var ctrl = items[position]
124 assert ctrl isa View
125 return ctrl.native
126 end
127 end
128
129 redef class Android_widget_ArrayAdapter
130 private new (context: NativeContext, res: Int, sender: ListLayout)
131 import ListLayout.create_view in "Java" `{
132 final int final_sender_object = sender;
133
134 return new android.widget.ArrayAdapter(context, (int)res) {
135 @Override
136 public android.view.View getView(int position, android.view.View convertView, android.view.ViewGroup parent) {
137 return ListLayout_create_view(final_sender_object, position);
138 }
139 };
140 `}
141 end
142
143 redef class TextView
144 redef type NATIVE: NativeTextView
145
146 redef fun text do return native.text.to_s
147 redef fun text=(value) do
148 if value == null then value = ""
149 native.text = value.to_java_string
150 end
151
152 # Size of the text
153 fun text_size: Float do return native.text_size
154
155 # Size of the text
156 fun text_size=(text_size: nullable Float) do
157 if text_size != null then native.text_size = text_size
158 end
159 end
160
161 redef class Label
162 redef type NATIVE: NativeTextView
163 redef var native do return (new NativeTextView(app.native_activity)).new_global_ref
164
165 init do native.set_text_appearance(app.native_activity, android_r_style_text_appearance_medium)
166 end
167
168 redef class TextInput
169 redef type NATIVE: NativeEditText
170 redef var native = (new NativeEditText(app.native_activity)).new_global_ref
171 end
172
173 redef class Button
174 super Finalizable
175
176 redef type NATIVE: NativeButton
177 redef var native = (new NativeButton(app.native_activity, self)).new_global_ref
178
179 private fun on_click do notify_observers new ButtonPressEvent(self)
180
181 redef fun finalize do native.delete_global_ref
182 end
183
184 redef class NativeButton
185 private new (context: NativeActivity, sender_object: Button)
186 import Button.on_click in "Java" `{
187 final int final_sender_object = sender_object;
188
189 return new android.widget.Button(context){
190 @Override
191 public boolean onTouchEvent(android.view.MotionEvent event) {
192 if(event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
193 Button_on_click(final_sender_object);
194 return true;
195 }
196 return false;
197 }
198 };
199 `}
200 end