applets/clipboard: Make Up/Down arrow keys work in ClipboardPage

ListView will absorb the key events, so `Keys.onPressed` in
ClipboardPage will not receive the event. Instead we should connect
those key events to `arrowKeyPressed()`.

BUG: 448811
wilder-5.25
Fushan Wen 4 years ago
parent 79344a3b26
commit 95a267d582
No known key found for this signature in database
GPG Key ID: 2E48D1487C91DCAA
  1. 64
      applets/clipboard/contents/ui/ClipboardPage.qml

@ -16,12 +16,6 @@ import org.kde.plasma.extras 2.0 as PlasmaExtras
Menu {
id: clipboardMenu
Keys.onPressed: {
function goToCurrent() {
clipboardMenu.view.positionViewAtIndex(clipboardMenu.view.currentIndex, ListView.Contain);
if (clipboardMenu.view.currentIndex != -1) {
clipboardMenu.view.currentItem.forceActiveFocus();
}
}
function forwardToFilter() {
// filter.text += event.text wil break if the key is backspace
if (event.key === Qt.Key_Backspace && filter.text == "") {
@ -44,24 +38,6 @@ Menu {
return;
}
switch(event.key) {
case Qt.Key_Up: {
if (clipboardMenu.view.currentIndex == 0) {
clipboardMenu.view.currentIndex = -1;
filter.forceActiveFocus();
filter.selectAll();
} else {
clipboardMenu.view.decrementCurrentIndex();
goToCurrent();
}
event.accepted = true;
break;
}
case Qt.Key_Down: {
clipboardMenu.view.incrementCurrentIndex();
goToCurrent();
event.accepted = true;
break;
}
case Qt.Key_Enter:
case Qt.Key_Return: {
if (clipboardMenu.view.currentIndex >= 0) {
@ -118,6 +94,8 @@ Menu {
inputMethodHints: Qt.ImhNoPredictiveText
Keys.onUpPressed: clipboardMenu.arrowKeyPressed(event)
Keys.onDownPressed: clipboardMenu.arrowKeyPressed(event)
Connections {
target: main
@ -177,4 +155,42 @@ Menu {
clipboardSource.service(uuid, "action")
clipboardMenu.view.currentIndex = 0
}
Component.onCompleted: {
// Intercept up/down key to prevent ListView from accepting the key event.
clipboardMenu.view.Keys.upPressed.connect(clipboardMenu.arrowKeyPressed);
clipboardMenu.view.Keys.downPressed.connect(clipboardMenu.arrowKeyPressed);
}
function goToCurrent() {
clipboardMenu.view.positionViewAtIndex(clipboardMenu.view.currentIndex, ListView.Contain);
if (clipboardMenu.view.currentIndex !== -1) {
clipboardMenu.view.currentItem.forceActiveFocus();
}
}
function arrowKeyPressed(event) {
switch (event.key) {
case Qt.Key_Up: {
if (clipboardMenu.view.currentIndex === 0) {
clipboardMenu.view.currentIndex = -1;
filter.forceActiveFocus();
filter.selectAll();
} else {
clipboardMenu.view.decrementCurrentIndex();
goToCurrent();
}
event.accepted = true;
break;
}
case Qt.Key_Down: {
clipboardMenu.view.incrementCurrentIndex();
goToCurrent();
event.accepted = true;
break;
}
default:
break;
}
}
}

Loading…
Cancel
Save