fix(user-config): Layer switcher key behaviour on non-base layers (#440)
* refactor(user-config): Optimize imports * feat(user-config): Clone SwitchLayerAction to destination layer * fix(user-config): Fix Keymap SwitchLayerAction normalization * test(user-config): Remove spy callThrough * build: Add uhk-common test runner * build: delete test serialization files * fix(user-config): Add missing "type": "basic" properties to the user-config.json * test(user-config): Add KeyMacroAction tests * fix(user-config): Delete SwitchLayerAction from non destination layer * fix(user-config): Keymap normalize delete SwitchLayerActions from non base layers * ci: turn of uhk-web tests * ci: turn off karma watch mode in uhk-web test
This commit is contained in:
committed by
László Monda
parent
46b97a9b62
commit
7baf9ad009
@@ -1,8 +1,7 @@
|
||||
import { UhkBuffer } from '../uhk-buffer';
|
||||
import { Layer } from './layer';
|
||||
import { Macro } from './macro';
|
||||
import { SwitchLayerAction } from './key-action/switch-layer-action';
|
||||
import { KeyAction } from './key-action/key-action';
|
||||
import { KeyActionHelper, SwitchLayerAction } from './key-action';
|
||||
import { UserConfiguration } from './user-configuration';
|
||||
|
||||
export class Keymap {
|
||||
@@ -97,30 +96,64 @@ export class Keymap {
|
||||
}
|
||||
|
||||
private normalize() {
|
||||
// Removes all the SwitchLayerActions from any non base layer
|
||||
for (let i = 1; i < this.layers.length; ++i) {
|
||||
for (const module of this.layers[i].modules) {
|
||||
module.keyActions = module.keyActions.map(keyAction => {
|
||||
if (keyAction instanceof SwitchLayerAction) {
|
||||
return undefined;
|
||||
}
|
||||
return keyAction;
|
||||
});
|
||||
}
|
||||
if (this.layers.length < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Adds the SwitchLayerActions from the base layer to any none base layer
|
||||
const baseLayerModules = this.layers[0].modules;
|
||||
for (let i = 0; i < baseLayerModules.length; ++i) {
|
||||
baseLayerModules[i].keyActions.forEach((keyAction: KeyAction, keyActionIndex: number) => {
|
||||
if (keyAction instanceof SwitchLayerAction) {
|
||||
for (let j = 1; j < this.layers.length; ++j) {
|
||||
this.layers[j].modules[i].keyActions[keyActionIndex] = new SwitchLayerAction(keyAction);
|
||||
for (let moduleId = 0; moduleId < this.layers[0].modules.length && moduleId < 2; moduleId++) {
|
||||
const baseModule = this.layers[0].modules[moduleId];
|
||||
for (let keyActionId = 0; keyActionId < baseModule.keyActions.length; keyActionId++) {
|
||||
const baseKeyAction = baseModule.keyActions[keyActionId];
|
||||
|
||||
if (baseKeyAction instanceof SwitchLayerAction) {
|
||||
const destinationLayerId = baseKeyAction.layer + 1;
|
||||
if (this.layers.length < destinationLayerId) {
|
||||
// TODO: What should we do???
|
||||
console.error(`${this.name} has not enough layer. Need: ${destinationLayerId}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (let currentLayerId = 1; currentLayerId < this.layers.length; currentLayerId++) {
|
||||
const currentLayer = this.layers[currentLayerId];
|
||||
if (currentLayer.modules.length < moduleId) {
|
||||
// TODO: What should we do???
|
||||
console.error(`${this.name}.layers[${currentLayerId}] has not enough module. Need: ${moduleId}`);
|
||||
continue;
|
||||
}
|
||||
const currentModule = currentLayer.modules[moduleId];
|
||||
const currentKeyAction = currentModule.keyActions[keyActionId];
|
||||
|
||||
if (baseKeyAction instanceof SwitchLayerAction) {
|
||||
if (currentLayerId - 1 === baseKeyAction.layer) {
|
||||
if (currentKeyAction instanceof SwitchLayerAction) {
|
||||
if (currentKeyAction.layer === baseKeyAction.layer &&
|
||||
currentKeyAction.isLayerToggleable === baseKeyAction.isLayerToggleable) {
|
||||
continue;
|
||||
}
|
||||
// tslint:disable-next-line: max-line-length
|
||||
const error = `${this.name}.layers[${currentLayerId}]modules[${moduleId}].keyActions[${keyActionId}]` +
|
||||
` is different switch layer. ${currentKeyAction} will be override with ${baseKeyAction}`;
|
||||
console.warn(error);
|
||||
} else {
|
||||
// tslint:disable-next-line: max-line-length
|
||||
const error = `${this.name}.layers[${currentLayerId}]modules[${moduleId}].keyActions[${keyActionId}]` +
|
||||
` is not switch layer. ${currentKeyAction} will be override with ${baseKeyAction}`;
|
||||
console.warn(error);
|
||||
}
|
||||
currentModule.keyActions[keyActionId] = KeyActionHelper.createKeyAction(baseKeyAction);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (currentKeyAction instanceof SwitchLayerAction) {
|
||||
// tslint:disable-next-line: max-line-length
|
||||
const error = `${this.name}.layers[${currentLayerId}]modules[${moduleId}].keyActions[${keyActionId}]` +
|
||||
` is switch layer action, but the base key action is not switch layer action, so will delete`;
|
||||
console.warn(error);
|
||||
currentModule.keyActions[keyActionId] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { KeyMacroAction } from './key-macro-action';
|
||||
import { binaryDefaultHelper, jsonDefaultHelper } from '../../../../test/serializer-test-helper';
|
||||
import { MacroSubAction } from './macro-action';
|
||||
import { KeystrokeType } from '../key-action';
|
||||
|
||||
describe('key-macro-action', () => {
|
||||
it('should be instantiate', () => {
|
||||
const action = new KeyMacroAction();
|
||||
expect(action).toBeTruthy();
|
||||
});
|
||||
|
||||
describe('full serialization', () => {
|
||||
it('should json match', () => {
|
||||
const action = new KeyMacroAction();
|
||||
action.action = MacroSubAction.hold;
|
||||
action.type = KeystrokeType.basic;
|
||||
action.scancode = 100;
|
||||
jsonDefaultHelper(action);
|
||||
});
|
||||
|
||||
it('should binary match', () => {
|
||||
const action = new KeyMacroAction();
|
||||
action.action = MacroSubAction.hold;
|
||||
action.type = KeystrokeType.basic;
|
||||
action.scancode = 100;
|
||||
binaryDefaultHelper(action);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,257 @@
|
||||
import { UserConfiguration } from '../user-configuration';
|
||||
|
||||
describe('keymap', () => {
|
||||
it('should normalize SwitchLayerAction if non base layer action is not SwitchLayerAction', () => {
|
||||
const inputJsonConfig = {
|
||||
dataModelVersion: 1,
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
{
|
||||
isDefault: true,
|
||||
abbreviation: 'QWR',
|
||||
name: 'QWERTY',
|
||||
description: '',
|
||||
layers: [
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
{
|
||||
keyActionType: 'switchLayer',
|
||||
layer: 'mod',
|
||||
toggle: false
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
{
|
||||
keyActionType: 'keystroke',
|
||||
type: 'basic',
|
||||
scancode: 44
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
null
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
null
|
||||
]
|
||||
}]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
const expectedJsonConfig = {
|
||||
dataModelVersion: 1,
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
{
|
||||
isDefault: true,
|
||||
abbreviation: 'QWR',
|
||||
name: 'QWERTY',
|
||||
description: '',
|
||||
layers: [
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
{
|
||||
keyActionType: 'switchLayer',
|
||||
layer: 'mod',
|
||||
toggle: false
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
{
|
||||
keyActionType: 'switchLayer',
|
||||
layer: 'mod',
|
||||
toggle: false
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
null
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
null
|
||||
]
|
||||
}]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
spyOn(console, 'warn');
|
||||
const inputUserConfig = new UserConfiguration().fromJsonObject(inputJsonConfig);
|
||||
|
||||
expect(inputUserConfig.toJsonObject()).toEqual(expectedJsonConfig);
|
||||
// tslint:disable-next-line: max-line-length
|
||||
expect(console.warn).toHaveBeenCalledWith('QWERTY.layers[1]modules[0].keyActions[0] is not switch layer. <KeystrokeAction type="basic" scancode="44"> will be override with <SwitchLayerAction layer="0" toggle="false">');
|
||||
});
|
||||
|
||||
it('should normalize SwitchLayerAction if non base layer action is other SwitchLayerAction', () => {
|
||||
const inputJsonConfig = {
|
||||
dataModelVersion: 1,
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
{
|
||||
isDefault: true,
|
||||
abbreviation: 'QWR',
|
||||
name: 'QWERTY',
|
||||
description: '',
|
||||
layers: [
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
{
|
||||
keyActionType: 'switchLayer',
|
||||
layer: 'mod',
|
||||
toggle: false
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
{
|
||||
keyActionType: 'switchLayer',
|
||||
layer: 'fn',
|
||||
toggle: false
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
null
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
null
|
||||
]
|
||||
}]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
const expectedJsonConfig = {
|
||||
dataModelVersion: 1,
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
{
|
||||
isDefault: true,
|
||||
abbreviation: 'QWR',
|
||||
name: 'QWERTY',
|
||||
description: '',
|
||||
layers: [
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
{
|
||||
keyActionType: 'switchLayer',
|
||||
layer: 'mod',
|
||||
toggle: false
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
{
|
||||
keyActionType: 'switchLayer',
|
||||
layer: 'mod',
|
||||
toggle: false
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
null
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
modules: [{
|
||||
id: 0,
|
||||
pointerRole: 'move',
|
||||
keyActions: [
|
||||
null
|
||||
]
|
||||
}]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
spyOn(console, 'warn');
|
||||
const inputUserConfig = new UserConfiguration().fromJsonObject(inputJsonConfig);
|
||||
|
||||
expect(inputUserConfig.toJsonObject()).toEqual(expectedJsonConfig);
|
||||
// tslint:disable-next-line: max-line-length
|
||||
expect(console.warn).toHaveBeenCalledWith('QWERTY.layers[1]modules[0].keyActions[0] is different switch layer. <SwitchLayerAction layer="1" toggle="false"> will be override with <SwitchLayerAction layer="0" toggle="false">');
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user