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

feat(android): minWidth/maxWidth and minHeight/maxHeight for Views #13795

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class TiBorderWrapperView extends FrameLayout
private Paint paint;
private Rect bounds;
private ViewOutlineProvider viewOutlineProvider;
private int viewMaxWidth = -1;
public TiBorderWrapperView(Context context)
{
super(context);
Expand Down Expand Up @@ -238,6 +239,20 @@ public void setRadius(Object obj)
}
}

/*
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredWidth = widthMeasureSpec;
int measuredHeight = heightMeasureSpec;
if (viewMaxWidth != -1 && measuredWidth > viewMaxWidth) {
measuredWidth = viewMaxWidth;
}
setMeasuredDimension(measuredWidth, measuredHeight);
}
*/

public void setBorderWidth(float borderWidth)
{
this.borderWidth = borderWidth;
Expand All @@ -248,4 +263,9 @@ public boolean onSetAlpha(int alpha)
{
return false;
}

public void setMaxWidth(int maxWidth)
{
viewMaxWidth = maxWidth;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public enum LayoutArrangement {
private boolean enableHorizontalWrap = true;
private int horizontalLayoutLastIndexBeforeWrap = 0;
private int horiztonalLayoutPreviousRight = 0;
private int viewMinWidth = -1;
private int viewMaxWidth = -1;
private int viewMaxHeight = -1;
private int viewMinHeight = -1;
int[] horizontal = new int[2];
int[] vertical = new int[2];
/**
Expand Down Expand Up @@ -641,6 +645,18 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

int measuredWidth = getMeasuredWidth(maxWidth, widthMeasureSpec);
int measuredHeight = getMeasuredHeight(maxHeight, heightMeasureSpec);
if (viewMaxWidth != -1 && measuredWidth > viewMaxWidth) {
measuredWidth = viewMaxWidth;
}
if (viewMinWidth != -1 && measuredWidth < viewMinWidth) {
measuredWidth = viewMinWidth;
}
if (viewMaxHeight != -1 && measuredHeight > viewMaxHeight) {
measuredHeight = viewMaxHeight;
}
if (viewMinHeight != -1 && measuredHeight < viewMinHeight) {
measuredHeight = viewMinHeight;
}
setMeasuredDimension(measuredWidth, measuredHeight);
}

Expand Down Expand Up @@ -1237,6 +1253,24 @@ public void setLayoutArrangement(String arrangementProperty)
}
}

public void setMaxWidth(Integer value)
{
viewMaxWidth = value;
}

public void setMinWidth(Integer value)
{
viewMinWidth = value;
}
public void setMaxHeight(Integer value)
{
viewMaxHeight = value;
}
public void setMinHeight(Integer value)
{
viewMinHeight = value;
}

public void setEnableHorizontalWrap(boolean enable)
{
enableHorizontalWrap = enable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public abstract class TiUIView implements KrollProxyListener, OnFocusChangeListe
protected LayoutParams layoutParams;
protected TiAnimationBuilder animBuilder;
protected TiBackgroundDrawable background;
private int minWidth = -1;
private int maxWidth = -1;
private int minHeight = -1;
private int maxHeight = -1;

public TiBackgroundDrawable getBackground()
{
Expand Down Expand Up @@ -784,6 +788,14 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
doSetClickable(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_FILTER_TOUCHES_WHEN_OBSCURED)) {
setFilterTouchesWhenObscured(TiConvert.toBoolean(newValue, false));
} else if (key.equals("maxWidth")) {
setMaxWidth(newValue, hasBorder(proxy.getProperties()));
} else if (key.equals("minWidth")) {
setMinWidth(newValue, hasBorder(proxy.getProperties()));
} else if (key.equals("maxHeight")) {
setMaxHeight(newValue, hasBorder(proxy.getProperties()));
} else if (key.equals("minHeight")) {
setMinHeight(newValue, hasBorder(proxy.getProperties()));
} else if (key.equals(TiC.PROPERTY_VISIBLE)) {
newValue = (newValue == null) ? false : newValue;
this.setVisibility(TiConvert.toBoolean(newValue) ? View.VISIBLE : View.INVISIBLE);
Expand Down Expand Up @@ -1054,6 +1066,18 @@ public void processProperties(KrollDict d)
nativeView.setEnabled(TiConvert.toBoolean(d, TiC.PROPERTY_ENABLED, true));
}

if (d.containsKey("maxWidth")) {
setMaxWidth(d.get("maxWidth"), hasBorder(d));
}
if (d.containsKey("minWidth")) {
setMinWidth(d.get("minWidth"), hasBorder(d));
}
if (d.containsKey("maxHeight")) {
setMaxHeight(d.get("maxHeight"), hasBorder(d));
}
if (d.containsKey("minHeight")) {
setMinHeight(d.get("minHeight"), hasBorder(d));
}
initializeBorder(d, bgColor);

if (d.containsKey(TiC.PROPERTY_OPACITY) && !nativeViewNull) {
Expand Down Expand Up @@ -1143,6 +1167,65 @@ public void processProperties(KrollDict d)
}
}

private void setMaxWidth(Object value, Boolean hasBorder)
{
maxWidth = -1;
if (value != null) {
maxWidth = TiConvert.toTiDimension(TiConvert.toInt(value),
TiDimension.TYPE_WIDTH).getAsPixels(nativeView);
}

if (!hasBorder) {
((TiCompositeLayout) nativeView).setMaxWidth(maxWidth);
} else {
Log.w(TAG, "You can only use maxWidth for Views without borders");
}
}

private void setMinWidth(Object value, Boolean hasBorder)
{
minWidth = -1;
if (value != null) {
minWidth = TiConvert.toTiDimension(TiConvert.toInt(value),
TiDimension.TYPE_WIDTH).getAsPixels(nativeView);
}

if (!hasBorder) {
((TiCompositeLayout) nativeView).setMinWidth(minWidth);
} else {
Log.w(TAG, "You can only use minWidth for Views without borders");
}
}

private void setMaxHeight(Object value, Boolean hasBorder)
{
maxHeight = -1;
if (value != null) {
maxHeight = TiConvert.toTiDimension(TiConvert.toInt(value),
TiDimension.TYPE_HEIGHT).getAsPixels(nativeView);
}

if (!hasBorder) {
((TiCompositeLayout) nativeView).setMaxHeight(maxHeight);
} else {
Log.w(TAG, "You can only use maxHeight for Views without borders");
}
}
private void setMinHeight(Object value, Boolean hasBorder)
{
minHeight = -1;
if (value != null) {
minHeight = TiConvert.toTiDimension(TiConvert.toInt(value),
TiDimension.TYPE_HEIGHT).getAsPixels(nativeView);
}

if (!hasBorder) {
((TiCompositeLayout) nativeView).setMinHeight(minHeight);
} else {
Log.w(TAG, "You can only use maxHeight for Views without borders");
}
}

private void setAnchor(HashMap point)
{
View outerView = getOuterView();
Expand Down Expand Up @@ -1499,6 +1582,7 @@ private void initializeBorder(KrollDict d, Integer bgColor)
currentActivity = TiApplication.getAppCurrentActivity();
}
borderView = new TiBorderWrapperView(currentActivity);
//borderView.setMaxWidth(maxWidth); // TODO fix borderView maxWidth
// Create new layout params for the child view since we just want the
// wrapper to control the layout
LayoutParams params = new LayoutParams();
Expand Down
36 changes: 36 additions & 0 deletions apidoc/Titanium/UI/View.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,42 @@ properties:
type: [Number,String]
constants: [Titanium.UI.FILL, Titanium.UI.SIZE]

- name: maxWidth
summary: Maximum width of the View, in platform-specific units.
description: |
You can use the setting in combination with `width: Ti.UI.FILL` to set a maximum width.
The view won't extend the `maxWidth` value.
type: Number
since: {android: "12.4.0"}
platforms: [android]

- name: maxHeight
summary: Maximum height of the View, in platform-specific units.
description: |
You can use the setting in combination with `height: Ti.UI.FILL` to set a maximum height.
The view won't extend the `maxHeight` value.
type: Number
since: {android: "12.4.0"}
platforms: [android]

- name: minWidth
summary: Minimum width of the View, in platform-specific units.
description: |
You can use the setting in combination with `width: Ti.UI.SIZE` to set a minimum width.
The view won't be smaller than the `minWidth` value.
type: Number
since: {android: "12.4.0"}
platforms: [android]

- name: minHeight
summary: Minimum height of the View, in platform-specific units.
description: |
You can use the setting in combination with `height: Ti.UI.SIZE` to set a minimum height.
The view won't be smaller than the `minHeight` value.
type: Number
since: {android: "12.4.0"}
platforms: [android]

- name: horizontalWrap
summary: Determines whether the layout has wrapping behavior.
description: |
Expand Down
Loading