Skip to content

Commit 26b8292

Browse files
committed
Split ImGuiItemFlags_LiveEdit into ImGuiItemFlags_LiveEditText, ImGuiItemFlags_LiveEditScalar. Added Demo contents. (#9476, #701)
cc #3936, #3946, #5904, #6284, #8149, #8065, #8665, #9117, #9299, #700, #1351, #1875, #2060, #2215, #2380, #2550, #3083, #3338, #3556, #4373, #4714, #4885, #5184,#5777, #6707, #6766, #8004, #8303, #8915, #9308
1 parent c958f16 commit 26b8292

6 files changed

Lines changed: 141 additions & 9 deletions

File tree

‎docs/CHANGELOG.txt‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,45 @@ Other Changes:
6060
- Fixed double-click collapse toggle not owning the mouse button. If a
6161
`SetNextWindowPos()` with pivot was queued in the same frame, the second
6262
click could trigger another item in the same window. (#9439) [@Cleroth]
63+
- Added `ImGuiItemFlags_LiveEditOnInputText` and `ImGuiItemFlags_LiveEditOnInputScalar`
64+
flags to configure the timing of applying edits of backing variables when typing
65+
values using a keyboard.
66+
(#701, #9476, #3936, #3946, #5904, #6284, #8149, #8065, #8665, #9117, #9299, #700, #1351,
67+
#1875, #2060, #2215, #2380, #2550, #3083, #3338, #3556, #4373, #4714, #4885, #5184,
68+
#5777, #6707, #6766, #8004, #8303, #8915, #9308)
69+
- Until now:
70+
- Edits where always applied immediately to backing variable, which is equivalent
71+
to the `ImGuiItemFlags_LiveEditXXX` flags being enabled.
72+
- Typing '123' in an integer field would output successively 1, 12 then 123.
73+
- Most uses of `IsItemDeactivatedAfterEdit()` or `ImGuiInputTextFlags_EnterReturnsTrue`
74+
were actually workarounds for this issue. Advanced applications would typically
75+
use `IsItemDeactivatedAfterEdit()` to distinguish transactions.
76+
Workarounds often required a backing store for scalar values, and there were
77+
also a few niggles related to `IsItemDeactivatedAfterEdit()` when using +/-
78+
buttons of an `InputInt()` widgets.
79+
Many of those situations can now be naturally simplified by disabling
80+
`ImGuiItemFlags_LiveEditOnInputScalar`, which is expected to become the default.
81+
- The new flags allows disabling this behavior selectively for strings fields
82+
such as `InputText()` vs scalar fields: `SliderInt()`, `InputFloat()`, etc.
83+
- When LiveEdit is disabled, edits are applied when pressing enter, tabbing out,
84+
clearing a field or deactivating due to a focus loss.
85+
- The flag may be altered programmatically:
86+
PushItemFlag(ImGuiItemFlags_LiveEditOnInputScalar, false); // Disable for scalars
87+
SliderInt(...);
88+
PopItemFlag();
89+
PushItemFlag(ImGuiItemFlags_LiveEditOnInput, true); // Enable for all
90+
SliderInt(...);
91+
PopItemFlag();
92+
But with upcoming new defaults it is expected you shouldn't touch them much.
93+
- Both flags currently defaults to true, which matches previous behavior.
94+
- The expectation is that for strings/text, enabling LiveEdit is a better default.
95+
- The expectation is that for scalars, disable LiveEdit is a better default.
96+
- The short-term intent is to change `ImGuiItemFlags_LiveEditOnInputScalar` to
97+
default to being disabled, as soon as we get more feedback from users (SOON).
98+
- We intentionally are not adding `io.ConfigLiveEditXXX` fields to dictate the
99+
default value of each `ImGuiItemFlags_LiveEditXXX`, because this is not
100+
expected to be a user preference but a programmer/widget preferences.
101+
Also, we strive to make the toolkit consistent.
63102
- InputText:
64103
- Added `style.InputTextCursorSize` to configure cursor/caret thickness. (#7031, #9409)
65104
This is automatically scaled by `style.ScaleAllSizes()`.

‎imgui.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6358,6 +6358,7 @@ bool ImGui::IsItemDeactivated()
63586358
return g.DeactivatedItemData.ID == g.LastItemData.ID && g.LastItemData.ID != 0 && g.DeactivatedItemData.ElapseFrame >= g.FrameCount;
63596359
}
63606360

6361+
// Since 1.92.9: consider using NoLiveEdit flags if all you need to that values are not written bo backing variable while typing.
63616362
bool ImGui::IsItemDeactivatedAfterEdit()
63626363
{
63636364
ImGuiContext& g = *GImGui;

‎imgui.h‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,16 +1238,29 @@ enum ImGuiChildFlags_
12381238
// (Those are shared by all submitted items)
12391239
enum ImGuiItemFlags_
12401240
{
1241-
ImGuiItemFlags_None = 0, // (Default)
1241+
ImGuiItemFlags_None = 0, // Default:
12421242
ImGuiItemFlags_NoTabStop = 1 << 0, // false // Disable keyboard tabbing. This is a "lighter" version of ImGuiItemFlags_NoNav.
1243-
ImGuiItemFlags_NoNav = 1 << 1, // false // Disable any form of focusing (keyboard/gamepad directional navigation and SetKeyboardFocusHere() calls).
1243+
ImGuiItemFlags_NoNav = 1 << 1, // false // Disable any form of focusing: keyboard/gamepad directional navigation and SetKeyboardFocusHere() calls.
12441244
ImGuiItemFlags_NoNavDefaultFocus = 1 << 2, // false // Disable item being a candidate for default focus (e.g. used by title bar items).
12451245
ImGuiItemFlags_ButtonRepeat = 1 << 3, // false // Any button-like behavior will have repeat mode enabled (based on io.KeyRepeatDelay and io.KeyRepeatRate values). Note that you can also call IsItemActive() after any button to tell if it is being held.
12461246
ImGuiItemFlags_AutoClosePopups = 1 << 4, // true // MenuItem()/Selectable() automatically close their parent popup window.
12471247
ImGuiItemFlags_AllowDuplicateId = 1 << 5, // false // Allow submitting an item with the same identifier as an item already submitted this frame without triggering a warning tooltip if io.ConfigDebugHighlightIdConflicts is set.
12481248
ImGuiItemFlags_Disabled = 1 << 6, // false // [Internal] Disable interactions. DOES NOT affect visuals. This is used by BeginDisabled()/EndDisabled() and only provided here so you can read back via GetItemFlags().
12491249

1250-
ImGuiItemFlags_LiveEdit = 1 << 7, // true // WIP
1250+
//---------------------------------------------------------------------------------
1251+
// LiveEdit refers to applying edits to backing variables _while_ typing.
1252+
// Widget: | Input: | w/ LiveEdit: | Output:
1253+
// InputText() | "123" | on(default) | "1" then "12" then "123"
1254+
// InputText() | "123" | off | "123" after validating or tabbing out or losing focus.
1255+
// DragFloat(), SliderInt(), etc. | "123" | on(default)* | 1 then 12 then 123
1256+
// DragFloat(), SliderInt(), etc. | "123" | off* | 123 after validation or tabbing out or losing focus.
1257+
//---------------------------------------------------------------------------------
1258+
// (*) The feature is currently being evaluated, and the aim is to change ImGuiItemFlags_LiveEditOnInputScalar to OFF by default.
1259+
// The legacy behavior until July 2026 was that LiveEdit was always ON for everything.
1260+
//---------------------------------------------------------------------------------
1261+
ImGuiItemFlags_LiveEditOnInputText = 1 << 7, // true // InputText: apply keyboard edits to backing value while typing. Otherwise, edits are applied when validating, tabbing out or losing focus.
1262+
ImGuiItemFlags_LiveEditOnInputScalar = 1 << 8, // true* // DragXXX, SliderXXX, InputScalar: apply keyboard edits to backing value while typing. Otherwise, edits are applied when validating, tabbing out or losing focus.
1263+
ImGuiItemFlags_LiveEditOnInput = ImGuiItemFlags_LiveEditOnInputText | ImGuiItemFlags_LiveEditOnInputScalar,
12511264
};
12521265

12531266
// Flags for ImGui::InputText()
@@ -1264,7 +1277,7 @@ enum ImGuiInputTextFlags_
12641277

12651278
// Inputs
12661279
ImGuiInputTextFlags_AllowTabInput = 1 << 5, // Pressing TAB input a '\t' character into the text field
1267-
ImGuiInputTextFlags_EnterReturnsTrue = 1 << 6, // Return 'true' when Enter is pressed (as opposed to every time the value was modified). Consider using IsItemDeactivatedAfterEdit() instead!
1280+
ImGuiInputTextFlags_EnterReturnsTrue = 1 << 6, // Return 'true' when Enter is pressed (as opposed to every time the value was modified). Consider disabling LiveEdit! or using IsItemDeactivatedAfterEdit() instead!
12681281
ImGuiInputTextFlags_EscapeClearsAll = 1 << 7, // Escape key clears content if not empty, and deactivate otherwise (contrast to default behavior of Escape to revert)
12691282
ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 8, // In multi-line mode: validate with Enter, add new line with Ctrl+Enter (default is opposite: validate with Ctrl+Enter, add line with Enter). Note that Shift+Enter always enter a new line either way.
12701283

@@ -1277,7 +1290,7 @@ enum ImGuiInputTextFlags_
12771290
ImGuiInputTextFlags_DisplayEmptyRefVal = 1 << 14, // InputFloat(), InputInt(), InputScalar() etc. only: when value is zero, do not display it. Generally used with ImGuiInputTextFlags_ParseEmptyRefVal.
12781291
ImGuiInputTextFlags_NoHorizontalScroll = 1 << 15, // Disable following the cursor horizontally
12791292
ImGuiInputTextFlags_NoUndoRedo = 1 << 16, // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID().
1280-
//ImGuiInputTextFlags_NoLiveEdit = 1 << 25,
1293+
//ImGuiInputTextFlags_NoLiveEdit = 1 << 25, // Disable applying output to backing variable while typing. Same as setting ImGuiItemFlags_LiveEditOnInput to false.
12811294

12821295
// Elide display / Alignment
12831296
ImGuiInputTextFlags_ElideLeft = 1 << 17, // When text doesn't fit, elide left side to ensure right side stays visible. Useful for path/filenames. Single-line only!
@@ -1956,6 +1969,8 @@ enum ImGuiSliderFlags_
19561969
ImGuiSliderFlags_NoSpeedTweaks = 1 << 11, // Disable keyboard modifiers altering tweak speed. Useful if you want to alter tweak speed yourself based on your own logic.
19571970
ImGuiSliderFlags_ColorMarkers = 1 << 12, // DragScalarN(), SliderScalarN(): Draw R/G/B/A color markers on each component.
19581971
ImGuiSliderFlags_AlwaysClamp = ImGuiSliderFlags_ClampOnInput | ImGuiSliderFlags_ClampZeroRange,
1972+
//ImGuiSliderFlags_LiveEditOnInput = 1 << 13, // Shortcut to enable LiveEdit for this field.
1973+
//ImGuiSliderFlags_NoLiveEditOnInput= 1 << 14, // Shortcut to disable LiveEdit for this field.
19591974
ImGuiSliderFlags_InvalidMask_ = 0x7000000F, // [Internal] We treat using those bits as being potentially a 'float power' argument from legacy API (obsoleted 2020-08) that has got miscast to this enum, and will trigger an assert if needed.
19601975
};
19611976

‎imgui_demo.cpp‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Index of this file:
8686
// [SECTION] DemoWindowWidgetsFonts()
8787
// [SECTION] DemoWindowWidgetsImages()
8888
// [SECTION] DemoWindowWidgetsListBoxes()
89+
// [SECTION] DemoWindowWidgetsLiveEdit()
8990
// [SECTION] DemoWindowWidgetsMultiComponents()
9091
// [SECTION] DemoWindowWidgetsPlotting()
9192
// [SECTION] DemoWindowWidgetsProgressBars()
@@ -331,6 +332,8 @@ struct ImGuiDemoWindowData
331332

332333
// Other data
333334
bool DisableSections = false;
335+
bool LiveEditOverride = false;
336+
ImGuiItemFlags LiveEditFlags = ImGuiItemFlags_LiveEditOnInputText;
334337
ExampleTreeNode* DemoTree = NULL;
335338

336339
~ImGuiDemoWindowData() { if (DemoTree) ExampleTree_DestroyNode(DemoTree); }
@@ -2009,6 +2012,44 @@ static void DemoWindowWidgetsListBoxes()
20092012
}
20102013
}
20112014

2015+
//-----------------------------------------------------------------------------
2016+
// [SECTION] DemoWindowWidgetsLiveEdit()
2017+
//-----------------------------------------------------------------------------
2018+
2019+
static void DemoWindowWidgetsLiveEdit(ImGuiDemoWindowData* demo_data)
2020+
{
2021+
if (ImGui::TreeNode("Live Edit Flags"))
2022+
{
2023+
IMGUI_DEMO_MARKER("Widgets/Live Edit Flgs");
2024+
2025+
ImGui::TextWrapped("Select whether to apply keyboard edits to backing variables _while_ typing.");
2026+
2027+
ImGui::Checkbox("Override Live Edit Flags in Demo Window", &demo_data->LiveEditOverride);
2028+
if (!demo_data->LiveEditOverride)
2029+
demo_data->LiveEditFlags = ImGui::GetItemFlags();
2030+
2031+
ImGui::BeginDisabled(demo_data->LiveEditOverride == false);
2032+
ImGui::Indent();
2033+
ImGui::CheckboxFlags("ImGuiItemFlags_LiveEditOnInputText", &demo_data->LiveEditFlags, ImGuiItemFlags_LiveEditOnInputText);
2034+
ImGui::CheckboxFlags("ImGuiItemFlags_LiveEditOnInputScalar", &demo_data->LiveEditFlags, ImGuiItemFlags_LiveEditOnInputScalar);
2035+
ImGui::Unindent();
2036+
ImGui::EndDisabled();
2037+
2038+
ImGui::Text("Try typing '123' and seeing effect on backing value:");
2039+
static char str[32] = "";
2040+
ImGui::InputText("str", str, IM_COUNTOF(str));
2041+
ImGui::Text("Backing value: \"%s\"", str);
2042+
static int i = 0;
2043+
ImGui::InputInt("int", &i, 0, 0);
2044+
ImGui::Text("Backing value: %d", i);
2045+
static float f = 0.0f;
2046+
ImGui::SliderFloat("float", &f, 0.0f, 100.0f);
2047+
ImGui::Text("Backing value: %f", f);
2048+
2049+
ImGui::TreePop();
2050+
}
2051+
}
2052+
20122053
//-----------------------------------------------------------------------------
20132054
// [SECTION] DemoWindowWidgetsMultiComponents()
20142055
//-----------------------------------------------------------------------------
@@ -2189,10 +2230,28 @@ static void DemoWindowWidgetsQueryingStatuses()
21892230
};
21902231
static int item_type = 4;
21912232
static bool item_disabled = false;
2233+
static bool liveedit_flags_override = false;
2234+
static ImGuiItemFlags liveedit_flags = 0;
21922235
ImGui::Combo("Item Type", &item_type, item_names, IM_COUNTOF(item_names), IM_COUNTOF(item_names));
21932236
ImGui::SameLine();
21942237
HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions. Note that the bool return value of most ImGui function is generally equivalent to calling ImGui::IsItemHovered().");
21952238
ImGui::Checkbox("Item Disabled", &item_disabled);
2239+
ImGui::Checkbox("Override LiveEdit:", &liveedit_flags_override);
2240+
ImGui::SameLine();
2241+
if (!liveedit_flags_override)
2242+
liveedit_flags = ImGui::GetItemFlags();
2243+
ImGui::BeginDisabled(liveedit_flags_override == false);
2244+
ImGui::CheckboxFlags("_LiveEditOnInput", &liveedit_flags, ImGuiItemFlags_LiveEditOnInput);
2245+
ImGui::SameLine();
2246+
ImGui::CheckboxFlags("_LiveEditOnInputText", &liveedit_flags, ImGuiItemFlags_LiveEditOnInputText);
2247+
ImGui::SameLine();
2248+
ImGui::CheckboxFlags("_LiveEditOnInputScalar", &liveedit_flags, ImGuiItemFlags_LiveEditOnInputScalar);
2249+
ImGui::EndDisabled();
2250+
if (liveedit_flags_override)
2251+
{
2252+
ImGui::PushItemFlag(ImGuiItemFlags_LiveEditOnInputText, (liveedit_flags & ImGuiItemFlags_LiveEditOnInputText) != 0);
2253+
ImGui::PushItemFlag(ImGuiItemFlags_LiveEditOnInputScalar, (liveedit_flags & ImGuiItemFlags_LiveEditOnInputScalar) != 0);
2254+
}
21962255

