Ui, Edit: a keyboard sent away stays away until the field is tapped
All checks were successful
Flatpak / check (push) Has been skipped
Flatpak / publish (push) Successful in 4m15s

On Plasma Mobile the on-screen keyboard came straight back after its own
hide key. The key makes the compositor give the keyboard's room back:
the window grows, the message field moves down with the window's lower
edge, and UpdateTextInput told the compositor the caret's new place, as
it does after every frame in which the caret moved. No focus had changed
and no enable went out again (the library sends one per focus); 120 ms
after that state was committed the keyboard was back, nothing else
having been sent.

A caret that only followed the window's growth is no longer told at
once: its box as it was, lower by what the window gained
(CaretFollowedGrowth, in Edit with its tests). It is told with the next
thing that is — a key that moves the caret, the field's tap — and a
window that shrinks back makes the old place true again. The compositor
keeps the old place meanwhile, which lies inside the taller window.

With that telling gone nothing would bring the keyboard back, so a tap
on a field that kept the focus while the window grew asks for it
(AskKeyboard): the text input goes off, the composing word going into
the field as at any reset, and the next frame turns it on again, which
is the request a compositor shows its keyboard for. A tap with the
keyboard up places the cursor as before. The number field of the
new-conversation screen takes the same tap.

Tested: tests/Edit (a caret lower by the window's growth, and one within
a pixel of that; and what is still told: a caret that stayed, one that
moved along its line or by another amount, a window as tall as it was or
lower, a box of another size). On a Fairphone 6 (Plasma Mobile 6.8.2,
plasma-keyboard 6.8.2), by the protocol's trace (WAYLAND_DEBUG): before,
the hide key's resize is followed by set_cursor_rectangle and commit,
and by the keyboard's resize 120 ms after them; with this, nothing is
sent after the hide key's resize and the keyboard stays away, and a tap
on the field sends disable, then enable with the caret's place, the
keyboard is back 85 ms later, and typing goes on. The number field's tap
was not tried on the phone, and no bench on the workstation has an
on-screen keyboard.
This commit is contained in:
Jorijn van der Graaf 2026-11-18 04:20:42 +01:00
commit ca5cd72d65
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 71 additions and 2 deletions

View file

@ -134,6 +134,32 @@ export namespace ImsdMessages {
ComposeInsert(c, preedit); ComposeInsert(c, preedit);
} }
// What the compositor was last told of a field's caret for its text
// input: the caret's box, in a window how tall.
struct CaretReport {
float x = 0;
float y = 0;
float w = 0;
float h = 0;
float windowHeight = 0;
};
// Whether a caret moved only because the window grew under it: its box
// as it was but for its place, which went down by what the window
// gained. That is a field at the window's lower edge after the
// on-screen keyboard gave its room back (the keyboard's own hide key).
// Told at once, the move would be a new text-input state, and a state
// committed while the input is on brings the keyboard back (seen on
// Plasma Mobile: KWin 6.8.2, plasma-keyboard), so it is held until the
// caret moves for another reason. A window that shrank, or a caret that
// moved along its text, is told as ever.
bool CaretFollowedGrowth(const CaretReport& told, const CaretReport& now) {
const float grown = now.windowHeight - told.windowHeight;
if (grown <= 0) return false;
const auto near = [](float a, float b) { return std::abs(a - b) <= 1.0f; };
return near(now.x, told.x) && near(now.w, told.w) && near(now.h, told.h) && near(now.y - told.y, grown);
}
// What was typed in the conversations that were left, and not sent, by // What was typed in the conversations that were left, and not sent, by
// conversation key, for this run. A draft is its conversation's, not // conversation key, for this run. A draft is its conversation's, not
// the view's: leaving the conversation keeps it, opening it again // the view's: leaving the conversation keeps it, opening it again

View file

