on xenial help sign in
Move power key long press functionality to PhysicalKeysFilter from dialogs
6dc418dJosh Arenson
qml/Components/Dialogs.qml+5−33
@@ -24,42 +24,14 @@ import Ubuntu.Components.Popups 0.1
24Item {
24Item {
25 id: root
25 id: root
26
26
27− function onPowerKeyPressed() {
28− // FIXME: event.isAutoRepeat is always false on Nexus 4.
29− // So we use powerKeyTimer.running to avoid the PowerOff key repeat
30− // https://launchpad.net/bugs/1349416
31− if (!powerKeyTimer.running) {
32− powerKeyTimer.restart();
33− }
34− }
35−
36− function onPowerKeyReleased() {
37− powerKeyTimer.stop();
38− }
39−
40 signal powerOffClicked();
27 signal powerOffClicked();
41
28
42− QtObject {
43− id: d // private stuff
44−
45− property bool dialogShown: false
29+ property bool dialogShown: false
46−
47− function showPowerDialog() {
48− if (!d.dialogShown) {
49− d.dialogShown = true;
50− PopupUtils.open(powerDialog, root, {"z": root.z});
51− }
52− }
53− }
54−
55− Timer {
56− id: powerKeyTimer
57− interval: 2000
58− repeat: false
59− triggeredOnStart: false
60
30
61− onTriggered: {
31+ function showPowerDialog() {
62− d.showPowerDialog();
32+ if (!d.dialogShown) {
33+ d.dialogShown = true;
34+ PopupUtils.open(powerDialog, root, {"z": root.z});
63 }
35 }
64 }
36 }
65
37
qml/Components/PhysicalKeysFilter.qml+21−6
@@ -34,10 +34,10 @@ QtObject {
34 id: root
34 id: root
35
35
36 signal bothVolumeKeysPressed();
36 signal bothVolumeKeysPressed();
37− signal powerPressed()
37+ signal powerKeyLongPress();
38− signal screenshotPressed()
38+ signal screenshotPressed();
39− signal volumeDownPressed()
39+ signal volumeDownPressed();
40− signal volumeUpPressed()
40+ signal volumeUpPressed();
41
41
42 property bool eventAccepted: false
42 property bool eventAccepted: false
43
43
@@ -48,11 +48,25 @@ QtObject {
48 property bool volumeDownKeyPressed: false
48 property bool volumeDownKeyPressed: false
49 property bool volumeUpKeyPressed: false
49 property bool volumeUpKeyPressed: false
50
50
51+ // FIXME: event.isAutoRepeat is always false on Nexus 4.
52+ // So we use powerKeyTimer.running to avoid the PowerOff key repeat
53+ // https://launchpad.net/bugs/1349416
54+ property variant pklpt: Timer {
55+ id: powerKeyLongPressTimer
56+
57+ interval: 2000
58+ repeat: false
59+ triggeredOnStart: false
60+
61+ onTriggered: powerKeyLongPress();
62+ }
63+
51 function onKeyPressed(key) {
64 function onKeyPressed(key) {
52 /* Determine what key was pressed */
65 /* Determine what key was pressed */
53−
54 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
66 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
55− powerPressed();
67+ if (!powerKeyLongPressTimer.running) {
68+ powerKeyLongPressTimer.restart();
69+ }
56 eventAccepted = true;
70 eventAccepted = true;
57 powerKeyPressed = true;
71 powerKeyPressed = true;
58 } else if (key == Qt.Key_VolumeDown) {
72 } else if (key == Qt.Key_VolumeDown) {
@@ -86,6 +100,7 @@ QtObject {
86
100
87 function onKeyReleased(key) {
101 function onKeyReleased(key) {
88 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
102 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
103+ powerKeyLongPressTimer.stop();
89 eventAccepted = true;
104 eventAccepted = true;
90 powerKeyPressed = false;
105 powerKeyPressed = false;
91 aPowerKeyWasReleased = true;
106 aPowerKeyWasReleased = true;
qml/Shell.qml+5−1
@@ -157,7 +157,6 @@ Item {
157 PhysicalKeysFilter {
157 PhysicalKeysFilter {
158 id: physicalKeysFilter
158 id: physicalKeysFilter
159
159
160− onPowerPressed: dialogs.onPowerKeyPressed();
161 onVolumeDownPressed: volumeControl.volumeDown()
160 onVolumeDownPressed: volumeControl.volumeDown()
162 onVolumeUpPressed: volumeControl.volumeUp()
161 onVolumeUpPressed: volumeControl.volumeUp()
163 onScreenshotPressed: screenGrabber.capture()
162 onScreenshotPressed: screenGrabber.capture()
@@ -804,6 +803,11 @@ Item {
804 shutdownFadeOutRectangle.visible = true;
803 shutdownFadeOutRectangle.visible = true;
805 shutdownFadeOut.start();
804 shutdownFadeOut.start();
806 }
805 }
806+
807+ Connections {
808+ target: physicalKeysFilter
809+ onPowerKeyLongPress: dialogs.showPowerDialog();
810+ }
807 }
811 }
808
812
809 EdgeDemo {
813 EdgeDemo {
tests/qmltests/Components/tst_PhysicalKeysFilter.qml+6−9
@@ -25,7 +25,7 @@ import Unity.Test 0.1 as UT
25Item {
25Item {
26
26
27 property int onBothVolumeKeysPressedCount: 0
27 property int onBothVolumeKeysPressedCount: 0
28− property int onPowerPressedCount: 0
28+ property int onPowerKeyLongPressCount: 0
29 property int onScreenshotPressedCount: 0
29 property int onScreenshotPressedCount: 0
30 property int onVolumeDownPressedCount: 0
30 property int onVolumeDownPressedCount: 0
31 property int onVolumeUpPressedCount: 0
31 property int onVolumeUpPressedCount: 0
@@ -34,7 +34,7 @@ Item {
34 id: physicalKeysFilter
34 id: physicalKeysFilter
35
35
36 onBothVolumeKeysPressed: onBothVolumeKeysPressedCount += 1
36 onBothVolumeKeysPressed: onBothVolumeKeysPressedCount += 1
37− onPowerPressed: onPowerPressedCount += 1
37+ onPowerKeyLongPress: onPowerKeyLongPressCount += 1
38 onScreenshotPressed: onScreenshotPressedCount += 1
38 onScreenshotPressed: onScreenshotPressedCount += 1
39 onVolumeDownPressed: onVolumeDownPressedCount += 1
39 onVolumeDownPressed: onVolumeDownPressedCount += 1
40 onVolumeUpPressed: onVolumeUpPressedCount += 1
40 onVolumeUpPressed: onVolumeUpPressedCount += 1
@@ -51,7 +51,7 @@ Item {
51 physicalKeysFilter.onKeyReleased(Qt.Key_VolumeUp);
51 physicalKeysFilter.onKeyReleased(Qt.Key_VolumeUp);
52
52
53 onBothVolumeKeysPressedCount = 0;
53 onBothVolumeKeysPressedCount = 0;
54− onPowerPressedCount = 0;
54+ onPowerKeyLongPressCount = 0;
55 onScreenshotPressedCount = 0;
55 onScreenshotPressedCount = 0;
56 onVolumeDownPressedCount = 0;
56 onVolumeDownPressedCount = 0;
57 onVolumeUpPressedCount = 0;
57 onVolumeUpPressedCount = 0;
@@ -68,24 +68,21 @@ Item {
68 compare(onBothVolumeKeysPressedCount, 1);
68 compare(onBothVolumeKeysPressedCount, 1);
69 }
69 }
70
70
71− function test_power_button() {
71+ function test_long_press_power_button() {
72 physicalKeysFilter.onKeyPressed(Qt.Key_PowerDown);
72 physicalKeysFilter.onKeyPressed(Qt.Key_PowerDown);
73− compare(onPowerPressedCount, 1);
73+ wait(3000);
74
74
75− sleep(3);
75+ compare(onPowerKeyLongPressCount, 1);
76− compare(onPowerPressedCount, 1);
77 }
76 }
78
77
79 function test_screenshot_buttons() {
78 function test_screenshot_buttons() {
80 physicalKeysFilter.onKeyPressed(Qt.Key_PowerDown);
79 physicalKeysFilter.onKeyPressed(Qt.Key_PowerDown);
81 physicalKeysFilter.onKeyPressed(Qt.Key_VolumeDown);
80 physicalKeysFilter.onKeyPressed(Qt.Key_VolumeDown);
82
81
83− compare(onPowerPressedCount, 1);
84 compare(onScreenshotPressedCount, 1);
82 compare(onScreenshotPressedCount, 1);
85 compare(onVolumeDownPressedCount, 0);
83 compare(onVolumeDownPressedCount, 0);
86
84
87 sleep(3);
85 sleep(3);
88− compare(onPowerPressedCount, 1);
89 compare(onScreenshotPressedCount, 1);
86 compare(onScreenshotPressedCount, 1);
90 compare(onVolumeDownPressedCount, 0);
87 compare(onVolumeDownPressedCount, 0);
91 }
88 }