Qt: fix the clear button in the search boxes

On Android the keyboard keeps the word being typed out of TextInput.text
until it is committed, so the clear button didn't show for it and clearing
left the composing word behind. The box now filters and shows the clear
button by displayText, and clearing resets the input method first. The
button is a 44px target that keeps its press.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiQsCq11CwCPnKPp9D
This commit is contained in:
Claude committed 2026-11-02 05:27:37 +00:00
1 parent 0367ff5e73
commit e050f52417
4 files changed
+42 -17

No files matched your search

+2 -2
View File
@@ -16,7 +16,7 @@ Item {
property alias query: search.text property alias query: search.text
readonly property real edge: y + height // the drawer's bottom edge, for covering widgets readonly property real edge: y + height // the drawer's bottom edge, for covering widgets
readonly property var shown: { readonly property var shown: {
const q = search.text.toLowerCase(); const q = search.shown.toLowerCase();
const out = []; const out = [];
for (let i = 0; i < apps.length; i++) for (let i = 0; i < apps.length; i++)
if (!q || apps[i].label.toLowerCase().indexOf(q) >= 0) out.push(i); if (!q || apps[i].label.toLowerCase().indexOf(q) >= 0) out.push(i);
@@ -34,7 +34,7 @@ Item {
Behavior on y { NumberAnimation { duration: 220; easing.type: Easing.OutCubic } } Behavior on y { NumberAnimation { duration: 220; easing.type: Easing.OutCubic } }
function openFirst() { function openFirst() {
if (shown.length > 0 && search.text.length > 0) openApp(shown[0]); if (shown.length > 0 && search.shown.length > 0) openApp(shown[0]);
} }
onOpenChanged: { onOpenChanged: {
+1 -1
View File
@@ -264,7 +264,7 @@ Window {
Qt.callLater(() => { providers = Host.widgetProviders(); wloading = false; }); Qt.callLater(() => { providers = Host.widgetProviders(); wloading = false; });
} }
readonly property var shownProviders: { readonly property var shownProviders: {
const q = widgetSearch.text.toLowerCase(), out = []; const q = widgetSearch.shown.toLowerCase(), out = [];
for (let i = 0; i < providers.length; i++) { for (let i = 0; i < providers.length; i++) {
const p = providers[i]; const p = providers[i];
if (!q || p.label.toLowerCase().indexOf(q) >= 0 || p.app.toLowerCase().indexOf(q) >= 0) out.push(i); if (!q || p.label.toLowerCase().indexOf(q) >= 0 || p.app.toLowerCase().indexOf(q) >= 0) out.push(i);
+29 -14
View File
@@ -1,11 +1,14 @@
import QtQuick import QtQuick
// A rounded search box. Enter is "accepted"; the text is "text". // A rounded search box. Enter is "accepted"; the text is "text", and "shown" is what
// the box shows, including a word the keyboard is still composing (Android keyboards
// keep it out of "text" until it is committed).
Rectangle { Rectangle {
id: root id: root
property alias text: input.text property alias text: input.text
property alias placeholder: hint.text property alias placeholder: hint.text
property alias input: input property alias input: input
readonly property string shown: input.displayText
property real fontSize: 16 property real fontSize: 16
signal accepted() signal accepted()
signal escaped() signal escaped()
@@ -16,13 +19,19 @@ Rectangle {
border.width: 1 border.width: 1
function focusNow() { input.forceActiveFocus(); Qt.inputMethod.show() } function focusNow() { input.forceActiveFocus(); Qt.inputMethod.show() }
// Empty the box, including a word still being composed, and keep typing in it.
function clearText() {
Qt.inputMethod.reset();
input.clear();
input.forceActiveFocus();
}
TextInput { TextInput {
id: input id: input
anchors.left: parent.left anchors.left: parent.left
anchors.right: clear.left anchors.right: clear.left
anchors.leftMargin: 20 anchors.leftMargin: 20
anchors.rightMargin: 8 anchors.rightMargin: 0
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
color: "white" color: "white"
selectionColor: Qt.rgba(1, 1, 1, .35) selectionColor: Qt.rgba(1, 1, 1, .35)
@@ -37,24 +46,30 @@ Rectangle {
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
color: Qt.rgba(1, 1, 1, .6) color: Qt.rgba(1, 1, 1, .6)
font.pixelSize: root.fontSize font.pixelSize: root.fontSize
visible: input.text.length === 0 && !input.preeditText visible: input.displayText.length === 0
} }
} }
Text { // The clear button: a finger-sized target at the right end of the box.
Item {
id: clear id: clear
objectName: "searchClear"
anchors.right: parent.right anchors.right: parent.right
anchors.rightMargin: 14 anchors.top: parent.top
anchors.verticalCenter: parent.verticalCenter anchors.bottom: parent.bottom
width: visible ? 20 : 0 width: visible ? 44 : 0
visible: input.text.length > 0 visible: input.displayText.length > 0
text: "✕" z: 1
color: Qt.rgba(1, 1, 1, .7) Text {
font.pixelSize: 14 anchors.centerIn: parent
horizontalAlignment: Text.AlignHCenter text: "✕"
color: Qt.rgba(1, 1, 1, .7)
font.pixelSize: 14
}
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
anchors.margins: -10 preventStealing: true
onClicked: { input.text = ""; root.escaped() } onPressed: (m) => m.accepted = true
onClicked: { root.clearText(); root.escaped() }
} }
} }
} }
+10
View File
@@ -249,6 +249,16 @@ int main(int argc, char *argv[]) {
snap("drawer"); snap("drawer");
js("drawer.query = 'ma'"); js("drawer.query = 'ma'");
eq(js("drawer.shown.map(i => apps[i].label).join(',')"), "Gmail,Maps", "the search filters the list"); eq(js("drawer.shown.map(i => apps[i].label).join(',')"), "Gmail,Maps", "the search filters the list");
{
// the clear button empties the search and shows every app again
auto *clear = win->findChild<QQuickItem *>("searchClear");
check(clear && clear->isVisible(), "the search shows a clear button");
if (clear) tap(clear->mapToScene(QPointF(clear->width() / 2, clear->height() / 2)));
eq(js("drawer.query"), "", "tapping it clears the search");
eq(js("drawer.shown.length === apps.length"), true, "and every app shows again");
check(clear && !clear->isVisible(), "the clear button goes");
js("drawer.query = 'ma'");
}
js("drawer.openFirst()"); js("drawer.openFirst()");
settle(300); settle(300);
eq(js("notice"), "Open com.example.gmail/.MainActivity", "opening the first match launches it"); eq(js("notice"), "Open com.example.gmail/.MainActivity", "opening the first match launches it");