Warning, /multimedia/plasmatube/src/ui/components/SourceSwitcher.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 import QtQuick
0005 import QtQuick.Controls as QQC2
0006 import QtQuick.Layouts
0007 import org.kde.kirigami as Kirigami
0008 import org.kde.kirigamiaddons.delegates as Delegates
0009 import org.kde.kirigamiaddons.labs.components as KirigamiComponents
0010 
0011 import org.kde.plasmatube
0012 
0013 QQC2.Pane {
0014     id: root
0015 
0016     readonly property var selectedSource: PlasmaTube.sourceManager.selectedSource
0017 
0018     property alias sourcesListVisible: sources.visible
0019 
0020     padding: 0
0021 
0022     contentItem: ColumnLayout {
0023         id: content
0024 
0025         spacing: 0
0026 
0027         QQC2.ItemDelegate {
0028             id: currentSourceDelegate
0029 
0030             text: root.selectedSource?.url
0031 
0032             Layout.fillWidth: true
0033 
0034             contentItem: RowLayout {
0035                 spacing: Kirigami.Units.smallSpacing
0036 
0037                 Delegates.SubtitleContentItem {
0038                     itemDelegate: currentSourceDelegate
0039                     subtitle: {
0040                         switch (root.selectedSource?.type) {
0041                             case VideoSource.Invidious:
0042                                 return i18n("Invidious");
0043                             case VideoSource.PeerTube:
0044                                 return i18n("PeerTube");
0045                             case VideoSource.Piped:
0046                                 return i18n("Piped");
0047                         }
0048                     }
0049                     subtitleItem.textFormat: Text.PlainText
0050 
0051                     Layout.fillWidth: true
0052                 }
0053 
0054                 QQC2.ToolButton {
0055                     text: i18n("Switch Source")
0056                     icon.name: "system-switch-user"
0057 
0058                     display: QQC2.AbstractButton.IconOnly
0059 
0060                     onClicked: root.sourcesListVisible = !root.sourcesListVisible
0061 
0062                     QQC2.ToolTip.text: text
0063                     QQC2.ToolTip.visible: hovered
0064                     QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
0065 
0066                     Layout.minimumWidth: Layout.preferredWidth
0067                 }
0068             }
0069         }
0070 
0071         ListView {
0072             id: sources
0073 
0074             model: PlasmaTube.sourceManager
0075             currentIndex: PlasmaTube.sourceManager.selectedIndex
0076 
0077             header: Kirigami.Separator {
0078                 anchors {
0079                     left: parent.left
0080                     leftMargin: Kirigami.Units.smallSpacing
0081 
0082                     right: parent.right
0083                     rightMargin: Kirigami.Units.smallSpacing
0084                 }
0085             }
0086 
0087             footer: QQC2.ItemDelegate {
0088                 id: addSourceDelegate
0089 
0090                 width: parent.width
0091                 highlighted: focus
0092                 icon {
0093                     name: "list-add"
0094                     width: Kirigami.Units.iconSizes.medium
0095                     height: Kirigami.Units.iconSizes.medium
0096                 }
0097                 text: i18n("Add Source")
0098 
0099                 contentItem: Delegates.SubtitleContentItem {
0100                     itemDelegate: addSourceDelegate
0101                     subtitle: i18n("Add a new video source")
0102                 }
0103 
0104                 onClicked: {
0105                     pageStack.pushDialogLayer(Qt.createComponent("org.kde.plasmatube", "WelcomePage"));
0106                     root.sourcesListVisible = false
0107                 }
0108 
0109                 Keys.onUpPressed: {
0110                     sources.currentIndex = accounts.count - 1
0111                     sources.forceActiveFocus()
0112                 }
0113                 Keys.onDownPressed: {
0114                     sources.currentIndex = 0
0115                     sources.forceActiveFocus()
0116                 }
0117             }
0118 
0119             visible: false
0120             onVisibleChanged: if (visible) focus = true
0121             clip: true
0122 
0123             keyNavigationEnabled: false
0124             Keys.onDownPressed: {
0125                 if (sources.currentIndex === sources.count - 1) {
0126                     addAccount.forceActiveFocus()
0127                     sources.currentIndex = -1
0128                 } else {
0129                     sources.incrementCurrentIndex()
0130                 }
0131             }
0132             Keys.onUpPressed: {
0133                 if (sources.currentIndex === 0) {
0134                     addAccount.forceActiveFocus()
0135                     sources.currentIndex = -1
0136                 } else {
0137                     sources.decrementCurrentIndex()
0138                 }
0139             }
0140 
0141             Keys.onReleased: if (event.key == Qt.Key_Escape) {
0142                 root.sourcesListVisible = false
0143             }
0144 
0145             Layout.fillWidth: true
0146             Layout.preferredHeight: contentHeight
0147             Layout.topMargin: Kirigami.Units.smallSpacing
0148 
0149             delegate: QQC2.ItemDelegate {
0150                 id: sourceDelegate
0151 
0152                 required property int index
0153                 required property var source
0154 
0155                 text: source.url
0156                 width: ListView.view.width
0157 
0158                 onClicked: {
0159                     if (PlasmaTube.sourceManager.selectedSource !== sourceDelegate.source) {
0160                         PlasmaTube.sourceManager.selectedSource = sourceDelegate.source;
0161                         sources.currentIndex = sourceDelegate.index;
0162                     }
0163                     root.sourcesListVisible = false
0164                 }
0165             }
0166         }
0167 
0168     }
0169 }