Skip to content

Commit a3e1964

Browse files
committed
fix: native class when possible. Will be faster
1 parent 69116a8 commit a3e1964

File tree

14 files changed

+295
-169
lines changed

14 files changed

+295
-169
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.nativescript.material.textfield;
2+
3+
import android.content.Context;
4+
import android.view.ViewGroup;
5+
import android.os.IBinder;
6+
import android.view.View;
7+
import android.view.KeyEvent;
8+
import android.util.AttributeSet;
9+
import android.view.inputmethod.InputMethodManager;
10+
import java.util.HashMap;
11+
12+
public class TextInputEditText extends com.google.android.material.textfield.TextInputEditText {
13+
private InputMethodManager imm;
14+
15+
public TextInputEditText(Context context) {
16+
this(context, null);
17+
}
18+
public TextInputEditText(Context context, android.util.AttributeSet attrs) {
19+
super(context);
20+
imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
21+
}
22+
23+
static void handleClearFocus(View view) {
24+
final View root = view.getRootView();
25+
boolean oldValue = true;
26+
int oldDesc = ViewGroup.FOCUS_BEFORE_DESCENDANTS;
27+
28+
if (root != null) {
29+
if (root instanceof ViewGroup) {
30+
oldDesc = ((ViewGroup) root).getDescendantFocusability();
31+
((ViewGroup) root).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
32+
}
33+
oldValue = root.isFocusable();
34+
root.setFocusable(false);
35+
}
36+
view.clearFocus();
37+
if (root != null) {
38+
root.setFocusable(oldValue);
39+
if (root instanceof ViewGroup) {
40+
((ViewGroup) root).setDescendantFocusability(oldDesc);
41+
}
42+
}
43+
}
44+
45+
void dismissSoftInput() {
46+
final IBinder windowToken = getWindowToken();
47+
if (imm != null && windowToken != null) {
48+
imm.hideSoftInputFromWindow(windowToken, 0);
49+
}
50+
}
51+
52+
public void fullClearFocus() {
53+
handleClearFocus(this);
54+
dismissSoftInput();
55+
}
56+
57+
public boolean dispatchKeyEventPreIme(KeyEvent event) {
58+
if (imm != null && imm.isActive() && event.getAction() == android.view.KeyEvent.ACTION_UP
59+
&& event.getKeyCode() == android.view.KeyEvent.KEYCODE_BACK) {
60+
// when hiding the keyboard with the back button also blur
61+
clearFocus();
62+
return true;
63+
}
64+
return super.dispatchKeyEventPreIme(event);
65+
}
66+
67+
}

packages/nativescript-material-textfield/platforms/android/res/layout/material_text_field.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
style="@style/TextInputLayout"
2020
android:layout_width="match_parent"
2121
android:layout_height="match_parent">
22-
<org.nativescript.material.TextInputEditText
22+
<com.nativescript.material.textfield.TextInputEditText
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"/>
2525
</com.google.android.material.textfield.TextInputLayout>

packages/nativescript-material-textfield/platforms/android/res/layout/material_text_field_filled.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
2020
android:layout_width="match_parent"
2121
android:layout_height="match_parent">
22-
<org.nativescript.material.TextInputEditText
22+
<com.nativescript.material.textfield.TextInputEditText
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"/>
2525
</com.google.android.material.textfield.TextInputLayout>

