Skip to content

Commit f45b01d

Browse files
author
vakrilov
committed
refactor(android): default minWidth/Height values for button
1 parent 8f7aad4 commit f45b01d

4 files changed

Lines changed: 43 additions & 19 deletions

File tree

nativescript-core/ui/button/button.android.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
ButtonBase, PseudoClassHandler,
33
paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty,
4-
Length, zIndexProperty, textAlignmentProperty, TextAlignment
4+
Length, zIndexProperty, textAlignmentProperty, TextAlignment, layout,
5+
minWidthProperty, minHeightProperty
56
} from "./button-common";
67
import { profile } from "../../profiling";
78
import { TouchGestureEventData, GestureTypes, TouchAction } from "../gestures";
@@ -111,6 +112,16 @@ export class Button extends ButtonBase {
111112
}
112113
}
113114

115+
[minWidthProperty.getDefault](): Length {
116+
const dips = org.nativescript.widgets.ViewHelper.getMinWidth(this.nativeViewProtected);
117+
return { value: dips, unit: "px" };
118+
}
119+
120+
[minHeightProperty.getDefault](): Length {
121+
const dips = org.nativescript.widgets.ViewHelper.getMinHeight(this.nativeViewProtected);
122+
return { value: dips, unit: "px" };
123+
}
124+
114125
[paddingTopProperty.getDefault](): Length {
115126
return { value: this._defaultPaddingTop, unit: "px" };
116127
}

nativescript-core/ui/core/view/view.android.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export class View extends ViewCommon {
388388
this.nativeViewProtected.setClickable(this._isClickable);
389389
}
390390
}
391-
391+
392392
this._manager = null;
393393
this._rootManager = null;
394394
super.onUnloaded();
@@ -962,17 +962,17 @@ export class View extends ViewCommon {
962962

963963
[minWidthProperty.setNative](value: Length) {
964964
if (this.parent instanceof CustomLayoutView && this.parent.nativeViewProtected) {
965-
this.parent._setChildMinWidthNative(this);
965+
this.parent._setChildMinWidthNative(this, value);
966966
} else {
967-
this._setMinWidthNative(this.minWidth);
967+
this._setMinWidthNative(value);
968968
}
969969
}
970970

971971
[minHeightProperty.setNative](value: Length) {
972972
if (this.parent instanceof CustomLayoutView && this.parent.nativeViewProtected) {
973-
this.parent._setChildMinHeightNative(this);
973+
this.parent._setChildMinHeightNative(this, value);
974974
} else {
975-
this._setMinHeightNative(this.minHeight);
975+
this._setMinHeightNative(value);
976976
}
977977
}
978978

@@ -1030,16 +1030,14 @@ export class CustomLayoutView extends ContainerView implements CustomLayoutViewD
10301030
}
10311031

10321032
public _updateNativeLayoutParams(child: View): void {
1033-
this._setChildMinWidthNative(child);
1034-
this._setChildMinHeightNative(child);
10351033
}
10361034

1037-
public _setChildMinWidthNative(child: View): void {
1038-
child._setMinWidthNative(child.minWidth);
1035+
public _setChildMinWidthNative(child: View, value: Length): void {
1036+
child._setMinWidthNative(value);
10391037
}
10401038

1041-
public _setChildMinHeightNative(child: View): void {
1042-
child._setMinHeightNative(child.minHeight);
1039+
public _setChildMinHeightNative(child: View, value: Length): void {
1040+
child._setMinHeightNative(value);
10431041
}
10441042

10451043
public _removeViewFromNativeVisualTree(child: ViewCommon): void {

nativescript-core/ui/core/view/view.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,11 @@ export class CustomLayoutView extends ContainerView {
775775
/**
776776
* @private
777777
*/
778-
_setChildMinWidthNative(child: View): void;
778+
_setChildMinWidthNative(child: View, value: Length): void;
779779
/**
780780
* @private
781781
*/
782-
_setChildMinHeightNative(child: View): void;
782+
_setChildMinHeightNative(child: View, value: Length): void;
783783
//@endprivate
784784
}
785785

nativescript-core/ui/layouts/flexbox-layout/flexbox-layout.android.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
flexShrinkProperty, FlexShrink,
88
flexWrapBeforeProperty, FlexWrapBefore,
99
alignSelfProperty, AlignSelf,
10-
flexDirectionProperty, flexWrapProperty, justifyContentProperty, alignItemsProperty, alignContentProperty
10+
flexDirectionProperty, flexWrapProperty, justifyContentProperty, alignItemsProperty, alignContentProperty,
11+
minWidthProperty, minHeightProperty,
12+
CssProperty
1113
} from "./flexbox-layout-common";
1214

1315
export * from "./flexbox-layout-common";
@@ -134,9 +136,22 @@ export class FlexboxLayout extends FlexboxLayoutBase {
134136
this.nativeViewProtected.setAlignContent(alignContentMap[alignContent]);
135137
}
136138

139+
private getChildPropertyValue<T>(child: View, prop: CssProperty<any, T>): T {
140+
if (minWidthProperty.isSet(child.style)) {
141+
return child[prop.name];
142+
} else if (child[prop.getDefault]) {
143+
return child[prop.getDefault]();
144+
} else {
145+
return prop.defaultValue;
146+
}
147+
}
148+
137149
public _updateNativeLayoutParams(child: View): void {
138150
super._updateNativeLayoutParams(child);
139151

152+
this._setChildMinWidthNative(child, this.getChildPropertyValue(child, minWidthProperty));
153+
this._setChildMinHeightNative(child, this.getChildPropertyValue(child, minHeightProperty));
154+
140155
const lp = <org.nativescript.widgets.FlexboxLayout.LayoutParams>child.nativeViewProtected.getLayoutParams();
141156
const style = child.style;
142157
lp.order = style.order;
@@ -147,22 +162,22 @@ export class FlexboxLayout extends FlexboxLayoutBase {
147162
child.nativeViewProtected.setLayoutParams(lp);
148163
}
149164

150-
public _setChildMinWidthNative(child: View): void {
165+
public _setChildMinWidthNative(child: View, value: Length): void {
151166
child._setMinWidthNative(0);
152167
const nativeView = child.nativeViewProtected;
153168
const lp = nativeView.getLayoutParams();
154169
if (lp instanceof widgetLayoutParams) {
155-
lp.minWidth = Length.toDevicePixels(child.style.minWidth, 0);
170+
lp.minWidth = Length.toDevicePixels(value, 0);
156171
nativeView.setLayoutParams(lp);
157172
}
158173
}
159174

160-
public _setChildMinHeightNative(child: View): void {
175+
public _setChildMinHeightNative(child: View, value: Length): void {
161176
child._setMinHeightNative(0);
162177
const nativeView = child.nativeViewProtected;
163178
const lp = nativeView.getLayoutParams();
164179
if (lp instanceof widgetLayoutParams) {
165-
lp.minHeight = Length.toDevicePixels(child.style.minHeight, 0);
180+
lp.minHeight = Length.toDevicePixels(value, 0);
166181
nativeView.setLayoutParams(lp);
167182
}
168183
}

0 commit comments

Comments
 (0)