Skip to content

Commit 3c2f3c5

Browse files
committed
SetCursorPosition: can specify desired cursor line location on the page
* SetCursorPosition(aPosition) : behaves as before * SetCursorPosition(aPosition, 3): will change the cursor position, and then on the subsequent call to EnsureCursorIsVisible(), will make the cursor line appear as the third line on the page.
1 parent 0a88824 commit 3c2f3c5

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

TextEditor.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ TextEditor::TextEditor()
3232
, mReadOnly(false)
3333
, mWithinRender(false)
3434
, mScrollToCursor(false)
35+
, mScrollToCursor_CursorLineOnPage(-1)
3536
, mScrollToTop(false)
3637
, mTextChanged(false)
3738
, mColorizerEnabled(true)
@@ -1115,6 +1116,7 @@ void TextEditor::Render()
11151116
EnsureCursorVisible();
11161117
ImGui::SetWindowFocus();
11171118
mScrollToCursor = false;
1119+
mScrollToCursor_CursorLineOnPage = -1;
11181120
}
11191121
}
11201122

@@ -1394,13 +1396,13 @@ void TextEditor::SetColorizerEnable(bool aValue)
13941396
mColorizerEnabled = aValue;
13951397
}
13961398

1397-
void TextEditor::SetCursorPosition(const Coordinates & aPosition)
1399+
void TextEditor::SetCursorPosition(const Coordinates & aPosition, int cursorLineOnPage)
13981400
{
13991401
if (mState.mCursorPosition != aPosition)
14001402
{
14011403
mState.mCursorPosition = aPosition;
14021404
mCursorPositionChanged = true;
1403-
EnsureCursorVisible();
1405+
EnsureCursorVisible(cursorLineOnPage);
14041406
}
14051407
}
14061408

@@ -2418,11 +2420,12 @@ float TextEditor::TextDistanceToLineStart(const Coordinates& aFrom) const
24182420
return distance;
24192421
}
24202422

2421-
void TextEditor::EnsureCursorVisible()
2423+
void TextEditor::EnsureCursorVisible(int cursorLineOnPage)
24222424
{
24232425
if (!mWithinRender)
24242426
{
24252427
mScrollToCursor = true;
2428+
mScrollToCursor_CursorLineOnPage = cursorLineOnPage;
24262429
return;
24272430
}
24282431

@@ -2441,10 +2444,17 @@ void TextEditor::EnsureCursorVisible()
24412444
auto pos = GetActualCursorCoordinates();
24422445
auto len = TextDistanceToLineStart(pos);
24432446

2444-
if (pos.mLine < top)
2445-
ImGui::SetScrollY(std::max(0.0f, (pos.mLine - 1) * mCharAdvance.y));
2446-
if (pos.mLine > bottom - 4)
2447-
ImGui::SetScrollY(std::max(0.0f, (pos.mLine + 4) * mCharAdvance.y - height));
2447+
if (mScrollToCursor_CursorLineOnPage >= 0)
2448+
{
2449+
ImGui::SetScrollY(std::max(0.0f, (pos.mLine - mScrollToCursor_CursorLineOnPage) * mCharAdvance.y));
2450+
}
2451+
else
2452+
{
2453+
if (pos.mLine < top)
2454+
ImGui::SetScrollY(std::max(0.0f, (pos.mLine - 1) * mCharAdvance.y));
2455+
if (pos.mLine > bottom - 4)
2456+
ImGui::SetScrollY(std::max(0.0f, (pos.mLine + 4) * mCharAdvance.y - height));
2457+
}
24482458
if (len + mTextStart < left + 4)
24492459
ImGui::SetScrollX(std::max(0.0f, len + mTextStart - 4));
24502460
if (len + mTextStart > right - 4)

TextEditor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class TextEditor
216216
void SetColorizerEnable(bool aValue);
217217

218218
Coordinates GetCursorPosition() const { return GetActualCursorCoordinates(); }
219-
void SetCursorPosition(const Coordinates& aPosition);
219+
void SetCursorPosition(const Coordinates& aPosition, int cursorLineOnPage = -1);
220220

221221
inline void SetHandleMouseInputs (bool aValue){ mHandleMouseInputs = aValue;}
222222
inline bool IsHandleMouseInputsEnabled() const { return mHandleKeyboardInputs; }
@@ -316,7 +316,7 @@ class TextEditor
316316
void ColorizeRange(int aFromLine = 0, int aToLine = 0);
317317
void ColorizeInternal();
318318
float TextDistanceToLineStart(const Coordinates& aFrom) const;
319-
void EnsureCursorVisible();
319+
void EnsureCursorVisible(int cursorLineOnPage = -1);
320320
int GetPageSize() const;
321321
std::string GetText(const Coordinates& aStart, const Coordinates& aEnd) const;
322322
Coordinates GetActualCursorCoordinates() const;
@@ -359,6 +359,7 @@ class TextEditor
359359
bool mReadOnly;
360360
bool mWithinRender;
361361
bool mScrollToCursor;
362+
int mScrollToCursor_CursorLineOnPage;
362363
bool mScrollToTop;
363364
bool mTextChanged;
364365
bool mColorizerEnabled;

0 commit comments

Comments
 (0)