From a39bf87f4f0ba3e03e9fdfb27a8ff3e874091529 Mon Sep 17 00:00:00 2001 From: Don Gagne Date: Fri, 24 Mar 2023 11:55:28 -0700 Subject: [PATCH] Add keyboard support to SliderSwitch --- src/FlightDisplay/GuidedActionConfirm.qml | 8 +++- src/QmlControls/SliderSwitch.qml | 68 +++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/src/FlightDisplay/GuidedActionConfirm.qml b/src/FlightDisplay/GuidedActionConfirm.qml index aba993c..b01dce3 100644 --- a/src/FlightDisplay/GuidedActionConfirm.qml +++ b/src/FlightDisplay/GuidedActionConfirm.qml @@ -40,6 +40,12 @@ Rectangle { Component.onCompleted: guidedController.confirmDialog = this + onVisibleChanged: { + if (visible) { + slider.focus = true + } + } + onHideTriggerChanged: { if (hideTrigger) { confirmCancelled() @@ -104,7 +110,7 @@ Rectangle { SliderSwitch { id: slider - confirmText: qsTr("Slide to confirm") + confirmText: ScreenTools.isMobile ? qsTr("Slide to confirm") : qsTr("Slide or hold spacebar") Layout.fillWidth: true onAccept: { diff --git a/src/QmlControls/SliderSwitch.qml b/src/QmlControls/SliderSwitch.qml index 112223a..f06575e 100644 --- a/src/QmlControls/SliderSwitch.qml +++ b/src/QmlControls/SliderSwitch.qml @@ -1,11 +1,11 @@ -import QtQuick 2.3 -import QtQuick.Controls 1.2 +import QtQuick 2.15 +import QtQuick.Controls 2.15 import QGroundControl.ScreenTools 1.0 import QGroundControl.Palette 1.0 /// The SliderSwitch control implements a sliding switch control similar to the power off -/// control on an iPhone. +/// control on an iPhone. It supports holding the space bar to slide the switch. Rectangle { id: _root implicitWidth: label.contentWidth + (_diameter * 2.5) + (_border * 4) @@ -18,15 +18,46 @@ Rectangle { property string confirmText ///< Text for slider property alias fontPointSize: label.font.pointSize ///< Point size for text - property real _border: 4 - property real _diameter: height - (_border * 2) + property real _border: 4 + property real _diameter: height - (_border * 2) + property real _dragStartX: _border + property real _dragStopX: _root.width - (_diameter + _border) + property bool _waitingForLastAutoRepeat: false + + Keys.onSpacePressed: { + if (visible && event.modifiers === Qt.NoModifier && event.isAutoRepeat && !sliderDragArea.drag.active) { + event.accepted = true + if (_waitingForLastAutoRepeat) { + resetSpaceBarSliding() + accept() + } else { + sliderAnimation.start() + spaceBarTimout.restart() + } + } + } + + function resetSpaceBarSliding() { + _waitingForLastAutoRepeat = false + spaceBarTimout.stop() + slider.reset() + } + + Timer { + id: spaceBarTimout + interval: 200 + repeat: false + onTriggered: _root.resetSpaceBarSliding() + } QGCPalette { id: qgcPal; colorGroupEnabled: true } QGCLabel { id: label - anchors.horizontalCenter: parent.horizontalCenter + x: _diameter + _border + width: parent.width - x anchors.verticalCenter: parent.verticalCenter + horizontalAlignment: Text.AlignHCenter text: confirmText color: qgcPal.buttonText } @@ -53,6 +84,19 @@ Rectangle { source: "/res/ArrowRight.svg" } + PropertyAnimation on x { + id: sliderAnimation + duration: 1500 + from: _dragStartX + to: _dragStopX + running: false + onFinished: _waitingForLastAutoRepeat = true + } + + function reset() { + slider.x = _border + sliderAnimation.stop() + } } QGCMouseArea { @@ -61,20 +105,18 @@ Rectangle { fillItem: slider drag.target: slider drag.axis: Drag.XAxis - drag.minimumX: _border - drag.maximumX: _maxXDrag + drag.minimumX: _dragStartX + drag.maximumX: _dragStopX preventStealing: true - property real _maxXDrag: _root.width - (_diameter + _border) - property bool dragActive: drag.active - property real _dragOffset: 1 + property bool dragActive: drag.active onDragActiveChanged: { if (!sliderDragArea.drag.active) { - if (slider.x > _maxXDrag - _border) { + if (slider.x > _dragStopX - _border) { _root.accept() } - slider.x = _border + slider.reset() } } }