-
Notifications
You must be signed in to change notification settings - Fork 68
/
View.kt
228 lines (190 loc) · 8.36 KB
/
View.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (c) 2021. Dylan Cai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("unused")
package com.dylanc.longan
import android.content.res.TypedArray
import android.graphics.Outline
import android.graphics.Rect
import android.util.AttributeSet
import android.view.*
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes
import androidx.annotation.StyleableRes
import androidx.core.content.withStyledAttributes
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
private var View.lastClickTime: Long? by viewTags(R.id.tag_last_click_time)
var debouncingClickIntervals = 500
@Deprecated("Use doOnDebouncingClick instead", ReplaceWith("doOnDebouncingClick(clickIntervals, isSharingIntervals, block)"))
fun List<View>.doOnClick(clickIntervals: Int, isSharingIntervals: Boolean = false, block: () -> Unit) =
doOnDebouncingClick(clickIntervals, isSharingIntervals, block)
@Deprecated("Use doOnDebouncingClick instead", ReplaceWith("doOnDebouncingClick(clickIntervals, isSharingIntervals, block)"))
fun View.doOnClick(clickIntervals: Int, isSharingIntervals: Boolean = false, block: () -> Unit) =
doOnDebouncingClick(clickIntervals, isSharingIntervals, block)
fun List<View>.doOnDebouncingClick(clickIntervals: Int = debouncingClickIntervals, isSharingIntervals: Boolean = false, block: () -> Unit) =
forEach { it.doOnDebouncingClick(clickIntervals, isSharingIntervals, block) }
fun View.doOnDebouncingClick(clickIntervals: Int = debouncingClickIntervals, isSharingIntervals: Boolean = false, block: () -> Unit) =
setOnClickListener {
val view = if (isSharingIntervals) context.asActivity()?.window?.decorView ?: this else this
val currentTime = System.currentTimeMillis()
if (currentTime - (view.lastClickTime ?: 0L) > clickIntervals) {
view.lastClickTime = currentTime
block()
}
}
inline fun List<View>.doOnClick(crossinline block: () -> Unit) = forEach { it.doOnClick(block) }
inline fun View.doOnClick(crossinline block: () -> Unit) = setOnClickListener { block() }
@Deprecated("Use doOnDebouncingLongClick instead", ReplaceWith("doOnDebouncingLongClick(clickIntervals, isSharingIntervals, block)"))
fun List<View>.doOnLongClick(clickIntervals: Int, isSharingIntervals: Boolean = false, block: () -> Unit) =
doOnDebouncingLongClick(clickIntervals, isSharingIntervals, block)
@Deprecated("Use doOnDebouncingLongClick instead", ReplaceWith("doOnDebouncingLongClick(clickIntervals, isSharingIntervals, block)"))
fun View.doOnLongClick(clickIntervals: Int, isSharingIntervals: Boolean = false, block: () -> Unit) =
doOnDebouncingLongClick(clickIntervals, isSharingIntervals, block)
fun List<View>.doOnDebouncingLongClick(clickIntervals: Int = debouncingClickIntervals, isSharingIntervals: Boolean = false, block: () -> Unit) =
forEach { it.doOnDebouncingLongClick(clickIntervals, isSharingIntervals, block) }
fun View.doOnDebouncingLongClick(clickIntervals: Int = debouncingClickIntervals, isSharingIntervals: Boolean = false, block: () -> Unit) =
doOnLongClick {
val view = if (isSharingIntervals) context.asActivity()?.window?.decorView ?: this else this
val currentTime = System.currentTimeMillis()
if (currentTime - (view.lastClickTime ?: 0L) > clickIntervals) {
view.lastClickTime = currentTime
block()
}
}
inline fun List<View>.doOnLongClick(crossinline block: () -> Unit) = forEach { it.doOnLongClick(block) }
inline fun View.doOnLongClick(crossinline block: () -> Unit) =
setOnLongClickListener {
block()
true
}
fun View.expandClickArea(expandSize: Float) = expandClickArea(expandSize.toInt())
fun View.expandClickArea(expandSize: Int) =
expandClickArea(expandSize, expandSize, expandSize, expandSize)
fun View.expandClickArea(top: Float, left: Float, right: Float, bottom: Float) =
expandClickArea(top.toInt(), left.toInt(), right.toInt(), bottom.toInt())
fun View.expandClickArea(top: Int, left: Int, right: Int, bottom: Int) {
val parent = parent as? ViewGroup ?: return
parent.post {
val rect = Rect()
getHitRect(rect)
rect.top -= top
rect.left -= left
rect.right += right
rect.bottom += bottom
val touchDelegate = parent.touchDelegate
if (touchDelegate == null || touchDelegate !is MultiTouchDelegate) {
parent.touchDelegate = MultiTouchDelegate(rect, this)
} else {
touchDelegate.put(rect, this)
}
}
}
var View.roundCorners: Float
@Deprecated(NO_GETTER, level = DeprecationLevel.ERROR)
get() = noGetter()
set(value) {
clipToOutline = true
outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
outline.setRoundRect(0, 0, view.width, view.height, value)
}
}
}
fun View?.isTouchedAt(x: Float, y: Float): Boolean =
isTouchedAt(x.toInt(), y.toInt())
fun View?.isTouchedAt(x: Int, y: Int): Boolean =
this?.locationOnScreen?.contains(x, y) == true
fun View.findTouchedChild(x: Float, y: Float): View? =
findTouchedChild(x.toInt(), y.toInt())
fun View.findTouchedChild(x: Int, y: Int): View? =
touchables.find { it.isTouchedAt(x, y) }
/**
* Computes the coordinates of this view on the screen.
*/
inline val View.locationOnScreen: Rect
get() = IntArray(2).let {
getLocationOnScreen(it)
Rect(it[0], it[1], it[0] + width, it[1] + height)
}
@Deprecated(
"Replace with new api",
replaceWith = ReplaceWith("withStyledAttributes(set, attrs, defStyleAttr, defStyleRes, block)")
)
inline fun View.withStyledAttrs(
set: AttributeSet?,
@StyleableRes attrs: IntArray,
@AttrRes defStyleAttr: Int = 0,
@StyleRes defStyleRes: Int = 0,
block: TypedArray.() -> Unit
) =
context.withStyledAttributes(set, attrs, defStyleAttr, defStyleRes, block)
inline fun View.withStyledAttributes(
set: AttributeSet?,
@StyleableRes attrs: IntArray,
@AttrRes defStyleAttr: Int = 0,
@StyleRes defStyleRes: Int = 0,
block: TypedArray.() -> Unit
) =
context.withStyledAttributes(set, attrs, defStyleAttr, defStyleRes, block)
val View.rootWindowInsetsCompat: WindowInsetsCompat? by viewTags(R.id.tag_root_window_insets) {
ViewCompat.getRootWindowInsets(this)
}
val View.windowInsetsControllerCompat: WindowInsetsControllerCompat? by viewTags(R.id.tag_window_insets_controller) {
ViewCompat.getWindowInsetsController(this)
}
fun View.doOnApplyWindowInsets(action: (View, WindowInsetsCompat) -> WindowInsetsCompat) =
ViewCompat.setOnApplyWindowInsetsListener(this, action)
fun <T> viewTags(key: Int) = object : ReadWriteProperty<View, T?> {
@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: View, property: KProperty<*>) =
thisRef.getTag(key) as? T
override fun setValue(thisRef: View, property: KProperty<*>, value: T?) =
thisRef.setTag(key, value)
}
@Suppress("UNCHECKED_CAST")
fun <T> viewTags(key: Int, block: View.() -> T) = ReadOnlyProperty<View, T> { thisRef, _ ->
if (thisRef.getTag(key) == null) {
thisRef.setTag(key, block(thisRef))
}
thisRef.getTag(key) as T
}
class MultiTouchDelegate(bound: Rect, delegateView: View) : TouchDelegate(bound, delegateView) {
private val map = mutableMapOf<View, Pair<Rect, TouchDelegate>>()
private var targetDelegate: TouchDelegate? = null
init {
put(bound, delegateView)
}
fun put(bound: Rect, delegateView: View) {
map[delegateView] = bound to TouchDelegate(bound, delegateView)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
val x = event.x.toInt()
val y = event.y.toInt()
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
targetDelegate = map.entries.find { it.value.first.contains(x, y) }?.value?.second
}
MotionEvent.ACTION_CANCEL -> {
targetDelegate = null
}
}
return targetDelegate?.onTouchEvent(event) ?: false
}
}