diff --git a/interfaces/ImsdMessages-Edit.cppm b/interfaces/ImsdMessages-Edit.cppm index 33d3fa5..15b894a 100644 --- a/interfaces/ImsdMessages-Edit.cppm +++ b/interfaces/ImsdMessages-Edit.cppm @@ -134,6 +134,32 @@ export namespace ImsdMessages { 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 // conversation key, for this run. A draft is its conversation's, not // the view's: leaving the conversation keeps it, opening it again diff --git a/interfaces/ImsdMessages.Ui-App.cppm b/interfaces/ImsdMessages.Ui-App.cppm index e62a5b6..1291d83 100644 --- a/interfaces/ImsdMessages.Ui-App.cppm +++ b/interfaces/ImsdMessages.Ui-App.cppm @@ -86,6 +86,7 @@ export namespace ImsdMessages::Ui { hitMessages.clear(); textSinceFrame.clear(); frameFailed = false; + keyboardGone = false; } 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 // 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 - // 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() { TextInputState want; if (!sheet) { if (fresh) want = { true, fresh->fieldLayout.caret, Crafter::TextInputPurpose::Phone }; else if (view && view->compose.focused) want = { true, view->composeLayout.caret, Crafter::TextInputPurpose::Normal }; } + const float height = static_cast(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; - 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"); // Turning the input off ends the composition on the input // method's side; the field keeps the word it showed. if (!want.active && lastTextInput.active) CommitComposition(); lastTextInput = want; + toldHeight = height; + keyboardGone = false; canvas->Window().SetTextInput(want.active, static_cast(want.caret.x), static_cast(want.caret.y), static_cast(want.caret.w), static_cast(want.caret.h), want.purpose); } @@ -1043,6 +1058,17 @@ export namespace ImsdMessages::Ui { 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 // field has the focus and the window is up (without one nothing // draws it; Show starts the blink again). @@ -1148,11 +1174,13 @@ export namespace ImsdMessages::Ui { } else if (*id == Hit::newField) { if (!fresh) return; TapField(fresh->field, fresh->fieldLayout, at); + AskKeyboard(); ShowCaret(); } else if (*id == Hit::compose) { if (!view) return; view->compose.focused = true; TapField(view->compose, view->composeLayout, at); + AskKeyboard(); ShowCaret(); } else if (*id == Hit::send) { SendDraft(); @@ -1237,6 +1265,8 @@ export namespace ImsdMessages::Ui { std::optional> preeditSub; std::optional> deleteSub; 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> resizeSub; std::optional> closeSub; std::optional> seenSub; diff --git a/tests/Edit/main.cpp b/tests/Edit/main.cpp index a91ed06..c73faea 100644 --- a/tests/Edit/main.cpp +++ b/tests/Edit/main.cpp @@ -143,6 +143,19 @@ int main() { 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"); + // --- 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 // next opening. {