Skip to content

Commit

Permalink
chore(web): patches web tools for enforcement of no implicit any
Browse files Browse the repository at this point in the history
  • Loading branch information
jahorton committed May 16, 2024
1 parent fc3eb0d commit faa3751
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
3 changes: 1 addition & 2 deletions common/web/keyboard-processor/src/keyboards/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ export default class Keyboard {
}
}

// TODO: Better typing.
private get _legacyLayoutSpec(): any {
private get _legacyLayoutSpec() {
return this.scriptObject['KV']; // used with buildDefaultLayout; layout must be constructed at runtime.
}

Expand Down
18 changes: 10 additions & 8 deletions web/src/tools/testing/bulk_rendering/renderer_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class BatchRenderer {
private filterKeyboards(): KeyboardMap {
let kbds = keyman.getKeyboards();

let keyboardMap = {};
let keyboardMap: Record<string, ReturnType<typeof keyman['getKeyboards']>[0]> = {};

for(var i = 0; i < kbds.length; i++) {
let id: string = kbds[i].InternalName;
Expand Down Expand Up @@ -167,24 +167,24 @@ export class BatchRenderer {
divSummary.appendChild(divRenders);

// Uses 'private' APIs that may be subject to change in the future. Keep it updated!
var layers;
let layers: string[];
if(!isMobile) {
// The desktop OSK will be overpopulated, with a number of blank layers to display in most cases.
// We instead rely upon the KLS definition to ensure we keep the renders sparse.
//
// _legacyLayoutSpec is technically private, but it's what we've been using, so... yeah.
layers = keyman.core.activeKeyboard['_legacyLayoutSpec']?.KLS;
layers = Object.keys(keyman.core.activeKeyboard['_legacyLayoutSpec']?.KLS);
}

// If mobile, or if the desktop definition lacks a `_legacyLayoutSpec` entry.
// Note: vkbd will be null for keyboards with desktop help-text, such as sil_euro_latin.
layers = layers || keyman.osk.vkbd?.layerGroup.layers;
layers = layers || Object.keys(keyman.osk.vkbd?.layerGroup.layers);

let renderLayer = function(i: number) {
return new Promise(function(resolve) {
// (Private API) Directly sets the keyboard layer within KMW, then uses .show to force-display it.
if(keyman.osk.vkbd) {
keyman.core.keyboardProcessor.layerId = Object.keys(layers)[i];
keyman.core.keyboardProcessor.layerId = layers[i];
} else {
// Again, occurs for keyboards with desktop help-text.
console.error(`Error - keyman.osk.vkbd is undefined for ${kbd.InternalName}!`);
Expand Down Expand Up @@ -261,10 +261,11 @@ export class BatchRenderer {
keyman.setActiveElement(BatchRenderer.dummy, true);
}

async run(allLayers, filter) {
async run(allLayers: boolean, filter: string) {
BatchRenderer.allLayers = allLayers;
BatchRenderer.keyboardMatch = new RegExp('^Keyboard_('+filter+')', 'i');
if(window['keyman']) {

if(keyman) {
let cc = await this.startCapture();
BatchRenderer.captureStream = cc.captureStream;
BatchRenderer.video = cc.video;
Expand Down Expand Up @@ -293,7 +294,7 @@ export class BatchRenderer {

let renderer = this;

let keyboardIterator = function(i) {
let keyboardIterator = function(i: number) {
return new Promise(function(resolve) {
renderer.processKeyboard(kbds[Object.keys(kbds)[i]]).then(function () {
//console.log("Keyboard " + i + " processed!");
Expand All @@ -319,5 +320,6 @@ export class BatchRenderer {
(function(){
// BatchRenderer's internal state stuff is static on the class and is not exposed with
// the line below.
// @ts-ignore
window['kmw_renderer'] = new BatchRenderer();
})();
56 changes: 35 additions & 21 deletions web/src/tools/testing/recorder/browserDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { type KeymanEngine } from 'keyman/app/browser';

declare var keyman: KeymanEngine;

export type Mutable<Type> = {
-readonly [Property in keyof Type]: Type[Property];
};

function asTouchList(arr: any[]) {
return {
get length() {
Expand Down Expand Up @@ -51,14 +55,14 @@ export class BrowserDriver {

simulateHardwareEvent(eventSpec: PhysicalInputEventSpec) {
// Yep, not KeyboardEvent. "keyCode" is nasty-bugged in Chrome and unusable if initializing through KeyboardEvent.
let event = new Event(BrowserDriver.physicalEventType);
let event: Mutable<Partial<KeyboardEvent>> = new Event(BrowserDriver.physicalEventType);
event['key'] = eventSpec.key;
event['code'] = eventSpec.code;
event['keyCode'] = eventSpec.keyCode;
event['location'] = eventSpec.location;
event['getModifierState'] = eventSpec.getModifierState.bind(eventSpec);

this.target.dispatchEvent(event);
this.target.dispatchEvent(event as KeyboardEvent);
}

async simulateOSKEvent(eventSpec: OSKInputEventSpec) {
Expand Down Expand Up @@ -94,30 +98,40 @@ export class BrowserDriver {
}

// To be safe, we replicate the MouseEvent similarly to the keystroke event.
var downEvent;
var upEvent;
let downEvent: Event;
var upEvent: Event;
if(keyman.config.hostDevice.touchable) {
downEvent = new Event(BrowserDriver.oskDownTouchType);
upEvent = new Event(BrowserDriver.oskUpTouchType);
downEvent['touches'] = asTouchList([{"target": oskKeyElement, ...center}]);
let touchDownEvent: Mutable<Partial<TouchEvent>>;
let touchUpEvent: Mutable<Partial<TouchEvent>>;
touchDownEvent = new Event(BrowserDriver.oskDownTouchType);
touchUpEvent = new Event(BrowserDriver.oskUpTouchType);
touchDownEvent['touches'] = asTouchList([{"target": oskKeyElement, ...center}]);
// The touch should NOT show up in event.touches when a touch ends.
upEvent['touches'] = asTouchList([]);
downEvent['changedTouches'] = asTouchList([{"target": oskKeyElement, ...center}]);
touchUpEvent['touches'] = asTouchList([]);
touchDownEvent['changedTouches'] = asTouchList([{"target": oskKeyElement, ...center}]);
// It should still show up in .changedTouches, though.
upEvent['changedTouches'] = asTouchList([{"target": oskKeyElement, ...center}]);
touchUpEvent['changedTouches'] = asTouchList([{"target": oskKeyElement, ...center}]);

downEvent = touchDownEvent as Event;
upEvent = touchUpEvent as Event;
} else {
downEvent = new Event(BrowserDriver.oskDownMouseType);
upEvent = new Event(BrowserDriver.oskUpMouseType);
downEvent.clientX = center.clientX;
downEvent.clientY = center.clientY;
downEvent['relatedTarget'] = target;
upEvent.clientX = center.clientX;
upEvent.clientY = center.clientY;
upEvent['relatedTarget'] = target;
let mouseDownEvent: Mutable<Partial<MouseEvent>>;
let mouseUpEvent: Mutable<Partial<MouseEvent>>;
mouseDownEvent = new Event(BrowserDriver.oskDownMouseType);
mouseUpEvent = new Event(BrowserDriver.oskUpMouseType);
mouseDownEvent.clientX = center.clientX;
mouseDownEvent.clientY = center.clientY;
mouseDownEvent['relatedTarget'] = target;
mouseUpEvent.clientX = center.clientX;
mouseUpEvent.clientY = center.clientY;
mouseUpEvent['relatedTarget'] = target;
// Mouse-click driven OSK use involves use of at least one mouse button.
downEvent['button'] = upEvent['button'] = 0;
downEvent['buttons'] = 1;
upEvent['buttons'] = 0;
mouseDownEvent['button'] = mouseUpEvent['button'] = 0;
mouseDownEvent['buttons'] = 1;
mouseUpEvent['buttons'] = 0;

downEvent = mouseDownEvent as Event;
upEvent = mouseUpEvent as Event;
}

// Note: our gesture engine's internal structure means that even simple keystrokes like this
Expand Down

0 comments on commit faa3751

Please sign in to comment.