Selection mode: Space to cancel the selection

This adds the "selection mode", which is activated when text is selected
in the text box. The selection mode is exited when the selection is
cleared.

While the selection mode is activated, the Space and Esc keys are
modified into the "selection cancel" key, which remove the selection
without changing the text.
The space bar is otherwise easy to type by accident during a selection
and causes the selected text to be deleted.
This commit is contained in:
Jules Aguillon 2025-03-01 15:07:03 +01:00
parent 55cb5f5315
commit 68c4ba96b7
6 changed files with 59 additions and 0 deletions

View File

@ -236,6 +236,7 @@ public final class KeyEventHandler
case REPLACE: send_context_menu_action(android.R.id.replaceText); break;
case ASSIST: send_context_menu_action(android.R.id.textAssist); break;
case AUTOFILL: send_context_menu_action(android.R.id.autofill); break;
case SELECTION_CANCEL: cancel_selection(); break;
}
}
@ -353,11 +354,25 @@ public final class KeyEventHandler
send_key_down_up(event_code);
}
void cancel_selection()
{
InputConnection conn = _recv.getCurrentInputConnection();
if (conn == null)
return;
ExtractedText et = get_cursor_pos(conn);
if (et == null) return;
final int curs = et.selectionStart;
// Notify the receiver as Android's [onUpdateSelection] is not triggered.
if (conn.setSelection(curs, curs));
_recv.selection_state_changed(false);
}
public static interface IReceiver
{
public void handle_event_key(KeyValue.Event ev);
public void set_shift_state(boolean state, boolean lock);
public void set_compose_pending(boolean pending);
public void selection_state_changed(boolean selection_is_ongoing);
public InputConnection getCurrentInputConnection();
}

View File

@ -82,6 +82,7 @@ public final class KeyModifier
case HOOK_ABOVE: return apply_compose(k, ComposeKeyData.accent_hook_above);
case DOUBLE_GRAVE: return apply_compose(k, ComposeKeyData.accent_double_grave);
case ARROW_RIGHT: return apply_combining_char(k, "\u20D7");
case SELECTION_MODE: return apply_selection_mode(k);
default: return k;
}
}
@ -385,6 +386,27 @@ public final class KeyModifier
return (name == null) ? k : KeyValue.getKeyByName(name);
}
private static KeyValue apply_selection_mode(KeyValue k)
{
String name = null;
switch (k.getKind())
{
case Char:
switch (k.getChar())
{
case ' ': name = "selection_cancel"; break;
}
break;
case Keyevent:
switch (k.getKeyevent())
{
case KeyEvent.KEYCODE_ESCAPE: name = "selection_cancel"; break;
}
break;
}
return (name == null) ? k : KeyValue.getKeyByName(name);
}
/** Compose the precomposed initial with the medial [kv]. */
private static KeyValue combine_hangul_initial(KeyValue kv, int precomposed)
{

View File

@ -59,6 +59,7 @@ public final class KeyValue implements Comparable<KeyValue>
BREVE,
BAR,
FN,
SELECTION_MODE,
} // Last is be applied first
public static enum Editing
@ -75,6 +76,7 @@ public final class KeyValue implements Comparable<KeyValue>
SHARE,
ASSIST,
AUTOFILL,
SELECTION_CANCEL,
}
public static enum Placeholder
@ -760,6 +762,10 @@ public final class KeyValue implements Comparable<KeyValue>
case "": case "":
return makeStringKey(name, FLAG_SMALLER_FONT);
/* Internal keys */
case "selection_mode": return makeInternalModifier(Modifier.SELECTION_MODE);
case "selection_cancel": return editingKey("Esc", Editing.SELECTION_CANCEL, FLAG_SMALLER_FONT);
default: return null;
}
}

View File

@ -359,6 +359,8 @@ public class Keyboard2 extends InputMethodService
{
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd);
_keyeventhandler.selection_updated(oldSelStart, newSelStart);
if ((oldSelStart == oldSelEnd) != (newSelStart == newSelEnd))
_keyboardView.set_selection_state(newSelStart != newSelEnd);
}
@Override
@ -477,6 +479,11 @@ public class Keyboard2 extends InputMethodService
_keyboardView.set_compose_pending(pending);
}
public void selection_state_changed(boolean selection_is_ongoing)
{
_keyboardView.set_selection_state(selection_is_ongoing);
}
public InputConnection getCurrentInputConnection()
{
return Keyboard2.this.getCurrentInputConnection();

View File

@ -139,6 +139,13 @@ public class Keyboard2View extends View
set_fake_ptr_latched(_compose_key, _compose_kv, pending, false);
}
/** Called from [Keybard2.onUpdateSelection]. */
public void set_selection_state(boolean selection_state)
{
set_fake_ptr_latched(KeyboardData.Key.EMPTY,
KeyValue.getKeyByName("selection_mode"), selection_state, true);
}
public KeyValue modifyKey(KeyValue k, Pointers.Modifiers mods)
{
return KeyModifier.modify(k, mods);

View File

@ -422,6 +422,8 @@ public final class KeyboardData
indication = i;
}
static final Key EMPTY = new Key(new KeyValue[9], null, 0, 1.f, 1.f, null);
/** Read a key value attribute that have a synonym. Having both synonyms
present at the same time is an error.
Returns [null] if the attributes are not present. */