Skip to content

Commit 1e4cb88

Browse files
committed
Improve readability of inflateAppMenu logic
We use two `return` statements to clearly distinguish between the classic and MVC UI designs. The function attempts to hook the legacy `prepareMenu` method first, falling back to the MVC `buildMenuModelList` logic if the former is unavailable.
1 parent b380555 commit 1e4cb88

File tree

1 file changed

+69
-67
lines changed

1 file changed

+69
-67
lines changed

app/src/main/java/org/matrix/chromext/hook/PageMenu.kt

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ object PageMenuHook : BaseHook() {
180180
isInit = true
181181
}
182182

183-
fun inflateAppMenu(tabbedAppMenuPropertiesDelegate: Class<*>) {
183+
fun inflateAppMenu(tabbedAppMenuPropertiesDelegate: Class<*>): Unhook {
184184
val proxy = PageMenuProxy
185185
val appMenuPropertiesDelegateImpl = tabbedAppMenuPropertiesDelegate.superclass as Class<*>
186186
// Can be found by searching `Android.PrepareMenu`
@@ -221,81 +221,85 @@ object PageMenuHook : BaseHook() {
221221
}
222222
// public void prepareMenu(Menu menu, AppMenuHandler handler)
223223

224-
prepareMenu?.hookAfter prepare@{
225-
val tabProvider = mActivityTabProvider.get(it.thisObject)!!
226-
Chrome.updateTab(tabProvider.invokeMethod { name == "get" })
227-
val ctx = mContext.get(it.thisObject) as Context
228-
Resource.enrich(ctx)
229-
230-
val menu = it.args[0] as Menu
231-
val url = getUrl()
232-
233-
val iconRowMenu = menu.getItem(0)
234-
if (iconRowMenu.hasSubMenu() && !Chrome.isBrave) {
235-
val infoMenu = iconRowMenu.getSubMenu()!!.getItem(3)
236-
infoMenu.setIcon(R.drawable.ic_book)
237-
infoMenu.setEnabled(true)
238-
val mId = infoMenu::class.java.getDeclaredField("mId")
239-
mId.setAccessible(true)
240-
mId.set(infoMenu, readerMode.ID)
241-
mId.setAccessible(false)
242-
}
224+
if (prepareMenu != null)
225+
return prepareMenu.hookAfter prepare@{
226+
val tabProvider = mActivityTabProvider.get(it.thisObject)!!
227+
Chrome.updateTab(tabProvider.invokeMethod { name == "get" })
228+
val ctx = mContext.get(it.thisObject) as Context
229+
Resource.enrich(ctx)
243230

244-
val mItems = menu::class.java.getDeclaredField("mItems").also { it.setAccessible(true) }
231+
val menu = it.args[0] as Menu
232+
val url = getUrl()
245233

246-
@Suppress("UNCHECKED_CAST") val items = mItems.get(menu) as ArrayList<MenuItem>
234+
val iconRowMenu = menu.getItem(0)
235+
if (iconRowMenu.hasSubMenu() && !Chrome.isBrave) {
236+
val infoMenu = iconRowMenu.getSubMenu()!!.getItem(3)
237+
infoMenu.setIcon(R.drawable.ic_book)
238+
infoMenu.setEnabled(true)
239+
val mId = infoMenu::class.java.getDeclaredField("mId")
240+
mId.setAccessible(true)
241+
mId.set(infoMenu, readerMode.ID)
242+
mId.setAccessible(false)
243+
}
247244

248-
val skip = items.filter { it.isVisible() }.size <= 10 || isChromeScheme(url)
249-
// Inflate only for the main_menu, which has more than visible 10 items at least
245+
val mItems = menu::class.java.getDeclaredField("mItems").also { it.setAccessible(true) }
250246

251-
if (skip && !isUserScript(url)) return@prepare
252-
MenuInflater(ctx).inflate(R.menu.main_menu, menu)
247+
@Suppress("UNCHECKED_CAST") val items = mItems.get(menu) as ArrayList<MenuItem>
253248

254-
// Show items with indices in main_menu.xml
255-
val toShow = mutableListOf<Int>(1) // Reversed index in main_menu
249+
val skip = items.filter { it.isVisible() }.size <= 10 || isChromeScheme(url)
250+
// Inflate only for the main_menu, which has more than visible 10 items at least
256251

257-
if (isDevToolsFrontEnd(url)) {
258-
toShow.clear()
259-
}
252+
if (skip && !isUserScript(url)) return@prepare
253+
MenuInflater(ctx).inflate(R.menu.main_menu, menu)
260254

261-
if (isUserScript(url)) {
262-
toShow.clear()
263-
toShow.add(2)
264-
if (skip) {
265-
// Show this menu for local preview pages (Custom Tab) of UserScripts
266-
items.find { it.itemId == R.id.install_script_id }?.setVisible(true)
267-
return@prepare
268-
}
269-
}
255+
// Show items with indices in main_menu.xml
256+
val toShow = mutableListOf<Int>(1) // Reversed index in main_menu
270257

271-
if (isChromeXtFrontEnd(url)) {
272-
toShow.clear()
273-
toShow.addAll(listOf(3, 4))
274-
}
258+
if (isDevToolsFrontEnd(url)) {
259+
toShow.clear()
260+
}
275261

276-
if (!Chrome.isVivaldi &&
277-
ctx.resources.configuration.smallestScreenWidthDp >= DisplayMetrics.DENSITY_XXHIGH &&
278-
toShow.size == 1 &&
279-
toShow.first() == 1) {
280-
iconRowMenu.setVisible(true)
281-
}
262+
if (isUserScript(url)) {
263+
toShow.clear()
264+
toShow.add(2)
265+
if (skip) {
266+
// Show this menu for local preview pages (Custom Tab) of UserScripts
267+
items.find { it.itemId == R.id.install_script_id }?.setVisible(true)
268+
return@prepare
269+
}
270+
}
282271

283-
val position =
284-
items
285-
.withIndex()
286-
.filter {
287-
ctx.resources.getResourceName(it.value.getItemId()).endsWith("id/divider_line_id")
288-
}
289-
.map { it.index }[1]
272+
if (isChromeXtFrontEnd(url)) {
273+
toShow.clear()
274+
toShow.addAll(listOf(3, 4))
275+
}
290276

291-
toShow.forEach {
292-
val newMenuItem: MenuItem = items[items.size - it]
293-
newMenuItem.setVisible(true)
294-
items.add(position + 1, newMenuItem)
295-
}
296-
for (i in 0..3) items.removeLast()
297-
}
277+
if (!Chrome.isVivaldi &&
278+
ctx.resources.configuration.smallestScreenWidthDp >= DisplayMetrics.DENSITY_XXHIGH &&
279+
toShow.size == 1 &&
280+
toShow.first() == 1) {
281+
iconRowMenu.setVisible(true)
282+
}
298283

284+
val position =
285+
items
286+
.withIndex()
287+
.filter {
288+
ctx.resources
289+
.getResourceName(it.value.getItemId())
290+
.endsWith("id/divider_line_id")
291+
}
292+
.map { it.index }[1]
293+
294+
toShow.forEach {
295+
val newMenuItem: MenuItem = items[items.size - it]
296+
newMenuItem.setVisible(true)
297+
items.add(position + 1, newMenuItem)
298+
}
299+
for (i in 0..3) items.removeLast()
300+
}
301+
302+
// Inflate for MVC UI model
299303
val maybeAddDividerLine =
300304
findMethodOrNull(tabbedAppMenuPropertiesDelegate) {
301305
parameterTypes.size == 2 &&
@@ -305,8 +309,6 @@ object PageMenuHook : BaseHook() {
305309
}
306310
// private void maybeAddDividerLine(MVCListAdapter.ModelList modelList, @IdRes int id)
307311

308-
if (prepareMenu == null && maybeAddDividerLine == null) return
309-
310312
val buildModelForStandardMenuItem =
311313
findMethod(appMenuPropertiesDelegateImpl) {
312314
parameterTypes contentDeepEquals
@@ -340,7 +342,7 @@ object PageMenuHook : BaseHook() {
340342

341343
val mData = findField(proxy.propertyModel) { type == Map::class.java }
342344

343-
findMethod(tabbedAppMenuPropertiesDelegate) {
345+
return findMethod(tabbedAppMenuPropertiesDelegate) {
344346
parameterTypes.size == 0 && returnType == MVCListAdapter_ModelList
345347
}
346348
// public MVCListAdapter.ModelList buildMenuModelList()

0 commit comments

Comments
 (0)