Scrolling: Made it possible for mouse wheel and navigation-triggered scrolling to override a call to SetScrollX()/SetScrollY(), making it possible to use a simpler stateless pattern for auto-scrolling.

Demo: Log, Console: Using a simpler stateless pattern for auto-scrolling.
This commit is contained in:
omar
2019-07-22 18:24:23 -07:00
parent 34cf00566f
commit dcd03f62a7
5 changed files with 40 additions and 42 deletions

View File

@ -3449,7 +3449,7 @@ struct ExampleAppConsole
Commands.push_back("CLEAR");
Commands.push_back("CLASSIFY"); // "classify" is only here to provide an example of "C"+[tab] completing to "CL" and displaying matches.
AutoScroll = true;
ScrollToBottom = true;
ScrollToBottom = false;
AddLog("Welcome to Dear ImGui!");
}
~ExampleAppConsole()
@ -3470,7 +3470,6 @@ struct ExampleAppConsole
for (int i = 0; i < Items.Size; i++)
free(Items[i]);
Items.clear();
ScrollToBottom = true;
}
void AddLog(const char* fmt, ...) IM_FMTARGS(2)
@ -3483,8 +3482,6 @@ struct ExampleAppConsole
buf[IM_ARRAYSIZE(buf)-1] = 0;
va_end(args);
Items.push_back(Strdup(buf));
if (AutoScroll)
ScrollToBottom = true;
}
void Draw(const char* title, bool* p_open)
@ -3513,8 +3510,7 @@ struct ExampleAppConsole
if (ImGui::SmallButton("Add Dummy Text")) { AddLog("%d some text", Items.Size); AddLog("some more text"); AddLog("display very important message here!"); } ImGui::SameLine();
if (ImGui::SmallButton("Add Dummy Error")) { AddLog("[error] something went wrong"); } ImGui::SameLine();
if (ImGui::SmallButton("Clear")) { ClearLog(); } ImGui::SameLine();
bool copy_to_clipboard = ImGui::SmallButton("Copy"); ImGui::SameLine();
if (ImGui::SmallButton("Scroll to bottom")) ScrollToBottom = true;
bool copy_to_clipboard = ImGui::SmallButton("Copy");
//static float t = 0.0f; if (ImGui::GetTime() - t > 0.02f) { t = ImGui::GetTime(); AddLog("Spam %f", t); }
ImGui::Separator();
@ -3522,9 +3518,7 @@ struct ExampleAppConsole
// Options menu
if (ImGui::BeginPopup("Options"))
{
if (ImGui::Checkbox("Auto-scroll", &AutoScroll))
if (AutoScroll)
ScrollToBottom = true;
ImGui::Checkbox("Auto-scroll", &AutoScroll);
ImGui::EndPopup();
}
@ -3573,9 +3567,11 @@ struct ExampleAppConsole
}
if (copy_to_clipboard)
ImGui::LogFinish();
if (ScrollToBottom)
if (ScrollToBottom || (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY()))
ImGui::SetScrollHereY(1.0f);
ScrollToBottom = false;
ImGui::PopStyleVar();
ImGui::EndChild();
ImGui::Separator();
@ -3767,13 +3763,11 @@ struct ExampleAppLog
ImGuiTextBuffer Buf;
ImGuiTextFilter Filter;
ImVector<int> LineOffsets; // Index to lines offset. We maintain this with AddLog() calls, allowing us to have a random access on lines
bool AutoScroll;
bool ScrollToBottom;
bool AutoScroll; // Keep scrolling if already at the bottom
ExampleAppLog()
{
AutoScroll = true;
ScrollToBottom = false;
Clear();
}
@ -3794,8 +3788,6 @@ struct ExampleAppLog
for (int new_size = Buf.size(); old_size < new_size; old_size++)
if (Buf[old_size] == '\n')
LineOffsets.push_back(old_size + 1);
if (AutoScroll)
ScrollToBottom = true;
}
void Draw(const char* title, bool* p_open = NULL)
@ -3809,9 +3801,7 @@ struct ExampleAppLog
// Options menu
if (ImGui::BeginPopup("Options"))
{
if (ImGui::Checkbox("Auto-scroll", &AutoScroll))
if (AutoScroll)
ScrollToBottom = true;
ImGui::Checkbox("Auto-scroll", &AutoScroll);
ImGui::EndPopup();
}
@ -3876,9 +3866,9 @@ struct ExampleAppLog
}
ImGui::PopStyleVar();
if (ScrollToBottom)
if (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
ImGui::SetScrollHereY(1.0f);
ScrollToBottom = false;
ImGui::EndChild();
ImGui::End();
}