android, ios & linux: implement `open_in_browser`
[nit.git] / lib / android / ui / ui.nit
index f9cb4d0..7e14b19 100644 (file)
 # limitations under the License.
 
 # Views and services to use the Android native user interface
-module ui
+module ui is
+       # `adjustPan` allows to use EditText in a ListLayout
+       android_manifest_activity """android:windowSoftInputMode="adjustPan""""
+end
 
 # Implementation note:
 #
@@ -78,6 +81,19 @@ redef class App
        end
 end
 
+redef class Activity
+       redef fun on_back_pressed
+       do
+               var window = app.window
+               if window.enable_back_button then
+                       window.on_back_button
+                       return true
+               end
+
+               return false
+       end
+end
+
 # On Android, a window is implemented with the fragment `native`
 redef class Window
        redef var native = (new Android_app_Fragment(self)).new_global_ref
@@ -201,20 +217,65 @@ redef class TextView
                native.text = value.to_java_string
        end
 
-       # Size of the text
-       fun text_size: Float do return native.text_size
+       redef fun size=(size) do set_size_native(app.native_activity, native, size or else 1.0)
 
-       # Size of the text
-       fun text_size=(text_size: nullable Float) do
-               if text_size != null then native.text_size = text_size
-       end
+       private fun set_size_native(context: NativeContext, view: NativeTextView, size: Float)
+       in "Java" `{
+               int s;
+               if (size == 1.0d)
+                       s = android.R.style.TextAppearance_Medium;
+               else if (size < 1.0d)
+                       s = android.R.style.TextAppearance_Small;
+               else // if (size > 1.0d)
+                       s = android.R.style.TextAppearance_Large;
+
+               view.setTextAppearance(context, s);
+       `}
+
+       redef fun align=(align) do set_align_native(native, align or else 0.0)
+
+       private fun set_align_native(view: NativeTextView, align: Float)
+       in "Java" `{
+               int g;
+               if (align == 0.5d)
+                       g = android.view.Gravity.CENTER_HORIZONTAL;
+               else if (align < 0.5d)
+                       g = android.view.Gravity.LEFT;
+               else // if (align > 0.5d)
+                       g = android.view.Gravity.RIGHT;
+
+               view.setGravity(g | android.view.Gravity.CENTER_VERTICAL);
+       `}
 end
 
 redef class Label
        redef type NATIVE: NativeTextView
        redef var native do return (new NativeTextView(app.native_activity)).new_global_ref
+end
 
-       init do native.set_text_appearance(app.native_activity, android_r_style_text_appearance_medium)
+redef class CheckBox
+       redef type NATIVE: Android_widget_CompoundButton
+       redef var native do return (new Android_widget_CheckBox(app.native_activity)).new_global_ref
+       init do set_callback_on_toggle(native)
+
+       redef fun is_checked do return native.is_checked
+       redef fun is_checked=(value) do native.set_checked(value)
+
+       private fun on_toggle do notify_observers new ToggleEvent(self)
+
+       private fun set_callback_on_toggle(view: NATIVE)
+       import on_toggle in "Java" `{
+               final int final_sender_object = self;
+               CheckBox_incr_ref(final_sender_object);
+
+               view.setOnCheckedChangeListener(
+                       new android.widget.CompoundButton.OnCheckedChangeListener() {
+                               @Override
+                               public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
+                                       CheckBox_on_toggle(final_sender_object);
+                               }
+                       });
+       `}
 end
 
 redef class TextInput
@@ -287,3 +348,18 @@ redef class Android_app_Fragment
                };
        `}
 end
+
+redef class Text
+       redef fun open_in_browser
+       do to_java_string.native_open_in_browser(app.native_activity)
+end
+
+redef class JavaString
+       private fun native_open_in_browser(context: NativeContext)
+       in "Java" `{
+               android.content.Intent intent = new android.content.Intent(
+                       android.content.Intent.ACTION_VIEW,
+                       android.net.Uri.parse(self));
+               context.startActivity(intent);
+       `}
+end