Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/view/src/widgets/rocprofvis_gui_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,33 @@ ElidedText(const char* text, float available_width, float tooltip_width,
std::string
ElideWithEllipsis(const std::string& text, float max_width, size_t max_chars)
{
std::string out = text.substr(0, max_chars);
bool truncated = text.size() > max_chars;
while(!out.empty() && ImGui::CalcTextSize((out + "...").c_str()).x > max_width)
// Optional hard character cap first.
const std::string capped =
(max_chars < text.size()) ? text.substr(0, max_chars) : text;
bool truncated = capped.size() < text.size();

// Trim to fit max_width in one pass (CalcTextSizeA reports where it stopped).
const char* begin = capped.c_str();
const char* end = begin + capped.size();
const float ellipsis_w = ImGui::CalcTextSize(TEXT_ELLIPSIS).x;
const char* remaining = begin;
ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(),
std::max(max_width - ellipsis_w, 0.0f), 0.0f, begin,
end, &remaining);
if(remaining < end)
{
out.pop_back();
truncated = true;
}
// Keep at least one character so a shortened value is not just the ellipsis.
if(remaining == begin && end > begin)
{
remaining = begin + 1;
}

std::string out(begin, remaining);
if(truncated)
{
out += "...";
out += TEXT_ELLIPSIS;
}
return out;
}
Expand Down
10 changes: 7 additions & 3 deletions src/view/src/widgets/rocprofvis_gui_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,19 @@ enum Alignment
Alignment_Right,
};

// Ellipsis appended to shortened text, so every caller renders the same marker.
inline constexpr const char* TEXT_ELLIPSIS = "...";

bool
ElidedText(const char* text, float available_width, float tooltip_width = 0.0f,
Alignment alignment = Alignment_Left,
bool imgui_AlignTextToFramePadding = false);

// Trims text to a single line fitting max_width (and max_chars), appending "..."
// when shortened. Uses the current font for measurement.
// Trims text to fit max_width (current font), appending TEXT_ELLIPSIS when
// shortened. max_chars optionally caps the character count.
std::string
ElideWithEllipsis(const std::string& text, float max_width, size_t max_chars);
ElideWithEllipsis(const std::string& text, float max_width,
size_t max_chars = std::string::npos);

void
CenterNextTextItem(const char* text);
Expand Down
Loading
Loading