Warning, /network/tokodon/src/content/ui/UserInfo.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <fella@posteo.de>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 import QtQuick 2.15
0005 import QtQuick.Controls 2.15 as QQC2
0006 import QtQuick.Layouts 1.15
0007 import org.kde.kirigami 2.20 as Kirigami
0008 import org.kde.kirigamiaddons.delegates 1.0 as Delegates
0009 import org.kde.kirigamiaddons.labs.components 1.0 as KirigamiComponents
0010 
0011 import org.kde.kmasto 1.0
0012 
0013 QQC2.Pane {
0014     id: userInfo
0015 
0016     property alias accountsListVisible: accounts.visible
0017     property var addAccount
0018 
0019     visible: AccountManager.selectedAccount
0020     padding: 0
0021 
0022     function openAccountPage() {
0023         const accountId = AccountManager.selectedAccountId;
0024         if (!pageStack.currentItem.model || !pageStack.currentItem.model.accountId || accountId !== pageStack.currentItem.accountId) {
0025             const item = pageStack.push('qrc:/content/ui/AccountInfo.qml', {
0026                 accountId: accountId,
0027             });
0028         }
0029     }
0030 
0031     contentItem: ColumnLayout {
0032         id: content
0033 
0034         spacing: 0
0035 
0036         Delegates.RoundedItemDelegate {
0037             id: currentAccountDelegate
0038 
0039             text: AccountManager.selectedAccount ? AccountManager.selectedAccount.identity.displayNameHtml : ''
0040 
0041             onClicked: openAccountPage()
0042             Layout.fillWidth: true
0043 
0044             contentItem: RowLayout {
0045                 spacing: Kirigami.Units.smallSpacing
0046 
0047                 QQC2.AbstractButton {
0048                     Layout.preferredHeight: Kirigami.Units.iconSizes.medium
0049                     Layout.preferredWidth: Kirigami.Units.iconSizes.medium
0050                     Layout.leftMargin: Kirigami.Units.smallSpacing
0051                     Layout.rightMargin: Kirigami.Units.smallSpacing
0052 
0053                     contentItem: KirigamiComponents.Avatar {
0054                         name: AccountManager.selectedAccount ? AccountManager.selectedAccount.identity.displayName : 'User'
0055                         source: AccountManager.selectedAccount ? AccountManager.selectedAccount.identity.avatarUrl : ''
0056                     }
0057 
0058                     onClicked: openAccountPage()
0059                 }
0060 
0061                 Delegates.SubtitleContentItem {
0062                     subtitle: AccountManager.selectedAccount ? AccountManager.selectedAccount.instanceName : ''
0063                     subtitleItem.textFormat: Text.PlainText
0064                     itemDelegate: currentAccountDelegate
0065                     Layout.fillWidth: true
0066                 }
0067 
0068                 QQC2.ToolButton {
0069                     icon.name: "system-switch-user"
0070                     onClicked: {
0071                         userInfo.accountsListVisible = !userInfo.accountsListVisible
0072                     }
0073                     text: i18n("Switch user")
0074                     display: QQC2.AbstractButton.IconOnly
0075                     QQC2.ToolTip.text: text
0076                     QQC2.ToolTip.visible: hovered
0077                     QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
0078                     Layout.minimumWidth: Layout.preferredWidth
0079                 }
0080             }
0081         }
0082 
0083         ListView {
0084             id: accounts
0085 
0086             model: AccountManager
0087             currentIndex: AccountManager.selectedIndex
0088 
0089             header: Kirigami.Separator {}
0090 
0091             footer: Delegates.RoundedItemDelegate {
0092                 id: addAccountDelegaze
0093 
0094                 width: parent.width
0095                 highlighted: focus
0096                 icon {
0097                     name: "list-add"
0098                     width: Kirigami.Units.iconSizes.medium
0099                     height: Kirigami.Units.iconSizes.medium
0100                 }
0101                 text: i18n("Add Account")
0102                 enabled: AccountManager.hasAccounts && applicationWindow().pageStack.depth > 0 && applicationWindow().pageStack.get(0).objectName !== 'loginPage' && applicationWindow().pageStack.get(0).objectName !== 'authorizationPage' && (applicationWindow().pageStack.layers.depth === 1 || applicationWindow().pageStack.layers.get(1).objectName !== 'loginPage' && applicationWindow().pageStack.layers.get(1).objectName !== 'authorizationPage')
0103 
0104                 contentItem: Delegates.SubtitleContentItem {
0105                     itemDelegate: addAccountDelegaze
0106                     subtitle: i18n("Log in to an existing account")
0107                 }
0108 
0109                 onClicked: {
0110                     pageStack.layers.push('qrc:/content/ui/LoginPage.qml');
0111                     userInfo.accountsListVisible = false
0112                     accounts.currentIndex = AccountManager.selectedIndex
0113                 }
0114 
0115                 Component.onCompleted: userInfo.addAccount = this
0116                 Keys.onUpPressed: {
0117                     accounts.currentIndex = accounts.count - 1
0118                     accounts.forceActiveFocus()
0119                 }
0120                 Keys.onDownPressed: {
0121                     accounts.currentIndex = 0
0122                     accounts.forceActiveFocus()
0123                 }
0124             }
0125 
0126             visible: false
0127             onVisibleChanged: if (visible) focus = true
0128             clip: true
0129 
0130             keyNavigationEnabled: false
0131             Keys.onDownPressed: {
0132                 if (accounts.currentIndex === accounts.count - 1) {
0133                     addAccount.forceActiveFocus()
0134                     accounts.currentIndex = -1
0135                 } else {
0136                     accounts.incrementCurrentIndex()
0137                 }
0138             }
0139             Keys.onUpPressed: {
0140                 if (accounts.currentIndex === 0) {
0141                     addAccount.forceActiveFocus()
0142                     accounts.currentIndex = -1
0143                 } else {
0144                     accounts.decrementCurrentIndex()
0145                 }
0146             }
0147 
0148             Keys.onReleased: if (event.key == Qt.Key_Escape) {
0149                 userInfo.accountsListVisible = false
0150             }
0151 
0152             Layout.fillWidth: true
0153             Layout.preferredHeight: contentHeight
0154 
0155             delegate: Delegates.RoundedItemDelegate {
0156                 id: accountDelegate
0157 
0158                 required property int index
0159                 required property string displayName
0160                 required property string instance
0161                 required property var account
0162 
0163                 text: displayName
0164 
0165                 contentItem: RowLayout {
0166                     spacing: Kirigami.Units.smallSpacing
0167 
0168                     KirigamiComponents.Avatar {
0169                         source: accountDelegate.account.identity.avatarUrl
0170                         name: accountDelegate.displayName
0171                         Layout.preferredHeight: Kirigami.Units.iconSizes.medium
0172                         Layout.preferredWidth: Kirigami.Units.iconSizes.medium
0173                         Layout.leftMargin: Kirigami.Units.smallSpacing
0174                         Layout.rightMargin: Kirigami.Units.smallSpacing
0175                     }
0176 
0177                     Delegates.SubtitleContentItem {
0178                         itemDelegate: accountDelegate
0179                         subtitleItem.textFormat: Text.PlainText
0180                         subtitle: accountDelegate.instance
0181                         Layout.fillWidth: true
0182                     }
0183                 }
0184 
0185                 onClicked: {
0186                     if (AccountManager.selectedAccount !== accountDelegate.account) {
0187                         AccountManager.selectedAccount = accountDelegate.account;
0188                         accounts.currentIndex = accountDelegate.index;
0189                     }
0190                     userInfo.accountsListVisible = false
0191                 }
0192             }
0193         }
0194 
0195     }
0196 }