Skip to content

Commit f4bd28a

Browse files
committed
drag scroll
1 parent 641f8fd commit f4bd28a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

include/imgui_ext.h

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "imgui.h"
2+
#include "imgui_internal.h"
23

34
namespace ImGui
45
{
@@ -38,4 +39,24 @@ inline bool MenuItem(const string &label, const string &shortcut, bool *p_select
3839
return MenuItem(label.c_str(), shortcut.c_str(), p_selected, enabled);
3940
}
4041

42+
// from https://github.com/ocornut/imgui/issues/3379#issuecomment-1678718752
43+
void ScrollWhenDraggingOnVoid(const ImVec2 &delta, ImGuiMouseButton mouse_button)
44+
{
45+
ImGuiContext &g = *ImGui::GetCurrentContext();
46+
ImGuiWindow *window = g.CurrentWindow;
47+
bool hovered = false;
48+
bool held = false;
49+
ImGuiID id = window->GetID("##scrolldraggingoverlay");
50+
ImGui::KeepAliveID(id);
51+
ImGuiButtonFlags button_flags = (mouse_button == 0) ? ImGuiButtonFlags_MouseButtonLeft
52+
: (mouse_button == 1) ? ImGuiButtonFlags_MouseButtonRight
53+
: ImGuiButtonFlags_MouseButtonMiddle;
54+
if (g.HoveredId == 0) // If nothing hovered so far in the frame (not same as IsAnyItemHovered()!)
55+
ImGui::ButtonBehavior(window->Rect(), id, &hovered, &held, button_flags);
56+
if (held && delta.x != 0.0f)
57+
ImGui::SetScrollX(window, window->Scroll.x + delta.x);
58+
if (held && delta.y != 0.0f)
59+
ImGui::SetScrollY(window, window->Scroll.y + delta.y);
60+
}
61+
4162
} // namespace ImGui

src/app.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ void SampleViewer::draw_about_dialog()
646646
}
647647
// ImGui::SetItemDefaultFocus();
648648

649+
ImGui::ScrollWhenDraggingOnVoid(ImVec2(0.0f, -ImGui::GetIO().MouseDelta.y), ImGuiMouseButton_Left);
649650
ImGui::EndPopup();
650651
g_open_help = false;
651652
}
@@ -963,6 +964,7 @@ void SampleViewer::draw_editor()
963964
ImGui::Dummy({0, HelloImGui::EmSize(0.25f)});
964965
}
965966
ImGui::PopItemWidth();
967+
ImGui::ScrollWhenDraggingOnVoid(ImVec2(0.0f, -ImGui::GetIO().MouseDelta.y), ImGuiMouseButton_Left);
966968
}
967969

968970
bool SampleViewer::process_event(void *e)

0 commit comments

Comments
 (0)