[vlc-devel] [PATCH 03/10] qml: factorize navigation key matching

Pierre Lamot pierre at videolabs.io
Mon Nov 18 18:14:06 CET 2019


---
 modules/gui/qt/Makefile.am                    |  1 +
 modules/gui/qt/qml/about/About.qml            |  3 +-
 .../gui/qt/qml/mediacenter/MCMainDisplay.qml  |  3 +-
 modules/gui/qt/qml/player/MiniPlayer.qml      |  5 +-
 modules/gui/qt/qml/player/Player.qml          |  1 +
 modules/gui/qt/qml/player/PlayerMenuItem.qml  |  2 +
 modules/gui/qt/qml/utils/ExpandGridView.qml   | 15 +++--
 modules/gui/qt/qml/utils/KeyHelper.js         | 64 +++++++++++++++++++
 .../gui/qt/qml/utils/KeyNavigableGridView.qml | 15 +++--
 .../gui/qt/qml/utils/KeyNavigableListView.qml | 19 +++---
 .../gui/qt/qml/utils/NavigableFocusScope.qml  | 11 ++--
 modules/gui/qt/vlc.qrc                        |  1 +
 12 files changed, 108 insertions(+), 32 deletions(-)
 create mode 100644 modules/gui/qt/qml/utils/KeyHelper.js

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 9abfa906b2..6985785cdb 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -585,6 +585,7 @@ libqt_plugin_la_QML = \
 	gui/qt/qml/dialogs/EditorTabButton.qml \
 	gui/qt/qml/dialogs/ToolbarEditorButtonList.qml \
 	gui/qt/qml/dialogs/ToolbarEditor.qml \
+	gui/qt/qml/utils/KeyHelper.js \
 	gui/qt/qml/utils/BusyIndicatorExt.qml \
 	gui/qt/qml/utils/DNDLabel.qml \
 	gui/qt/qml/utils/ToolTipArea.qml \
diff --git a/modules/gui/qt/qml/about/About.qml b/modules/gui/qt/qml/about/About.qml
index 1112cdfb75..3f52fcfe95 100644
--- a/modules/gui/qt/qml/about/About.qml
+++ b/modules/gui/qt/qml/about/About.qml
@@ -22,6 +22,7 @@ import org.videolan.vlc 0.1
 
 import "qrc:///style/"
 import "qrc:///utils/" as Utils
