[ui] TextFileViewer: simplify position restoring when source changes

Remove 'keepPosition' parameter: always try to reset position as close as possible to the previous state.
This commit is contained in:
Yann Lanthony 2019-05-15 20:16:45 +02:00
parent d7f8311dcf
commit 55c9e3063d
No known key found for this signature in database
GPG key ID: 519FAE6DF7A70642

View file

@ -41,7 +41,7 @@ Item {
ToolTip.text: "Reload" ToolTip.text: "Reload"
ToolTip.visible: hovered ToolTip.visible: hovered
font.family: MaterialIcons.fontFamily font.family: MaterialIcons.fontFamily
onClicked: loadSource(false) onClicked: loadSource()
} }
ToolButton { ToolButton {
text: MaterialIcons.vertical_align_top text: MaterialIcons.vertical_align_top
@ -51,6 +51,7 @@ Item {
onClicked: textView.positionViewAtBeginning() onClicked: textView.positionViewAtBeginning()
} }
ToolButton { ToolButton {
id: autoscroll
text: MaterialIcons.vertical_align_bottom text: MaterialIcons.vertical_align_bottom
ToolTip.text: "Scroll to Bottom" ToolTip.text: "Scroll to Bottom"
ToolTip.visible: hovered ToolTip.visible: hovered
@ -145,17 +146,21 @@ Item {
} }
} }
function setText(value, keepPosition) { function setText(value) {
// store cursor position and content position // store current first index
var topIndex = firstVisibleIndex(); var topIndex = firstVisibleIndex();
var scrollToBottom = atYEnd; // store whether autoscroll to bottom is active
var scrollToBottom = atYEnd && autoscroll.checked;
// replace text // replace text
text = value; text = value;
// restore content position by either:
// - autoscrolling to bottom
if(scrollToBottom) if(scrollToBottom)
positionViewAtEnd(); positionViewAtEnd();
else if(topIndex !== firstVisibleIndex() && keepPosition) // - setting first visible index back (when possible)
positionViewAtIndex(topIndex, ListView.Beginning); else if(topIndex !== firstVisibleIndex())
positionViewAtIndex(Math.min(topIndex, count-1), ListView.Beginning);
} }
function firstVisibleIndex() { function firstVisibleIndex() {
@ -306,15 +311,17 @@ Item {
interval: root.autoReloadInterval interval: root.autoReloadInterval
repeat: true repeat: true
// reload file on start and stop // reload file on start and stop
onRunningChanged: loadSource(true) onRunningChanged: loadSource()
onTriggered: loadSource(true) onTriggered: loadSource()
} }
// Load current source file and update ListView's model // Load current source file and update ListView's model
function loadSource(keepPosition) function loadSource()
{ {
if(!visible) if(!visible)
return; return;
loading = true; loading = true;
var xhr = new XMLHttpRequest; var xhr = new XMLHttpRequest;
@ -324,7 +331,7 @@ Item {
// that file has changed on disk (not always up-to-date) // that file has changed on disk (not always up-to-date)
// - instead, let QML engine evaluate whether 'text' property value has changed // - instead, let QML engine evaluate whether 'text' property value has changed
if(xhr.readyState === XMLHttpRequest.DONE) { if(xhr.readyState === XMLHttpRequest.DONE) {
textView.setText(xhr.status === 200 ? xhr.responseText : "", keepPosition); textView.setText(xhr.status === 200 ? xhr.responseText : "");
loading = false; loading = false;
} }
}; };