#include "KeyboardShortcut.h" namespace minixer { namespace { static constexpr int defaultRawFlags = 0; } //============================================================================== std::unique_ptr SlotShortcut::toXml (const juce::String& tagName) const { auto xml = std::make_unique (tagName); inputSource.writeToXml (*xml); xml->setAttribute ("defaultBypassed", defaultBypassed); xml->setAttribute ("mode", ShortcutSettings::slotShortcutModeToString (mode)); return xml; } //============================================================================== bool SlotShortcut::fromXml (const juce::XmlElement& xml) { defaultBypassed = xml.getBoolAttribute ("defaultBypassed", false); mode = ShortcutSettings::stringToSlotShortcutMode (xml.getStringAttribute ("mode", "cycle")); // 优先读取新版 inputType 字段 if (xml.hasAttribute ("inputType")) return inputSource.readFromXml (xml); // 兼容旧版:仅有 keyCode / modifiers / textCharacter if (xml.hasAttribute ("keyCode")) return inputSource.readLegacyKeyboardFromXml (xml); return false; } //============================================================================== ShortcutSettings::ShortcutSettings() { resetToDefaults(); } //============================================================================== void ShortcutSettings::resetToDefaults() { globalShortcuts[static_cast (GlobalShortcutAction::bringWindowToFront)] = juce::KeyPress ('m', juce::ModifierKeys::ctrlModifier | juce::ModifierKeys::shiftModifier, 0); globalShortcuts[static_cast (GlobalShortcutAction::toggleAllPluginsBypass)] = juce::KeyPress ('b', juce::ModifierKeys::ctrlModifier | juce::ModifierKeys::shiftModifier, 0); globalShortcuts[static_cast (GlobalShortcutAction::toggleSettingsPanel)] = juce::KeyPress ('d', juce::ModifierKeys::ctrlModifier, 0); globalShortcuts[static_cast (GlobalShortcutAction::deleteFocusedSlot)] = juce::KeyPress (juce::KeyPress::deleteKey); for (auto& slot : slotShortcuts) slot.clear(); } //============================================================================== juce::KeyPress ShortcutSettings::getGlobalShortcut (GlobalShortcutAction action) const { auto index = static_cast (action); if (index < globalShortcuts.size()) return globalShortcuts[index]; return {}; } //============================================================================== void ShortcutSettings::setGlobalShortcut (GlobalShortcutAction action, const juce::KeyPress& key) { auto index = static_cast (action); if (index < globalShortcuts.size()) globalShortcuts[index] = key; } //============================================================================== const SlotShortcut& ShortcutSettings::getSlotShortcut (int slotIndex) const { jassert (juce::isPositiveAndBelow (slotIndex, static_cast (slotShortcuts.size()))); return slotShortcuts[static_cast (slotIndex)]; } //============================================================================== void ShortcutSettings::setSlotShortcut (int slotIndex, const SlotShortcut& shortcut) { jassert (juce::isPositiveAndBelow (slotIndex, static_cast (slotShortcuts.size()))); slotShortcuts[static_cast (slotIndex)] = shortcut; } //============================================================================== juce::Array ShortcutSettings::findSlotIndicesForInputSource (const ShortcutInputSource& source) const { juce::Array result; if (! source.isValid()) return result; for (int i = 0; i < static_cast (slotShortcuts.size()); ++i) { if (slotShortcuts[static_cast (i)].inputSource.matches (source)) result.add (i); } return result; } //============================================================================== juce::Array ShortcutSettings::findSlotIndicesForKey (const juce::KeyPress& key) const { if (! key.isValid()) return {}; return findSlotIndicesForInputSource (ShortcutInputSource (key)); } //============================================================================== std::unique_ptr ShortcutSettings::toXml() const { auto xml = std::make_unique ("ShortcutSettings"); xml->setAttribute ("version", 2); auto globalsXml = std::make_unique ("GlobalShortcuts"); for (size_t i = 0; i < globalShortcuts.size(); ++i) { auto action = static_cast (i); auto keyXml = std::make_unique ("Shortcut"); keyXml->setAttribute ("action", static_cast (action)); keyXml->setAttribute ("keyCode", globalShortcuts[i].getKeyCode()); keyXml->setAttribute ("modifiers", static_cast (globalShortcuts[i].getModifiers().getRawFlags())); keyXml->setAttribute ("textCharacter", juce::String::charToString (globalShortcuts[i].getTextCharacter())); globalsXml->addChildElement (keyXml.release()); } xml->addChildElement (globalsXml.release()); auto slotsXml = std::make_unique ("SlotShortcuts"); for (int i = 0; i < static_cast (slotShortcuts.size()); ++i) { auto slotXml = slotShortcuts[static_cast (i)].toXml ("Slot"); slotXml->setAttribute ("index", i); slotsXml->addChildElement (slotXml.release()); } xml->addChildElement (slotsXml.release()); return xml; } //============================================================================== bool ShortcutSettings::fromXml (const juce::XmlElement& xml) { if (! xml.hasTagName ("ShortcutSettings")) return false; if (auto* globalsXml = xml.getChildByName ("GlobalShortcuts")) { for (auto* child = globalsXml->getFirstChildElement(); child != nullptr; child = child->getNextElement()) { if (child->hasTagName ("Shortcut")) { auto actionIndex = child->getIntAttribute ("action", -1); if (juce::isPositiveAndBelow (actionIndex, static_cast (GlobalShortcutAction::numActions))) { auto keyCode = child->getIntAttribute ("keyCode"); auto modifiers = juce::ModifierKeys (child->getIntAttribute ("modifiers", 0)); auto textChar = child->getStringAttribute ("textCharacter")[0]; globalShortcuts[static_cast (actionIndex)] = juce::KeyPress (keyCode, modifiers, textChar); } } } } if (auto* slotsXml = xml.getChildByName ("SlotShortcuts")) { for (auto* child = slotsXml->getFirstChildElement(); child != nullptr; child = child->getNextElement()) { if (child->hasTagName ("Slot")) { auto index = child->getIntAttribute ("index", -1); if (juce::isPositiveAndBelow (index, static_cast (slotShortcuts.size()))) { SlotShortcut slot; if (slot.fromXml (*child)) slotShortcuts[static_cast (index)] = slot; } } } } return true; } //============================================================================== juce::String ShortcutSettings::toXmlString() const { auto xml = toXml(); return xml->toString(); } //============================================================================== bool ShortcutSettings::fromXmlString (const juce::String& text) { if (text.isEmpty()) return false; auto xml = juce::XmlDocument::parse (text); if (xml == nullptr) return false; return fromXml (*xml); } //============================================================================== juce::String ShortcutSettings::getGlobalShortcutActionName (GlobalShortcutAction action) { switch (action) { case GlobalShortcutAction::bringWindowToFront: return TRANS ("Bring window to front"); case GlobalShortcutAction::toggleAllPluginsBypass: return TRANS ("Toggle all plugins bypass"); case GlobalShortcutAction::toggleSettingsPanel: return TRANS ("Toggle settings panel"); case GlobalShortcutAction::deleteFocusedSlot: return TRANS ("Delete focused slot"); default: return TRANS ("Unknown"); } } //============================================================================== juce::String ShortcutSettings::getGlobalShortcutActionDescription (GlobalShortcutAction action) { switch (action) { case GlobalShortcutAction::bringWindowToFront: return TRANS ("Show and focus the main Minixer window."); case GlobalShortcutAction::toggleAllPluginsBypass: return TRANS ("Bypass or unbypass all loaded plugins at once."); case GlobalShortcutAction::toggleSettingsPanel: return TRANS ("Open or close the settings panel."); case GlobalShortcutAction::deleteFocusedSlot: return TRANS ("Remove the plugin in the currently focused slot."); default: return {}; } } //============================================================================== juce::String ShortcutSettings::slotShortcutModeToString (SlotShortcutMode mode) { return mode == SlotShortcutMode::holdToggle ? "hold" : "cycle"; } //============================================================================== SlotShortcutMode ShortcutSettings::stringToSlotShortcutMode (const juce::String& text) { return text.equalsIgnoreCase ("hold") ? SlotShortcutMode::holdToggle : SlotShortcutMode::cycleToggle; } } // namespace minixer