feat/p02-dual-connections #3

已合并
YokiiroBW 已将来自 feat/p02-dual-connections 的 31 提交合并入 main 2026-07-21 14:40:19 +08:00
共有 8 个文件被更改,包括 1182 次插入 和 811 次删除
仅显示提交 92ad44d327 的更改 - 显示所有提交

查看文件

@ -220,3 +220,35 @@ Still not claimed complete: automatic startup activation, real Tauri multi-windo
- Fixed the tray Quit action so it destroys the main, quick-chat, status-bubble, and pet windows before requesting process exit.
- This avoids the global close-to-tray handler intercepting shutdown and leaving the process resident.
- Validation: Rust format, Clippy with warnings denied, Rust workspace tests (66), TypeScript, ESLint, Vitest (6), and NSIS bundle build pass.
## 2026-07-20: desktop pet rendering and tray exit follow-up
- Fixed the pixel spritesheet crop by setting the sprite image transform origin to the top-left; the previous default center origin moved the selected frame outside the clipped viewport and produced a blank transparent pet window.
- Added an application exiting state so tray Quit allows all windows to close instead of the global close-to-tray handler hiding them and leaving an empty, non-interactive placeholder.
- Validation: frontend typecheck, Vitest (6), Vite production build, Rust format, Rust check, and Rust workspace tests (66) pass.
## 2026-07-20: transparent pet and tray process exit hardening
- Replaced the transparent-window spritesheet crop with a PNG-backed Canvas renderer; this avoids WebView2 transparent-window failures with the large WebP image and CSS transform cropping.
- Changed tray Quit to call Tauri cleanup and terminate the process immediately, ensuring the tray icon, hidden main window, and pet window cannot remain as orphaned UI surfaces.
- Rebuilt and launched the Release executable at 18:24:20 for manual verification.
## 2026-07-20: wire pet display mode and render diagnostics
- Connected the pet display-mode setting to persisted local UI state; `app-ui` now embeds the pet in the Pet page, while `desktop-floating` explicitly opens the transparent Tauri window.
- Added sprite loading and Canvas failure feedback instead of leaving a silent blank/white surface.
- Validation: TypeScript, Vitest (6), Vite build, Rust format/check, and Release bundle build pass. The rebuilt executable started at 18:35:29.
## 2026-07-20: in-app draggable pet overlay
- Promoted the app-ui pet from a static page preview to an application-level floating overlay, matching the Codex/Hermes-style in-window companion behavior.
- Added pointer dragging with clamped coordinates and local persistence; the overlay can be hidden without creating a native desktop window.
- Disabled the broken desktop-floating launch path in the Pet settings and provided a direct switch back to the in-app mode.
- Validation: TypeScript, Vitest (6), Vite build, Rust format/check, and Release bundle build pass. The rebuilt executable started at 18:44:43.
## 2026-07-20: restore desktop floating pet transparency
- Restored the desktop-floating display option and its `open_pet_window` action; it is no longer disabled.
- Fixed the Windows transparent WebView configuration by explicitly setting an RGBA-zero background and disabling undecorated window shadow, which could otherwise render a white rectangle around the pet.
- Preserved the app-ui draggable overlay implementation.
- Validation: TypeScript, Vitest (6), Rust format/check, Vite build, and Release bundle build pass. The rebuilt executable started at 18:50:25.

查看文件

