WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit d449544

Browse files
committed
Windows, Internal: added experimental SkipRefresh mode. (#3515, #4763, #7556, #5116 , #4076, #2749, #2268)
currently: ImGui::SetNextWindowRefreshPolicy(ImGuiWindowRefreshFlags_TryToAvoidRefresh); - This is NOT meant to replace frame-wide/app-wide idle mode. - This is another tool: the idea that a given window could avoid refresh and reuse last frame contents. - I think it needs to be backed by a careful and smart design overall (refresh policy, load balancing, making it easy and obvious to user). - It's not there yet, this is currently a toy for experimenting. My other issues with this: - It appears to be very simple, but skipping most of Begin() logic will inevitably lead to tricky/confusing bugs. Let's see how it goes. - I don't like very much that this opens a door to varying inconsistencies - I don't like very much that it can lead us to situation where the lazy refresh gets disabled in bulk due to some reason (e.g. resizing a dock space) and we get sucked in the temptation to update for idle rather than update for dynamism.
1 parent 0b30947 commit d449544

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

imgui.cpp

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6334,6 +6334,30 @@ void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags
63346334
}
63356335
}
63366336

6337+
// [EXPERIMENTAL] Called by Begin(). NextWindowData is valid at this point.
6338+
// This is designed as a toy/test-bed for
6339+
void ImGui::UpdateWindowSkipRefresh(ImGuiWindow* window)
6340+
{
6341+
ImGuiContext& g = *GImGui;
6342+
window->SkipRefresh = false;
6343+
if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasRefreshPolicy) == 0)
6344+
return;
6345+
if (g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_TryToAvoidRefresh)
6346+
{
6347+
// FIXME-IDLE: Tests for e.g. mouse clicks or keyboard while focused.
6348+
if (window->Appearing) // If currently appearing
6349+
return;
6350+
if (window->Hidden) // If was hidden (previous frame)
6351+
return;
6352+
if ((g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_RefreshOnHover) && g.HoveredWindow && window->RootWindow == g.HoveredWindow->RootWindow)
6353+
return;
6354+
if ((g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_RefreshOnFocus) && g.NavWindow && window->RootWindow == g.NavWindow->RootWindow)
6355+
return;
6356+
window->DrawList = NULL;
6357+
window->SkipRefresh = true;
6358+
}
6359+
}
6360+
63376361
// When a modal popup is open, newly created windows that want focus (i.e. are not popups and do not specify ImGuiWindowFlags_NoFocusOnAppearing)
63386362
// should be positioned behind that modal window, unless the window was created inside the modal begin-stack.
63396363
// In case of multiple stacked modals newly created window honors begin stack order and does not go below its own modal parent.
@@ -6532,11 +6556,14 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
65326556
if (window->Appearing)
65336557
SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, false);
65346558

6559+
// [EXPERIMENTAL] Skip Refresh mode
6560+
UpdateWindowSkipRefresh(window);
6561+
65356562
// We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow()
65366563
g.CurrentWindow = NULL;
65376564

65386565
// When reusing window again multiple times a frame, just append content (don't need to setup again)
6539-
if (first_begin_of_the_frame)
6566+
if (first_begin_of_the_frame && !window->SkipRefresh)
65406567
{
65416568
// Initialize
65426569
const bool window_is_child_tooltip = (flags & ImGuiWindowFlags_ChildWindow) && (flags & ImGuiWindowFlags_Tooltip); // FIXME-WIP: Undocumented behavior of Child+Tooltip for pinned tooltip (#1345)
@@ -7027,20 +7054,25 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
70277054
}
70287055
else
70297056
{
7057+
// Skip refresh always mark active
7058+
if (window->SkipRefresh)
7059+
window->Active = true;
7060+
70307061
// Append
70317062
SetCurrentWindow(window);
70327063
SetLastItemDataForWindow(window, window->TitleBarRect());
70337064
}
70347065

7035-
PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true);
7066+
if (!window->SkipRefresh)
7067+
PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true);
70367068

