Skip to content

Commit 70240a1

Browse files
committed
Merge: app::ui: intro Label and apply many other fixes
Most features are self-explanatory, but here it is anyway. `Label` defines a simple non-editable text field. And the newly public `remove` method remove views from other views (such as `Layouts`). Other fixes clean up the relation with the Java GC and multi-threads behavior. Pull-Request: #1827 Reviewed-by: Jean Privat <jean@pryen.org>
2 parents f251627 + 35bbd20 commit 70240a1

File tree

4 files changed

+69
-41
lines changed

4 files changed

+69
-41
lines changed

lib/android/ui/native_ui.nit

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,7 @@ redef extern class NativeActivity
4141

4242
# Set the main layout of this activity
4343
fun content_view=(layout: NativeViewGroup) in "Java" `{
44-
final ViewGroup final_layout = layout;
45-
final Activity final_self = self;
46-
47-
self.runOnUiThread(new Runnable() {
48-
@Override
49-
public void run() {
50-
final_self.setContentView(final_layout);
51-
52-
final_layout.requestFocus();
53-
}
54-
});
44+
self.setContentView(layout);
5545
`}
5646
end
5747

@@ -63,17 +53,7 @@ extern class NativeView in "Java" `{ android.view.View `}
6353
fun minimum_height=(val: Int) in "Java" `{ self.setMinimumHeight((int)val); `}
6454

6555
fun enabled: Bool in "Java" `{ return self.isEnabled(); `}
66-
fun enabled=(value: Bool) in "Java" `{
67-
final View final_self = self;
68-
final boolean final_value = value;
69-
70-
((Activity)self.getContext()).runOnUiThread(new Runnable() {
71-
@Override
72-
public void run() {
73-
final_self.setEnabled(final_value);
74-
}
75-
});
76-
`}
56+
fun enabled=(value: Bool) in "Java" `{ self.setEnabled(value); `}
7757
end
7858

7959
# A collection of `NativeView`
@@ -82,6 +62,8 @@ extern class NativeViewGroup in "Java" `{ android.view.ViewGroup `}
8262

8363
fun add_view(view: NativeView) in "Java" `{ self.addView(view); `}
8464

65+
fun remove_view(view: NativeView) in "Java" `{ self.removeView(view); `}
66+
8567
fun add_view_with_weight(view: NativeView, weight: Float)
8668
in "Java" `{
8769
self.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, (float)weight));
@@ -104,6 +86,12 @@ extern class NativeLinearLayout in "Java" `{ android.widget.LinearLayout `}
10486
LinearLayout.LayoutParams.WRAP_CONTENT);
10587
self.addView(view, params);
10688
`}
89+
90+
redef fun new_global_ref import sys, Sys.jni_env `{
91+
Sys sys = NativeLinearLayout_sys(self);
92+
JNIEnv *env = Sys_jni_env(sys);
93+
return (*env)->NewGlobalRef(env, self);
94+
`}
10795
end
10896

10997
# A `NativeViewGroup` organized as a grid
@@ -117,6 +105,12 @@ extern class NativeGridLayout in "Java" `{ android.widget.GridLayout `}
117105
fun column_count=(val: Int) in "Java" `{ self.setColumnCount((int)val); `}
118106

119107
redef fun add_view(view) in "Java" `{ self.addView(view); `}
108+
109+
redef fun new_global_ref import sys, Sys.jni_env `{
110+
Sys sys = NativeGridLayout_sys(self);
111+
JNIEnv *env = Sys_jni_env(sys);
112+
return (*env)->NewGlobalRef(env, self);
113+
`}
120114
end
121115

