Start using string_view in truncate text

This commit is contained in:
Filip Sykala 2022-03-03 08:13:00 +01:00
parent a0dde4651b
commit ad1510a30c
5 changed files with 55 additions and 27 deletions

View File

@ -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() = "";

View File

@ -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);

View File

@ -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)

View File

@ -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;
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<unsigned>(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<unsigned>(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;
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,

View File

@ -2,6 +2,7 @@
#define slic3r_ImGuiWrapper_hpp_
#include <string>
#include <string_view>
#include <map>
#include <imgui/imgui.h>
@ -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;
/// <summary>
/// Extend ImGui::CalcTextSize to use string_view
/// </summary>
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,9 +159,9 @@ public:
/// NOTE 1: ImGui must be initialized
/// NOTE 2: Calculation for actual acive imgui font
/// </summary>
/// <param name="text">text to be truncated</param>
/// <param name="width">maximal widht before truncate</param>
/// <param name="tail">strung put on end of text to be visible truncation</param>
/// <param name="text">Text to be truncated</param>
/// <param name="width">Maximal width before truncate</param>
/// <param name="tail">String puted on end of text to be visible truncation</param>
/// <returns>Truncated text</returns>
static std::string trunc(const std::string &text,
float width,