Skip to content

Fix inverted RTL word navigation in the subtitle edit box#11715

Open
muaz978 wants to merge 1 commit into
SubtitleEdit:mainfrom
muaz978:fix/rtl-textbox-caret-navigation
Open

Fix inverted RTL word navigation in the subtitle edit box#11715
muaz978 wants to merge 1 commit into
SubtitleEdit:mainfrom
muaz978:fix/rtl-textbox-caret-navigation

Conversation

@muaz978

@muaz978 muaz978 commented Jun 20, 2026

Copy link
Copy Markdown

What

Fixes Ctrl+Left / Ctrl+Right jumping the wrong way when editing a right-to-left subtitle (e.g. Arabic), and makes Ctrl+Up / Ctrl+Down go to the start / end of the current line.

Why

Avalonia's TextBox moves the caret one character visually (bidi-aware, already correct) but moves whole words by logical text offset, so word steps run opposite to the visual direction under RTL. Single-character steps were fine; word steps were inverted.

How

A tunnel-phase key handler on the main and original edit text boxes that runs only when the box is in RTL flow:

  • Ctrl+Left / Ctrl+Right move a word in the visually-correct direction.
  • Ctrl+Up / Ctrl+Down move to start / end of the current line.
  • Shift extends the selection.

It no-ops for left-to-right editing, so LTR behaviour and global Ctrl+Arrow shortcuts are unchanged.

Note: please verify in a real RTL editing session - developed on macOS, not yet runtime-tested.

@muaz978 muaz978 force-pushed the fix/rtl-textbox-caret-navigation branch from 7581526 to a6ce028 Compare June 20, 2026 23:41
@GrampaWildWilly

Copy link
Copy Markdown

This caught my eye because there was a recent fix made to let Ctrl+Right/LeftArrow skip words instead of navigating the video. This was an issue of there being shortcuts for video navigation that happen to collide with Windows standard keyboard combinations that navigate text. The solution was to suspend recognition of the shortcuts while focus is in the text box. At least, that's my understanding of the solution. I'm just a user. I have no hope of ever actually understanding the code were I to see it.

However, I take issue with your selection of Ctrl+Up/DownArrow as text movement keyboard directives. These are NOT standard text editing operations in Windows. To go to the beginning of a line, the standard keyboard operation is the Home key. For line end, it's the End key. To go to the beginning of the text in the textbox -- like if there's 2 lines of text & the cursor is somewhere in the second line -- the standard key combination is Ctrl+Home. To go to the end of the text, it's Ctrl+End. So I say that you should remove whatever logic you have coded to recognize Ctrl+Up/DownArrow. The keystrokes I have described are provided by Windows itself & these should simply be allowed to operate within the textbox. This should not be an issue of left-to-right vs right-to-left text orientation. To my way of thinking, making sure Ctrl+Left/RightArrow swap directions depending on RTL or LTR seems valid. But the rest does not seem valid. In addition, I would assume there are keyboard operations on Mac that are equivalent to the ones I've described for Windows. You should not add code that duplicates what the operating system already provides. If standard keystrokes don't filter through in the textbox on Mac, you should be looking at suspending colliding shortcuts from interfering with standard text operations defined in the operating system, not customizing new keystrokes to circumvent the root cause.

@muaz978

muaz978 commented Jun 21, 2026

Copy link
Copy Markdown
Author

Thank you for your comment,

In SE4, on Windows, ctrl + up or down arrows would take you to the head or end of the line you are in if your are using the text box, double do that and it would take you to the head of the first line if you were in the second in case you had two. However, this was not functioning properly in latest 5 release, and in Arabic language it is even inverted, that is what I was trying to fix actually.

@muaz978 muaz978 force-pushed the fix/rtl-textbox-caret-navigation branch 2 times, most recently from 01b3ba8 to 4583ce8 Compare June 21, 2026 06:02
@muaz978

muaz978 commented Jun 21, 2026

Copy link
Copy Markdown
Author

Still working on this, and will confirm once I test it myself.

@muaz978 muaz978 force-pushed the fix/rtl-textbox-caret-navigation branch 5 times, most recently from 83a7320 to 960cc51 Compare June 21, 2026 07:33
@muaz978

muaz978 commented Jun 21, 2026

