Skip to content

Commit 0eae77f

Browse files
committed
Inputs: SetItemKeyOwner(): return true if ownership has been requested, which can to be checked to accurately gate further input test.
(#456, #2637, #2620, #2891, #3370, #3724, #4828, #5108, #5242, #5641)
1 parent 02ccd9f commit 0eae77f

5 files changed

Lines changed: 19 additions & 11 deletions

File tree

docs/CHANGELOG.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ Other Changes:
152152
items straying out of columns boundaries. (#7994, #2221)
153153
- Box-Select + Tables: fixed an issue when calling `BeginMultiSelect()` in a table
154154
before layout has been locked (first row or headers row submitted). (#8250)
155+
- Inputs:
156+
- SetItemKeyOwner(): return true if ownership has been requested, which typically
157+
needs to to checked for gating further tests. This is important as the function
158+
may fail. (#456, #2637, #2620, #2891, #3370, #3724, #4828, #5108, #5242, #5641)
155159
- Style:
156160
- Fixed vertical scrollbar top coordinates when using thick borders on windows
157161
with no title bar and no menu bar. (#9366)

imgui.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10682,6 +10682,7 @@ bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id)
1068210682
// - SetKeyOwner(..., None) : clears owner
1068310683
// - SetKeyOwner(..., Any, !Lock) : illegal (assert)
1068410684
// - SetKeyOwner(..., Any or None, Lock) : set lock
10685+
// Ownership is automatically released on the frame after a release, see code in UpdateKeyboardInputs().
1068510686
void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags)
1068610687
{
1068710688
ImGuiContext& g = *GImGui;
@@ -10708,30 +10709,32 @@ void ImGui::SetKeyOwnersForKeyChord(ImGuiKeyChord key_chord, ImGuiID owner_id, I
1070810709
if (key_chord & ~ImGuiMod_Mask_) { SetKeyOwner((ImGuiKey)(key_chord & ~ImGuiMod_Mask_), owner_id, flags); }
1070910710
}
1071010711

10711-
// This is more or less equivalent to:
10712+
// This is more or less equivalent to a fancier version of:
1071210713
// if (IsItemHovered() || IsItemActive())
1071310714
// SetKeyOwner(key, GetItemID());
1071410715
// Extensive uses of that (e.g. many calls for a single item) may want to manually perform the tests once and then call SetKeyOwner() multiple times.
1071510716
// More advanced usage scenarios may want to call SetKeyOwner() manually based on different condition.
1071610717
// Worth noting is that only one item can be hovered and only one item can be active, therefore this usage pattern doesn't need to bother with routing and priority.
10717-
void ImGui::SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags)
10718+
bool ImGui::SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags)
1071810719
{
1071910720
ImGuiContext& g = *GImGui;
1072010721
ImGuiID id = g.LastItemData.ID;
1072110722
if (id == 0 || (g.HoveredId != id && g.ActiveId != id))
10722-
return;
10723+
return false;
1072310724
if ((flags & ImGuiInputFlags_CondMask_) == 0)
1072410725
flags |= ImGuiInputFlags_CondDefault_;
1072510726
if ((g.HoveredId == id && (flags & ImGuiInputFlags_CondHovered)) || (g.ActiveId == id && (flags & ImGuiInputFlags_CondActive)))
1072610727
{
1072710728
IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetItemKeyOwner) == 0); // Passing flags not supported by this function!
1072810729
SetKeyOwner(key, id, flags & ~ImGuiInputFlags_CondMask_);
10730+
return true;
1072910731
}
10732+
return false;
1073010733
}
1073110734

10732-
void ImGui::SetItemKeyOwner(ImGuiKey key)
10735+
bool ImGui::SetItemKeyOwner(ImGuiKey key)
1073310736
{
10734-
SetItemKeyOwner(key, ImGuiInputFlags_None);
10737+
return SetItemKeyOwner(key, ImGuiInputFlags_None);
1073510738
}
1073610739

1073710740
// This is the only public API until we expose owner_id versions of the API as replacements.