@ -86,6 +86,7 @@ export namespace ImsdMessages::Ui {
hitMessages.clear(); hitMessages.clear();
textSinceFrame.clear(); textSinceFrame.clear();
frameFailed = false; frameFailed = false;
keyboardGone = false;
} }
static gboolean OnHideIdle(gpointer data) { static gboolean OnHideIdle(gpointer data) {
@ -852,20 +853,34 @@ export namespace ImsdMessages::Ui {
// Tells the compositor which field has the focus and where its caret // Tells the compositor which field has the focus and where its caret
// is, after every frame, when that changed: the number field at once, // is, after every frame, when that changed: the number field at once,
// the message field after a tap or a key; a sheet or leaving the // the message field after a tap or a key; a sheet or leaving the
// screen takes it back. // screen takes it back. A window taller than at the last telling has
// lost its on-screen keyboard (the keyboard's own hide key: the
// compositor gives the room back), and a caret that only followed
// that growth is not told (Edit's CaretFollowedGrowth): the telling
// would bring the keyboard straight back. A tap on the field asks
// for it (AskKeyboard).
void UpdateTextInput() { void UpdateTextInput() {
TextInputState want; TextInputState want;
if (!sheet) { if (!sheet) {
if (fresh) want = { true, fresh->fieldLayout.caret, Crafter::TextInputPurpose::Phone }; if (fresh) want = { true, fresh->fieldLayout.caret, Crafter::TextInputPurpose::Phone };
else if (view && view->compose.focused) want = { true, view->composeLayout.caret, Crafter::TextInputPurpose::Normal }; else if (view && view->compose.focused) want = { true, view->composeLayout.caret, Crafter::TextInputPurpose::Normal };
} }
const float height = static_cast<float>(canvas->Window().height);
const bool sameCaret = want.caret.x == lastTextInput.caret.x && want.caret.y == lastTextInput.caret.y && want.caret.w == lastTextInput.caret.w && want.caret.h == lastTextInput.caret.h; const bool sameCaret = want.caret.x == lastTextInput.caret.x && want.caret.y == lastTextInput.caret.y && want.caret.w == lastTextInput.caret.w && want.caret.h == lastTextInput.caret.h;
if (want.active == lastTextInput.active && want.purpose == lastTextInput.purpose && (!want.active || sameCaret)) return; if (want.active == lastTextInput.active && want.purpose == lastTextInput.purpose) {
if (!want.active) return;
if (height < toldHeight) toldHeight = height; // the keyboard is up, or up again
keyboardGone = height > toldHeight;
if (sameCaret) return;
if (CaretFollowedGrowth({ lastTextInput.caret.x, lastTextInput.caret.y, lastTextInput.caret.w, lastTextInput.caret.h, toldHeight }, { want.caret.x, want.caret.y, want.caret.w, want.caret.h, height })) return;
}
if (want.active != lastTextInput.active) Log(want.active ? (want.purpose == Crafter::TextInputPurpose::Phone ? "text input on (a number)" : "text input on") : "text input off"); if (want.active != lastTextInput.active) Log(want.active ? (want.purpose == Crafter::TextInputPurpose::Phone ? "text input on (a number)" : "text input on") : "text input off");
// Turning the input off ends the composition on the input // Turning the input off ends the composition on the input
// method's side; the field keeps the word it showed. // method's side; the field keeps the word it showed.
if (!want.active && lastTextInput.active) CommitComposition(); if (!want.active && lastTextInput.active) CommitComposition();
lastTextInput = want; lastTextInput = want;
toldHeight = height;
keyboardGone = false;
canvas->Window().SetTextInput(want.active, static_cast<std::int32_t>(want.caret.x), static_cast<std::int32_t>(want.caret.y), static_cast<std::int32_t>(want.caret.w), static_cast<std::int32_t>(want.caret.h), want.purpose); canvas->Window().SetTextInput(want.active, static_cast<std::int32_t>(want.caret.x), static_cast<std::int32_t>(want.caret.y), static_cast<std::int32_t>(want.caret.w), static_cast<std::int32_t>(want.caret.h), want.purpose);
} }
@ -1043,6 +1058,17 @@ export namespace ImsdMessages::Ui {
lastTextInput = {}; lastTextInput = {};
} }
// A tap on a field that kept the focus while its keyboard went
// (UpdateTextInput saw the window grow) asks for the keyboard again:
// the text input goes off now and the next frame turns it on, which
// is the request a compositor shows its keyboard for. With the
// keyboard up a tap only places the cursor.
void AskKeyboard() {
if (!keyboardGone) return;
keyboardGone = false;
EndComposition();
}
// The caret shows at once after an edit and blinks from there while a // The caret shows at once after an edit and blinks from there while a
// field has the focus and the window is up (without one nothing // field has the focus and the window is up (without one nothing
// draws it; Show starts the blink again). // draws it; Show starts the blink again).
@ -1148,11 +1174,13 @@ export namespace ImsdMessages::Ui {
} else if (*id == Hit::newField) { } else if (*id == Hit::newField) {
if (!fresh) return; if (!fresh) return;
TapField(fresh->field, fresh->fieldLayout, at); TapField(fresh->field, fresh->fieldLayout, at);
AskKeyboard();
ShowCaret(); ShowCaret();
} else if (*id == Hit::compose) { } else if (*id == Hit::compose) {
if (!view) return; if (!view) return;
view->compose.focused = true; view->compose.focused = true;
TapField(view->compose, view->composeLayout, at); TapField(view->compose, view->composeLayout, at);
AskKeyboard();
ShowCaret(); ShowCaret();
} else if (*id == Hit::send) { } else if (*id == Hit::send) {
SendDraft(); SendDraft();
@ -1237,6 +1265,8 @@ export namespace ImsdMessages::Ui {
std::optional<Crafter::EventListener<const std::string_view>> preeditSub; std::optional<Crafter::EventListener<const std::string_view>> preeditSub;
std::optional<Crafter::EventListener<Crafter::TextDelete>> deleteSub; std::optional<Crafter::EventListener<Crafter::TextDelete>> deleteSub;
TextInputState lastTextInput; TextInputState lastTextInput;
float toldHeight = 0; // the window's height when the compositor was last told of the text input: lower since, the keyboard is up
bool keyboardGone = false; // the window is taller than that: the keyboard gave its room back while the field kept the focus (UpdateTextInput)
std::optional<Crafter::EventListener<void>> resizeSub; std::optional<Crafter::EventListener<void>> resizeSub;
std::optional<Crafter::EventListener<void>> closeSub; std::optional<Crafter::EventListener<void>> closeSub;
std::optional<Crafter::EventListener<void>> seenSub; std::optional<Crafter::EventListener<void>> seenSub;

View file

@ -143,6 +143,19 @@ int main() {
Check(ComposeFirstLine(19, 5) == 15, "the same far down a long text"); Check(ComposeFirstLine(19, 5) == 15, "the same far down a long text");
Check(ComposeFirstLine(0, 1) == 0 && ComposeFirstLine(7, 1) == 7, "a one-line field shows the caret's line"); Check(ComposeFirstLine(0, 1) == 0 && ComposeFirstLine(7, 1) == 7, "a one-line field shows the caret's line");
// --- a caret that only followed the window's growth: the keyboard went -
{
const CaretReport told{ 76, 1002, 4, 24, 1068 }; // the message field's caret above the keyboard
Check(CaretFollowedGrowth(told, { 76, 1526, 4, 24, 1592 }), "the window grew and the caret went down by as much: it only followed the growth");
Check(CaretFollowedGrowth(told, { 76.5f, 1526.8f, 4, 24, 1592 }), "within a pixel of it, as a rounded layout gives");
Check(!CaretFollowedGrowth(told, { 76, 1002, 4, 24, 1592 }), "a caret that stayed where it was in a window that grew (a field at the top) did not move at all: nothing held");
Check(!CaretFollowedGrowth(told, { 94, 1526, 4, 24, 1592 }), "a caret that moved along its line as well is told");
Check(!CaretFollowedGrowth(told, { 76, 1502, 4, 24, 1592 }), "one that moved by another amount (a line more or less) is told");
Check(!CaretFollowedGrowth(told, { 76, 1002, 4, 24, 1068 }) && !CaretFollowedGrowth(told, { 76, 1026, 4, 24, 1068 }), "the window as tall as it was: every move is told");
Check(!CaretFollowedGrowth({ 76, 1526, 4, 24, 1592 }, { 76, 1002, 4, 24, 1068 }), "the window shrank (the keyboard came up): the caret's new place is told");
Check(!CaretFollowedGrowth(told, { 76, 1526, 4, 30, 1592 }), "a box of another size is told");
}
// The drafts of the conversations left: kept by key, taken up at the // The drafts of the conversations left: kept by key, taken up at the
// next opening. // next opening.
{ {