Skip to content
Repos

mologram

C++ 1.4 KB37 lines Opens in site

Directory Raw Download

#include "mologram/ui/ActionRouter.hpp"

#include "mologram/ActionRegistry.hpp"

namespace mologram::ui {

std::string ActionRouter::canonicalKey(const std::string& name) {
  size_t plus = name.rfind('+');
  std::string head = plus == std::string::npos ? "" : name.substr(0, plus + 1);
  std::string base = plus == std::string::npos ? name : name.substr(plus + 1);
  if (base.size() == 1 && std::isalpha((unsigned char)base[0])) base[0] = (char)std::toupper((unsigned char)base[0]);
  return head + base;
}

bool ActionRouter::run(const std::string& action) {
  auto it = handlers_.find(action);
  if (it == handlers_.end()) return false;
  it->second();
  return true;
}

std::string ActionRouter::actionFor(const ttgui::KeyEventData& k, bool composerFocused) const {
  std::string name = canonicalKey(k.name());
  if (name.empty()) return "";
  bool modified = k.ctrl || k.alt || (k.code >= ttgui::KeyCode::F1 && k.code <= ttgui::KeyCode::F12);
  if (!modified) return "";
  for (const auto& a : ActionRegistry::all()) {
    if (canonicalKey(cfg_.keyFor(a.id)) != name) continue;
    // One key shared by search and the link dialog (D-08): the composer decides.
    if ((a.id == "search" || a.id == "fmt_link") && canonicalKey(cfg_.keyFor("search")) == name && canonicalKey(cfg_.keyFor("fmt_link")) == name)
      return composerFocused ? "fmt_link" : "search";
    return a.id;
  }
  return "";
}

} // namespace mologram::ui
32.9ms