Fix for not resetting the scroll position back to the top when SetText or SetTextLines is called. This causes the scroll position to remain where it is when for instance loading another file from disk. Expected behavior would be to scroll back to the top, instead of presenting the new file somewhere in the middle (or, cause often, at the end).
diff --git a/TextEditor.cpp b/TextEditor.cpp index 963e754..d8a2acd 100644 --- a/TextEditor.cpp +++ b/TextEditor.cpp
@@ -33,7 +33,8 @@ , mOverwrite(false) , mReadOnly(false) , mWithinRender(false) - , mScrollToCursor(false) + , mScrollToCursor(false) + , mScrollToTop(false) , mTextChanged(false) , mTextStart(20.0f) , mLeftMargin(10) @@ -617,7 +618,13 @@ static std::string buffer; auto contentSize = ImGui::GetWindowContentRegionMax(); auto drawList = ImGui::GetWindowDrawList(); - float longest(mTextStart); + float longest(mTextStart); + + if (mScrollToTop) + { + mScrollToTop = false; + ImGui::SetScrollY(0.f); + } ImVec2 cursorScreenPos = ImGui::GetCursorScreenPos(); auto scrollX = ImGui::GetScrollX(); @@ -847,9 +854,10 @@ { mLines.back().emplace_back(Glyph(chr, PaletteIndex::Default)); } - - mTextChanged = true; - } + } + + mTextChanged = true; + mScrollToTop = true; mUndoBuffer.clear(); @@ -878,7 +886,8 @@ } } - mTextChanged = true; + mTextChanged = true; + mScrollToTop = true; mUndoBuffer.clear();
diff --git a/TextEditor.h b/TextEditor.h index ac03ad4..3ca3813 100644 --- a/TextEditor.h +++ b/TextEditor.h
@@ -327,7 +327,8 @@ bool mOverwrite; bool mReadOnly; bool mWithinRender; - bool mScrollToCursor; + bool mScrollToCursor; + bool mScrollToTop; bool mTextChanged; float mTextStart; // position (in pixels) where a code line starts relative to the left of the TextEditor. int mLeftMargin;