lib/android/ui: use more global references
[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 end
74
75 redef class HorizontalLayout
76 redef var native do
77 var layout = new NativeLinearLayout(app.native_activity)
78 layout = layout.new_global_ref
79 layout.set_horizontal
80 return layout
81 end
82 end
83
84 redef class VerticalLayout
85 redef var native do
86 var layout = new NativeLinearLayout(app.native_activity)
87 layout = layout.new_global_ref
88 layout.set_vertical
89 return layout
90 end
91 end
92
93 redef class TextView
94 redef type NATIVE: NativeTextView
95
96 redef fun text do return native.text.to_s
97 redef fun text=(value) do
98 if value == null then value = ""
99 native.text = value.to_java_string
100 end
101
102 # Size of the text
103 fun text_size: Float do return native.text_size
104
105 # Size of the text
106 fun text_size=(text_size: nullable Float) do
107 if text_size != null then native.text_size = text_size
108 end
109 end
110
111 redef class TextInput
112 redef type NATIVE: NativeEditText
113 redef var native = (new NativeEditText(app.native_activity)).new_global_ref
114 end
115
116 redef class Button
117 super Finalizable
118
119 redef type NATIVE: NativeButton
120 redef var native = (new NativeButton(app.native_activity, self)).new_global_ref
121
122 private fun on_click do notify_observers new ButtonPressEvent(self)
123
124 redef fun finalize do native.delete_global_ref
125 end
126
127 redef class NativeButton
128 private new (context: NativeActivity, sender_object: Button)
129 import Button.on_click in "Java" `{
130 final int final_sender_object = sender_object;
131
132 return new android.widget.Button(context){
133 @Override
134 public boolean onTouchEvent(android.view.MotionEvent event) {
135 if(event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
136 Button_on_click(final_sender_object);
137 return true;
138 }
139 return false;
140 }
141 };
142 `}
143 end