Skip to content

Commit 838ff00

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 838ff00

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

TextEditor.cpp

Lines changed: 16 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)
@@ -1394,13 +1395,13 @@ void TextEditor::SetColorizerEnable(bool aValue)
13941395
mColorizerEnabled = aValue;
13951396
}
13961397

1397-
void TextEditor::SetCursorPosition(const Coordinates & aPosition)
1398+
void TextEditor::SetCursorPosition(const Coordinates & aPosition, unsigned int cursorLineOnPage)
13981399
{
13991400
if (mState.mCursorPosition != aPosition)
14001401
{
14011402
mState.mCursorPosition = aPosition;
14021403
mCursorPositionChanged = true;
1403-
EnsureCursorVisible();
1404+
EnsureCursorVisible(cursorLineOnPage);
14041405
}
14051406
}
14061407

@@ -2418,11 +2419,12 @@ float TextEditor::TextDistanceToLineStart(const Coordinates& aFrom) const
24182419
return distance;
24192420
}
24202421

2421-
void TextEditor::EnsureCursorVisible()
2422+
void TextEditor::EnsureCursorVisible(unsigned int cursorLineOnPage)
24222423
{
24232424
if (!mWithinRender)
24242425
{
24252426
mScrollToCursor = true;
2427+
mScrollToCursor_CursorLineOnPage = cursorLineOnPage;
24262428
return;
24272429
}
24282430

@@ -2441,10 +2443,17 @@ void TextEditor::EnsureCursorVisible()
24412443
auto pos = GetActualCursorCoordinates();
24422444
auto len = TextDistanceToLineStart(pos);
24432445

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));
2446+
if (mScrollToCursor_CursorLineOnPage >= 0)
2447+
{
2448+
ImGui::SetScrollY(std::max(0.0f, (pos.mLine - mScrollToCursor_CursorLineOnPage) * mCharAdvance.y));
2449+
}
2450+
else
2451+
{
2452+
if (pos.mLine < top)
2453+
ImGui::SetScrollY(std::max(0.0f, (pos.mLine - 1) * mCharAdvance.y));
2454+
if (pos.mLine > bottom - 4)
2455+
ImGui::SetScrollY(std::max(0.0f, (pos.mLine + 4) * mCharAdvance.y - height));
2456+
}
24482457
if (len + mTextStart < left + 4)
24492458
ImGui::SetScrollX(std::max(0.0f, len + mTextStart - 4));
24502459
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, unsigned 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(unsigned 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)