Skip to content

Commit 180bbb8

Browse files
committed
fix: now cursor stays within bounds of the view
1 parent dad39ed commit 180bbb8

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ FetchContent_MakeAvailable(textedit)
2929
file(READ "${textedit_SOURCE_DIR}/TextEditor.cpp" TEXT_EDITOR_CPP)
3030
string(REPLACE "ImGui::PushItemFlag(ImGuiItemFlags_NoTabStop, false);" "/* patched no tab stop */" TEXT_EDITOR_CPP "${TEXT_EDITOR_CPP}")
3131
string(REPLACE "ImGui::PopItemFlag();" "/* patched pop item flag */" TEXT_EDITOR_CPP "${TEXT_EDITOR_CPP}")
32+
33+
set(SCROLL_ORIG " if (pos.mLine < top)
34+
ImGui::SetScrollY(std::max(0.0f, (pos.mLine - 1) * mCharAdvance.y));
35+
if (pos.mLine > bottom - 4)
36+
ImGui::SetScrollY(std::max(0.0f, (pos.mLine + 4) * mCharAdvance.y - height));
37+
if (len + mTextStart < left + 4)
38+
ImGui::SetScrollX(std::max(0.0f, len + mTextStart - 4));
39+
if (len + mTextStart > right - 4)
40+
ImGui::SetScrollX(std::max(0.0f, len + mTextStart + 4 - width));")
41+
42+
set(SCROLL_NEW " float padY = std::min(2.0f * mCharAdvance.y, height * 0.25f);
43+
float padX = std::min(4.0f * mCharAdvance.x, width * 0.25f);
44+
45+
float cursorPosY = pos.mLine * mCharAdvance.y;
46+
float cursorPosX = len + mTextStart;
47+
48+
if (cursorPosY < scrollY + padY)
49+
ImGui::SetScrollY(std::max(0.0f, cursorPosY - padY));
50+
if (cursorPosY + mCharAdvance.y > scrollY + height - padY)
51+
ImGui::SetScrollY(std::max(0.0f, cursorPosY + mCharAdvance.y - height + padY));
52+
53+
if (cursorPosX < scrollX + padX)
54+
ImGui::SetScrollX(std::max(0.0f, cursorPosX - padX));
55+
if (cursorPosX > scrollX + width - padX)
56+
ImGui::SetScrollX(std::max(0.0f, cursorPosX - width + padX));")
57+
58+
string(REPLACE "${SCROLL_ORIG}" "${SCROLL_NEW}" TEXT_EDITOR_CPP "${TEXT_EDITOR_CPP}")
59+
3260
file(WRITE "${textedit_SOURCE_DIR}/TextEditor.cpp" "${TEXT_EDITOR_CPP}")
3361

3462
# 4. Fetch Portable File Dialogs

0 commit comments

Comments
 (0)