diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index 12018ac47e..a0690cb90a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -1045,7 +1045,7 @@ void GLGizmoEmboss::draw_rename_style(bool start_rename) is_unique && !new_name.empty(); ImGuiInputTextFlags flags = ImGuiInputTextFlags_EnterReturnsTrue; - if ((ImGui::InputText("##font name", &new_name, flags) && allow_change) || + if ((ImGui::InputText(("##font name" + original_style_name).c_str(), &new_name, flags) && allow_change) || m_imgui->button(_L("ok"), ImVec2(0.f, 0.f), allow_change)) { rename_item->name = new_name; m_font_manager.get_truncated_name() = ""; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp index dd9cf0de2e..8549a9022c 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp @@ -282,7 +282,7 @@ void GLGizmoMmuSegmentation::on_render_input_window(float x, float y, float bott const float button_width = m_imgui->calc_text_size(m_desc.at("remove_all")).x + m_imgui->scaled(1.f); const float buttons_width = m_imgui->scaled(0.5f); const float minimal_slider_width = m_imgui->scaled(4.f); - const float color_button_width = m_imgui->calc_text_size("").x + m_imgui->scaled(1.75f); + const float color_button_width = m_imgui->scaled(1.75f); const float combo_label_width = std::max(m_imgui->calc_text_size(m_desc.at("first_color")).x, m_imgui->calc_text_size(m_desc.at("second_color")).x) + m_imgui->scaled(1.f); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp index 244ede5dc1..500712fe30 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp @@ -585,7 +585,7 @@ void GLGizmoSimplify::on_set_state() void GLGizmoSimplify::create_gui_cfg() { if (m_gui_cfg.has_value()) return; - int space_size = m_imgui->calc_text_size(":MM").x; + int space_size = m_imgui->calc_text_size(std::string_view{":MM"}).x; GuiCfg cfg; cfg.top_left_width = std::max(m_imgui->calc_text_size(tr_mesh_name).x, m_imgui->calc_text_size(tr_triangles).x) diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index dbac75ba54..3c25ef628a 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -297,10 +297,27 @@ void ImGuiWrapper::render() m_new_frame_open = false; } -ImVec2 ImGuiWrapper::calc_text_size(const wxString &text, float wrap_width) const +ImVec2 ImGuiWrapper::calc_text_size(std::string_view text, + bool hide_text_after_double_hash, + float wrap_width) +{ + return ImGui::CalcTextSize(text.data(), text.data() + text.length(), + hide_text_after_double_hash, wrap_width); +} + +ImVec2 ImGuiWrapper::calc_text_size(const std::string& text, + bool hide_text_after_double_hash, + float wrap_width) +{ + return ImGui::CalcTextSize(text.c_str(), NULL, hide_text_after_double_hash, wrap_width); +} + +ImVec2 ImGuiWrapper::calc_text_size(const wxString &text, + bool hide_text_after_double_hash, + float wrap_width) { auto text_utf8 = into_u8(text); - ImVec2 size = ImGui::CalcTextSize(text_utf8.c_str(), NULL, false, wrap_width); + ImVec2 size = ImGui::CalcTextSize(text_utf8.c_str(), NULL, hide_text_after_double_hash, wrap_width); /*#ifdef __linux__ size.x *= m_style_scaling; @@ -1309,36 +1326,41 @@ std::string ImGuiWrapper::trunc(const std::string &text, { float text_width = ImGui::CalcTextSize(text.c_str()).x; if (text_width < width) return text; - float letter_width = ImGui::CalcTextSize("n").x; // average letter width - assert(width > letter_width); - if (width < letter_width) return "Error: too small widht to trunc"; - float tail_width = ImGui::CalcTextSize(tail).x; + float tail_width = ImGui::CalcTextSize(tail).x; assert(width > tail_width); - if (width < tail_width) return "Error: Can't add tail and not be under wanted width."; + if (width <= tail_width) return "Error: Can't add tail and not be under wanted width."; float allowed_width = width - tail_width; - unsigned count_letter = static_cast(allowed_width / letter_width); - std::string result_text = text.substr(0, count_letter); - text_width = ImGui::CalcTextSize(result_text.c_str()).x; + + // guess approx count of letter + float average_letter_width = calc_text_size(std::string_view("n")).x; // average letter width + unsigned count_letter = static_cast(allowed_width / average_letter_width); + + std::string_view text_ = text; + std::string_view result_text = text_.substr(0, count_letter); + text_width = calc_text_size(result_text).x; if (text_width < allowed_width) { // increase letter count while (true) { ++count_letter; - std::string act_text = text.substr(0, count_letter); - text_width = ImGui::CalcTextSize(act_text.c_str()).x; - if (text_width < allowed_width) return result_text+tail; - result_text = std::move(act_text); + std::string_view act_text = text_.substr(0, count_letter); + text_width = calc_text_size(act_text).x; + if (text_width < allowed_width) { + result_text = std::move(act_text); + break; + } } } else { // decrease letter count while (true) { --count_letter; - result_text = text.substr(0, count_letter); - text_width = ImGui::CalcTextSize(result_text.c_str()).x; - if (text_width > allowed_width) return result_text+tail; + result_text = text_.substr(0, count_letter); + text_width = calc_text_size(result_text).x; + if (text_width > allowed_width) break; + if (count_letter == 1) return "Error: No letters left. Can't return only tail."; + } } - assert(false); - return "Should not be accessible"; + return std::string(result_text) + tail; } ImVec2 ImGuiWrapper::suggest_location(const ImVec2 &dialog_size, diff --git a/src/slic3r/GUI/ImGuiWrapper.hpp b/src/slic3r/GUI/ImGuiWrapper.hpp index cee192ec38..009b239b6e 100644 --- a/src/slic3r/GUI/ImGuiWrapper.hpp +++ b/src/slic3r/GUI/ImGuiWrapper.hpp @@ -2,6 +2,7 @@ #define slic3r_ImGuiWrapper_hpp_ #include +#include #include #include @@ -72,7 +73,12 @@ public: float scaled(float x) const { return x * m_font_size; } ImVec2 scaled(float x, float y) const { return ImVec2(x * m_font_size, y * m_font_size); } - ImVec2 calc_text_size(const wxString &text, float wrap_width = -1.0f) const; + /// + /// Extend ImGui::CalcTextSize to use string_view + /// + static ImVec2 calc_text_size(std::string_view text, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); + static ImVec2 calc_text_size(const std::string& text, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); + static ImVec2 calc_text_size(const wxString &text, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); ImVec2 calc_button_size(const wxString &text, const ImVec2 &button_size = ImVec2(0, 0)) const; ImVec2 get_item_spacing() const; @@ -153,13 +159,13 @@ public: /// NOTE 1: ImGui must be initialized /// NOTE 2: Calculation for actual acive imgui font /// - /// text to be truncated - /// maximal widht before truncate - /// strung put on end of text to be visible truncation + /// Text to be truncated + /// Maximal width before truncate + /// String puted on end of text to be visible truncation /// Truncated text static std::string trunc(const std::string &text, float width, - const char * tail = " .."); + const char *tail = " .."); /// /// Suggest loacation of dialog window,