Skip to content

Fix GuiSlider jumping to mouse position on click#526

Draft
grerfou wants to merge 1 commit intoraysan5:masterfrom
grerfou:fix-guislider-jump-on-click
Draft

Fix GuiSlider jumping to mouse position on click#526
grerfou wants to merge 1 commit intoraysan5:masterfrom
grerfou:fix-guislider-jump-on-click

Conversation

@grerfou
Copy link

@grerfou grerfou commented Feb 10, 2026

Fixes #474

Problem

When clicking on the slider track (not on the handle), the slider would immediately jump to the mouse position before dragging could begin.

Solution

  • Store the initial offset between mouse position and slider position when clicking
  • Apply this offset during dragging to prevent the jump
  • The slider now drags smoothly from its current position without repositioning

Testing

Created a test program that demonstrates the fix works for both GuiSlider() and GuiSliderBar().

Before: Clicking anywhere on track caused slider to jump to that position
After: Clicking starts dragging from current slider position, no jump occurs

Note: Opened as draft pending confirmation from @raysan5 on the expected behavior (see issue discussion). Will mark as ready for review once confirmed.

@raysan5
Copy link
Owner

raysan5 commented Feb 10, 2026

@grerfou I'm afraid this is not the desired approach, it's an immediate-mode UI so the priority is avoidinng any kind of static data storage, like this one:

static float dragOffsetX = 0.0f;

If the only possible solution implies storing data, I'm afraid this is not a solution.

@grerfou
Copy link
Author

grerfou commented Feb 11, 2026

@raysan5 You're totally right about the static variable... I've been thinking about other ways to approach this.

What if we use GetMouseDelta() instead? The idea is to apply incremental changes to the value rather than calculating absolute positions:

  • First frame when you click: mouseDelta.x = 0 → value doesn't change → no jump
  • Next frames while dragging: only apply the mouse movement since last frame → should give smooth drag

Here's what I'm thinking for the key change:

if (guiControlExclusiveMode) // Dragging in progress
{
    if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
    {
        if (CHECK_BOUNDS_ID(bounds, guiControlExclusiveRec))
        {
            state = STATE_PRESSED;
            
            // Use mouse delta to update value incrementally (no jump on click)
            Vector2 mouseDelta = GetMouseDelta();
            float deltaValue = (maxValue - minValue) * (mouseDelta.x / (bounds.width - sliderWidth));
            *value += deltaValue;
        }
    }
    else
    {
        guiControlExclusiveMode = false;
        guiControlExclusiveRec = RAYGUI_CLITERAL(Rectangle){ 0, 0, 0, 0 };
    }
}
else if (CheckCollisionPointRec(mousePoint, bounds))
{
    if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
    {
        state = STATE_PRESSED;
        
        // Start dragging and apply delta
        guiControlExclusiveMode = true;
        guiControlExclusiveRec = bounds;
        
        Vector2 mouseDelta = GetMouseDelta();
        float deltaValue = (maxValue - minValue) * (mouseDelta.x / (bounds.width - sliderWidth));
        *value += deltaValue;
    }
    else state = STATE_FOCUSED;
}

I tested it and it seems to work - no jump on click, smooth dragging, no static storage.

One thing I noticed: if someone clicks and moves super fast in the same frame, there could be a tiny movement. But that's pretty rare and still way better than jumping to the mouse position.

Does this approach make sense to you? Happy to try something else if you think there's a better way!

@grerfou
Copy link
Author

grerfou commented Feb 16, 2026

@raysan5 Hey ! Wondering if you had thoughts on the GetMouseDelta() approach ?

If it doesn't feel right for raygui, I can try something else, like only dragging
from the handle itself.
Just let me know what direction you'd prefer !

@raysan5
Copy link
Owner

raysan5 commented Feb 17, 2026

@grerfou Using GetMouseDelta() would imply adding a new raylib function dependency to raygui, at the moment it's not used anywhere, I prefer to minimize raylib-coupling, to allow usign other libraries backends if desired. Actually recently there were some raygui internal redesigns adding MACROS() for inputs additional decoupling.

I don't know if there is any simpler solution to this problem...

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GuiSlider() when clicking on the slider-bar for draging, position changes to become mouse-centered

2 participants