21972256
// Submit selected items so we can query their status in the code following it.
21982257
bool ret = false;
@@ -2279,6 +2338,11 @@ static void DemoWindowWidgetsQueryingStatuses()
22792338
"IsItemHovered(_Tooltip) = %d",
22802339
hovered_delay_none, hovered_delay_stationary, hovered_delay_short, hovered_delay_normal, hovered_delay_tooltip);
22812340

2341+
if (liveedit_flags_override)
2342+
{
2343+
ImGui::PopItemFlag();
2344+
ImGui::PopItemFlag();
2345+
}
22822346
if (item_disabled)
22832347
ImGui::EndDisabled();
22842348

@@ -4413,8 +4477,14 @@ static void DemoWindowWidgets(ImGuiDemoWindowData* demo_data)
44134477
// IMGUI_DEMO_MARKER("Widgets");
44144478

44154479
const bool disable_all = demo_data->DisableSections; // The Checkbox for that is inside the "Disabled" section at the bottom
4480+
const bool override_liveedit = demo_data->LiveEditOverride;
44164481
if (disable_all)
44174482
ImGui::BeginDisabled();
4483+
if (override_liveedit)
4484+
{
4485+
ImGui::PushItemFlag(ImGuiItemFlags_LiveEditOnInputText, (demo_data->LiveEditFlags & ImGuiItemFlags_LiveEditOnInputText) != 0);
4486+
ImGui::PushItemFlag(ImGuiItemFlags_LiveEditOnInputScalar, (demo_data->LiveEditFlags & ImGuiItemFlags_LiveEditOnInputScalar) != 0);
4487+
}
44184488

