Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

WIP: Let apps request to be headless #14

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
32 changes: 28 additions & 4 deletions src/modules/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ class UI extends EventListener {
// The most recent value that we received for a given broadcast, by name.
#lastBroadcastValue = new Map(); // name -> data

// Sent as the `windowConfig` field of the READY message.
#windowConfig = {
headless: false,
};

// Whether we've sent the READY message.
#readyMessageSent = false;

// Replaces boilerplate for most methods: posts a message to the GUI with a unique ID, and sets a callback for it.
#postMessageWithCallback = function(name, resolve, args = {}) {
const msg_id = this.#messageID++;
Expand Down Expand Up @@ -162,10 +170,14 @@ class UI extends EventListener {

// Tell the host environment that this app is using the Puter SDK and is ready to receive messages,
// this will allow the OS to send custom messages to the app
this.messageTarget?.postMessage({
msg: "READY",
appInstanceID: this.appInstanceID,
}, '*');
document.addEventListener('DOMContentLoaded', () => {
this.messageTarget?.postMessage({
msg: "READY",
appInstanceID: this.appInstanceID,
windowConfig: this.#windowConfig,
}, '*');
this.#readyMessageSent = true;
});

// When this app's window is focused send a message to the host environment
window.addEventListener('focus', (e) => {
Expand Down Expand Up @@ -405,6 +417,18 @@ class UI extends EventListener {
});
}

// Available options:
// - headless: Run app with no visible window
configureWindow = function(options) {
if (this.#readyMessageSent) {
throw new Error('Window configuration has already been sent; this call to puter.ui.configureWindow will have no effect! ' +
'Make sure to call it before the DOMContentLoaded event.');
}
this.#windowConfig = {
headless: options?.headless ?? false,
};
}

onWindowClose = function(callback) {
this.#onWindowClose = callback;
}
Expand Down