Skip to content

Trouble detecting the end of editing a drag float when pressing Tab #1351

Closed

Description

@onqtam

So I've written these helper functions to determine when dragging starts and ends for float drags (so I can implement undo/redo with the values from before starting the dragging and after the dragging stops)

static bool IsItemActiveLastFrame() {
    ImGuiContext& g = *GImGui;

    if(g.ActiveIdPreviousFrame)
        return g.ActiveIdPreviousFrame == g.CurrentWindow->DC.LastItemId;
    return false;
}

static bool IsItemJustReleased() { return IsItemActiveLastFrame() && !ImGui::IsItemActive(); }
static bool IsItemJustActivated() { return !IsItemActiveLastFrame() && ImGui::IsItemActive(); }

This works like a charm.

When I edit the float drag by entering a number (so not dragging) and I press Return - IsItemJustReleased() again returns true as expected.

My problem is when I press Tab (to move the focus on the next field) - then this doesn't return true and I don't detect the change of the value.

Does anyone have any idea what the problem might be?

Activity

  1. onqtam commented on Oct 5, 2017

    @onqtam
    ContributorAuthor

    I managed to fix my problem by changing this piece of code (using the latest version of ImGui):

    else if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressedMap(ImGuiKey_Tab) && !io.KeyCtrl && !io.KeyShift && !io.KeyAlt && is_editable)
    {
        unsigned int c = '\t'; // Insert TAB
        if (InputTextFilterCharacter(&c, flags, callback, user_data))
            edit_state.OnKeyPressed((int)c);
    }

    to this:

    else if (IsKeyPressedMap(ImGuiKey_Tab))
    {
        if (window->FocusIdxTabRequestNext != INT_MAX)
            clear_active_id = true;
    
        if ((flags & ImGuiInputTextFlags_AllowTabInput) && !io.KeyCtrl && !io.KeyShift && !io.KeyAlt && is_editable)
        {
            unsigned int c = '\t'; // Insert TAB
            if (InputTextFilterCharacter(&c, flags, callback, user_data))
                edit_state.OnKeyPressed((int)c);
        }
    }

    Basically I clear the active id - just like when enter is pressed (10 lines above that).

    Tabbing still works through fields properly - so far this doesn't seem to break anything but I bet I'm missing something. Any feedback?

  2. ocornut commented on Aug 27, 2020

    @ocornut
    Owner

    Hello @onqtam, apologies for never answering this.

    This has been solved as discussed in #1875 and other topics.
    You can use IsItemDeactivated() or IsItemDeactivatedAfterEdit() added in 1.62, for the purpose you mention, and it works with tabbing out as well (the fix for tabbing out was part of 1.69),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址