imgui.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,11 @@ namespace ImGui
10921092
// Inputs Utilities: Key/Input Ownership [BETA]
10931093
// - One common use case would be to allow your items to disable standard inputs behaviors such
10941094
// as Tab or Alt key handling, Mouse Wheel scrolling, etc.
1095-
// e.g. Button(...); SetItemKeyOwner(ImGuiKey_MouseWheelY); to make hovering/activating a button disable wheel for scrolling.
1095+
// e.g. `Button(...); if (SetItemKeyOwner(ImGuiKey_MouseWheelY)) { ... }` to make hovering/activating a button disable wheel for scrolling.
10961096
// - Reminder ImGuiKey enum include access to mouse buttons and gamepad, so key ownership can apply to them.
1097+
// - The return value of SetItemKeyOwner() says if ownership has been requested for the item, which is a shortcut to calling yet non-public TestKeyOwner() function.
10971098
// - Many related features are still in imgui_internal.h. For instance, most IsKeyXXX()/IsMouseXXX() functions have an owner-id-aware version.
1098-
IMGUI_API void SetItemKeyOwner(ImGuiKey key); // Set key owner to last item ID if it is hovered or active. Equivalent to 'if (IsItemHovered() || IsItemActive()) { SetKeyOwner(key, GetItemID());'.
1099+
IMGUI_API bool SetItemKeyOwner(ImGuiKey key); // Set key owner to last item ID if it is hovered or active. Return true when ownership has been set. Roughly equivalent to 'if (TestKeyOwner(key, GetItemID()) && (IsItemHovered() || IsItemActive())) { SetKeyOwner(key, GetItemID());'.
10991100

11001101
// Inputs Utilities: Mouse
11011102
// - To refer to a mouse button, you may use named enums in your code e.g. ImGuiMouseButton_Left, ImGuiMouseButton_Right.

imgui_demo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,9 +875,9 @@ static void ExampleImageViewer_DrawCanvas(ExampleImageViewerData* data, ImVec2 c
875875
data->ViewReset = false;
876876

877877
// Handle inputs
878-
ImGui::SetItemKeyOwner(ImGuiKey_MouseWheelY); // FIXME: Not while scrolling?
879-
if (ImGui::IsItemHovered() && io.MouseWheel != 0.0f)
880-
data->Zoom = IM_CLAMP(data->Zoom * (1.0f + io.MouseWheel * 0.10f), data->ZoomMin, data->ZoomMax);
878+
if (ImGui::SetItemKeyOwner(ImGuiKey_MouseWheelY)) // FIXME: Not while scrolling?
879+
if (io.MouseWheel != 0.0f)
880+
data->Zoom = IM_CLAMP(data->Zoom * (1.0f + io.MouseWheel * 0.10f), data->ZoomMin, data->ZoomMax);
881881
float zoom = data->Zoom; // (float)(int)ViewZoom;
882882
if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0))
883883
{

imgui_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3436,7 +3436,7 @@ namespace ImGui
34363436
IMGUI_API ImGuiID GetKeyOwner(ImGuiKey key);
34373437
IMGUI_API void SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags = 0);
34383438
IMGUI_API void SetKeyOwnersForKeyChord(ImGuiKeyChord key, ImGuiID owner_id, ImGuiInputFlags flags = 0);
3439-
IMGUI_API void SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags); // Set key owner to last item if it is hovered or active. Equivalent to 'if (IsItemHovered() || IsItemActive()) { SetKeyOwner(key, GetItemID());'.
3439+
IMGUI_API bool SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags);
34403440
IMGUI_API bool TestKeyOwner(ImGuiKey key, ImGuiID owner_id); // Test that key is either not owned, either owned by 'owner_id'
34413441
inline ImGuiKeyOwnerData* GetKeyOwnerData(ImGuiContext* ctx, ImGuiKey key) { if (key & ImGuiMod_Mask_) key = ConvertSingleModFlagToKey(key); IM_ASSERT(IsNamedKey(key)); return &ctx->KeysOwnerData[key - ImGuiKey_NamedKey_BEGIN]; }
34423442

0 commit comments

Comments
 (0)