diff --git a/docs/progress.md b/docs/progress.md index 5f44fc2..371f775 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -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. diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index ed383b0..01bdefa 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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, diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 27b3b8e..3dd71ad 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -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" + ] + } +} diff --git a/src/App.tsx b/src/App.tsx index cf4b0e6..65be483 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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("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(() => + 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 ; if (isPetView) return ; if (isQuickView) { @@ -160,7 +174,15 @@ export function App() { } /> ) : null} - {page === "pet" ? : null} + {page === "pet" ? ( + { + setPetDisplayMode(value); + localStorage.setItem("astrbot.petDisplayMode", value); + }} + /> + ) : null} {page === "diagnostics" ? ( ) : null} + {petDisplayMode === "app-ui" ? : null} ); } +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 ? ( +
{ + 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; + }} + > + +
+ ) : 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 (
+ {displayMode === "app-ui" ? ( +
+
+ App UI +

应用内活动桌宠

+

桌宠已叠加在主界面上,可直接拖动;关闭后可重新打开应用。

+
+
+ ) : null}
- onDisplayModeChange(event.target.value as PetDisplayMode)} + > + +