Skip to content

Commit

Permalink
Feat: Add shortcut key for menu items
Browse files Browse the repository at this point in the history
Fixes: #1054
  • Loading branch information
zhanghai committed Mar 4, 2024
1 parent df802b5 commit c91754b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package me.zhanghai.android.files.filelist
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.activity.result.contract.ActivityResultContract
import androidx.fragment.app.commit
Expand Down Expand Up @@ -35,6 +36,13 @@ class FileListActivity : AppActivity() {
}
}

override fun onKeyShortcut(keyCode: Int, event: KeyEvent): Boolean {
if (fragment.onKeyShortcut(keyCode, event)) {
return true
}
return super.onKeyUp(keyCode, event)
}

override fun onBackPressed() {
if (fragment.onBackPressed()) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.view.KeyCharacterMap
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
Expand Down Expand Up @@ -502,6 +504,28 @@ class FileListFragment : Fragment(), BreadcrumbLayout.Listener, FileListAdapter.
}
}

fun onKeyShortcut(keyCode: Int, event: KeyEvent): Boolean {
if (bottomActionMode.isActive) {
val menu = bottomActionMode.menu
menu.setQwertyMode(
KeyCharacterMap.load(event.deviceId).keyboardType != KeyCharacterMap.NUMERIC
)
if (menu.performShortcut(keyCode, event, 0)) {
return true
}
}
if (overlayActionMode.isActive) {
val menu = overlayActionMode.menu
menu.setQwertyMode(
KeyCharacterMap.load(event.deviceId).keyboardType != KeyCharacterMap.NUMERIC
)
if (menu.performShortcut(keyCode, event, 0)) {
return true
}
}
return false
}

fun onBackPressed(): Boolean {
val drawerLayout = binding.drawerLayout
if (drawerLayout != null && drawerLayout.isDrawerOpen(GravityCompat.START)) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/file_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,28 @@

<item
android:id="@+id/action_navigate_to"
android:alphabeticShortcut="l"
android:orderInCategory="100"
android:title="@string/file_list_action_navigate_to"
app:showAsAction="never" />

<item
android:id="@+id/action_refresh"
android:alphabeticShortcut="r"
android:orderInCategory="100"
android:title="@string/refresh"
app:showAsAction="never" />

<item
android:id="@+id/action_select_all"
android:alphabeticShortcut="a"
android:orderInCategory="100"
android:title="@string/select_all"
app:showAsAction="never" />

<item
android:id="@+id/action_show_hidden_files"
android:alphabeticShortcut="h"
android:orderInCategory="100"
android:title="@string/file_list_action_show_hidden_files"
android:checkable="true"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/menu/file_list_paste.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<item
android:id="@+id/action_paste"
android:alphabeticShortcut="v"
android:icon="@drawable/paste_icon_control_normal_24dp"
android:orderInCategory="100"
android:title="@string/paste"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/menu/file_list_pick.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<item
android:id="@+id/action_select_all"
android:alphabeticShortcut="a"
android:orderInCategory="100"
android:title="@string/select_all"
app:showAsAction="never" />
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/menu/file_list_select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

<item
android:id="@+id/action_cut"
android:alphabeticShortcut="x"
android:icon="@drawable/cut_icon_control_normal_24dp"
android:orderInCategory="100"
android:title="@string/cut"
app:showAsAction="always" />

<item
android:id="@+id/action_copy"
android:alphabeticShortcut="c"
android:icon="@drawable/copy_icon_control_normal_24dp"
android:orderInCategory="100"
android:title="@string/copy"
Expand All @@ -46,6 +48,7 @@

<item
android:id="@+id/action_select_all"
android:alphabeticShortcut="a"
android:orderInCategory="100"
android:title="@string/select_all"
app:showAsAction="never" />
Expand Down

0 comments on commit c91754b

Please sign in to comment.