From 90cbe37a92805d74e6c2b39b2e6c6438336c3fd5 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sun, 13 Nov 2022 15:54:42 +0000 Subject: [PATCH] Create new Account Switcher --- src/controller.cpp | 17 +++ src/controller.h | 3 + src/qml/Component/UserInfo.qml | 196 +++++++++++++++++++++++++++++++++ src/qml/Page/RoomListPage.qml | 17 +-- src/res.qrc | 1 + 5 files changed, 220 insertions(+), 14 deletions(-) create mode 100644 src/qml/Component/UserInfo.qml diff --git a/src/controller.cpp b/src/controller.cpp index 7c4e0bbb5..9bce811a4 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -813,3 +813,20 @@ void Controller::setApplicationProxy() QNetworkProxy::setApplicationProxy(proxy); } + +int Controller::activeConnectionIndex() const +{ +#ifdef QUOTIENT_07 + auto result = std::find_if(Accounts.accounts().begin(), Accounts.accounts().end(), [this](const auto &it) { + return it == m_connection; + }); + return result - Accounts.accounts().begin(); +#else + for (int i = 0; i < AccountRegistry::instance().rowCount(); i++) { + if (AccountRegistry::instance().data(AccountRegistry::instance().index(i, 0), AccountRegistry::UserIdRole).toString() == m_connection->userId()) { + return i; + } + } + return 0; +#endif +} diff --git a/src/controller.h b/src/controller.h index 9364788fe..72f7f0946 100644 --- a/src/controller.h +++ b/src/controller.h @@ -41,6 +41,7 @@ class Controller : public QObject Q_PROPERTY(bool hasWindowSystem READ hasWindowSystem CONSTANT) Q_PROPERTY(bool isOnline READ isOnline NOTIFY isOnlineChanged) Q_PROPERTY(bool encryptionSupported READ encryptionSupported CONSTANT) + Q_PROPERTY(int activeConnectionIndex READ activeConnectionIndex CONSTANT) public: static Controller &instance(); @@ -73,6 +74,8 @@ public: bool saveAccessTokenToFile(const Quotient::AccountSettings &account, const QByteArray &accessToken); bool saveAccessTokenToKeyChain(const Quotient::AccountSettings &account, const QByteArray &accessToken); + int activeConnectionIndex() const; + enum PasswordStatus { Success, Wrong, diff --git a/src/qml/Component/UserInfo.qml b/src/qml/Component/UserInfo.qml new file mode 100644 index 000000000..ac8fef2a0 --- /dev/null +++ b/src/qml/Component/UserInfo.qml @@ -0,0 +1,196 @@ +// SPDX-FileCopyrightText: 2022 Tobias Fella +// SPDX-License-Identifier: GPL-2.0-or-later + +import QtQuick 2.15 +import QtQuick.Controls 2.15 as QQC2 +import QtQuick.Layouts 1.15 +import org.kde.kirigami 2.20 as Kirigami + +import org.kde.neochat 1.0 + +QQC2.ToolBar { + id: userInfo + + padding: 0 + + height: content.height + + property alias accountsListVisible: accounts.visible + property var addAccount + + ColumnLayout { + id: content + width: parent.width + + spacing: 0 + + ListView { + id: accounts + + currentIndex: Controller.activeConnectionIndex + + header: Kirigami.Separator {} + + footer: Kirigami.BasicListItem { + width: parent.width + highlighted: focus + background: Rectangle { + id: background + color: addAccount.backgroundColor + + Rectangle { + anchors.fill: parent + visible: !Kirigami.Settings.tabletMode && addAccount.hoverEnabled + color: addAccount.activeBackgroundColor + opacity: { + if ((addAccount.highlighted || addAccount.ListView.isCurrentItem) && !addAccount.pressed) { + return .6 + } else if (addAccount.hovered && !addAccount.pressed) { + return .3 + } else { + return 0 + } + } + } + } + Component.onCompleted: userInfo.addAccount = this + icon: "list-add" + text: i18n("Add Account") + subtitle: i18n("Log in to an existing account") + onClicked: { + pageStack.pushDialogLayer("qrc:/WelcomePage.qml") + userInfo.accountsListVisible = false + accounts.currentIndex = Controller.activeConnectionIndex + } + Keys.onUpPressed: { + accounts.currentIndex = accounts.count - 1 + accounts.forceActiveFocus() + } + Keys.onDownPressed: { + accounts.currentIndex = 0 + accounts.forceActiveFocus() + } + } + + visible: false + onVisibleChanged: if (visible) focus = true + clip: true + model: AccountRegistry + + keyNavigationEnabled: false + Keys.onDownPressed: { + if (accounts.currentIndex === accounts.count - 1) { + addAccount.forceActiveFocus() + accounts.currentIndex = -1 + } else { + accounts.incrementCurrentIndex() + } + } + Keys.onUpPressed: { + if (accounts.currentIndex === 0) { + addAccount.forceActiveFocus() + accounts.currentIndex = -1 + } else { + accounts.decrementCurrentIndex() + } + } + + Keys.onReleased: if (event.key == Qt.Key_Escape) { + userInfo.accountsListVisible = false + } + + width: parent.width + height: contentHeight + delegate: Kirigami.BasicListItem { + leftPadding: topPadding + leading: Kirigami.Avatar { + width: height + source: model.connection.localUser.avatarMediaId ? ("image://mxc/" + model.connection.localUser.avatarMediaId) : "" + name: model.connection.localUser.displayName ?? model.connection.localUser.id + } + width: parent.width + text: model.connection.localUser.displayName + labelItem.textFormat: Text.PlainText + subtitleItem.textFormat: Text.PlainText + subtitle: model.connection.localUser.id + + onClicked: { + Controller.activeConnection = model.connection + userInfo.accountsListVisible = false + } + } + } + + Kirigami.Separator { + Layout.fillWidth: true + } + + RowLayout { + Layout.preferredHeight: Kirigami.Units.gridUnit * 3 + Item { + Layout.fillHeight: true + Layout.preferredWidth: height + Kirigami.Avatar { + readonly property string mediaId: Controller.activeConnection.localUser.avatarMediaId + anchors.fill: parent + anchors.margins: Kirigami.Units.smallSpacing + source: mediaId ? ("image://mxc/" + mediaId) : "" + name: Controller.activeConnection.localUser.displayName ?? Controller.activeConnection.localUser.id + } + } + ColumnLayout { + spacing: 0 + QQC2.Label { + id: displayNameLabel + text: Controller.activeConnection.localUser.displayName + textFormat: Text.PlainText + elide: Text.ElideRight + } + QQC2.Label { + text: (Controller.activeConnection.localUser.accountLabel.length > 0 ? (Controller.activeConnection.localUser.accountLabel + " ") : "") + Controller.activeConnection.localUser.id + font.pointSize: displayNameLabel.font.pointSize * 0.8 + opacity: 0.7 + textFormat: Text.PlainText + elide: Text.ElideRight + } + } + Item { + Layout.fillWidth: true + } + QQC2.ToolButton { + icon.name: "system-switch-user" + onClicked: { + userInfo.accountsListVisible = !userInfo.accountsListVisible + } + text: i18n("Switch User") + display: QQC2.AbstractButton.IconOnly + Accessible.name: text + QQC2.ToolTip.text: text + QQC2.ToolTip.visible: hovered + QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay + } + QQC2.ToolButton { + icon.name: "list-add" + onClicked: ; //TODO + text: i18n("Add") //TODO find better message + display: QQC2.AbstractButton.IconOnly + QQC2.ToolTip.text: text + QQC2.ToolTip.visible: hovered + QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay + visible: false + } + QQC2.ToolButton { + icon.name: "settings-configure" + onClicked: pageStack.pushDialogLayer("qrc:/SettingsPage.qml", {}, { title: i18n("Configure") }) + text: i18n("Open Settings") + display: QQC2.AbstractButton.IconOnly + QQC2.ToolTip { + text: parent.text + } + } + Item { + width: 1 + } + } + } +} diff --git a/src/qml/Page/RoomListPage.qml b/src/qml/Page/RoomListPage.qml index 84b8b356e..e75c38fe3 100644 --- a/src/qml/Page/RoomListPage.qml +++ b/src/qml/Page/RoomListPage.qml @@ -165,7 +165,7 @@ Kirigami.ScrollablePage { id: listView activeFocusOnTab: true - clip: accountList.count > 1 + clip: AccountRegistry.count > 1 header: QQC2.ItemDelegate { visible: page.collapsedMode @@ -376,18 +376,7 @@ Kirigami.ScrollablePage { } } - footer: QQC2.ToolBar { - visible: AccountRegistry.accountCount > 1 && !collapsedMode - height: visible ? implicitHeight : 0 - - contentItem: QQC2.ComboBox { - id: accountList - - model: AccountRegistry - textRole: "userId" - valueRole: "connection" - onActivated: Controller.activeConnection = currentValue - Component.onCompleted: currentIndex = indexOfValue(Controller.activeConnection) - } + footer: UserInfo { + width: parent.width } } diff --git a/src/res.qrc b/src/res.qrc index c43f5b415..f226288f0 100644 --- a/src/res.qrc +++ b/src/res.qrc @@ -17,6 +17,7 @@ qml/RoomSettings/PushNotification.qml qml/RoomSettings/Categories.qml qml/Component/FullScreenImage.qml + qml/Component/UserInfo.qml qml/Component/FancyEffectsContainer.qml qml/Component/TypingPane.qml qml/Component/ShimmerGradient.qml