← Back to context

Comment by flohofwoe

2 hours ago

When the UI is highly dynamic/animated it needs to be redrawn each frame also in a 'retained mode' UI framework.

When the UI is static and only needs to change on user input, an immediate mode UI can 'stop' too until there's new input to process.

For further low-power optimizations, immediate mode UI frameworks could skip describing parts of the UI when the application knows that this part doesn't need to change (contrary to popular belief, immediate mode UI frameworks do track and retain state between frames, just usually less than retained mode UIs - but how much state is retained is an internal implementation detail).

The problem is that widgets still need to store state somewhere, and that storage space needs to be reclaimed at some point. How does the system know when that can be done? I suppose the popular approach is to just reclaim space that wasn't referenced during a draw.

However ...

When you have a listbox of 10,000 rows and you only draw the visible rows, then the others will lose their state because of this.

Of course there are ways around that but it becomes messy. Maybe so messy that retained mode becomes attractive.

  • > How does the system know when that can be done?

    At the earlist in the first frame the application UI description code doesn't mention an UI item (that means UI items need a persistent id, in Dear ImGui this is a string hash, usually created from the item's label which can have a hidden `##` identifier to make it unique, plus a push/pop-id stack for hierarchical namespacing.

    > then the others will lose their state because of this

    Once an item is visible, the state must have been provided by the application's UI description code, when the item is invisible, that state becomes irrelevant.

    • > when the item is invisible, that state becomes irrelevant.

      What happens when the item moves out of view, e.g. because the user scrolls down?

      State should be preserved, because the user might scroll back up.

  • The job of the immediate UI is to just draw the things. Where and how you manage your state is completely up to you.

    It seems you assume some sort of OO model.

    > When you have a listbox of 10,000 rows and you only draw the visible rows, then the others will lose their state because of this.

    Well keep the state then.

    Immediate mode really just means you have your data as an array of things or whatever and the UI library creates the draw calls for you. Drawing and data are separate.

    • > The job of the immediate UI is to just draw the things. Where and how you manage your state is completely up to you.

      This is a bit oversimplified. For instance Dear ImGui needs to store at least the window positions between frames since the application code doesn't need to track window positions.