@ -25,10 +25,11 @@ use runtime_guards::RuntimeGuards;
use serde::Serialize;
use tauri::{
AppHandle, Manager, PhysicalPosition, PhysicalSize, WebviewUrl, WebviewWindowBuilder,
AppHandle, Emitter, Manager, PhysicalPosition, PhysicalSize, WebviewUrl, WebviewWindowBuilder,
WindowEvent,
menu::{Menu, MenuItem},
menu::{Menu, MenuItem, PredefinedMenuItem},
tray::TrayIconBuilder,
window::Color,
};
const PET_WINDOW_ID: &str = "desktop-pet";
@ -58,7 +59,9 @@ fn open_status_bubble_window(app: AppHandle) -> Result<(), String> {
.inner_size(108.0, 108.0)
.resizable(false)
.decorations(false)
.shadow(false)
.transparent(true)
.background_color(Color(0, 0, 0, 0))
.always_on_top(false)
.skip_taskbar(true)
.build()
@ -157,12 +160,8 @@ fn save_quick_window_state(window: &tauri::Window, app: &AppHandle) {
}
fn exit_application(app: &AppHandle) {
for label in ["main", QUICK_WINDOW_ID, "status-bubble", PET_WINDOW_ID] {
if let Some(window) = app.get_webview_window(label) {
let _ = window.destroy();
}
}
app.exit(0);
app.cleanup_before_exit();
std::process::exit(0);
}
#[tauri::command]
@ -177,24 +176,97 @@ fn open_main_window(app: AppHandle) -> Result<(), String> {
#[tauri::command]
fn open_pet_window(app: AppHandle) -> Result<(), String> {
if let Some(window) = app.get_webview_window(PET_WINDOW_ID) {
window
.set_always_on_top(true)
.map_err(|error| error.to_string())?;
window
.set_position(PhysicalPosition::new(120, 120))
.map_err(|error| error.to_string())?;
window.show().map_err(|error| error.to_string())?;
return Ok(());
}
WebviewWindowBuilder::new(
let config = app
.config()
.app
.windows
.iter()
.find(|window| window.label == PET_WINDOW_ID)
.ok_or_else(|| "pet window config is unavailable".to_owned())?;
WebviewWindowBuilder::from_config(&app, config)
.map_err(|error| error.to_string())?
.build()
.map(|_| ())
.map_err(|error| error.to_string())
}
#[tauri::command]
fn close_pet_window(app: AppHandle) -> Result<(), String> {
if let Some(window) = app.get_webview_window(PET_WINDOW_ID) {
window.destroy().map_err(|error| error.to_string())?;
}
Ok(())
}
#[tauri::command]
fn show_pet_context_menu(app: AppHandle) -> Result<(), String> {
let open_main = MenuItem::with_id(
&app,
PET_WINDOW_ID,
WebviewUrl::App("index.html?view=pet".into()),
"pet-open-main",
"\u{6253}\u{5f00}\u{4e3b}\u{754c}\u{9762}",
true,
None::<&str>,
)
.title("AstrBot Pet")
.inner_size(180.0, 220.0)
.resizable(false)
.decorations(false)
.transparent(true)
.always_on_top(false)
.skip_taskbar(true)
.build()
.map(|_| ())
.map_err(|error| error.to_string())
.map_err(|error| error.to_string())?;
let open_quick = MenuItem::with_id(
&app,
"pet-open-quick",
"\u{6253}\u{5f00}\u{5feb}\u{6377}\u{804a}\u{5929}",
true,
None::<&str>,
)
.map_err(|error| error.to_string())?;
let toggle_game = MenuItem::with_id(
&app,
"pet-game-mode",
"\u{5207}\u{6362}\u{6e38}\u{620f}\u{6a21}\u{5f0f}",
true,
None::<&str>,
)
.map_err(|error| error.to_string())?;
let separator = PredefinedMenuItem::separator(&app).map_err(|error| error.to_string())?;
let hide_pet = MenuItem::with_id(
&app,
"pet-hide",
"\u{5173}\u{95ed}\u{684c}\u{5ba0}",
true,
None::<&str>,
)
.map_err(|error| error.to_string())?;
let quit = MenuItem::with_id(
&app,
"pet-exit",
"\u{5173}\u{95ed}\u{7a0b}\u{5e8f}",
true,
None::<&str>,
)
.map_err(|error| error.to_string())?;
let menu = Menu::with_items(
&app,
&[
&open_main,
&open_quick,
&toggle_game,
&separator,
&hide_pet,
&quit,
],
)
.map_err(|error| error.to_string())?;
let window = app
.get_webview_window(PET_WINDOW_ID)
.or_else(|| app.get_webview_window("main"))
.ok_or_else(|| "pet context menu window is unavailable".to_owned())?;
window.popup_menu(&menu).map_err(|error| error.to_string())
}
#[tauri::command]
@ -241,6 +313,23 @@ pub fn run() {
let _ = window.set_focus();
}
}))
.on_menu_event(|app, event| match event.id.as_ref() {
"pet-open-main" => {
let _ = open_main_window(app.clone());
}
"pet-open-quick" => {
let _ = open_quick_chat_window(app.clone(), None);
}
"pet-game-mode" => {
let _ = app.emit("pet-game-mode-toggle", ());
}
"pet-hide" => {
let _ = close_pet_window(app.clone());
let _ = app.emit("pet-hide-requested", ());
}
"pet-exit" => exit_application(app),
_ => {}
})
.setup(|app| {
let app_data_dir = app.path().app_data_dir()?;
std::fs::create_dir_all(&app_data_dir)?;
@ -305,6 +394,8 @@ pub fn run() {
open_main_window,
open_status_bubble_window,
open_pet_window,
close_pet_window,
show_pet_context_menu,
connection_policy_status,
profiles::list_connection_profiles,
profiles::create_connection_profile,

查看文件

@ -1,39 +1,58 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "AstrBot Desktop",
"version": "0.1.0-alpha.1",
"identifier": "com.yokiirobw.astrbot-desktop",
"build": {
"beforeDevCommand": "npm run dev",
"devUrl": "http://127.0.0.1:1420",
"beforeBuildCommand": "npm run build",
"frontendDist": "../dist"
},
"app": {
"windows": [
{
"label": "main",
"title": "AstrBot Desktop",
"width": 1100,
"height": 760,
"minWidth": 720,
"minHeight": 520,
"center": true,
"resizable": true
}
],
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost http://127.0.0.1:*; img-src 'self' asset: http://asset.localhost data:; style-src 'self' 'unsafe-inline'; script-src 'self'"
}
},
"bundle": {
"active": true,
"targets": ["nsis"],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.ico"
]
}
}
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "AstrBot Desktop",
"version": "0.1.0-alpha.1",
"identifier": "com.yokiirobw.astrbot-desktop",
"build": {
"beforeDevCommand": "npm run dev",
"devUrl": "http://127.0.0.1:1420",
"beforeBuildCommand": "npm run build",
"frontendDist": "../dist"
},
"app": {
"windows": [
{
"label": "main",
"title": "AstrBot Desktop",
"width": 1100,
"height": 760,
"minWidth": 720,
"minHeight": 520,
"center": true,
"resizable": true
},
{
"label": "desktop-pet",
"title": "AstrBot Pet",
"url": "index.html?view=pet",
"width": 180,
"height": 220,
"resizable": false,
"decorations": false,
"shadow": false,
"transparent": true,
"alwaysOnTop": true,
"skipTaskbar": true,
"visible": false,
"x": 120,
"y": 120,
"create": true
}
],
"security": {
"csp": "default-src \u0027self\u0027; connect-src ipc: http://ipc.localhost http://127.0.0.1:*; img-src \u0027self\u0027 asset: http://asset.localhost data:; style-src \u0027self\u0027 \u0027unsafe-inline\u0027; script-src \u0027self\u0027"
}
},
"bundle": {
"active": true,
"targets": [
"nsis"
],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.ico"
]
}
}

