cleanup references to button, tablet

This commit is contained in:
howard-stearns 2018-07-16 13:06:12 -07:00
parent 46061a2a15
commit ffa5259319
2 changed files with 32 additions and 64 deletions

View file

@ -13,13 +13,16 @@ function AppUi(properties) {
1. var AppUi = Script.require('appUi'); 1. var AppUi = Script.require('appUi');
2. Put appname-i.svg, appname-a.svg in graphicsDirectory (where non-default graphicsDirectory can be added in #3). 2. Put appname-i.svg, appname-a.svg in graphicsDirectory (where non-default graphicsDirectory can be added in #3).
3. ui = new AppUi({buttonName: "APPNAME", home: "qml-or-html-path"}); 3. ui = new AppUi({buttonName: "APPNAME", home: "qml-or-html-path"});
(and if converting an existing app, (And if converting an existing app,
define var tablet = ui.tablet, button = ui.button; as needed. define var tablet = ui.tablet, button = ui.button; as needed.
remove button.clicked.[dis]connect and tablet.remove(button).) remove button.clicked.[dis]connect and tablet.remove(button).)
4. Define onOpened and onClosed behavior in #3, if any. 4. Define onOpened and onClosed behavior in #3, if any.
(and if converting an existing app, remove screenChanged.[dis]connect.) (And if converting an existing app, remove screenChanged.[dis]connect.)
5. Define onMessage in #3, if any. 5. Define onMessage and sendMessage in #3, if any.
(and if converting an existing app, remove code that [un]wires that message handling.) (And if converting an existing app, remove code that [un]wires that message handling such as
fromQml/sendToQml or webEventReceived/emitScriptEvent.)
6. (If converting an existing app, cleanup stuff that is no longer necessary, like references to button, tablet,
and use isOpen, open(), and close() as needed.)
x. lint! x. lint!
*/ */
@ -36,14 +39,14 @@ function AppUi(properties) {
that.checkIsOpen = function checkIsOpen(type, tabletUrl) { // Are we active? Value used to set isOpen. that.checkIsOpen = function checkIsOpen(type, tabletUrl) { // Are we active? Value used to set isOpen.
return (type === that.type) && (tabletUrl.indexOf(that.home) >= 0); // Actual url may have prefix or suffix. return (type === that.type) && (tabletUrl.indexOf(that.home) >= 0); // Actual url may have prefix or suffix.
} }
that.toOpen = function toOpen() { // How to open the app. that.open = function open() { // How to open the app.
if (that.isQML()) { if (that.isQML()) {
that.tablet.loadQMLSource(that.home); that.tablet.loadQMLSource(that.home);
} else { } else {
that.tablet.gotoWebScreen(that.home, that.inject); that.tablet.gotoWebScreen(that.home, that.inject);
} }
}; };
that.toClose = function toClose() { // How to close the app. that.close = function close() { // How to close the app.
// for toolbar-mode: go back to home screen, this will close the window. // for toolbar-mode: go back to home screen, this will close the window.
that.tablet.gotoHomeScreen(); that.tablet.gotoHomeScreen();
}; };
@ -87,7 +90,7 @@ function AppUi(properties) {
that.onScreenChanged = function onScreenChanged(type, url) { that.onScreenChanged = function onScreenChanged(type, url) {
// Set isOpen, wireEventBridge, set buttonActive as appropriate, // Set isOpen, wireEventBridge, set buttonActive as appropriate,
// and finally call onOpened() or onClosed() IFF defined. // and finally call onOpened() or onClosed() IFF defined.
print('hrs fixme onScreenChanged', type, url, that.isOpen); console.debug(that.buttonName, 'onScreenChanged', type, url, that.isOpen);
if (that.checkIsOpen(type, url)) { if (that.checkIsOpen(type, url)) {
if (!that.isOpen) { if (!that.isOpen) {
that.isOpen = true; that.isOpen = true;
@ -112,17 +115,17 @@ function AppUi(properties) {
that.hasEventBridge = false; that.hasEventBridge = false;
that.wireEventBridge = function wireEventBridge(on) { that.wireEventBridge = function wireEventBridge(on) {
// Sets hasEventBridge and wires onMessage to eventSignal as appropriate, IFF onMessage defined. // Sets hasEventBridge and wires onMessage to eventSignal as appropriate, IFF onMessage defined.
print('hrs fixme wireEventBridge', on, that.hasEventBridge); console.debug(that.buttonName, 'wireEventBridge', on, that.hasEventBridge);
if (!that.onMessage) { return; } if (!that.onMessage) { return; }
if (on) { if (on) {
if (!that.hasEventBridge) { if (!that.hasEventBridge) {
print('hrs fixme connecting', that.eventSignal()); console.debug(that.buttonName, 'connecting', that.eventSignal());
that.eventSignal().connect(that.onMessage); that.eventSignal().connect(that.onMessage);
that.hasEventBridge = true; that.hasEventBridge = true;
} }
} else { } else {
if (that.hasEventBridge) { if (that.hasEventBridge) {
print('hrs fixme connecting', that.eventSignal()); console.debug(that.buttonName, 'connecting', that.eventSignal());
that.eventSignal().disconnect(that.onMessage); that.eventSignal().disconnect(that.onMessage);
that.hasEventBridge = false; that.hasEventBridge = false;
} }
@ -132,18 +135,18 @@ function AppUi(properties) {
// To facilitate incremental development, only wire onClicked to do something when "home" is defined in properties. // To facilitate incremental development, only wire onClicked to do something when "home" is defined in properties.
that.onClicked = that.home that.onClicked = that.home
? function onClicked() { ? function onClicked() {
// Call toOpen() or toClose(), and reset type based on current home property. // Call open() or close(), and reset type based on current home property.
if (that.isOpen) { if (that.isOpen) {
that.toClose(); that.close();
} else { } else {
that.type = /.qml$/.test(that.home) ? 'QML' : 'Web' that.type = /.qml$/.test(that.home) ? 'QML' : 'Web'
that.toOpen(); that.open();
} }
} : that.ignore; } : that.ignore;
that.onScriptEnding = function onScriptEnding() { that.onScriptEnding = function onScriptEnding() {
// Close if necessary, clean up any remaining handlers, and remove the button. // Close if necessary, clean up any remaining handlers, and remove the button.
if (that.isOpen) { if (that.isOpen) {
that.toClose(); that.close();
} }
that.tablet.screenChanged.disconnect(that.onScreenChanged); that.tablet.screenChanged.disconnect(that.onScreenChanged);
if (that.button) { if (that.button) {

View file

@ -325,7 +325,7 @@ function fromQml(message) { // messages are {method, params}, like json-rpc. See
} }
function sendToQml(message) { function sendToQml(message) {
tablet.sendToQml(message); ui.tablet.sendToQml(message);
} }
function updateUser(data) { function updateUser(data) {
print('PAL update:', JSON.stringify(data)); print('PAL update:', JSON.stringify(data));
@ -670,44 +670,24 @@ triggerMapping.from(Controller.Standard.LTClick).peek().to(makeClickHandler(Cont
triggerPressMapping.from(Controller.Standard.RT).peek().to(makePressHandler(Controller.Standard.RightHand)); triggerPressMapping.from(Controller.Standard.RT).peek().to(makePressHandler(Controller.Standard.RightHand));
triggerPressMapping.from(Controller.Standard.LT).peek().to(makePressHandler(Controller.Standard.LeftHand)); triggerPressMapping.from(Controller.Standard.LT).peek().to(makePressHandler(Controller.Standard.LeftHand));
var ui;
// Most apps can have people toggle the tablet closed and open again, and the app should remain "open" even while
// the tablet is not shown. However, for the pal, we explicitly close the app and return the tablet to it's
// home screen (so that the avatar highlighting goes away).
function tabletVisibilityChanged() { function tabletVisibilityChanged() {
if (!tablet.tabletShown && onPalScreen) { if (!ui.tablet.tabletShown && ui.isOpen) {
ContextOverlay.enabled = true; ui.close();
tablet.gotoHomeScreen();
} }
} }
var wasOnPalScreen = false;
var onPalScreen = false;
/*var hasEventBridge = false;
function wireEventBridge(on) {
if (on) {
if (!hasEventBridge) {
tablet.fromQml.connect(fromQml);
hasEventBridge = true;
}
} else {
if (hasEventBridge) {
tablet.fromQml.disconnect(fromQml);
hasEventBridge = false;
}
}
}*/
function captureState() {
wasOnPalScreen = onPalScreen;
onPalScreen = ui.isOpen;
//wireEventBridge(onPalScreen);
}
function on() { function on() {
captureState();
isWired = true;
ContextOverlay.enabled = false; ContextOverlay.enabled = false;
Users.requestsDomainListData = true; Users.requestsDomainListData = true;
audioTimer = createAudioInterval(AUDIO_LEVEL_UPDATE_INTERVAL_MS); audioTimer = createAudioInterval(AUDIO_LEVEL_UPDATE_INTERVAL_MS);
tablet.tabletShownChanged.connect(tabletVisibilityChanged); ui.tablet.tabletShownChanged.connect(tabletVisibilityChanged);
Script.update.connect(updateOverlays); Script.update.connect(updateOverlays);
Controller.mousePressEvent.connect(handleMouseEvent); Controller.mousePressEvent.connect(handleMouseEvent);
Controller.mouseMoveEvent.connect(handleMouseMoveEvent); Controller.mouseMoveEvent.connect(handleMouseMoveEvent);
@ -716,7 +696,6 @@ function on() {
triggerPressMapping.enable(); triggerPressMapping.enable();
populateNearbyUserList(); populateNearbyUserList();
} }
var button, ui, tablet;
// //
// Message from other scripts, such as edit.js // Message from other scripts, such as edit.js
@ -729,8 +708,8 @@ function receiveMessage(channel, messageString, senderID) {
var message = JSON.parse(messageString); var message = JSON.parse(messageString);
switch (message.method) { switch (message.method) {
case 'select': case 'select':
if (!onPalScreen) { if (!ui.isOpen) {
tablet.loadQMLSource(ui.home); ui.open();
Script.setTimeout(function () { sendToQml(message); }, 1000); Script.setTimeout(function () { sendToQml(message); }, 1000);
} else { } else {
sendToQml(message); // Accepts objects, not just strings. sendToQml(message); // Accepts objects, not just strings.
@ -810,9 +789,8 @@ function avatarDisconnected(nodeID) {
function clearLocalQMLDataAndClosePAL() { function clearLocalQMLDataAndClosePAL() {
sendToQml({ method: 'clearLocalQMLData' }); sendToQml({ method: 'clearLocalQMLData' });
if (onPalScreen) { if (ui.isOpen) {
ContextOverlay.enabled = true; ui.close();
tablet.gotoHomeScreen();
} }
} }
@ -838,7 +816,6 @@ function startup() {
onMessage: fromQml onMessage: fromQml
}); });
tablet = ui.tablet; tablet = ui.tablet;
button = ui.button;
Window.domainChanged.connect(clearLocalQMLDataAndClosePAL); Window.domainChanged.connect(clearLocalQMLDataAndClosePAL);
Window.domainConnectionRefused.connect(clearLocalQMLDataAndClosePAL); Window.domainConnectionRefused.connect(clearLocalQMLDataAndClosePAL);
Messages.subscribe(CHANNEL); Messages.subscribe(CHANNEL);
@ -850,40 +827,28 @@ function startup() {
} }
startup(); startup();
var isWired = false;
var audioTimer; var audioTimer;
var AUDIO_LEVEL_UPDATE_INTERVAL_MS = 100; // 10hz for now (change this and change the AVERAGING_RATIO too) var AUDIO_LEVEL_UPDATE_INTERVAL_MS = 100; // 10hz for now (change this and change the AVERAGING_RATIO too)
function off() { function off() {
captureState(); if (ui.isOpen) { // i.e., only when connected
if (isWired) {
Script.update.disconnect(updateOverlays); Script.update.disconnect(updateOverlays);
Controller.mousePressEvent.disconnect(handleMouseEvent); Controller.mousePressEvent.disconnect(handleMouseEvent);
Controller.mouseMoveEvent.disconnect(handleMouseMoveEvent); Controller.mouseMoveEvent.disconnect(handleMouseMoveEvent);
tablet.tabletShownChanged.disconnect(tabletVisibilityChanged); ui.tablet.tabletShownChanged.disconnect(tabletVisibilityChanged);
Users.usernameFromIDReply.disconnect(usernameFromIDReply); Users.usernameFromIDReply.disconnect(usernameFromIDReply);
ContextOverlay.enabled = true
triggerMapping.disable(); triggerMapping.disable();
triggerPressMapping.disable(); triggerPressMapping.disable();
Users.requestsDomainListData = false; Users.requestsDomainListData = false;
isWired = false;
if (audioTimer) { if (audioTimer) {
Script.clearInterval(audioTimer); Script.clearInterval(audioTimer);
} }
} }
removeOverlays(); removeOverlays();
if (wasOnPalScreen) { ContextOverlay.enabled = true;
ContextOverlay.enabled = true;
}
} }
function shutdown() { function shutdown() {
if (onPalScreen) {
tablet.gotoHomeScreen();
}
Window.domainChanged.disconnect(clearLocalQMLDataAndClosePAL); Window.domainChanged.disconnect(clearLocalQMLDataAndClosePAL);
Window.domainConnectionRefused.disconnect(clearLocalQMLDataAndClosePAL); Window.domainConnectionRefused.disconnect(clearLocalQMLDataAndClosePAL);
Messages.subscribe(CHANNEL); Messages.subscribe(CHANNEL);