Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoder support for KeyboardModel and some classes #734

Merged
merged 2 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/models/KeyModel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import KeyModel from './KeyModel';

describe('KeyModel', () => {
test('isEncoder', () => {
let subject = new KeyModel(null, '0,1', 0, 0, '', 0, 0, 0, 0);
expect(subject.isEncoder).toBeTruthy();
subject = new KeyModel(null, '0,1', 0, 0, '', 0, 0, 0, null);
expect(subject.isEncoder).toBeFalsy();
});

test('encoderId', () => {
let subject = new KeyModel(null, '0,1', 0, 0, '', 0, 0, 0, 2);
expect(subject.encoderId).toEqual(2);
subject = new KeyModel(null, '0,1', 0, 0, '', 0, 0, 0, null);
expect(subject.encoderId).toBeNull();
});
});
9 changes: 8 additions & 1 deletion src/models/KeyModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class KeyModel {
readonly h: number;
readonly w2: number;
readonly h2: number;
readonly encoderId: number | null;

constructor(
op: KeyOp | null,
Expand All @@ -43,13 +44,15 @@ export default class KeyModel {
c: string,
r: number = 0,
rx: number = 0,
ry: number = 0
ry: number = 0,
encoderId: number | null = null
) {
this.keyOp = op;
this.x = x;
this.y = y;
this.rx = rx;
this.ry = ry;
this.encoderId = encoderId;
this.location = location;
const locs = location.split('\n');
this.pos = locs[0]; // 0 < locs[0].length ? locs[0] : locs[3];
Expand Down Expand Up @@ -142,6 +145,10 @@ export default class KeyModel {
return this.isOddly && this.top != this.top2;
}

get isEncoder(): boolean {
return this.encoderId !== null;
}

get style(): CSSProperties {
return {
width: this.width,
Expand Down
24 changes: 24 additions & 0 deletions src/models/KeyboardModel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Current, KeymapItem } from './KeyboardModel';

describe('KeymapItem', () => {
test('encoder exists', () => {
const current = new Current();
const subject = new KeymapItem(current, '0,0\n\n\n\n\n\n\n\n\ne0');
expect(subject.encoderId).toEqual(0);
});

test('encoder exists', () => {
const current = new Current();
const subject = new KeymapItem(
current,
'0,0\n0,6\n0,2\n0,8\n0.9\n1,1\n0,3\n0,5\n0,1\ne1\n0,7\n1,0'
);
expect(subject.encoderId).toEqual(1);
});

test('encoder not exists', () => {
const current = new Current();
const subject = new KeymapItem(current, '0,0');
expect(subject.encoderId).toBeNull();
});
});
28 changes: 25 additions & 3 deletions src/models/KeyboardModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type KeyboardViewContent = {
top: number;
};

class Current {
export class Current {
x = 0;
y = -1;
c = '#cccccc';
Expand Down Expand Up @@ -81,14 +81,15 @@ class Current {
}
}

class KeymapItem {
export class KeymapItem {
private _curr: Current;
readonly op: KeyOp;
readonly label: string;
private pos: string;
readonly option: string;
readonly choice: string;
private _toBeDelete: boolean;
private readonly _encoderId: number | null;

constructor(curr: Current, label: string, op: KeyOp | null = null) {
this._curr = new Current(curr);
Expand All @@ -101,6 +102,13 @@ class KeymapItem {
this.option = options[0];
this.choice = options[1];
this._toBeDelete = false;
// Check whether this key is an encoder or not
const positions = label.split('\n');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might integrate this code to line#100 which is the splitter of contents in a label.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamrocker I refactored the logic, and I removed the pos instance variable because it is not used. Could you review them again? The diff is 8e714d7.

if (10 <= positions.length && positions[9].match(/^e[0-9]+$/i)) {
this._encoderId = Number(positions[9].substring(1));
} else {
this._encoderId = null;
}
}

get isDefault(): boolean {
Expand Down Expand Up @@ -169,6 +177,10 @@ class KeymapItem {
relocate(curr: Current) {
this._curr = curr;
}

get encoderId(): number | null {
return this._encoderId;
}
}

export default class KeyboardModel {
Expand Down Expand Up @@ -353,7 +365,17 @@ export default class KeyboardModel {
keymapsList.flat().forEach((item: KeymapItem) => {
if (item.toBeDeleted) return;

let model = new KeyModel(item.op, item.label, item.x, item.y, item.c, item.r, item.rx, item.ry); // prettier-ignore
const model = new KeyModel(
item.op,
item.label,
item.x,
item.y,
item.c,
item.r,
item.rx,
item.ry,
item.encoderId
);
list.push(model);
});
return list;
Expand Down