UNPKG

6.19 kBJavaScriptView Raw
1import { SwitchBase, checkedProperty, offBackgroundColorProperty } from './switch-common';
2import { colorProperty, backgroundColorProperty, backgroundInternalProperty } from '../styling/style-properties';
3import { Color } from '../../color';
4import { iOSNativeHelper, layout } from '../../utils';
5export * from './switch-common';
6const majorVersion = iOSNativeHelper.MajorVersion;
7var SwitchChangeHandlerImpl = /** @class */ (function (_super) {
8 __extends(SwitchChangeHandlerImpl, _super);
9 function SwitchChangeHandlerImpl() {
10 return _super !== null && _super.apply(this, arguments) || this;
11 }
12 SwitchChangeHandlerImpl.initWithOwner = function (owner) {
13 var handler = SwitchChangeHandlerImpl.new();
14 handler._owner = owner;
15 return handler;
16 };
17 SwitchChangeHandlerImpl.prototype.valueChanged = function (sender) {
18 var _a;
19 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
20 if (owner) {
21 checkedProperty.nativeValueChange(owner, sender.on);
22 }
23 };
24 SwitchChangeHandlerImpl.ObjCExposedMethods = {
25 valueChanged: { returns: interop.types.void, params: [UISwitch] },
26 };
27 return SwitchChangeHandlerImpl;
28}(NSObject));
29const zeroSize = { width: 0, height: 0 };
30export class Switch extends SwitchBase {
31 constructor() {
32 super();
33 this.width = 51;
34 this.height = 31;
35 }
36 createNativeView() {
37 return UISwitch.new();
38 }
39 initNativeView() {
40 super.initNativeView();
41 const nativeView = this.nativeViewProtected;
42 this._handler = SwitchChangeHandlerImpl.initWithOwner(new WeakRef(this));
43 nativeView.addTargetActionForControlEvents(this._handler, 'valueChanged', 4096 /* UIControlEvents.ValueChanged */);
44 }
45 disposeNativeView() {
46 this._handler = null;
47 super.disposeNativeView();
48 }
49 setNativeBackgroundColor(value) {
50 if (value) {
51 this.nativeViewProtected.onTintColor = value instanceof Color ? value.ios : value;
52 this.nativeViewProtected.tintColor = value instanceof Color ? value.ios : value;
53 this.nativeViewProtected.backgroundColor = value instanceof Color ? value.ios : value;
54 this.nativeViewProtected.layer.cornerRadius = this.nativeViewProtected.frame.size.height / 2;
55 }
56 else {
57 this.nativeViewProtected.onTintColor = null;
58 this.nativeViewProtected.tintColor = null;
59 this.nativeViewProtected.backgroundColor = null;
60 this.nativeViewProtected.layer.cornerRadius = 0;
61 }
62 }
63 _onCheckedPropertyChanged(newValue) {
64 // only add :checked pseudo handling on supported iOS versions
65 // ios <13 works but causes glitchy animations when toggling
66 // so we decided to keep the old behavior on older versions.
67 if (majorVersion >= 13) {
68 super._onCheckedPropertyChanged(newValue);
69 if (this.offBackgroundColor) {
70 if (!newValue) {
71 this.setNativeBackgroundColor(this.offBackgroundColor);
72 }
73 else {
74 this.setNativeBackgroundColor(this.backgroundColor instanceof Color ? this.backgroundColor : new Color(this.backgroundColor));
75 }
76 }
77 }
78 }
79 // @ts-ignore
80 get ios() {
81 return this.nativeViewProtected;
82 }
83 onMeasure(widthMeasureSpec, heightMeasureSpec) {
84 // It can't be anything different from 51x31
85 const nativeSize = this.nativeViewProtected.sizeThatFits(zeroSize);
86 this.width = nativeSize.width;
87 this.height = nativeSize.height;
88 const widthAndState = Switch.resolveSizeAndState(layout.toDevicePixels(nativeSize.width), layout.toDevicePixels(51), layout.EXACTLY, 0);
89 const heightAndState = Switch.resolveSizeAndState(layout.toDevicePixels(nativeSize.height), layout.toDevicePixels(31), layout.EXACTLY, 0);
90 this.setMeasuredDimension(widthAndState, heightAndState);
91 }
92 [checkedProperty.getDefault]() {
93 return false;
94 }
95 [checkedProperty.setNative](value) {
96 this.nativeViewProtected.on = value;
97 }
98 [colorProperty.getDefault]() {
99 return this.nativeViewProtected.thumbTintColor;
100 }
101 [colorProperty.setNative](value) {
102 const color = value instanceof Color ? value.ios : value;
103 this.nativeViewProtected.thumbTintColor = color;
104 if (color && this.nativeViewProtected.subviews.count > 0) {
105 const alpha = new interop.Reference(1.0);
106 const res = color.getRedGreenBlueAlpha(null, null, null, alpha);
107 this.nativeViewProtected.subviews[0].alpha = (res && alpha.value) ?? 1;
108 }
109 }
110 [backgroundColorProperty.getDefault]() {
111 return this.nativeViewProtected.onTintColor;
112 }
113 [backgroundColorProperty.setNative](value) {
114 if (majorVersion >= 13) {
115 if (!this.offBackgroundColor || this.checked) {
116 this.setNativeBackgroundColor(value);
117 }
118 }
119 else {
120 // old behavior on unsupported iOS versions
121 this.nativeViewProtected.onTintColor = value instanceof Color ? value.ios : value;
122 }
123 }
124 [backgroundInternalProperty.getDefault]() {
125 return null;
126 }
127 [backgroundInternalProperty.setNative](value) {
128 //
129 }
130 [offBackgroundColorProperty.getDefault]() {
131 return this.nativeViewProtected.backgroundColor;
132 }
133 [offBackgroundColorProperty.setNative](value) {
134 if (majorVersion >= 13) {
135 if (!this.checked) {
136 this.setNativeBackgroundColor(value);
137 }
138 }
139 else {
140 // old behavior on unsupported iOS versions...
141 const nativeValue = value instanceof Color ? value.ios : value;
142 this.nativeViewProtected.tintColor = nativeValue;
143 this.nativeViewProtected.backgroundColor = nativeValue;
144 this.nativeViewProtected.layer.cornerRadius = this.nativeViewProtected.frame.size.height / 2;
145 }
146 }
147}
148//# sourceMappingURL=index.ios.js.map
\No newline at end of file