+import "qrc:///utils/KeyHelper.js" as KeyHelper
 
 Utils.NavigableFocusScope {
     id: root
@@ -139,7 +140,7 @@ Utils.NavigableFocusScope {
                     Layout.alignment:  Qt.AlignHCenter
 
                     Keys.onPressed:  {
-                        if (event.key === Qt.Key_Left) {
+                        if (KeyHelper.matchLeft(event)) {
                             backBtn.focus = true
                             event.accepted = true
                         }
diff --git a/modules/gui/qt/qml/mediacenter/MCMainDisplay.qml b/modules/gui/qt/qml/mediacenter/MCMainDisplay.qml
index 2e29a70d15..3d36b9ee4a 100644
--- a/modules/gui/qt/qml/mediacenter/MCMainDisplay.qml
+++ b/modules/gui/qt/qml/mediacenter/MCMainDisplay.qml
@@ -24,6 +24,7 @@ import org.videolan.vlc 0.1
 import "qrc:///style/"
 import "qrc:///qml/"
 import "qrc:///utils/" as Utils
+import "qrc:///utils/KeyHelper.js" as KeyHelper
 import "qrc:///playlist/" as PL
 import "qrc:///player/" as Player
 
@@ -187,7 +188,7 @@ Utils.NavigableFocusScope {
                                 defaultKeyAction(event, 0)
                         }
                         Keys.onReleased: {
-                            if (!event.accepted && (event.key === Qt.Key_Return || event.key === Qt.Key_Space)) {
+                            if (!event.accepted && KeyHelper.matchOk(event)) {
                                 event.accepted = true
                                 stackView.focus = true
                             }
diff --git a/modules/gui/qt/qml/player/MiniPlayer.qml b/modules/gui/qt/qml/player/MiniPlayer.qml
index 33fdfb2a4a..ccf4a39ae5 100644
--- a/modules/gui/qt/qml/player/MiniPlayer.qml
+++ b/modules/gui/qt/qml/player/MiniPlayer.qml
@@ -5,6 +5,7 @@ import QtQuick.Layouts 1.3
 import org.videolan.vlc 0.1
 
 import "qrc:///utils/" as Utils
+import "qrc:///utils/KeyHelper.js" as KeyHelper
 import "qrc:///style/"
 
 Utils.NavigableFocusScope {
@@ -73,12 +74,12 @@ Utils.NavigableFocusScope {
                 }
 
                 Keys.onPressed: {
-                    if (event.key === Qt.Key_Return || event.key === Qt.Key_Space) {
+                    if (KeyHelper.matchOk(event) ) {
                         event.accepted = true
                     }
                 }
                 Keys.onReleased: {
-                    if (!event.accepted && (event.key === Qt.Key_Return || event.key === Qt.Key_Space))
+                    if (!event.accepted && KeyHelper.matchOk(event))
                         history.push(["player"], History.Go)
                 }
 
diff --git a/modules/gui/qt/qml/player/Player.qml b/modules/gui/qt/qml/player/Player.qml
index 6e73684e7f..4ca5695fd0 100644
--- a/modules/gui/qt/qml/player/Player.qml
+++ b/modules/gui/qt/qml/player/Player.qml
@@ -24,6 +24,7 @@ import org.videolan.vlc 0.1
 
 import "qrc:///style/"
 import "qrc:///utils/" as Utils
+import "qrc:///utils/KeyHelper.js" as KeyHelper
 import "qrc:///playlist/" as PL
 import "qrc:///menus/" as Menus
 
diff --git a/modules/gui/qt/qml/player/PlayerMenuItem.qml b/modules/gui/qt/qml/player/PlayerMenuItem.qml
index 71476917b2..4f6b6a0cac 100644
--- a/modules/gui/qt/qml/player/PlayerMenuItem.qml
+++ b/modules/gui/qt/qml/player/PlayerMenuItem.qml
@@ -27,6 +27,7 @@ import org.videolan.vlc 0.1
 
 import "qrc:///style/"
 import "qrc:///utils/" as Utils
+import "qrc:///utils/KeyHelper.js" as KeyHelper
 
 T.MenuItem {
     id: control
@@ -122,6 +123,7 @@ T.MenuItem {
         }
         event.accepted = false
     }
+
     onTriggered: {
         if (parentMenu && subMenu) {
             parentMenu._emitMenuClose = false
diff --git a/modules/gui/qt/qml/utils/ExpandGridView.qml b/modules/gui/qt/qml/utils/ExpandGridView.qml
index 7b1ebab661..eb2d0c4f2c 100644
--- a/modules/gui/qt/qml/utils/ExpandGridView.qml
+++ b/modules/gui/qt/qml/utils/ExpandGridView.qml
@@ -17,6 +17,7 @@
  *****************************************************************************/
 import QtQuick 2.11
 import QtQuick.Controls 2.4
+import "KeyHelper.js" as KeyHelper
 
 NavigableFocusScope {
     id: root
@@ -411,25 +412,25 @@ NavigableFocusScope {
         var colCount = root.getNbItemsPerRow()
 
         var newIndex = -1
-        if (event.key === Qt.Key_Right || event.matches(StandardKey.MoveToNextChar)) {
+        if (KeyHelper.matchRight(event)) {
             if ((currentIndex + 1) % colCount !== 0) {//are we not at the end of line
                 newIndex = Math.min(root.modelCount - 1, currentIndex + 1)
             }
-        } else if (event.key === Qt.Key_Left || event.matches(StandardKey.MoveToPreviousChar)) {
+        } else if (KeyHelper.matchLeft(event)) {
             if (currentIndex % colCount !== 0) {//are we not at the begining of line
                 newIndex = Math.max(0, currentIndex - 1)
             }
-        } else if (event.key === Qt.Key_Down || event.matches(StandardKey.MoveToNextLine) ||event.matches(StandardKey.SelectNextLine) ) {
+        } else if (KeyHelper.matchDown(event)) {
             if (Math.floor(currentIndex / colCount) !== Math.floor(root.modelCount / colCount)) { //we are not on the last line
                 newIndex = Math.min(root.modelCount - 1, currentIndex + colCount)
             }
-        } else if (event.key === Qt.Key_PageDown || event.matches(StandardKey.MoveToNextPage) ||event.matches(StandardKey.SelectNextPage)) {
+        } else if (KeyHelper.matchPageDown(event)) {
             newIndex = Math.min(root.modelCount - 1, currentIndex + colCount * 5)
-        } else if (event.key === Qt.Key_Up || event.matches(StandardKey.MoveToPreviousLine) ||event.matches(StandardKey.SelectPreviousLine)) {
+        } else if (KeyHelper.matchUp(event)) {
             if (Math.floor(currentIndex / colCount) !== 0) { //we are not on the first line
                 newIndex = Math.max(0, currentIndex - colCount)
             }
-        } else if (event.key === Qt.Key_PageUp || event.matches(StandardKey.MoveToPreviousPage) ||event.matches(StandardKey.SelectPreviousPage)) {
+        } else if (KeyHelper.matchPageUp(event)) {
             newIndex = Math.max(0, currentIndex - colCount * 5)
         }
 
@@ -449,7 +450,7 @@ NavigableFocusScope {
         if (event.matches(StandardKey.SelectAll)) {
             event.accepted = true
             root.selectAll()
-        } else if (event.key === Qt.Key_Space || event.matches(StandardKey.InsertParagraphSeparator)) { //enter/return/space
+        } else if ( KeyHelper.matchOk(event) ) {
             event.accepted = true
             root.actionAtIndex(currentIndex)
         }
diff --git a/modules/gui/qt/qml/utils/KeyHelper.js b/modules/gui/qt/qml/utils/KeyHelper.js
new file mode 100644
index 0000000000..5864842fab
--- /dev/null
+++ b/modules/gui/qt/qml/utils/KeyHelper.js
@@ -0,0 +1,64 @@
+/*****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+.pragma library
+
+.import QtQuick 2.11 as QtQuick
+
+function matchLeft(event) {
+    return event.key === Qt.Key_Left
+        || event.matches(QtQuick.StandardKey.MoveToPreviousChar)
+}
+
+function matchRight(event) {
+    return event.key === Qt.Key_Right
+        || event.matches(QtQuick.StandardKey.MoveToNextChar)
+}
+
+function matchUp(event) {
+    return event.key === Qt.Key_Up
+        || event.matches(QtQuick.StandardKey.MoveToPreviousLine)
+}
+
+function matchDown(event) {
+    return event.key === Qt.Key_Down
+        || event.matches(QtQuick.StandardKey.MoveToNextLine)
+}
+
+function matchPageDown(event) {
+    return event.key === Qt.Key_PageDown
+        || event.matches(QtQuick.StandardKey.MoveToNextPage)
+}
+
+function matchPageUp(event) {
+    return event.key === Qt.Key_PageUp
+        || event.matches(QtQuick.StandardKey.MoveToPreviousPage)
+}
+
+function matchOk( event ) {
+    return event.key === Qt.Key_Space
+        || event.matches(QtQuick.StandardKey.InsertParagraphSeparator)
+}
+
+function matchCancel(event) {
+    return event.key === Qt.Key_Backspace
+        || event.key === Qt.Key_Back
+        || event.key === Qt.Key_Cancel
+        || event.matches(QtQuick.StandardKey.Back)
+        || event.matches(QtQuick.StandardKey.Cancel)
+}
diff --git a/modules/gui/qt/qml/utils/KeyNavigableGridView.qml b/modules/gui/qt/qml/utils/KeyNavigableGridView.qml
index e4af15c35e..1eddabda6e 100644
--- a/modules/gui/qt/qml/utils/KeyNavigableGridView.qml
+++ b/modules/gui/qt/qml/utils/KeyNavigableGridView.qml
@@ -17,6 +17,7 @@
  *****************************************************************************/
 import QtQuick 2.11
 import QtQuick.Controls 2.4
+import "KeyHelper.js" as KeyHelper
 
 
 NavigableFocusScope {
@@ -67,25 +68,25 @@ NavigableFocusScope {
 
         Keys.onPressed: {
             var newIndex = -1
-            if (event.key === Qt.Key_Right || event.matches(StandardKey.MoveToNextChar)) {
+            if (KeyHelper.matchRight(event)) {
                 if ((currentIndex + 1) % _colCount !== 0) {//are we not at the end of line
                     newIndex = Math.min(gridview_id.modelCount - 1, currentIndex + 1)
                 }
-            } else if (event.key === Qt.Key_Left || event.matches(StandardKey.MoveToPreviousChar)) {
+            } else if (KeyHelper.matchLeft(event)) {
                 if (currentIndex % _colCount !== 0) {//are we not at the begining of line
                     newIndex = Math.max(0, currentIndex - 1)
                 }
-            } else if (event.key === Qt.Key_Down || event.matches(StandardKey.MoveToNextLine) ||event.matches(StandardKey.SelectNextLine) ) {
+            } else if (KeyHelper.matchDown(event)) {
                 if (Math.floor(currentIndex / _colCount) !== Math.floor(gridview_id.modelCount / _colCount)) { //we are not on the last line
                     newIndex = Math.min(gridview_id.modelCount - 1, currentIndex + _colCount)
                 }
-            } else if (event.key === Qt.Key_PageDown || event.matches(StandardKey.MoveToNextPage) ||event.matches(StandardKey.SelectNextPage)) {
+            } else if (KeyHelper.matchPageDown(event)) {
                 newIndex = Math.min(gridview_id.modelCount - 1, currentIndex + _colCount * 5)
-            } else if (event.key === Qt.Key_Up || event.matches(StandardKey.MoveToPreviousLine) ||event.matches(StandardKey.SelectPreviousLine)) {
+            } else if (KeyHelper.matchUp(event)) {
                 if (Math.floor(currentIndex / _colCount) !== 0) { //we are not on the first line
                     newIndex = Math.max(0, currentIndex - _colCount)
                 }
-            } else if (event.key === Qt.Key_PageUp || event.matches(StandardKey.MoveToPreviousPage) ||event.matches(StandardKey.SelectPreviousPage)) {
+            } else if (KeyHelper.matchPageUp(event)) {
                 newIndex = Math.max(0, currentIndex - _colCount * 5)
             }
 
@@ -104,7 +105,7 @@ NavigableFocusScope {
             if (event.matches(StandardKey.SelectAll)) {
                 event.accepted = true
                 selectAll()
-            } else if (event.key === Qt.Key_Space || event.matches(StandardKey.InsertParagraphSeparator)) { //enter/return/space
+            } else if (KeyHelper.matchOk(event)) {
                 event.accepted = true
                 actionAtIndex(currentIndex)
             }
diff --git a/modules/gui/qt/qml/utils/KeyNavigableListView.qml b/modules/gui/qt/qml/utils/KeyNavigableListView.qml
index 475af5457a..fdc1ea57ba 100644
--- a/modules/gui/qt/qml/utils/KeyNavigableListView.qml
+++ b/modules/gui/qt/qml/utils/KeyNavigableListView.qml
@@ -19,6 +19,7 @@ import QtQuick 2.11
 import QtQuick.Controls 2.4
 
 import "qrc:///style/"
+import "KeyHelper.js" as KeyHelper
 
 NavigableFocusScope {
     id: listview_id
@@ -137,28 +138,28 @@ NavigableFocusScope {
 
             if (orientation === ListView.Vertical)
             {
-                if ( event.key === Qt.Key_Down || event.matches(StandardKey.MoveToNextLine) ||event.matches(StandardKey.SelectNextLine) ) {
+                if ( KeyHelper.matchDown(event) ) {
                     if (currentIndex !== modelCount - 1 )
                         newIndex = currentIndex + 1
-                } else if ( event.key === Qt.Key_PageDown || event.matches(StandardKey.MoveToNextPage) ||event.matches(StandardKey.SelectNextPage)) {
+                } else if ( KeyHelper.matchPageDown(event) ) {
                     newIndex = Math.min(modelCount - 1, currentIndex + 10)
-                } else if ( event.key === Qt.Key_Up || event.matches(StandardKey.MoveToPreviousLine) ||event.matches(StandardKey.SelectPreviousLine) ) {
+                } else if ( KeyHelper.matchUp(event) ) {
                     if ( currentIndex !== 0 )
                         newIndex = currentIndex - 1
-                } else if ( event.key === Qt.Key_PageUp || event.matches(StandardKey.MoveToPreviousPage) ||event.matches(StandardKey.SelectPreviousPage)) {
+                } else if ( KeyHelper.matchPageUp(event) ) {
                     newIndex = Math.max(0, currentIndex - 10)
                 }
             }else{
-                if ( event.key === Qt.Key_Right || event.matches(StandardKey.SelectNextLine) ) {
+                if ( KeyHelper.matchRight(event) ) {
                     if (currentIndex !== modelCount - 1 )
                         newIndex = currentIndex + 1
                 }
-                else if ( event.key === Qt.Key_PageDown || event.matches(StandardKey.MoveToNextPage) ||event.matches(StandardKey.SelectNextPage)) {
+                else if ( KeyHelper.matchPageDown(event) ) {
                     newIndex = Math.min(modelCount - 1, currentIndex + 10)
-                } else if ( event.key === Qt.Key_Left || event.matches(StandardKey.SelectPreviousLine) ) {
+                } else if ( KeyHelper.matchLeft(event) ) {
                     if ( currentIndex !== 0 )
                         newIndex = currentIndex - 1
-                } else if ( event.key === Qt.Key_PageUp || event.matches(StandardKey.MoveToPreviousPage) ||event.matches(StandardKey.SelectPreviousPage)) {
+                } else if ( KeyHelper.matchPageUp(event) ) {
                     newIndex = Math.max(0, currentIndex - 10)
                 }
             }
@@ -178,7 +179,7 @@ NavigableFocusScope {
             if (event.matches(StandardKey.SelectAll)) {
                 event.accepted = true
                 selectAll()
-            } else if (event.key === Qt.Key_Space || event.matches(StandardKey.InsertParagraphSeparator)) { //enter/return/space
+            } else if ( KeyHelper.matchOk(event) ) { //enter/return/space
                 event.accepted = true
                 actionAtIndex(currentIndex)
             }
diff --git a/modules/gui/qt/qml/utils/NavigableFocusScope.qml b/modules/gui/qt/qml/utils/NavigableFocusScope.qml
index f24743becf..d811927cd7 100644
--- a/modules/gui/qt/qml/utils/NavigableFocusScope.qml
+++ b/modules/gui/qt/qml/utils/NavigableFocusScope.qml
@@ -17,6 +17,7 @@
  *****************************************************************************/
 import QtQuick 2.11
 import QtQuick.Controls 2.4
+import "KeyHelper.js" as KeyHelper
 
 /*
  * This class is designed to be inherited, It provide basic key handling to navigate between view
@@ -132,19 +133,19 @@ FocusScope {
     function defaultKeyAction(event, index) {
         if (event.accepted)
             return
-        if ( event.key === Qt.Key_Down || event.matches(StandardKey.MoveToNextLine) ||event.matches(StandardKey.SelectNextLine) ) {
+        if ( KeyHelper.matchDown(event) ) {
             event.accepted = true
             navigationDown( index )
-        } else if ( event.key === Qt.Key_Up || event.matches(StandardKey.MoveToPreviousLine) ||event.matches(StandardKey.SelectPreviousLine) ) {
+        } else if ( KeyHelper.matchUp(event) ) {
             event.accepted = true
             navigationUp( index  )
-        } else if (event.key === Qt.Key_Right || event.matches(StandardKey.MoveToNextChar) ) {
+        } else if ( KeyHelper.matchRight(event) ) {
             event.accepted = true
             navigationRight( index )
-        } else if (event.key === Qt.Key_Left || event.matches(StandardKey.MoveToPreviousChar) ) {
+        } else if ( KeyHelper.matchLeft(event) ) {
             event.accepted = true
             navigationLeft( index )
-        } else if ( event.key === Qt.Key_Back || event.key === Qt.Key_Cancel || event.matches(StandardKey.Back) || event.matches(StandardKey.Cancel)) {
+        } else if ( KeyHelper.matchCancel(event) ) {
             event.accepted = true
             navigationCancel( index )
         }
diff --git a/modules/gui/qt/vlc.qrc b/modules/gui/qt/vlc.qrc
index 42ecf9582c..1bb063fc68 100644
--- a/modules/gui/qt/vlc.qrc
+++ b/modules/gui/qt/vlc.qrc
@@ -208,6 +208,7 @@
         <file alias="NavigableCol.qml">qml/utils/NavigableCol.qml</file>
         <file alias="NavigableRow.qml">qml/utils/NavigableRow.qml</file>
         <file alias="TabButtonExt.qml">qml/utils/TabButtonExt.qml</file>
+        <file alias="KeyHelper.js">qml/utils/KeyHelper.js</file>
     </qresource>
     <qresource prefix="/mediacenter">
         <file alias="MCMusicDisplay.qml">qml/mediacenter/MCMusicDisplay.qml</file>
-- 
2.17.1



More information about the vlc-devel mailing list