fix(tauri): grant the window controls the permissions they call
The minimize, maximize and close buttons did nothing. They were wired
correctly; the ACL rejected them. core:default resolves to
core🪟default, which grants only read-only queries — is-maximized
is in it, which is why the maximize icon tracked state correctly, but
minimize, toggle-maximize, close and start-dragging are not. Both
windows are decorations: false, so with close denied the main window
could not be closed at all except through the tray.
Add two capabilities rather than widening the shared one: the main
window gets the four controls it draws, and the pet window gets dragging
alone, since it is moved by dragging its body and must not be able to
close or resize itself.
Every one of these calls was a bare void expression, so the rejection
was swallowed and looked identical to an unwired button. Route them
through a helper that reports the failure in the shell error banner.
This is the second time a missing capability has surfaced as a silent
no-op — the pet window previously had no entry at all. Add tests that
read the shipped capability files and assert each permission the UI
calls is granted to the window that calls it.
Also translate the TypeError the Tauri shim throws outside the
application into a sentence a user can act on. The dev server is
reachable at 127.0.0.1:1420, so opening it in a browser and hitting save
produced "Cannot read properties of undefined (reading 'invoke')".
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
父节点
bc0948731c
当前提交
779ffc3d55
共有 6 个文件被更改,包括 171 次插入 和 6 次删除
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "main-window-controls",
|
||||
"description": "Window controls for the main window, which is decorationless and therefore draws its own title bar. core:window:default grants only read-only queries, so without these the minimize, maximize and close buttons are rejected by the ACL and the window cannot be closed except through the tray.",
|
||||
"windows": ["main"],
|
||||
"permissions": [
|
||||
"core:window:allow-minimize",
|
||||
"core:window:allow-toggle-maximize",
|
||||
"core:window:allow-close",
|
||||
"core:window:allow-start-dragging"
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "pet-window-drag",
|
||||
"description": "The desktop pet is decorationless and is moved by dragging its body. Scoped to dragging alone: the pet must not be able to close or resize itself, which is why this is separate from the main window capability.",
|
||||
"windows": ["desktop-pet"],
|
||||
"permissions": ["core:window:allow-start-dragging"]
|
||||
}
|
||||
77
src-tauri/tests/capabilities.rs
普通文件
77
src-tauri/tests/capabilities.rs
普通文件
|
|
@ -0,0 +1,77 @@
|
|||
//! Guards the capability set against the recurring "silent no-op" defect.
|
||||
//!
|
||||
//! A Tauri command that the ACL rejects fails at the IPC boundary, and every
|
||||
//! rejection the UI does not explicitly catch looks exactly like an unwired
|
||||
//! button. This class has now bitten twice: the desktop pet window had no
|
||||
//! capability entry at all (F-03), and the main window's title bar controls were
|
||||
//! rejected because `core:window:default` grants only read-only queries.
|
||||
//!
|
||||
//! These tests read the shipped capability files, so a permission removed while
|
||||
//! the UI still calls it fails here rather than in someone's hands.
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Capability {
|
||||
identifier: String,
|
||||
windows: Vec<String>,
|
||||
permissions: Vec<String>,
|
||||
}
|
||||
|
||||
fn capability(source: &str) -> Capability {
|
||||
serde_json::from_str(source).expect("capability file must be valid JSON")
|
||||
}
|
||||
|
||||
const DEFAULT: &str = include_str!("../capabilities/default.json");
|
||||
const MAIN_CONTROLS: &str = include_str!("../capabilities/main-window-controls.json");
|
||||
const PET_DRAG: &str = include_str!("../capabilities/pet-window-drag.json");
|
||||
|
||||
#[test]
|
||||
fn the_main_window_may_drive_its_own_title_bar() {
|
||||
let capability = capability(MAIN_CONTROLS);
|
||||
|
||||
assert_eq!(capability.identifier, "main-window-controls");
|
||||
assert!(capability.windows.iter().any(|label| label == "main"));
|
||||
|
||||
// Each entry corresponds to a call in `WindowChrome` in src/App.tsx. The
|
||||
// window is `decorations: false`, so without `allow-close` there is no way
|
||||
// to close it other than the tray.
|
||||
for permission in [
|
||||
"core:window:allow-minimize",
|
||||
"core:window:allow-toggle-maximize",
|
||||
"core:window:allow-close",
|
||||
"core:window:allow-start-dragging",
|
||||
] {
|
||||
assert!(
|
||||
capability.permissions.iter().any(|held| held == permission),
|
||||
"{permission} is called by the title bar but not granted"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_pet_window_may_drag_itself_and_nothing_more() {
|
||||
let capability = capability(PET_DRAG);
|
||||
|
||||
assert_eq!(capability.windows, vec!["desktop-pet".to_owned()]);
|
||||
assert_eq!(
|
||||
capability.permissions,
|
||||
vec!["core:window:allow-start-dragging".to_owned()],
|
||||
"the pet is moved by dragging its body; it must not gain close or resize"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_declared_window_carries_the_base_capability() {
|
||||
let capability = capability(DEFAULT);
|
||||
|
||||
// Missing a label here is what left the pet window unable to receive events
|
||||
// or use IPC at all.
|
||||
for label in ["main", "quick-chat", "status-bubble", "desktop-pet"] {
|
||||
assert!(
|
||||
capability.windows.iter().any(|held| held == label),
|
||||
"window {label} has no base capability"
|
||||
);
|
||||
}
|
||||
assert_eq!(capability.permissions, vec!["core:default".to_owned()]);
|
||||
}
|
||||
30
src/App.tsx
30
src/App.tsx
|
|
@ -151,6 +151,7 @@ export function App() {
|
|||
onToggleTheme={() =>
|
||||
setTheme((current) => (current === "dark" ? "light" : "dark"))
|
||||
}
|
||||
onError={setError}
|
||||
/>
|
||||
<div className="app-shell-body">
|
||||
<aside className="app-sidebar">
|
||||
|
|
@ -244,9 +245,11 @@ export function App() {
|
|||
function WindowChrome({
|
||||
theme,
|
||||
onToggleTheme,
|
||||
onError,
|
||||
}: {
|
||||
theme: ThemeMode;
|
||||
onToggleTheme: () => void;
|
||||
onError: (message: string) => void;
|
||||
}) {
|
||||
const [currentWindow, setCurrentWindow] = useState<ReturnType<
|
||||
typeof getCurrentWindow
|
||||
|
|
@ -263,12 +266,25 @@ function WindowChrome({
|
|||
}
|
||||
}, []);
|
||||
|
||||
const toggleMaximize = async () => {
|
||||
/// Every one of these is an ACL-gated IPC call. They used to be bare
|
||||
/// `void ...()` expressions, so when the capability set was missing the
|
||||
/// window permissions the rejection was swallowed and the buttons simply did
|
||||
/// nothing, with no way to tell that from an unwired handler.
|
||||
const runWindowAction = async (action: () => Promise<void>) => {
|
||||
if (!currentWindow) return;
|
||||
await currentWindow.toggleMaximize();
|
||||
setMaximized(await currentWindow.isMaximized());
|
||||
try {
|
||||
await action();
|
||||
} catch (reason) {
|
||||
onError(errorMessage(reason));
|
||||
}
|
||||
};
|
||||
|
||||
const toggleMaximize = () =>
|
||||
runWindowAction(async () => {
|
||||
await currentWindow!.toggleMaximize();
|
||||
setMaximized(await currentWindow!.isMaximized());
|
||||
});
|
||||
|
||||
return (
|
||||
<header
|
||||
className="window-chrome"
|
||||
|
|
@ -281,7 +297,7 @@ function WindowChrome({
|
|||
'[data-tauri-drag-region="false"]',
|
||||
)
|
||||
) {
|
||||
void currentWindow?.startDragging();
|
||||
void runWindowAction(() => currentWindow!.startDragging());
|
||||
}
|
||||
}}
|
||||
>
|
||||
|
|
@ -302,7 +318,9 @@ function WindowChrome({
|
|||
<button
|
||||
aria-label="Minimize window"
|
||||
className="window-control"
|
||||
onClick={() => void currentWindow?.minimize()}
|
||||
onClick={() =>
|
||||
void runWindowAction(() => currentWindow!.minimize())
|
||||
}
|
||||
onMouseDown={(event) => event.stopPropagation()}
|
||||
type="button"
|
||||
>
|
||||
|
|
@ -326,7 +344,7 @@ function WindowChrome({
|
|||
<button
|
||||
aria-label="Close window"
|
||||
className="window-control window-control-close"
|
||||
onClick={() => void currentWindow?.close()}
|
||||
onClick={() => void runWindowAction(() => currentWindow!.close())}
|
||||
onMouseDown={(event) => event.stopPropagation()}
|
||||
type="button"
|
||||
>
|
||||
|
|
|
|||
37
src/lib/errorMessage.test.ts
普通文件
37
src/lib/errorMessage.test.ts
普通文件
|
|
@ -0,0 +1,37 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
errorMessage,
|
||||
IPC_UNAVAILABLE_MESSAGE,
|
||||
isCancelledError,
|
||||
} from "./errorMessage";
|
||||
|
||||
describe("errorMessage", () => {
|
||||
it("explains a page running outside the application window", () => {
|
||||
// The exact shape thrown by the Tauri shim in a plain browser tab.
|
||||
const reason = new TypeError(
|
||||
"Cannot read properties of undefined (reading 'invoke')",
|
||||
);
|
||||
|
||||
expect(errorMessage(reason)).toBe(IPC_UNAVAILABLE_MESSAGE);
|
||||
});
|
||||
|
||||
it("keeps the message of an ordinary error", () => {
|
||||
expect(errorMessage(new Error("boom"))).toBe("boom");
|
||||
});
|
||||
|
||||
it("reads the message out of a Rust command error", () => {
|
||||
expect(
|
||||
errorMessage({ code: "runtimeUnavailable", message: "未连接" }),
|
||||
).toBe("未连接");
|
||||
});
|
||||
|
||||
it("falls back to the code when no message is present", () => {
|
||||
expect(errorMessage({ code: "slotBusy" })).toBe("slotBusy");
|
||||
});
|
||||
|
||||
it("recognizes a deliberate cancellation", () => {
|
||||
expect(isCancelledError({ code: "cancelled" })).toBe(true);
|
||||
expect(isCancelledError({ code: "runtimeUnavailable" })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
@ -6,8 +6,22 @@
|
|||
* than an `Error` instance. Passing such a value to `String()` yields
|
||||
* "[object Object]", so every IPC failure has to go through this helper.
|
||||
*/
|
||||
/**
|
||||
* The Tauri API shim reads `window.__TAURI_INTERNALS__.invoke`, so calling any
|
||||
* command from a plain browser tab throws a `TypeError` about reading 'invoke'
|
||||
* rather than anything a user could act on. The dev server is reachable at
|
||||
* 127.0.0.1:1420, so this is easy to hit by accident.
|
||||
*/
|
||||
const IPC_UNAVAILABLE_PATTERN = /reading ['"]?invoke['"]?|__TAURI_INTERNALS__/;
|
||||
|
||||
export const IPC_UNAVAILABLE_MESSAGE =
|
||||
"此页面不在 AstrBot Desktop 应用窗口内运行,无法使用本地功能。请在应用中操作。";
|
||||
|
||||
export function errorMessage(reason: unknown): string {
|
||||
if (reason instanceof Error) {
|
||||
if (IPC_UNAVAILABLE_PATTERN.test(reason.message)) {
|
||||
return IPC_UNAVAILABLE_MESSAGE;
|
||||
}
|
||||
return reason.message;
|
||||
}
|
||||
if (typeof reason === "string") {
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用