ENH:add a search box for text toolbar

Change-Id: If8a43671916e56d6cb52b9fbd21dd8b8f9539083
This commit is contained in:
liz.li 2022-12-15 17:32:29 +08:00 committed by Lane.Wei
parent ac854d8f4d
commit 78b6e75955
8 changed files with 177 additions and 29 deletions

View file

@ -82,6 +82,8 @@ static const std::map<const wchar_t, std::string> font_icons = {
{ImGui::GapFillDarkIcon , "gap_fill_dark" },
{ImGui::SphereButtonDarkIcon , "toolbar_modifier_sphere_dark" },
{ImGui::TextSearchIcon , "im_text_search" },
{ImGui::TextSearchCloseIcon , "im_text_search_close" },
};
static const std::map<const wchar_t, std::string> font_icons_large = {
{ImGui::CloseNotifButton , "notification_close" },
@ -542,6 +544,123 @@ void ImGuiWrapper::set_next_window_size(float x, float y, ImGuiCond cond)
}
/* BBL style widgets */
bool ImGuiWrapper::bbl_combo_with_filter(const char* label, const std::string& preview_value, const std::vector<std::string>& all_items, std::vector<int>* filtered_items_idx, bool* is_filtered, float item_height)
{
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
ImGuiWindow* window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return false;
static char pattern_buffer[256] = { 0 };
auto simple_match = [](const char* pattern, const char* str) {
wxString sub_str = wxString(pattern).Lower();
wxString main_str = wxString(str).Lower();
return main_str.Find(sub_str);
};
bool is_filtering = false;
bool is_new_open = false;
float sz = ImGui::GetFrameHeight();
ImVec2 arrow_size(sz, sz);
ImVec2 CursorPos = window->DC.CursorPos;
const ImRect arrow_bb(CursorPos, CursorPos + arrow_size);
float ButtonTextAlignX = g.Style.ButtonTextAlign.x;
g.Style.ButtonTextAlign.x = 0;
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { sz, style.FramePadding.y});
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered));
if (button(preview_value + label, ImGui::CalcItemWidth(), 0))
{
ImGui::OpenPopup(label);
is_new_open = true;
}
g.Style.ButtonTextAlign.x = ButtonTextAlignX;
ImGui::PopStyleVar();
ImGui::PopStyleColor();
ImGui::BBLRenderArrow(window->DrawList, arrow_bb.Min + ImVec2(ImMax(0.0f, (arrow_size.x - g.FontSize) * 0.5f), ImMax(0.0f, (arrow_size.y - g.FontSize) * 0.5f)), ImGui::GetColorU32(ImGuiCol_Text), ImGuiDir_Down);
if (is_new_open)
memset(pattern_buffer, 0, IM_ARRAYSIZE(pattern_buffer));
float item_rect_width = ImGui::GetItemRectSize().x;
float item_rect_height = item_height ? item_height : ImGui::GetItemRectSize().y;
ImGui::SetNextWindowPos({ CursorPos.x, ImGui::GetItemRectMax().y + 4 * m_style_scaling });
ImGui::SetNextWindowSize({ item_rect_width, 0 });
if (ImGui::BeginPopup(label))
{
ImGuiWindow* popup_window = ImGui::GetCurrentWindow();
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(8.0f * m_style_scaling, item_rect_height - g.FontSize) * 0.5f);
wchar_t ICON_SEARCH = *pattern_buffer != '\0' ? ImGui::TextSearchCloseIcon : ImGui::TextSearchIcon;
const ImVec2 label_size = ImGui::CalcTextSize(into_u8(ICON_SEARCH).c_str(), nullptr, true);
const ImVec2 search_icon_pos(ImGui::GetItemRectMax().x - label_size.x, popup_window->DC.CursorPos.y + style.FramePadding.y);
ImGui::RenderText(search_icon_pos, into_u8(ICON_SEARCH).c_str());
auto temp = popup_window->DC.CursorPos;
popup_window->DC.CursorPos = search_icon_pos;
ImGui::PushStyleColor(ImGuiCol_Button, {0, 0, 0, 0});
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_Button));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_Button));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_Button));
ImGui::PushStyleColor(ImGuiCol_Border, { 0, 0, 0, 0 });
if (button("##invisible_clear_button", label_size.x, label_size.y))
{
if (*pattern_buffer != '\0')
memset(pattern_buffer, 0, IM_ARRAYSIZE(pattern_buffer));
}
ImGui::PopStyleColor(5);
popup_window->DC.CursorPos = temp;
ImGui::PushItemWidth(item_rect_width);
if (is_new_open)
ImGui::SetKeyboardFocusHere();
ImGui::InputText("##bbl_combo_with_filter_inputText", pattern_buffer, sizeof(pattern_buffer));
ImGui::PopItemWidth();
ImGui::PopStyleVar();
if (*pattern_buffer != '\0')
is_filtering = true;
if (is_filtering)
{
std::vector<std::pair<int, int> > filtered_items_with_priority;// std::pair<index, priority>
for (int i = 0; i < all_items.size(); i++)
{
int priority = simple_match(pattern_buffer, all_items[i].c_str());
if (priority != wxNOT_FOUND)
filtered_items_with_priority.push_back({ i, priority });
}
std::sort(filtered_items_with_priority.begin(), filtered_items_with_priority.end(), [](const std::pair<int, int>& a, const std::pair<int, int>& b) {return (b.second > a.second); });
for (auto item : filtered_items_with_priority)
{
filtered_items_idx->push_back(item.first);
}
}
*is_filtered = is_filtering;
popup_window->DC.CursorPos.y -= 1 * m_style_scaling;
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(1.0f, 1.0f) * m_style_scaling);
if (ImGui::BeginListBox("##bbl_combo_with_filter_listBox", { item_rect_width, item_rect_height * 7.75f})) {
ImGui::PopStyleVar(2);
return true;
}
else
{
ImGui::PopStyleVar(2);
ImGui::EndPopup();
return false;
}
}
else
return false;
}
bool ImGuiWrapper::bbl_input_double(const wxString& label, const double& value, const std::string& format)
{
//return ImGui::InputDouble(label.c_str(), const_cast<double *>(&value), 0.0f, 0.0f, format.c_str(), ImGuiInputTextFlags_CharsDecimal);