Skip to content

Commit

Permalink
InputText: added ImGuiInputTextFlags_ElideLeft. (ocornut#1442, ocornu…
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Dec 9, 2024
1 parent d264542 commit d78e823
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Other changes:
processing errors outside of the NewFrame()..EndFrame() scope. (#1651)
- Tables: fixed SetNextWindowScroll() value being ignored by BeginTable() during
the first frame or when scrolling flags have changed. (#8196)
- InputText: added ImGuiInputTextFlags_ElideLeft to elide left side and ensure right side
of contents is visible when whole text is not fitting (useful for paths/filenames).
(#1442, #1440, #4391, #7208, #8216) [@kucoman, @ocornut]
- InputText: reactivating last activated InputText() doesn't restore horizontal scrolling
(which was disabled during deactivation anyway).
- Misc: changed embedded ProggyClean encoding to save a bit of binary space (~12kb to 9.5kb).
Expand Down
17 changes: 10 additions & 7 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// Library Version
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345')
#define IMGUI_VERSION "1.91.6 WIP"
#define IMGUI_VERSION_NUM 19152
#define IMGUI_VERSION_NUM 19153
#define IMGUI_HAS_TABLE

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

// Elide display / Alignment
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!

// Callback features
ImGuiInputTextFlags_CallbackCompletion = 1 << 17, // Callback on pressing TAB (for completion handling)
ImGuiInputTextFlags_CallbackHistory = 1 << 18, // Callback on pressing Up/Down arrows (for history handling)
ImGuiInputTextFlags_CallbackAlways = 1 << 19, // Callback on each iteration. User code may query cursor position, modify text buffer.
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.
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)
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)
ImGuiInputTextFlags_CallbackCompletion = 1 << 18, // Callback on pressing TAB (for completion handling)
ImGuiInputTextFlags_CallbackHistory = 1 << 19, // Callback on pressing Up/Down arrows (for history handling)
ImGuiInputTextFlags_CallbackAlways = 1 << 20, // Callback on each iteration. User code may query cursor position, modify text buffer.
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.
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)
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)

// Obsolete names
//ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior
Expand Down
10 changes: 10 additions & 0 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,16 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data)
ImGui::TreePop();
}

IMGUI_DEMO_MARKER("Widgets/Text Input/Eliding, Alignment");
if (ImGui::TreeNode("Eliding, Alignment"))
{
static char buf1[128] = "/path/to/some/folder/with/long/filename.cpp";
static ImGuiInputTextFlags flags = ImGuiInputTextFlags_ElideLeft;
ImGui::CheckboxFlags("ImGuiInputTextFlags_ElideLeft", &flags, ImGuiInputTextFlags_ElideLeft);
ImGui::InputText("Path", buf1, IM_ARRAYSIZE(buf1), flags);
ImGui::TreePop();
}

IMGUI_DEMO_MARKER("Widgets/Text Input/Miscellaneous");
if (ImGui::TreeNode("Miscellaneous"))
{
Expand Down
9 changes: 9 additions & 0 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4403,6 +4403,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
IM_ASSERT(buf != NULL && buf_size >= 0);
IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys)
IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key)
IM_ASSERT(!((flags & ImGuiInputTextFlags_ElideLeft) && (flags & ImGuiInputTextFlags_Multiline))); // Multiline will not work with left-trimming

ImGuiContext& g = *GImGui;
ImGuiIO& io = g.IO;
Expand Down Expand Up @@ -4537,7 +4538,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
state->TextA.resize(buf_size + 1); // we use +1 to make sure that .Data is always pointing to at least an empty string.
state->TextLen = (int)strlen(buf);
memcpy(state->TextA.Data, buf, state->TextLen + 1);

// Find initial scroll position for right alignment
state->Scroll = ImVec2(0.0f, 0.0f);
if (flags & ImGuiInputTextFlags_ElideLeft)
state->Scroll.x += ImMax(0.0f, CalcTextSize(buf).x - frame_size.x + style.FramePadding.x * 2.0f);

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

if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length)
{
// Find render position for right alignment
if (flags & ImGuiInputTextFlags_ElideLeft)
draw_pos.x = ImMin(draw_pos.x, frame_bb.Max.x - CalcTextSize(buf_display, NULL).x - style.FramePadding.x);

const ImVec2 draw_scroll = /*state ? ImVec2(state->Scroll.x, 0.0f) :*/ ImVec2(0.0f, 0.0f); // Preserve scroll when inactive?
ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);
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);
Expand Down

0 comments on commit d78e823

Please sign in to comment.