Skip to content

Commit

Permalink
Set g.HoveredId when item is disabled. Prevents window from being dra…
Browse files Browse the repository at this point in the history
…gged if mouse hovers a disabled item.
  • Loading branch information
rokups committed Jul 15, 2020
1 parent eefae08 commit c24b470
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
5 changes: 3 additions & 2 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3062,15 +3062,16 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id)
return false;
if (g.NavDisableMouseHover || !IsWindowContentHoverable(window, ImGuiHoveredFlags_None))
return false;

SetHoveredID(id);

if (window->DC.ItemFlags & ImGuiItemFlags_Disabled)
return false;

// We exceptionally allow this function to be called with id==0 to allow using it for easy high-level
// hover test in widgets code. We could also decide to split this function is two.
if (id != 0)
{
SetHoveredID(id);

// [DEBUG] Item Picker tool!
// We perform the check here because SetHoveredID() is not frequently called (1~ time a frame), making
// the cost of this tool near-zero. We can get slightly better call-stack and support picking non-hovered
Expand Down
19 changes: 9 additions & 10 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
ImGuiContext& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();

bool pressed = false;
bool hovered = ItemHoverable(bb, id);

if (flags & ImGuiButtonFlags_Disabled)
{
if (out_hovered) *out_hovered = false;
Expand All @@ -489,9 +492,6 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
IMGUI_TEST_ENGINE_ITEM_ADD(bb, id);
#endif

bool pressed = false;
bool hovered = ItemHoverable(bb, id);

// Drag source doesn't report as hovered
if (hovered && g.DragDropActive && g.DragDropPayload.SourceId == id && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoDisableHover))
hovered = false;
Expand Down Expand Up @@ -1365,7 +1365,7 @@ bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float
if (g.ActiveId != id)
SetItemAllowOverlap();

if (held || (g.HoveredId == id && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay))
if (held || (hovered && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay))
SetMouseCursor(axis == ImGuiAxis_Y ? ImGuiMouseCursor_ResizeNS : ImGuiMouseCursor_ResizeEW);

ImRect bb_render = bb;
Expand Down Expand Up @@ -2189,7 +2189,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data,
return TempInputScalar(frame_bb, id, label, data_type, p_data, format);// , p_min, p_max);

// Draw frame
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
RenderNavHighlight(frame_bb, id);
RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding);

Expand Down Expand Up @@ -2642,7 +2642,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat
return TempInputScalar(frame_bb, id, label, data_type, p_data, format);// , p_min, p_max);

// Draw frame
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
RenderNavHighlight(frame_bb, id);
RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding);

Expand Down Expand Up @@ -2788,7 +2788,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d
}

// Draw frame
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
RenderNavHighlight(frame_bb, id);
RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding);

Expand Down Expand Up @@ -6347,7 +6347,7 @@ bool ImGui::BeginMenu(const char* label, bool enabled)
RenderArrow(window->DrawList, pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), text_col, ImGuiDir_Right);
}

const bool hovered = enabled && ItemHoverable(window->DC.LastItemRect, id);
const bool hovered = ItemHoverable(window->DC.LastItemRect, id) && enabled;
if (menuset_is_open)
g.NavWindow = backed_nav_window;

Expand Down Expand Up @@ -7216,7 +7216,6 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open,
bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags);
if (pressed)
tab_bar->NextSelectedTabId = id;
hovered |= (g.HoveredId == id);

// Allow the close button to overlap unless we are dragging (in which case we don't want any overlapping tabs to be hovered)
if (!held)
Expand Down Expand Up @@ -7281,7 +7280,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open,

// Tooltip (FIXME: Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer)
// We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar (which g.HoveredId ignores)
if (g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered())
if (hovered && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered())
if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip) && !(tab->Flags & ImGuiTabItemFlags_NoTooltip))
SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label);

Expand Down

0 comments on commit c24b470

Please sign in to comment.