mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 11:01:21 +08:00

* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
133 lines
4.5 KiB
TypeScript
133 lines
4.5 KiB
TypeScript
import { Font } from "../styling/font";
|
|
import {
|
|
SegmentedBarItemBase, SegmentedBarBase, selectedIndexProperty, itemsProperty, selectedBackgroundColorProperty,
|
|
colorProperty, fontInternalProperty, Color
|
|
} from "./segmented-bar-common";
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
export class SegmentedBar extends SegmentedBarBase {
|
|
nativeViewProtected: UISegmentedControl;
|
|
private _selectionHandler: NSObject;
|
|
|
|
createNativeView() {
|
|
return UISegmentedControl.new();
|
|
}
|
|
|
|
initNativeView() {
|
|
super.initNativeView();
|
|
this._selectionHandler = SelectionHandlerImpl.initWithOwner(new WeakRef(this));
|
|
this.nativeViewProtected.addTargetActionForControlEvents(this._selectionHandler, "selected", UIControlEvents.ValueChanged);
|
|
}
|
|
|
|
disposeNativeView() {
|
|
this._selectionHandler = null;
|
|
super.disposeNativeView();
|
|
}
|
|
|
|
get ios(): UISegmentedControl {
|
|
return this.nativeViewProtected;
|
|
}
|
|
|
|
[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;
|
|
|
|
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);
|
|
}
|
|
|
|
[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;
|
|
}
|
|
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|
|
|
|
class SelectionHandlerImpl extends NSObject {
|
|
|
|
private _owner: WeakRef<SegmentedBar>;
|
|
|
|
public static initWithOwner(owner: WeakRef<SegmentedBar>): SelectionHandlerImpl {
|
|
let handler = <SelectionHandlerImpl>SelectionHandlerImpl.new();
|
|
handler._owner = owner;
|
|
|
|
return handler;
|
|
}
|
|
|
|
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] }
|
|
};
|
|
}
|