1
0
mirror of synced 2025-01-09 21:21:38 +01:00

impr: Turned static variables into member variable in the Text Editor (#1973)

to avoid potential problems in the future if opening several popup
windows at the same time becomes possible.
This commit is contained in:
paxcut 2024-11-29 12:44:56 -07:00 committed by GitHub
parent 71ec6c5061
commit a639dddd19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 20 deletions

View File

@ -495,7 +495,10 @@ private:
std::string mLineBuffer; std::string mLineBuffer;
uint64_t mStartTime; uint64_t mStartTime;
std::vector<std::string> mDefines; std::vector<std::string> mDefines;
float m_linesAdded = 0;
float m_savedScrollY = 0;
float m_pixelsAdded = 0;
float m_shiftedScrollY = 0;
float mLastClick; float mLastClick;
bool mShowCursor; bool mShowCursor;
bool mShowLineNumbers; bool mShowLineNumbers;

View File

@ -1093,15 +1093,13 @@ void TextEditor::Render() {
if (mTopMargin != oldTopMargin) { if (mTopMargin != oldTopMargin) {
static float savedScrollY = 0;
if (oldTopMargin == 0) if (oldTopMargin == 0)
savedScrollY = ImGui::GetScrollY(); m_savedScrollY = ImGui::GetScrollY();
auto window = ImGui::GetCurrentWindow(); auto window = ImGui::GetCurrentWindow();
auto maxScroll = window->ScrollMax.y; auto maxScroll = window->ScrollMax.y;
if (maxScroll > 0) { if (maxScroll > 0) {
float lineCount; float lineCount;
float pixelCount; float pixelCount;
static float linesAdded = 0;
if (mTopMargin > oldTopMargin) { if (mTopMargin > oldTopMargin) {
pixelCount = mTopMargin - oldTopMargin; pixelCount = mTopMargin - oldTopMargin;
lineCount = pixelCount / mCharAdvance.y; lineCount = pixelCount / mCharAdvance.y;
@ -1110,14 +1108,14 @@ void TextEditor::Render() {
lineCount = pixelCount / mCharAdvance.y; lineCount = pixelCount / mCharAdvance.y;
} else { } else {
pixelCount = oldTopMargin; pixelCount = oldTopMargin;
lineCount = std::round(linesAdded); lineCount = std::round(m_linesAdded);
} }
auto state = mState; auto state = mState;
auto oldScrollY = ImGui::GetScrollY(); auto oldScrollY = ImGui::GetScrollY();
int lineCountInt; int lineCountInt;
if (mTopMargin > oldTopMargin) { if (mTopMargin > oldTopMargin) {
lineCountInt = std::round(lineCount + linesAdded - std::floor(linesAdded)); lineCountInt = std::round(lineCount + m_linesAdded - std::floor(m_linesAdded));
} else } else
lineCountInt = std::round(lineCount); lineCountInt = std::round(lineCount);
for (int i = 0; i < lineCountInt; i++) { for (int i = 0; i < lineCountInt; i++) {
@ -1126,28 +1124,26 @@ void TextEditor::Render() {
else else
mLines.erase(mLines.begin() + mLines.size() - 1); mLines.erase(mLines.begin() + mLines.size() - 1);
} }
static float pixelsAdded = 0;
if (mTopMargin > oldTopMargin) { if (mTopMargin > oldTopMargin) {
linesAdded += lineCount; m_linesAdded += lineCount;
pixelsAdded += pixelCount; m_pixelsAdded += pixelCount;
} else if (mTopMargin > 0) { } else if (mTopMargin > 0) {
linesAdded -= lineCount; m_linesAdded -= lineCount;
pixelsAdded -= pixelCount; m_pixelsAdded -= pixelCount;
} else { } else {
linesAdded = 0; m_linesAdded = 0;
pixelsAdded = 0; m_pixelsAdded = 0;
} }
if (oldScrollY + pixelCount < maxScroll) { if (oldScrollY + pixelCount < maxScroll) {
static float shiftedScrollY = 0;
if (mTopMargin > oldTopMargin) if (mTopMargin > oldTopMargin)
shiftedScrollY = oldScrollY + pixelCount; m_shiftedScrollY = oldScrollY + pixelCount;
else if (mTopMargin > 0) else if (mTopMargin > 0)
shiftedScrollY = oldScrollY - pixelCount; m_shiftedScrollY = oldScrollY - pixelCount;
else if (ImGui::GetScrollY() == shiftedScrollY) else if (ImGui::GetScrollY() == m_shiftedScrollY)
shiftedScrollY = savedScrollY; m_shiftedScrollY = m_savedScrollY;
else else
shiftedScrollY = ImGui::GetScrollY() - pixelCount; m_shiftedScrollY = ImGui::GetScrollY() - pixelCount;
ImGui::SetScrollY(shiftedScrollY); ImGui::SetScrollY(m_shiftedScrollY);
} else { } else {
if (mTopMargin > oldTopMargin) if (mTopMargin > oldTopMargin)
mScrollToBottom = true; mScrollToBottom = true;