From c88062a2432ec49694522cf37f8574486c13c4e8 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 13 Aug 2017 11:24:25 +0200 Subject: [PATCH] layers: Code refactoring Refactor the momentary layer handling part of `handleKeymapKeyswitchEvent`. Instead of a bunch of ifs that are increasingly hard to follow, use a switch based on the target layer, and branch out depending on `keyState` from there. Makes it easier to follow what happens. Signed-off-by: Gergely Nagy --- src/layers.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/layers.cpp b/src/layers.cpp index 0e046eab..e453b51a 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -12,32 +12,32 @@ static void handleKeymapKeyswitchEvent(Key keymapEntry, uint8_t keyState) { if (keymapEntry.keyCode >= MOMENTARY_OFFSET) { uint8_t target = keymapEntry.keyCode - MOMENTARY_OFFSET; - if (keyToggledOn(keyState)) { - if (target == KEYMAP_NEXT) { + switch (target) { + case KEYMAP_NEXT: + if (keyToggledOn(keyState)) Layer.next(); - } else if (target == KEYMAP_PREVIOUS) { + else if (keyToggledOff(keyState)) Layer.previous(); - } else { - Layer.on(target); - } - } else if (keyIsPressed(keyState) && - target != KEYMAP_NEXT && - target != KEYMAP_PREVIOUS) { - if (!Layer.isOn(target)) - Layer.on(target); - } - if (keyToggledOff(keyState)) { - if (target == KEYMAP_NEXT) { + break; + + case KEYMAP_PREVIOUS: + if (keyToggledOn(keyState)) Layer.previous(); - } else if (target == KEYMAP_PREVIOUS) { + else if (keyToggledOff(keyState)) Layer.next(); - } else { + break; + + default: + if (keyIsPressed(keyState)) { + if (!Layer.isOn(target)) + Layer.on(target); + } else if (keyToggledOff(keyState)) { Layer.off(target); } + break; } - - // switch keymap and stay there } else if (keyToggledOn(keyState)) { + // switch keymap and stay there if (Layer.isOn(keymapEntry.keyCode) && keymapEntry.keyCode) Layer.off(keymapEntry.keyCode); else