diff --git a/TextEditor.cpp b/TextEditor.cpp index 02966f0b..69ffc3c4 100644 --- a/TextEditor.cpp +++ b/TextEditor.cpp @@ -32,6 +32,7 @@ TextEditor::TextEditor() , mReadOnly(false) , mWithinRender(false) , mScrollToCursor(false) + , mScrollToCursor_CursorLineOnPage(-1) , mScrollToTop(false) , mTextChanged(false) , mColorizerEnabled(true) @@ -711,55 +712,55 @@ void TextEditor::HandleKeyboardInputs() io.WantCaptureKeyboard = true; io.WantTextInput = true; - if (!IsReadOnly() && ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Z))) + if (!IsReadOnly() && ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Z)) Undo(); - else if (!IsReadOnly() && !ctrl && !shift && alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Backspace))) + else if (!IsReadOnly() && !ctrl && !shift && alt && ImGui::IsKeyPressed(ImGuiKey_Backspace)) Undo(); - else if (!IsReadOnly() && ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Y))) + else if (!IsReadOnly() && ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Y)) Redo(); - else if (!ctrl && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_UpArrow))) + else if (!ctrl && !alt && ImGui::IsKeyPressed(ImGuiKey_UpArrow)) MoveUp(1, shift); - else if (!ctrl && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_DownArrow))) + else if (!ctrl && !alt && ImGui::IsKeyPressed(ImGuiKey_DownArrow)) MoveDown(1, shift); - else if (!alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_LeftArrow))) + else if (!alt && ImGui::IsKeyPressed(ImGuiKey_LeftArrow)) MoveLeft(1, shift, ctrl); - else if (!alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_RightArrow))) + else if (!alt && ImGui::IsKeyPressed(ImGuiKey_RightArrow)) MoveRight(1, shift, ctrl); - else if (!alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_PageUp))) + else if (!alt && ImGui::IsKeyPressed(ImGuiKey_PageUp)) MoveUp(GetPageSize() - 4, shift); - else if (!alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_PageDown))) + else if (!alt && ImGui::IsKeyPressed(ImGuiKey_PageDown)) MoveDown(GetPageSize() - 4, shift); - else if (!alt && ctrl && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Home))) + else if (!alt && ctrl && ImGui::IsKeyPressed(ImGuiKey_Home)) MoveTop(shift); - else if (ctrl && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_End))) + else if (ctrl && !alt && ImGui::IsKeyPressed(ImGuiKey_End)) MoveBottom(shift); - else if (!ctrl && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Home))) + else if (!ctrl && !alt && ImGui::IsKeyPressed(ImGuiKey_Home)) MoveHome(shift); - else if (!ctrl && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_End))) + else if (!ctrl && !alt && ImGui::IsKeyPressed(ImGuiKey_End)) MoveEnd(shift); - else if (!IsReadOnly() && !ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Delete))) + else if (!IsReadOnly() && !ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Delete)) Delete(); - else if (!IsReadOnly() && !ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Backspace))) + else if (!IsReadOnly() && !ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Backspace)) Backspace(); - else if (!ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Insert))) + else if (!ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Insert)) mOverwrite ^= true; - else if (ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Insert))) + else if (ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Insert)) Copy(); - else if (ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_C))) + else if (ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_C)) Copy(); - else if (!IsReadOnly() && !ctrl && shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Insert))) + else if (!IsReadOnly() && !ctrl && shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Insert)) Paste(); - else if (!IsReadOnly() && ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_V))) + else if (!IsReadOnly() && ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_V)) Paste(); - else if (ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_X))) + else if (ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_X)) Cut(); - else if (!ctrl && shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Delete))) + else if (!ctrl && shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Delete)) Cut(); - else if (ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_A))) + else if (ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_A)) SelectAll(); - else if (!IsReadOnly() && !ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Enter))) + else if (!IsReadOnly() && !ctrl && !shift && !alt && ImGui::IsKeyPressed(ImGuiKey_Enter)) EnterCharacter('\n', false); - else if (!IsReadOnly() && !ctrl && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Tab))) + else if (!IsReadOnly() && !ctrl && !alt && ImGui::IsKeyPressed(ImGuiKey_Tab)) EnterCharacter('\t', shift); if (!IsReadOnly() && !io.InputQueueCharacters.empty()) @@ -1113,8 +1114,8 @@ void TextEditor::Render() if (mScrollToCursor) { EnsureCursorVisible(); - ImGui::SetWindowFocus(); mScrollToCursor = false; + mScrollToCursor_CursorLineOnPage = -1; } } @@ -1394,13 +1395,13 @@ void TextEditor::SetColorizerEnable(bool aValue) mColorizerEnabled = aValue; } -void TextEditor::SetCursorPosition(const Coordinates & aPosition) +void TextEditor::SetCursorPosition(const Coordinates & aPosition, int cursorLineOnPage) { if (mState.mCursorPosition != aPosition) { mState.mCursorPosition = aPosition; mCursorPositionChanged = true; - EnsureCursorVisible(); + EnsureCursorVisible(cursorLineOnPage); } } @@ -2418,11 +2419,12 @@ float TextEditor::TextDistanceToLineStart(const Coordinates& aFrom) const return distance; } -void TextEditor::EnsureCursorVisible() +void TextEditor::EnsureCursorVisible(int cursorLineOnPage) { if (!mWithinRender) { mScrollToCursor = true; + mScrollToCursor_CursorLineOnPage = cursorLineOnPage; return; } @@ -2441,10 +2443,17 @@ void TextEditor::EnsureCursorVisible() auto pos = GetActualCursorCoordinates(); auto len = TextDistanceToLineStart(pos); - if (pos.mLine < top) - ImGui::SetScrollY(std::max(0.0f, (pos.mLine - 1) * mCharAdvance.y)); - if (pos.mLine > bottom - 4) - ImGui::SetScrollY(std::max(0.0f, (pos.mLine + 4) * mCharAdvance.y - height)); + if (mScrollToCursor_CursorLineOnPage >= 0) + { + ImGui::SetScrollY(std::max(0.0f, (pos.mLine - mScrollToCursor_CursorLineOnPage) * mCharAdvance.y)); + } + else + { + if (pos.mLine < top) + ImGui::SetScrollY(std::max(0.0f, (pos.mLine - 1) * mCharAdvance.y)); + if (pos.mLine > bottom - 4) + ImGui::SetScrollY(std::max(0.0f, (pos.mLine + 4) * mCharAdvance.y - height)); + } if (len + mTextStart < left + 4) ImGui::SetScrollX(std::max(0.0f, len + mTextStart - 4)); if (len + mTextStart > right - 4) diff --git a/TextEditor.h b/TextEditor.h index bd52e131..3c57359a 100644 --- a/TextEditor.h +++ b/TextEditor.h @@ -216,7 +216,7 @@ class TextEditor void SetColorizerEnable(bool aValue); Coordinates GetCursorPosition() const { return GetActualCursorCoordinates(); } - void SetCursorPosition(const Coordinates& aPosition); + void SetCursorPosition(const Coordinates& aPosition, int cursorLineOnPage = -1); inline void SetHandleMouseInputs (bool aValue){ mHandleMouseInputs = aValue;} inline bool IsHandleMouseInputsEnabled() const { return mHandleKeyboardInputs; } @@ -316,7 +316,7 @@ class TextEditor void ColorizeRange(int aFromLine = 0, int aToLine = 0); void ColorizeInternal(); float TextDistanceToLineStart(const Coordinates& aFrom) const; - void EnsureCursorVisible(); + void EnsureCursorVisible(int cursorLineOnPage = -1); int GetPageSize() const; std::string GetText(const Coordinates& aStart, const Coordinates& aEnd) const; Coordinates GetActualCursorCoordinates() const; @@ -359,6 +359,7 @@ class TextEditor bool mReadOnly; bool mWithinRender; bool mScrollToCursor; + int mScrollToCursor_CursorLineOnPage; bool mScrollToTop; bool mTextChanged; bool mColorizerEnabled;