Skip to content

Commit 936ae9e

Browse files
ocornutmatthew-mccall
authored andcommitted
InputText: added ImGuiInputTextFlags_ElideLeft. (ocornut#1442, ocornut#1440, ocornut#4391, ocornut#7208, ocornut#8216)
1 parent 1500410 commit 936ae9e

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

docs/CHANGELOG.txt

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Other changes:
5858
processing errors outside of the NewFrame()..EndFrame() scope. (#1651)
5959
- Tables: fixed SetNextWindowScroll() value being ignored by BeginTable() during
6060
the first frame or when scrolling flags have changed. (#8196)
61+
- InputText: added ImGuiInputTextFlags_ElideLeft to elide left side and ensure right side
62+
of contents is visible when whole text is not fitting (useful for paths/filenames).
63+
(#1442, #1440, #4391, #7208, #8216) [@kucoman, @ocornut]
6164
- InputText: reactivating last activated InputText() doesn't restore horizontal scrolling
6265
(which was disabled during deactivation anyway).
6366
- Misc: changed embedded ProggyClean encoding to save a bit of binary space (~12kb to 9.5kb).

imgui.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
// Library Version
3030
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345')
3131
#define IMGUI_VERSION "1.91.6 WIP"
32-
#define IMGUI_VERSION_NUM 19152
32+
#define IMGUI_VERSION_NUM 19153
3333
#define IMGUI_HAS_TABLE
3434

3535
/*
@@ -1171,13 +1171,16 @@ enum ImGuiInputTextFlags_
11711171
ImGuiInputTextFlags_NoHorizontalScroll = 1 << 15, // Disable following the cursor horizontally
11721172
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().
11731173

1174+
// Elide display / Alignment
1175+
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!
1176+
11741177
// Callback features
1175-
ImGuiInputTextFlags_CallbackCompletion = 1 << 17, // Callback on pressing TAB (for completion handling)
1176-
ImGuiInputTextFlags_CallbackHistory = 1 << 18, // Callback on pressing Up/Down arrows (for history handling)
1177-
ImGuiInputTextFlags_CallbackAlways = 1 << 19, // Callback on each iteration. User code may query cursor position, modify text buffer.
1178-
ImGuiInputTextFlags_CallbackCharFilter = 1 << 20, // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard.
1179-
ImGuiInputTextFlags_CallbackResize = 1 << 21, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this)
1180-
ImGuiInputTextFlags_CallbackEdit = 1 << 22, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active)
1178+
ImGuiInputTextFlags_CallbackCompletion = 1 << 18, // Callback on pressing TAB (for completion handling)
1179+
ImGuiInputTextFlags_CallbackHistory = 1 << 19, // Callback on pressing Up/Down arrows (for history handling)
1180+
ImGuiInputTextFlags_CallbackAlways = 1 << 20, // Callback on each iteration. User code may query cursor position, modify text buffer.
1181+
ImGuiInputTextFlags_CallbackCharFilter = 1 << 21, // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard.
1182+
ImGuiInputTextFlags_CallbackResize = 1 << 22, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this)
1183+
ImGuiInputTextFlags_CallbackEdit = 1 << 23, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active)
11811184

11821185
// Obsolete names
11831186
//ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior

imgui_demo.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,16 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data)
18301830
ImGui::TreePop();
18311831
}
18321832

1833+
IMGUI_DEMO_MARKER("Widgets/Text Input/Eliding, Alignment");
1834+
if (ImGui::TreeNode("Eliding, Alignment"))
1835+
{
1836+
static char buf1[128] = "/path/to/some/folder/with/long/filename.cpp";
1837+
static ImGuiInputTextFlags flags = ImGuiInputTextFlags_ElideLeft;
1838+
ImGui::CheckboxFlags("ImGuiInputTextFlags_ElideLeft", &flags, ImGuiInputTextFlags_ElideLeft);
1839+
ImGui::InputText("Path", buf1, IM_ARRAYSIZE(buf1), flags);
1840+
ImGui::TreePop();
1841+
}
1842+
18331843
IMGUI_DEMO_MARKER("Widgets/Text Input/Miscellaneous");
18341844
if (ImGui::TreeNode("Miscellaneous"))
18351845
{

imgui_widgets.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -4403,6 +4403,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
44034403
IM_ASSERT(buf != NULL && buf_size >= 0);
44044404
IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys)
44054405
IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key)
4406+
IM_ASSERT(!((flags & ImGuiInputTextFlags_ElideLeft) && (flags & ImGuiInputTextFlags_Multiline))); // Multiline will not work with left-trimming
44064407

44074408
ImGuiContext& g = *GImGui;
44084409
ImGuiIO& io = g.IO;
@@ -4537,7 +4538,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
45374538
state->TextA.resize(buf_size + 1); // we use +1 to make sure that .Data is always pointing to at least an empty string.
45384539
state->TextLen = (int)strlen(buf);
45394540
memcpy(state->TextA.Data, buf, state->TextLen + 1);
4541+
4542+
// Find initial scroll position for right alignment
45404543
state->Scroll = ImVec2(0.0f, 0.0f);
4544+
if (flags & ImGuiInputTextFlags_ElideLeft)
4545+
state->Scroll.x += ImMax(0.0f, CalcTextSize(buf).x - frame_size.x + style.FramePadding.x * 2.0f);
45414546

45424547
if (recycle_state)
45434548
{
@@ -5287,6 +5292,10 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
52875292

52885293
if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length)
52895294
{
5295+
// Find render position for right alignment
5296+
if (flags & ImGuiInputTextFlags_ElideLeft)
5297+
draw_pos.x = ImMin(draw_pos.x, frame_bb.Max.x - CalcTextSize(buf_display, NULL).x - style.FramePadding.x);
5298+
52905299
const ImVec2 draw_scroll = /*state ? ImVec2(state->Scroll.x, 0.0f) :*/ ImVec2(0.0f, 0.0f); // Preserve scroll when inactive?
52915300
ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);
52925301
draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos - draw_scroll, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);

0 commit comments

Comments
 (0)