Skip to content

Commit

Permalink
Inputs: changed specs of SetKeyOwner() to alter OwnerCurr immediately.
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut authored and kjblanchard committed May 5, 2023
1 parent eb4c06e commit ef7ba12
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8370,17 +8370,9 @@ bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id)
if (owner_id == ImGuiKeyOwner_Any)
return (owner_data->LockThisFrame == false);

// FIXME: For consistency with routing may be good to disable that, OR we could differentiate preemptive routing from setting owner on click.
// For now it is better to disable so we can actually get report of case where this may matter.
// -> Better to move in the SetKeyOwner() call, aka have a flag to set ->OwnerCurr in SetKeyOwner() ?
// May simply be inconsistent and unneeded to offer that feature:
// - for typical keyboard/gamepad routing with multiple claims and ServeLast, we don't want to affect owner testing during the frame.
// - for typical mouse routing overlap + hovered window generally prevents making it useful to alter owner testing during the frame.
// - but effectively Locked flag can alter same-frame behavior.
//// We want OwnerNext to be handled immediately so SetKeyOwner(key, id1), TestKeyOwner(key, id2) == false
//if (owner_data->OwnerNext != ImGuiKeyOwner_None && owner_data->OwnerNext != owner)
// return false;

// Note: SetKeyOwner() sets OwnerCurr. It is not strictly required for most mouse routing overlap (because of ActiveId/HoveredId
// are acting as filter before this has a chance to filter), but sane as soon as user tries to look into things.
// Setting OwnerCurr in SetKeyOwner() is more consistent than testing OwnerNext here: would be inconsistent with getter and other functions.
if (owner_data->OwnerCurr != owner_id)
{
if (owner_data->LockThisFrame)
Expand All @@ -8392,6 +8384,7 @@ bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id)
return true;
}

// _LockXXX flags are useful to lock keys away from code which is not input-owner aware.
// When using _LockXXX flags, you can use ImGuiKeyOwner_Any to lock keys from everyone.
// - SetKeyOwner(..., None) : clears owner
// - SetKeyOwner(..., Any, !Lock) : illegal (assert)
Expand All @@ -8401,14 +8394,12 @@ void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags)
IM_ASSERT(IsNamedKeyOrModKey(key) && (owner_id != ImGuiKeyOwner_Any || (flags & (ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease)))); // Can only use _Any with _LockXXX flags (to eat a key away without an ID to retrieve it)

ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key);
owner_data->OwnerNext = owner_id;
owner_data->OwnerCurr = owner_data->OwnerNext = owner_id;

// We cannot lock by default as it would likely break lots of legacy code.
// In the case of using LockUntilRelease while key is not down we still lock during the frame (no key_data->Down test)
owner_data->LockUntilRelease = (flags & ImGuiInputFlags_LockUntilRelease) != 0;
owner_data->LockThisFrame = (flags & ImGuiInputFlags_LockThisFrame) != 0 || (owner_data->LockUntilRelease);
if (owner_data->LockThisFrame)
owner_data->OwnerCurr = owner_id;
}

// This is more or less equivalent to:
Expand Down

0 comments on commit ef7ba12

Please sign in to comment.