Copy link
Copy Markdown
Author

Tested and working on macOS 26.5 (Apple Silicon) from a local build of this branch, with Arabic subtitles, in both edit-box modes (the AvaloniaEdit editor used when color tags are on, and the plain TextBox).

Behaviour (right-to-left text only)

For a focused subtitle edit box whose content is right-to-left, caret navigation now moves in the visually-correct direction, using each platform's standard navigation keys:

  • WordCtrl+Left/Right (all platforms); Option(Alt)+Left/Right (macOS). Moves one word in the visual direction of the arrow.
  • LineCmd(Meta)+Left/Right (macOS). Moves to the visual start/end of the current line.
  • Shift + any of the above extends the selection, and repeated presses grow it word-by-word / out to the line edge.
  • Bare Left/Right with an active selection collapses it to the visually-correct edge (Left → left edge, Right → right edge). With no selection, single-character movement is left to the editor (it was already bidi-correct).

Why it lives in the key pipeline

A handler attached directly to the TextBox / AvaloniaEdit TextArea does not reliably win against AvaloniaEdit's built-in caret commands. So this is handled in the central OnKeyDownHandler, in the root tunnel phase — the same place the edit box already overrides keys (e.g. the Return-key line limiter) — where marking the event handled cleanly pre-empts the editor's inverted logical-order navigation. Movement goes through the ITextBoxWrapper abstraction, so it works for both edit-box implementations.

Two implementation notes:

  • RTL is detected by content, per line with a whole-box fallback — a subtitle is normally edited in an otherwise left-to-right window, so the control's FlowDirection stays LtR even for Arabic; keying off FlowDirection doesn't work.
  • AvaloniaEdit relocates the caret of a programmatic selection back to its anchor between key presses, which broke growing a selection. The selection's fixed anchor and moving end are now tracked in the view model, so Shift+word/line accumulates correctly.

Scope — what is NOT affected

  • Left-to-right text: the handler returns immediately — native navigation everywhere, unchanged.
  • Non-arrow shortcuts (copy/paste/undo), Home/End, Ctrl+Home/Ctrl+End: untouched.
  • Windows: the only change for Windows users is Ctrl+Left/Right word-jump inside RTL text (the same inversion fix); English/LTR editing is unchanged. The Option/Cmd handling is macOS-only.

Verified on-device: word nav (Option and the Cmd-position key), line nav, Shift-extend accumulation across multiple words and to line edges, bare-arrow collapse to the correct edge, and LTR/English left unaffected.

@muaz978

muaz978 commented Jun 23, 2026

Copy link
Copy Markdown
Author

Is there any changes or enhancements that you suggest me to edit or improve?
The last version I comitted was clean and tested it myself, I got a notification that it now has conflict, but didn't understand what was really changed, can you advice on this please?

In a right-to-left subtitle, word and line caret navigation jumped the wrong
way. Avalonia (and AvaloniaEdit, used when color tags are on) move whole words
by logical text offset, which runs backwards for RTL text; single-character
steps were already visually correct.

Handle these keys in the central OnKeyDownHandler - the same place the edit box
already overrides keys (e.g. the Return-key line limiter) by marking the event
handled in the root tunnel phase, which reliably pre-empts the editor's built-in
navigation. A side TextBox/TextArea handler does not win against AvaloniaEdit,
which is why this lives in the key pipeline instead.

For right-to-left text in the focused edit box (detected by content, per line
with a whole-box fallback - a subtitle is usually edited in an otherwise LtR
window so FlowDirection stays LtR), using the platform's standard navigation
keys:
- Word: Ctrl+Left/Right everywhere; Option(Alt)+Left/Right on macOS - moves a
  word in the visually-correct direction.
- Line: Command(Meta)+Left/Right on macOS - moves to the visual start/end of
  the line.
- Shift extends the selection.

Movement goes through the ITextBoxWrapper abstraction, so it works for both the
plain TextBox and the AvaloniaEdit TextEditor edit-box implementations.
Left-to-right text keeps native navigation, and non-arrow shortcuts (copy /
paste / undo) plus Home / End are untouched.
@muaz978 muaz978 force-pushed the fix/rtl-textbox-caret-navigation branch from 960cc51 to a85495e Compare June 23, 2026 22:11
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.

2 participants