From adbba4dc747507e0ff8b479e38850528ea98e8cd Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Mon, 3 Jun 2024 14:01:18 +0200 Subject: [PATCH 1/6] feat(android): track color of the Ti.UI.Switch --- .../ti/modules/titanium/ui/SwitchProxy.java | 2 + .../titanium/ui/widget/TiUISwitch.java | 40 +++++++++++++++++-- apidoc/Titanium/UI/Switch.yml | 19 ++++++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java index a4389620c6a..a3aeeb14921 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java @@ -23,6 +23,8 @@ TiC.PROPERTY_COLOR, TiC.PROPERTY_FONT, TiC.PROPERTY_TEXT_ALIGN, + TiC.PROPERTY_TINT_COLOR, + TiC.PROPERTY_ACTIVE_TINT_COLOR, TiC.PROPERTY_VERTICAL_ALIGN }) public class SwitchProxy extends TiViewProxy diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java index cd4f66f0482..96fc99a63e9 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java @@ -7,13 +7,17 @@ package ti.modules.titanium.ui.widget; import android.app.Activity; +import android.content.res.ColorStateList; import android.view.View; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; + import androidx.appcompat.widget.AppCompatToggleButton; + import com.google.android.material.checkbox.MaterialCheckBox; import com.google.android.material.chip.Chip; import com.google.android.material.switchmaterial.SwitchMaterial; + import org.appcelerator.kroll.KrollDict; import org.appcelerator.kroll.KrollProxy; import org.appcelerator.kroll.common.Log; @@ -23,13 +27,14 @@ import org.appcelerator.titanium.util.TiConvert; import org.appcelerator.titanium.util.TiUIHelper; import org.appcelerator.titanium.view.TiUIView; + import ti.modules.titanium.ui.UIModule; public class TiUISwitch extends TiUIView implements OnCheckedChangeListener { private static final String TAG = "TiUISwitch"; - private View.OnLayoutChangeListener layoutListener; + private final View.OnLayoutChangeListener layoutListener; private boolean oldValue = false; public TiUISwitch(TiViewProxy proxy) @@ -37,7 +42,8 @@ public TiUISwitch(TiViewProxy proxy) super(proxy); Log.d(TAG, "Creating a switch", Log.DEBUG_MODE); - this.layoutListener = new View.OnLayoutChangeListener() { + this.layoutListener = new View.OnLayoutChangeListener() + { @Override public void onLayoutChange( View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) @@ -58,6 +64,34 @@ public void processProperties(KrollDict d) setStyle(TiConvert.toInt(d.get(TiC.PROPERTY_STYLE), UIModule.SWITCH_STYLE_SLIDER)); } + if (d.containsKeyAndNotNull(TiC.PROPERTY_TINT_COLOR) + || d.containsKeyAndNotNull(TiC.PROPERTY_ACTIVE_TINT_COLOR)) { + CompoundButton currentButton = (CompoundButton) getNativeView(); + if (currentButton instanceof SwitchMaterial) { + + int colActive = d.containsKeyAndNotNull(TiC.PROPERTY_ACTIVE_TINT_COLOR) + ? TiConvert.toColor(d, TiC.PROPERTY_ACTIVE_TINT_COLOR) + : TiConvert.toColor(d, TiC.PROPERTY_TINT_COLOR); + int colNormal = d.containsKeyAndNotNull(TiC.PROPERTY_TINT_COLOR) + ? TiConvert.toColor(d, TiC.PROPERTY_TINT_COLOR) + : TiConvert.toColor(d, TiC.PROPERTY_ACTIVE_TINT_COLOR); + + ColorStateList trackStates = new ColorStateList( + new int[][] { + new int[] { -android.R.attr.state_enabled }, + new int[] { android.R.attr.state_checked }, + new int[] {} + }, + new int[] { + colNormal, + colActive, + colNormal + } + ); + ((SwitchMaterial) currentButton).setTrackTintList(trackStates); + } + } + if (d.containsKey(TiC.PROPERTY_VALUE)) { oldValue = TiConvert.toBoolean(d, TiC.PROPERTY_VALUE); } @@ -151,7 +185,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP } else { super.propertyChanged(key, oldValue, newValue, proxy); } - } + } @Override public void onCheckedChanged(CompoundButton btn, boolean value) diff --git a/apidoc/Titanium/UI/Switch.yml b/apidoc/Titanium/UI/Switch.yml index 326e54165f7..fb74c425e02 100644 --- a/apidoc/Titanium/UI/Switch.yml +++ b/apidoc/Titanium/UI/Switch.yml @@ -118,10 +118,25 @@ properties: - name: tintColor summary: The color used to tint the outline of the switch when it is turned off. + description: | + The color used to tint the outline of the switch when it is turned off. + + Android: Track color of the Material Switch. type: [String, Titanium.UI.Color] default: undefined - platforms: [iphone, ipad, macos] - since: "3.3.0" + platforms: [android, iphone, ipad, macos] + since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0, macos: 3.3.0} + + - name: activeTintColor + summary: The color used to tint the track of the switch when it is turned on. + description: | + The color used to tint the track of the switch when it is turned on. + + Android: Active track color of the Material Switch. + type: [String, Titanium.UI.Color] + default: undefined + platforms: [android] + since: {android: 12.4.0} - name: onTintColor summary: The color used to tint the appearance of the switch when it is turned on. From 7880589a4422498fa76fd390b13d51a4ae8dd4ac Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Mon, 3 Jun 2024 14:05:52 +0200 Subject: [PATCH 2/6] docs --- apidoc/Titanium/UI/Switch.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apidoc/Titanium/UI/Switch.yml b/apidoc/Titanium/UI/Switch.yml index fb74c425e02..65135520449 100644 --- a/apidoc/Titanium/UI/Switch.yml +++ b/apidoc/Titanium/UI/Switch.yml @@ -124,8 +124,8 @@ properties: Android: Track color of the Material Switch. type: [String, Titanium.UI.Color] default: undefined - platforms: [android, iphone, ipad, macos] - since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0, macos: 3.3.0} + platforms: [android, iphone, ipad] + since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0} - name: activeTintColor summary: The color used to tint the track of the switch when it is turned on. From c5a195385a91c3803b353a9b77dd51a18431df4c Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Tue, 4 Jun 2024 14:23:36 +0200 Subject: [PATCH 3/6] thumb colors --- .../ti/modules/titanium/ui/SwitchProxy.java | 4 ++- .../titanium/ui/widget/TiUISwitch.java | 28 +++++++++++++++++++ .../java/org/appcelerator/titanium/TiC.java | 2 ++ apidoc/Titanium/UI/Switch.yml | 22 +++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java index a3aeeb14921..a96e30f2de2 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java @@ -25,7 +25,9 @@ TiC.PROPERTY_TEXT_ALIGN, TiC.PROPERTY_TINT_COLOR, TiC.PROPERTY_ACTIVE_TINT_COLOR, - TiC.PROPERTY_VERTICAL_ALIGN + TiC.PROPERTY_VERTICAL_ALIGN, + TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR, + TiC.EVENT_PROPERTY_THUMB_COLOR }) public class SwitchProxy extends TiViewProxy { diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java index 96fc99a63e9..b72f730193a 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java @@ -64,6 +64,34 @@ public void processProperties(KrollDict d) setStyle(TiConvert.toInt(d.get(TiC.PROPERTY_STYLE), UIModule.SWITCH_STYLE_SLIDER)); } + if (d.containsKeyAndNotNull(TiC.EVENT_PROPERTY_THUMB_COLOR) + || d.containsKeyAndNotNull(TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR)) { + CompoundButton currentButton = (CompoundButton) getNativeView(); + if (currentButton instanceof SwitchMaterial) { + + int colActive = d.containsKeyAndNotNull(TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR) + ? TiConvert.toColor(d, TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR) + : TiConvert.toColor(d, TiC.EVENT_PROPERTY_THUMB_COLOR); + int colNormal = d.containsKeyAndNotNull(TiC.EVENT_PROPERTY_THUMB_COLOR) + ? TiConvert.toColor(d, TiC.EVENT_PROPERTY_THUMB_COLOR) + : TiConvert.toColor(d, TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR); + + ColorStateList trackStates = new ColorStateList( + new int[][] { + new int[] { -android.R.attr.state_enabled }, + new int[] { android.R.attr.state_checked }, + new int[] {} + }, + new int[] { + colNormal, + colActive, + colNormal + } + ); + ((SwitchMaterial) currentButton).setThumbTintList(trackStates); + } + } + if (d.containsKeyAndNotNull(TiC.PROPERTY_TINT_COLOR) || d.containsKeyAndNotNull(TiC.PROPERTY_ACTIVE_TINT_COLOR)) { CompoundButton currentButton = (CompoundButton) getNativeView(); diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiC.java b/android/titanium/src/java/org/appcelerator/titanium/TiC.java index 58beebdde16..2cb05f66dd6 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiC.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiC.java @@ -142,6 +142,8 @@ public class TiC public static final String EVENT_PROPERTY_TAB = "tab"; public static final String EVENT_PROPERTY_THUMB_OFFSET = "thumbOffset"; public static final String EVENT_PROPERTY_THUMB_SIZE = "thumbSize"; + public static final String EVENT_PROPERTY_THUMB_COLOR = "thumbColor"; + public static final String EVENT_PROPERTY_ACTIVE_THUMB_COLOR = "activeThumbColor"; public static final String EVENT_PROPERTY_TYPE = "type"; public static final String EVENT_PROPERTY_VELOCITY = "velocity"; public static final String EVENT_PROPERTY_X = "x"; diff --git a/apidoc/Titanium/UI/Switch.yml b/apidoc/Titanium/UI/Switch.yml index 65135520449..7df9af141ad 100644 --- a/apidoc/Titanium/UI/Switch.yml +++ b/apidoc/Titanium/UI/Switch.yml @@ -138,6 +138,28 @@ properties: platforms: [android] since: {android: 12.4.0} + - name: activeThumbColor + summary: The color used to tint the thumb icon of the switch when it is turned on. + description: | + The color used to tint the thumb icon of the switch when it is turned on. + + Android: Active thumb color of the Material Switch. + type: [String, Titanium.UI.Color] + default: undefined + platforms: [android] + since: {android: 12.4.0} + + - name: thumbColor + summary: The color used to tint the thumb icon of the switch when it is turned off. + description: | + The color used to tint the thumb icon of the switch when it is turned off. + + Android: Inactive thumb color of the Material Switch. + type: [String, Titanium.UI.Color] + default: undefined + platforms: [android] + since: {android: 12.4.0} + - name: onTintColor summary: The color used to tint the appearance of the switch when it is turned on. type: [String, Titanium.UI.Color] From cfba7dbc0ae80eb00668297af2a8a7c420d0393c Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Sat, 15 Jun 2024 15:22:56 +0200 Subject: [PATCH 4/6] update --- .../ti/modules/titanium/ui/SwitchProxy.java | 6 ++--- .../titanium/ui/widget/TiUISwitch.java | 24 +++++++++---------- .../java/org/appcelerator/titanium/TiC.java | 5 ++-- apidoc/Titanium/UI/Switch.yml | 6 ++--- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java index a96e30f2de2..3bdcac73f38 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/SwitchProxy.java @@ -24,10 +24,10 @@ TiC.PROPERTY_FONT, TiC.PROPERTY_TEXT_ALIGN, TiC.PROPERTY_TINT_COLOR, - TiC.PROPERTY_ACTIVE_TINT_COLOR, + TiC.PROPERTY_ON_TINT_COLOR, TiC.PROPERTY_VERTICAL_ALIGN, - TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR, - TiC.EVENT_PROPERTY_THUMB_COLOR + TiC.PROPERTY_ON_THUMB_COLOR, + TiC.PROPERTY_THUMB_COLOR }) public class SwitchProxy extends TiViewProxy { diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java index b72f730193a..da3bb4a1112 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUISwitch.java @@ -64,17 +64,17 @@ public void processProperties(KrollDict d) setStyle(TiConvert.toInt(d.get(TiC.PROPERTY_STYLE), UIModule.SWITCH_STYLE_SLIDER)); } - if (d.containsKeyAndNotNull(TiC.EVENT_PROPERTY_THUMB_COLOR) - || d.containsKeyAndNotNull(TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR)) { + if (d.containsKeyAndNotNull(TiC.PROPERTY_THUMB_COLOR) + || d.containsKeyAndNotNull(TiC.PROPERTY_ON_THUMB_COLOR)) { CompoundButton currentButton = (CompoundButton) getNativeView(); if (currentButton instanceof SwitchMaterial) { - int colActive = d.containsKeyAndNotNull(TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR) - ? TiConvert.toColor(d, TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR) - : TiConvert.toColor(d, TiC.EVENT_PROPERTY_THUMB_COLOR); - int colNormal = d.containsKeyAndNotNull(TiC.EVENT_PROPERTY_THUMB_COLOR) - ? TiConvert.toColor(d, TiC.EVENT_PROPERTY_THUMB_COLOR) - : TiConvert.toColor(d, TiC.EVENT_PROPERTY_ACTIVE_THUMB_COLOR); + int colActive = d.containsKeyAndNotNull(TiC.PROPERTY_ON_THUMB_COLOR) + ? TiConvert.toColor(d, TiC.PROPERTY_ON_THUMB_COLOR) + : TiConvert.toColor(d, TiC.PROPERTY_THUMB_COLOR); + int colNormal = d.containsKeyAndNotNull(TiC.PROPERTY_THUMB_COLOR) + ? TiConvert.toColor(d, TiC.PROPERTY_THUMB_COLOR) + : TiConvert.toColor(d, TiC.PROPERTY_ON_THUMB_COLOR); ColorStateList trackStates = new ColorStateList( new int[][] { @@ -93,16 +93,16 @@ public void processProperties(KrollDict d) } if (d.containsKeyAndNotNull(TiC.PROPERTY_TINT_COLOR) - || d.containsKeyAndNotNull(TiC.PROPERTY_ACTIVE_TINT_COLOR)) { + || d.containsKeyAndNotNull(TiC.PROPERTY_ON_TINT_COLOR)) { CompoundButton currentButton = (CompoundButton) getNativeView(); if (currentButton instanceof SwitchMaterial) { - int colActive = d.containsKeyAndNotNull(TiC.PROPERTY_ACTIVE_TINT_COLOR) - ? TiConvert.toColor(d, TiC.PROPERTY_ACTIVE_TINT_COLOR) + int colActive = d.containsKeyAndNotNull(TiC.PROPERTY_ON_TINT_COLOR) + ? TiConvert.toColor(d, TiC.PROPERTY_ON_TINT_COLOR) : TiConvert.toColor(d, TiC.PROPERTY_TINT_COLOR); int colNormal = d.containsKeyAndNotNull(TiC.PROPERTY_TINT_COLOR) ? TiConvert.toColor(d, TiC.PROPERTY_TINT_COLOR) - : TiConvert.toColor(d, TiC.PROPERTY_ACTIVE_TINT_COLOR); + : TiConvert.toColor(d, TiC.PROPERTY_ON_TINT_COLOR); ColorStateList trackStates = new ColorStateList( new int[][] { diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiC.java b/android/titanium/src/java/org/appcelerator/titanium/TiC.java index 2cb05f66dd6..4f219b4c308 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiC.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiC.java @@ -142,8 +142,8 @@ public class TiC public static final String EVENT_PROPERTY_TAB = "tab"; public static final String EVENT_PROPERTY_THUMB_OFFSET = "thumbOffset"; public static final String EVENT_PROPERTY_THUMB_SIZE = "thumbSize"; - public static final String EVENT_PROPERTY_THUMB_COLOR = "thumbColor"; - public static final String EVENT_PROPERTY_ACTIVE_THUMB_COLOR = "activeThumbColor"; + public static final String PROPERTY_THUMB_COLOR = "thumbColor"; + public static final String PROPERTY_ON_THUMB_COLOR = "onThumbColor"; public static final String EVENT_PROPERTY_TYPE = "type"; public static final String EVENT_PROPERTY_VELOCITY = "velocity"; public static final String EVENT_PROPERTY_X = "x"; @@ -610,6 +610,7 @@ public class TiC public static final String PROPERTY_ON_RESTART = "onRestart"; public static final String PROPERTY_ON_PAUSE = "onPause"; public static final String PROPERTY_ON_STOP = "onStop"; + public static final String PROPERTY_ON_TINT_COLOR = "onTintColor"; public static final String PROPERTY_TLS_VERSION = "tlsVersion"; public static final String PROPERTY_ON_DESTROY = "onDestroy"; public static final String PROPERTY_ON_CREATE_WINDOW = "onCreateWindow"; diff --git a/apidoc/Titanium/UI/Switch.yml b/apidoc/Titanium/UI/Switch.yml index 7df9af141ad..e33c592ca21 100644 --- a/apidoc/Titanium/UI/Switch.yml +++ b/apidoc/Titanium/UI/Switch.yml @@ -127,7 +127,7 @@ properties: platforms: [android, iphone, ipad] since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0} - - name: activeTintColor + - name: onTintColor summary: The color used to tint the track of the switch when it is turned on. description: | The color used to tint the track of the switch when it is turned on. @@ -138,7 +138,7 @@ properties: platforms: [android] since: {android: 12.4.0} - - name: activeThumbColor + - name: onThumbColor summary: The color used to tint the thumb icon of the switch when it is turned on. description: | The color used to tint the thumb icon of the switch when it is turned on. @@ -165,7 +165,7 @@ properties: type: [String, Titanium.UI.Color] default: undefined platforms: [iphone, ipad, macos] - since: "3.3.0" + since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0} - name: thumbTintColor summary: The color used to tint the appearance of the thumb. From cc1c8d47ee465c2cf9434c4af87c2c4c3e0455fb Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Sat, 15 Jun 2024 15:26:39 +0200 Subject: [PATCH 5/6] docs --- apidoc/Titanium/UI/Switch.yml | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/apidoc/Titanium/UI/Switch.yml b/apidoc/Titanium/UI/Switch.yml index e33c592ca21..be63ca200a6 100644 --- a/apidoc/Titanium/UI/Switch.yml +++ b/apidoc/Titanium/UI/Switch.yml @@ -127,17 +127,6 @@ properties: platforms: [android, iphone, ipad] since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0} - - name: onTintColor - summary: The color used to tint the track of the switch when it is turned on. - description: | - The color used to tint the track of the switch when it is turned on. - - Android: Active track color of the Material Switch. - type: [String, Titanium.UI.Color] - default: undefined - platforms: [android] - since: {android: 12.4.0} - - name: onThumbColor summary: The color used to tint the thumb icon of the switch when it is turned on. description: | @@ -164,7 +153,7 @@ properties: summary: The color used to tint the appearance of the switch when it is turned on. type: [String, Titanium.UI.Color] default: undefined - platforms: [iphone, ipad, macos] + platforms: [android, iphone, ipad, macos] since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0} - name: thumbTintColor From ef5b82b0ccc00ce58b1eea152d77f7cda66bbb6f Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Sat, 15 Jun 2024 16:22:42 +0200 Subject: [PATCH 6/6] doc update --- apidoc/Titanium/UI/Switch.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/apidoc/Titanium/UI/Switch.yml b/apidoc/Titanium/UI/Switch.yml index be63ca200a6..4ff5536acd5 100644 --- a/apidoc/Titanium/UI/Switch.yml +++ b/apidoc/Titanium/UI/Switch.yml @@ -128,7 +128,6 @@ properties: since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0} - name: onThumbColor - summary: The color used to tint the thumb icon of the switch when it is turned on. description: | The color used to tint the thumb icon of the switch when it is turned on. @@ -139,7 +138,6 @@ properties: since: {android: 12.4.0} - name: thumbColor - summary: The color used to tint the thumb icon of the switch when it is turned off. description: | The color used to tint the thumb icon of the switch when it is turned off.