diff --git a/src/gui/Base/HSortFilterProxyModel.qml b/src/gui/Base/HSortFilterProxyModel.qml new file mode 100644 index 00000000..54c9944d --- /dev/null +++ b/src/gui/Base/HSortFilterProxyModel.qml @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later + +import QtQuick 2.12 +import SortFilterProxyModel 0.2 + +SortFilterProxyModel { + function findIndex(id) { + for (let i = 0; i < count; i++) + if (get(i).id === id) return i + + return null + } + + function find(id) { + for (let i = 0; i < count; i++) + if (get(i).id === id) return get(i) + + return null + } +} diff --git a/src/gui/Base/HTileDelegate.qml b/src/gui/Base/HTileDelegate.qml index 7fa51ac3..ba9c6967 100644 --- a/src/gui/Base/HTileDelegate.qml +++ b/src/gui/Base/HTileDelegate.qml @@ -5,7 +5,6 @@ import QtQuick.Layouts 1.12 HTile { id: tile - onActivated: view.currentIndex = model.index onLeftClicked: { view.highlightRangeMode = ListView.NoHighlightRange view.highlightMoveDuration = 0 diff --git a/src/gui/MainPane/Account.qml b/src/gui/MainPane/Account.qml index 7d5578d0..4486adf5 100644 --- a/src/gui/MainPane/Account.qml +++ b/src/gui/MainPane/Account.qml @@ -43,9 +43,12 @@ HTileDelegate { } } - onActivated: pageLoader.showPage( - "AccountSettings/AccountSettings", { "userId": model.id } - ) + onActivated: { + pageLoader.showPage( + "AccountSettings/AccountSettings", { "userId": model.id } + ) + mainPaneList.detachedCurrentIndex = false + } readonly property alias addChat: addChat @@ -54,18 +57,6 @@ HTileDelegate { (window.uiState.collapseAccounts[model.id] || false) && ! mainPane.filter - readonly property bool shouldBeSelected: - ( - window.uiState.page === "Pages/AddChat/AddChat.qml" || - window.uiState.page === "Pages/AccountSettings/AccountSettings.qml" - ) && - window.uiState.pageProperties.userId === model.id - - - function becomeSelected() { - accountRooms.roomList.currentIndex = -1 - mainPaneList.currentIndex = index - } function setCollapse(collapse) { window.uiState.collapseAccounts[model.id] = collapse @@ -80,16 +71,6 @@ HTileDelegate { Behavior on opacity { HNumberAnimation {} } - // Trying to set the current item to ourself usually won't work from the - // first time, when this delegate is being initialized - Timer { - interval: 100 - repeat: true - running: shouldBeSelected && mainPaneList.currentIndex === -1 - triggeredOnStart: true - onTriggered: becomeSelected() - } - HButton { id: addChat iconItem.small: true diff --git a/src/gui/MainPane/AccountRoomsDelegate.qml b/src/gui/MainPane/AccountRoomsDelegate.qml index 006a4dc6..627c1730 100644 --- a/src/gui/MainPane/AccountRoomsDelegate.qml +++ b/src/gui/MainPane/AccountRoomsDelegate.qml @@ -36,7 +36,7 @@ Column { height: contentHeight interactive: false - model: SortFilterProxyModel { + model: HSortFilterProxyModel { sourceModel: ModelStore.get(accountRooms.userId, "rooms") filters: [ @@ -60,6 +60,19 @@ Column { highlight: null // managed by the AccountRoomsList + + Binding on currentIndex { + value: + window.uiState.page === "Pages/Chat/Chat.qml" && + window.uiState.pageProperties.userId === userId ? + + roomList.model.findIndex(window.uiState.pageProperties.roomId) + || -1 : + -1 + + when: ! view.detachedCurrentIndex + } + Behavior on height { HNumberAnimation {} } } } diff --git a/src/gui/MainPane/AccountRoomsList.qml b/src/gui/MainPane/AccountRoomsList.qml index f46aab11..d9dab4e6 100644 --- a/src/gui/MainPane/AccountRoomsList.qml +++ b/src/gui/MainPane/AccountRoomsList.qml @@ -1,7 +1,6 @@ // SPDX-License-Identifier: LGPL-3.0-or-later import QtQuick 2.12 -import SortFilterProxyModel 0.2 import ".." import "../Base" @@ -55,10 +54,15 @@ HListView { } + property bool detachedCurrentIndex: false + readonly property Room selectedRoom: currentItem ? currentItem.roomList.currentItem : null + function previous() { + detachedCurrentIndex = true + if (! mainPane.filter) { _previous() return @@ -96,13 +100,15 @@ HListView { if (currentAccount.collapsed || selectedIsFirst || noRooms) { // Have the account itself be selected - roomList.currentIndex = -1 + roomList.currentIndex = -1 // XXX } else { roomList.decrementCurrentIndex() } } function next() { + detachedCurrentIndex = true + if (! mainPane.filter) { _next() return @@ -142,7 +148,7 @@ HListView { const noRooms = roomList.count === 0 if (currentAccount.collapsed || selectedIsLast || noRooms) { - roomList.currentIndex = -1 + roomList.currentIndex = -1 // XXX mainPaneList.incrementCurrentIndex() } else { roomList.incrementCurrentIndex() @@ -159,20 +165,22 @@ HListView { selectedRoom ? currentItem.roomList.currentItem.activated() : currentItem.account.activated() + + detachedCurrentIndex = false } function accountSettings() { if (! currentItem) next() - - currentItem.roomList.currentIndex = -1 currentItem.account.activated() + + detachedCurrentIndex = false } function addNewChat() { if (! currentItem) next() - - currentItem.roomList.currentIndex = -1 currentItem.account.addChat.clicked() + + detachedCurrentIndex = false } function setCollapseAccount(collapse) { @@ -187,17 +195,20 @@ HListView { currentItem.account.toggleCollapse() } - function clearSelection() { - if (selectedRoom) currentItem.roomList.currentIndex = -1 - currentIndex = -1 - } - function forceUpdateSelection() { - // When the selection is cleared, if an account or room delegate is - // supposed to be selected, it will try to be so again. - clearSelection() - } + Binding on currentIndex { + value: + [ + "Pages/Chat/Chat.qml", + "Pages/AddChat/AddChat.qml", + "Pages/AccountSettings/AccountSettings.qml", + ].includes(window.uiState.page) ? + model.findIndex(window.uiState.pageProperties.userId) || -1 : + -1 + + when: ! detachedCurrentIndex + } Timer { id: activateLimiter diff --git a/src/gui/MainPane/MainPaneToolBar.qml b/src/gui/MainPane/MainPaneToolBar.qml index 0795facb..65b1c93d 100644 --- a/src/gui/MainPane/MainPaneToolBar.qml +++ b/src/gui/MainPane/MainPaneToolBar.qml @@ -19,10 +19,7 @@ HRowLayout { icon.name: "add-account" toolTip.text: qsTr("Add another account") backgroundColor: theme.mainPane.settingsButton.background - onClicked: { - mainPaneList.clearSelection() - pageLoader.showPage("AddAccount/AddAccount") - } + onClicked: pageLoader.showPage("AddAccount/AddAccount") Layout.fillHeight: true } diff --git a/src/gui/MainPane/Room.qml b/src/gui/MainPane/Room.qml index 4c03cea0..fa760740 100644 --- a/src/gui/MainPane/Room.qml +++ b/src/gui/MainPane/Room.qml @@ -136,8 +136,8 @@ HTileDelegate { } onActivated: { - becomeSelected() pageLoader.showRoom(userId, model.id) + mainPaneList.detachedCurrentIndex = false } @@ -152,32 +152,6 @@ HTileDelegate { readonly property QtObject lastEvent: eventModel.count > 0 ? eventModel.get(0) : null - readonly property bool shouldBeSelected: - window.uiState.page === "Pages/Chat/Chat.qml" && - window.uiState.pageProperties.userId === userId && - window.uiState.pageProperties.roomId === model.id - - - function becomeSelected() { - mainPaneList.currentIndex = accountRooms.listIndex - roomList.currentIndex = index - } - Behavior on opacity { HNumberAnimation {} } - - - // Trying to set the current item to ourself usually won't work from the - // first time, when this delegate is being initialized - Timer { - interval: 100 - repeat: true - running: - shouldBeSelected && - mainPaneList.currentIndex === -1 && - roomList.currentIndex === -1 - - triggeredOnStart: true - onTriggered: becomeSelected() - } } diff --git a/src/gui/ModelStore.qml b/src/gui/ModelStore.qml index 3f6e2387..9a693d40 100644 --- a/src/gui/ModelStore.qml +++ b/src/gui/ModelStore.qml @@ -14,6 +14,13 @@ QtObject { ListModel { property var modelId + function findIndex(id) { + for (let i = 0; i < count; i++) + if (get(i).id === id) return i + + return null + } + function find(id) { for (let i = 0; i < count; i++) if (get(i).id === id) return get(i) diff --git a/src/gui/PageLoader.qml b/src/gui/PageLoader.qml index e4a17f9a..527ce163 100644 --- a/src/gui/PageLoader.qml +++ b/src/gui/PageLoader.qml @@ -73,7 +73,6 @@ HLoader { window.uiState.page = componentUrl window.uiState.pageProperties = properties window.uiStateChanged() - mainUI.mainPane.mainPaneList.forceUpdateSelection() return true } diff --git a/src/gui/Pages/Chat/RoomPane/MemberView.qml b/src/gui/Pages/Chat/RoomPane/MemberView.qml index 1a168858..98f328c6 100644 --- a/src/gui/Pages/Chat/RoomPane/MemberView.qml +++ b/src/gui/Pages/Chat/RoomPane/MemberView.qml @@ -11,7 +11,7 @@ HColumnLayout { id: memberList clip: true - model: SortFilterProxyModel { + model: HSortFilterProxyModel { sourceModel: ModelStore.get(chat.userId, chat.roomId, "members") filters: ExpressionFilter { diff --git a/src/gui/Pages/Chat/Timeline/EventList.qml b/src/gui/Pages/Chat/Timeline/EventList.qml index e0afa63f..b824aca4 100644 --- a/src/gui/Pages/Chat/Timeline/EventList.qml +++ b/src/gui/Pages/Chat/Timeline/EventList.qml @@ -182,12 +182,6 @@ Rectangle { model: ModelStore.get(chat.userId, chat.roomId, "events") - // model: HSortFilterProxy { - // model: ModelStore.get(chat.userId, chat.roomId, "events") - // comparator: "date" - // descendingSort: true - // } - delegate: EventDelegate {} } }