on xenial help sign in
Privitize internal properties in PhysicalKeysMapper
ffe76ccJosh Arenson
qml/Components/PhysicalKeysMapper.qml+23−28
@@ -30,63 +30,60 @@ import QtQuick 2.0
30
30
31*/
31*/
32
32
33−QtObject {
33+Item {
34 id: root
34 id: root
35
35
36− signal bothVolumeKeysPressed();
37 signal powerKeyLongPress();
36 signal powerKeyLongPress();
38 signal screenshotPressed();
37 signal screenshotPressed();
39 signal volumeDownPressed();
38 signal volumeDownPressed();
40 signal volumeUpPressed();
39 signal volumeUpPressed();
41
40
42− property bool aPowerKeyWasReleased: true
41+ QtObject {
43− property bool powerKeyPressed: false
42+ id: d
44
43
45− property bool aVolumeKeyWasReleased: true
44+ property bool aPowerKeyWasReleased: true
45+ property bool powerKeyPressed: false
46+
46− property bool volumeDownKeyPressed: false
47+ property bool volumeDownKeyPressed: false
47− property bool volumeUpKeyPressed: false
48+ property bool volumeUpKeyPressed: false
49+ }
48
50
49 // FIXME: event.isAutoRepeat is always false on Nexus 4.
51 // FIXME: event.isAutoRepeat is always false on Nexus 4.
50 // So we use powerKeyTimer.running to avoid the PowerOff key repeat
52 // So we use powerKeyTimer.running to avoid the PowerOff key repeat
51 // https://launchpad.net/bugs/1349416
53 // https://launchpad.net/bugs/1349416
52− property variant pklpt: Timer {
54+ Timer {
53 id: powerKeyLongPressTimer
55 id: powerKeyLongPressTimer
54
56
55 interval: 2000
57 interval: 2000
56 repeat: false
58 repeat: false
57 triggeredOnStart: false
59 triggeredOnStart: false
58−
59 onTriggered: powerKeyLongPress();
60 onTriggered: powerKeyLongPress();
60 }
61 }
61
62
63+
62 function onKeyPressed(key) {
64 function onKeyPressed(key) {
63 /* Determine what key was pressed */
65 /* Determine what key was pressed */
64 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
66 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
65 if (!powerKeyLongPressTimer.running) {
67 if (!powerKeyLongPressTimer.running) {
66 powerKeyLongPressTimer.restart();
68 powerKeyLongPressTimer.restart();
67 }
69 }
68− powerKeyPressed = true;
70+ d.powerKeyPressed = true;
69 } else if (key == Qt.Key_VolumeDown) {
71 } else if (key == Qt.Key_VolumeDown) {
70− volumeDownKeyPressed = true;
72+ d.volumeDownKeyPressed = true;
71 } else if (key == Qt.Key_VolumeUp) {
73 } else if (key == Qt.Key_VolumeUp) {
72− volumeUpKeyPressed = true;
74+ d.volumeUpKeyPressed = true;
73 }
75 }
74
76
75 /* Determine how to handle it */
77 /* Determine how to handle it */
76− if (volumeDownKeyPressed && volumeUpKeyPressed) {
77− if (aVolumeKeyWasReleased) {
78− bothVolumeKeysPressed();
79− }
80− aVolumeKeyWasReleased = false;
81− } else if (volumeDownKeyPressed) {
78+ if (d.volumeDownKeyPressed) {
82− if (powerKeyPressed && aPowerKeyWasReleased) {
79+ if (d.powerKeyPressed && d.aPowerKeyWasReleased) {
83 screenshotPressed();
80 screenshotPressed();
84− aPowerKeyWasReleased = false;
81+ d.aPowerKeyWasReleased = false;
85 // Don't emit volumeDownPressed if power key is held
82 // Don't emit volumeDownPressed if power key is held
86− } else if (aPowerKeyWasReleased){
83+ } else if (d.aPowerKeyWasReleased){
87 volumeDownPressed();
84 volumeDownPressed();
88 }
85 }
89− } else if (volumeUpKeyPressed) {
86+ } else if (d.volumeUpKeyPressed) {
90 volumeUpPressed();
87 volumeUpPressed();
91 }
88 }
92 }
89 }
@@ -94,14 +91,12 @@ QtObject {
94 function onKeyReleased(key) {
91 function onKeyReleased(key) {
95 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
92 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
96 powerKeyLongPressTimer.stop();
93 powerKeyLongPressTimer.stop();
97− powerKeyPressed = false;
94+ d.powerKeyPressed = false;
98− aPowerKeyWasReleased = true;
95+ d.aPowerKeyWasReleased = true;
99 } else if (key == Qt.Key_VolumeDown) {
96 } else if (key == Qt.Key_VolumeDown) {
100− volumeDownKeyPressed = false;
97+ d.volumeDownKeyPressed = false;
101− aVolumeKeyWasReleased = false;
102 } else if (key == Qt.Key_VolumeUp) {
98 } else if (key == Qt.Key_VolumeUp) {
103− volumeUpKeyPressed = false;
99+ d.volumeUpKeyPressed = false;
104− aVolumeKeyWasReleased = true;
105 }
100 }
106 }
101 }
107}
102}
tests/qmltests/Components/tst_PhysicalKeysMapper.qml−14
@@ -21,7 +21,6 @@ import "../../../qml/Components"
21TestCase {
21TestCase {
22 name: "PhysicalKeysMapper"
22 name: "PhysicalKeysMapper"
23
23
24− property int onBothVolumeKeysPressedCount: 0
25 property int onPowerKeyLongPressCount: 0
24 property int onPowerKeyLongPressCount: 0
26 property int onScreenshotPressedCount: 0
25 property int onScreenshotPressedCount: 0
27 property int onVolumeDownPressedCount: 0
26 property int onVolumeDownPressedCount: 0
@@ -30,7 +29,6 @@ TestCase {
30 PhysicalKeysMapper {
29 PhysicalKeysMapper {
31 id: physicalKeysMapper
30 id: physicalKeysMapper
32
31
33− onBothVolumeKeysPressed: onBothVolumeKeysPressedCount += 1
34 onPowerKeyLongPress: onPowerKeyLongPressCount += 1
32 onPowerKeyLongPress: onPowerKeyLongPressCount += 1
35 onScreenshotPressed: onScreenshotPressedCount += 1
33 onScreenshotPressed: onScreenshotPressedCount += 1
36 onVolumeDownPressed: onVolumeDownPressedCount += 1
34 onVolumeDownPressed: onVolumeDownPressedCount += 1
@@ -43,24 +41,12 @@ TestCase {
43 physicalKeysMapper.onKeyReleased(Qt.Key_VolumeDown);
41 physicalKeysMapper.onKeyReleased(Qt.Key_VolumeDown);
44 physicalKeysMapper.onKeyReleased(Qt.Key_VolumeUp);
42 physicalKeysMapper.onKeyReleased(Qt.Key_VolumeUp);
45
43
46− onBothVolumeKeysPressedCount = 0;
47 onPowerKeyLongPressCount = 0;
44 onPowerKeyLongPressCount = 0;
48 onScreenshotPressedCount = 0;
45 onScreenshotPressedCount = 0;
49 onVolumeDownPressedCount = 0;
46 onVolumeDownPressedCount = 0;
50 onVolumeUpPressedCount = 0;
47 onVolumeUpPressedCount = 0;
51 }
48 }
52
49
53− function test_BothVolumeKeysSimultaneously() {
54− physicalKeysMapper.onKeyPressed(Qt.Key_VolumeDown);
55− physicalKeysMapper.onKeyPressed(Qt.Key_VolumeUp);
56−
57− compare(onBothVolumeKeysPressedCount, 1);
58−
59− // Simulate holding the keys down
60− wait(3000);
61− compare(onBothVolumeKeysPressedCount, 1);
62− }
63−
64 function test_LongPressPowerButton() {
50 function test_LongPressPowerButton() {
65 physicalKeysMapper.onKeyPressed(Qt.Key_PowerDown);
51 physicalKeysMapper.onKeyPressed(Qt.Key_PowerDown);
66 wait(3000);
52 wait(3000);