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,133 +1,162 @@
|
||||
import { Font } from "../styling/font";
|
||||
import {
|
||||
SegmentedBarItemBase, SegmentedBarBase, selectedIndexProperty, itemsProperty, selectedBackgroundColorProperty
|
||||
SegmentedBarItemBase,
|
||||
SegmentedBarBase,
|
||||
selectedIndexProperty,
|
||||
itemsProperty,
|
||||
selectedBackgroundColorProperty,
|
||||
} from "./segmented-bar-common";
|
||||
import { colorProperty, fontInternalProperty } from "../styling/style-properties";
|
||||
import {
|
||||
colorProperty,
|
||||
fontInternalProperty,
|
||||
} from "../styling/style-properties";
|
||||
import { Color } from "../../color";
|
||||
import { ios as iosUtils } from "../../utils/utils";
|
||||
export * from "./segmented-bar-common";
|
||||
|
||||
export class SegmentedBarItem extends SegmentedBarItemBase {
|
||||
public _update() {
|
||||
const parent = <SegmentedBar>this.parent;
|
||||
if (parent) {
|
||||
let tabIndex = parent.items.indexOf(this);
|
||||
let title = this.title;
|
||||
title = (title === null || title === undefined) ? "" : title;
|
||||
parent.ios.setTitleForSegmentAtIndex(title, tabIndex);
|
||||
}
|
||||
}
|
||||
public _update() {
|
||||
const parent = <SegmentedBar>this.parent;
|
||||
if (parent) {
|
||||
let tabIndex = parent.items.indexOf(this);
|
||||
let title = this.title;
|
||||
title = title === null || title === undefined ? "" : title;
|
||||
parent.ios.setTitleForSegmentAtIndex(title, tabIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SegmentedBar extends SegmentedBarBase {
|
||||
nativeViewProtected: UISegmentedControl;
|
||||
private _selectionHandler: NSObject;
|
||||
nativeViewProtected: UISegmentedControl;
|
||||
private _selectionHandler: NSObject;
|
||||
|
||||
createNativeView() {
|
||||
return UISegmentedControl.new();
|
||||
}
|
||||
createNativeView() {
|
||||
return UISegmentedControl.new();
|
||||
}
|
||||
|
||||
initNativeView() {
|
||||
super.initNativeView();
|
||||
this._selectionHandler = SelectionHandlerImpl.initWithOwner(new WeakRef(this));
|
||||
this.nativeViewProtected.addTargetActionForControlEvents(this._selectionHandler, "selected", UIControlEvents.ValueChanged);
|
||||
}
|
||||
initNativeView() {
|
||||
super.initNativeView();
|
||||
this._selectionHandler = SelectionHandlerImpl.initWithOwner(
|
||||
new WeakRef(this)
|
||||
);
|
||||
this.nativeViewProtected.addTargetActionForControlEvents(
|
||||
this._selectionHandler,
|
||||
"selected",
|
||||
UIControlEvents.ValueChanged
|
||||
);
|
||||
}
|
||||
|
||||
disposeNativeView() {
|
||||
this._selectionHandler = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
disposeNativeView() {
|
||||
this._selectionHandler = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
|
||||
get ios(): UISegmentedControl {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
get ios(): UISegmentedControl {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
|
||||
[selectedIndexProperty.getDefault](): number {
|
||||
return -1;
|
||||
}
|
||||
[selectedIndexProperty.setNative](value: number) {
|
||||
this.ios.selectedSegmentIndex = value;
|
||||
}
|
||||
[selectedIndexProperty.getDefault](): number {
|
||||
return -1;
|
||||
}
|
||||
[selectedIndexProperty.setNative](value: number) {
|
||||
this.ios.selectedSegmentIndex = value;
|
||||
}
|
||||
|
||||
[itemsProperty.getDefault](): SegmentedBarItem[] {
|
||||
return null;
|
||||
}
|
||||
[itemsProperty.setNative](value: SegmentedBarItem[]) {
|
||||
const segmentedControl = this.ios;
|
||||
segmentedControl.removeAllSegments();
|
||||
const newItems = value;
|
||||
[itemsProperty.getDefault](): SegmentedBarItem[] {
|
||||
return null;
|
||||
}
|
||||
[itemsProperty.setNative](value: SegmentedBarItem[]) {
|
||||
const segmentedControl = this.ios;
|
||||
segmentedControl.removeAllSegments();
|
||||
const newItems = value;
|
||||
|
||||
if (newItems && newItems.length) {
|
||||
newItems.forEach((item, index, arr) => {
|
||||
let title = item.title;
|
||||
title = (title === null || title === undefined) ? "" : title;
|
||||
segmentedControl.insertSegmentWithTitleAtIndexAnimated(title, index, false);
|
||||
});
|
||||
}
|
||||
if (newItems && newItems.length) {
|
||||
newItems.forEach((item, index, arr) => {
|
||||
let title = item.title;
|
||||
title = title === null || title === undefined ? "" : title;
|
||||
segmentedControl.insertSegmentWithTitleAtIndexAnimated(
|
||||
title,
|
||||
index,
|
||||
false
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
selectedIndexProperty.coerce(this);
|
||||
}
|
||||
selectedIndexProperty.coerce(this);
|
||||
}
|
||||
|
||||
[selectedBackgroundColorProperty.getDefault](): UIColor {
|
||||
const currentOsVersion = iosUtils.MajorVersion;
|
||||
[selectedBackgroundColorProperty.getDefault](): UIColor {
|
||||
const currentOsVersion = iosUtils.MajorVersion;
|
||||
|
||||
return currentOsVersion < 13 ? this.ios.tintColor : this.ios.selectedSegmentTintColor;
|
||||
}
|
||||
[selectedBackgroundColorProperty.setNative](value: UIColor | Color) {
|
||||
const currentOsVersion = iosUtils.MajorVersion;
|
||||
let color = value instanceof Color ? value.ios : value;
|
||||
if (currentOsVersion < 13) {
|
||||
this.ios.tintColor = color;
|
||||
} else {
|
||||
this.ios.selectedSegmentTintColor = color;
|
||||
}
|
||||
|
||||
}
|
||||
return currentOsVersion < 13
|
||||
? this.ios.tintColor
|
||||
: this.ios.selectedSegmentTintColor;
|
||||
}
|
||||
[selectedBackgroundColorProperty.setNative](value: UIColor | Color) {
|
||||
const currentOsVersion = iosUtils.MajorVersion;
|
||||
let color = value instanceof Color ? value.ios : value;
|
||||
if (currentOsVersion < 13) {
|
||||
this.ios.tintColor = color;
|
||||
} else {
|
||||
this.ios.selectedSegmentTintColor = color;
|
||||
}
|
||||
}
|
||||
|
||||
[colorProperty.getDefault](): UIColor {
|
||||
return null;
|
||||
}
|
||||
[colorProperty.setNative](value: Color | UIColor) {
|
||||
let color = value instanceof Color ? value.ios : value;
|
||||
let bar = this.ios;
|
||||
let currentAttrs = bar.titleTextAttributesForState(UIControlState.Normal);
|
||||
let attrs = currentAttrs ? currentAttrs.mutableCopy() : NSMutableDictionary.new();
|
||||
attrs.setValueForKey(color, NSForegroundColorAttributeName);
|
||||
bar.setTitleTextAttributesForState(attrs, UIControlState.Normal);
|
||||
}
|
||||
[colorProperty.getDefault](): UIColor {
|
||||
return null;
|
||||
}
|
||||
[colorProperty.setNative](value: Color | UIColor) {
|
||||
let color = value instanceof Color ? value.ios : value;
|
||||
let bar = this.ios;
|
||||
let currentAttrs = bar.titleTextAttributesForState(
|
||||
UIControlState.Normal
|
||||
);
|
||||
let attrs = currentAttrs
|
||||
? currentAttrs.mutableCopy()
|
||||
: NSMutableDictionary.new();
|
||||
attrs.setValueForKey(color, NSForegroundColorAttributeName);
|
||||
bar.setTitleTextAttributesForState(attrs, UIControlState.Normal);
|
||||
}
|
||||
|
||||
[fontInternalProperty.getDefault](): Font {
|
||||
return null;
|
||||
}
|
||||
[fontInternalProperty.setNative](value: Font) {
|
||||
let font: UIFont = value ? value.getUIFont(UIFont.systemFontOfSize(UIFont.labelFontSize)) : null;
|
||||
let bar = this.ios;
|
||||
let currentAttrs = bar.titleTextAttributesForState(UIControlState.Normal);
|
||||
let attrs = currentAttrs ? currentAttrs.mutableCopy() : NSMutableDictionary.new();
|
||||
attrs.setValueForKey(font, NSFontAttributeName);
|
||||
bar.setTitleTextAttributesForState(attrs, UIControlState.Normal);
|
||||
}
|
||||
[fontInternalProperty.getDefault](): Font {
|
||||
return null;
|
||||
}
|
||||
[fontInternalProperty.setNative](value: Font) {
|
||||
let font: UIFont = value
|
||||
? value.getUIFont(UIFont.systemFontOfSize(UIFont.labelFontSize))
|
||||
: null;
|
||||
let bar = this.ios;
|
||||
let currentAttrs = bar.titleTextAttributesForState(
|
||||
UIControlState.Normal
|
||||
);
|
||||
let attrs = currentAttrs
|
||||
? currentAttrs.mutableCopy()
|
||||
: NSMutableDictionary.new();
|
||||
attrs.setValueForKey(font, NSFontAttributeName);
|
||||
bar.setTitleTextAttributesForState(attrs, UIControlState.Normal);
|
||||
}
|
||||
}
|
||||
|
||||
class SelectionHandlerImpl extends NSObject {
|
||||
private _owner: WeakRef<SegmentedBar>;
|
||||
|
||||
private _owner: WeakRef<SegmentedBar>;
|
||||
public static initWithOwner(
|
||||
owner: WeakRef<SegmentedBar>
|
||||
): SelectionHandlerImpl {
|
||||
let handler = <SelectionHandlerImpl>SelectionHandlerImpl.new();
|
||||
handler._owner = owner;
|
||||
|
||||
public static initWithOwner(owner: WeakRef<SegmentedBar>): SelectionHandlerImpl {
|
||||
let handler = <SelectionHandlerImpl>SelectionHandlerImpl.new();
|
||||
handler._owner = owner;
|
||||
return handler;
|
||||
}
|
||||
|
||||
return handler;
|
||||
}
|
||||
public selected(sender: UISegmentedControl) {
|
||||
let owner = this._owner.get();
|
||||
if (owner) {
|
||||
owner.selectedIndex = sender.selectedSegmentIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public selected(sender: UISegmentedControl) {
|
||||
let owner = this._owner.get();
|
||||
if (owner) {
|
||||
owner.selectedIndex = sender.selectedSegmentIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public static ObjCExposedMethods = {
|
||||
"selected": { returns: interop.types.void, params: [UISegmentedControl] }
|
||||
};
|
||||
public static ObjCExposedMethods = {
|
||||
selected: { returns: interop.types.void, params: [UISegmentedControl] },
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user