@@ -7,6 +7,7 @@ import android.graphics.Shader.TileMode
77import android.util.AttributeSet
88import android.view.MotionEvent
99import android.view.View
10+ import android.view.ViewConfiguration
1011import androidx.core.content.ContextCompat
1112import com.therealbluepandabear.pixapencil.R
1213
@@ -95,6 +96,10 @@ class ColorSwitcherView(context: Context, attributeSet: AttributeSet) : View(con
9596
9697 private var onColorPickerTapped: () -> Unit = { }
9798
99+ private var onPrimaryColorLongTapped: () -> Unit = { }
100+
101+ private var onSecondaryColorLongTapped: () -> Unit = { }
102+
98103 private fun toDp (px : Int ): Float {
99104 return px * resources.displayMetrics.density
100105 }
@@ -117,6 +122,14 @@ class ColorSwitcherView(context: Context, attributeSet: AttributeSet) : View(con
117122 this .onColorPickerTapped = onColorPickerTapped
118123 }
119124
125+ fun setOnPrimaryColorLongTapped (onPrimaryColorLongTapped : () -> Unit ) {
126+ this .onPrimaryColorLongTapped = onPrimaryColorLongTapped
127+ }
128+
129+ fun setOnSecondaryColorLongTapped (onSecondaryColorLongTapped : () -> Unit ) {
130+ this .onSecondaryColorLongTapped = onSecondaryColorLongTapped
131+ }
132+
120133 fun setIsPrimarySelected (isPrimarySelected : Boolean ) {
121134 this .isPrimarySelected = isPrimarySelected
122135 invalidate()
@@ -305,6 +318,14 @@ class ColorSwitcherView(context: Context, attributeSet: AttributeSet) : View(con
305318 }
306319 }
307320
321+ private val longPressedRunnable = Runnable {
322+ if (isPrimarySelected) {
323+ onPrimaryColorLongTapped.invoke()
324+ } else {
325+ onSecondaryColorLongTapped.invoke()
326+ }
327+ }
328+
308329 @SuppressLint(" ClickableViewAccessibility" ) /* * I will un-suppress in future commits **/
309330 override fun onTouchEvent (event : MotionEvent ): Boolean {
310331 val xOrY = if (! orientationLandscape) {
@@ -319,18 +340,34 @@ class ColorSwitcherView(context: Context, attributeSet: AttributeSet) : View(con
319340 event.x
320341 }
321342
322- if ((xOrY in toDp(50 ).. toDp(100 ) - insetStroke) && isPrimarySelected
343+ if ((xOrY in toDp(50 ).. toDp(100 ) - insetStroke)
323344 && inverse !in toDp(50 ).. toDp(70 )) {
324- isPrimarySelected = false
325- invalidate()
326- } else if ((xOrY !in toDp(50 ).. toDp(100 ) - insetStroke) && ! isPrimarySelected
345+ // secondary color has been selected
346+ handler.postDelayed(longPressedRunnable, ViewConfiguration .getLongPressTimeout().toLong())
347+
348+ if (isPrimarySelected) {
349+ isPrimarySelected = false
350+ invalidate()
351+ }
352+ } else if ((xOrY !in toDp(50 ).. toDp(100 ) - insetStroke)
327353 && inverse !in toDp(50 ).. toDp(70 )) {
328- isPrimarySelected = true
329- invalidate()
354+ // primary color has been selected
355+ handler.postDelayed(longPressedRunnable, ViewConfiguration .getLongPressTimeout().toLong())
356+
357+ if (! isPrimarySelected) {
358+ isPrimarySelected = true
359+ invalidate()
360+ }
330361 } else if ((inverse in toDp(55 ).. toDp(70 ))) {
362+ // color picker has been selected
363+
331364 onColorPickerTapped.invoke()
332365 }
333366
367+ if (event.action == MotionEvent .ACTION_UP ) {
368+ handler.removeCallbacks(longPressedRunnable)
369+ }
370+
334371 return true
335372 }
336373}
0 commit comments