122116
extern class NativePopupWindow in "Java" `{ android.widget.PopupWindow `}
@@ -131,6 +125,12 @@ extern class NativePopupWindow in "Java" `{ android.widget.PopupWindow `}
131125
`}
132126

133127
fun content_view=(layout: NativeViewGroup) in "Java" `{ self.setContentView(layout); `}
128+
129+
redef fun new_global_ref import sys, Sys.jni_env `{
130+
Sys sys = NativePopupWindow_sys(self);
131+
JNIEnv *env = Sys_jni_env(sys);
132+
return (*env)->NewGlobalRef(env, self);
133+
`}
134134
end
135135

136136
extern class NativeTextView in "Java" `{ android.widget.TextView `}
@@ -140,18 +140,7 @@ extern class NativeTextView in "Java" `{ android.widget.TextView `}
140140

141141
fun text: JavaString in "Java" `{ return self.getText().toString(); `}
142142

143-
fun text=(value: JavaString) in "Java" `{
144-
145-
final TextView final_self = self;
146-
final String final_value = value;
147-
148-
((Activity)self.getContext()).runOnUiThread(new Runnable() {
149-
@Override
150-
public void run() {
151-
final_self.setText(final_value);
152-
}
153-
});
154-
`}
143+
fun text=(value: JavaString) in "Java" `{ self.setText(value); `}
155144

156145
fun gravity_center in "Java" `{
157146
self.setGravity(Gravity.CENTER);
@@ -163,6 +152,12 @@ extern class NativeTextView in "Java" `{ android.widget.TextView `}
163152
fun text_size=(dpi: Float) in "Java" `{
164153
self.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, (float)dpi);
165154
`}
155+
156+
redef fun new_global_ref import sys, Sys.jni_env `{
157+
Sys sys = NativeTextView_sys(self);
158+
JNIEnv *env = Sys_jni_env(sys);
159+
return (*env)->NewGlobalRef(env, self);
160+
`}
166161
end
167162

168163
extern class NativeEditText in "Java" `{ android.widget.EditText `}
@@ -176,7 +171,7 @@ extern class NativeEditText in "Java" `{ android.widget.EditText `}
176171

177172
fun input_type_text in "Java" `{ self.setInputType(android.text.InputType.TYPE_CLASS_TEXT); `}
178173

179-
redef fun new_global_ref: SELF import sys, Sys.jni_env `{
174+
redef fun new_global_ref import sys, Sys.jni_env `{
180175
Sys sys = NativeEditText_sys(self);
181176
JNIEnv *env = Sys_jni_env(sys);
182177
return (*env)->NewGlobalRef(env, self);
@@ -188,7 +183,7 @@ extern class NativeButton in "Java" `{ android.widget.Button `}
188183

189184
redef type SELF: NativeButton
190185

191-
redef fun new_global_ref: SELF import sys, Sys.jni_env `{
186+
redef fun new_global_ref import sys, Sys.jni_env `{
192187
Sys sys = NativeButton_sys(self);
193188
JNIEnv *env = Sys_jni_env(sys);
194189
return (*env)->NewGlobalRef(env, self);

lib/android/ui/ui.nit

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ redef class Control
3737
end
3838

3939
redef class Window
40-
redef var native = app.native_activity
40+
redef var native = app.native_activity.new_global_ref
4141

4242
redef type NATIVE: NativeActivity
4343

@@ -70,11 +70,18 @@ redef class Layout
7070
# FIXME abstract the use either homogeneous or weight to balance views size in a layout
7171
native.add_view_with_weight(item.native, 1.0)
7272
end
73+
74+
redef fun remove(item)
75+
do
76+
super
77+
if item isa View then native.remove_view item.native
78+
end
7379
end
7480

7581
redef class HorizontalLayout
7682
redef var native do
7783
var layout = new NativeLinearLayout(app.native_activity)
84+
layout = layout.new_global_ref
7885
layout.set_horizontal
7986
return layout
8087
end
@@ -83,6 +90,7 @@ end
8390
redef class VerticalLayout
8491
redef var native do
8592
var layout = new NativeLinearLayout(app.native_activity)
93+
layout = layout.new_global_ref
8694
layout.set_vertical
8795
return layout
8896
end
@@ -106,6 +114,11 @@ redef class TextView
106114
end
107115
end
108116

117+
redef class Label
118+
redef type NATIVE: NativeTextView
119+
redef var native do return (new NativeTextView(app.native_activity)).new_global_ref
120+
end
121+
109122
redef class TextInput
110123
redef type NATIVE: NativeEditText
111124
redef var native = (new NativeEditText(app.native_activity)).new_global_ref

lib/app/ui.nit

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ class CompositeControl
112112
protected fun add(item: Control) do items.add item
113113

114114
# Remove `item` from `self`
115-
protected fun remove(item: Control) do if has(item) then items.remove item
115+
fun remove(item: Control) do if has(item) then items.remove item
116116

117117
# Is `item` in `self`?
118-
protected fun has(item: Control): Bool do return items.has(item)
118+
fun has(item: Control): Bool do return items.has(item)
119119

120120
redef fun on_create do for i in items do i.on_create
121121

@@ -146,7 +146,7 @@ abstract class View
146146
# Is this control enabled so the user can interact with it?
147147
#
148148
# By default, or if set to `null`, the control is enabled.
149-
var enabled: nullable Bool is writable #, abstract FIXME with #1311
149+
var enabled: nullable Bool is writable, abstract, autoinit
150150
end
151151

152152
# A control with some `text`
@@ -156,7 +156,7 @@ abstract class TextView
156156
# Main `Text` of this control
157157
#
158158
# By default, or if set to `null`, no text is shown.
159-
var text: nullable Text is writable #, abstract FIXME with #1311
159+
var text: nullable Text is writable, abstract, autoinit
160160
end
161161

162162
# A control for the user to enter custom `text`
@@ -169,6 +169,11 @@ class Button
169169
super TextView
170170
end
171171

172+
# A text label
173+
class Label
174+
super TextView
175+
end
176+
172177
# A `Button` press event
173178
class ButtonPressEvent
174179
super AppEvent

lib/linux/ui.nit

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,19 @@ redef class Window
8383
end
8484

8585
redef class View
86+
init do native.show
87+
8688
redef fun enabled do return native.sensitive
8789
redef fun enabled=(enabled) do native.sensitive = enabled or else true
8890
end
8991

9092
redef class Layout
9193
redef type NATIVE: GtkBox
94+
redef fun remove(view)
95+
do
96+
super
97+
native.remove view.native
98+
end
9299
end
93100

94101
redef class HorizontalLayout
@@ -127,6 +134,14 @@ redef class Button
127134
init do native.signal_connect("clicked", self, null)
128135
end
129136

137+
redef class Label
138+
redef type NATIVE: GtkLabel
139+
redef var native = new GtkLabel("")
140+
141+
redef fun text do return native.text
142+
redef fun text=(value) do native.text = (value or else "").to_s
143+
end
144+
130145
redef class TextInput
131146
redef type NATIVE: GtkEntry
132147
redef var native = new GtkEntry

0 commit comments

Comments
 (0)