Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timob 12026: add default behaviors for views properties #3892

Merged
merged 7 commits into from
Feb 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
public class TiUIButton extends TiUIView
{
private static final String TAG = "TiUIButton";

private int defaultColor;

public TiUIButton(final TiViewProxy proxy)
{
Expand All @@ -42,6 +44,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
}
};
btn.setGravity(Gravity.CENTER);
defaultColor = btn.getCurrentTextColor();
setNativeView(btn);
}

Expand Down Expand Up @@ -73,7 +76,12 @@ public void processProperties(KrollDict d)
btn.setText(d.getString(TiC.PROPERTY_TITLE));
}
if (d.containsKey(TiC.PROPERTY_COLOR)) {
btn.setTextColor(TiConvert.toColor(d, TiC.PROPERTY_COLOR));
Object color = d.get(TiC.PROPERTY_COLOR);
if (color == null) {
btn.setTextColor(defaultColor);
} else {
btn.setTextColor(TiConvert.toColor(d, TiC.PROPERTY_COLOR));
}
}
if (d.containsKey(TiC.PROPERTY_FONT)) {
TiUIHelper.styleText(btn, d.getKrollDict(TiC.PROPERTY_FONT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,10 +965,10 @@ public void processProperties(KrollDict d)
setImages();
}
if (d.containsKey(TiC.PROPERTY_CAN_SCALE)) {
view.setCanScaleImage(TiConvert.toBoolean(d, TiC.PROPERTY_CAN_SCALE));
view.setCanScaleImage(TiConvert.toBoolean(d, TiC.PROPERTY_CAN_SCALE, false));
}
if (d.containsKey(TiC.PROPERTY_ENABLE_ZOOM_CONTROLS)) {
view.setEnableZoomControls(TiConvert.toBoolean(d, TiC.PROPERTY_ENABLE_ZOOM_CONTROLS));
view.setEnableZoomControls(TiConvert.toBoolean(d, TiC.PROPERTY_ENABLE_ZOOM_CONTROLS, true));
}
if (d.containsKey(TiC.PROPERTY_DEFAULT_IMAGE)) {
Object defaultImage = d.get(TiC.PROPERTY_DEFAULT_IMAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import android.graphics.Color;
import android.text.Html;
import android.text.InputType;
import android.text.TextUtils.TruncateAt;
Expand All @@ -28,6 +29,8 @@
public class TiUILabel extends TiUIView
{
private static final String TAG = "TiUILabel";

private int defaultColor;

public TiUILabel(final TiViewProxy proxy)
{
Expand All @@ -50,7 +53,9 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
tv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tv.setKeyListener(null);
tv.setFocusable(false);
defaultColor = tv.getCurrentTextColor();
setNativeView(tv);

}

@Override
Expand All @@ -65,15 +70,24 @@ public void processProperties(KrollDict d)

// Only accept one, prefer text to title.
if (d.containsKey(TiC.PROPERTY_HTML)) {
tv.setText(Html.fromHtml(TiConvert.toString(d, TiC.PROPERTY_HTML)), TextView.BufferType.SPANNABLE);
String html = TiConvert.toString(d, TiC.PROPERTY_HTML);
if (html == null) {
html = "";
}
tv.setText(Html.fromHtml(html), TextView.BufferType.SPANNABLE);
} else if (d.containsKey(TiC.PROPERTY_TEXT)) {
tv.setText(TiConvert.toString(d,TiC.PROPERTY_TEXT));
} else if (d.containsKey(TiC.PROPERTY_TITLE)) { //TODO this may not need to be supported.
tv.setText(TiConvert.toString(d,TiC.PROPERTY_TITLE));
}

if (d.containsKey(TiC.PROPERTY_COLOR)) {
tv.setTextColor(TiConvert.toColor(d, TiC.PROPERTY_COLOR));
Object color = d.get(TiC.PROPERTY_COLOR);
if (color == null) {
tv.setTextColor(defaultColor);
} else {
tv.setTextColor(TiConvert.toColor(d, TiC.PROPERTY_COLOR));
}
}
if (d.containsKey(TiC.PROPERTY_HIGHLIGHTED_COLOR)) {
tv.setHighlightColor(TiConvert.toColor(d, TiC.PROPERTY_HIGHLIGHTED_COLOR));
Expand All @@ -87,14 +101,14 @@ public void processProperties(KrollDict d)
TiUIHelper.setAlignment(tv, textAlign, verticalAlign);
}
if (d.containsKey(TiC.PROPERTY_ELLIPSIZE)) {
if (TiConvert.toBoolean(d, TiC.PROPERTY_ELLIPSIZE)) {
if (TiConvert.toBoolean(d, TiC.PROPERTY_ELLIPSIZE, false)) {
tv.setEllipsize(TruncateAt.END);
} else {
tv.setEllipsize(null);
}
}
if (d.containsKey(TiC.PROPERTY_WORD_WRAP)) {
tv.setSingleLine(!TiConvert.toBoolean(d, TiC.PROPERTY_WORD_WRAP));
tv.setSingleLine(!TiConvert.toBoolean(d, TiC.PROPERTY_WORD_WRAP, true));
}
// This needs to be the last operation.
TiUIHelper.linkifyIfEnabled(tv, d.get(TiC.PROPERTY_AUTO_LINK));
Expand Down Expand Up @@ -127,13 +141,13 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
TiUIHelper.styleText(tv, (HashMap) newValue);
tv.requestLayout();
} else if (key.equals(TiC.PROPERTY_ELLIPSIZE)) {
if (TiConvert.toBoolean(newValue)) {
if (TiConvert.toBoolean(newValue, false)) {
tv.setEllipsize(TruncateAt.END);
} else {
tv.setEllipsize(null);
}
} else if (key.equals(TiC.PROPERTY_WORD_WRAP)) {
tv.setSingleLine(!TiConvert.toBoolean(newValue));
tv.setSingleLine(!TiConvert.toBoolean(newValue, true));
} else if (key.equals(TiC.PROPERTY_AUTO_LINK)) {
Linkify.addLinks(tv, TiConvert.toInt(newValue));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ public void processProperties(KrollDict d)
SeekBar seekBar = (SeekBar) getNativeView();

if (d.containsKey(TiC.PROPERTY_VALUE)) {
pos = TiConvert.toFloat(d, TiC.PROPERTY_VALUE);
pos = TiConvert.toFloat(d, TiC.PROPERTY_VALUE, 0);
}
if (d.containsKey("min")) {
min = TiConvert.toInt(d, "min");
if (d.containsKey(TiC.PROPERTY_MIN)) {
min = TiConvert.toInt(d.get(TiC.PROPERTY_MIN), 0);
}
if (d.containsKey("max")) {
max = TiConvert.toInt(d, "max");;
if (d.containsKey(TiC.PROPERTY_MAX)) {
max = TiConvert.toInt(d.get(TiC.PROPERTY_MAX), 0);;
}
if (d.containsKey("minRange")) {
minRange = TiConvert.toInt(d, "minRange");
minRange = TiConvert.toInt(d.get("minRange"), 0);
} else {
minRange = min;
}
if (d.containsKey("maxRange")) {
maxRange = TiConvert.toInt(d, "maxRange");
maxRange = TiConvert.toInt(d.get("maxRange"), 0);
} else {
maxRange = max;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void processProperties(KrollDict d)
super.processProperties(d);

if (d.containsKey(TiC.PROPERTY_STYLE)) {
setStyle(TiConvert.toInt(d, TiC.PROPERTY_STYLE));
setStyle(TiConvert.toInt(d.get(TiC.PROPERTY_STYLE), AndroidModule.SWITCH_STYLE_TOGGLEBUTTON));
}

View nativeView = getNativeView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ public void processProperties(KrollDict d)
super.processProperties(d);

if (d.containsKey(TiC.PROPERTY_ENABLED)) {
tv.setEnabled(d.getBoolean(TiC.PROPERTY_ENABLED));
tv.setEnabled(TiConvert.toBoolean(d, TiC.PROPERTY_ENABLED, true));
}

if (d.containsKey(TiC.PROPERTY_MAX_LENGTH) && field) {
maxLength = TiConvert.toInt(d, TiC.PROPERTY_MAX_LENGTH);
maxLength = TiConvert.toInt(d.get(TiC.PROPERTY_MAX_LENGTH), -1);
}
if (d.containsKey(TiC.PROPERTY_VALUE)) {
tv.setText(d.getString(TiC.PROPERTY_VALUE));
Expand Down Expand Up @@ -180,7 +180,7 @@ public void processProperties(KrollDict d)
}

if (d.containsKey(TiC.PROPERTY_RETURN_KEY_TYPE)) {
handleReturnKeyType(d.getInt(TiC.PROPERTY_RETURN_KEY_TYPE));
handleReturnKeyType(TiConvert.toInt(d.get(TiC.PROPERTY_RETURN_KEY_TYPE), RETURNKEY_DEFAULT));
}

if (d.containsKey(TiC.PROPERTY_KEYBOARD_TYPE) || d.containsKey(TiC.PROPERTY_AUTOCORRECT) || d.containsKey(TiC.PROPERTY_PASSWORD_MASK) || d.containsKey(TiC.PROPERTY_AUTOCAPITALIZATION) || d.containsKey(TiC.PROPERTY_EDITABLE)) {
Expand Down Expand Up @@ -377,17 +377,17 @@ public void handleKeyboard(KrollDict d)
int autocorrect = InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
int autoCapValue = 0;

if (d.containsKey(TiC.PROPERTY_AUTOCORRECT) && !TiConvert.toBoolean(d, TiC.PROPERTY_AUTOCORRECT)) {
if (d.containsKey(TiC.PROPERTY_AUTOCORRECT) && !TiConvert.toBoolean(d, TiC.PROPERTY_AUTOCORRECT, true)) {
autocorrect = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
}

if (d.containsKey(TiC.PROPERTY_EDITABLE)) {
editable = TiConvert.toBoolean(d, TiC.PROPERTY_EDITABLE);
editable = TiConvert.toBoolean(d, TiC.PROPERTY_EDITABLE, true);
}

if (d.containsKey(TiC.PROPERTY_AUTOCAPITALIZATION)) {

switch (TiConvert.toInt(d,TiC.PROPERTY_AUTOCAPITALIZATION)) {
switch (TiConvert.toInt(d.get(TiC.PROPERTY_AUTOCAPITALIZATION), TEXT_AUTOCAPITALIZATION_NONE)) {
case TEXT_AUTOCAPITALIZATION_NONE:
autoCapValue = 0;
break;
Expand All @@ -411,11 +411,11 @@ public void handleKeyboard(KrollDict d)
}

if (d.containsKey(TiC.PROPERTY_PASSWORD_MASK)) {
passwordMask = TiConvert.toBoolean(d, TiC.PROPERTY_PASSWORD_MASK);
passwordMask = TiConvert.toBoolean(d, TiC.PROPERTY_PASSWORD_MASK, false);
}

if (d.containsKey(TiC.PROPERTY_KEYBOARD_TYPE)) {
type = TiConvert.toInt(d, TiC.PROPERTY_KEYBOARD_TYPE);
type = TiConvert.toInt(d.get(TiC.PROPERTY_KEYBOARD_TYPE), KEYBOARD_DEFAULT);
}

int typeModifiers = autocorrect | autoCapValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void processProperties(KrollDict d)
super.processProperties(d);

if (d.containsKey("showCancel")) {
boolean showCancel = TiConvert.toBoolean(d, "showCancel");
boolean showCancel = TiConvert.toBoolean(d, "showCancel", false);
cancelBtn.setVisibility(showCancel ? View.VISIBLE : View.GONE);
} else if (d.containsKey("barColor")) {
nativeView.setBackgroundColor(TiConvert.toColor(d, "barColor"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TiColorHelper
* @return the RGB/RGBA representation (int) of the color.
*/
public static int parseColor(String value) {
int color = Color.YELLOW; // Something noticeable
int color = Color.TRANSPARENT;
if (value != null) {
String lowval = value.trim().toLowerCase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ public static boolean fillLayout(HashMap<String, Object> hashMap, LayoutParams l

if (hashMap.containsKey(TiC.PROPERTY_SIZE)) {
HashMap<String, Object> size = (HashMap<String, Object>) hashMap.get(TiC.PROPERTY_SIZE);
width = size.get(TiC.PROPERTY_WIDTH);
height = size.get(TiC.PROPERTY_HEIGHT);
if (size != null) {
width = size.get(TiC.PROPERTY_WIDTH);
height = size.get(TiC.PROPERTY_HEIGHT);
}
}

if (hashMap.containsKey(TiC.PROPERTY_LEFT)) {
Expand Down Expand Up @@ -318,6 +320,23 @@ public static void updateLayoutCenter(Object value, LayoutParams layoutParams)
}
}

/**
* Attempts to convert a value into a boolean, if value is a Boolean or String. Otherwise,
* default value is returned
* @param value the value to convert.
* @param def the default value.
* @return a boolean value.
* @module.api
*/
public static boolean toBoolean(Object value, boolean def)
{
try {
return toBoolean(value);
} catch (IllegalArgumentException e) {
return def;
}
}

/**
* Attempts to convert a value into a boolean, if value is a Boolean or String. Otherwise,
* an exception is thrown.
Expand All @@ -338,6 +357,19 @@ public static boolean toBoolean(Object value)
}
}

/**
* Takes a value out of a hash table then attempts to convert it using {@link #toBoolean(Object)}.
* @param hashMap the hash map to search.
* @param key the lookup key.
* @param def the default value.
* @return a boolean value.
* @module.api
*/
public static boolean toBoolean(HashMap<String, Object> hashMap, String key, boolean def)
{
return toBoolean(hashMap.get(key), def);
}

/**
* Takes a value out of a hash table then attempts to convert it using {@link #toBoolean(Object)}.
* @param hashMap the hash map to search.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ public static float getRawSize(String size, Context context) {
}

public static void styleText(TextView tv, HashMap<String, Object> d) {

if (d == null) {
TiUIHelper.styleText(tv, null, null, null);
return;
}

String fontSize = null;
String fontWeight = null;
String fontFamily = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ protected void setNativeView(View view)
boolean clickable = true;

if (proxy.hasProperty(TiC.PROPERTY_TOUCH_ENABLED)) {
clickable = TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_TOUCH_ENABLED));
clickable = TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_TOUCH_ENABLED), true);
}
doSetClickable(nativeView, clickable);
nativeView.setOnFocusChangeListener(this);
Expand Down Expand Up @@ -491,7 +491,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_HORIZONTAL_WRAP)) {
if (nativeView instanceof TiCompositeLayout) {
((TiCompositeLayout) nativeView).setEnableHorizontalWrap(TiConvert.toBoolean(newValue));
((TiCompositeLayout) nativeView).setEnableHorizontalWrap(TiConvert.toBoolean(newValue,true));
}
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_WIDTH)) {
Expand Down Expand Up @@ -523,7 +523,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
setzIndexChanged(true);
}
} else if (key.equals(TiC.PROPERTY_FOCUSABLE) && newValue != null) {
registerForKeyPress(nativeView, TiConvert.toBoolean(newValue));
registerForKeyPress(nativeView, TiConvert.toBoolean(newValue, false));
} else if (key.equals(TiC.PROPERTY_TOUCH_ENABLED)) {
doSetClickable(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_VISIBLE)) {
Expand Down Expand Up @@ -659,7 +659,7 @@ public void processProperties(KrollDict d)

if (d.containsKey(TiC.PROPERTY_HORIZONTAL_WRAP)) {
if (nativeView instanceof TiCompositeLayout) {
((TiCompositeLayout) nativeView).setEnableHorizontalWrap(TiConvert.toBoolean(d, TiC.PROPERTY_HORIZONTAL_WRAP));
((TiCompositeLayout) nativeView).setEnableHorizontalWrap(TiConvert.toBoolean(d,TiC.PROPERTY_HORIZONTAL_WRAP,true));
}
}

Expand Down Expand Up @@ -688,10 +688,10 @@ public void processProperties(KrollDict d)
}

if (d.containsKey(TiC.PROPERTY_VISIBLE) && !nativeViewNull) {
this.setVisibility(TiConvert.toBoolean(d, TiC.PROPERTY_VISIBLE) ? View.VISIBLE : View.INVISIBLE);
setVisibility(TiConvert.toBoolean(d, TiC.PROPERTY_VISIBLE, true) ? View.VISIBLE : View.INVISIBLE);
}
if (d.containsKey(TiC.PROPERTY_ENABLED) && !nativeViewNull) {
nativeView.setEnabled(TiConvert.toBoolean(d, TiC.PROPERTY_ENABLED));
nativeView.setEnabled(TiConvert.toBoolean(d, TiC.PROPERTY_ENABLED, true));
}

initializeBorder(d, bgColor);
Expand All @@ -709,7 +709,7 @@ public void processProperties(KrollDict d)
}

if (d.containsKey(TiC.PROPERTY_KEEP_SCREEN_ON) && !nativeViewNull) {
nativeView.setKeepScreenOn(TiConvert.toBoolean(d, TiC.PROPERTY_KEEP_SCREEN_ON));
nativeView.setKeepScreenOn(TiConvert.toBoolean(d, TiC.PROPERTY_KEEP_SCREEN_ON, false));

}

Expand Down Expand Up @@ -1269,7 +1269,7 @@ protected void registerForKeyPress(final View v)

Object focusable = proxy.getProperty(TiC.PROPERTY_FOCUSABLE);
if (focusable != null) {
registerForKeyPress(v, TiConvert.toBoolean(focusable));
registerForKeyPress(v, TiConvert.toBoolean(focusable, false));
}
}

Expand Down Expand Up @@ -1629,7 +1629,7 @@ private void applyAccessibilityHidden(Object hiddenPropertyValue)

int importanceMode = ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO;

if (hiddenPropertyValue != null && TiConvert.toBoolean(hiddenPropertyValue)) {
if (hiddenPropertyValue != null && TiConvert.toBoolean(hiddenPropertyValue, false)) {
importanceMode = ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO;
}

Expand Down