mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
An application with the ask close policy could decline every close, and nothing could end it. TERMINATE_APP now takes force, which terminates the session without asking. The taskbar offers it as Force quit once an app has had longer than the close timeout to answer. The close interaction now comes before Running -> Stopping, as blueprint 6.2 requires. The session stays running while the application answers, and a declined close no longer moves Stopping back to Running. Stop requests reach the supervisor on a channel, so a repeated or forced request during the close wait is seen without registering again. A declining window is brought to the front, so its prompt is visible. A clean exit after a close request is recorded as a close on request. A decline report is recognised after page output that lacks a newline. Notes keeps a pending Save and close when asked again. It saves text typed during that save before closing, and reports when it cannot save because it is not connected.
315 lines
9 KiB
HTML
315 lines
9 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Notes</title>
|
|
<style>
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
background: #0f0f0f;
|
|
color: #e8e8e8;
|
|
display: flex;
|
|
flex-direction: column;
|
|
height: 100vh;
|
|
padding: 24px;
|
|
gap: 16px;
|
|
}
|
|
header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
flex-shrink: 0;
|
|
}
|
|
h1 {
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
letter-spacing: 0.12em;
|
|
text-transform: uppercase;
|
|
color: #666;
|
|
}
|
|
.actions {
|
|
display: flex;
|
|
gap: 8px;
|
|
align-items: center;
|
|
}
|
|
button {
|
|
height: 32px;
|
|
padding: 0 16px;
|
|
border-radius: 8px;
|
|
border: 1px solid #2a2a2a;
|
|
background: #1a1a1a;
|
|
color: #e8e8e8;
|
|
font-size: 13px;
|
|
font-weight: 500;
|
|
cursor: pointer;
|
|
transition: background 0.1s;
|
|
}
|
|
button:hover { background: #222; }
|
|
button:active { background: #2a2a2a; }
|
|
button.primary {
|
|
background: #2a2a2a;
|
|
border-color: #444;
|
|
}
|
|
button.primary:hover { background: #333; }
|
|
textarea {
|
|
flex: 1;
|
|
background: #1a1a1a;
|
|
border: 1px solid #2a2a2a;
|
|
border-radius: 12px;
|
|
color: #e8e8e8;
|
|
font-family: inherit;
|
|
font-size: 15px;
|
|
line-height: 1.6;
|
|
padding: 20px;
|
|
resize: none;
|
|
outline: none;
|
|
transition: border-color 0.15s;
|
|
}
|
|
textarea:focus { border-color: #444; }
|
|
textarea::placeholder { color: #444; }
|
|
footer {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
flex-shrink: 0;
|
|
height: 20px;
|
|
}
|
|
#status {
|
|
font-size: 12px;
|
|
color: #444;
|
|
}
|
|
#closing {
|
|
display: none;
|
|
align-items: center;
|
|
gap: 8px;
|
|
flex-shrink: 0;
|
|
font-size: 13px;
|
|
}
|
|
#closing.shown { display: flex; }
|
|
#closing span { flex: 1; }
|
|
#charcount {
|
|
font-size: 12px;
|
|
color: #444;
|
|
font-variant-numeric: tabular-nums;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1>Notes</h1>
|
|
<div class="actions">
|
|
<button id="discard">Discard</button>
|
|
<button class="primary" id="save">Save</button>
|
|
</div>
|
|
</header>
|
|
<div id="closing" role="alertdialog" aria-label="Unsaved changes">
|
|
<span>These notes have unsaved changes.</span>
|
|
<button class="primary" id="close-save">Save and close</button>
|
|
<button id="close-discard">Close without saving</button>
|
|
<button id="close-cancel">Cancel</button>
|
|
</div>
|
|
<textarea id="editor" placeholder="Start typing…" spellcheck="false"></textarea>
|
|
<footer>
|
|
<span id="status">connecting…</span>
|
|
<span id="charcount">0 chars</span>
|
|
</footer>
|
|
|
|
<script>
|
|
(function () {
|
|
// The component stores the text exactly as typed. Messages are JSON; each
|
|
// save names the revision it was edited from and is acknowledged by its
|
|
// sequence number, so a reply never replaces text typed after it was sent.
|
|
// The editor is read-only until it holds the stored text, so nothing typed
|
|
// before a load completes can be saved over notes the user never saw.
|
|
var MAX_MESSAGE = 65536; // weft-appd's limit for one app message, in bytes
|
|
var REPLY_TIMEOUT = 10000;
|
|
var editor = document.getElementById('editor');
|
|
var statusEl = document.getElementById('status');
|
|
var charEl = document.getElementById('charcount');
|
|
var saveButton = document.getElementById('save');
|
|
var saved = '';
|
|
var rev = null;
|
|
var seq = 0;
|
|
var pending = null;
|
|
var lastSent = null;
|
|
var loading = null; // timer of the outstanding load, if any
|
|
var encoder = new TextEncoder();
|
|
var closing = document.getElementById('closing');
|
|
var closeAfterSave = false;
|
|
|
|
function setStatus(msg) { statusEl.textContent = msg; }
|
|
function countChars() { charEl.textContent = editor.value.length + ' chars'; }
|
|
function send(message) { window.weftIpc.send(JSON.stringify(message)); }
|
|
function setLoaded(loaded) {
|
|
editor.readOnly = !loaded;
|
|
saveButton.disabled = !loaded || pending !== null;
|
|
}
|
|
|
|
editor.addEventListener('input', countChars);
|
|
|
|
function finishSave() {
|
|
if (pending) clearTimeout(pending.timer);
|
|
pending = null;
|
|
setLoaded(rev !== null);
|
|
}
|
|
|
|
// Close policy ("ask" in wapp.toml): when the window is asked to close
|
|
// with unsaved changes, the close is cancelled and the user chooses to
|
|
// save first, close without saving, or keep editing. The window closes
|
|
// after a save only once the save is acknowledged, saving again first if
|
|
// the text changed meanwhile; a failed save keeps it open with the error
|
|
// shown. Text typed while the notes are not loaded cannot be saved, so it
|
|
// does not hold the window open.
|
|
function unsaved() {
|
|
return pending !== null || (rev !== null && editor.value !== saved);
|
|
}
|
|
|
|
function hideClosing() {
|
|
closing.classList.remove('shown');
|
|
closeAfterSave = false;
|
|
}
|
|
|
|
window.addEventListener('weftclose', function (e) {
|
|
if (!unsaved()) return;
|
|
e.preventDefault();
|
|
// Asked again while the prompt is up, keep the user's choice so far.
|
|
closing.classList.add('shown');
|
|
});
|
|
|
|
document.getElementById('close-save').addEventListener('click', function () {
|
|
closeAfterSave = true;
|
|
if (!pending) saveButton.click();
|
|
// The save could not start (status says why): stay open.
|
|
if (!pending) hideClosing();
|
|
});
|
|
|
|
document.getElementById('close-discard').addEventListener('click', function () {
|
|
window.close();
|
|
});
|
|
|
|
document.getElementById('close-cancel').addEventListener('click', hideClosing);
|
|
|
|
saveButton.addEventListener('click', function () {
|
|
if (pending) return;
|
|
if (!window.weftIpc) {
|
|
setStatus('not saved: not connected');
|
|
return;
|
|
}
|
|
if (rev === null) {
|
|
setStatus('the notes have not loaded; use Discard to reload them');
|
|
return;
|
|
}
|
|
var request = { op: 'save', seq: seq + 1, base: rev, text: editor.value };
|
|
try {
|
|
encodeURIComponent(request.text);
|
|
} catch (_) {
|
|
setStatus('not saved: the text contains characters that cannot be stored');
|
|
return;
|
|
}
|
|
if (encoder.encode(JSON.stringify(request)).length > MAX_MESSAGE) {
|
|
setStatus('not saved: the notes are too long');
|
|
return;
|
|
}
|
|
seq += 1;
|
|
lastSent = { seq: seq, text: request.text };
|
|
pending = {
|
|
seq: seq,
|
|
text: request.text,
|
|
timer: setTimeout(function () {
|
|
finishSave();
|
|
hideClosing();
|
|
setStatus('not saved: no reply; try again');
|
|
}, REPLY_TIMEOUT),
|
|
};
|
|
setLoaded(true);
|
|
send(request);
|
|
setStatus('saving…');
|
|
});
|
|
|
|
// Loads the stored notes into a read-only editor. Only one load is
|
|
// outstanding at a time; a failed or unanswered load can be retried with
|
|
// Discard.
|
|
function load(status) {
|
|
rev = null;
|
|
lastSent = null;
|
|
setLoaded(false);
|
|
loading = setTimeout(function () {
|
|
loading = null;
|
|
setStatus('the notes did not load; use Discard to retry');
|
|
}, REPLY_TIMEOUT);
|
|
send({ op: 'load' });
|
|
setStatus(status);
|
|
}
|
|
|
|
function finishLoad() {
|
|
clearTimeout(loading);
|
|
loading = null;
|
|
}
|
|
|
|
// Discard reloads the stored notes, replacing unsaved edits.
|
|
document.getElementById('discard').addEventListener('click', function () {
|
|
if (!window.weftIpc || pending || loading) return;
|
|
editor.value = saved;
|
|
countChars();
|
|
load('reloading…');
|
|
});
|
|
|
|
document.addEventListener('keydown', function (e) {
|
|
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 's') {
|
|
e.preventDefault();
|
|
saveButton.click();
|
|
}
|
|
});
|
|
|
|
function onMessage(raw) {
|
|
var msg;
|
|
try { msg = JSON.parse(raw); } catch (_) { return; }
|
|
if (msg.op === 'loaded') {
|
|
// Only the reply to the outstanding load applies; the editor is
|
|
// read-only until it arrives, so it never replaces newer text.
|
|
if (!loading) return;
|
|
finishLoad();
|
|
editor.value = msg.text;
|
|
countChars();
|
|
saved = msg.text;
|
|
rev = msg.rev;
|
|
setLoaded(true);
|
|
setStatus('');
|
|
} else if (msg.op === 'saved' && !loading && lastSent && msg.seq === lastSent.seq) {
|
|
// Also accepts an acknowledgement that arrives after the timeout.
|
|
saved = lastSent.text;
|
|
rev = msg.rev;
|
|
finishSave();
|
|
setStatus(editor.value === saved ? 'saved' : 'saved; newer changes are not saved yet');
|
|
if (closeAfterSave) {
|
|
if (editor.value === saved) window.close();
|
|
// Text typed after this save began is saved before closing.
|
|
else saveButton.click();
|
|
if (!pending && editor.value !== saved) hideClosing();
|
|
}
|
|
} else if (msg.op === 'error') {
|
|
if (pending && msg.seq === pending.seq) {
|
|
finishSave();
|
|
hideClosing();
|
|
}
|
|
if (loading && msg.seq === undefined) finishLoad();
|
|
setStatus(msg.conflict
|
|
? 'not saved: changed in another window. Copy your text, then Discard to reload.'
|
|
: 'error: ' + msg.message);
|
|
}
|
|
}
|
|
|
|
function setupIpc() {
|
|
if (!window.weftIpc) { setTimeout(setupIpc, 50); return; }
|
|
window.weftIpc.onmessage = onMessage;
|
|
load('loading…');
|
|
}
|
|
|
|
setLoaded(false);
|
|
setupIpc();
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|