useHostAction
Trigger safe native macOS host actions from widget interactions: open public web URLs, copy text to the clipboard, open this widget's Edit Widget inspector, or update an option.
Signature#
SIGNATURE
function useHostAction(): (
action: string,
payload?: Record<string, any>
) => any
Supported Actions#
openURL{ url: string }Public URL- Opens a public
https://orhttp://URL in the default browser, e.g.act("openURL", { url: "https://ideskset.com" })(blocksfile://and private hosts). copyText{ text: string }Clipboard- Copies a string to the macOS clipboard, e.g.
act("copyText", { text: "#0071E3" }). openInspectorNoneInspector- Opens this widget's Edit Widget panel, e.g.
act("openInspector"). updateSetting{ key, value }Option- Updates an option declared in
deskpack.json, e.g.act("updateSetting", { key: "goal", value: 10 })(prefer useSetting for most UI controls).
Example: Quick Actions & Copy-to-Clipboard Bar#
TSX
import React, { Button, HStack, Spacer, Text, VStack, useHostAction, useState, useWidget } from "deskset-react";
export default function QuickLinks() {
const { theme, t } = useWidget();
const act = useHostAction();
const [copied, setCopied] = useState(false);
const handleCopy = () => {
act("copyText", { text: "https://ideskset.com/docs/" });
setCopied(true);
setTimeout(() => setCopied(false), 1500);
};
return (
<VStack
maxWidth={true}
maxHeight={true}
padding={16}
spacing={10}
background={theme.isPure ? "clear" : "theme.card"}
cornerRadius={theme.isPure ? 0 : theme.corner}
clip={!theme.isPure}
>
<Text fontSize={14} fontWeight="semibold">{t("开发快捷入口", "Quick Actions")}</Text>
<Spacer />
<HStack spacing={8}>
<Button
title={t("打开文档", "Open Docs")}
icon="safari"
variant="filled"
color="theme.accent"
onClick={() => act("openURL", { url: "https://ideskset.com/docs/" })}
/>
<Button
title={copied ? t("已复制", "Copied") : t("复制链接", "Copy")}
icon={copied ? "checkmark" : "doc.on.doc"}
variant="tinted"
onClick={handleCopy}
/>
<Button
icon="slider.horizontal.3"
variant="tinted"
onClick={() => act("openInspector")}
/>
</HStack>
</VStack>
);
}
Notes#
- All host actions are safely no-ops while
useWidget().isPreview === true. - Community widgets cannot run shell commands, read arbitrary local files, or launch local executables.