Skip to content

Commit c87a665

Browse files
committed
chore: refactoring
1 parent 3ca6ba1 commit c87a665

File tree

3 files changed

+58
-120
lines changed

3 files changed

+58
-120
lines changed

Diff for: src/label-common.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ export const needFormattedStringComputation = function (
3232
) {
3333
const originalMethod = descriptor.value;
3434
descriptor.value = function (...args: any[]) {
35-
if (!this._canChangeText) {
36-
this._needFormattedStringComputation = true;
37-
return;
38-
}
39-
return originalMethod.apply(this, args);
40-
};
41-
};
42-
export const needFontComputation = function (target: any, propertyKey: string | Symbol, descriptor: PropertyDescriptor) {
43-
const originalMethod = descriptor.value;
44-
descriptor.value = function (...args: any[]) {
45-
if (!this._canChangeText) {
46-
this._needFontComputation = true;
35+
if (!this.mCanChangeText) {
36+
this.mNeedFormattedStringComputation = true;
4737
return;
4838
}
4939
return originalMethod.apply(this, args);
5040
};
5141
};
42+
// export const needFontComputation = function (target: any, propertyKey: string | Symbol, descriptor: PropertyDescriptor) {
43+
// const originalMethod = descriptor.value;
44+
// descriptor.value = function (...args: any[]) {
45+
// if (!this.mCanChangeText) {
46+
// this._needFontComputation = true;
47+
// return;
48+
// }
49+
// return originalMethod.apply(this, args);
50+
// };
51+
// };
5252

5353
@CSSType('HTMLLabel')
5454
export abstract class LabelBase extends TNLabel implements LabelViewDefinition {
@@ -67,20 +67,20 @@ export abstract class LabelBase extends TNLabel implements LabelViewDefinition {
6767
@cssProperty maxFontSize: number;
6868
@cssProperty autoFontSizeStep: number;
6969

70-
_canChangeText = true;
71-
_needFormattedStringComputation = false;
72-
_needFontComputation = false;
70+
mCanChangeText = true;
71+
mNeedFormattedStringComputation = false;
72+
mNeedFontComputation = false;
7373
public onResumeNativeUpdates(): void {
7474
// {N} suspends properties update on `_suspendNativeUpdates`. So we only need to do this in onResumeNativeUpdates
75-
this._canChangeText = false;
75+
this.mCanChangeText = false;
7676
super.onResumeNativeUpdates();
77-
this._canChangeText = true;
78-
if (this._needFormattedStringComputation) {
79-
this._needFormattedStringComputation = false;
77+
this.mCanChangeText = true;
78+
if (this.mNeedFormattedStringComputation) {
79+
this.mNeedFormattedStringComputation = false;
8080
this._setNativeText();
8181
}
82-
if (this._needFontComputation) {
83-
this._needFontComputation = false;
82+
if (this.mNeedFontComputation) {
83+
this.mNeedFontComputation = false;
8484
this[fontInternalProperty.setNative](this.style.fontInternal);
8585
}
8686
}

Diff for: src/label.android.ts

+15-71
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,6 @@ let TextView: typeof com.nativescript.label.EllipsizingTextView;
6060
const CHILD_FORMATTED_TEXT = 'formattedText';
6161

6262
const resetSymbol = Symbol('textPropertyDefault');
63-
enum SuspendType {
64-
Incremental = 0,
65-
Loaded = 1 << 20,
66-
NativeView = 1 << 21,
67-
UISetup = 1 << 22,
68-
IncrementalCountMask = ~((1 << 20) + (1 << 21) + (1 << 22))
69-
}
70-
declare module '@nativescript/core/ui/core/view-base' {
71-
interface ViewBase {
72-
_resumeNativeUpdates(type: SuspendType);
73-
_defaultPaddingTop: number;
74-
_defaultPaddingRight: number;
75-
_defaultPaddingBottom: number;
76-
_defaultPaddingLeft: number;
77-
_isPaddingRelative: boolean;
78-
_androidView: any;
79-
}
80-
}
8163
declare module '@nativescript/core/ui/text-base' {
8264
interface TextBase {
8365
_setTappableState(tappable: boolean);
@@ -245,15 +227,15 @@ abstract class LabelBase extends View implements LabelViewDefinition {
245227
@cssProperty paddingLeft: CoreTypes.LengthType;
246228

247229
// for now code is duplicated as Android version is a full rewrite
248-
_canChangeText = true;
249-
_needFormattedStringComputation = false;
230+
mCanChangeText = true;
231+
mNeedFormattedStringComputation = false;
250232
public onResumeNativeUpdates(): void {
251233
// {N} suspends properties update on `_suspendNativeUpdates`. So we only need to do this in onResumeNativeUpdates
252-
this._canChangeText = false;
234+
this.mCanChangeText = false;
253235
super.onResumeNativeUpdates();
254-
this._canChangeText = true;
255-
if (this._needFormattedStringComputation) {
256-
this._needFormattedStringComputation = false;
236+
this.mCanChangeText = true;
237+
if (this.mNeedFormattedStringComputation) {
238+
this.mNeedFormattedStringComputation = false;
257239
this._setNativeText();
258240
}
259241
}
@@ -319,9 +301,10 @@ abstract class LabelBase extends View implements LabelViewDefinition {
319301
export class Label extends LabelBase {
320302
nativeViewProtected: com.nativescript.label.EllipsizingTextView;
321303
mHandleFontSize = true;
304+
mTappable = false;
322305
private mAutoFontSize = false;
323306

324-
private _defaultMovementMethod: android.text.method.MovementMethod;
307+
private mDefaultMovementMethod: android.text.method.MovementMethod;
325308
get nativeTextViewProtected() {
326309
return this.nativeViewProtected;
327310
}
@@ -616,23 +599,22 @@ export class Label extends LabelBase {
616599
}
617600
return result;
618601
}
619-
_tappable = false;
620602
_setTappableState(tappable: boolean) {
621-
if (this._tappable !== tappable) {
622-
this._tappable = tappable;
623-
if (this._tappable) {
603+
if (this.mTappable !== tappable) {
604+
this.mTappable = tappable;
605+
if (this.mTappable) {
624606
this.nativeViewProtected.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());
625607
this.nativeViewProtected.setHighlightColor(null);
626608
} else {
627-
this.nativeViewProtected.setMovementMethod(this._defaultMovementMethod);
609+
this.nativeViewProtected.setMovementMethod(this.mDefaultMovementMethod);
628610
}
629611
}
630612
}
631613

632614
@profile
633615
_setNativeText(reset: boolean = false): void {
634-
if (!this._canChangeText) {
635-
this._needFormattedStringComputation = true;
616+
if (!this.mCanChangeText) {
617+
this.mNeedFormattedStringComputation = true;
636618
return;
637619
}
638620
if (reset) {
@@ -657,52 +639,14 @@ export class Label extends LabelBase {
657639
}
658640
this.nativeTextViewProtected.setText(transformedText);
659641
}
660-
661-
@profile
662-
public _setupUI(context: android.content.Context, atIndex?: number, parentIsLoaded?: boolean): void {
663-
if (this._context === context) {
664-
return;
665-
} else if (this._context) {
666-
this._tearDownUI(true);
667-
}
668-
669-
this._context = context;
670-
671-
// This will account for nativeView that is created in createNativeView, recycled
672-
// or for backward compatability - set before _setupUI in iOS contructor.
673-
let nativeView = this.nativeViewProtected;
674-
675-
if (!nativeView) {
676-
nativeView = this.createNativeView();
677-
}
678-
679-
this._androidView = nativeView;
680-
// if (nativeView) {
681-
// if (this._isPaddingRelative === undefined) {
682-
// this._isPaddingRelative = false;
683-
// }
684-
685-
// this._defaultPaddingTop = 0;
686-
// this._defaultPaddingRight = 0;
687-
// this._defaultPaddingBottom = 0;
688-
// this._defaultPaddingLeft = 0;
689-
690-
// }
691-
692-
this.setNativeView(nativeView);
693-
if (this.parent) {
694-
this._isAddedToNativeVisualTree = this.parent._addViewToNativeVisualTree(this, undefined);
695-
}
696-
this._resumeNativeUpdates(SuspendType.UISetup);
697-
}
698642
}
699643

700644
function getCapitalizedString(str: string): string {
701645
const words = str.split(' ');
702646
const newWords = [];
703647
for (let i = 0, length = words.length; i < length; i++) {
704648
const word = words[i].toLowerCase();
705-
newWords.push(word.substr(0, 1).toUpperCase() + word.substring(1));
649+
newWords.push(word.substring(0, 1).toUpperCase() + word.substring(1));
706650
}
707651

708652
return newWords.join(' ');

Diff for: src/label.ios.ts

+22-28
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {
3737
linkColorProperty,
3838
linkUnderlineProperty,
3939
maxLinesProperty,
40-
needFontComputation,
4140
needFormattedStringComputation,
4241
selectableProperty,
4342
textShadowProperty
@@ -159,12 +158,17 @@ class LabelObserverClass extends NSObject {
159158
}
160159

161160
export class Label extends LabelBase {
162-
private _observer: NSObject;
161+
private mObserver: NSObject;
163162
nativeViewProtected: UITextView;
164163
nativeTextViewProtected: UITextView;
165164
attributedString: NSMutableAttributedString;
166-
private _delegate: LabelUITextViewDelegateImpl;
165+
private mDelegate: LabelUITextViewDelegateImpl;
166+
private mFixedSize: FixedSize;
167167
static DTCORETEXT_INIT = false;
168+
169+
fontSizeRatio = 1;
170+
mLastAutoSizeKey: string;
171+
168172
// constructor() {
169173
// super();
170174
// if (iOSUseDTCoreText && !Label.DTCORETEXT_INIT) {
@@ -194,32 +198,32 @@ export class Label extends LabelBase {
194198

195199
public initNativeView() {
196200
super.initNativeView();
197-
this._delegate = LabelUITextViewDelegateImpl.initWithOwner(new WeakRef(this));
198-
this._observer = LabelObserverClass.alloc().init();
199-
this._observer['_owner'] = new WeakRef(this);
201+
this.mDelegate = LabelUITextViewDelegateImpl.initWithOwner(new WeakRef(this));
202+
this.mObserver = LabelObserverClass.alloc().init();
203+
this.mObserver['_owner'] = new WeakRef(this);
200204
this.nativeViewProtected.addObserverForKeyPathOptionsContext(
201-
this._observer,
205+
this.mObserver,
202206
'contentSize',
203207
NSKeyValueObservingOptions.New,
204208
null
205209
);
206210
this.nativeViewProtected.attributedText = this.attributedString;
207211
}
208212
public disposeNativeView() {
209-
this._delegate = null;
213+
this.mDelegate = null;
210214
super.disposeNativeView();
211215
// if (this._htmlTapGestureRecognizer) {
212216
// this.nativeViewProtected.removeGestureRecognizer(this._htmlTapGestureRecognizer);
213217
// this._htmlTapGestureRecognizer = null;
214218
// }
215-
if (this._observer) {
216-
this.nativeViewProtected.removeObserverForKeyPath(this._observer, 'contentSize');
217-
this._observer = null;
219+
if (this.mObserver) {
220+
this.nativeViewProtected.removeObserverForKeyPath(this.mObserver, 'contentSize');
221+
this.mObserver = null;
218222
}
219223
}
220224
public onLoaded() {
221225
super.onLoaded();
222-
this.nativeTextViewProtected.delegate = this._delegate;
226+
this.nativeTextViewProtected.delegate = this.mDelegate;
223227
}
224228

225229
public onUnloaded() {
@@ -302,17 +306,11 @@ export class Label extends LabelBase {
302306
}
303307
}
304308

305-
// @ts-ignore
306-
get ios(): UITextView {
307-
return this.nativeViewProtected;
308-
}
309-
private _fixedSize: FixedSize;
310-
311309
_requestLayoutOnTextChanged(): void {
312-
if (this._fixedSize === FixedSize.BOTH) {
310+
if (this.mFixedSize === FixedSize.BOTH) {
313311
return;
314312
}
315-
if (this._fixedSize === FixedSize.WIDTH && !this.textWrap && this.getMeasuredHeight() > 0) {
313+
if (this.mFixedSize === FixedSize.WIDTH && !this.textWrap && this.getMeasuredHeight() > 0) {
316314
// Single line label with fixed width will skip request layout on text change.
317315
return;
318316
}
@@ -513,8 +511,8 @@ export class Label extends LabelBase {
513511
if (!this.formattedText && !this.html) {
514512
nativeView.font = newFont;
515513
} else if (newFont) {
516-
if (!this._canChangeText) {
517-
this._needFormattedStringComputation = true;
514+
if (!this.mCanChangeText) {
515+
this.mNeedFormattedStringComputation = true;
518516
return;
519517
}
520518
this._setNativeText();
@@ -763,10 +761,6 @@ export class Label extends LabelBase {
763761
}
764762
}
765763
}
766-
767-
fontSizeRatio = 1;
768-
_lastAutoSizeKey: string;
769-
770764
updateAutoFontSize({
771765
textView,
772766
width,
@@ -799,11 +793,11 @@ export class Label extends LabelBase {
799793
let expectFont = (this.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(fontSize));
800794
//if we are not on the "default" font size we need to measure again or we could break
801795
//the layout behavior like for flexbox where there are multiple measure passes
802-
if (!force && autoSizeKey === this._lastAutoSizeKey && expectFont.pointSize === textView.font.pointSize) {
796+
if (!force && autoSizeKey === this.mLastAutoSizeKey && expectFont.pointSize === textView.font.pointSize) {
803797
return null;
804798
}
805799
currentFont = textView.font;
806-
this._lastAutoSizeKey = autoSizeKey;
800+
this.mLastAutoSizeKey = autoSizeKey;
807801
const nbLines = textView.textContainer.maximumNumberOfLines;
808802
// we need to reset verticalTextAlignment or computation will be wrong
809803
this.updateTextContainerInset(false);

0 commit comments

Comments
 (0)