mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
* Layout round instead of cailing Add helper method to layout module to convert to/from dips to px and measure the native view whiteSpace affects layout added for iOS Fix bug in switch onMeasure implementation Fix bug in cssValueToDevicePixels iOS implementation ActionBar for iOS is measured with AT_MOST modifier * Fix switch measure routine
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import {
|
|
SwitchBase, layout, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty
|
|
} from "./switch-common";
|
|
|
|
export * from "./switch-common";
|
|
|
|
class SwitchChangeHandlerImpl extends NSObject {
|
|
|
|
private _owner: WeakRef<Switch>;
|
|
|
|
public static initWithOwner(owner: WeakRef<Switch>): SwitchChangeHandlerImpl {
|
|
let handler = <SwitchChangeHandlerImpl>SwitchChangeHandlerImpl.new();
|
|
handler._owner = owner;
|
|
return handler;
|
|
}
|
|
|
|
public valueChanged(sender: UISwitch) {
|
|
let owner = this._owner.get();
|
|
if (owner) {
|
|
checkedProperty.nativeValueChange(owner, sender.on);
|
|
}
|
|
}
|
|
|
|
public static ObjCExposedMethods = {
|
|
'valueChanged': { returns: interop.types.void, params: [UISwitch] }
|
|
};
|
|
}
|
|
|
|
const zeroSize = { width: 0, height: 0 };
|
|
export class Switch extends SwitchBase {
|
|
private _ios: UISwitch;
|
|
private _handler: NSObject;
|
|
|
|
constructor() {
|
|
super();
|
|
this._ios = UISwitch.new();
|
|
|
|
this._handler = SwitchChangeHandlerImpl.initWithOwner(new WeakRef(this));
|
|
this._ios.addTargetActionForControlEvents(this._handler, "valueChanged", UIControlEvents.ValueChanged);
|
|
}
|
|
|
|
get ios(): UISwitch {
|
|
return this._ios;
|
|
}
|
|
|
|
get _nativeView(): UISwitch {
|
|
return this._ios;
|
|
}
|
|
|
|
get nativeView(): UISwitch {
|
|
return this._ios;
|
|
}
|
|
|
|
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
|
|
// It can't be anything different from 51x31
|
|
let nativeSize = this._nativeView.sizeThatFits(zeroSize);
|
|
this.width = nativeSize.width;
|
|
this.height = nativeSize.height;
|
|
|
|
const widthAndState = Switch.resolveSizeAndState(layout.toDevicePixels(nativeSize.width), layout.toDevicePixels(51), layout.EXACTLY, 0);
|
|
const heightAndState = Switch.resolveSizeAndState(layout.toDevicePixels(nativeSize.height), layout.toDevicePixels(31), layout.EXACTLY, 0);
|
|
this.setMeasuredDimension(widthAndState, heightAndState);
|
|
}
|
|
|
|
get [checkedProperty.native](): boolean {
|
|
return false;
|
|
}
|
|
set [checkedProperty.native](value: boolean) {
|
|
this._ios.on = value;
|
|
}
|
|
|
|
get [colorProperty.native](): UIColor {
|
|
return this._ios.thumbTintColor;
|
|
}
|
|
set [colorProperty.native](value: UIColor | Color) {
|
|
this._ios.thumbTintColor = value instanceof Color ? value.ios : value;
|
|
}
|
|
|
|
get [backgroundColorProperty.native](): UIColor {
|
|
return this._ios.onTintColor;
|
|
}
|
|
set [backgroundColorProperty.native](value: UIColor | Color) {
|
|
this._ios.onTintColor = value instanceof Color ? value.ios : value;
|
|
}
|
|
|
|
get [backgroundInternalProperty.native](): any {
|
|
return null;
|
|
}
|
|
set [backgroundInternalProperty.native](value: any) {
|
|
//
|
|
}
|
|
} |