Skip to content

Commit

Permalink
Merge branch 'master' into parity/ti.ui.color_in_props
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn authored Mar 22, 2022
2 parents fda1f98 + 07b108a commit 47e84ff
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
TiC.PROPERTY_TABS_BACKGROUND_COLOR,
TiC.PROPERTY_TABS_BACKGROUND_SELECTED_COLOR,
TiC.PROPERTY_SWIPEABLE,
TiC.PROPERTY_AUTO_TAB_TITLE,
TiC.PROPERTY_EXIT_ON_CLOSE,
TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK
})
Expand All @@ -68,11 +69,13 @@ public class TabGroupProxy extends TiWindowProxy implements TiActivityWindow
private Object selectedTab; // NOTE: Can be TabProxy or Number
private String tabGroupTitle = null;
private static int id_toolbar;
private boolean autoTabTitle = false;

public TabGroupProxy()
{
super();
defaultValues.put(TiC.PROPERTY_SWIPEABLE, true);
defaultValues.put(TiC.PROPERTY_AUTO_TAB_TITLE, autoTabTitle);
defaultValues.put(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK, true);
}

Expand Down Expand Up @@ -246,6 +249,9 @@ public void handleCreationDict(KrollDict options)
}
}

if (options.containsKeyAndNotNull(TiC.PROPERTY_AUTO_TAB_TITLE)) {
autoTabTitle = options.getBoolean(TiC.PROPERTY_AUTO_TAB_TITLE);
}
if (options.containsKeyAndNotNull(TiC.PROPERTY_TABS)) {
setTabs(options.get(TiC.PROPERTY_TABS));
}
Expand Down Expand Up @@ -277,7 +283,11 @@ public void setTitle(String title)
{
// If the native view is drawn directly set the String as a title for the SupportActionBar.
if (view != null) {
((TiUIAbstractTabGroup) view).updateTitle(title);
if (autoTabTitle) {
((TiUIAbstractTabGroup) view).updateTitle("");
} else {
((TiUIAbstractTabGroup) view).updateTitle(title);
}
} else {
// If the native view is not yet drawn save the value to be passed during creation.
this.tabGroupTitle = title;
Expand Down Expand Up @@ -336,7 +346,11 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState)
}
// If we have set a title before the creation of the native view, set it now.
if (this.tabGroupTitle != null) {
((TiUIAbstractTabGroup) view).updateTitle(this.tabGroupTitle);
if (autoTabTitle) {
((TiUIAbstractTabGroup) view).updateTitle("");
} else {
((TiUIAbstractTabGroup) view).updateTitle(this.tabGroupTitle);
}
}
setModelListener(view);

Expand Down Expand Up @@ -381,6 +395,15 @@ public void onWindowActivityCreated()
// Finish open handling by loading proxy settings.
handlePostOpen();

if (autoTabTitle) {
// expand title to fix truncated title
TiUIAbstractTabGroup tabGroup = (TiUIAbstractTabGroup) view;
tabGroup.updateTitle(tabs.get(0).getProperty(TiC.PROPERTY_TITLE).toString() + " "
+ tabs.get(0).getProperty(TiC.PROPERTY_TITLE).toString());

// clear it again
tabGroup.updateTitle("");
}
super.onWindowActivityCreated();
}

Expand All @@ -394,18 +417,18 @@ protected void handlePostOpen()
}

// Load any tabs added before the tab group opened.
TiUIAbstractTabGroup tg = (TiUIAbstractTabGroup) view;
TiUIAbstractTabGroup tabGroup = (TiUIAbstractTabGroup) view;
for (TabProxy tab : tabs) {
if (tab != null) {
tg.addTab(tab);
tabGroup.addTab(tab);
}
}

