FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(switch): add property for off state background color by MartoYankov · Pull Request #7138 · NativeScript/NativeScript · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (4) .xml  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions apps/app/ui-tests-app/css/progress-switch.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<Page>
<StackLayout>
<Progress value="42" color="red" backgroundColor="green" height="10" width="300" />

<Switch checked="true" color="red" backgroundColor="green" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" />
<Switch checked="false" color="red" backgroundColor="green" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" />

<Switch checked="true" color="red" backgroundColor="green" offBackgroundColor="purple" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" />
<Switch checked="false" color="red" backgroundColor="green" offBackgroundColor="purple" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" />

<Switch checked="true" color="red" offBackgroundColor="purple" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" />
<Switch checked="false" color="red" offBackgroundColor="purple" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" />

<Switch />
</StackLayout>
</Page>
17 changes: 15 additions & 2 deletions tns-core-modules/ui/switch/switch-common.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { Switch as SwitchDefinition } from ".";
import { View, Property, booleanConverter, CSSType } from "../core/view";
import { Color } from "../../color";

export * from "../core/view";

@CSSType("Switch")
export class SwitchBase extends View implements SwitchDefinition {
public checked: boolean;
public offBackgroundColor: Color;

_onCheckedPropertyChanged(newValue: boolean) {
//
}
}

SwitchBase.prototype.recycleNativeView = "auto";

export const checkedProperty = new Property<SwitchBase, boolean>({ name: "checked", defaultValue: false, valueConverter: booleanConverter });
checkedProperty.register(SwitchBase);
function onCheckedPropertyChanged(switchBase: SwitchBase, oldValue: boolean, newValue: boolean) {
switchBase._onCheckedPropertyChanged(newValue);
}

export const checkedProperty = new Property<SwitchBase, boolean>({ name: "checked", defaultValue: false, valueConverter: booleanConverter, valueChanged: onCheckedPropertyChanged });
checkedProperty.register(SwitchBase);

export const offBackgroundColorProperty = new Property<SwitchBase, Color>({ name: "offBackgroundColor", equalityComparer: Color.equals, valueConverter: (v) => new Color(v) });
offBackgroundColorProperty.register(SwitchBase);
35 changes: 30 additions & 5 deletions tns-core-modules/ui/switch/switch.android.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
SwitchBase, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty
SwitchBase, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty, offBackgroundColorProperty
} from "./switch-common";

export * from "./switch-common";
Expand Down Expand Up @@ -54,6 +54,24 @@ export class Switch extends SwitchBase {
super.disposeNativeView();
}

private setNativeBackgroundColor(value: string | number | Color) {
if (value instanceof Color) {
this.nativeViewProtected.getTrackDrawable().setColorFilter(value.android, android.graphics.PorterDuff.Mode.SRC_IN);
} else {
this.nativeViewProtected.getTrackDrawable().clearColorFilter();
}
}

_onCheckedPropertyChanged(newValue: boolean) {
if (this.offBackgroundColor) {
if (!newValue) {
this.setNativeBackgroundColor(this.offBackgroundColor);
} else {
this.setNativeBackgroundColor(this.backgroundColor);
}
}
}

[checkedProperty.getDefault](): boolean {
return false;
}
Expand All @@ -76,10 +94,8 @@ export class Switch extends SwitchBase {
return -1;
}
[backgroundColorProperty.setNative](value: number | Color) {
if (value instanceof Color) {
this.nativeViewProtected.getTrackDrawable().setColorFilter(value.android, android.graphics.PorterDuff.Mode.SRC_IN);
} else {
this.nativeViewProtected.getTrackDrawable().clearColorFilter();
if (!this.offBackgroundColor || this.checked) {
this.setNativeBackgroundColor(value);
}
}

Expand All @@ -89,4 +105,13 @@ export class Switch extends SwitchBase {
[backgroundInternalProperty.setNative](value: any) {
//
}

[offBackgroundColorProperty.getDefault](): number {
return -1;
}
[offBackgroundColorProperty.setNative](value: number | Color) {
if (!this.checked) {
this.setNativeBackgroundColor(value);
}
}
}
6 changes: 6 additions & 0 deletions tns-core-modules/ui/switch/switch.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/ /** */

import { View, Property } from "../core/view";
import { Color } from "../../color";

/**
* Represents a switch component.
Expand All @@ -24,6 +25,11 @@ export class Switch extends View {
* Gets or sets if a switch is checked or not.
*/
checked: boolean;

/**
* Gets or sets the background color of the Switch when it is turned off.
*/
offBackgroundColor: Color;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion tns-core-modules/ui/switch/switch.ios.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
SwitchBase, layout, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty
SwitchBase, layout, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty, offBackgroundColorProperty
} from "./switch-common";

export * from "./switch-common";
Expand Down Expand Up @@ -95,4 +95,15 @@ export class Switch extends SwitchBase {
[backgroundInternalProperty.setNative](value: any) {
//
}

[offBackgroundColorProperty.getDefault](): UIColor {
return this.nativeViewProtected.backgroundColor;
}
[offBackgroundColorProperty.setNative](value: Color | UIColor) {
const nativeValue = value instanceof Color ? value.ios : value;

this.nativeViewProtected.tintColor = nativeValue;
this.nativeViewProtected.backgroundColor = nativeValue;
this.nativeViewProtected.layer.cornerRadius = this.nativeViewProtected.frame.size.height / 2;
}
}

Back | FazBrowse Home | New Git URL