| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,16 @@ | |||
| 1 | 1 | <Page> | |
| 2 | 2 | <StackLayout> | |
| 3 | 3 | <Progress value="42" color="red" backgroundColor="green" height="10" width="300" /> | |
| 4 | + | ||
| 4 | 5 | <Switch checked="true" color="red" backgroundColor="green" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" /> | |
| 6 | + <Switch checked="false" color="red" backgroundColor="green" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" /> | ||
| 7 | + | ||
| 8 | + <Switch checked="true" color="red" backgroundColor="green" offBackgroundColor="purple" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" /> | ||
| 9 | + <Switch checked="false" color="red" backgroundColor="green" offBackgroundColor="purple" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" /> | ||
| 10 | + | ||
| 11 | + <Switch checked="true" color="red" offBackgroundColor="purple" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" /> | ||
| 12 | + <Switch checked="false" color="red" offBackgroundColor="purple" height="30" width="52" borderWidth="2" borderColor="blue" borderRadius="15" /> | ||
| 13 | + | ||
| 5 | 14 | <Switch /> | |
| 6 | 15 | </StackLayout> | |
| 7 | 16 | </Page> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,27 @@ | |||
| 1 | 1 | import { Switch as SwitchDefinition } from "."; | |
| 2 | 2 | import { View, Property, booleanConverter, CSSType } from "../core/view"; | |
| 3 | + import { Color } from "../../color"; | ||
| 3 | 4 | ||
| 4 | 5 | export * from "../core/view"; | |
| 5 | 6 | ||
| 6 | 7 | @CSSType("Switch") | |
| 7 | 8 | export class SwitchBase extends View implements SwitchDefinition { | |
| 8 | 9 | public checked: boolean; | |
| 10 | + public offBackgroundColor: Color; | ||
| 11 | + | ||
| 12 | + _onCheckedPropertyChanged(newValue: boolean) { | ||
| 13 | + // | ||
| 14 | + } | ||
| 9 | 15 | } | |
| 10 | 16 | ||
| 11 | 17 | SwitchBase.prototype.recycleNativeView = "auto"; | |
| 12 | 18 | ||
| 13 | - export const checkedProperty = new Property<SwitchBase, boolean>({ name: "checked", defaultValue: false, valueConverter: booleanConverter }); | ||
| 14 | - checkedProperty.register(SwitchBase); | ||
| 19 | + function onCheckedPropertyChanged(switchBase: SwitchBase, oldValue: boolean, newValue: boolean) { | ||
| 20 | + switchBase._onCheckedPropertyChanged(newValue); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + export const checkedProperty = new Property<SwitchBase, boolean>({ name: "checked", defaultValue: false, valueConverter: booleanConverter, valueChanged: onCheckedPropertyChanged }); | ||
| 24 | + checkedProperty.register(SwitchBase); | ||
| 25 | + | ||
| 26 | + export const offBackgroundColorProperty = new Property<SwitchBase, Color>({ name: "offBackgroundColor", equalityComparer: Color.equals, valueConverter: (v) => new Color(v) }); | ||
| 27 | + offBackgroundColorProperty.register(SwitchBase); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | import { | |
| 2 | - SwitchBase, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty | ||
| 2 | + SwitchBase, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty, offBackgroundColorProperty | ||
| 3 | 3 | } from "./switch-common"; | |
| 4 | 4 | ||
| 5 | 5 | export * from "./switch-common"; | |
@@ -54,6 +54,24 @@ export class Switch extends SwitchBase { | |||
| 54 | 54 | super.disposeNativeView(); | |
| 55 | 55 | } | |
| 56 | 56 | ||
| 57 | + private setNativeBackgroundColor(value: string | number | Color) { | ||
| 58 | + if (value instanceof Color) { | ||
| 59 | + this.nativeViewProtected.getTrackDrawable().setColorFilter(value.android, android.graphics.PorterDuff.Mode.SRC_IN); | ||
| 60 | + } else { | ||
| 61 | + this.nativeViewProtected.getTrackDrawable().clearColorFilter(); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + _onCheckedPropertyChanged(newValue: boolean) { | ||
| 66 | + if (this.offBackgroundColor) { | ||
| 67 | + if (!newValue) { | ||
| 68 | + this.setNativeBackgroundColor(this.offBackgroundColor); | ||
| 69 | + } else { | ||
| 70 | + this.setNativeBackgroundColor(this.backgroundColor); | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + | ||
| 57 | 75 | [checkedProperty.getDefault](): boolean { | |
| 58 | 76 | return false; | |
| 59 | 77 | } | |
@@ -76,10 +94,8 @@ export class Switch extends SwitchBase { | |||
| 76 | 94 | return -1; | |
| 77 | 95 | } | |
| 78 | 96 | [backgroundColorProperty.setNative](value: number | Color) { | |
| 79 | - if (value instanceof Color) { | ||
| 80 | - this.nativeViewProtected.getTrackDrawable().setColorFilter(value.android, android.graphics.PorterDuff.Mode.SRC_IN); | ||
| 81 | - } else { | ||
| 82 | - this.nativeViewProtected.getTrackDrawable().clearColorFilter(); | ||
| 97 | + if (!this.offBackgroundColor || this.checked) { | ||
| 98 | + this.setNativeBackgroundColor(value); | ||
| 83 | 99 | } | |
| 84 | 100 | } | |
| 85 | 101 | ||
@@ -89,4 +105,13 @@ export class Switch extends SwitchBase { | |||
| 89 | 105 | [backgroundInternalProperty.setNative](value: any) { | |
| 90 | 106 | // | |
| 91 | 107 | } | |
| 108 | + | ||
| 109 | + [offBackgroundColorProperty.getDefault](): number { | ||
| 110 | + return -1; | ||
| 111 | + } | ||
| 112 | + [offBackgroundColorProperty.setNative](value: number | Color) { | ||
| 113 | + if (!this.checked) { | ||
| 114 | + this.setNativeBackgroundColor(value); | ||
| 115 | + } | ||
| 116 | + } | ||
| 92 | 117 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | */ /** */ | |
| 5 | 5 | ||
| 6 | 6 | import { View, Property } from "../core/view"; | |
| 7 | + import { Color } from "../../color"; | ||
| 7 | 8 | ||
| 8 | 9 | /** | |
| 9 | 10 | * Represents a switch component. | |
@@ -24,6 +25,11 @@ export class Switch extends View { | |||
| 24 | 25 | * Gets or sets if a switch is checked or not. | |
| 25 | 26 | */ | |
| 26 | 27 | checked: boolean; | |
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * Gets or sets the background color of the Switch when it is turned off. | ||
| 31 | + */ | ||
| 32 | + offBackgroundColor: Color; | ||
| 27 | 33 | } | |
| 28 | 34 | ||
| 29 | 35 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | import { | |
| 2 | - SwitchBase, layout, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty | ||
| 2 | + SwitchBase, layout, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, checkedProperty, offBackgroundColorProperty | ||
| 3 | 3 | } from "./switch-common"; | |
| 4 | 4 | ||
| 5 | 5 | export * from "./switch-common"; | |
@@ -95,4 +95,15 @@ export class Switch extends SwitchBase { | |||
| 95 | 95 | [backgroundInternalProperty.setNative](value: any) { | |
| 96 | 96 | // | |
| 97 | 97 | } | |
| 98 | + | ||
| 99 | + [offBackgroundColorProperty.getDefault](): UIColor { | ||
| 100 | + return this.nativeViewProtected.backgroundColor; | ||
| 101 | + } | ||
| 102 | + [offBackgroundColorProperty.setNative](value: Color | UIColor) { | ||
| 103 | + const nativeValue = value instanceof Color ? value.ios : value; | ||
| 104 | + | ||
| 105 | + this.nativeViewProtected.tintColor = nativeValue; | ||
| 106 | + this.nativeViewProtected.backgroundColor = nativeValue; | ||
| 107 | + this.nativeViewProtected.layer.cornerRadius = this.nativeViewProtected.frame.size.height / 2; | ||
| 108 | + } | ||
| 98 | 109 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments