[vlc-commits] [Git][videolan/vlc][master] qml: use nullish coalescence and optional chaining to simplify conditions

Steve Lhomme (@robUx4) gitlab at videolan.org
Sat Apr 13 14:21:49 UTC 2024



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
04a49bff by Pierre Lamot at 2024-04-13T13:02:12+00:00
qml: use nullish coalescence and optional chaining to simplify conditions

optional chaining was introduced in 6.2
nullish coalescence was introduced in 5.15

no functional changes

- - - - -


27 changed files:

- modules/gui/qt/dialogs/toolbar/qml/ToolbarEditor.qml
- modules/gui/qt/maininterface/qml/MainDisplay.qml
- modules/gui/qt/maininterface/qml/MainViewLoader.qml
- modules/gui/qt/medialibrary/qml/MusicAlbumsGridExpandDelegate.qml
- modules/gui/qt/medialibrary/qml/VideoAllSubDisplay.qml
- modules/gui/qt/medialibrary/qml/VideoGridItem.qml
- modules/gui/qt/network/qml/BrowseDeviceView.qml
- modules/gui/qt/network/qml/DiscoverUrlDisplay.qml
- modules/gui/qt/network/qml/NetworkCustomCover.qml
- modules/gui/qt/player/qml/ControlRepeater.qml
- modules/gui/qt/player/qml/PlayerControlLayout.qml
- modules/gui/qt/player/qml/SliderBar.qml
- modules/gui/qt/player/qml/TopBar.qml
- modules/gui/qt/playlist/qml/PlaylistDelegate.qml
- modules/gui/qt/playlist/qml/PlaylistListView.qml
- modules/gui/qt/util/qml/FadeControllerStateGroup.qml
- modules/gui/qt/util/qml/ViewDragAutoScrollHandler.qml
- modules/gui/qt/widgets/qml/BannerTabButton.qml
- modules/gui/qt/widgets/qml/CSDMouseStealer.qml
- modules/gui/qt/widgets/qml/CSDThemeButtonSet.qml
- modules/gui/qt/widgets/qml/DoubleShadow.qml
- modules/gui/qt/widgets/qml/DropShadowImage.qml
- modules/gui/qt/widgets/qml/ExpandGridView.qml
- modules/gui/qt/widgets/qml/FadingEdgeForListView.qml
- modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
- modules/gui/qt/widgets/qml/PageLoader.qml
- modules/gui/qt/widgets/qml/TableColumns.qml


Changes:

=====================================
modules/gui/qt/dialogs/toolbar/qml/ToolbarEditor.qml
=====================================
@@ -70,8 +70,7 @@ Item {
                     text: {
                         const text = modelData.name
 
-                        if (!!MainCtx.controlbarProfileModel.currentModel &&
-                                MainCtx.controlbarProfileModel.currentModel.getModel(identifier).dirty)
+                        if (MainCtx.controlbarProfileModel.currentModel?.getModel(identifier).dirty)
                             return _markDirty(text)
                         else
                             return text
@@ -188,7 +187,7 @@ Item {
                                                                                                       rightMetric.width) * 1.25
                                                                                              : 0
 
-                            readonly property int count: !!item ? item.count : 0
+                            readonly property int count: item?.count ?? 0
 
                             sourceComponent: Rectangle {
                                 color: theme.bg.primary
@@ -304,6 +303,6 @@ Item {
     Util.ViewDragAutoScrollHandler {
         id: dragAutoScrollHandler
 
-        view: _viewThatContainsDrag ? _viewThatContainsDrag : null
+        view: _viewThatContainsDrag ?? null
     }
 }


=====================================
modules/gui/qt/maininterface/qml/MainDisplay.qml
=====================================
@@ -67,16 +67,13 @@ FocusScope {
         const item = stackView.currentItem
 
         sourcesBanner.localMenuDelegate = Qt.binding(function () {
-            return !!item.localMenuDelegate ? item.localMenuDelegate : null
+            return item.localMenuDelegate ?? null
         })
 
         // NOTE: sortMenu is declared with the SortMenu type, so when it's undefined we have to
         //       return null to avoid a QML warning.
         sourcesBanner.sortMenu = Qt.binding(function () {
-            if (item.sortMenu)
-                return item.sortMenu
-            else
-                return null
+            return item.sortMenu ?? null
         })
 
         MainCtx.hasGridListMode = Qt.binding(() => item.hasGridListMode !== undefined && item.hasGridListMode)


=====================================
modules/gui/qt/maininterface/qml/MainViewLoader.qml
=====================================
@@ -56,7 +56,7 @@ Widgets.StackViewExt {
     property Component loadingComponent: null
 
     // NOTE: Sometimes the model has no 'loading' property.
-    readonly property bool isLoading: (model.loading) ? model.loading : false
+    readonly property bool isLoading: model.loading ?? false
 
     readonly property int count: model.count
 


=====================================
modules/gui/qt/medialibrary/qml/MusicAlbumsGridExpandDelegate.qml
=====================================
@@ -371,7 +371,7 @@ FocusScope {
                 anchors.fill: parent
 
                 Widgets.ListLabel {
-                    text: !!rowModel && !!rowModel.track_number ? rowModel.track_number : ""
+                    text: rowModel?.track_number ?? ""
                     color: theme.fg.primary
                     font.weight: Font.Normal
 
@@ -381,7 +381,7 @@ FocusScope {
                 }
 
                 Widgets.ListLabel {
-                    text: !!rowModel && !!rowModel.title ? rowModel.title : ""
+                    text: rowModel?.title ?? ""
                     color: theme.fg.primary
 
                     Layout.fillHeight: true


=====================================
modules/gui/qt/medialibrary/qml/VideoAllSubDisplay.qml
=====================================
@@ -50,15 +50,15 @@ VideoAll {
 
     // Settings
 
-    model: !!_meta ? _meta.model : null
+    model: _meta?.model ?? null
 
     contextMenu: Util.MLContextMenu { model: _meta ? _meta.model : null; showPlayAsAudioAction: true }
 
-    gridLabels: !!_meta ? _meta.gridLabels : root.getLabel
+    gridLabels: _meta?.gridLabels ?? root.getLabel
 
-    listLabels: !!_meta ? _meta.listLabels : root.getLabel
+    listLabels: _meta?.listLabels ?? root.getLabel
 
-    sectionProperty: !!_meta && !!_meta.sectionProperty ? _meta.sectionProperty : ""
+    sectionProperty: _meta?.sectionProperty ?? ""
 
     headerPositioning: headerItem.model.count > 0 ? ListView.InlineHeader : ListView.OverlayHeader
 


=====================================
modules/gui/qt/medialibrary/qml/VideoGridItem.qml
=====================================
@@ -42,7 +42,7 @@ Widgets.GridItem {
     fallbackImage: VLCStyle.noArtVideoCover
 
     title: model.title || qsTr("Unknown title")
-    subtitle: !!model && (typeof model.duration !== "undefined") ? model.duration.formatHMS() : ""
+    subtitle: model?.duration?.formatHMS() ?? ""
     pictureWidth: VLCStyle.gridCover_video_width
     pictureHeight: VLCStyle.gridCover_video_height
     playCoverBorderWidth: VLCStyle.gridCover_video_border


=====================================
modules/gui/qt/network/qml/BrowseDeviceView.qml
=====================================
@@ -34,22 +34,22 @@ FocusScope {
 
     /* required */ property var model
 
-    readonly property int rowHeight: (_currentView) ? _currentView.rowHeight : 0
+    readonly property int rowHeight: _currentView?.rowHeight ?? 0
 
-    readonly property int contentHeight: (_currentView) ? _currentView.contentHeight : 0
+    readonly property int contentHeight: _currentView?.contentHeight ?? 0
 
-    readonly property int contentLeftMargin: (_currentView) ? _currentView.contentLeftMargin : 0
-    readonly property int contentRightMargin: (_currentView) ? _currentView.contentRightMargin : 0
+    readonly property int contentLeftMargin: _currentView?.contentLeftMargin ?? 0
+    readonly property int contentRightMargin: _currentView?.contentRightMargin ?? 0
 
     property int displayMarginEnd: 0
 
-    readonly property int currentIndex: (_currentView) ? _currentView.currentIndex : -1
+    readonly property int currentIndex: _currentView?.currentIndex ?? -1
 
     property int maximumRows: -1
 
-    readonly property int maximumCount: (_currentView) ? _currentView.maximumCount : -1
+    readonly property int maximumCount: _currentView?.maximumCount ?? -1
 
-    readonly property int nbItemPerRow: (_currentView) ? _currentView.nbItemPerRow : 1
+    readonly property int nbItemPerRow: _currentView?.nbItemPerRow ?? 1
 
     property bool isSearchable: true
 


=====================================
modules/gui/qt/network/qml/DiscoverUrlDisplay.qml
=====================================
@@ -64,7 +64,7 @@ FocusScope {
             focus: true
 
             Navigation.parentItem:  root
-            Navigation.downItem: (!!urlListDisplay.item) ? urlListDisplay.item : null
+            Navigation.downItem: urlListDisplay.item ?? null
 
             Widgets.TextFieldExt {
                 id: searchField


=====================================
modules/gui/qt/network/qml/NetworkCustomCover.qml
=====================================
@@ -107,7 +107,7 @@ Item {
         verticalAlignment: root.verticalAlignment
 
         source: {
-            if (!!networkModel && !!networkModel.artwork && networkModel.artwork.length > 0)
+            if (networkModel?.artwork && networkModel.artwork.length > 0)
                 return VLCAccessImage.uri(networkModel.artwork)
 
             return ""


=====================================
modules/gui/qt/player/qml/ControlRepeater.qml
=====================================
@@ -71,7 +71,7 @@ Repeater {
         // Events
 
         onActiveFocusChanged: {
-            if (activeFocus && (!!item && !item.focus)) {
+            if (activeFocus && !item?.focus) {
                 recoverFocus()
             }
         }
@@ -209,7 +209,7 @@ Repeater {
         // Private
 
         function _focusIfFocusable(_loader) {
-            if (!!_loader && !!_loader.item && _loader.item.focus) {
+            if (_loader?.item?.focus) {
                 if (item.focusReason !== undefined)
                     _loader.item.forceActiveFocus(item.focusReason)
                 else {


=====================================
modules/gui/qt/player/qml/PlayerControlLayout.qml
=====================================
@@ -176,7 +176,7 @@ FocusScope {
             rightMargin: layoutSpacing - spacing
         }
 
-        active: !!playerControlLayout.model && !!playerControlLayout.model.left && (playerControlLayout.model.left.count > 0) &&
+        active: !!playerControlLayout.model?.left && (playerControlLayout.model.left.count > 0) &&
                 !loaderLeftRight.active
 
         focus: active


=====================================
modules/gui/qt/player/qml/SliderBar.qml
=====================================
@@ -287,8 +287,8 @@ T.ProgressBar {
             Item {
                 Rectangle {
                     id: seekpointsRect
-                    readonly property real startPosition: model.startPosition === undefined ? 0.0 : model.startPosition
-                    readonly property real endPosition: model.endPosition === undefined ? 1.0 : model.endPosition
+                    readonly property real startPosition: model.startPosition ??  0.0
+                    readonly property real endPosition: model.endPosition ?? 1.0
 
                     readonly property int _currentChapter: {
                         if (control.visualPosition < seekpointsRect.startPosition)


=====================================
modules/gui/qt/player/qml/TopBar.qml
=====================================
@@ -91,8 +91,8 @@ FocusScope{
 
     function _layoutLine(c1, c2, offset)
     {
-        let c1Height = c1 !== undefined ? c1.implicitHeight : 0
-        let c2Height = c2 !== undefined ? c2.implicitHeight : 0
+        let c1Height = c1?.implicitHeight ?? 0
+        let c2Height = c2?.implicitHeight ?? 0
 
         if (c2 === csdDecorations) {
             //csdDecorations.implicitHeight gets overwritten when the height is set,


=====================================
modules/gui/qt/playlist/qml/PlaylistDelegate.qml
=====================================
@@ -163,7 +163,7 @@ T.ItemDelegate {
 
                 anchors.fill: parent
                 fillMode: Image.PreserveAspectFit
-                source: (model.artwork && model.artwork.toString()) ? VLCAccessImage.uri(model.artwork) : VLCStyle.noArtAlbumCover
+                source: (model?.artwork.toString()) ? VLCAccessImage.uri(model.artwork) : VLCStyle.noArtAlbumCover
                 visible: !statusIcon.visible
                 asynchronous: true
 
@@ -219,7 +219,7 @@ T.ItemDelegate {
                 Layout.fillHeight: true
                 Layout.fillWidth: true
 
-                text: (model.artist ? model.artist : qsTr("Unknown Artist"))
+                text: model?.artist ?? qsTr("Unknown Artist")
                 color: theme.fg.primary
                 verticalAlignment: Text.AlignBottom
             }


=====================================
modules/gui/qt/playlist/qml/PlaylistListView.qml
=====================================
@@ -34,7 +34,7 @@ T.Pane {
     property var model: PlaylistListModel {
         playlist: MainPlaylistController.playlist
     }
-    readonly property ListSelectionModel selectionModel: listView ? listView.selectionModel : null
+    readonly property ListSelectionModel selectionModel: listView?.selectionModel ?? null
 
     property bool useAcrylic: true
 


=====================================
modules/gui/qt/util/qml/FadeControllerStateGroup.qml
=====================================
@@ -25,7 +25,7 @@ import "qrc:///style/"
 StateGroup {
     id: root
 
-    state: target ? target.state : ""
+    state: target?.state ?? ""
 
     property Item target
 


=====================================
modules/gui/qt/util/qml/ViewDragAutoScrollHandler.qml
=====================================
@@ -28,7 +28,7 @@ QtObject {
     // if 'dragItem' is null, user must override property 'dragging' and 'dragPosProvider'
     property Item dragItem: null
 
-    property bool dragging: !!dragItem && dragItem.visible
+    property bool dragging: dragItem?.visible
 
     property var dragPosProvider: function () {
         return root.view.mapFromItem(root.dragItem.parent,


=====================================
modules/gui/qt/widgets/qml/BannerTabButton.qml
=====================================
@@ -96,7 +96,7 @@ T.TabButton {
                 horizontalCenter: parent.horizontalCenter
             }
 
-            width: control.contentItem ? control.contentItem.implicitWidth : 0
+            width: control.contentItem?.implicitWidth ?? 0
 
             visible: (width > 0 && control.showCurrentIndicator && control.selected)
         }


=====================================
modules/gui/qt/widgets/qml/CSDMouseStealer.qml
=====================================
@@ -29,8 +29,8 @@ Item {
     property bool anchorInside: true
 
     //private
-    readonly property int _targetHeight: target ? target.height : 0
-    readonly property int _targetWidth: target ? target.width : 0
+    readonly property int _targetHeight: target?.height ?? 0
+    readonly property int _targetWidth: target?.width ?? 0
 
     readonly property int _edgeVtHeight: target ? (target.height - root.csdSize * 2) : 0
     readonly property int _edgeHzWidth:  target ? (target.width  - root.csdSize * 2) : 0


=====================================
modules/gui/qt/widgets/qml/CSDThemeButtonSet.qml
=====================================
@@ -34,9 +34,9 @@ Rectangle {
         return false
     }
 
-    readonly property int _frameMarginLeft: VLCStyle.palette.csdMetrics ? VLCStyle.palette.csdMetrics.csdFrameMarginLeft : 0
-    readonly property int _frameMarginRight: VLCStyle.palette.csdMetrics ? VLCStyle.palette.csdMetrics.csdFrameMarginRight : 0
-    readonly property int _interNavButtonSpacing: VLCStyle.palette.csdMetrics ? VLCStyle.palette.csdMetrics.interNavButtonSpacing : 0
+    readonly property int _frameMarginLeft: VLCStyle.palette.csdMetrics?.csdFrameMarginLeft ?? 0
+    readonly property int _frameMarginRight: VLCStyle.palette.csdMetrics?.csdFrameMarginRight ?? 0
+    readonly property int _interNavButtonSpacing: VLCStyle.palette.csdMetrics?.interNavButtonSpacing ?? 0
 
     implicitWidth: layout.implicitWidth + _frameMarginLeft + _frameMarginRight
     implicitHeight: layout.implicitHeight


=====================================
modules/gui/qt/widgets/qml/DoubleShadow.qml
=====================================
@@ -32,10 +32,10 @@ ScaledImage {
     property real viewportWidth: rectWidth + (Math.max(Math.abs(primaryHorizontalOffset) + primaryBlurRadius, Math.abs(secondaryHorizontalOffset) + secondaryBlurRadius)) * 2
     property real viewportHeight: rectHeight + (Math.max(Math.abs(primaryVerticalOffset) + primaryBlurRadius, Math.abs(secondaryVerticalOffset) + secondaryBlurRadius)) * 2
 
-    property real rectWidth: sourceItem ? sourceItem.width : 0
-    property real rectHeight: sourceItem ? sourceItem.height : 0
-    property real xRadius: (sourceItem && sourceItem.radius !== undefined ) ? sourceItem.radius : 0
-    property real yRadius: (sourceItem && sourceItem.radius !== undefined ) ? sourceItem.radius : 0
+    property real rectWidth: sourceItem?.width ?? 0
+    property real rectHeight: sourceItem?.height ?? 0
+    property real xRadius: sourceItem?.radius ?? 0
+    property real yRadius: sourceItem?.radius ?? 0
 
     property color primaryColor: Qt.rgba(0, 0, 0, .18)
     property real primaryVerticalOffset: 0


=====================================
modules/gui/qt/widgets/qml/DropShadowImage.qml
=====================================
@@ -30,13 +30,13 @@ ScaledImage {
     property real blurRadius: 0
     property color color
 
-    property real rectWidth: sourceItem ? sourceItem.width : 0
-    property real rectHeight: sourceItem ? sourceItem.height : 0
+    property real rectWidth: sourceItem?.width ?? 0
+    property real rectHeight: sourceItem?.height ?? 0
 
     property real xOffset: 0
     property real yOffset: 0
-    property real xRadius: (sourceItem && sourceItem.radius !== undefined) ? sourceItem.radius : 0
-    property real yRadius: (sourceItem && sourceItem.radius !== undefined) ? sourceItem.radius : 0
+    property real xRadius: sourceItem?.radius ?? 0
+    property real yRadius: sourceItem?.radius ?? 0
 
     sourceSize: Qt.size(viewportWidth, viewportHeight)
 


=====================================
modules/gui/qt/widgets/qml/ExpandGridView.qml
=====================================
@@ -696,7 +696,7 @@ FocusScope {
     }
 
     function _onModelCountChanged() {
-        _count = model ? model.rowCount() : 0
+        _count = model?.rowCount() ?? 0
         if (!_isInitialised)
             return
 


=====================================
modules/gui/qt/widgets/qml/FadingEdgeForListView.qml
=====================================
@@ -38,11 +38,11 @@ FadingEdge {
                                                           : delegateItem.width) / 2
                            : (VLCStyle.margin_large * 2)
 
-    readonly property bool transitionsRunning: ((listView.add ? listView.add.running : false) ||
-                                                (listView.addDisplaced ? listView.addDisplaced.running : false) ||
-                                                (listView.populate ? listView.populate.running : false) ||
-                                                (listView.remove ? listView.remove.running : false) ||
-                                                (listView.removeDisplaced ? listView.removeDisplaced.running : false))
+    readonly property bool transitionsRunning: (listView.add?.running ||
+                                                listView.addDisplaced?.running ||
+                                                listView.populate?.running ||
+                                                listView.remove?.running ||
+                                                listView.removeDisplaced?.running)
 
     // TODO: Use itemAtIndex(0) Qt >= 5.13
     // FIXME: Delegate with variable size


=====================================
modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
=====================================
@@ -70,7 +70,7 @@ FocusScope {
     }
 
     property Component header: null
-    property Item headerItem: view.headerItem ? view.headerItem.loadedHeader : null
+    property Item headerItem: view.headerItem?.loadedHeader ?? null
     property color headerColor: colorContext.bg.primary
     property int headerTopPadding: 0
 


=====================================
modules/gui/qt/widgets/qml/PageLoader.qml
=====================================
@@ -35,20 +35,14 @@ StackViewExt {
     property var pageModel: []
 
     //indicates whether the subview support grid/list mode
-    readonly property bool hasGridListMode: (currentItem
-                                            && currentItem.hasGridListMode !== undefined
-                                            && currentItem.hasGridListMode)
+    readonly property bool hasGridListMode: currentItem?.hasGridListMode ?? false
 
-    readonly property bool isSearchable: (currentItem
-                                    && currentItem.isSearchable !== undefined
-                                    && currentItem.isSearchable)
+    readonly property bool isSearchable: currentItem?.isSearchable ?? false
 
-    readonly property var sortModel: (currentItem
-                                    && currentItem.sortModel !== undefined) ? currentItem.sortModel : null
+    readonly property var sortModel: currentItem?.sortModel ?? null
 
     //property is *not* readOnly, a PageLoader may define a localMenuDelegate common for its subviews (music, video)
-    property Component localMenuDelegate: (currentItem
-                                    && currentItem.localMenuDelegate
+    property Component localMenuDelegate: (currentItem?.localMenuDelegate
                                     && (currentItem.localMenuDelegate instanceof Component)) ? currentItem.localMenuDelegate : null
 
 


=====================================
modules/gui/qt/widgets/qml/TableColumns.qml
=====================================
@@ -102,13 +102,7 @@ Item {
             Layout.preferredHeight: root.titleCover_height
             Layout.preferredWidth: root.titleCover_width
 
-            source: {
-                let cover = null
-                if (!!titleDel.rowModel) {
-                    cover = titleDel.rowModel[root.criteriaCover]
-                }
-                return cover || ""
-            }
+            source: titleDel.rowModel?.[root.criteriaCover] ?? ""
 
             fallbackImageSource: titleDel.model.placeHolder || VLCStyle.noArtAlbumCover
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/04a49bff37cc0c046b5a4b828d2e130f291f4704

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/04a49bff37cc0c046b5a4b828d2e130f291f4704
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list