logmeow/windows/filters.cpp

258 lines
10 KiB
C++
Raw Normal View History

2023-01-28 14:11:46 +00:00
#include <imgui.h>
#include <imgui_stdlib.h>
#include "../group_panel.h"
#include "../filters.h"
#include "filters.h"
static inline void render_integer_filter(IntegerFilter* filter);
static inline void render_string_filter(StringFilter* filter);
static inline void render_buffer_filter(BufferFilter* filter);
static inline void render_priority_filter(PriorityFilter* filter);
static inline void render_group_filter(GroupFilter* filter);
2023-01-28 15:12:11 +00:00
static std::unique_ptr<Filter> render_add_filter_popup();
2023-01-28 14:11:46 +00:00
static void update_logcat_entries(const Filters& filters,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets);
static void render_filter(Filter* filter, std::string* title, bool* request_removal) {
ImGui::PushID(filter);
ImGui::BeginGroupPanel();
if (title) {
ImGui::AlignTextToFramePadding();
ImGui::Text("Title:%s%s", title->empty() ? "" : " ", title->c_str());
ImGui::SameLine();
std::string change_title_id = std::string("changefiltertitle_") + std::to_string(reinterpret_cast<size_t>(filter));
if (ImGui::Button("Edit")) {
ImGui::OpenPopup(change_title_id.c_str());
}
if (ImGui::BeginPopup(change_title_id.c_str())) {
ImGui::InputText("##title", title);
ImGui::EndPopup();
}
ImGui::SameLine();
}
if (ImGui::Button("Remove")) {
*request_removal = true;
}
{
ImGui::SameLine();
bool disabled = filter->disabled();
if (ImGui::Checkbox("Disabled", &disabled)) {
filter->disabled(disabled);
}
}
{
StringFilter* sfilter = dynamic_cast<StringFilter*>(filter);
IntegerFilter* ifilter = !sfilter ? dynamic_cast<IntegerFilter*>(filter) : nullptr;
if (sfilter || ifilter) {
ImGui::SameLine();
ImGui::Checkbox("Inverted", sfilter ? &sfilter->inverted : &ifilter->inverted);
}
}
if (filter->error()) {
ImGui::SameLine();
ImGui::Text("(%s)", filter->error()->c_str());
}
if (IntegerFilter* ifilter = dynamic_cast<IntegerFilter*>(filter)) {
render_integer_filter(ifilter);
} else if (StringFilter* sfilter = dynamic_cast<StringFilter*>(filter)) {
render_string_filter(sfilter);
} else if (BufferFilter* bfilter = dynamic_cast<BufferFilter*>(filter)) {
render_buffer_filter(bfilter);
} else if (PriorityFilter* pfilter = dynamic_cast<PriorityFilter*>(filter)) {
render_priority_filter(pfilter);
} else if (GroupFilter* gfilter = dynamic_cast<GroupFilter*>(filter)) {
render_group_filter(gfilter);
} else {
ImGui::TextUnformatted("An unknown filter, this probably shouldn't be mutated");
}
ImGui::EndGroupPanel();
ImGui::PopID();
}
static inline void render_integer_filter(IntegerFilter* filter) {
const char* head;
switch (filter->key) {
case FilterKey::PID: head = "PID"; break;
case FilterKey::TID: head = "TID"; break;
default: head = "Something";
};
ImGui::AlignTextToFramePadding();
ImGui::Text("%s is%s", head, filter->inverted ? " not" : "");
ImGui::SameLine();
if (ImGui::InputScalar("##int", ImGuiDataType_U64, &filter->other)) {
filter->updated();
}
}
static inline void render_string_filter(StringFilter* filter) {
const char* head;
switch (filter->key) {
case FilterKey::User: head = "User"; break;
case FilterKey::Tag: head = "Tag"; break;
case FilterKey::Message: head = "Message"; break;
default: head = "Something";
};
ImGui::AlignTextToFramePadding();
ImGui::Text("%s is%s", head, filter->inverted ? " not" : "");
ImGui::SameLine();
if (ImGui::InputText("##str", &filter->other)) {
filter->updated();
}
}
static inline void render_buffer_filter(BufferFilter* filter) {
auto update_if_needed = [&](bool updated) {
if (updated) {
filter->updated();
}
};
ImGui::AlignTextToFramePadding();
ImGui::TextUnformatted("Buffer is:"); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Unknown", &filter->wanted, static_cast<unsigned int>(Buffer::Unknown))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Main", &filter->wanted, static_cast<unsigned int>(Buffer::Main))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("System", &filter->wanted, static_cast<unsigned int>(Buffer::System))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Radio", &filter->wanted, static_cast<unsigned int>(Buffer::Radio))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Events", &filter->wanted, static_cast<unsigned int>(Buffer::Events))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Crash", &filter->wanted, static_cast<unsigned int>(Buffer::Crash)));
}
static inline void render_priority_filter(PriorityFilter* filter) {
auto update_if_needed = [&](bool updated) {
if (updated) {
filter->updated();
}
};
ImGui::AlignTextToFramePadding();
ImGui::TextUnformatted("Priority is:"); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Unknown", &filter->wanted, static_cast<unsigned int>(Priority::Unknown))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Verbose", &filter->wanted, static_cast<unsigned int>(Priority::Verbose))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Debug", &filter->wanted, static_cast<unsigned int>(Priority::Debug))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Info", &filter->wanted, static_cast<unsigned int>(Priority::Info))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Warning", &filter->wanted, static_cast<unsigned int>(Priority::Warn))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Error", &filter->wanted, static_cast<unsigned int>(Priority::Error))); ImGui::SameLine();
update_if_needed(ImGui::CheckboxFlags("Fatal", &filter->wanted, static_cast<unsigned int>(Priority::Fatal)));
}
static inline void render_group_filter(GroupFilter* filter) {
int selected_type = static_cast<int>(filter->type);
if (ImGui::Combo("of these are true", &selected_type, "All\0Any\0One\0None\0")) {
filter->type = static_cast<GroupFilter::Type>(selected_type);
}
for (std::vector<std::unique_ptr<Filter>>::iterator it = filter->filters.begin(); it != filter->filters.end();) {
bool removal_requested = false;
render_filter(it->get(), nullptr, &removal_requested);
if (removal_requested) {
filter->filters.erase(it);
filter->updated();
} else {
it++;
}
}
std::string add_filter_id = std::string("addfilter_") + std::to_string(reinterpret_cast<size_t>(filter));
if (ImGui::Button("+ add filter")) {
ImGui::OpenPopup(add_filter_id.c_str());
}
if (ImGui::BeginPopup(add_filter_id.c_str())) {
std::unique_ptr<Filter> added_filter = render_add_filter_popup();
if (added_filter) {
filter->filters.push_back(std::move(added_filter));
filter->updated();
}
ImGui::EndPopup();
}
}
2023-01-28 15:12:11 +00:00
static std::unique_ptr<Filter> render_add_filter_popup() {
2023-01-28 14:11:46 +00:00
if (ImGui::Selectable("Buffer")) {
return std::make_unique<BufferFilter>(0);
} else if (ImGui::Selectable("User")) {
return std::make_unique<StringFilter>(FilterKey::User, "");
} else if (ImGui::Selectable("PID")) {
return std::make_unique<IntegerFilter>(FilterKey::PID, 0);
} else if (ImGui::Selectable("TID")) {
return std::make_unique<IntegerFilter>(FilterKey::TID, 0);
} else if (ImGui::Selectable("Priority")) {
return std::make_unique<PriorityFilter>(0);
} else if (ImGui::Selectable("Tag")) {
return std::make_unique<StringFilter>(FilterKey::Tag, "");
} else if (ImGui::Selectable("Message")) {
return std::make_unique<StringFilter>(FilterKey::Message, "");
} else if (ImGui::Selectable("Group of filters")) {
return std::make_unique<GroupFilter>(std::vector<std::unique_ptr<Filter>>(), GroupFilter::Type::All);
}
return std::unique_ptr<Filter>();
}
static void update_logcat_entries(const Filters& filters,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets) {
filtered_logcat_entry_offsets.clear();
for (size_t i=0; i < logcat_entries.size(); i++) {
if (matches(filters, logcat_entries[i])) {
filtered_logcat_entry_offsets.push_back(i);
}
}
}
void filters_window(Filters& active_filters, Filters& inactive_filters,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
bool* p_open) {
if (!ImGui::Begin("Filters", p_open)) {
ImGui::End();
return;
}
ImGui::TextUnformatted("You can use regex for strings by prepending \"regex:\"");
for (Filters::iterator it = inactive_filters.begin(); it != inactive_filters.end();) {
bool removal_requested = false;
render_filter(it->second.get(), &it->first, &removal_requested);
if (removal_requested) {
inactive_filters.erase(it);
} else {
it++;
}
}
if (ImGui::Button("+ add filter")) {
ImGui::OpenPopup("addfilter_root");
}
if (ImGui::BeginPopup("addfilter_root")) {
std::unique_ptr<Filter> added_filter = render_add_filter_popup();
if (added_filter) {
inactive_filters.push_back(std::make_pair("", std::move(added_filter)));
}
ImGui::EndPopup();
}
ImGui::Separator();
ImVec2 button_size(4 * ImGui::GetFontSize(), 0);
if (ImGui::Button("OK", button_size)) {
active_filters = std::move(inactive_filters);
update_logcat_entries(active_filters, logcat_entries, filtered_logcat_entry_offsets);
*p_open = false;
}
ImGui::SameLine();
if (ImGui::Button("Cancel", button_size)) {
*p_open = false;
}
ImGui::SameLine();
if (ImGui::Button("Apply", button_size)) {
copy_filters(active_filters, inactive_filters);
update_logcat_entries(active_filters, logcat_entries, filtered_logcat_entry_offsets);
}
ImGui::End();
}