44194489
DemoWindowWidgetsBasic();
44204490
DemoWindowWidgetsBullets();
@@ -4434,6 +4504,7 @@ static void DemoWindowWidgets(ImGuiDemoWindowData* demo_data)
44344504
DemoWindowWidgetsFonts();
44354505
DemoWindowWidgetsImages();
44364506
DemoWindowWidgetsListBoxes();
4507+
DemoWindowWidgetsLiveEdit(demo_data);
44374508
DemoWindowWidgetsMultiComponents();
44384509
DemoWindowWidgetsPlotting();
44394510
DemoWindowWidgetsProgressBars();
@@ -4448,6 +4519,11 @@ static void DemoWindowWidgets(ImGuiDemoWindowData* demo_data)
44484519
DemoWindowWidgetsTreeNodes();
44494520
DemoWindowWidgetsVerticalSliders();
44504521

4522+
if (override_liveedit)
4523+
{
4524+
ImGui::PopItemFlag();
4525+
ImGui::PopItemFlag();
4526+
}
44514527
if (disable_all)
44524528
ImGui::EndDisabled();
44534529
}

‎imgui_internal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ enum ImGuiItemFlagsPrivate_
10061006
ImGuiItemFlags_HasSelectionUserData = 1 << 21, // false // Set by SetNextItemSelectionUserData()
10071007
ImGuiItemFlags_IsMultiSelect = 1 << 22, // false // Set by SetNextItemSelectionUserData()
10081008

