imgui icon indicating copy to clipboard operation
imgui copied to clipboard

Power save mode. Allow user call RequestRedraw to redraw a new frame.

Open ForrestFeng opened this issue 3 years ago • 0 comments

This is a new try inspired by the pull request of #5116.

This new approach allows the user to choose which mode to use by specifying the timeout.

To tryout, please set the timeout value to -1 and open the Tools->Matics/Debugger to see how many frames are drawed when you interact with the demo.

image

Sample of code in the file of main.cpp to enable/disable power save. The default timeout value is 0 which keeps current ImGui no blocking behavior. Users can change it as needed.

   while (!glfwWindowShouldClose(window))
   {
        // wait and poll event
        // timeout:
        //  0 process event immediatly, not blocking
        // -1 wait for ever untill an event comes
        // positive number wait untill timedout or event comes
        // call ImGui::RequestRedraw to awake waiting of this function. Return 0 to signel a quict event.
        int timeout = 0;
        ImGui::WaitAndPollEvents(timeout);

       ...
    }

This PR also allows users to request a redraw on demand. For example, the Widgets/Plotting/ProgressBar animation calls this ImGui::RequestRedraw(ImGuiRedrawFlags_Everything) to animate the progress bar change.

        // Animate a simple progress bar
        IMGUI_DEMO_MARKER("Widgets/Plotting/ProgressBar");
        static float progress = 0.0f, progress_dir = 1.0f;
        if (animate)
        {
            progress += progress_dir * 0.4f * ImGui::GetIO().DeltaTime;
            if (progress >= +1.1f) { progress = +1.1f; progress_dir *= -1.0f; }
            if (progress <= -0.1f) { progress = -0.1f; progress_dir *= -1.0f; }
            // in power save mode when animate is enabled and, request redraw a new frame to show animation
            ImGui::RequestRedraw(ImGuiRedrawFlags_Everything);
        }

image

ImGuiRedrawFlags is an enum currently only implemented the redraw for everything. We can implement redraw for specific window or region of a window in the future.

Currently, only tested on Windows works well for all backends. Any feedback or suggestions on Linux and Mac are welcomed.

ForrestFeng avatar Mar 27 '22 10:03 ForrestFeng