| 11 | const observableVisualStates = ['highlighted']; // States like :disabled are handled elsewhere |
| 12 | |
| 13 | export class Button extends ButtonBase { |
| 14 | public nativeViewProtected: UIButton; |
| 15 | |
| 16 | private _tapHandler: NSObject; |
| 17 | private _stateChangedHandler: ControlStateChangeListener; |
| 18 | |
| 19 | createNativeView() { |
| 20 | const nativeView = UIButton.buttonWithType(UIButtonType.System); |
| 21 | |
| 22 | // This is the default for both platforms |
| 23 | if (nativeView.titleLabel) { |
| 24 | nativeView.titleLabel.textAlignment = NSTextAlignment.Center; |
| 25 | } |
| 26 | return nativeView; |
| 27 | } |
| 28 | |
| 29 | public initNativeView(): void { |
| 30 | super.initNativeView(); |
| 31 | |
| 32 | this._tapHandler = TapHandlerImpl.initWithOwner(new WeakRef(this)); |
| 33 | this.nativeViewProtected.addTargetActionForControlEvents(this._tapHandler, 'tap', UIControlEvents.TouchUpInside); |
| 34 | this._setDefaultPaddings(this.nativeViewProtected.contentEdgeInsets); |
| 35 | } |
| 36 | |
| 37 | public disposeNativeView(): void { |
| 38 | this._tapHandler = null; |
| 39 | |
| 40 | if (this._stateChangedHandler) { |
| 41 | this._stateChangedHandler.stop(); |
| 42 | this._stateChangedHandler = null; |
| 43 | } |
| 44 | |
| 45 | super.disposeNativeView(); |
| 46 | } |
| 47 | |
| 48 | // @ts-ignore |
| 49 | get ios() { |
| 50 | return this.nativeViewProtected; |
| 51 | } |
| 52 | |
| 53 | @PseudoClassHandler('normal', 'highlighted', 'pressed', 'active') |
| 54 | _updateButtonStateChangeHandler(subscribe: boolean) { |
| 55 | if (subscribe) { |
| 56 | if (!this._stateChangedHandler) { |
| 57 | const viewRef = new WeakRef<Button>(this); |
| 58 | |
| 59 | this._stateChangedHandler = new ControlStateChangeListener(this.nativeViewProtected, observableVisualStates, (state: string, add: boolean) => { |
| 60 | const view = viewRef?.deref?.(); |
| 61 | |
| 62 | if (view) { |
| 63 | if (add) { |
| 64 | view._addVisualState(state); |
| 65 | } else { |
| 66 | view._removeVisualState(state); |
| 67 | } |
| 68 | } |
| 69 | }); |
| 70 | } |
nothing calls this directly
no test coverage detected