packages/nativescript-material-textfield/platforms/android/res/layout/material_text_field_outline.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
2020
android:layout_width="match_parent"
2121
android:layout_height="match_parent">
22-
<org.nativescript.material.TextInputEditText
22+
<com.nativescript.material.textfield.TextInputEditText
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"/>
2525
</com.google.android.material.textfield.TextInputLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.nativescript.material.textview;
2+
3+
import android.content.Context;
4+
import android.view.ViewGroup;
5+
import android.os.IBinder;
6+
import android.view.View;
7+
import android.view.KeyEvent;
8+
import android.util.AttributeSet;
9+
import android.view.inputmethod.InputMethodManager;
10+
import java.util.HashMap;
11+
12+
public class TextViewInputEditText extends com.google.android.material.textfield.TextInputEditText {
13+
private InputMethodManager imm;
14+
15+
public TextViewInputEditText(Context context) {
16+
this(context, null);
17+
}
18+
public TextViewInputEditText(Context context, android.util.AttributeSet attrs) {
19+
super(context);
20+
imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
21+
}
22+
23+
static void handleClearFocus(View view) {
24+
final View root = view.getRootView();
25+
boolean oldValue = true;
26+
int oldDesc = ViewGroup.FOCUS_BEFORE_DESCENDANTS;
27+
28+
if (root != null) {
29+
if (root instanceof ViewGroup) {
30+
oldDesc = ((ViewGroup) root).getDescendantFocusability();
31+
((ViewGroup) root).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
32+
}
33+
oldValue = root.isFocusable();
34+
root.setFocusable(false);
35+
}
36+
view.clearFocus();
37+
if (root != null) {
38+
root.setFocusable(oldValue);
39+
if (root instanceof ViewGroup) {
40+
((ViewGroup) root).setDescendantFocusability(oldDesc);
41+
}
42+
}
43+
}
44+
45+
void dismissSoftInput() {
46+
final IBinder windowToken = getWindowToken();
47+
if (imm != null && windowToken != null) {
48+
imm.hideSoftInputFromWindow(windowToken, 0);
49+
}
50+
}
51+
52+
public void fullClearFocus() {
53+
handleClearFocus(this);
54+
dismissSoftInput();
55+
}
56+
57+
public boolean dispatchKeyEventPreIme(KeyEvent event) {
58+
if (imm != null && imm.isActive() && event.getAction() == android.view.KeyEvent.ACTION_UP
59+
&& event.getKeyCode() == android.view.KeyEvent.KEYCODE_BACK) {
60+
// when hiding the keyboard with the back button also blur
61+
clearFocus();
62+
return true;
63+
}
64+
return super.dispatchKeyEventPreIme(event);
65+
}
66+
67+
}

packages/nativescript-material-textview/platforms/android/res/layout/material_text_view.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
style="@style/TextInputLayout"
2020
android:layout_width="match_parent"
2121
android:layout_height="match_parent">
22-
<org.nativescript.material.TextViewInputEditText
22+
<com.nativescript.material.textview.TextViewInputEditText
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"/>
2525
</com.google.android.material.textfield.TextInputLayout>

packages/nativescript-material-textview/platforms/android/res/layout/material_text_view_filled.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
2020
android:layout_width="match_parent"
2121
android:layout_height="match_parent">
22-
<org.nativescript.material.TextViewInputEditText
22+
<com.nativescript.material.textview.TextViewInputEditText
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"/>
2525
</com.google.android.material.textfield.TextInputLayout>

packages/nativescript-material-textview/platforms/android/res/layout/material_text_view_outline.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
2020
android:layout_width="match_parent"
2121
android:layout_height="match_parent">
22-
<org.nativescript.material.TextViewInputEditText
22+
<com.nativescript.material.textview.TextViewInputEditText
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"/>
2525
</com.google.android.material.textfield.TextInputLayout>

src/button/button.android.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { Background } from '@nativescript/core/ui/styling/background';
55
import { androidDynamicElevationOffsetProperty, androidElevationProperty, backgroundInternalProperty, Length } from '@nativescript/core/ui/styling/style-properties';
66
import { ButtonBase } from './button-common';
77
import { VerticalTextAlignment } from 'nativescript-material-core';
8+
import { profile } from '@nativescript/core/profiling/profiling';
89