查看文件

@ -1,18 +1,19 @@
import { invoke } from "@tauri-apps/api/core";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { useCallback, useEffect, useState } from "react";
import { listen } from "@tauri-apps/api/event";
import { useCallback, useEffect, useRef, useState } from "react";
import { ChatPanel } from "./features/chat/ChatPanel";
import { StatusBubble } from "./features/chat/StatusBubble";
import { ProfileManager } from "./features/profiles/ProfileManager";
import { PetPackImporter } from "./features/pet/PetPackImporter";
import { connectionRuntimeStatusSchema } from "./features/chat/types";
import aemeathSpritesheet from "./features/pet/assets/aemeath/aemeath-spritesheet.webp";
import aemeathSpritesheet from "./features/pet/assets/aemeath/aemeath-spritesheet.png?inline";
import { bootstrapStatusSchema, type BootstrapStatus } from "./types/bootstrap";
type PageKey = "chat" | "connections" | "desktop" | "pet" | "diagnostics";
type ChatLayout = "single" | "focus-companion" | "focus-operations";
type ChatSlot = "companion" | "operations";
type PetDisplayMode = "app-ui" | "desktop-floating";
const initialStatus: BootstrapStatus = {
appVersion: "unknown",
@ -50,7 +51,7 @@ export function App() {
const view = params.get("view");
const isQuickView = view === "quick";
const isBubbleView = view === "bubble";
const isPetView = view === "pet";
const isPetView = view === "pet" || window.location.hash === "#pet" || document.documentElement.dataset.astrbotPet === "true";
const quickSlot = params.get("slot");
const [page, setPage] = useState<PageKey>("chat");
const [status, setStatus] = useState(initialStatus);
@ -64,6 +65,11 @@ export function App() {
const [bubbleTarget, setBubbleTarget] = useState<"main" | "quick">(() =>
localStorage.getItem("astrbot.bubbleTarget") === "quick" ? "quick" : "main",
);
const [petDisplayMode, setPetDisplayMode] = useState<PetDisplayMode>(() =>
localStorage.getItem("astrbot.petDisplayMode") === "desktop-floating"
? "desktop-floating"
: "app-ui",
);
const refreshStatus = useCallback(async () => {
try {
@ -79,6 +85,14 @@ export function App() {
void refreshStatus();
}, [refreshStatus]);
useEffect(() => {
if (isBubbleView || isPetView || isQuickView) return;
const command = petDisplayMode === "desktop-floating" ? "open_pet_window" : "close_pet_window";
void invoke(command).catch((reason) => {
setError(reason instanceof Error ? reason.message : String(reason));
});
}, [isBubbleView, isPetView, isQuickView, petDisplayMode]);
if (isBubbleView) return <StatusBubble />;
if (isPetView) return <DesktopPetView />;
if (isQuickView) {
@ -160,7 +174,15 @@ export function App() {
}
/>
) : null}
{page === "pet" ? <PetPage /> : null}
{page === "pet" ? (
<PetPage
displayMode={petDisplayMode}
onDisplayModeChange={(value) => {
setPetDisplayMode(value);
localStorage.setItem("astrbot.petDisplayMode", value);
}}
/>
) : null}
{page === "diagnostics" ? (
<DiagnosticsPage
status={status}
@ -168,10 +190,78 @@ export function App() {
/>
) : null}
</section>
{petDisplayMode === "app-ui" ? <InAppPetOverlay /> : null}
</main>
);
}
function InAppPetOverlay() {
const [visible, setVisible] = useState(true);
const [position, setPosition] = useState(() => {
try {
const saved = JSON.parse(localStorage.getItem("astrbot.petPosition") ?? "null");
return saved && typeof saved.x === "number" && typeof saved.y === "number"
? saved
: { x: 24, y: 96 };
} catch {
return { x: 24, y: 96 };
}
});
const dragRef = useRef<{ pointerId: number; offsetX: number; offsetY: number } | null>(null);
const savePosition = (x: number, y: number) => {
const next = {
x: Math.max(0, Math.min(x, window.innerWidth - 188)),
y: Math.max(0, Math.min(y, window.innerHeight - 226)),
};
setPosition(next);
localStorage.setItem("astrbot.petPosition", JSON.stringify(next));
};
useEffect(() => {
let unlisten: (() => void) | undefined;
void listen("pet-hide-requested", () => setVisible(false)).then((cleanup) => {
unlisten = cleanup;
});
return () => unlisten?.();
}, []);
return visible ? (
<div
className="app-pet-overlay"
style={{ left: position.x, top: position.y }}
onContextMenu={(event) => {
event.preventDefault();
void invoke("show_pet_context_menu");
}}
onPointerDown={(event) => {
if (event.button !== 0) return;
event.currentTarget.setPointerCapture(event.pointerId);
dragRef.current = {
pointerId: event.pointerId,
offsetX: event.clientX - position.x,
offsetY: event.clientY - position.y,
};
}}
onPointerMove={(event) => {
if (dragRef.current?.pointerId !== event.pointerId) return;
savePosition(
event.clientX - dragRef.current.offsetX,
event.clientY - dragRef.current.offsetY,
);
}}
onPointerUp={(event) => {
if (dragRef.current?.pointerId === event.pointerId) dragRef.current = null;
}}
onPointerCancel={() => {
dragRef.current = null;
}}
>
<DesktopPetView embedded />
</div>
) : null;
}
function navIcon(page: PageKey) {
return {
chat: "⌁",
@ -364,23 +454,41 @@ function DesktopBehaviorPage({
);
}
function PetPage() {
function PetPage({
displayMode,
onDisplayModeChange,
}: {
displayMode: PetDisplayMode;
onDisplayModeChange: (value: PetDisplayMode) => void;
}) {
return (
<div className="page-stack">
<PetPackImporter />
{displayMode === "app-ui" ? (
<section className="pet-inline-preview" aria-label="应用内桌宠预览">
<div>
<span className="section-kicker">App UI</span>
<h2>应用内活动桌宠</h2>
<p>桌宠已叠加在主界面上,可直接拖动;关闭后可重新打开应用。</p>
</div>
</section>
) : null}
<section className="settings-grid">
<SettingCard
title="显示范围"
description="决定桌宠只在应用内显示,还是允许在整个桌面活动。"
description="应用内活动桌宠不创建桌面浮窗,避免透明窗口白框。"
>
<select defaultValue="app">
<option value="app">仅应用 UI 内</option>
<option value="desktop">整个桌面</option>
<select
value={displayMode}
onChange={(event) => onDisplayModeChange(event.target.value as PetDisplayMode)}
>
<option value="app-ui">仅应用 UI 内</option>
<option value="desktop-floating">&#x684c;&#x9762;&#x6d6e;&#x52a8;</option>
</select>
</SettingCard>
<SettingCard
title="活动策略"
description="为后续游戏模式和双屏行为预留配置入口。"
description="为后续游戏模式和多显示器行为预留配置入口。"
>
<select defaultValue="smart">
<option value="smart">智能选择显示器</option>
@ -390,24 +498,58 @@ function PetPage() {
</SettingCard>
<SettingCard
title="运行桌宠"
description="桌宠窗口使用透明无边框模式,不创建任务栏按钮;关闭窗口后继续驻留托盘。"
description="应用内桌宠可在主界面中自由移动,不再创建白色桌面浮窗。"
>
<button type="button" onClick={() => void invoke("open_pet_window")}>
打开桌宠
</button>
{displayMode === "desktop-floating" ? (
<button type="button" onClick={() => onDisplayModeChange("app-ui")}>
改用应用内活动桌宠
</button>
) : (
<span className="form-note">应用内活动桌宠已显示,可直接拖动</span>
)}
</SettingCard>
</section>
</div>
);
}
function DesktopPetView() {
function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
const [state, setState] = useState<"idle" | "working" | "resting" | "speaking" | "error">("idle");
const [frameIndex, setFrameIndex] = useState(0);
const [spriteReady, setSpriteReady] = useState(false);
const [spriteError, setSpriteError] = useState<string | null>(null);
const [gameMode, setGameMode] = useState(false);
const spriteImageRef = useRef<HTMLImageElement | null>(null);
const spriteCanvasRef = useRef<HTMLCanvasElement | null>(null);
useEffect(() => {
if (embedded) return;
document.documentElement.classList.add("pet-view");
return () => document.documentElement.classList.remove("pet-view");
}, [embedded]);
useEffect(() => {
let unlisten: (() => void) | undefined;
void listen("pet-game-mode-toggle", () => setGameMode((current) => !current)).then((cleanup) => {
unlisten = cleanup;
});
return () => unlisten?.();
}, []);
useEffect(() => {
const image = new Image();
image.onload = () => {
spriteImageRef.current = image;
setSpriteError(null);
setSpriteReady(true);
};
image.onerror = () => {
setSpriteError("????????");
};
image.src = AEMEATH_SPRITESHEET;
return () => {
image.onload = null;
};
}, []);
useEffect(() => {
@ -459,31 +601,61 @@ function DesktopPetView() {
const frameRow = Math.floor(frame / 8);
const frameColumn = frame % 8;
useEffect(() => {
const canvas = spriteCanvasRef.current;
const image = spriteImageRef.current;
if (!canvas || !image || !spriteReady) return;
const context = canvas.getContext("2d");
if (!context) {
setSpriteError("?? Canvas ????");
return;
}
try {
context.clearRect(0, 0, 192, 208);
context.imageSmoothingEnabled = false;
context.drawImage(image, frameColumn * 192, frameRow * 208, 192, 208, 0, 0, 192, 208);
} catch (reason) {
setSpriteError(reason instanceof Error ? reason.message : "??????");
}
}, [frameColumn, frameRow, spriteReady]);
return (
<main className={`pet-window pet-window-${state}`} aria-label="AstrBot 桌宠">
<button
type="button"
className="pet-window-close"
aria-label="隐藏桌宠"
onClick={() => void getCurrentWindow().close()}
>
×
</button>
<main
className={`pet-window pet-window-${state}${gameMode ? " pet-window-game" : ""}`}
aria-label="AstrBot ??"
onContextMenu={(event) => {
event.preventDefault();
void invoke("show_pet_context_menu");
}}
>
<div
className={`aemeath-pet-frame aemeath-pet-${state}`}
role="img"
aria-label={`桌宠状态:${state}`}
>
<img
className="aemeath-pet-sprite"
className="aemeath-pet-sprite-sheet"
src={AEMEATH_SPRITESHEET}
alt=""
draggable={false}
aria-hidden="true"
onLoad={() => setSpriteReady(true)}
onError={() => setSpriteError("????????")}
style={{
transform: `translate(-${frameColumn * 192}px, -${frameRow * 208}px)`,
left: -(frameColumn * 192),
top: -(frameRow * 208),
}}
/>
<canvas
ref={spriteCanvasRef}
className="aemeath-pet-sprite"
width={192}
height={208}
role="img"
aria-label={`pet-${state}`}
/>
</div>
{spriteError ? <span className="pet-render-error">{spriteError}</span> : null}
{!spriteReady && !spriteError ? <span className="pet-render-loading">Loading pet...</span> : null}
<span className="pet-window-status">{state === "working" ? "正在写写画画" : state === "resting" ? "休息中" : state === "speaking" ? "正在回应" : state === "error" ? "需要重新登录或检查连接" : "陪伴中"}</span>
</main>
);

二进制文件未显示。

之后

宽度:  |  高度:  |  大小: 3.7 MiB

查看文件

@ -2,7 +2,7 @@
"format": "astrbot-pet-pack.v1",
"name": "Aemeath Pixel Companion",
"version": "1.0.0",
"image": "aemeath-spritesheet.webp",
"image": "aemeath-spritesheet.png",
"width": 1536,
"height": 3120,
"frameWidth": 192,

文件差异内容过多而无法显示 加载差异

查看文件

@ -4,6 +4,7 @@ import react from "@vitejs/plugin-react";
const host = process.env.TAURI_DEV_HOST;
export default defineConfig({
base: "./",
plugins: [react()],
clearScreen: false,
server: {