HBox: Add arrow keys navigation

Also rename enterButtonTarget to clickButtonOnEnter
This commit is contained in:
miruka 2019-09-09 07:41:48 -04:00
parent 7f89604789
commit e5893c5569
4 changed files with 39 additions and 14 deletions

View File

@ -1,5 +1,6 @@
import QtQuick 2.12 import QtQuick 2.12
import QtQuick.Layouts 1.12 import QtQuick.Layouts 1.12
import "../utils.js" as Utils
Rectangle { Rectangle {
id: interfaceBox id: interfaceBox
@ -9,6 +10,10 @@ Rectangle {
) )
implicitHeight: childrenRect.height implicitHeight: childrenRect.height
Keys.onReturnPressed: if (clickButtonOnEnter) enterClickButton()
Keys.onEnterPressed: Keys.onReturnPressed(event)
property real multiplyWidth: 1.0 property real multiplyWidth: 1.0
property real multiplyHorizontalSpacing: 1.5 property real multiplyHorizontalSpacing: 1.5
property real multiplyVerticalSpacing: 1.5 property real multiplyVerticalSpacing: 1.5
@ -20,21 +25,21 @@ Rectangle {
property int verticalSpacing: theme.spacing * multiplyVerticalSpacing property int verticalSpacing: theme.spacing * multiplyVerticalSpacing
property alias title: interfaceTitle.text property alias title: interfaceTitle.text
property alias buttonModel: interfaceButtonsRepeater.model property alias buttonModel: buttonRepeater.model
property var buttonCallbacks: [] property var buttonCallbacks: []
property string enterButtonTarget: "" property string focusButton: ""
property string clickButtonOnEnter: ""
default property alias body: interfaceBody.data default property alias body: interfaceBody.data
function clickEnterButtonTarget() {
function enterClickButton() {
for (let i = 0; i < buttonModel.length; i++) { for (let i = 0; i < buttonModel.length; i++) {
let btn = interfaceButtonsRepeater.itemAt(i) let btn = buttonRepeater.itemAt(i)
if (btn.enabled && btn.name === enterButtonTarget) btn.clicked() if (btn.enabled && btn.name === clickButtonOnEnter) btn.clicked()
} }
} }
Keys.onReturnPressed: clickEnterButtonTarget()
Keys.onEnterPressed: clickEnterButtonTarget()
HColumnLayout { HColumnLayout {
id: mainColumn id: mainColumn
@ -69,12 +74,10 @@ Rectangle {
visible: buttonModel.length > 0 visible: buttonModel.length > 0
Repeater { Repeater {
id: interfaceButtonsRepeater id: buttonRepeater
model: [] model: []
HButton { HButton {
property string name: modelData.name
id: button id: button
text: modelData.text text: modelData.text
icon.name: modelData.iconName || "" icon.name: modelData.iconName || ""
@ -91,10 +94,31 @@ Rectangle {
enabled: (modelData.enabled == undefined ? enabled: (modelData.enabled == undefined ?
true : modelData.enabled) && true : modelData.enabled) &&
! button.loading ! button.loading
onClicked: buttonCallbacks[modelData.name](button) onClicked: buttonCallbacks[modelData.name](button)
Keys.onLeftPressed: previous.forceActiveFocus()
Keys.onUpPressed: previous.forceActiveFocus()
Keys.onRightPressed: next.forceActiveFocus()
Keys.onDownPressed: next.forceActiveFocus()
Keys.onReturnPressed: if (button.enabled) button.clicked()
Keys.onEnterPressed: Keys.onReturnPressed(event)
Component.onCompleted:
if (modelData.name == focusButton) forceActiveFocus()
Layout.fillWidth: true Layout.fillWidth: true
Layout.preferredHeight: theme.baseElementsHeight Layout.preferredHeight: theme.baseElementsHeight
property string name: modelData.name
property Item next: buttonRepeater.itemAt(
Utils.numberWrapAt(index + 1, buttonRepeater.count),
)
property Item previous: buttonRepeater.itemAt(
Utils.numberWrapAt(index - 1, buttonRepeater.count),
)
} }
} }
} }

View File

@ -15,7 +15,8 @@ HPopup {
property bool okClicked: false property bool okClicked: false
box.enterButtonTarget: "ok" box.focusButton: "ok"
box.clickButtonOnEnter: "ok"
box.buttonModel: [ box.buttonModel: [
{ name: "ok", text: qsTr("OK"), iconName: "ok" }, { name: "ok", text: qsTr("OK"), iconName: "ok" },
{ name: "cancel", text: qsTr("Cancel"), iconName: "cancel" }, { name: "cancel", text: qsTr("Cancel"), iconName: "cancel" },

View File

@ -15,7 +15,7 @@ HPage {
multiplyWidth: 0.85 multiplyWidth: 0.85
title: qsTr("Sign in") title: qsTr("Sign in")
enterButtonTarget: "login" clickButtonOnEnter: "login"
buttonModel: [ buttonModel: [
{ name: "register", text: qsTr("Register"), enabled: false }, { name: "register", text: qsTr("Register"), enabled: false },

View File

@ -45,13 +45,13 @@ function isEmptyObject(obj) {
} }
function numberWrapAround(num, max) { function numberWrapAt(num, max) {
return num < 0 ? max + (num % max) : (num % max) return num < 0 ? max + (num % max) : (num % max)
} }
function hsluv(hue, saturation, lightness, alpha=1.0) { function hsluv(hue, saturation, lightness, alpha=1.0) {
hue = numberWrapAround(hue, 360) hue = numberWrapAt(hue, 360)
let rgb = py.callSync("hsluv", [hue, saturation, lightness]) let rgb = py.callSync("hsluv", [hue, saturation, lightness])
return Qt.rgba(rgb[0], rgb[1], rgb[2], alpha) return Qt.rgba(rgb[0], rgb[1], rgb[2], alpha)
} }