Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GSoC'24] CardTemplateEditor: Implementation of Keyboard Shortcuts #16722

Merged

Conversation

SanjaySargam
Copy link
Contributor

Purpose / Description

Implement keyboard shortcuts to enhance the user experience by providing quick access to commonly used functionalities within the application. This will improve the overall usability, especially for power users who prefer using the keyboard over the mouse.

Approach

The shortcuts are implemented by adding event listeners for keypress events and mapping them to the respective functions. The approach ensures that the shortcuts are intuitive and do not conflict with existing shortcuts.

How Has This Been Tested?

HP Chromebook

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

when (menuItem.itemId) {
R.id.action_add -> {

This comment was marked as outdated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DIdn't removed

Copy link
Contributor

@criticalAY criticalAY left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subject: [PATCH] Template
---
Index: AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt	(revision 49cae72ffb7fe08a5a9a77a0c87b8bf1dac5b2b2)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt	(date 1720726825334)
@@ -564,25 +564,50 @@
                     }
 
                     override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
-                        when (menuItem.itemId) {
+                        return when (menuItem.itemId) {
                             R.id.action_add -> addCard()
-                            R.id.action_reposition -> showRepositionDialog()
-                            R.id.action_rename -> showRenameDialog()
-                            R.id.action_copy_as_markdown -> copyMarkdownTemplateToClipboard()
-                            R.id.action_insert_field -> showInsertFieldDialog()
-                            R.id.action_delete -> deleteCard()
+                            R.id.action_reposition -> {
+                                showRepositionDialog()
+                                return true
+                            }
+
+                            R.id.action_rename -> {
+                                showRenameDialog()
+                                return true
+                            }
+
+                            R.id.action_copy_as_markdown -> {
+                                copyMarkdownTemplateToClipboard()
+                                return true
+
+                            }
+
+                            R.id.action_insert_field -> {
+                                showInsertFieldDialog()
+                                return true
+
+                            }
+
+                            R.id.action_delete -> {
+                                return deleteCard()
+                            }
+
                             R.id.action_add_deck_override -> {
                                 displayDeckOverrideDialog(tempModel)
                                 return true
                             }
+
                             R.id.action_preview -> {
                                 performPreview()
                                 return true
                             }
+
                             R.id.action_confirm -> saveNote()
                             R.id.action_card_browser_appearance -> openBrowserAppearance()
+                            else -> {
+                               false
+                            }
                         }
-                        return false
                     }
                 },
                 viewLifecycleOwner,

should resolve the test issue

@SanjaySargam SanjaySargam force-pushed the card-template-editor-shortcuts branch from 49cae72 to 4d92a55 Compare July 11, 2024 20:05
@SanjaySargam
Copy link
Contributor Author

@criticalAY Thank You ❤️

}

fun addCard(): Boolean {
Timber.i("CardTemplateEditor:: Add template button pressed")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This timber is false. You Timber it also when the user click "a".

I prefer the Timbers to remain in the onMenuItemSelected and also in onKeyUp. Because we are trying to tell to which event we react, and those two methods are the one dealing with the event.

Same remakrs for every other functions you introduce please

}
confirmAddCards(tempModel.notetype, numAffectedCards)
return when (menuItem.itemId) {
R.id.action_add -> addCard()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer for addCard not to return anything, and have return true in the code. It does not seems that addCard has any reason to return anything

}
},
viewLifecycleOwner,
Lifecycle.State.RESUMED
)
}

fun deleteCard(): Boolean {
val col = templateEditor.getColUnsafe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a "todo" stating to use withCol? I don't request a change right now because you are just moving code and that's unrelated to your change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it was not clear, this is the kind of remark that should be applied everywhere. We should not use getColUnsafe
It's currently used, so I won't require you to rewrite this part of the code, your commit is an improvement to our codebase. Still, each getColUnsafe should have its todo

val col = templateEditor.getColUnsafe
val tempModel = templateEditor.tempModel
Timber.i("CardTemplateEditor:: Delete template button pressed")
val res = resources
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use it a single time, we can delete this line and use resources instead

0
}
confirmDeleteCards(template, tempModel.notetype, numAffectedCards)
deleteCard()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call it deleteNote, because you'll delete the whole note

return true
}

fun addCard(): Boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call it addCardTemplate. You're adding card templates. As a side effect it add cards, but that's not the main effect.

}
},
viewLifecycleOwner,
Lifecycle.State.RESUMED
)
}

fun deleteCard(): Boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call it deleteCardTemplate. Same reason as "addCard" above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arthur-Milchior in previous review you said

Please call it deleteNote, because you'll delete the whole note

// Don't do anything if only one template
if (tempModel.templateCount < 2) {
templateEditor.showSimpleMessageDialog(res.getString(R.string.card_template_editor_cant_delete))
return true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for addCard, I'd prefer you don't return a boolean, given it's always the same value.
But keep the early return

return true
}

fun saveNote(): Boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saveNoteType

- Implemented keyboard shortcuts:
  - Ctrl+P: Perform Preview
  - Ctrl+1: Navigate to Front Edit
  - Ctrl+2: Navigate to Back Edit
  - Ctrl+3: Navigate to Styling Edit
  - Ctrl+S: Save Note
  - Ctrl+R: Rename template
  - Ctrl+I: Show Insert Field Dialog
  - Ctrl+A: Add template
  - Ctrl+B: Open Browser Appearance
  - Ctrl+D: Delete template
  - Ctrl+O: Display Deck Override Dialog
  - Ctrl+M: Copy Markdown Template to Clipboard
@SanjaySargam SanjaySargam force-pushed the card-template-editor-shortcuts branch from 4d92a55 to 41d7402 Compare July 12, 2024 12:31
@lukstbit lukstbit added Needs Review GSoC Pull requests authored by a Google Summer of Code participant [Candidate/Selected], for GSoC mentors labels Jul 14, 2024
Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, cheers!

@david-allison david-allison added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Jul 18, 2024
}
},
viewLifecycleOwner,
Lifecycle.State.RESUMED
)
}

fun deleteCard(): Boolean {
val col = templateEditor.getColUnsafe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it was not clear, this is the kind of remark that should be applied everywhere. We should not use getColUnsafe
It's currently used, so I won't require you to rewrite this part of the code, your commit is an improvement to our codebase. Still, each getColUnsafe should have its todo

}

if (deletionWouldOrphanNote(col, tempModel, ordinal)) {
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created #16789. The user should be told why it's not done. But this an already existing bug, so it's not blocking

@Arthur-Milchior Arthur-Milchior added this pull request to the merge queue Jul 28, 2024
Merged via the queue into ankidroid:main with commit 87e2a79 Jul 28, 2024
8 checks passed
@github-actions github-actions bot removed the Needs Second Approval Has one approval, one more approval to merge label Jul 28, 2024
@github-actions github-actions bot added this to the 2.19 release milestone Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GSoC Pull requests authored by a Google Summer of Code participant [Candidate/Selected], for GSoC mentors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants