플러그인 설정 추가
본 문서에서는
@muvel-plugins/settings,@muvel-plugins/settings-schema,@muvel-plugins/settings-store플러그인을 사용하여 설정을 추가하는 방법을 설명합니다. 아래 예시는drpc.desktop,webview-zoom.desktop,path-restorer.tauri,open-folder.tauri구현 패턴을 바탕으로 정리했습니다.
설정값 추가하기
먼저 설정 스토어를 정의합니다.
// src/settings.ts
import { defineSettings } from "@muvel-plugins/settings-store"
export const settings = defineSettings("my-plugin", {
default: {
enabled: false,
zoom: 1,
},
})그다음 plugin.ts에서 스토어를 등록합니다.
import { settingsStorePluginId } from "@muvel-plugins/settings-store"
import { settings } from "./settings"
setup: async (mgr, ctx) => {
const storesApi = mgr.use(ctx, settingsStorePluginId)
const store = await storesApi.register(settings)
// JS 영역에서 즉시 읽기
const enabled = store.get("enabled")
// 변경 구독 (예: webview zoom 반영)
store.subscribeValues<number>(
async (zoom) => {
// zoom 변경 시 처리
},
(config) => config.zoom,
)
}store.get("key"), store.subscribeValues(...) 패턴은 path-restorer.tauri, webview-zoom.desktop에서 실제로 사용 중인 방식입니다.
설정 UI 추가하기
설정 화면에 항목을 노출하려면 settingsApi.register(...)를 통해 탭 아이템을 등록합니다.
import { settingsPluginId } from "@muvel-plugins/settings"
import {
categoryIds,
tabIds,
} from "@muvel-plugins/settings-schema"
import { MyPluginConfig } from "./settings-ui"
setup: async (mgr, ctx) => {
const settingsApi = mgr.use(ctx, settingsPluginId)
settingsApi.register((data) => {
const tab = data[categoryIds.general]!.tabs[tabIds.general.general]!
tab.items["my-plugin"] = {
order: 500,
component: MyPluginConfig,
}
})
}간단한 스위치/슬라이더 UI는 SettingStoreField를 쓰면 빠르게 만들 수 있습니다.
// src/settings-ui.tsx
import { Switch } from "@chakra-ui/react"
import {
SettingStoreField,
useSettingsStore,
} from "@muvel-plugins/settings-store"
import { settings } from "./settings"
export const MyPluginConfig = () => {
const configStore = useSettingsStore(settings.id)
return (
<SettingStoreField
store={configStore}
fieldKey="enabled"
render={({ value, onChange, disabled }) => (
<Switch.Root
checked={value}
disabled={disabled}
onCheckedChange={({ checked }) => onChange(checked)}
>
<Switch.HiddenInput />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
</Switch.Root>
)}
/>
)
}이 구조는 drpc.desktop, path-restorer.tauri의 실제 구현과 동일한 패턴입니다.
설정값 불러오기
React 내부에서
React 컴포넌트에서는 useSettingsStore(settings.id)를 사용합니다.
import { useSettingsStore } from "@muvel-plugins/settings-store"
import { settings } from "./settings"
const Example = () => {
const store = useSettingsStore(settings.id)
const enabled = store.useValues((v) => v.enabled)
return <>{enabled ? "ON" : "OFF"}</>
}store.useValues(selector) 패턴은 webview-zoom.desktop의 타이틀 표시처럼 UI 반영에 유용합니다.
JS 영역에서
plugin.ts의 setup 내부에서는 등록된 store로 즉시 조회/구독할 수 있습니다.
setup: async (mgr, ctx) => {
const storesApi = mgr.use(ctx, settingsStorePluginId)
const store = await storesApi.register(settings)
const enabled = store.get("enabled")
if (!enabled) return
store.subscribeValues(
(value) => {
// 설정 변경 시 동작
},
(config) => config.enabled,
)
}의존성 체크리스트
plugin.ts의dependencies에 아래 ID를 누락 없이 선언합니다.settingsPluginIdsettingsSchemaPluginIdsettingsStorePluginId
- 설정 화면 노출만 필요하고 별도 저장이 없으면
settings-store없이도 가능하지만, 일반적으로는 함께 사용하는 것을 권장합니다. - 대부분
pnpm gen의플러그인 설정옵션으로 기본 구조가 자동 생성되므로, 위 내용은 커스텀 시 참고용으로 보시면 됩니다.
Last updated on