Merge pull request #58 from marcel303/scrollFixOnSetText

Fix for not resetting the scroll position back to the top when SetText or SetTextLines is called.
diff --git a/TextEditor.cpp b/TextEditor.cpp
index d9a81e2..277344c 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)

@@ -627,7 +628,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();

@@ -857,9 +864,10 @@
 		{

 			mLines.back().emplace_back(Glyph(chr, PaletteIndex::Default));

 		}

-

-		mTextChanged = true;

-	}

+	}
+	
+	mTextChanged = true;
+	mScrollToTop = true;

 

 	mUndoBuffer.clear();
 	mUndoIndex = 0;

@@ -889,7 +897,8 @@
 		}

 	}

 

-	mTextChanged = true;

+	mTextChanged = true;
+	mScrollToTop = true;

 

 	mUndoBuffer.clear();
 	mUndoIndex = 0;

diff --git a/TextEditor.h b/TextEditor.h
index 93a6bd7..0f32ed9 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;