moment/src/gui/MainPane/AccountRoomsList.qml

228 lines
5.9 KiB
QML
Raw Normal View History

// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import ".."
import "../Base"
HListView {
id: mainPaneList
model: ModelStore.get("accounts")
spacing: mainPane.small ? theme.spacing : 0
delegate: AccountRoomsDelegate {
width: mainPaneList.width
height: childrenRect.height
}
// Must handle the highlight's position and size manually because
// of our nested lists
highlightFollowsCurrentItem: false
highlightRangeMode: ListView.NoHighlightRange
highlight: Rectangle {
id: highlightRectangle
y:
selectedRoom ?
currentItem.y + currentItem.account.height +
currentItem.roomList.currentItem.y :
currentItem.y
width: mainPaneList.width
height:
selectedRoom ?
currentItem.roomList.currentItem.height :
currentItem.account.height
color:
mainPane.small ?
theme.controls.listView.smallPaneHighlight :
theme.controls.listView.highlight
2020-02-14 10:24:42 -04:00
Behavior on y { HNumberAnimation { id: yAnimation } }
Behavior on height { HNumberAnimation {} }
Behavior on color { HColorAnimation {} }
Binding {
target: mainPaneList
property: "contentY"
value: highlightRectangle.y + highlightRectangle.height / 2 -
mainPaneList.height / 2
delayed: true
2020-02-14 10:24:42 -04:00
when: yAnimation.running
}
2020-03-07 09:32:56 -04:00
Connections {
target: mainPaneList
enabled: yAnimation.running
onContentYChanged: mainPaneList.returnToBounds()
}
}
2020-03-10 05:38:28 -04:00
property bool detachedCurrentIndex: false
readonly property Room selectedRoom:
currentItem ? currentItem.roomList.currentItem : null
2020-03-10 06:07:09 -04:00
readonly property bool hasActiveAccount:
window.uiState.page === "Pages/Chat/Chat.qml" ||
window.uiState.page === "Pages/AddChat/AddChat.qml" ||
window.uiState.page === "Pages/AccountSettings/AccountSettings.qml"
readonly property var activeAccountIndex:
hasActiveAccount ?
model.findIndex(window.uiState.pageProperties.userId) : null
2020-03-10 05:38:28 -04:00
function previous() {
2020-03-10 05:38:28 -04:00
detachedCurrentIndex = true
if (! mainPane.filter) {
_previous()
return
}
let reachedStart = false
do {
if (currentIndex === count - 1 && reachedStart) break
_previous()
if (currentIndex === 0) reachedStart = true
} while (! currentItem.roomList.currentItem)
}
function _previous() {
2020-03-08 04:46:20 -04:00
const currentAccount = currentItem
// Nothing is selected
if (! currentAccount) {
decrementCurrentIndex()
}
2020-03-08 04:46:20 -04:00
const roomList = currentAccount.roomList
// An account is selected
if (! roomList.currentItem) {
decrementCurrentIndex()
// Select the last room of the previous account that's now selected
currentItem.roomList.decrementCurrentIndex()
return
}
// A room is selected
const selectedIsFirst = roomList.currentIndex === 0
const noRooms = roomList.count === 0
if (currentAccount.collapsed || selectedIsFirst || noRooms) {
// Have the account itself be selected
2020-03-10 05:38:28 -04:00
roomList.currentIndex = -1 // XXX
} else {
roomList.decrementCurrentIndex()
}
}
function next() {
2020-03-10 05:38:28 -04:00
detachedCurrentIndex = true
if (! mainPane.filter) {
_next()
return
}
let reachedEnd = false
do {
if (currentIndex === 0 && reachedEnd) break
_next()
if (currentIndex === count - 1) reachedEnd = true
} while (! currentItem.roomList.currentItem)
}
function _next() {
const currentAccount = currentItem
// Nothing is selected
if (! currentAccount) {
incrementCurrentIndex()
return
}
const roomList = currentAccount.roomList
// An account is selected
if (! roomList.currentItem) {
if (currentAccount.collapsed || roomList.count === 0) {
incrementCurrentIndex()
} else {
roomList.incrementCurrentIndex()
}
return
}
// A room is selected
const selectedIsLast = roomList.currentIndex >= roomList.count - 1
const noRooms = roomList.count === 0
if (currentAccount.collapsed || selectedIsLast || noRooms) {
2020-03-10 05:38:28 -04:00
roomList.currentIndex = -1 // XXX
mainPaneList.incrementCurrentIndex()
} else {
roomList.incrementCurrentIndex()
}
}
function requestActivate() {
activateLimiter.restart()
}
function activate() {
if (! currentItem) next()
selectedRoom ?
currentItem.roomList.currentItem.activated() :
currentItem.account.activated()
2020-03-10 05:38:28 -04:00
detachedCurrentIndex = false
}
function accountSettings() {
if (! currentItem) next()
currentItem.account.activated()
2020-03-10 05:38:28 -04:00
detachedCurrentIndex = false
}
function addNewChat() {
if (! currentItem) next()
currentItem.account.addChat.clicked()
2020-03-10 05:38:28 -04:00
detachedCurrentIndex = false
}
function setCollapseAccount(collapse) {
if (! currentItem) return
currentItem.account.setCollapse(collapse)
}
function toggleCollapseAccount() {
if (mainPane.filter) return
if (! currentItem) next()
currentItem.account.toggleCollapse()
}
2020-03-10 05:38:28 -04:00
Binding on currentIndex {
value:
2020-03-10 06:07:09 -04:00
hasActiveAccount ?
(activeAccountIndex === null ? -1 : activeAccountIndex) : -1
2020-03-10 05:38:28 -04:00
when: ! detachedCurrentIndex
}
Timer {
id: activateLimiter
interval: 200
onTriggered: activate()
}
}