Commit 608edd2
Fix cursor position jumping to top after update/delete operations (#5)
## Problem
When updating or deleting a package, the cursor selection would jump
back to the topmost element in the list, making it frustrating to
perform multiple operations in succession. This was especially
noticeable when working through a list of packages that need updating or
when cleaning up multiple packages.
## Root Cause
The `apply_filter()` method was unconditionally resetting the list
selection to index 0 after filtering. This method was called by
`refresh_packages()`, which is invoked after both update and delete
operations to ensure the package list is up-to-date.
## Solution
This PR implements cursor position preservation for both update and
delete operations:
### UPDATE Operation
- **Before**: Cursor jumps to the first package in the list
- **After**: Cursor stays on the updated package
The implementation saves the current selection index before refreshing
the package list, then restores it after the refresh is complete.
### DELETE Operation
- **Before**: Cursor jumps to the first package in the list
- **After**: Cursor moves to the package above the deleted one (or stays
at position 0 if deleting the first item)
The implementation calculates the appropriate new position (`index - 1`
for items after the first, `0` for the first item) before refreshing the
package list.
## Implementation Details
The changes introduce two new internal methods that allow optional
selection preservation:
- `apply_filter_with_selection(preserve_selection: Option<usize>)` -
Applies filtering while optionally preserving a specific cursor position
- `refresh_packages_with_selection(preserve_selection: Option<usize>)` -
Refreshes packages while optionally preserving a specific cursor
position
The original public methods (`apply_filter()` and `refresh_packages()`)
remain unchanged and delegate to these new methods, ensuring backward
compatibility.
## Testing
- All existing tests pass
- Code compiles without warnings
- Clippy produces no issues
- Changes are backward compatible with no breaking changes to the API
Fixes #[issue_number]
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Potential improvement</issue_title>
> <issue_description>## Issue
> When deleting or updating a package, the cursor selection jumps back
to the topmost element.
>
> ## Potential Improvement
> UPDATE -> Keep the position on the updated package.
> DELETE -> Move the cursor position to the element above the deleted
element.
> </issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> <comment_new><author>@cosmincatalin</author><body>
> Sounds reasonable, let me look into it.</body></comment_new>
> </comments>
>
</details>
Fixes #3
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/cosmincatalin/brew-explorer/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: cosmincatalin <525590+cosmincatalin@users.noreply.github.com>
Co-authored-by: Cosmin Catalin SANDA <me@cosminsanda.com>1 parent 0381592 commit 608edd2
1 file changed
Lines changed: 55 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
95 | 100 | | |
96 | 101 | | |
97 | 102 | | |
98 | 103 | | |
99 | 104 | | |
100 | | - | |
| 105 | + | |
101 | 106 | | |
102 | 107 | | |
103 | 108 | | |
| |||
440 | 445 | | |
441 | 446 | | |
442 | 447 | | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
443 | 452 | | |
444 | 453 | | |
445 | 454 | | |
| |||
455 | 464 | | |
456 | 465 | | |
457 | 466 | | |
458 | | - | |
459 | | - | |
460 | | - | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
461 | 483 | | |
462 | | - | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
463 | 490 | | |
464 | 491 | | |
465 | 492 | | |
| |||
608 | 635 | | |
609 | 636 | | |
610 | 637 | | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
611 | 641 | | |
612 | 642 | | |
613 | 643 | | |
| |||
627 | 657 | | |
628 | 658 | | |
629 | 659 | | |
630 | | - | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
637 | | - | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
638 | 669 | | |
639 | | - | |
| 670 | + | |
640 | 671 | | |
641 | 672 | | |
642 | | - | |
643 | | - | |
644 | | - | |
645 | | - | |
646 | | - | |
647 | | - | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
648 | 677 | | |
649 | 678 | | |
650 | 679 | | |
| |||
655 | 684 | | |
656 | 685 | | |
657 | 686 | | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
658 | 690 | | |
659 | 691 | | |
660 | 692 | | |
| |||
673 | 705 | | |
674 | 706 | | |
675 | 707 | | |
676 | | - | |
677 | | - | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
678 | 711 | | |
679 | 712 | | |
680 | 713 | | |
| |||
0 commit comments