// Round 190sj (issue #327, 0.1.32). Included in the app class of Main.cpp; --keysheet190sjtest and --selftest // "keysheet190sj". Every key of the F1 shortcut sheet (keys::sheet::rows) is pressed in the main window // (MainComponent::keyPressed, as a key the system delivers) with the panel it belongs to focused, the typing keyboard off // and on: the action the key map gives it there must be the one that runs (seen by MainComponent::actionFilterForCheck, // which keeps file choosers and dialogs from opening). A General key is pressed with every panel focused; where a panel // has its own action on it (FL's Ctrl+T in the Playlist and the Piano roll: Add auto marker) that one must run. Then the // keys the issue named do what they say, a remapped key moves with its action, and the sheet shows the remapped key. juce::String keySheet190sjCheck (juce::String& log) { using namespace lynx; using Panel = workspace::Panel; juce::String result = "OK"; const auto check = [&] (const juce::String& what, bool ok, const juce::String& detail = {}) { log << (ok ? "ok " : "FAIL ") << what << (detail.isNotEmpty() ? " [" + detail + "]" : juce::String()) << "\n"; if (! ok && result == "OK") result = "ERROR " + what + " " + detail; }; struct Keep { layoutstyle::Style was = layoutstyle::current(); ~Keep() { layoutstyle::setCurrent (was); MainComponent::actionFilterForCheck = nullptr; } } keep; AudioEngine offline (false); MainComponent ui (offline, false); ui.setSize (1600, 900); ui.setLayoutStyle (layoutstyle::Style::pattern); const auto settle = [&] { for (int k = 0; k < 3; ++k) offline.dispatchPendingMessages(); }; settle(); ui.showPanelsForSnapshot ({ Panel::playlist, Panel::channelRack, Panel::pianoRoll, Panel::mixer, Panel::browser }); settle(); const auto focus = [&] (Panel p) { ui.workspaceForTest().activate (p); settle(); }; // a key as JUCE hands it over: letters by their upper-case code, the others by JUCE's key codes const auto keyPressOf = [] (const keys::Chord& c) { int code = c.key; struct Pair { int key, juce; }; static const Pair specials[] = { { keys::space, juce::KeyPress::spaceKey }, { keys::enter, juce::KeyPress::returnKey }, { keys::escape, juce::KeyPress::escapeKey }, { keys::tab, juce::KeyPress::tabKey }, { keys::backspace, juce::KeyPress::backspaceKey }, { keys::del, juce::KeyPress::deleteKey }, { keys::insert, juce::KeyPress::insertKey }, { keys::up, juce::KeyPress::upKey }, { keys::down, juce::KeyPress::downKey }, { keys::left, juce::KeyPress::leftKey }, { keys::right, juce::KeyPress::rightKey }, { keys::pageUp, juce::KeyPress::pageUpKey }, { keys::pageDown, juce::KeyPress::pageDownKey }, { keys::home, juce::KeyPress::homeKey }, { keys::end, juce::KeyPress::endKey }, { keys::numpadPlus, juce::KeyPress::numberPadAdd }, { keys::numpadMinus, juce::KeyPress::numberPadSubtract }, { keys::numpadMultiply, juce::KeyPress::numberPadMultiply }, { keys::numpadDivide, juce::KeyPress::numberPadDivide }, }; for (const auto& p : specials) if (p.key == c.key) code = p.juce; if (c.key >= keys::f1 && c.key <= keys::f12) code = juce::KeyPress::F1Key + (c.key - keys::f1); const auto mods = ((c.mods & keys::ctrl) ? juce::ModifierKeys::commandModifier : 0) | ((c.mods & keys::shift) ? juce::ModifierKeys::shiftModifier : 0) | ((c.mods & keys::alt) ? juce::ModifierKeys::altModifier : 0); const auto character = c.key > ' ' && c.key < 0x7f && (c.mods & (keys::ctrl | keys::alt)) == 0 ? (juce::juce_wchar) c.key : (juce::juce_wchar) 0; return juce::KeyPress (code, juce::ModifierKeys (mods), character); }; const auto contextOf = [] (Panel p) { return p == Panel::playlist ? keys::Context::playlist : p == Panel::channelRack ? keys::Context::channelRack : p == Panel::pianoRoll ? keys::Context::pianoRoll : p == Panel::mixer ? keys::Context::mixer : keys::Context::browser; }; const auto panelName = [] (Panel p) { return juce::String (p == Panel::playlist ? "Playlist" : p == Panel::channelRack ? "Channel rack" : p == Panel::pianoRoll ? "Piano roll" : p == Panel::mixer ? "Mixer" : "Browser"); }; const auto& map = ui.getKeyMapForSnapshot(); // 1. every key of the sheet, dry (the action is seen, not run) std::vector ran; MainComponent::actionFilterForCheck = [&ran] (const std::string& id) { ran.push_back (id); return false; }; int pressed = 0, rowsWithKeys = 0; std::set panelOwn; // a General key that a focused panel uses for its own action for (const auto piano : { false, true }) { ui.setTypingPianoForTest (piano); for (const auto& row : keys::sheet::rows()) { if (row.actions.empty()) { check (juce::String ("a row without keys is a mouse row: ") + row.gesture, juce::String (row.gesture).isNotEmpty()); continue; } ++rowsWithKeys; const auto ctx = keys::sheet::sectionContext (row.section); for (const auto& id : keys::sheet::shownActions (row, map)) { const auto* action = keys::findAction (id); check (juce::String ("the sheet's action exists: ") + id, action != nullptr); if (action == nullptr) continue; check (juce::String (id) + " has a key", ! map.chordsFor (id).empty()); check (juce::String (id) + " belongs to its section's panel", action->context == ctx || action->context == keys::Context::global); const auto panels = action->context == keys::Context::global ? std::vector { Panel::channelRack, Panel::mixer, Panel::browser, Panel::playlist, Panel::pianoRoll } : std::vector { action->context == keys::Context::pianoRoll ? Panel::pianoRoll : action->context == keys::Context::playlist ? Panel::playlist : Panel::channelRack }; for (const auto& chord : map.chordsFor (id)) { // the typing keyboard on: a plain letter / digit is its (a note, or nothing), unless the action works then const auto plain = (chord.mods & (keys::ctrl | keys::alt | keys::shift)) == 0 && chord.isPrintable(); if (piano && plain && (! action->whilePiano || keys::typingPianoKey (chord.key, keys::typingpiano::defaultOctave, 0, keys::typingpiano::pianoLayout()).taken)) continue; for (const auto p : panels) { focus (p); const auto expected = map.find (chord, contextOf (p), piano); ran.clear(); ui.keyPressed (keyPressOf (chord)); settle(); ++pressed; const auto got = ran.empty() ? std::string ("(nothing)") : ran.front(); const auto what = juce::String (keys::toString (chord)) + " (" + id + "), " + panelName (p) + " focused, typing keyboard " + (piano ? "on" : "off"); check (what + " runs " + expected, got == expected, got); if (expected != id) { log << " (that panel's own key: " << expected << ")\n"; panelOwn.insert (id + " @ " + panelName (p).toStdString() + " = " + expected); } } } } } } MainComponent::actionFilterForCheck = nullptr; log << " " << pressed << " key presses, " << rowsWithKeys << " rows with keys\n"; // the General keys a panel keeps for itself, all FL's: Ctrl+T in the Playlist / Piano roll (Add auto marker, REA 168t2), // Ctrl+Shift+S in the Mixer (Save the mixer track state), Enter in the Browser (load the selected file) { const std::set known { "view.typingPiano @ Playlist = playlist.markerAuto", "view.typingPiano @ Piano roll = pianoRoll.markerAuto", "file.saveAs @ Mixer = mixer.saveTrackState", "panel.maximisePlaylist @ Browser = browser.load" }; juce::String got; for (const auto& k : panelOwn) got << k << "; "; check ("the General keys a panel takes for itself are FL's (and only those)", panelOwn == known, got); } // 2. what the issue named, run for real ui.setTypingPianoForTest (false); for (const auto p : { Panel::channelRack, Panel::mixer, Panel::browser }) { focus (p); ui.keyPressed (juce::KeyPress ('T', juce::ModifierKeys::commandModifier, 0)); settle(); check ("Ctrl+T switches the typing keyboard on, " + panelName (p) + " focused", ui.typingPianoForCheck()); ui.keyPressed (juce::KeyPress ('T', juce::ModifierKeys::commandModifier, 0)); settle(); check ("Ctrl+T switches it off again, " + panelName (p) + " focused", ! ui.typingPianoForCheck()); } { focus (Panel::playlist); const auto markers = offline.getSong().markers().size(); ui.keyPressed (juce::KeyPress ('T', juce::ModifierKeys::commandModifier, 0)); settle(); check ("Ctrl+T with the Playlist focused adds an Auto time marker, as FL (REA 168t2)", offline.getSong().markers().size() == markers + 1 && ! ui.typingPianoForCheck()); } const auto channel = offline.getSelectedChannel(); offline.editSong ([channel] (Song& s) { auto& pat = s.patterns[(std::size_t) s.currentPattern]; pat.notes.clear(); for (int i = 0; i < 3; ++i) { Note n; n.channel = channel; n.key = 60 + 2 * i; n.start = i * ppq; n.length = ppq / 2; pat.notes.push_back (n); } }); settle(); focus (Panel::pianoRoll); auto& roll = ui.getPianoRollForSnapshot(); const auto firstKey = [&] { return offline.getSong().patterns[(std::size_t) offline.getSong().currentPattern].notes.front().key; }; const auto firstStart = [&] { return offline.getSong().patterns[(std::size_t) offline.getSong().currentPattern].notes.front().start; }; ui.keyPressed (juce::KeyPress ('A', juce::ModifierKeys::commandModifier, 0)); settle(); check ("Piano roll: Ctrl+A selects every note", roll.selectionForTests().size() == 3, juce::String ((int) roll.selectionForTests().size())); struct Move { const char* name; int code, mods, keys, steps; }; const auto cmd = juce::ModifierKeys::commandModifier, shift = juce::ModifierKeys::shiftModifier; for (const auto& m : { Move { "Shift+Up", juce::KeyPress::upKey, shift, 1, 0 }, Move { "Shift+Down", juce::KeyPress::downKey, shift, -1, 0 }, Move { "Ctrl+Up", juce::KeyPress::upKey, cmd, 12, 0 }, Move { "Ctrl+Down", juce::KeyPress::downKey, cmd, -12, 0 }, Move { "Shift+Right", juce::KeyPress::rightKey, shift, 0, 1 }, Move { "Shift+Left", juce::KeyPress::leftKey, shift, 0, -1 } }) { const auto key = firstKey(); const auto start = firstStart(); ui.keyPressed (juce::KeyPress (m.code, juce::ModifierKeys (m.mods), 0)); settle(); check (juce::String ("Piano roll: ") + m.name + " moves the selected notes", firstKey() == key + m.keys && firstStart() == start + m.steps * roll.gridTicksForSnapshot(), juce::String (key) + " -> " + juce::String (firstKey())); } ui.keyPressed (juce::KeyPress ('D', juce::ModifierKeys::commandModifier, 0)); settle(); check ("Piano roll: Ctrl+D deselects", roll.selectionForTests().empty()); { const auto key = firstKey(); ui.keyPressed (juce::KeyPress (juce::KeyPress::upKey, juce::ModifierKeys (shift), 0)); settle(); check ("Piano roll: Shift+Up with nothing selected moves nothing", firstKey() == key); } // 3. remapped: the octave on Alt+Shift+Up, in Manage shortcuts' map; Ctrl+Up no longer moves; the sheet says so ui.keyMapForCheck().setChords ("pianoRoll.octaveUp", { keys::Chord { keys::up, (std::uint8_t) (keys::alt | keys::shift) } }); roll.setSelectionForSnapshot ({ 0, 1, 2 }); { const auto key = firstKey(); ui.keyPressed (juce::KeyPress (juce::KeyPress::upKey, juce::ModifierKeys (cmd), 0)); settle(); check ("remapped: Ctrl+Up no longer transposes", firstKey() == key); ui.keyPressed (juce::KeyPress (juce::KeyPress::upKey, juce::ModifierKeys (juce::ModifierKeys::altModifier | shift), 0)); settle(); check ("remapped: Alt+Shift+Up transposes an octave", firstKey() == key + 12); const auto& rows = keys::sheet::rows(); const auto octave = std::find_if (rows.begin(), rows.end(), [] (const keys::sheet::Row& r) { return ! r.actions.empty() && r.actions.front() == "pianoRoll.octaveUp"; }); const auto text = octave == rows.end() ? std::string() : keys::sheet::keysText (*octave, ui.getKeyMapForSnapshot(), modmap::Os::windows); check ("the sheet shows the remapped key", text == "Shift+Alt+Up / Ctrl+Down", text); } ui.keyMapForCheck().resetToDefaults(); return result; } // --keyroute190sjtest report.txt (round 190sj, issue #327): the same keys as the user presses them, in a window. Each step // clicks as the system would (through the native window under the point) and presses the key where the system sends it // (Windows: posted to the window with the system's focus, the modifiers in the thread's keyboard state; elsewhere the // focused component's native window). Shows a window (Windows: GUI lock). void runKeyRoute190sj (const juce::String& reportPath) { juce::LookAndFeel::setDefaultLookAndFeel (&lookAndFeel); struct RouteTest final : juce::Timer { juce::String reportPath, report; bool ok = true; int stepIndex = 0; std::unique_ptr engine; lynx::MainComponent* ui = nullptr; std::unique_ptr main; std::function done; void line (const juce::String& text) { report << text << "\n"; juce::File (reportPath).replaceWithText (report); } void check (const juce::String& what, bool passed, const juce::String& detail = {}) { line ((passed ? "ok " : "FAIL ") + what + (detail.isNotEmpty() ? " [" + detail + "]" : juce::String())); ok = ok && passed; } static juce::String describe (juce::Component* c) { if (c == nullptr) return "nothing"; juce::String path; for (auto* p = c; p != nullptr && path.length() < 160; p = p->getParentComponent()) { auto name = p->getComponentID().isNotEmpty() ? p->getComponentID() : p->getName(); if (name.isEmpty()) name = juce::String (typeid (*p).name()).fromLastOccurrenceOf ("::", false, false).substring (0, 28); path << (path.isEmpty() ? "" : " < ") << name; } return path; } void click (juce::Component& c, juce::Point local, juce::ModifierKeys mods = {}) { auto* peer = c.getPeer(); if (peer == nullptr) { line (" (no native window for the click)"); return; } const auto at = peer->getComponent().getLocalPoint (&c, local.toFloat()); line (" click at " + local.toString() + " -> " + describe (peer->getComponent().getComponentAt (at.roundToInt()))); const auto now = juce::Time::currentTimeMillis(); peer->handleMouseEvent (juce::MouseInputSource::InputSourceType::mouse, at, mods.withFlags (juce::ModifierKeys::leftButtonModifier), 1.0f, 0.0f, now); peer->handleMouseEvent (juce::MouseInputSource::InputSourceType::mouse, at, mods, 1.0f, 0.0f, now + 40); } // a key where the system sends it void press (int code, int mods, juce::juce_wchar character = 0) { const juce::ModifierKeys m (mods); juce::String arrived; lynx::MainComponent::keySeenForCheck = [&arrived] (const juce::KeyPress& k) { arrived << k.getTextDescription() << " "; }; const juce::ScopeGuard report { [&] { lynx::MainComponent::keySeenForCheck = nullptr; line (" pressed " + juce::KeyPress (code, m, character).getTextDescription() + ", the main window got: " + (arrived.isEmpty() ? juce::String ("nothing") : arrived)); } }; #if JUCE_WINDOWS // a plain key: posted to the window with the system's focus (Windows makes the character, as for a real key). // With modifiers: JUCE reads them from the hardware (GetAsyncKeyState), which only real input sets, so the key // goes to the JUCE peer of that same window instead (the routing from there on is the real one) if (! m.isAnyModifierKeyDown()) { int vk = code; if (code == juce::KeyPress::upKey) vk = 0x26; // (VK_UP) else if (code == juce::KeyPress::downKey) vk = 0x28; // (VK_DOWN) else if (code == juce::KeyPress::leftKey) vk = 0x25; // (VK_LEFT) else if (code == juce::KeyPress::rightKey) vk = 0x27; // (VK_RIGHT) else if (code >= 'a' && code <= 'z') vk = code - 'a' + 'A'; if (lynx::nativechild::pressKeyForTest (main->getWindowHandle(), vk, false, false, false)) return; } juce::ComponentPeer* systemPeer = nullptr; if (auto* window = lynx::nativechild::focusedWindowForTest()) for (int i = 0; i < juce::ComponentPeer::getNumPeers(); ++i) if (auto* candidate = juce::ComponentPeer::getPeer (i); candidate != nullptr && candidate->getNativeHandle() == window) systemPeer = candidate; line (" (the system's focus: " + (systemPeer != nullptr ? describe (&systemPeer->getComponent()) : juce::String ("not a LYNX window: the main one")) + ")"); #endif auto* focused = juce::Component::getCurrentlyFocusedComponent(); auto* peer = focused != nullptr && focused->getPeer() != nullptr ? focused->getPeer() : main->getPeer(); #if JUCE_WINDOWS peer = systemPeer != nullptr ? systemPeer : main->getPeer(); #endif juce::ModifierKeys::currentModifiers = m; if (peer != nullptr) peer->handleKeyPress (juce::KeyPress (code, m, character)); juce::ModifierKeys::currentModifiers = {}; } juce::String keysNow() const { juce::String k; const auto& s = engine->getSong(); for (const auto& n : s.patterns[(std::size_t) s.currentPattern].notes) k << n.key << " "; return k.trim(); } void transposeCase (const juce::String& after) { auto& roll = ui->getPianoRollForSnapshot(); line (" keys at: " + describe (juce::Component::getCurrentlyFocusedComponent()) + ", panel focus " + juce::String ((int) ui->workspaceForTest().getFocused())); press ('A', juce::ModifierKeys::commandModifier, 'a'); check ("Ctrl+A selects the notes (" + after + ")", roll.selectionForTests().size() == 3, juce::String ((int) roll.selectionForTests().size())); struct Step { const char* name; int code, mods, delta; }; for (const auto& s : { Step { "Shift+Up", juce::KeyPress::upKey, juce::ModifierKeys::shiftModifier, 1 }, Step { "Shift+Down", juce::KeyPress::downKey, juce::ModifierKeys::shiftModifier, -1 }, Step { "Ctrl+Up", juce::KeyPress::upKey, juce::ModifierKeys::commandModifier, 12 }, Step { "Ctrl+Down", juce::KeyPress::downKey, juce::ModifierKeys::commandModifier, -12 } }) { const auto before = engine->getSong().patterns[(std::size_t) engine->getSong().currentPattern].notes.front().key; const auto text = keysNow(); press (s.code, s.mods); const auto now = engine->getSong().patterns[(std::size_t) engine->getSong().currentPattern].notes.front().key; check (juce::String (s.name) + " moves the selected notes (" + after + ")", now == before + s.delta, text + " -> " + keysNow()); } } void timerCallback() override { auto& roll = ui->getPianoRollForSnapshot(); switch (stepIndex++) { case 0: { ui->setLayoutStyle (lynx::layoutstyle::Style::pattern); const auto channel = engine->getSelectedChannel(); engine->editSong ([channel] (lynx::Song& s) { auto& pat = s.patterns[(std::size_t) s.currentPattern]; pat.notes.clear(); for (int i = 0; i < 3; ++i) { lynx::Note n; n.channel = channel; n.key = 60 + 2 * i; n.start = i * lynx::ppq; n.length = lynx::ppq / 2; pat.notes.push_back (n); } }); ui->setTypingPianoForTest (false); ui->grabKeyboardFocus(); break; } case 1: if (! ui->workspaceForTest().isPanelVisible (lynx::workspace::Panel::pianoRoll)) ui->runActionForSnapshot ("panel.pianoRoll"); break; case 2: { check ("the Piano roll shows", roll.isShowing()); const auto grid = roll.gridAreaForSnapshot(); click (roll, { grid.getRight() - 30, grid.getBottom() - 30 }); // (an empty spot: no note made, Draw tool) break; } case 3: engine->editSong ([] (lynx::Song& s) { // (the click may have drawn a note: the three are kept) auto& notes = s.patterns[(std::size_t) s.currentPattern].notes; if (notes.size() > 3) notes.resize (3); }); // the control: a plain Space, as --simplekeytest presses it (playback starts) press (juce::KeyPress::spaceKey, 0, ' '); check ("control: Space plays", engine->getTransport().playing.load()); engine->stop(); transposeCase ("after a click in the Piano roll"); break; case 4: { // the Browser's search box clicked, then the Piano roll again juce::TextEditor* field = nullptr; std::function look = [&] (juce::Component& c) { if (auto* t = dynamic_cast (&c); t != nullptr && t->isShowing() && field == nullptr) field = t; for (auto* child : c.getChildren()) look (*child); }; look (ui->getBrowserForSnapshot()); line (" Browser search box found=" + juce::String ((int) (field != nullptr))); if (field != nullptr) click (*field, field->getLocalBounds().getCentre()); break; } case 5: { const auto grid = roll.gridAreaForSnapshot(); click (roll, { grid.getRight() - 30, grid.getBottom() - 30 }); break; } case 6: engine->editSong ([] (lynx::Song& s) { auto& notes = s.patterns[(std::size_t) s.currentPattern].notes; if (notes.size() > 3) notes.resize (3); }); transposeCase ("after a click in the Browser's search box, then the Piano roll"); break; case 7: { // Windows: a real click on the Piano roll's native child window may give it the system's focus #if JUCE_WINDOWS if (auto* frame = roll.getPeer(); frame != nullptr && frame != main->getPeer()) { lynx::nativechild::focusWindowForTest (frame->getNativeHandle()); engine->editSong ([] (lynx::Song& s) { auto& notes = s.patterns[(std::size_t) s.currentPattern].notes; if (notes.size() > 3) notes.resize (3); }); transposeCase ("with the system's focus in the Piano roll's own native window"); } #endif break; } case 8: { // Ctrl+T after a click in each panel using Panel = lynx::workspace::Panel; for (const auto p : { Panel::channelRack, Panel::playlist, Panel::pianoRoll }) { juce::Component* c = p == Panel::pianoRoll ? (juce::Component*) &roll : p == Panel::playlist ? (juce::Component*) &ui->getPlaylistForSnapshot() : nullptr; if (p == Panel::channelRack) ui->workspaceForTest().activate (p); else if (c != nullptr && c->isShowing()) click (*c, { c->getWidth() - 40, c->getHeight() - 40 }); const auto before = ui->typingPianoForCheck(); const auto markers = engine->getSong().markers().size(); press ('T', juce::ModifierKeys::commandModifier, 't'); line (" Ctrl+T, panel " + juce::String ((int) p) + " focused (" + juce::String ((int) ui->workspaceForTest().getFocused()) + "): typing keyboard " + juce::String ((int) before) + " -> " + juce::String ((int) ui->typingPianoForCheck()) + ", song markers " + juce::String ((int) markers) + " -> " + juce::String ((int) engine->getSong().markers().size())); if (ui->typingPianoForCheck() != before) ui->setTypingPianoForTest (before); } finish(); break; } default: break; } } void finish() { line (ok ? "result=OK" : "result=FAILED"); stopTimer(); done(); } }; auto* test = new RouteTest(); test->reportPath = reportPath; startFocusTestWindow (*test); #if JUCE_WINDOWS test->main->setAlwaysOnTop (true); #endif test->startTimer (700); }