Create new Account Switcher

This commit is contained in:
Tobias Fella
2022-11-13 15:54:42 +00:00
parent cfe3825d63
commit 90cbe37a92
5 changed files with 220 additions and 14 deletions

View File

@@ -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
}

View File

@@ -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,

View File

@@ -0,0 +1,196 @@
// SPDX-FileCopyrightText: 2022 Tobias Fella <fella@posteo.de>
// 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
}
}
}
}

View File

@@ -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
}
}

View File

@@ -17,6 +17,7 @@
<file alias="PushNotification.qml">qml/RoomSettings/PushNotification.qml</file>
<file alias="Categories.qml">qml/RoomSettings/Categories.qml</file>
<file alias="FullScreenImage.qml">qml/Component/FullScreenImage.qml</file>
<file alias="UserInfo.qml">qml/Component/UserInfo.qml</file>
<file alias="FancyEffectsContainer.qml">qml/Component/FancyEffectsContainer.qml</file>
<file alias="TypingPane.qml">qml/Component/TypingPane.qml</file>
<file alias="ShimmerGradient.qml">qml/Component/ShimmerGradient.qml</file>