on xenial help sign in
Clear the search when the Drawer is closed

I ended up fighting with keyboard focus while writing this commit, so I had to refactor how the Launcher and Drawer handle keeping focus on the Drawer's search field. https://github.com/ubports/unity8/issues/110

commit1395d6d660d55946466a5e6ded7017caa2c809a8 authorDalton Durst <d@ltondur.st> dateTue 19 Mar 2019, 17:05 (8 years ago) parent7f172ec
2 files changed+32−17
qml/Launcher/Drawer.qml−7 qml/Launcher/Launcher.qml+32−10
qml/Launcher/Drawer.qml−7
@@ -35,12 +35,6 @@ FocusScope {
3535 property bool draggingHorizontally: false
3636 property int dragDistance: 0
3737
38− onFocusChanged: {
39− if (focus) {
40− searchField.selectAll();
41− }
42− }
43−
4438 function focusInput() {
4539 searchField.selectAll();
4640 searchField.focus = true;
@@ -93,7 +87,6 @@ FocusScope {
9387 objectName: "searchField"
9488 anchors { left: parent.left; top: parent.top; right: parent.right; margins: units.gu(1) }
9589 placeholderText: i18n.tr("Search…")
96− focus: true
9790
9891 KeyNavigation.down: sections
9992
qml/Launcher/Launcher.qml+32−10
@@ -176,7 +176,6 @@ FocusScope {
176176 superPressTimer.stop();
177177 superLongPressTimer.stop();
178178 root.focus = true;
179− drawer.focus = true;
180179 if (focusInputField) {
181180 drawer.focusInput();
182181 }
@@ -350,19 +349,26 @@ FocusScope {
350349 bottom: parent.bottom
351350 right: parent.left
352351 onRightMarginChanged: {
353− // Remove (and put back) the focus for the searchfield in
354− // order to hide the copy/paste popover when we move the drawer
355− var hadFocus = drawer.searchTextField.focus;
356− var oldSelectionStart = drawer.searchTextField.selectionStart;
357− var oldSelectionEnd = drawer.searchTextField.selectionEnd;
358− drawer.searchTextField.focus = false;
359− drawer.searchTextField.focus = hadFocus;
360− drawer.searchTextField.select(oldSelectionStart, oldSelectionEnd);
352+ if (fullyOpen && hadFocus) {
353+ // See drawer.onDraggingHorizontallyChanged below
354+ drawer.searchTextField.focus = hadFocus;
355+ drawer.searchTextField.select(oldSelectionStart, oldSelectionEnd);
356+ resetOldFocus();
357+ } else if (fullyClosed) {
358+ drawer.searchTextField.clear();
359+ resetOldFocus();
360+ }
361361 }
362362 }
363363 width: Math.min(root.width, units.gu(81))
364364 panelWidth: panel.width
365365 visible: x > -width
366+ property var fullyOpen: x === 0
367+ property var fullyClosed: x === -width
368+
369+ property var hadFocus: false
370+ property var oldSelectionStart: null
371+ property var oldSelectionEnd: null
366372
367373 Behavior on anchors.rightMargin {
368374 enabled: !dragArea.dragging && !launcherDragArea.drag.active && panel.animate && !drawer.draggingHorizontally
@@ -378,6 +384,12 @@ FocusScope {
378384 root.focus = false;
379385 }
380386
387+ function resetOldFocus() {
388+ hadFocus = false;
389+ oldSelectionStart = null;
390+ oldSelectionEnd = null;
391+ }
392+
381393 Keys.onEscapePressed: {
382394 root.hide()
383395 }
@@ -386,7 +398,15 @@ FocusScope {
386398 anchors.rightMargin = Math.max(-drawer.width, anchors.rightMargin + dragDistance);
387399 }
388400 onDraggingHorizontallyChanged: {
389− if (!draggingHorizontally) {
401+ if (draggingHorizontally) {
402+ // Remove (and put back using anchors.onRightMarginChanged) the
403+ // focus for the searchfield in order to hide the copy/paste
404+ // popover when we move the drawer
405+ hadFocus = drawer.searchTextField.focus;
406+ oldSelectionStart = drawer.searchTextField.selectionStart;
407+ oldSelectionEnd = drawer.searchTextField.selectionEnd;
408+ drawer.searchTextField.focus = false;
409+ } else {
390410 if (drawer.x < -units.gu(10)) {
391411 root.hide();
392412 } else {
@@ -572,6 +592,7 @@ FocusScope {
572592 PropertyChanges {
573593 target: drawer
574594 anchors.rightMargin: 0
595+ focus: false
575596 }
576597 },
577598 State {
@@ -591,6 +612,7 @@ FocusScope {
591612 PropertyChanges {
592613 target: drawer
593614 anchors.rightMargin: -drawer.width + root.x // so we never go past panelWidth, even when teased by tutorial
615+ focus: true
594616 }
595617 },
596618 State {