Skip to content

Commit

Permalink
drag scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Jan 4, 2024
1 parent 641f8fd commit f4bd28a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions include/imgui_ext.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "imgui.h"
#include "imgui_internal.h"

namespace ImGui
{
Expand Down Expand Up @@ -38,4 +39,24 @@ inline bool MenuItem(const string &label, const string &shortcut, bool *p_select
return MenuItem(label.c_str(), shortcut.c_str(), p_selected, enabled);
}

// from https://github.com/ocornut/imgui/issues/3379#issuecomment-1678718752
void ScrollWhenDraggingOnVoid(const ImVec2 &delta, ImGuiMouseButton mouse_button)
{
ImGuiContext &g = *ImGui::GetCurrentContext();
ImGuiWindow *window = g.CurrentWindow;
bool hovered = false;
bool held = false;
ImGuiID id = window->GetID("##scrolldraggingoverlay");
ImGui::KeepAliveID(id);
ImGuiButtonFlags button_flags = (mouse_button == 0) ? ImGuiButtonFlags_MouseButtonLeft
: (mouse_button == 1) ? ImGuiButtonFlags_MouseButtonRight
: ImGuiButtonFlags_MouseButtonMiddle;
if (g.HoveredId == 0) // If nothing hovered so far in the frame (not same as IsAnyItemHovered()!)
ImGui::ButtonBehavior(window->Rect(), id, &hovered, &held, button_flags);
if (held && delta.x != 0.0f)
ImGui::SetScrollX(window, window->Scroll.x + delta.x);
if (held && delta.y != 0.0f)
ImGui::SetScrollY(window, window->Scroll.y + delta.y);
}

} // namespace ImGui
2 changes: 2 additions & 0 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ void SampleViewer::draw_about_dialog()
}
// ImGui::SetItemDefaultFocus();

ImGui::ScrollWhenDraggingOnVoid(ImVec2(0.0f, -ImGui::GetIO().MouseDelta.y), ImGuiMouseButton_Left);
ImGui::EndPopup();
g_open_help = false;
}
Expand Down Expand Up @@ -963,6 +964,7 @@ void SampleViewer::draw_editor()
ImGui::Dummy({0, HelloImGui::EmSize(0.25f)});
}
ImGui::PopItemWidth();
ImGui::ScrollWhenDraggingOnVoid(ImVec2(0.0f, -ImGui::GetIO().MouseDelta.y), ImGuiMouseButton_Left);
}

bool SampleViewer::process_event(void *e)
Expand Down

0 comments on commit f4bd28a

Please sign in to comment.