Skip to content

Commit

Permalink
display bindings as table
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 10, 2024
1 parent 55fb90a commit 2f002af
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
3 changes: 2 additions & 1 deletion include/helpers/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ inline std::string join(std::vector<std::string> v, std::string_view sep) {
}

std::vector<std::string> split(const std::string& str, const std::string& sep);
} // namespace ImPlay

bool findCase(std::string haystack, std::string needle);
} // namespace ImPlay
3 changes: 3 additions & 0 deletions include/mpv.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ class Mpv {
};

struct BindingItem {
std::string section;
std::string key;
std::string cmd;
std::string comment;
int64_t priority;
bool weak;
};

struct AudioDevice {
Expand Down
6 changes: 6 additions & 0 deletions source/helpers/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,10 @@ std::vector<std::string> split(const std::string& str, const std::string& sep) {
if (pos1 != str.length()) v.push_back(str.substr(pos1));
return v;
}

bool findCase(std::string haystack, std::string needle) {
auto it = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(),
[](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); });
return it != haystack.end();
}
} // namespace ImPlay
8 changes: 7 additions & 1 deletion source/mpv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,18 @@ void Mpv::initBindings(mpv_node &node) {
for (int j = 0; j < item.u.list->num; j++) {
auto key = item.u.list->keys[j];
auto value = item.u.list->values[j];
if (strcmp(key, "key") == 0) {
if (strcmp(key, "section") == 0) {
t.section = value.u.string;
} else if (strcmp(key, "key") == 0) {
t.key = value.u.string;
} else if (strcmp(key, "cmd") == 0) {
t.cmd = value.u.string;
} else if (strcmp(key, "comment") == 0) {
t.comment = value.u.string;
} else if (strcmp(key, "priority") == 0) {
t.priority = value.u.int64;
} else if (strcmp(key, "is_weak") == 0) {
t.weak = value.u.flag;
}
}
bindings.emplace_back(t);
Expand Down
54 changes: 34 additions & 20 deletions source/views/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,41 @@ void Debug::drawBindings() {
if (!ImGui::CollapsingHeader(i18n_a("views.debug.bindings", bindings.size()).c_str())) return;
m_node = "Bindings";

if (ImGui::BeginListBox("input-bindings", ImVec2(-FLT_MIN, -FLT_MIN))) {
for (auto& binding : bindings) {
std::string title = binding.comment;
if (title.empty()) title = binding.cmd;
title = title.substr(0, 50);
if (title.size() == 50) title += "...";

ImGui::PushID(&binding);
ImGui::Selectable("", false);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal)) ImGui::SetTooltip("%s", binding.cmd.c_str());

ImGui::SameLine();
ImGui::Text("%s", title.c_str());
static char buf[256] = "";
ImGui::TextUnformatted("views.debug.commands.filter"_i18n);
ImGui::SameLine();
ImGui::PushItemWidth(-1);
ImGui::InputText("##Filter.bindings", buf, IM_ARRAYSIZE(buf));
ImGui::PopItemWidth();

ImGui::SameLine(ImGui::GetContentRegionAvail().x * 0.75f);
ImGui::BeginDisabled();
ImGui::Button(binding.key.c_str());
ImGui::EndDisabled();
ImGui::PopID();
static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter |
ImGuiTableFlags_BordersV | ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_ScrollY;
if (ImGui::BeginTable("input-bindings", 6, flags)) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("Section", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Priority", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Weak", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Key", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Command", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("Comment", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableHeadersRow();
for (auto& binding : bindings) {
if (buf[0] != '\0' && !findCase(binding.key, buf) && !findCase(binding.cmd, buf)) continue;
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Selectable(binding.section.c_str(), false, ImGuiSelectableFlags_SpanAllColumns);
ImGui::TableNextColumn();
ImGui::Text("%d", binding.priority);
ImGui::TableNextColumn();
ImGui::Text("%s", binding.weak ? "yes" : "no");
ImGui::TableNextColumn();
ImGui::Text("%s", binding.key.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", binding.cmd.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", binding.comment.c_str());
}
ImGui::EndListBox();
ImGui::EndTable();
}
}

Expand Down Expand Up @@ -167,7 +181,7 @@ void Debug::drawCommands() {
ImGui::PopItemWidth();
if (ImGui::BeginListBox("command-list", ImVec2(-FLT_MIN, -FLT_MIN))) {
for (auto& [name, args] : commands) {
if (!name.starts_with(buf)) continue;
if (buf[0] != '\0' && !findCase(name, buf)) continue;
ImGui::PushID(name.c_str());
ImGui::Selectable("", false);
ImGui::SameLine();
Expand Down

0 comments on commit 2f002af

Please sign in to comment.