spiegel-keyman/web/source/kmwrotation.ts
Marc Durdin 4d94786852 chore(web): replace lerna with npm workspaces and ts projects
Fixes #6320.

Part 1 of moving from lerna to a simpler, maintained monorepo solution,
using TypeScript Projects and NPM Workspaces.

There is more work to be done here. At this point, KeymanWeb builds and
runs without errors, but the built file is substantially different,
mostly in include order.

Using TypeScript Projects, we move away from the need to run build
scripts in various locations for almost all the Typescript modules.

TODO: Embedded versions and tests have not been verified.

TODO: developer/server is not yet verified.

TODO: developer/js (needs a rename!) is not yet verified.

TODO: Currently, the predictive-text folder needs refactoring to move
the construction of the worker wrapper out of the Predictive Text build
and into the final assembly of keymanweb.js (as it should be valid to
run it as a separate .js anyway).

TODO: Most of the `<reference>` paths need to be re-verified. Ideally
there should be no references outside the current module for any given
.ts.

TODO: The embedded vs browser vs node (headless) builds should be tidied
up for consistency so that it's obvious what depends on what. This is
currently messiest in the predictive-text folder, where the output names
diverge from the filenames and the various files are mixed in the same
folder (as evidenced by the exclusions listed in each tsconfig.json).

TODO: `npm install` should be removed from most build scripts and
instead `npm ci` (#6196) should be run only once from the top-level
folder for any given build. I've had eliminated side-effects from the
`install` action for npm, which makes it easier to reason about state.

TODO: verify_npm_setup and related functions can probably be eliminated.

TODO: most of the build scripts should be largely eliminated for web.

TODO: several ts projects use inconsistent output folders.

TODO: it may be possible to generate a .d.ts for models/types so that
we can use a consistent reference for those as well.

TODO: build.sh, tsconfig.json should always be in the module's top-level
folder, not in a subfolder such as src (e.g. see input-processor/src,
keyboard-processor/src, web/source).

TODO: resources/web-environment should be in common/web.

TODO: other js node_modules imports should be wrapped like es6-shim.

TODO: fix up the publish code for npm modules

TODO: eliminate version numbers from package.json if possible?

Whew, that's most of the stuff I noticed!
2022-04-12 21:02:33 +10:00

152 lines
No EOL
4.9 KiB
TypeScript

// References the base KMW object.
/// <reference path="kmwbase.ts" />
namespace com.keyman {
class RotationState {
innerWidth: number;
innerHeight: number;
constructor() {
this.innerWidth = window.innerWidth;
this.innerHeight = window.innerHeight;
}
equals(other: RotationState) {
return this.innerWidth == other.innerWidth && this.innerHeight == other.innerHeight;
}
}
// Please reference /testing/rotation-events/index.html and update it as necessary when maintaining this class.
export class RotationManager {
private keyman: KeymanBase;
// State variables used by rotations.
private oskVisible: boolean;
private isActive: boolean;
// iOS-oriented members
// --------------------
// We'll assume permutations are complete after this many 'update' iterations.
private static readonly IDLE_PERMUTATION_CAP = 15;
// Tracks the number of idle 'update' iterations since the last permutation.
private idlePermutationCounter: number = RotationManager.IDLE_PERMUTATION_CAP;
// Tracks the most recent rotation state snapshot.
private rotState: RotationState;
// Tracks the window.setTimeout id for rotation update checks.
private updateTimer: number;
private static readonly UPDATE_INTERVAL = 20; // 20 ms, that is.
// --------------------
constructor(keyman: KeymanBase) {
this.keyman = keyman;
}
resolve() {
this.keyman.alignInputs();
var osk = this.keyman.osk;
// TODO: Reattach later in the refactoring process!
//osk.hideLanguageList();
// Force a re-layout for the active keyboard's currently-utilized layout.
if(this.keyman.core.activeKeyboard) {
this.keyman.core.activeKeyboard.refreshLayouts();
}
osk._Load();
if(this.oskVisible) {
osk.refreshLayout();
}
this.isActive = false;
// If we've been using an update interval loop, we should clear the state information.
if(this.updateTimer) {
window.clearInterval(this.updateTimer);
this.rotState = null;
}
}
// Used by both Android and iOS.
initNewRotation() {
this.oskVisible = this.keyman.osk.isVisible();
this.keyman.osk.hideNow();
this.isActive = true;
}
/**
* Establishes rotation-oriented event handling for native-mode KeymanWeb. At this time, tablet PCs are not directly supported.
*/
init() {
// If we're in embedded mode, we really should NOT run this method.
if(this.keyman.isEmbedded) {
return;
}
// Note: we use wrapper functions instead of `.bind(this)` in this method to facilitate stubbing for our rotation test page.
var os = this.keyman.util.device.OS;
var util = this.keyman.util;
var rotationManager = this;
if(os == 'iOS') {
/* iOS is rather inconsistent about these events, with changes to important window state information -
* especially to `window.innerWidth` - possible after the events trigger! They don't always trigger
* the same amount or in a consistently predictable manner.
*
* The overall idea is to wait out all those changes so that we don't produce a bad keyboard layout.
*/
util.attachDOMEvent(window, 'orientationchange', function() {
rotationManager.iOSEventHandler();
return false;
});
util.attachDOMEvent(window, 'resize', function() {
rotationManager.iOSEventHandler();
return false;
});
} else if(os == 'Android') {
// Android's far more consistent with its event generation than iOS.
if('onmozorientationchange' in screen) {
util.attachDOMEvent(<any>screen, 'mozorientationchange', function() {
rotationManager.initNewRotation();
return false;
});
} else {
util.attachDOMEvent(window, 'orientationchange', function() {
rotationManager.initNewRotation();
return false;
});
}
util.attachDOMEvent(window, 'resize', function() {
rotationManager.resolve();
return false;
});
}
}
iOSEventHandler() {
if(!this.isActive) {
this.initNewRotation();
this.rotState = new RotationState();
this.updateTimer = window.setInterval(this.iOSEventUpdate.bind(this), RotationManager.UPDATE_INTERVAL);
}
// If one of the rotation-oriented events just triggered, we should ALWAYS reset the counter.
this.idlePermutationCounter = 0;
}
iOSEventUpdate() {
var newState = new RotationState();
if(this.rotState.equals(newState)) {
if(++this.idlePermutationCounter == RotationManager.IDLE_PERMUTATION_CAP) {
this.resolve();
}
} else {
this.rotState = newState;
this.idlePermutationCounter = 0;
}
}
}
}