mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: format
This commit is contained in:
@@ -1,146 +1,167 @@
|
||||
import { Background } from "../styling/background";
|
||||
import {
|
||||
SliderBase, valueProperty, minValueProperty, maxValueProperty
|
||||
SliderBase,
|
||||
valueProperty,
|
||||
minValueProperty,
|
||||
maxValueProperty,
|
||||
} from "./slider-common";
|
||||
import { colorProperty, backgroundColorProperty, backgroundInternalProperty } from "../styling/style-properties";
|
||||
import {
|
||||
colorProperty,
|
||||
backgroundColorProperty,
|
||||
backgroundInternalProperty,
|
||||
} from "../styling/style-properties";
|
||||
import { Color } from "../../color";
|
||||
|
||||
export * from "./slider-common";
|
||||
|
||||
interface OwnerSeekBar extends android.widget.SeekBar {
|
||||
owner: Slider;
|
||||
owner: Slider;
|
||||
}
|
||||
|
||||
let SeekBar: typeof android.widget.SeekBar;
|
||||
let SeekBarChangeListener: android.widget.SeekBar.OnSeekBarChangeListener;
|
||||
|
||||
function initializeListenerClass(): void {
|
||||
if (!SeekBarChangeListener) {
|
||||
@Interfaces([android.widget.SeekBar.OnSeekBarChangeListener])
|
||||
class SeekBarChangeListenerImpl extends java.lang.Object
|
||||
implements android.widget.SeekBar.OnSeekBarChangeListener {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
if (!SeekBarChangeListener) {
|
||||
@Interfaces([android.widget.SeekBar.OnSeekBarChangeListener])
|
||||
class SeekBarChangeListenerImpl extends java.lang.Object implements android.widget.SeekBar.OnSeekBarChangeListener {
|
||||
constructor() {
|
||||
super();
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
return global.__native(this);
|
||||
}
|
||||
onProgressChanged(
|
||||
seekBar: OwnerSeekBar,
|
||||
progress: number,
|
||||
fromUser: boolean
|
||||
): void {
|
||||
const owner = seekBar.owner;
|
||||
if (owner && !owner._supressNativeValue) {
|
||||
const newValue = progress + owner.minValue;
|
||||
valueProperty.nativeValueChange(owner, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
onProgressChanged(seekBar: OwnerSeekBar, progress: number, fromUser: boolean): void {
|
||||
const owner = seekBar.owner;
|
||||
if (owner && !owner._supressNativeValue) {
|
||||
const newValue = progress + owner.minValue;
|
||||
valueProperty.nativeValueChange(owner, newValue);
|
||||
}
|
||||
}
|
||||
onStartTrackingTouch(seekBar: OwnerSeekBar): void {
|
||||
//
|
||||
}
|
||||
|
||||
onStartTrackingTouch(seekBar: OwnerSeekBar): void {
|
||||
//
|
||||
}
|
||||
onStopTrackingTouch(seekBar: OwnerSeekBar): void {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
onStopTrackingTouch(seekBar: OwnerSeekBar): void {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
SeekBarChangeListener = new SeekBarChangeListenerImpl();
|
||||
}
|
||||
SeekBarChangeListener = new SeekBarChangeListenerImpl();
|
||||
}
|
||||
}
|
||||
|
||||
function getListener(): android.widget.SeekBar.OnSeekBarChangeListener {
|
||||
return SeekBarChangeListener;
|
||||
return SeekBarChangeListener;
|
||||
}
|
||||
|
||||
export class Slider extends SliderBase {
|
||||
_supressNativeValue: boolean;
|
||||
nativeViewProtected: OwnerSeekBar;
|
||||
_supressNativeValue: boolean;
|
||||
nativeViewProtected: OwnerSeekBar;
|
||||
|
||||
public createNativeView() {
|
||||
if (!SeekBar) {
|
||||
SeekBar = android.widget.SeekBar;
|
||||
}
|
||||
public createNativeView() {
|
||||
if (!SeekBar) {
|
||||
SeekBar = android.widget.SeekBar;
|
||||
}
|
||||
|
||||
return new SeekBar(this._context);
|
||||
}
|
||||
return new SeekBar(this._context);
|
||||
}
|
||||
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
nativeView.owner = this;
|
||||
initializeListenerClass();
|
||||
const listener = getListener();
|
||||
nativeView.setOnSeekBarChangeListener(listener);
|
||||
}
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
nativeView.owner = this;
|
||||
initializeListenerClass();
|
||||
const listener = getListener();
|
||||
nativeView.setOnSeekBarChangeListener(listener);
|
||||
}
|
||||
|
||||
public disposeNativeView() {
|
||||
this.nativeViewProtected.owner = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
public disposeNativeView() {
|
||||
this.nativeViewProtected.owner = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
|
||||
public resetNativeView(): void {
|
||||
super.resetNativeView();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
nativeView.setMax(100);
|
||||
nativeView.setProgress(0);
|
||||
nativeView.setKeyProgressIncrement(1);
|
||||
}
|
||||
public resetNativeView(): void {
|
||||
super.resetNativeView();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
nativeView.setMax(100);
|
||||
nativeView.setProgress(0);
|
||||
nativeView.setKeyProgressIncrement(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* There is no minValue in Android. We simulate this by subtracting the minValue from the native value and maxValue.
|
||||
* We need this method to call native setMax and setProgress methods when minValue property is changed,
|
||||
* without handling the native value changed callback.
|
||||
*/
|
||||
private setNativeValuesSilently() {
|
||||
this._supressNativeValue = true;
|
||||
const nativeView = this.nativeViewProtected;
|
||||
try {
|
||||
nativeView.setMax(this.maxValue - this.minValue);
|
||||
nativeView.setProgress(this.value - this.minValue);
|
||||
} finally {
|
||||
this._supressNativeValue = false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* There is no minValue in Android. We simulate this by subtracting the minValue from the native value and maxValue.
|
||||
* We need this method to call native setMax and setProgress methods when minValue property is changed,
|
||||
* without handling the native value changed callback.
|
||||
*/
|
||||
private setNativeValuesSilently() {
|
||||
this._supressNativeValue = true;
|
||||
const nativeView = this.nativeViewProtected;
|
||||
try {
|
||||
nativeView.setMax(this.maxValue - this.minValue);
|
||||
nativeView.setProgress(this.value - this.minValue);
|
||||
} finally {
|
||||
this._supressNativeValue = false;
|
||||
}
|
||||
}
|
||||
|
||||
[valueProperty.setNative](value: number) {
|
||||
this.setNativeValuesSilently();
|
||||
}
|
||||
[valueProperty.setNative](value: number) {
|
||||
this.setNativeValuesSilently();
|
||||
}
|
||||
|
||||
[minValueProperty.setNative](value: number) {
|
||||
this.setNativeValuesSilently();
|
||||
}
|
||||
[minValueProperty.setNative](value: number) {
|
||||
this.setNativeValuesSilently();
|
||||
}
|
||||
|
||||
[maxValueProperty.getDefault](): number {
|
||||
return 100;
|
||||
}
|
||||
[maxValueProperty.setNative](value: number) {
|
||||
this.setNativeValuesSilently();
|
||||
}
|
||||
[maxValueProperty.getDefault](): number {
|
||||
return 100;
|
||||
}
|
||||
[maxValueProperty.setNative](value: number) {
|
||||
this.setNativeValuesSilently();
|
||||
}
|
||||
|
||||
[colorProperty.getDefault](): number {
|
||||
return -1;
|
||||
}
|
||||
[colorProperty.setNative](value: number | Color) {
|
||||
if (value instanceof Color) {
|
||||
this.nativeViewProtected.getThumb().setColorFilter(value.android, android.graphics.PorterDuff.Mode.SRC_IN);
|
||||
} else {
|
||||
this.nativeViewProtected.getThumb().clearColorFilter();
|
||||
}
|
||||
}
|
||||
[colorProperty.getDefault](): number {
|
||||
return -1;
|
||||
}
|
||||
[colorProperty.setNative](value: number | Color) {
|
||||
if (value instanceof Color) {
|
||||
this.nativeViewProtected
|
||||
.getThumb()
|
||||
.setColorFilter(
|
||||
value.android,
|
||||
android.graphics.PorterDuff.Mode.SRC_IN
|
||||
);
|
||||
} else {
|
||||
this.nativeViewProtected.getThumb().clearColorFilter();
|
||||
}
|
||||
}
|
||||
|
||||
[backgroundColorProperty.getDefault](): number {
|
||||
return -1;
|
||||
}
|
||||
[backgroundColorProperty.setNative](value: number | Color) {
|
||||
if (value instanceof Color) {
|
||||
this.nativeViewProtected.getProgressDrawable().setColorFilter(value.android, android.graphics.PorterDuff.Mode.SRC_IN);
|
||||
} else {
|
||||
this.nativeViewProtected.getProgressDrawable().clearColorFilter();
|
||||
}
|
||||
}
|
||||
[backgroundColorProperty.getDefault](): number {
|
||||
return -1;
|
||||
}
|
||||
[backgroundColorProperty.setNative](value: number | Color) {
|
||||
if (value instanceof Color) {
|
||||
this.nativeViewProtected
|
||||
.getProgressDrawable()
|
||||
.setColorFilter(
|
||||
value.android,
|
||||
android.graphics.PorterDuff.Mode.SRC_IN
|
||||
);
|
||||
} else {
|
||||
this.nativeViewProtected.getProgressDrawable().clearColorFilter();
|
||||
}
|
||||
}
|
||||
|
||||
[backgroundInternalProperty.getDefault](): Background {
|
||||
return null;
|
||||
}
|
||||
[backgroundInternalProperty.setNative](value: Background) {
|
||||
//
|
||||
}
|
||||
[backgroundInternalProperty.getDefault](): Background {
|
||||
return null;
|
||||
}
|
||||
[backgroundInternalProperty.setNative](value: Background) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
40
nativescript-core/ui/slider/index.d.ts
vendored
40
nativescript-core/ui/slider/index.d.ts
vendored
@@ -5,30 +5,30 @@ import { Property, CoercibleProperty } from "../core/properties";
|
||||
* Represents a slider component.
|
||||
*/
|
||||
export class Slider extends View {
|
||||
/**
|
||||
* Gets the native [android widget](http://developer.android.com/reference/android/widget/SeekBar.html) that represents the user interface for this component. Valid only when running on Android OS.
|
||||
*/
|
||||
android: any /* android.widget.SeekBar */;
|
||||
/**
|
||||
* Gets the native [android widget](http://developer.android.com/reference/android/widget/SeekBar.html) that represents the user interface for this component. Valid only when running on Android OS.
|
||||
*/
|
||||
android: any /* android.widget.SeekBar */;
|
||||
|
||||
/**
|
||||
* Gets the native iOS [UISlider](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISlider_Class/) that represents the user interface for this component. Valid only when running on iOS.
|
||||
*/
|
||||
ios: any /* UISlider */;
|
||||
/**
|
||||
* Gets the native iOS [UISlider](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISlider_Class/) that represents the user interface for this component. Valid only when running on iOS.
|
||||
*/
|
||||
ios: any /* UISlider */;
|
||||
|
||||
/**
|
||||
* Gets or sets a slider current value. The default value is 0.
|
||||
*/
|
||||
value: number;
|
||||
/**
|
||||
* Gets or sets a slider current value. The default value is 0.
|
||||
*/
|
||||
value: number;
|
||||
|
||||
/**
|
||||
* Gets or sets a slider min value. The default value is 0.
|
||||
*/
|
||||
minValue: number;
|
||||
/**
|
||||
* Gets or sets a slider min value. The default value is 0.
|
||||
*/
|
||||
minValue: number;
|
||||
|
||||
/**
|
||||
* Gets or sets a slider max value. The default value is 100.
|
||||
*/
|
||||
maxValue: number;
|
||||
/**
|
||||
* Gets or sets a slider max value. The default value is 100.
|
||||
*/
|
||||
maxValue: number;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,102 +1,116 @@
|
||||
import { Background } from "../styling/background";
|
||||
|
||||
import {
|
||||
SliderBase, valueProperty, minValueProperty, maxValueProperty
|
||||
SliderBase,
|
||||
valueProperty,
|
||||
minValueProperty,
|
||||
maxValueProperty,
|
||||
} from "./slider-common";
|
||||
import { colorProperty, backgroundColorProperty, backgroundInternalProperty } from "../styling/style-properties";
|
||||
import {
|
||||
colorProperty,
|
||||
backgroundColorProperty,
|
||||
backgroundInternalProperty,
|
||||
} from "../styling/style-properties";
|
||||
import { Color } from "../../color";
|
||||
|
||||
export * from "./slider-common";
|
||||
|
||||
class SliderChangeHandlerImpl extends NSObject {
|
||||
private _owner: WeakRef<Slider>;
|
||||
|
||||
private _owner: WeakRef<Slider>;
|
||||
public static initWithOwner(
|
||||
owner: WeakRef<Slider>
|
||||
): SliderChangeHandlerImpl {
|
||||
let handler = <SliderChangeHandlerImpl>SliderChangeHandlerImpl.new();
|
||||
handler._owner = owner;
|
||||
|
||||
public static initWithOwner(owner: WeakRef<Slider>): SliderChangeHandlerImpl {
|
||||
let handler = <SliderChangeHandlerImpl>SliderChangeHandlerImpl.new();
|
||||
handler._owner = owner;
|
||||
return handler;
|
||||
}
|
||||
|
||||
return handler;
|
||||
}
|
||||
public sliderValueChanged(sender: UISlider) {
|
||||
let owner = this._owner.get();
|
||||
if (owner) {
|
||||
valueProperty.nativeValueChange(owner, sender.value);
|
||||
}
|
||||
}
|
||||
|
||||
public sliderValueChanged(sender: UISlider) {
|
||||
let owner = this._owner.get();
|
||||
if (owner) {
|
||||
valueProperty.nativeValueChange(owner, sender.value);
|
||||
}
|
||||
}
|
||||
|
||||
public static ObjCExposedMethods = {
|
||||
"sliderValueChanged": { returns: interop.types.void, params: [UISlider] }
|
||||
};
|
||||
public static ObjCExposedMethods = {
|
||||
sliderValueChanged: { returns: interop.types.void, params: [UISlider] },
|
||||
};
|
||||
}
|
||||
|
||||
export class Slider extends SliderBase {
|
||||
nativeViewProtected: UISlider;
|
||||
private _changeHandler: NSObject;
|
||||
nativeViewProtected: UISlider;
|
||||
private _changeHandler: NSObject;
|
||||
|
||||
public createNativeView() {
|
||||
return UISlider.new();
|
||||
}
|
||||
public createNativeView() {
|
||||
return UISlider.new();
|
||||
}
|
||||
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
// default values
|
||||
nativeView.minimumValue = 0;
|
||||
nativeView.maximumValue = this.maxValue;
|
||||
this._changeHandler = SliderChangeHandlerImpl.initWithOwner(new WeakRef(this));
|
||||
nativeView.addTargetActionForControlEvents(this._changeHandler, "sliderValueChanged", UIControlEvents.ValueChanged);
|
||||
}
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
// default values
|
||||
nativeView.minimumValue = 0;
|
||||
nativeView.maximumValue = this.maxValue;
|
||||
this._changeHandler = SliderChangeHandlerImpl.initWithOwner(
|
||||
new WeakRef(this)
|
||||
);
|
||||
nativeView.addTargetActionForControlEvents(
|
||||
this._changeHandler,
|
||||
"sliderValueChanged",
|
||||
UIControlEvents.ValueChanged
|
||||
);
|
||||
}
|
||||
|
||||
public disposeNativeView() {
|
||||
this._changeHandler = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
public disposeNativeView() {
|
||||
this._changeHandler = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
|
||||
get ios(): UISlider {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
get ios(): UISlider {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
|
||||
[valueProperty.getDefault](): number {
|
||||
return 0;
|
||||
}
|
||||
[valueProperty.setNative](value: number) {
|
||||
this.ios.value = value;
|
||||
}
|
||||
[minValueProperty.getDefault](): number {
|
||||
return 0;
|
||||
}
|
||||
[minValueProperty.setNative](value: number) {
|
||||
this.ios.minimumValue = value;
|
||||
}
|
||||
[maxValueProperty.getDefault](): number {
|
||||
return 100;
|
||||
}
|
||||
[maxValueProperty.setNative](value: number) {
|
||||
this.ios.maximumValue = value;
|
||||
}
|
||||
[valueProperty.getDefault](): number {
|
||||
return 0;
|
||||
}
|
||||
[valueProperty.setNative](value: number) {
|
||||
this.ios.value = value;
|
||||
}
|
||||
[minValueProperty.getDefault](): number {
|
||||
return 0;
|
||||
}
|
||||
[minValueProperty.setNative](value: number) {
|
||||
this.ios.minimumValue = value;
|
||||
}
|
||||
[maxValueProperty.getDefault](): number {
|
||||
return 100;
|
||||
}
|
||||
[maxValueProperty.setNative](value: number) {
|
||||
this.ios.maximumValue = value;
|
||||
}
|
||||
|
||||
[colorProperty.getDefault](): UIColor {
|
||||
return this.ios.thumbTintColor;
|
||||
}
|
||||
[colorProperty.setNative](value: UIColor | Color) {
|
||||
let color = value instanceof Color ? value.ios : value;
|
||||
this.ios.thumbTintColor = color;
|
||||
}
|
||||
[colorProperty.getDefault](): UIColor {
|
||||
return this.ios.thumbTintColor;
|
||||
}
|
||||
[colorProperty.setNative](value: UIColor | Color) {
|
||||
let color = value instanceof Color ? value.ios : value;
|
||||
this.ios.thumbTintColor = color;
|
||||
}
|
||||
|
||||
[backgroundColorProperty.getDefault](): UIColor {
|
||||
return this.ios.minimumTrackTintColor;
|
||||
}
|
||||
[backgroundColorProperty.setNative](value: UIColor | Color) {
|
||||
let color = value instanceof Color ? value.ios : value;
|
||||
this.ios.minimumTrackTintColor = color;
|
||||
}
|
||||
[backgroundColorProperty.getDefault](): UIColor {
|
||||
return this.ios.minimumTrackTintColor;
|
||||
}
|
||||
[backgroundColorProperty.setNative](value: UIColor | Color) {
|
||||
let color = value instanceof Color ? value.ios : value;
|
||||
this.ios.minimumTrackTintColor = color;
|
||||
}
|
||||
|
||||
[backgroundInternalProperty.getDefault](): Background {
|
||||
return null;
|
||||
}
|
||||
[backgroundInternalProperty.setNative](value: Background) {
|
||||
//
|
||||
}
|
||||
[backgroundInternalProperty.getDefault](): Background {
|
||||
return null;
|
||||
}
|
||||
[backgroundInternalProperty.setNative](value: Background) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ import { Property, CoercibleProperty } from "../core/properties";
|
||||
// TODO: Extract base Range class for slider and progress
|
||||
@CSSType("Slider")
|
||||
export class SliderBase extends View implements SliderDefinition {
|
||||
public value: number;
|
||||
public minValue: number;
|
||||
public maxValue: number;
|
||||
public value: number;
|
||||
public minValue: number;
|
||||
public maxValue: number;
|
||||
}
|
||||
|
||||
SliderBase.prototype.recycleNativeView = "auto";
|
||||
@@ -17,12 +17,15 @@ SliderBase.prototype.recycleNativeView = "auto";
|
||||
* Represents the observable property backing the value property of each Slider instance.
|
||||
*/
|
||||
export const valueProperty = new CoercibleProperty<SliderBase, number>({
|
||||
name: "value", defaultValue: 0, coerceValue: (target, value) => {
|
||||
value = Math.max(value, target.minValue);
|
||||
value = Math.min(value, target.maxValue);
|
||||
name: "value",
|
||||
defaultValue: 0,
|
||||
coerceValue: (target, value) => {
|
||||
value = Math.max(value, target.minValue);
|
||||
value = Math.min(value, target.maxValue);
|
||||
|
||||
return value;
|
||||
}, valueConverter: (v) => isIOS ? parseFloat(v) : parseInt(v)
|
||||
return value;
|
||||
},
|
||||
valueConverter: (v) => (isIOS ? parseFloat(v) : parseInt(v)),
|
||||
});
|
||||
valueProperty.register(SliderBase);
|
||||
|
||||
@@ -30,10 +33,13 @@ valueProperty.register(SliderBase);
|
||||
* Represents the observable property backing the minValue property of each Slider instance.
|
||||
*/
|
||||
export const minValueProperty = new Property<SliderBase, number>({
|
||||
name: "minValue", defaultValue: 0, valueChanged: (target, oldValue, newValue) => {
|
||||
maxValueProperty.coerce(target);
|
||||
valueProperty.coerce(target);
|
||||
}, valueConverter: (v) => isIOS ? parseFloat(v) : parseInt(v)
|
||||
name: "minValue",
|
||||
defaultValue: 0,
|
||||
valueChanged: (target, oldValue, newValue) => {
|
||||
maxValueProperty.coerce(target);
|
||||
valueProperty.coerce(target);
|
||||
},
|
||||
valueConverter: (v) => (isIOS ? parseFloat(v) : parseInt(v)),
|
||||
});
|
||||
minValueProperty.register(SliderBase);
|
||||
|
||||
@@ -41,15 +47,17 @@ minValueProperty.register(SliderBase);
|
||||
* Represents the observable property backing the maxValue property of each Slider instance.
|
||||
*/
|
||||
export const maxValueProperty = new CoercibleProperty<SliderBase, number>({
|
||||
name: "maxValue", defaultValue: 100, coerceValue: (target, value) => {
|
||||
let minValue = target.minValue;
|
||||
if (value < minValue) {
|
||||
value = minValue;
|
||||
}
|
||||
name: "maxValue",
|
||||
defaultValue: 100,
|
||||
coerceValue: (target, value) => {
|
||||
let minValue = target.minValue;
|
||||
if (value < minValue) {
|
||||
value = minValue;
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
valueChanged: (target, oldValue, newValue) => valueProperty.coerce(target),
|
||||
valueConverter: (v) => isIOS ? parseFloat(v) : parseInt(v)
|
||||
return value;
|
||||
},
|
||||
valueChanged: (target, oldValue, newValue) => valueProperty.coerce(target),
|
||||
valueConverter: (v) => (isIOS ? parseFloat(v) : parseInt(v)),
|
||||
});
|
||||
maxValueProperty.register(SliderBase);
|
||||
|
||||
Reference in New Issue
Block a user