Add cursor_up and cursor_down slider keys

Implement up and down cursor movement slider. This is not added to any
layout yet due to the undesirable behavior when moving the focus out of
the text box being edited.
This commit is contained in:
Jules Aguillon 2025-01-12 19:16:04 +01:00
parent ca05c073d2
commit bffc76907a
3 changed files with 48 additions and 35 deletions

View File

@ -68,6 +68,8 @@ Value | Meaning
:----------------- | :------
`cursor_left` | Moves the cursor to the left with the slider gesture.
`cursor_right` | Moves the cursor to the right with the slider gesture.
`cursor_up` | Moves the cursor up with the slider gesture. Warning: this might make the cursor leave the text box.
`cursor_down` | Moves the cursor down with the slider gesture. Warning: this might make the cursor leave the text box.
## Other modifiers and diacritics
Value | Meaning

View File

@ -262,12 +262,15 @@ public final class KeyEventHandler
return conn.getExtractedText(_move_cursor_req, 0);
}
/** [repeatition] might be negative, in which case the direction is reversed. */
void handle_slider(KeyValue.Slider s, int repeatition)
{
switch (s)
{
case Cursor_left: move_cursor(-repeatition); break;
case Cursor_right: move_cursor(repeatition); break;
case Cursor_up: move_cursor_vertical(-repeatition); break;
case Cursor_down: move_cursor_vertical(repeatition); break;
}
}
@ -283,47 +286,51 @@ public final class KeyEventHandler
ExtractedText et = get_cursor_pos(conn);
int system_mods =
KeyEvent.META_CTRL_ON | KeyEvent.META_ALT_ON | KeyEvent.META_META_ON;
// Fallback to sending key events
if (_move_cursor_force_fallback || et == null
|| (_meta_state & system_mods) != 0)
// Fallback to sending key events if system modifiers are activated or
// ExtractedText is not supported, for example on Termux.
if (!_move_cursor_force_fallback && et != null
&& (_meta_state & system_mods) == 0)
{
move_cursor_fallback(d);
return;
}
int sel_start = et.selectionStart;
int sel_end = et.selectionEnd;
// Continue expanding the selection even if shift is not pressed
if (sel_end != sel_start)
{
sel_end += d;
if (sel_end == sel_start) // Avoid making the selection empty
int sel_start = et.selectionStart;
int sel_end = et.selectionEnd;
// Continue expanding the selection even if shift is not pressed
if (sel_end != sel_start)
{
sel_end += d;
if (sel_end == sel_start) // Avoid making the selection empty
sel_end += d;
}
else
{
sel_end += d;
// Leave 'sel_start' where it is if shift is pressed
if ((_meta_state & KeyEvent.META_SHIFT_ON) == 0)
sel_start = sel_end;
}
if (conn.setSelection(sel_start, sel_end))
return; // [setSelection] succeeded, don't fallback to key events
}
if (d < 0)
send_key_down_up_repeat(KeyEvent.KEYCODE_DPAD_LEFT, -d);
else
{
sel_end += d;
// Leave 'sel_start' where it is if shift is pressed
if ((_meta_state & KeyEvent.META_SHIFT_ON) == 0)
sel_start = sel_end;
}
if (!conn.setSelection(sel_start, sel_end))
move_cursor_fallback(d);
send_key_down_up_repeat(KeyEvent.KEYCODE_DPAD_RIGHT, d);
}
/** Send arrow keys as a fallback for editors that do not support
[getExtractedText] like Termux. */
void move_cursor_fallback(int d)
/** Move the cursor up and down. This sends UP and DOWN key events that might
make the focus exit the text box. */
void move_cursor_vertical(int d)
{
while (d < 0)
{
send_key_down_up(KeyEvent.KEYCODE_DPAD_LEFT);
d++;
}
while (d > 0)
{
send_key_down_up(KeyEvent.KEYCODE_DPAD_RIGHT);
d--;
}
if (d < 0)
send_key_down_up_repeat(KeyEvent.KEYCODE_DPAD_UP, -d);
else
send_key_down_up_repeat(KeyEvent.KEYCODE_DPAD_DOWN, d);
}
/** Repeat calls to [send_key_down_up]. */
void send_key_down_up_repeat(int event_code, int repeat)
{
while (repeat-- > 0)
send_key_down_up(event_code);
}
public static interface IReceiver

View File

@ -687,6 +687,8 @@ public final class KeyValue implements Comparable<KeyValue>
case "redo": return editingKey(0xE037, Editing.REDO);
case "cursor_left": return sliderKey(Slider.Cursor_left, 1);
case "cursor_right": return sliderKey(Slider.Cursor_right, 1);
case "cursor_up": return sliderKey(Slider.Cursor_up, 1);
case "cursor_down": return sliderKey(Slider.Cursor_down, 1);
// These keys are not used
case "replaceText": return editingKey("repl", Editing.REPLACE);
case "textAssist": return editingKey(0xE038, Editing.ASSIST);
@ -805,7 +807,9 @@ public final class KeyValue implements Comparable<KeyValue>
public static enum Slider
{
Cursor_left(0xE008),
Cursor_right(0xE006);
Cursor_right(0xE006),
Cursor_up(0xE005),
Cursor_down(0xE007);
final String symbol;