Prevent focus loss after save #5742
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #5132
Before
before.mp4
After
after.mp4
This pull request updates #5702 by now accounting for zooming.
Explanation
In a previous pull request,
pos.x
andpos.y
already accounted for zooming, whileonButtonPressEvent
andonButtonReleaseEvent
expected positions that did not account for zooming.onButtonPressEvent
andonButtonReleaseEvent
were applying zooming by dividing coordinates by a zooming factor.To place the cursor in the correct position, I had to multiply
pos.x
andpos.y
by the zooming factor.Possible Improvements
This is not the only solution I considered. There were improvements I wanted to make, but due to my limited understanding of the contribution requirements, I was unsure whether they would be beneficial.
While writing this pull request, I tried to be as concise as possible, changing a minimal number of files and making the least number of additions. Yet, it is not the most elegant or correct solution.
I believe that the correct solution would be to add a new method to the
TextEditor
class namedsync
, which would synchronize theTextEditor
contents with a text element, without closing theTextEditor
(freeing the pointer). Then, we would refactor thecloseSelectionEndText
method intocloseSelectionSyncText
, which would call thesync
method instead of freeing the pointer. However, this seemed as an overkill for such small enhancement, so I implemented a simple, hacky solution.My current solution feels hacky because the
onButtonPressEvent
andonButtonReleaseEvent
methods weren't intended to be used this way. The fact that coordinates were divided by the zooming factor, only to be multiplied by it again, seems a bit inelegant. Nonetheless, the solution works, and it is the only one I could implement without refactoring many classes and methods.