Add some keyboard shortcuts

This commit is contained in:
William Brawner 2020-08-23 05:02:36 -07:00
parent a1e114b162
commit c16ed3cbbe
2 changed files with 64 additions and 52 deletions

View file

@ -9,12 +9,11 @@ import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.*
import android.webkit.MimeTypeMap
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
@ -56,6 +55,13 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_edit, menu)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
menu.findItem(R.id.action_save_as)?.setAlphabeticShortcut('S', KeyEvent.META_CTRL_ON or KeyEvent.META_SHIFT_ON)
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_main, container, false)
@ -63,9 +69,26 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
with(findNavController()) {
appBarConfiguration = AppBarConfiguration(graph, drawerLayout)
toolbar.setupWithNavController(this, appBarConfiguration!!)
toolbar.inflateMenu(R.menu.menu_edit)
toolbar.setOnMenuItemClickListener { item ->
return@setOnMenuItemClickListener when (item.itemId) {
(activity as? AppCompatActivity)?.setSupportActionBar(toolbar)
navigationView.setupWithNavController(this)
}
val adapter = EditPagerAdapter(childFragmentManager, view.context)
pager.adapter = adapter
pager.addOnPageChangeListener(adapter)
pager.pageMargin = 1
pager.setPageMarginDrawable(R.color.colorAccent)
tabLayout.setupWithViewPager(pager)
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
tabLayout!!.visibility = View.GONE
}
@Suppress("CAST_NEVER_SUCCEEDS")
viewModel.fileName.observe(viewLifecycleOwner, Observer {
toolbar?.title = it
})
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_save -> {
launch {
if (!viewModel.save(requireContext())) {
@ -110,22 +133,6 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
else -> item.onNavDestinationSelected(findNavController())
}
}
navigationView.setupWithNavController(this)
}
val adapter = EditPagerAdapter(childFragmentManager, view.context)
pager.adapter = adapter
pager.addOnPageChangeListener(adapter)
pager.pageMargin = 1
pager.setPageMarginDrawable(R.color.colorAccent)
tabLayout.setupWithViewPager(pager)
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
tabLayout!!.visibility = View.GONE
}
@Suppress("CAST_NEVER_SUCCEEDS")
viewModel.fileName.observe(viewLifecycleOwner, Observer {
toolbar?.title = it
})
}
override fun onPause() {
super.onPause()

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_share"
@ -9,17 +10,21 @@
<item
android:id="@+id/action_new"
android:title="@string/action_new"
android:alphabeticShortcut="N"
app:showAsAction="never" />
<item
android:id="@+id/action_load"
android:title="@string/action_open"
android:alphabeticShortcut="O"
app:showAsAction="never" />
<item
android:id="@+id/action_save"
android:alphabeticShortcut="S"
android:title="@string/action_save" />
<item
android:id="@+id/action_save_as"
android:title="@string/action_save_as" />
android:title="@string/action_save_as"
tools:ignore="UnusedAttribute" />
<item
android:id="@+id/action_lock_swipe"
android:checkable="true"