Add visited pages history back/forward keys (#204)

* Add keys for back and forward through history

Move through history of visited rooms without altering it.
Ctrl+H and Ctrl+Y by default.
Deviating from history while in the middle of it
causes a part of history to be discarded.

* Change default history binding to Ctrl+H and Ctrl+Shift+H

* Rename functions and settings

`earlier_page` and `later_page` is consistent naming with `last_page`

* Add option to wrap history

If you press `later_page` while at index 0 (newest), you loop to the end
of recorded history (oldest). If you press `earlier_page` while at at
the end of history (oldest), you loop back to the top (newest).
This can be disabled.

* Add showNthFromHistory

Grouped duplicated code into a new funcion.

* Minor formatting changes

* Move wrap_history setting from to General section

The Keys section is purely for keybindings; this setting should also be
able to control history navigation not done by keyboard such as extra
mouse button bindings.

* Change default bind for later_page and clear_all

Ctrl+H and Ctrl+L are a more intuitive pair than Ctrl+H and Ctrl+Shift+H.
Meanwhile, clear_all does not need the accessible binding Ctrl+L.

Co-authored-by: miruka <miruka@disroot.org>
This commit is contained in:
Maze
2021-04-06 00:57:19 +02:00
committed by miruka
parent f88ab45b94
commit c140d69138
2 changed files with 60 additions and 7 deletions

View File

@@ -15,6 +15,7 @@ HLoader {
// List of previously loaded [componentUrl, {properties}]
property var history: []
property int historyLength: 20
property int historyPosition: 0
readonly property alias appearAnimation: appearAnimation
@@ -22,9 +23,18 @@ HLoader {
signal recycled()
signal previousShown(string componentUrl, var properties)
function show(componentUrl, properties={}) {
history.unshift([componentUrl, properties])
if (history.length > historyLength) history.pop()
function show(componentUrl, properties={}, alterHistory=true) {
if (alterHistory) {
// A new branch of history will be added.
// The new branch replaces everything after the current point.
while (historyPosition > 0) {
history.shift()
historyPosition--
}
// Add entry to history
history.unshift([componentUrl, properties])
if (history.length > historyLength) history.pop()
}
const recycle =
window.uiState.page === componentUrl &&
@@ -51,13 +61,37 @@ HLoader {
show("Pages/Chat/Chat.qml", {userRoomId: [userId, roomId]})
}
function showNthFromHistory(n, alterHistory=true) {
const [componentUrl, properties] = history[n]
show(componentUrl, properties, alterHistory)
previousShown(componentUrl, properties)
}
function showPrevious(timesBack=1) {
timesBack = Math.min(timesBack, history.length - 1)
if (timesBack < 1) return false
const [componentUrl, properties] = history[timesBack]
show(componentUrl, properties)
previousShown(componentUrl, properties)
showNthFromHistory(timesBack)
return true
}
function moveThroughHistory(relativeMovement=1) {
if (history.length === 0) return false
// Going beyond oldest entry in history
if (historyPosition + relativeMovement >= history.length) {
if (! window.settings.General.wrap_history) return false
relativeMovement -= history.length
// Going beyond newest entry in history
} else if (historyPosition + relativeMovement < 0){
if (! window.settings.General.wrap_history) return false
relativeMovement += history.length
}
historyPosition += relativeMovement
showNthFromHistory(historyPosition, false)
return true
}
@@ -94,4 +128,14 @@ HLoader {
sequences: window.settings.Keys.last_page
onActivated: showPrevious()
}
HShortcut {
sequences: window.settings.Keys.earlier_page
onActivated: moveThroughHistory(1)
}
HShortcut {
sequences: window.settings.Keys.later_page
onActivated: moveThroughHistory(-1)
}
}