1 | import { SwitchBase, checkedProperty, offBackgroundColorProperty } from './switch-common';
|
2 | import { colorProperty, backgroundColorProperty, backgroundInternalProperty } from '../styling/style-properties';
|
3 | import { Color } from '../../color';
|
4 | import { iOSNativeHelper, layout } from '../../utils';
|
5 | export * from './switch-common';
|
6 | const majorVersion = iOSNativeHelper.MajorVersion;
|
7 | var SwitchChangeHandlerImpl = (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));
|
29 | const zeroSize = { width: 0, height: 0 };
|
30 | export 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 );
|
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 |
|
65 |
|
66 |
|
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 |
|
80 | get ios() {
|
81 | return this.nativeViewProtected;
|
82 | }
|
83 | onMeasure(widthMeasureSpec, heightMeasureSpec) {
|
84 |
|
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 |
|
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 |
|
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 |
|
\ | No newline at end of file |