Merge pull request #61 from UltimateHackingKeyboard/fjozsef-redundant

Redundant KeystrokeAction behavior refactoring
This commit is contained in:
László Monda
2016-06-21 19:52:54 +02:00
committed by GitHub
16 changed files with 213 additions and 263 deletions

View File

@@ -1,58 +0,0 @@
import {UhkBuffer} from '../UhkBuffer';
import {keyActionType, KeyActionId, KeyAction} from './KeyAction';
import {assertUInt8, assertEnum} from '../assert';
enum LongPressAction {
leftCtrl,
leftShift,
leftAlt,
leftSuper,
rightCtrl,
rightShift,
rightAlt,
rightSuper,
mod,
fn,
mouse
}
export class DualRoleKeystrokeAction extends KeyAction {
@assertUInt8
scancode: number;
@assertEnum(LongPressAction)
private longPressAction: LongPressAction;
_fromJsObject(jsObject: any): DualRoleKeystrokeAction {
this.assertKeyActionType(jsObject);
this.scancode = jsObject.scancode;
this.longPressAction = LongPressAction[<string> jsObject.longPressAction];
return this;
}
_fromBinary(buffer: UhkBuffer): DualRoleKeystrokeAction {
this.readAndAssertKeyActionId(buffer);
this.scancode = buffer.readUInt8();
this.longPressAction = buffer.readUInt8();
return this;
}
_toJsObject(): any {
return {
keyActionType: keyActionType.DualRoleKeystrokeAction,
scancode: this.scancode,
longPressAction: LongPressAction[this.longPressAction]
};
}
_toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(KeyActionId.DualRoleKeystrokeAction);
buffer.writeUInt8(this.scancode);
buffer.writeUInt8(this.longPressAction);
}
toString(): string {
return `<DualRoleKeystrokeAction scancode="${this.scancode}" longPressAction="${this.longPressAction}">`;
}
}

View File

@@ -1,5 +1,5 @@
import {KeyModifiers} from './KeystrokeModifiersAction';
import {MacroAction, MacroActionId, macroActionType} from './MacroAction';
import {KeyModifiers} from './KeyModifiers';
import {UhkBuffer} from '../UhkBuffer';
import {assertUInt8} from '../assert';

View File