1009-
ImGuiItemFlags_Default_ = ImGuiItemFlags_AutoClosePopups | ImGuiItemFlags_LiveEdit, // Please don't change, use PushItemFlag() instead.
1009+
ImGuiItemFlags_Default_ = ImGuiItemFlags_AutoClosePopups | ImGuiItemFlags_LiveEditOnInputText | ImGuiItemFlags_LiveEditOnInputScalar, // Please don't change, use PushItemFlag() instead.
10101010

10111011
// Obsolete
10121012
//ImGuiItemFlags_SelectableDontClosePopup = !ImGuiItemFlags_AutoClosePopups, // Can't have a redirect as we inverted the behavior

‎imgui_widgets.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,6 +3763,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG
37633763

37643764
ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint;
37653765
g.LastItemData.ItemFlags |= ImGuiItemFlags_NoMarkEdited; // Because TempInputText() uses ImGuiInputTextFlags_MergedItem it doesn't submit a new item, so we poke LastItemData.
3766+
g.LastItemData.ItemFlags = (g.LastItemData.ItemFlags & ImGuiItemFlags_LiveEditOnInputScalar) ? (g.LastItemData.ItemFlags | ImGuiItemFlags_LiveEditOnInputText) : (g.LastItemData.ItemFlags & ~ImGuiItemFlags_LiveEditOnInputText);
37663767
if (!TempInputText(bb, id, label, data_buf, IM_COUNTOF(data_buf), flags))
37673768
return false;
37683769