70377069
// Clear 'accessed' flag last thing (After PushClipRect which will set the flag. We want the flag to stay false when the default "Debug" window is unused)
70387070
window->WriteAccessed = false;
70397071
window->BeginCount++;
70407072
g.NextWindowData.ClearFlags();
70417073

70427074
// Update visibility
7043-
if (first_begin_of_the_frame)
7075+
if (first_begin_of_the_frame && !window->SkipRefresh)
70447076
{
70457077
if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_ChildMenu))
70467078
{
@@ -7086,6 +7118,11 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
70867118
skip_items = true;
70877119
window->SkipItems = skip_items;
70887120
}
7121+
else if (first_begin_of_the_frame)
7122+
{
7123+
// Skip refresh mode
7124+
window->SkipItems = true;
7125+
}
70897126

70907127
// [DEBUG] io.ConfigDebugBeginReturnValue override return value to test Begin/End and BeginChild/EndChild behaviors.
70917128
// (The implicit fallback window is NOT automatically ended allowing it to always be able to receive commands without crashing)
@@ -7128,9 +7165,16 @@ void ImGui::End()
71287165
// Close anything that is open
71297166
if (window->DC.CurrentColumns)
71307167
EndColumns();
7131-
PopClipRect(); // Inner window clip rectangle
7168+
if (!window->SkipRefresh)
7169+
PopClipRect(); // Inner window clip rectangle
71327170
PopFocusScope();
71337171

7172+
if (window->SkipRefresh)
7173+
{
7174+
IM_ASSERT(window->DrawList == NULL);
7175+
window->DrawList = &window->DrawListInst;
7176+
}
7177+
71347178
// Stop logging
71357179
if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging
71367180
LogFinish();
@@ -7815,6 +7859,14 @@ void ImGui::SetNextWindowBgAlpha(float alpha)
78157859
g.NextWindowData.BgAlphaVal = alpha;
78167860
}
78177861

7862+
// This is experimental and meant to be a toy for exploring a future/wider range of features.
7863+
void ImGui::SetNextWindowRefreshPolicy(ImGuiWindowRefreshFlags flags)
7864+
{
7865+
ImGuiContext& g = *GImGui;
7866+
g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasRefreshPolicy;
7867+
g.NextWindowData.RefreshFlagsVal = flags;
7868+
}
7869+
78187870
ImDrawList* ImGui::GetWindowDrawList()
78197871
{
78207872
ImGuiWindow* window = GetCurrentWindow();

imgui_demo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
// Read top of imgui.cpp and imgui.h for many details, documentation, comments, links.
1111
// Get the latest version at https://github.com/ocornut/imgui
1212

13+
// How to easily locate code?
14+
// - Use the Item Picker to debug break in code by clicking any widgets: https://github.com/ocornut/imgui/wiki/Debug-Tools
15+
// - Browse an online version the demo with code linked to hovered widgets: https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html
16+
// - Find a visible string and search for it in the code!
17+
1318
//---------------------------------------------------
1419
// PLEASE DO NOT REMOVE THIS FILE FROM YOUR PROJECT!
1520
//---------------------------------------------------

imgui_draw.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error)
385385
}
386386

387387
// Initialize before use in a new frame. We always have a command ready in the buffer.
388+
// In the majority of cases, you would want to call PushClipRect() and PushTextureID() after this.
388389
void ImDrawList::_ResetForNewFrame()
389390
{
390391
// Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory.

imgui_internal.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // F
182182
typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx()
183183
typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // Flags: for BeginTooltipEx()
184184
typedef int ImGuiTypingSelectFlags; // -> enum ImGuiTypingSelectFlags_ // Flags: for GetTypingSelectRequest()
185+
typedef int ImGuiWindowRefreshFlags; // -> enum ImGuiWindowRefreshFlags_ // Flags: for SetNextWindowRefreshPolicy()
185186

186187
typedef void (*ImGuiErrorLogCallback)(void* user_data, const char* fmt, ...);
187188

@@ -1117,6 +1118,15 @@ struct IMGUI_API ImGuiInputTextState
11171118

11181119
};
11191120