10+
let LayoutInflater: typeof android.view.LayoutInflater;
11+
12+
let textId;
13+
let containedId;
14+
let flatId;
15+
let grayColorStateList: android.content.res.ColorStateList;
916
export class Button extends ButtonBase {
1017
nativeViewProtected: com.google.android.material.button.MaterialButton;
1118

@@ -14,27 +21,46 @@ export class Button extends ButtonBase {
1421
get android(): com.google.android.material.button.MaterialButton {
1522
return this.nativeView;
1623
}
17-
24+
25+
@profile
1826
public createNativeView() {
19-
let layoutIdName = 'material_button';
20-
if (this.variant === 'text' || this.variant === 'outline') {
21-
layoutIdName = 'material_button_text';
22-
} else if (this.variant === 'flat') {
23-
layoutIdName = 'material_button_flat';
27+
let layoutId;
28+
const variant = this.variant;
29+
// let layoutIdName = 'material_button';
30+
if (variant === 'text' || variant === 'outline') {
31+
if (!textId) {
32+
textId = getLayout('material_button_text');
33+
}
34+
layoutId = textId;
35+
} else if (variant === 'flat') {
36+
if (!flatId) {
37+
flatId = getLayout('material_button_flat');
38+
}
39+
layoutId = flatId;
2440
} else {
41+
if (!containedId) {
42+
containedId = getLayout('material_button');
43+
}
44+
layoutId = containedId;
2545
// contained
2646
// we need to set the default through css or user would not be able to overload it through css...
2747
this.style['css:margin-left'] = 10;
2848
this.style['css:margin-right'] = 10;
2949
this.style['css:margin-top'] = 12;
3050
this.style['css:margin-bottom'] = 12;
3151
}
32-
const layoutId = getLayout(layoutIdName);
52+
// const layoutId = getLayout(layoutIdName);
53+
if (!LayoutInflater) {
54+
LayoutInflater = android.view.LayoutInflater;
55+
}
3356
const view = android.view.LayoutInflater.from(this._context).inflate(layoutId, null, false) as com.google.android.material.button.MaterialButton;
3457

35-
if (this.variant === 'outline') {
58+
if (variant === 'outline') {
3659
view.setStrokeWidth(1);
37-
view.setStrokeColor(android.content.res.ColorStateList.valueOf(new Color('gray').android));
60+
if (!grayColorStateList) {
61+
grayColorStateList = android.content.res.ColorStateList.valueOf(new Color('gray').android);
62+
}
63+
view.setStrokeColor(grayColorStateList);
3864
}
3965
return view;
4066
}
@@ -80,35 +106,37 @@ export class Button extends ButtonBase {
80106
this.nativeViewProtected.setStrokeWidth(newValue);
81107
}
82108
[backgroundInternalProperty.setNative](value: android.graphics.drawable.Drawable | Background) {
83-
if (this.nativeViewProtected) {
109+
const view = this.nativeTextViewProtected;
110+
if (view) {
84111
if (value instanceof android.graphics.drawable.Drawable) {
85-
this.nativeViewProtected.setBackgroundDrawable(value);
112+
view.setBackgroundDrawable(value);
86113
} else {
87114
if (value.color) {
88-
this.nativeViewProtected.setBackgroundTintList(getEnabledColorStateList(value.color.android, this.variant));
115+
view.setBackgroundTintList(getEnabledColorStateList(value.color.android, this.variant));
89116
}
90117
this.setCornerRadius(value.borderTopLeftRadius);
91-
this.nativeViewProtected.setStrokeWidth(value.borderTopWidth);
118+
view.setStrokeWidth(value.borderTopWidth);
92119
if (value.borderTopColor) {
93-
this.nativeViewProtected.setStrokeColor(android.content.res.ColorStateList.valueOf(value.borderTopColor.android));
120+
view.setStrokeColor(android.content.res.ColorStateList.valueOf(value.borderTopColor.android));
94121
}
95122
}
96123
}
97124
}
98125

99126
[verticalTextAlignmentProperty.setNative](value: VerticalTextAlignment) {
100-
const horizontalGravity = this.nativeTextViewProtected.getGravity() & android.view.Gravity.HORIZONTAL_GRAVITY_MASK;
127+
const view = this.nativeTextViewProtected;
128+
const horizontalGravity = view.getGravity() & android.view.Gravity.HORIZONTAL_GRAVITY_MASK;
101129
switch (value) {
102130
case 'initial':
103131
case 'top':
104-
this.nativeTextViewProtected.setGravity(android.view.Gravity.TOP | horizontalGravity);
132+
view.setGravity(android.view.Gravity.TOP | horizontalGravity);
105133
break;
106134
case 'middle':
107-
this.nativeTextViewProtected.setGravity(android.view.Gravity.CENTER_VERTICAL | horizontalGravity);
135+
view.setGravity(android.view.Gravity.CENTER_VERTICAL | horizontalGravity);
108136
break;
109137

110138
case 'bottom':
111-
this.nativeTextViewProtected.setGravity(android.view.Gravity.BOTTOM | horizontalGravity);
139+
view.setGravity(android.view.Gravity.BOTTOM | horizontalGravity);
112140
break;
113141
}
114142
}

0 commit comments

Comments
 (0)