@@ -873,7 +884,7 @@ const S = { sending: [], voice: {},
relay: false, token: localStorage.getItem("anybot.token") || "", host: localStorage.getItem("anybot.host") || "",
version: 0, bots: [], approvals: [], pages: [], models: [], hostName: "", mac: false, box: "", boxOn: false, role: "owner",
prevShown: 0, prevFirst: "", answer: null, sc: "",
- open: 0, msgs: [], pending: [], quote: null, unfold: {}, openSteps: {}, detail: {}, audit: {}, settings: null, polling: false,
+ open: 0, msgs: [], pending: [], quote: null, unfold: {}, openSteps: {}, detail: {}, audit: {}, settings: null, polling: false, entered: false,
chatSig: "", file: null,
search: "", size: [0, 0], right: false,
};
@@ -1061,14 +1072,18 @@ const cur = () => NAV[NAV.length - 1] || {v: "home", arg: {}};
// ---------- sign in ----------
async function hello(tries = 0) {
- // Opened on a bad network: the way in it had last time (the account's or the phone password's),
- // not the phone password's because nothing answered.
+ // Still outside: the login and the bot list wait until this answers. A remembered way in
+ // (account or phone password) only labels the wait — it does not open the app on a dead line.
+ if (!S.entered) show("gate");
const known = localStorage.getItem("anybot.relay");
try { const v = await api("api/hello", {timeout: 12000}); S.relay = v.app === "anybot-relay"; localStorage.setItem("anybot.relay", S.relay ? "1" : "0"); netDown(false); }
catch (e) {
if (!e.transient) S.relay = false;
- else if (known !== null) { S.relay = known === "1"; netDown(true); }
- else { netDown(true); await rest(backoff(tries, 15000)); return hello(tries + 1); }
+ else {
+ if (known !== null) S.relay = known === "1";
+ netDown(true);
+ if (!S.entered) { await rest(backoff(tries, 15000)); return hello(tries + 1); }
+ }
}
$("f-user").style.display = S.relay ? "" : "none";
$("b-mode").style.display = S.relay ? "" : "none";
@@ -1107,7 +1122,7 @@ $("b-login").onclick = async () => {
$("f-pass").addEventListener("keydown", (e) => { if (e.key === "Enter") $("b-login").click(); });
function signOut(expired) {
if (S.token && !expired) api(S.relay ? "relay/logout" : "api/logout", {json: {}}).catch(() => {});
- S.token = ""; localStorage.removeItem("anybot.token"); S.version = 0; NAV.length = 0; show("login");
+ S.token = ""; localStorage.removeItem("anybot.token"); S.version = 0; S.entered = false; NAV.length = 0; show("login");
if (expired) $("login-err").textContent = "登入已失效,請重新登入";
}
$("b-out1").innerHTML = ic("log-out", 20);
@@ -1146,19 +1161,34 @@ async function pickHost() {
const waiting = hosts.length && !live.length && wanted ? `
「${esc(wanted.name)}」暫時沒有連線(可能正在重新啟動,或那台電腦睡著了)。
連上後會自動進入,不用重新整理。
` : "";
$("hosts").innerHTML = waiting + (hosts.length ? hosts.map((h) => `
${ic(h.online ? "monitor" : "power", 18)}
${esc(h.name)}
${h.online ? "在線上" : "離線(" + esc(h.last_seen_text || "很久沒連線") + ")"}${note(h)}
`).join("")
: `
這個帳號還沒有連上任何 AnyBot。
在電腦上打開 AnyBot › 設定 › 手機與帳號,登入同一個帳號。
`);
- document.querySelectorAll("#hosts .row").forEach((r) => r.onclick = () => { clearTimeout(hostRetry); S.host = r.dataset.h; localStorage.setItem("anybot.host", S.host); start(); });
+ document.querySelectorAll("#hosts .row").forEach((r) => r.onclick = () => { clearTimeout(hostRetry); S.host = r.dataset.h; localStorage.setItem("anybot.host", S.host); if (!S.entered) show("gate"); start(); });
show("hosts");
if (S.token && !live.length) hostRetry = setTimeout(pickHost, 3000);
}
// ---------- live state ----------
const TZ = (() => { try { return Intl.DateTimeFormat().resolvedOptions().timeZone || ""; } catch (_) { return ""; } })();
+// The first state is in: now the list (or the chat a link asked for) is drawn, already filled.
+function enter() {
+ if (S.entered) return;
+ S.entered = true;
+ netDown(false);
+ if (!NAV.length) { NAV.push({v: "home", arg: {}}); history.replaceState({n: 1}, ""); }
+ if (S.answer && S.token) {
+ const a = S.answer; S.answer = null;
+ api(base() + `api/bots/${a.bot}/answer`, {json: {answer: a.answer, call: a.call, from: "notice"}}).then(() => toast("已從通知回覆")).catch((e) => toast(e.message));
+ }
+ if (S.sc && !S.open) {
+ const want = S.sc === "unread" ? S.bots.find((b) => b.unread) : (S.bots.find((b) => b.primary) || S.bots[0]);
+ S.sc = "";
+ if (want) S.open = want.id;
+ }
+ if (S.open && cur().v !== "chat") go("chat", {bot: S.open});
+ else { const t = cur(); draw(t.v, t.arg); }
+}
async function start() {
- if (!NAV.length) {
- NAV.push({v: "home", arg: {}}); history.replaceState({n: 1}, "");
- if (S.open) go("chat", {bot: S.open}); else draw("home", {});
- } else { const t = cur(); draw(t.v, t.arg); }
if (S.polling) return;
+ if (!S.entered) show("gate");
S.polling = true;
let fails = 0;
while (S.token) {
@@ -1168,7 +1198,7 @@ async function start() {
// when their signature changed, and then only from where they differ (api/state?bot=…
// &chat_sig=…&chat_delta=1). The rest of the state likewise: only what changed since the
// state this page holds (ssig).
- const open = S.view === "chat" && S.open ? "&bot=" + S.open + "&chat_sig=" + encodeURIComponent(S.chatSig) + "&chat_delta=1" : "";
+ const open = S.open && (S.view === "chat" || !S.entered) ? "&bot=" + S.open + "&chat_sig=" + encodeURIComponent(S.chatSig) + "&chat_delta=1" : "";
const held = S.version && S.ssig ? "&ssig=" + encodeURIComponent(S.ssig) : "";
let v = await api(base() + "api/state?since=" + S.version + "&tz=" + encodeURIComponent(TZ) + held + open, {timeout: POLL_MS, signal: pollCtl.signal});
fails = 0; netDown(false);
@@ -1179,7 +1209,8 @@ async function start() {
v = whole;
}
S.st = v; S.ssig = v.ssig || "";
- if (v.version !== S.version) { S.version = v.version; apply(v); }
+ if (v.version !== S.version || !S.entered) { S.version = v.version; apply(v); }
+ if (!S.entered) enter();
} catch (e) {
if (!S.token) break;
// Cut short by kick(): ask again at once.
@@ -1217,6 +1248,18 @@ function mergeState(prev, v) {
}
function apply(v) {
S.bots = v.bots || []; S.approvals = v.approvals || []; S.hostName = v.host || ""; S.mac = !!v.mac;
+ S.box = v.box || ""; S.boxOn = !!v.box_available; S.pages = v.pages || []; S.models = v.models || []; S.sections = v.sections || null; S.role = v.role || "owner";
+ S.voice = v.voice || {}; micShown(); appBadge();
+ // The computer's 外觀 (an older brain sends neither: the built-in look stays).
+ if (v.palettes) applyPalette(v.palettes);
+ if (typeof v.theme === "string" && v.theme !== DESK.theme) { DESK.theme = v.theme; try { localStorage.setItem("anybot.desk-theme", v.theme); } catch (_) {} applyTheme(); }
+ // Still on the gate: keep what arrived (so the list and an opened chat are full the moment
+ // we go in) but don't draw the inside yet.
+ if (!S.entered) {
+ const c = v.chat;
+ if (c && c.bot === S.open && c.messages && c.from === undefined) { S.msgs = c.messages; S.chatSig = c.sig || ""; }
+ return;
+ }
// An approval answered from a notification banner, and a home-screen shortcut (主要 Bot, 未讀).
if (S.answer && S.token) {
const a = S.answer; S.answer = null;
@@ -1228,11 +1271,6 @@ function apply(v) {
S.sc = "";
if (want) { S.open = want.id; go("chat", {bot: want.id}); }
}
- S.box = v.box || ""; S.boxOn = !!v.box_available; S.pages = v.pages || []; S.models = v.models || []; S.sections = v.sections || null; S.role = v.role || "owner";
- S.voice = v.voice || {}; micShown(); appBadge();
- // The computer's 外觀 (an older brain sends neither: the built-in look stays).
- if (v.palettes) applyPalette(v.palettes);
- if (typeof v.theme === "string" && v.theme !== DESK.theme) { DESK.theme = v.theme; try { localStorage.setItem("anybot.desk-theme", v.theme); } catch (_) {} applyTheme(); }
const t = cur();
if (t.v === "home") renderHome();
else if (t.v === "chat") {
@@ -1388,7 +1426,7 @@ $("b-me").onclick = () => {
if (!installed() && (S.installPrompt || navigator.standalone === false)) items.push({icon: "download", label: "安裝成 App", on: installApp});
if (S.mac) items.push({icon: "monitor", label: "看這台 Mac 的螢幕", on: () => go("screen")});
if (S.relay && owner()) items.push({icon: "plus", label: "加一台電腦(Linux、伺服器)", on: pair});
- if (S.relay) items.push({icon: "refresh", label: "切換到別台 AnyBot", on: () => { S.host = ""; localStorage.removeItem("anybot.host"); S.version = 0; NAV.length = 0; pickHost(); }});
+ if (S.relay) items.push({icon: "refresh", label: "切換到別台 AnyBot", on: () => { S.host = ""; localStorage.removeItem("anybot.host"); S.version = 0; S.ssig = ""; S.entered = false; NAV.length = 0; show("gate"); kick(); pickHost(); }});
items.push({icon: "log-out", label: "登出這支手機", danger: true, on: async () => { if (await confirmSheet("要登出這支手機嗎?", "", "登出", true)) signOut(); }});
sheet(S.hostName || "AnyBot", items);
};
@@ -1427,7 +1465,9 @@ function openChat(id) {
chat.classList.toggle("quiet", bubble === "quiet");
if (bubble === "bot" && ob.color && ob.color_key !== "black") { chat.style.setProperty("--bot", ob.color); chat.style.setProperty("--bot-ink", "var(--on-color)"); }
else { chat.style.removeProperty("--bot"); chat.style.removeProperty("--bot-ink"); }
- chatHead(); renderChips(); renderQuote(); loadChat(true); renderDock();
+ chatHead(); renderChips(); renderQuote();
+ if (S.msgs.length) { renderMsgs(); $("scroller").scrollTop = $("scroller").scrollHeight; }
+ loadChat(true); renderDock();
api(base() + `api/bots/${id}/read`, {json: {}}).catch(() => {});
}
let loading = false, again = false;
@@ -2796,7 +2836,7 @@ function wireSettings(kind, st) {
else if (k === "2fa") twoFaSheet();
else if (k === "devices") devicesSheet();
else if (k === "pair") pair();
- else if (k === "host") { S.host = ""; localStorage.removeItem("anybot.host"); S.version = 0; NAV.length = 0; pickHost(); }
+ else if (k === "host") { S.host = ""; localStorage.removeItem("anybot.host"); S.version = 0; S.ssig = ""; S.entered = false; NAV.length = 0; show("gate"); kick(); pickHost(); }
else if (k === "out") { if (await confirmSheet("要登出這支手機嗎?", "", "登出")) signOut(); }
});
on("[data-bx]", "onclick", async (x) => {
diff --git a/src/agent.rs b/src/agent.rs
index c01ef46..9df57ad 100644
--- a/src/agent.rs
+++ b/src/agent.rs
@@ -1944,9 +1944,18 @@ pub fn spawn(job: Job, bot_id: u64, tx: Sender<(u64, StreamEvent)>, cancel: Arc<
let mut job = job;
crate::providers::set_patient(job.unattended);
TURN_BOT.with(|b| b.set(bot_id));
+ // A token must not redraw the whole window on its own: they arrive faster than a
+ // slower machine can paint, and the fans stay on for the whole answer.
+ let last_paint = std::cell::Cell::new(Instant::now() - Duration::from_millis(50));
let send = |ev: StreamEvent| {
let _ = tx.send((bot_id, ev));
- ctx.request_repaint();
+ let now = Instant::now();
+ if now.saturating_duration_since(last_paint.get()) >= Duration::from_millis(50) {
+ last_paint.set(now);
+ ctx.request_repaint();
+ } else {
+ ctx.request_repaint_after(Duration::from_millis(50));
+ }
};
let mut history = job.req.history.clone();
let mut grants = job.grants.clone();
diff --git a/src/app/chat_ui.rs b/src/app/chat_ui.rs
index c149549..09ae326 100644
--- a/src/app/chat_ui.rs
+++ b/src/app/chat_ui.rs
@@ -420,7 +420,7 @@ pub fn spinner(ui: &mut egui::Ui, size: f32, col: Color32) {
c + vec2(a.cos(), a.sin()) * r
}).collect();
ui.painter().add(egui::Shape::line(pts, Stroke::new(2.0, col)));
- ui.ctx().request_repaint();
+ ui.ctx().request_repaint_after(std::time::Duration::from_millis(50));
}
/// Text with a light band sweeping across it (the original's sand-shimmer-text).
@@ -436,7 +436,7 @@ pub fn shimmer(ui: &mut egui::Ui, text: &str, base: Color32, bright: Color32, si
job.append(&ch.to_string(), 0.0, egui::TextFormat { font_id: egui::FontId::proportional(size), color: col, ..Default::default() });
}
ui.label(job);
- ui.ctx().request_repaint();
+ ui.ctx().request_repaint_after(std::time::Duration::from_millis(50));
}
impl App {
@@ -962,7 +962,7 @@ impl App {
let now = ui.input(|i| i.time);
self.jump_bottom = jump_still_on(self.jump_bottom, from_bottom, now);
if self.jump_bottom.is_some() {
- ui.ctx().request_repaint();
+ ui.ctx().request_repaint_after(std::time::Duration::from_millis(50));
}
let id = egui::Id::unique("jump-bottom");
let k = ui.ctx().animate_bool_with_time(id, jump_shows(from_bottom, self.jump_bottom.is_some()), JUMP_BOTTOM_FADE);
diff --git a/src/app/mood_ui.rs b/src/app/mood_ui.rs
index 49733d7..8c6e7a7 100644
--- a/src/app/mood_ui.rs
+++ b/src/app/mood_ui.rs
@@ -259,10 +259,17 @@ impl App {
Mood::Idle => false,
// An error stays on screen: the shake and the drop play once, then it rests.
Mood::Failed => age < 2.6,
+ // Needs the person for as long as they take: the ring and the hop still move,
+ // but the whole window is not redrawn every frame while the card sits there.
+ Mood::Waiting => false,
+ Mood::Done => age < CHEER_SECS,
_ => true,
};
// (Its own state, apart from the same bot's face in the sidebar, which looks elsewhere.)
let mut pose = self.settle_face(&ctx, !bot.id, target, moving, false);
+ if mood == Mood::Waiting {
+ ctx.request_repaint_after(std::time::Duration::from_millis(200));
+ }
pose.eyes = match mood {
Mood::Done => avatar::Eyes::Smile,
Mood::Failed => avatar::Eyes::Flat,
diff --git a/src/app/sidebar_ui.rs b/src/app/sidebar_ui.rs
index b65fc58..e706d6f 100644
--- a/src/app/sidebar_ui.rs
+++ b/src/app/sidebar_ui.rs
@@ -161,7 +161,9 @@ impl App {
}
let moving = f.poke.is_some() || (f.gaze - target).length() > 0.002 || blink > 0.0 || working || f.spin != 0.0;
if moving {
- ctx.request_repaint();
+ // One frame of the whole window, not as fast as the display can go: on a slower
+ // machine an uncapped face redrew the chat flat out and the fans never stopped.
+ ctx.request_repaint_after(std::time::Duration::from_millis(50));
} else {
ctx.request_repaint_after(std::time::Duration::from_secs_f64((f.next_blink - now).max(0.05)));
}
@@ -1059,7 +1061,7 @@ impl App {
ui.allocate_exact_size(screen.size(), egui::Sense::click());
});
}
- ctx.request_repaint();
+ ctx.request_repaint_after(std::time::Duration::from_millis(50));
true
}
}