1121+
enum ImGuiWindowRefreshFlags_
1122+
{
1123+
ImGuiWindowRefreshFlags_None = 0,
1124+
ImGuiWindowRefreshFlags_TryToAvoidRefresh = 1 << 0, // [EXPERIMENTAL] Try to keep existing contents, USER MUST NOT HONOR BEGIN() RETURNING FALSE AND NOT APPEND.
1125+
ImGuiWindowRefreshFlags_RefreshOnHover = 1 << 1, // [EXPERIMENTAL] Always refresh on hover
1126+
ImGuiWindowRefreshFlags_RefreshOnFocus = 1 << 2, // [EXPERIMENTAL] Always refresh on focus
1127+
// Refresh policy/frequency, Load Balancing etc.
1128+
};
1129+
11201130
enum ImGuiNextWindowDataFlags_
11211131
{
11221132
ImGuiNextWindowDataFlags_None = 0,
@@ -1129,6 +1139,7 @@ enum ImGuiNextWindowDataFlags_
11291139
ImGuiNextWindowDataFlags_HasBgAlpha = 1 << 6,
11301140
ImGuiNextWindowDataFlags_HasScroll = 1 << 7,
11311141
ImGuiNextWindowDataFlags_HasChildFlags = 1 << 8,
1142+
ImGuiNextWindowDataFlags_HasRefreshPolicy = 1 << 9,
11321143
};
11331144

11341145
// Storage for SetNexWindow** functions
@@ -1150,6 +1161,7 @@ struct ImGuiNextWindowData
11501161
void* SizeCallbackUserData;
11511162
float BgAlphaVal; // Override background alpha
11521163
ImVec2 MenuBarOffsetMinVal; // (Always on) This is not exposed publicly, so we don't clear it and it doesn't have a corresponding flag (could we? for consistency?)
1164+
ImGuiWindowRefreshFlags RefreshFlagsVal;
11531165

11541166
ImGuiNextWindowData() { memset(this, 0, sizeof(*this)); }
11551167
inline void ClearFlags() { Flags = ImGuiNextWindowDataFlags_None; }
@@ -2534,6 +2546,7 @@ struct IMGUI_API ImGuiWindow
25342546
bool Collapsed; // Set when collapsing window to become only title-bar
25352547
bool WantCollapseToggle;
25362548
bool SkipItems; // Set when items can safely be all clipped (e.g. window not visible or collapsed)
2549+
bool SkipRefresh; // [EXPERIMENTAL] Reuse previous frame drawn contents, Begin() returns false.
25372550
bool Appearing; // Set during the frame where the window is appearing (or re-appearing)
25382551
bool Hidden; // Do not display (== HiddenFrames*** > 0)
25392552
bool IsFallbackWindow; // Set on the "Debug##Default" window.
@@ -3007,6 +3020,7 @@ namespace ImGui
30073020
IMGUI_API ImGuiWindow* FindWindowByID(ImGuiID id);
30083021
IMGUI_API ImGuiWindow* FindWindowByName(const char* name);
30093022
IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window);
3023+
IMGUI_API void UpdateWindowSkipRefresh(ImGuiWindow* window);
30103024
IMGUI_API ImVec2 CalcWindowNextAutoFitSize(ImGuiWindow* window);
30113025
IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent, bool popup_hierarchy);
30123026
IMGUI_API bool IsWindowWithinBeginStackOf(ImGuiWindow* window, ImGuiWindow* potential_parent);
@@ -3032,6 +3046,9 @@ namespace ImGui
30323046
IMGUI_API int FindWindowDisplayIndex(ImGuiWindow* window);
30333047
IMGUI_API ImGuiWindow* FindBottomMostVisibleWindowWithinBeginStack(ImGuiWindow* window);
30343048

3049+
// Windows: Idle, Refresh Policies [EXPERIMENTAL]
3050+
IMGUI_API void SetNextWindowRefreshPolicy(ImGuiWindowRefreshFlags flags);
3051+
30353052
// Fonts, drawing
30363053
IMGUI_API void SetCurrentFont(ImFont* font);
30373054
inline ImFont* GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; }

0 commit comments

Comments
 (0)