@@ -3843,7 +3844,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data
38433844

38443845
// Apply
38453846
bool value_changed = false;
3846-
if (g.LastItemData.ItemFlags & ImGuiItemFlags_LiveEdit)
3847+
if (g.LastItemData.ItemFlags & ImGuiItemFlags_LiveEditOnInputScalar)
38473848
{
38483849
bool input_edited = (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_EditedInternal) != 0; // We would be using 'ret' if ImGuiInputTextFlags_EnterReturnsTrue was not involved.
38493850
if (input_edited)
@@ -4868,7 +4869,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
48684869
state->CursorAnimReset();
48694870

48704871
// Backup state of deactivating item so they'll have a chance to do a write to output buffer on the same frame they report IsItemDeactivatedAfterEdit (#4714)
4871-
if (state->ID != id && state->ID == g.ActiveId && (init_make_active && g.ActiveId != id))
4872+
if (state->ID != id && state->ID == g.ActiveId && (init_make_active && g.ActiveId != id)) //-V560
48724873
InputTextDeactivateHook(state->ID); // <-- this is essentially an earlier call to what SetActiveID() would do below.
48734874

48744875
// Take a copy of the initial buffer value.
@@ -5410,7 +5411,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
54105411
// FIXME-OPT: Could mark dirty state from the stb_textedit callbacks
54115412
if (!is_readonly)
54125413
{
5413-
if (g.LastItemData.ItemFlags & ImGuiItemFlags_LiveEdit)
5414+
if (g.LastItemData.ItemFlags & ImGuiItemFlags_LiveEditOnInputText)
54145415
{
54155416
// Apply when modified
54165417
if (strcmp(state->TextSrc, buf) != 0)

0 commit comments

Comments
 (0)
⌂ 换网址