Fix truncation text by imgui width

This commit is contained in:
Filip Sykala - NTB T15p 2022-11-23 13:35:59 +01:00
parent f3724c8dac
commit 52460f0c89
2 changed files with 15 additions and 16 deletions

View file

@ -1406,10 +1406,18 @@ void GLGizmoEmboss::init_face_names() {
// to reload fonts from system, when install new one // to reload fonts from system, when install new one
wxFontEnumerator::InvalidateCache(); wxFontEnumerator::InvalidateCache();
auto create_truncated_names = [&facenames = m_face_names, &width = m_gui_cfg->face_name_max_width]() {
for (FaceName &face : facenames.faces) {
std::string name_str(face.wx_name.ToUTF8().data());
face.name_truncated = ImGuiWrapper::trunc(name_str, width);
}
};
// try load cache // try load cache
// Only not OS enumerated face has hash value 0 // Only not OS enumerated face has hash value 0
if (m_face_names.hash == 0) { if (m_face_names.hash == 0) {
load(m_face_names); load(m_face_names);
create_truncated_names();
} }
using namespace std::chrono; using namespace std::chrono;
@ -1479,6 +1487,7 @@ void GLGizmoEmboss::init_face_names() {
} }
} }
assert(std::is_sorted(m_face_names.bad.begin(), m_face_names.bad.end())); assert(std::is_sorted(m_face_names.bad.begin(), m_face_names.bad.end()));
create_truncated_names();
store(m_face_names); store(m_face_names);
} }
@ -1638,13 +1647,7 @@ void GLGizmoEmboss::draw_font_list()
if (ImGui::IsItemHovered()) if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", wx_face_name.ToUTF8().data()); ImGui::SetTooltip("%s", wx_face_name.ToUTF8().data());
if (is_selected) ImGui::SetItemDefaultFocus(); if (is_selected) ImGui::SetItemDefaultFocus();
bool is_visible = ImGui::IsItemVisible(); draw_font_preview(face, ImGui::IsItemVisible());
if (is_visible && face.name_truncated.empty()) {
float width = m_gui_cfg->face_name_max_width;
std::string name_str(face.wx_name.ToUTF8().data());
face.name_truncated = ImGuiWrapper::trunc(name_str, width);
}
draw_font_preview(face, is_visible);
} }
#ifdef SHOW_FONT_COUNT #ifdef SHOW_FONT_COUNT
ImGui::TextColored(ImGuiWrapper::COL_GREY_LIGHT, "Count %d", ImGui::TextColored(ImGuiWrapper::COL_GREY_LIGHT, "Count %d",

View file

@ -1326,24 +1326,20 @@ std::string ImGuiWrapper::trunc(const std::string &text,
text_width = calc_text_size(result_text).x; text_width = calc_text_size(result_text).x;
if (text_width < allowed_width) { if (text_width < allowed_width) {
// increase letter count // increase letter count
while (true) { while (count_letter < text.length()) {
++count_letter; ++count_letter;
std::string_view act_text = text_.substr(0, count_letter); std::string_view act_text = text_.substr(0, count_letter);
text_width = calc_text_size(act_text).x; text_width = calc_text_size(act_text).x;
if (text_width < allowed_width) { if (text_width > allowed_width) break;
result_text = std::move(act_text); result_text = act_text;
break;
}
} }
} else { } else {
// decrease letter count // decrease letter count
while (true) { while (count_letter > 1) {
--count_letter; --count_letter;
result_text = text_.substr(0, count_letter); result_text = text_.substr(0, count_letter);
text_width = calc_text_size(result_text).x; text_width = calc_text_size(result_text).x;
if (text_width > allowed_width) break; if (text_width < allowed_width) break;
if (count_letter == 1) return "Error: No letters left. Can't return only tail.";
} }
} }
return std::string(result_text) + tail; return std::string(result_text) + tail;