@@ -6,21 +6,23 @@ import {UhkBuffer} from '../UhkBuffer';
export enum KeyActionId {
NoneAction = 0,
KeystrokeAction = 1,
KeystrokeModifiersAction = 2,
KeystrokeWithModifiersAction = 3,
DualRoleKeystrokeAction = 4,
SwitchLayerAction = 5,
SwitchKeymapAction = 6,
MouseAction = 7,
PlayMacroAction = 8
/*
1 - 7 are reserved for KeystrokeAction
3 bits:
1: Do we have scancode?
2: Do we have modifiers?
3: Do we have longpress?
*/
LastKeystrokeAction = 7, // TODO: remove this after refactoring the keyActionId check
SwitchLayerAction = 8,
SwitchKeymapAction = 9,
MouseAction = 10,
PlayMacroAction = 11
}
export let keyActionType = {
NoneAction : 'none',
KeystrokeAction : 'keystroke',
KeystrokeModifiersAction : 'keystrokeModifiers',
KeystrokeWithModifiersAction : 'keystrokeWithModifiers',
DualRoleKeystrokeAction : 'dualRoleKeystroke',
SwitchLayerAction : 'switchLayer',
SwitchKeymapAction : 'switchKeymap',
MouseAction : 'mouse',
@@ -28,21 +30,27 @@ export let keyActionType = {
};
export abstract class KeyAction extends Serializable<KeyAction> {
assertKeyActionType(jsObject: any) {
let keyActionClassname = this.constructor.name;
let keyActionTypeString = keyActionType[keyActionClassname];
assertKeyActionType(jsObject: any): void {
let keyActionClassname: string = this.constructor.name;
let keyActionTypeString: string = keyActionType[keyActionClassname];
if (jsObject.keyActionType !== keyActionTypeString) {
throw `Invalid ${keyActionClassname}.keyActionType: ${jsObject.keyActionType}`;
}
}
readAndAssertKeyActionId(buffer: UhkBuffer) {
let classname = this.constructor.name;
let readKeyActionId = buffer.readUInt8();
let keyActionId = KeyActionId[<string> classname];
if (readKeyActionId !== keyActionId) {
readAndAssertKeyActionId(buffer: UhkBuffer): KeyActionId {
let classname: string = this.constructor.name;
let readKeyActionId: number = buffer.readUInt8();
let keyActionId: number = KeyActionId[classname];
if (keyActionId === KeyActionId.KeystrokeAction) {
if (readKeyActionId < KeyActionId.KeystrokeAction || readKeyActionId > KeyActionId.LastKeystrokeAction) {
throw `Invalid ${classname} first byte: ${readKeyActionId}`;
}
} else if (readKeyActionId !== keyActionId) {
throw `Invalid ${classname} first byte: ${readKeyActionId}`;
}
return readKeyActionId;
}
abstract _fromJsObject(jsObject: any): KeyAction;

View File

@@ -1,11 +1,8 @@
import {ClassArray} from '../ClassArray';
import {UhkBuffer} from '../UhkBuffer';
import {DualRoleKeystrokeAction} from './DualRoleKeystrokeAction';
import {NoneAction} from './NoneAction';
import {KeystrokeAction} from './KeystrokeAction';
import {keyActionType, KeyActionId, KeyAction} from './KeyAction';
import {KeystrokeModifiersAction} from './KeystrokeModifiersAction';
import {KeystrokeWithModifiersAction} from './KeystrokeWithModifiersAction';
import {SwitchLayerAction} from './SwitchLayerAction';
import {SwitchKeymapAction} from './SwitchKeymapAction';
import {MouseAction} from './MouseAction';
@@ -19,12 +16,6 @@ export class KeyActions extends ClassArray<KeyAction> {
return new NoneAction().fromJsObject(jsObject);
case keyActionType.KeystrokeAction:
return new KeystrokeAction().fromJsObject(jsObject);
case keyActionType.KeystrokeModifiersAction:
return new KeystrokeModifiersAction().fromJsObject(jsObject);
case keyActionType.KeystrokeWithModifiersAction:
return new KeystrokeWithModifiersAction().fromJsObject(jsObject);
case keyActionType.DualRoleKeystrokeAction:
return new DualRoleKeystrokeAction().fromJsObject(jsObject);
case keyActionType.SwitchLayerAction:
return new SwitchLayerAction().fromJsObject(jsObject);
case keyActionType.SwitchKeymapAction:
@@ -47,17 +38,13 @@ export class KeyActions extends ClassArray<KeyAction> {
buffer.enableDump = false;
}
if (keyActionFirstByte >= KeyActionId.KeystrokeAction && keyActionFirstByte < KeyActionId.LastKeystrokeAction) {
return new KeystrokeAction().fromBinary(buffer);
}
switch (keyActionFirstByte) {
case KeyActionId.NoneAction:
return new NoneAction().fromBinary(buffer);
case KeyActionId.KeystrokeAction:
return new KeystrokeAction().fromBinary(buffer);
case KeyActionId.KeystrokeModifiersAction:
return new KeystrokeModifiersAction().fromBinary(buffer);
case KeyActionId.KeystrokeWithModifiersAction:
return new KeystrokeWithModifiersAction().fromBinary(buffer);
case KeyActionId.DualRoleKeystrokeAction:
return new DualRoleKeystrokeAction().fromBinary(buffer);
case KeyActionId.SwitchLayerAction:
return new SwitchLayerAction().fromBinary(buffer);
case KeyActionId.SwitchKeymapAction:

View File

@@ -0,0 +1,10 @@
export enum KeyModifiers {
leftCtrl = 1 << 0,
leftShift = 1 << 1,
leftAlt = 1 << 2,
leftGui = 1 << 3,
rightCtrl = 1 << 4,
rightShift = 1 << 5,
rightAlt = 1 << 6,
rightGui = 1 << 7
}

View File

@@ -1,37 +1,133 @@
import {UhkBuffer} from '../UhkBuffer';
import {keyActionType, KeyActionId, KeyAction} from './KeyAction';
import {assertUInt8} from '../assert';
import {KeyModifiers} from './KeyModifiers';
import {assertUInt8, assertEnum} from '../assert';
import {LongPressAction} from './LongPressAction';
export enum KeystrokeActionFlag {
scancode = 1 << 0,
modifierMask = 1 << 1,
longPressAction = 1 << 2,
}
interface JsObjectKeystrokeAction {
keyActionType: string;
scancode?: number;
modifierMask?: number;
longPressAction?: string;
}
export class KeystrokeAction extends KeyAction {
@assertUInt8
scancode: number;
_fromJsObject(jsObject: any): KeystrokeAction {
@assertUInt8
modifierMask: number;
@assertEnum(LongPressAction)
longPressAction: LongPressAction;
_fromJsObject(jsObject: JsObjectKeystrokeAction): KeystrokeAction {
this.assertKeyActionType(jsObject);
this.scancode = jsObject.scancode;
this.modifierMask = jsObject.modifierMask;
this.longPressAction = LongPressAction[jsObject.longPressAction];
return this;
}
_fromBinary(buffer: UhkBuffer): KeystrokeAction {
this.readAndAssertKeyActionId(buffer);
this.scancode = buffer.readUInt8();
let keyActionId: KeyActionId = this.readAndAssertKeyActionId(buffer);
let flags: number = keyActionId - KeyActionId.KeystrokeAction;
if (flags & KeystrokeActionFlag.scancode) {
this.scancode = buffer.readUInt8();
}
if (flags & KeystrokeActionFlag.modifierMask) {
this.modifierMask = buffer.readUInt8();
}
if (flags & KeystrokeActionFlag.longPressAction) {
this.longPressAction = buffer.readUInt8();
}
return this;
}
_toJsObject(): any {
return {
keyActionType: keyActionType.KeystrokeAction,
scancode: this.scancode
_toJsObject(): JsObjectKeystrokeAction {
let jsObject: JsObjectKeystrokeAction = {
keyActionType: keyActionType.KeystrokeAction
};
if (this.hasScancode()) {
jsObject.scancode = this.scancode;
}
if (this.hasActiveModifier()) {
jsObject.modifierMask = this.modifierMask;
}
if (this.hasLongPressAction()) {
jsObject.longPressAction = LongPressAction[this.longPressAction];
}
return jsObject;
}
_toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.KeystrokeAction);
buffer.writeUInt8(this.scancode);
let flags = 0;
let bufferData: number[] = [];
if (this.hasScancode()) {
flags |= KeystrokeActionFlag.scancode;
bufferData.push(this.scancode);
}
if (this.hasActiveModifier()) {
flags |= KeystrokeActionFlag.modifierMask;
bufferData.push(this.modifierMask);
}
if (this.hasLongPressAction()) {
flags |= KeystrokeActionFlag.longPressAction;
bufferData.push(this.longPressAction);
}
buffer.writeUInt8(KeyActionId.KeystrokeAction + flags);
for (let i = 0; i < bufferData.length; ++i) {
buffer.writeUInt8(bufferData[i]);
}
}
toString(): string {
return `<KeystrokeAction scancode="${this.scancode}">`;
let properties: string[] = [];
if (this.hasScancode()) {
properties.push(`scancode="${this.scancode}"`);
}
if (this.hasActiveModifier()) {
properties.push(`modifierMask="${this.modifierMask}"`);
}
if (this.hasLongPressAction()) {
properties.push(`longPressAction="${this.longPressAction}"`);
}
return `<KeystrokeAction ${properties.join(' ')}>`;
}
isActive(modifier: KeyModifiers): boolean {
return (this.modifierMask & modifier) > 0;
}
hasActiveModifier(): boolean {
return this.modifierMask > 0;
}
hasLongPressAction(): boolean {
return this.longPressAction !== undefined;
}
hasScancode(): boolean {
return !!this.scancode;
}
hasOnlyOneActiveModifier(): boolean {
return this.modifierMask !== 0 && !(this.modifierMask & this.modifierMask - 1);
}
}

View File

@@ -1,56 +0,0 @@
import {UhkBuffer} from '../UhkBuffer';
import {keyActionType, KeyActionId, KeyAction} from './KeyAction';
import {assertUInt8} from '../assert';
export enum KeyModifiers {
leftCtrl = 1 << 0,
leftShift = 1 << 1,
leftAlt = 1 << 2,
leftGui = 1 << 3,
rightCtrl = 1 << 4,
rightShift = 1 << 5,
rightAlt = 1 << 6,
rightGui = 1 << 7
}
export class KeystrokeModifiersAction extends KeyAction {
@assertUInt8
modifierMask: number;
_fromJsObject(jsObject: any): KeystrokeModifiersAction {
this.assertKeyActionType(jsObject);
this.modifierMask = jsObject.modifierMask;
return this;
}
_fromBinary(buffer: UhkBuffer): KeystrokeModifiersAction {
this.readAndAssertKeyActionId(buffer);
this.modifierMask = buffer.readUInt8();
return this;
}
_toJsObject(): any {
return {
keyActionType: keyActionType.KeystrokeModifiersAction,
modifierMask: this.modifierMask
};
}
_toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.KeystrokeModifiersAction);
buffer.writeUInt8(this.modifierMask);
}
toString(): string {
return `<KeystrokeModifiersAction modifierMask="${this.modifierMask}">`;
}
isModifierActive(modifier: KeyModifiers): boolean {
return (this.modifierMask & modifier) > 0;
}
isOnlyOneModifierActive(): boolean {
return this.modifierMask !== 0 && !(this.modifierMask & this.modifierMask - 1);
}
}

View File

@@ -1,49 +0,0 @@
import {UhkBuffer} from '../UhkBuffer';
import {keyActionType, KeyActionId, KeyAction} from './KeyAction';
import {KeyModifiers} from './KeystrokeModifiersAction';
import {assertUInt8} from '../assert';
export class KeystrokeWithModifiersAction extends KeyAction {
@assertUInt8
modifierMask: number;
@assertUInt8
scancode: number;
_fromJsObject(jsObject: any): KeystrokeWithModifiersAction {
this.assertKeyActionType(jsObject);
this.scancode = jsObject.scancode;
this.modifierMask = jsObject.modifierMask;
return this;
}
_fromBinary(buffer: UhkBuffer): KeystrokeWithModifiersAction {
this.readAndAssertKeyActionId(buffer);
this.scancode = buffer.readUInt8();
this.modifierMask = buffer.readUInt8();
return this;
}
_toJsObject(): any {
return {
keyActionType: keyActionType.KeystrokeWithModifiersAction,
scancode: this.scancode,
modifierMask: this.modifierMask
};
}
_toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.KeystrokeWithModifiersAction);
buffer.writeUInt8(this.scancode);
buffer.writeUInt8(this.modifierMask);
}
toString(): string {
return `<KeystrokeWithModifiersAction scancode="${this.scancode}" modifierMask="${this.modifierMask}">`;
}
isModifierActive(modifier: KeyModifiers): boolean {
return (this.modifierMask & modifier) > 0;
}
}

View File

@@ -0,0 +1,13 @@
export enum LongPressAction {
leftCtrl,
leftShift,
leftAlt,
leftSuper,
rightCtrl,
rightShift,
rightAlt,
rightSuper,
mod,
fn,
mouse
};

View File

@@ -1,5 +1,5 @@
import {KeyModifiers} from './KeystrokeModifiersAction';
import {MacroAction, MacroActionId, macroActionType} from './MacroAction';
import {KeyModifiers} from './KeyModifiers';
import {UhkBuffer} from '../UhkBuffer';
import {assertUInt8} from '../assert';

View File

@@ -1,5 +1,5 @@
import {KeyModifiers} from './KeystrokeModifiersAction';
import {MacroAction, MacroActionId, macroActionType} from './MacroAction';
import {KeyModifiers} from './KeyModifiers';
import {UhkBuffer} from '../UhkBuffer';
import {assertUInt8} from '../assert';

View File

@@ -41,7 +41,6 @@
"enum": [
"none",
"keystroke",
"keystrokeModifiers",
"switchLayer",
"switchKeymap",
"playMacro",

View File

@@ -134,7 +134,7 @@
"scancode": 56
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 32
},
{
@@ -147,15 +147,15 @@
"toggle": false
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 64
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 128
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 16
},
{
@@ -247,7 +247,7 @@
"scancode": 10
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 2
},
{
@@ -271,15 +271,15 @@
"scancode": 5
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 1
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 8
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 4
},
{
@@ -403,7 +403,7 @@
"scancode": 118
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 32
},
{
@@ -415,15 +415,15 @@
"toggle": false
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 64
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 128
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 16
},
{
@@ -497,7 +497,7 @@
"keyActionType": "none"
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 2
},
{
@@ -516,15 +516,15 @@
"keyActionType": "none"
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 1
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 8
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 4
},
{
@@ -641,7 +641,7 @@
"keyActionType": "none"
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 32
},
{
@@ -653,15 +653,15 @@
"toggle": false
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 64
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 128
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 16
},
{
@@ -739,7 +739,7 @@
"keyActionType": "none"
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 2
},
{
@@ -758,15 +758,15 @@
"keyActionType": "none"
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 1
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 8
},
{
"keyActionType": "keystrokeModifiers",
"keyActionType": "keystroke",
"modifierMask": 4
},
{

View File

@@ -3,7 +3,7 @@ import {NgSwitch, NgSwitchWhen} from '@angular/common';
import {KeyAction} from '../../../config-serializer/config-items/KeyAction';
import {KeystrokeAction} from '../../../config-serializer/config-items/KeystrokeAction';
import {KeystrokeModifiersAction, KeyModifiers} from '../../../config-serializer/config-items/KeystrokeModifiersAction';
import {KeyModifiers} from '../../../config-serializer/config-items/KeyModifiers';
import {SwitchLayerAction, LayerName} from '../../../config-serializer/config-items/SwitchLayerAction';
import {MapperService} from '../../services/mapper.service';
import {SwitchKeymapAction} from '../../../config-serializer/config-items/SwitchKeymapAction';
@@ -115,10 +115,27 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges {
this.labelType = LabelTypes.OneLineText;
if (this.keyAction instanceof KeystrokeModifiersAction) {
let keyAction: KeystrokeModifiersAction = this.keyAction as KeystrokeModifiersAction;
let newLabelSource: string[] = [];
if (keyAction.isOnlyOneModifierActive()) {
if (this.keyAction instanceof KeystrokeAction) {
let keyAction: KeystrokeAction = this.keyAction as KeystrokeAction;
let newLabelSource: string[];
if (keyAction.hasScancode()) {
let scancode: number = keyAction.scancode;
newLabelSource = this.mapperService.scanCodeToText(scancode);
if (newLabelSource) {
if (newLabelSource.length === 1) {
this.labelSource = newLabelSource[0];
this.labelType = LabelTypes.OneLineText;
} else {
this.labelSource = newLabelSource;
this.labelType = LabelTypes.TwoLineText;
}
} else {
this.labelSource = this.mapperService.scanCodeToSvgImagePath(scancode);
this.labelType = LabelTypes.SingleIcon;
}
} else if (keyAction.hasOnlyOneActiveModifier()) {
newLabelSource = [];
switch (keyAction.modifierMask) {
case KeyModifiers.leftCtrl:
case KeyModifiers.rightCtrl:
@@ -140,22 +157,7 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges {
newLabelSource.push('Undefined');
break;
}
}
this.labelSource = newLabelSource;
} else if (this.keyAction instanceof KeystrokeAction) {
let scancode: number = (this.keyAction as KeystrokeAction).scancode;
let newLabelSource: string[] = this.mapperService.scanCodeToText(scancode);
if (newLabelSource) {
if (newLabelSource.length === 1) {
this.labelSource = newLabelSource[0];
this.labelType = LabelTypes.OneLineText;
} else {
this.labelSource = newLabelSource;
this.labelType = LabelTypes.TwoLineText;
}
} else {
this.labelSource = this.mapperService.scanCodeToSvgImagePath(scancode);
this.labelType = LabelTypes.SingleIcon;
this.labelSource = newLabelSource;
}
} else if (this.keyAction instanceof SwitchLayerAction) {
let keyAction: SwitchLayerAction = this.keyAction as SwitchLayerAction;

View File

@@ -4,8 +4,6 @@ import { CaptureKeystrokeButtonComponent } from '../widgets/capture-keystroke-bu
import { KeyAction } from '../../../../config-serializer/config-items/KeyAction';
import { KeystrokeAction } from '../../../../config-serializer/config-items/KeystrokeAction';
import { KeystrokeModifiersAction } from '../../../../config-serializer/config-items/KeystrokeModifiersAction';
import { KeystrokeWithModifiersAction } from '../../../../config-serializer/config-items/KeystrokeWithModifiersAction';
import { KeyActionSaver } from '../key-action-saver';
import {IconComponent} from '../widgets/icon.component';
@@ -66,8 +64,8 @@ export class KeypressTabComponent implements OnInit, KeyActionSaver {
ngOnInit() { }
getKeyAction(): KeystrokeAction | KeystrokeModifiersAction | KeystrokeWithModifiersAction {
return;
getKeyAction(): KeystrokeAction {
return undefined;
}
keyActionValid(): boolean {

View File

@@ -18,7 +18,7 @@ import {TextMacroAction} from '../../../../config-serializer/config-items/TextMa
import {IconComponent} from '../widgets/icon.component';
import {KeyModifiers} from '../../../../config-serializer/config-items/KeystrokeModifiersAction';
import {KeyModifiers} from '../../../../config-serializer/config-items/KeyModifiers';
@Component({
moduleId: module.id,