Skip to content

Bugfix/18670 cursor position retention #18759

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

Merged

Conversation

Aryan171
Copy link
Contributor

@Aryan171 Aryan171 commented Jul 3, 2025

Purpose / Description

  • Remembering and showing the cursor position while switching views in card template editor
  • Adding different cursor position for different views in the card template editor

Fixes

Approach

In the card template editor when switching from front to back to css. The cursor position was not retained, this was due to faulty cursor position updation. This issue was solved by saving the cursor position when the editor view was changed and not when the text was changed. Also only a single cursor position was stored per card, which is not enough for all the three views i.e. front, back and css. This was taken care of by storing a HashMap<Int, Int> for every card instead of a card.

How Has This Been Tested?

On an Android 15 phone

Checklist

  • 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

Aryan171 added 6 commits July 3, 2025 11:51
This commit refactors the `CardTemplateEditor` to remove the `cursorPosition` argument from `CardTemplateFragment.newInstance`.

The `cursorPosition` is now directly retrieved from the `tabToCursorPosition` map within the `CardTemplateFragment` when setting the editor view. This simplifies the fragment's initialization and makes the cursor position handling more localized.

Additionally, a check is added in `afterTextChanged` to ensure `tabToCursorPosition` is only updated if the selection start is not 0, preventing incorrect cursor position updates in certain scenarios.
This change modifies how cursor positions are stored in the Card Template Editor.
Previously, only one cursor position was saved per card template.

Now, the cursor position is saved independently for each editor window (front, style, back) within each card template.

This ensures that when switching between editor windows or card templates, the cursor remains in its last known position for that specific window and template combination.
When a new card template tab is created in the CardTemplateEditor, ensure the `tabToCursorPosition` map, which stores cursor positions for each editor window within that tab, is initialized.
The variable `tabToCursorPosition` stores a map of tab indices to a map of editor view IDs to cursor positions. Renaming it to `tabToCursorPositions` (plural) better reflects that it holds multiple cursor positions for each tab, one for each editor window (front, style, back) within that tab.
The `setCurrentEditorView` method now requires a `cardId` parameter. This commit updates the test cases to pass a default `cardId` of 0 when calling this method.
The cursor position was not being saved correctly when switching between editor views (Front, Back, Styling). This commit ensures that the cursor position is saved before changing the editor view and restored when switching back.

The previous logic that saved the cursor position whenever the text changed did not account for just the cursor position change and one had to change the text to trigger a save of cursor position.
@Aryan171
Copy link
Contributor Author

Aryan171 commented Jul 3, 2025

Closed the last PR due to some missed updates. This one contains the complete and corrected changes.

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.

Looks great, cheers!

* The inner HashMap's key is the editor window ID (e.g., R.id.front_edit).
* The value is the cursor position within that editor window.
*/
private var tabToCursorPositions: HashMap<Int, HashMap<Int, Int>> = HashMap()
Copy link
Member

Choose a reason for hiding this comment

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

This would be more readable if you made private typealiases for the Ints

@david-allison david-allison added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Jul 9, 2025
@david-allison david-allison added this to the 2.21.1 release milestone Jul 9, 2025
Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

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

this looks good to me, thanks!

noting squash merge intention for maintainers

@mikehardy mikehardy added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) squash-merge Squash & force push by author/maintainers requested and removed Needs Second Approval Has one approval, one more approval to merge labels Jul 12, 2025
@mikehardy mikehardy merged commit d4e1ff3 into ankidroid:main Jul 12, 2025
12 checks passed
@github-actions github-actions bot removed squash-merge Squash & force push by author/maintainers requested Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) labels Jul 12, 2025
@github-actions github-actions bot modified the milestones: 2.21.1 release, 2.22 release Jul 12, 2025
@mikehardy
Copy link
Member

This doesn't cherry-pick cleanly and I'd prefer not to hand-hack the diff for a non-crash fix
This will release with 2.22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template editor should retain where the cursor was
3 participants