-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathMenuView.kt
More file actions
356 lines (317 loc) · 11.1 KB
/
MenuView.kt
File metadata and controls
356 lines (317 loc) · 11.1 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package com.reactnativemenu
import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Rect
import android.os.Build
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.*
import android.widget.PopupMenu
import com.facebook.react.bridge.*
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.views.view.ReactViewGroup
import com.reactnativemenu.ThemeHelper.appcompat
import com.reactnativemenu.ThemeHelper.isNight
import com.reactnativemenu.ThemeHelper.material3
import java.lang.reflect.Field
class MenuView(private val mContext: ReactContext) : ReactViewGroup(mContext) {
private lateinit var mActions: ReadableArray
private var uiKit = "auto"
private var themeVariant = "system"
private var mIsAnchoredToRight = false
private var mPopupMenu: PopupMenu = PopupMenu(context, this)
private var mIsMenuDisplayed = false
private var mIsOnLongPress = false
private var mGestureDetector: GestureDetector
private var mHitSlopRect: Rect? = null
init {
mGestureDetector = GestureDetector(mContext, object : GestureDetector.SimpleOnGestureListener() {
override fun onLongPress(e: MotionEvent) {
if (!mIsOnLongPress) {
return
}
prepareMenu()
}
override fun onSingleTapUp(e: MotionEvent): Boolean {
if (!mIsOnLongPress) {
prepareMenu()
}
return true
}
})
}
fun show() {
prepareMenu()
}
override fun setHitSlopRect(rect: Rect?) {
super.setHitSlopRect(rect)
mHitSlopRect = rect
updateTouchDelegate()
}
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
return true
}
override fun onTouchEvent(ev: MotionEvent): Boolean {
mGestureDetector.onTouchEvent(ev)
return true
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
updateTouchDelegate()
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
updateTouchDelegate()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
if (mIsMenuDisplayed) {
mPopupMenu.dismiss()
}
}
fun setActions(actions: ReadableArray) {
mActions = actions
}
fun setIsAnchoredToRight(isAnchoredToRight: Boolean) {
if (mIsAnchoredToRight == isAnchoredToRight) {
return
}
mIsAnchoredToRight = isAnchoredToRight
}
fun setIsOpenOnLongPress(isLongPress: Boolean) {
mIsOnLongPress = isLongPress
}
fun setUiKit(uiKit: String?) {
this.uiKit = uiKit ?: "auto"
setStyle()
}
fun setThemeVariant(themeVariant: String?) {
this.themeVariant = themeVariant ?: "system"
setStyle()
}
private val getActionsCount: Int
get() = mActions.size()
private fun prepareMenuItem(menuItem: MenuItem, config: ReadableMap?) {
val titleColor = when (config != null && config.hasKey("titleColor") && !config.isNull("titleColor")) {
true -> config.getInt("titleColor")
else -> null
}
val imageName = when (config != null && config.hasKey("image") && !config.isNull("image")) {
true -> config.getString("image")
else -> null
}
val imageColor = when (config != null && config.hasKey("imageColor") && !config.isNull("imageColor")) {
true -> config.getInt("imageColor")
else -> null
}
val attributes = when (config != null && config.hasKey("attributes") && !config.isNull(("attributes"))) {
true -> config.getMap("attributes")
else -> null
}
val subactions = when (config != null && config.hasKey("subactions") && !config.isNull(("subactions"))) {
true -> config.getArray("subactions")
else -> null
}
val menuState = config?.getString("state")
if (titleColor != null) {
menuItem.title = getMenuItemTextWithColor(menuItem.title.toString(), titleColor)
}
if (imageName != null) {
val resourceId: Int = getDrawableIdWithName(imageName)
if (resourceId != 0) {
val icon = resources.getDrawable(resourceId, context.theme)
if (imageColor != null) {
icon.setTintList(ColorStateList.valueOf(imageColor))
}
menuItem.icon = icon
}
}
if (attributes != null) {
// actions.attributes.disabled
val disabled = when (attributes.hasKey("disabled") && !attributes.isNull("disabled")) {
true -> attributes.getBoolean("disabled")
else -> false
}
menuItem.isEnabled = !disabled
if (!menuItem.isEnabled) {
val disabledColor = 0x77888888
menuItem.title = getMenuItemTextWithColor(menuItem.title.toString(), disabledColor)
if (imageName != null) {
val icon = menuItem.icon
icon?.setTintList(ColorStateList.valueOf(disabledColor))
menuItem.icon = icon
}
}
// actions.attributes.hidden
val hidden = when (attributes.hasKey("hidden") && !attributes.isNull("hidden")) {
true -> attributes.getBoolean("hidden")
else -> false
}
menuItem.isVisible = !hidden
// actions.attributes.destructive
val destructive = when (attributes.hasKey("destructive") && !attributes.isNull("destructive")) {
true -> attributes.getBoolean("destructive")
else -> false
}
if (destructive) {
menuItem.title = getMenuItemTextWithColor(menuItem.title.toString(), Color.RED)
if (imageName != null) {
val icon = menuItem.icon
icon?.setTintList(ColorStateList.valueOf(Color.RED))
menuItem.icon = icon
}
}
}
// Handle menuState for checkable items
when (menuState) {
"on", "off" -> {
menuItem.isCheckable = true
menuItem.isChecked = menuState == "on"
}
else -> menuItem.isCheckable = false
}
// On Android SubMenu cannot contain another SubMenu, so even if there are subactions provided
// we are checking if item has submenu (which will occur only for 1 lvl nesting)
if (subactions != null && menuItem.hasSubMenu()) {
var i = 0
val subactionsCount = subactions.size()
while (i < subactionsCount) {
if (!subactions.isNull(i)) {
val subMenuConfig = subactions.getMap(i)
val subMenuItem = menuItem.subMenu?.add(Menu.NONE, Menu.NONE, i, subMenuConfig?.getString("title"))
if (subMenuItem != null) {
prepareMenuItem(subMenuItem, subMenuConfig)
subMenuItem.setOnMenuItemClickListener {
if (!it.hasSubMenu()) {
mIsMenuDisplayed = false
if (!subactions.isNull(it.order)) {
val selectedItem = subactions.getMap(it.order)
val dispatcher =
UIManagerHelper.getEventDispatcherForReactTag(mContext, id)
val surfaceId: Int = UIManagerHelper.getSurfaceId(this)
dispatcher?.dispatchEvent(
MenuOnPressActionEvent(surfaceId, id, selectedItem?.getString("id"), id)
)
}
true
} else {
false
}
}
}
}
i++
}
}
}
private fun prepareMenu() {
if (getActionsCount > 0) {
mPopupMenu.menu.clear()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mPopupMenu.gravity = when (mIsAnchoredToRight) {
true -> Gravity.RIGHT
false -> Gravity.LEFT
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
mPopupMenu.setForceShowIcon(true)
}
var i = 0
while (i < getActionsCount) {
if (!mActions.isNull(i)) {
val item = mActions.getMap(i)
val menuItem = when (item != null && item.hasKey("subactions") && !item.isNull("subactions")) {
true -> mPopupMenu.menu.addSubMenu(Menu.NONE, Menu.NONE, i, item.getString("title")).item
else -> mPopupMenu.menu.add(Menu.NONE, Menu.NONE, i, item?.getString("title"))
}
prepareMenuItem(menuItem, item)
menuItem.setOnMenuItemClickListener {
if (!it.hasSubMenu()) {
mIsMenuDisplayed = false
if (!mActions.isNull(it.order)) {
val selectedItem = mActions.getMap(it.order)
val dispatcher =
UIManagerHelper.getEventDispatcherForReactTag(mContext, id)
val surfaceId: Int = UIManagerHelper.getSurfaceId(this)
dispatcher?.dispatchEvent(
MenuOnPressActionEvent(surfaceId, id, selectedItem?.getString("id"), id)
)
}
true
} else {
false
}
}
}
i++
}
mPopupMenu.setOnDismissListener {
mIsMenuDisplayed = false
val dispatcher = UIManagerHelper.getEventDispatcherForReactTag(mContext, id)
val surfaceId: Int = UIManagerHelper.getSurfaceId(this)
dispatcher?.dispatchEvent(MenuOnCloseEvent(surfaceId, id, id))
}
mIsMenuDisplayed = true
val dispatcher = UIManagerHelper.getEventDispatcherForReactTag(mContext, id)
val surfaceId: Int = UIManagerHelper.getSurfaceId(this)
dispatcher?.dispatchEvent(MenuOnOpenEvent(surfaceId, id, id))
mPopupMenu.show()
}
}
private fun updateTouchDelegate() {
post {
val hitRect = Rect()
getHitRect(hitRect)
mHitSlopRect?.let {
hitRect.left -= it.left
hitRect.top -= it.top
hitRect.right += it.right
hitRect.bottom += it.bottom
}
(parent as? ViewGroup)?.let {
it.touchDelegate = TouchDelegate(hitRect, this)
}
}
}
private fun getDrawableIdWithName(name: String): Int {
val appResources: Resources = context.resources
var resourceId = appResources.getIdentifier(name, "drawable", context.packageName)
if (resourceId == 0) {
// If drawable is not present in app's resources, check system's resources
resourceId = getResId(name, android.R.drawable::class.java)
}
return resourceId
}
private fun getResId(resName: String?, c: Class<*>): Int {
return try {
val idField: Field = c.getDeclaredField(resName!!)
idField.getInt(idField)
} catch (e: Exception) {
e.printStackTrace()
0
}
}
private fun getMenuItemTextWithColor(text: String, color: Int): SpannableStringBuilder {
val textWithColor = SpannableStringBuilder()
textWithColor.append(text)
textWithColor.setSpan(ForegroundColorSpan(color),
0, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
return textWithColor
}
private fun setStyle() {
val dark = when (this.themeVariant) {
"dark" -> true
"light" -> false
else -> isNight(context)
}
val theme = when (this.uiKit) {
"material3" -> material3(dark).takeIf { it != 0 } ?: appcompat(dark)
"appcompat" -> appcompat(dark)
else -> material3(dark).takeIf { it != 0 } ?: appcompat(dark)
}
val themedCtx = ContextThemeWrapper(context, theme)
mPopupMenu = PopupMenu(themedCtx, this)
}
}