// If TabGroup's selected tab is same as the active tab,
// then we need to invoke onTabSelected so focus/blur event fire appropriately
TabProxy tab = getActiveTabProxy();
if (tab != null) {
tg.selectTab(tab);
tabGroup.selectTab(tab);
selectedTab = tab;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.shape.CornerFamily;

public class TiUICardView extends TiUIView
{
Expand Down Expand Up @@ -165,12 +166,7 @@ public void processProperties(KrollDict d)
}

if (d.containsKey(TiC.PROPERTY_BORDER_RADIUS)) {
float radius = 0;
TiDimension radiusDim = TiConvert.toTiDimension(d.get(TiC.PROPERTY_BORDER_RADIUS), TiDimension.TYPE_WIDTH);
if (radiusDim != null) {
radius = (float) radiusDim.getPixels(cardview);
}
cardview.setRadius(radius);
setRadius(d.get(TiC.PROPERTY_BORDER_RADIUS));
}

if (d.containsKey(TiC.PROPERTY_BORDER_WIDTH)) {
Expand Down Expand Up @@ -297,12 +293,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else if (key.equals(TiC.PROPERTY_BORDER_COLOR)) {
cardview.setStrokeColor(TiConvert.toColor(newValue, proxy.getActivity()));
} else if (key.equals(TiC.PROPERTY_BORDER_RADIUS)) {
float radius = 0;
TiDimension radiusDim = TiConvert.toTiDimension(newValue, TiDimension.TYPE_WIDTH);
if (radiusDim != null) {
radius = (float) radiusDim.getPixels(cardview);
}
cardview.setRadius(radius);
setRadius(newValue);
cardview.requestLayout();
} else if (key.equals(TiC.PROPERTY_BORDER_WIDTH)) {
TiDimension tiDimension = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_WIDTH);
Expand Down Expand Up @@ -414,4 +405,65 @@ protected boolean hasBorder(KrollDict d)
// We apply border properties to CardView ourselves via its stroke methods.
return false;
}

private void setRadius(Object borderRadius)
{
float radius = 0;
TiCardView cardView = (TiCardView) getNativeView();

// Case 1: A string of border radii (e.g. '0 0 20 20') or a single string radius (e.g. '20')
if (borderRadius instanceof String) {
final String[] corners = ((String) borderRadius).split("\\s");
if (corners != null && corners.length > 1) {
setRadius(corners);
} else {
setRadius(new String[]{ corners[0], corners[0], corners[0], corners[0] });
}

// Case 2: An array of border radii (e.g. ['0 0 20 20'])
} else if (borderRadius instanceof Object[]) {
final Object[] cornerObjects = (Object[]) borderRadius;
final float[] cornerPixels = new float[cornerObjects.length];

for (int i = 0; i < cornerObjects.length; i++) {
final Object corner = cornerObjects[i];
final TiDimension radiusDimension = TiConvert.toTiDimension(corner, TiDimension.TYPE_WIDTH);
if (radiusDimension != null) {
cornerPixels[i] = (float) radiusDimension.getPixels(this.nativeView);
} else {
Log.w(TAG, "Invalid value specified for borderRadius[" + i + "].");
cornerPixels[i] = 0;
}
}

if (cornerPixels.length >= 4) {
cardView.setShapeAppearanceModel(
cardView.getShapeAppearanceModel()
.toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED, cornerPixels[0])
.setTopRightCorner(CornerFamily.ROUNDED, cornerPixels[1])
.setBottomRightCorner(CornerFamily.ROUNDED, cornerPixels[2])
.setBottomLeftCorner(CornerFamily.ROUNDED, cornerPixels[3])
.build());
} else {
Log.w(TAG, "Could not set borderRadius, empty array.");
}
// Case 3: A single radius (e.g. 20)
} else {
final TiDimension radiusDimension = TiConvert.toTiDimension(borderRadius, TiDimension.TYPE_WIDTH);
float pixels = 0;

if (radiusDimension != null) {
pixels = (float) radiusDimension.getPixels(this.nativeView);
} else {
Log.w(TAG, "Invalid value specified for borderRadius.");
}

TiDimension radiusDim = TiConvert.toTiDimension(borderRadius, TiDimension.TYPE_WIDTH);
if (radiusDim != null) {
radius = (float) radiusDim.getPixels(cardView);
}
cardView.setRadius(radius);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,16 @@ public abstract class TiUIAbstractTabGroup extends TiUIView
// endregion

// region private fields
private boolean autoTabTitle = false;
private int lastTab = -1;
private AtomicLong fragmentIdGenerator = new AtomicLong();
private ArrayList<Long> tabFragmentIDs = new ArrayList<Long>();
protected ArrayList<TiUITab> tabs = new ArrayList<TiUITab>();
private final boolean isUsingSolidTitaniumTheme;
private int colorBackgroundInt;
private int colorSurfaceInt;
private int colorPrimaryInt;
private int colorOnSurfaceInt;
private final AtomicLong fragmentIdGenerator = new AtomicLong();
private final ArrayList<Long> tabFragmentIDs = new ArrayList<>();
protected ArrayList<TiUITab> tabs = new ArrayList<>();
// endregion

public TiUIAbstractTabGroup(final TabGroupProxy proxy, TiBaseActivity activity)
Expand Down Expand Up @@ -428,6 +430,12 @@ public void selectTab(int tabIndex)
@Override
public void onPageScrolled(int i, float v, int i1)
{
if (autoTabTitle && i != lastTab) {
if (tabs.get(i).getWindowProxy() != null) {
updateTitle(tabs.get(i).getWindowProxy().getProperty(TiC.PROPERTY_TITLE).toString());
}
lastTab = i;
}
}

@Override
Expand Down Expand Up @@ -476,6 +484,9 @@ public void processProperties(KrollDict d)
if (d.containsKey(TiC.PROPERTY_SWIPEABLE)) {
this.swipeable = d.getBoolean(TiC.PROPERTY_SWIPEABLE);
}
if (d.containsKey(TiC.PROPERTY_AUTO_TAB_TITLE)) {
this.autoTabTitle = d.getBoolean(TiC.PROPERTY_AUTO_TAB_TITLE);
}
if (d.containsKey(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK)) {
this.smoothScrollOnTabClick = d.getBoolean(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK);
}
Expand All @@ -494,6 +505,8 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
this.swipeable = TiConvert.toBoolean(newValue);
} else if (key.equals(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK)) {
this.smoothScrollOnTabClick = TiConvert.toBoolean(newValue);
} else if (key.equals(TiC.PROPERTY_AUTO_TAB_TITLE)) {
this.autoTabTitle = TiConvert.toBoolean(newValue);
} else if (key.equals(TiC.PROPERTY_TABS_BACKGROUND_COLOR)) {
for (TiUITab tabView : tabs) {
updateTabBackgroundDrawable(tabs.indexOf(tabView));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public View getContentView()
return windowProxy.getOrCreateView().getOuterView();
}

private TiWindowProxy getWindowProxy()
public TiWindowProxy getWindowProxy()
{
if (proxy != null) {
Object windowProxy = proxy.getProperty(TiC.PROPERTY_WINDOW);
Expand Down
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ public class TiC
public static final String PROPERTY_AUTOROTATE = "autorotate";
public static final String PROPERTY_AUTO_REDIRECT = "autoRedirect";
public static final String PROPERTY_AUTO_ENCODE_URL = "autoEncodeUrl";
public static final String PROPERTY_AUTO_TAB_TITLE = "autoTabTitle";

/**
* @module.api
*/
public static final String PROPERTY_BACKGROUND_COLOR = "backgroundColor";
public static final String PROPERTY_BACKGROUND_DISABLED_COLOR = "backgroundDisabledColor";
public static final String PROPERTY_BACKGROUND_DISABLED_IMAGE = "backgroundDisabledImage";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
*/
package org.appcelerator.titanium.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.proxy.ColorProxy;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.TypedValue;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.proxy.ColorProxy;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* This class contain utility methods that converts a String color, like "red", into its corresponding RGB/RGBA representation.
*/
Expand All @@ -39,6 +40,9 @@ public class TiColorHelper
"rgba\\(\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*(\\d\\.\\d+)\\s*\\)");
static Pattern floatsPattern = Pattern.compile(
"rgba\\(\\s*(\\d\\.\\d+)\\s*,\\s*(\\d\\.\\d+)\\s*,\\s*(\\d\\.\\d+)\\s*,\\s*(\\d\\.\\d+)\\s*\\)");
static Pattern rgbafloatPattern = Pattern.compile(
"rgba\\((0|[1-9]\\d{0,2}),\\s*(0|[1-9]\\d{0,2}),\\s*(0|[1-9]\\d{0,2}),"
+ "\\s*(0|1|(0){0,1}\\.\\d{1,10}|1\\.0{1,10})\\s*\\)");
static Pattern rgbaPatternFallback =
Pattern.compile("rgba\\(\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*\\)");

Expand Down Expand Up @@ -109,6 +113,11 @@ public static int parseColor(String value, Context context)
return Color.argb(Math.round(Float.valueOf(m.group(4)) * 255f), Integer.valueOf(m.group(1)),
Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)));
}
// rgba(int, int, int, float)
if ((m = rgbafloatPattern.matcher(lowval)).matches()) {
return Color.argb(Math.round(Float.valueOf(m.group(4)) * 255f), Integer.valueOf(m.group(1)),
Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)));
}
// rgba(int, int, int) with missing alpha value
if ((m = rgbaPatternFallback.matcher(lowval)).matches()) {
return Color.rgb(Integer.valueOf(m.group(1)), Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;
Expand Down Expand Up @@ -104,12 +105,12 @@ protected void onDraw(Canvas canvas)
canvas.drawPath(outerPath, paint);

// TIMOB-16909: hack to fix anti-aliasing
if (backgroundColor != Color.TRANSPARENT) {
if (Build.VERSION.SDK_INT < 31 && backgroundColor != Color.TRANSPARENT) {
paint.setColor(backgroundColor);
canvas.drawPath(innerPath, paint);
}
canvas.clipPath(innerPath);
if (backgroundColor != Color.TRANSPARENT) {
if (Build.VERSION.SDK_INT < 31 && backgroundColor != Color.TRANSPARENT) {
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
}
} else {
Expand Down
7 changes: 7 additions & 0 deletions apidoc/Titanium/UI/TabGroup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ properties:
default: true
platforms: [iphone, ipad, macos]

- name: autoTabTitle
summary: If set to `true` it will automatically set the actionbar title to the current tabs window title.
type: Boolean
default: false
since: 10.2.0
platforms: [android]

- name: barColor
summary: |
Default navigation bar color (typically for the **More** tab), as a color name or hex triplet.
Expand Down
2 changes: 1 addition & 1 deletion iphone/TitaniumKit/TitaniumKit/Sources/API/TiViewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -2736,7 +2736,7 @@ - (void)layoutChild:(TiViewProxy *)child optimize:(BOOL)optimize withMeasuredBou
[child setSandboxBounds:bounds];
if ([[child view] animating]) {
// changing the layout while animating is bad, ignore for now
DebugLog(@"[WARN] New layout set while view %@ animating: Will relayout after animation.", child);
DebugLog(@"[DEBUG] New layout set while view %@ animating: Will relayout after animation.", child);
} else {
[child relayout];
}
Expand Down
Loading

0 comments on commit 47e84ff

Please sign in to comment.