fix(plugins): show active runtime compatibility in plugin settings
This commit is contained in:
1 parent
d8355b8656
commit
a853460fc3
10 files changed
+145
-3
No files matched your search
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"group": "fixes",
|
||||
"order": 0,
|
||||
"authors": ["cosimawei"],
|
||||
"zh": "在插件列表和设置中提示当前运行时不支持的插件设置。",
|
||||
"en": "Show which settings the active runtime does not support in the plugin list and settings dialog."
|
||||
}
|
||||
@@ -1212,6 +1212,7 @@ export const App: React.FC = () => {
|
||||
locale={locale}
|
||||
appearance={theme}
|
||||
safeMode={state.safe_mode}
|
||||
runtimeCommit={state.active_runtime?.commit}
|
||||
onRefresh={refresh}
|
||||
onError={fail}
|
||||
onThemeChanged={() => setThemeRevision((r) => r + 1)}
|
||||
|
||||
@@ -27,6 +27,7 @@ interface Props {
|
||||
locale: Locale
|
||||
appearance: 'light' | 'dark'
|
||||
safeMode?: boolean
|
||||
runtimeCommit?: string
|
||||
onRefresh: () => void
|
||||
onError: (title: string, e: unknown) => void
|
||||
onThemeChanged?: () => void
|
||||
@@ -46,7 +47,7 @@ function hasAppSettings(p: PluginInfo): boolean {
|
||||
|
||||
/** Top-level Plugins & Themes management page. */
|
||||
export const PluginsPage: React.FC<Props> = ({
|
||||
locale, appearance, safeMode, onRefresh, onError, onThemeChanged, onLocaleSwitch,
|
||||
locale, appearance, safeMode, runtimeCommit, onRefresh, onError, onThemeChanged, onLocaleSwitch,
|
||||
}) => {
|
||||
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
||||
const [activeTab, setActiveTab] = useState<'installed' | 'market'>('installed')
|
||||
@@ -61,6 +62,7 @@ export const PluginsPage: React.FC<Props> = ({
|
||||
const [approveChecked, setApproveChecked] = useState(false)
|
||||
const approveTargetIDRef = useRef('')
|
||||
const [settingsTarget, setSettingsTarget] = useState<PluginInfo | null>(null)
|
||||
const settingsRuntimeWarning = plugins.find((p) => p.id === settingsTarget?.id)?.runtime_warning
|
||||
const [settingsValues, setSettingsValues] = useState<Record<string, unknown>>({})
|
||||
const [logTarget, setLogTarget] = useState<{ id: string; text: string } | null>(null)
|
||||
const [removeTarget, setRemoveTarget] = useState<PluginInfo | null>(null)
|
||||
@@ -151,7 +153,7 @@ export const PluginsPage: React.FC<Props> = ({
|
||||
onThemeChanged?.()
|
||||
}
|
||||
|
||||
useEffect(() => { void reload() }, [safeMode])
|
||||
useEffect(() => { void reload() }, [safeMode, runtimeCommit, locale])
|
||||
|
||||
useEffect(() => { if (activeTab === 'market') setMarketOpened(true) }, [activeTab])
|
||||
|
||||
@@ -429,6 +431,12 @@ export const PluginsPage: React.FC<Props> = ({
|
||||
<span>{alert}</span>
|
||||
</div>
|
||||
)}
|
||||
{p.runtime_warning && (
|
||||
<div className="row-sub plugin-row-alert">
|
||||
<AlertTriangle size={12} aria-hidden="true" />
|
||||
<span>{p.runtime_warning}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="row-right">
|
||||
{!isBuiltin && (
|
||||
@@ -868,6 +876,12 @@ export const PluginsPage: React.FC<Props> = ({
|
||||
onClose={() => setSettingsTarget(null)}
|
||||
>
|
||||
<div className="dialog-body">
|
||||
{settingsRuntimeWarning && (
|
||||
<div className="notice notice-warn">
|
||||
<AlertTriangle size={15} className="notice-icon" aria-hidden="true" />
|
||||
<span>{settingsRuntimeWarning}</span>
|
||||
</div>
|
||||
)}
|
||||
<SchemaForm
|
||||
locale={locale}
|
||||
schema={settingsTarget.settings}
|
||||
|
||||
@@ -2305,6 +2305,7 @@ export namespace plugin {
|
||||
in_use: boolean;
|
||||
error?: string;
|
||||
degraded_reason?: string;
|
||||
runtime_warning?: string;
|
||||
permissions?: PermissionSpec[];
|
||||
approved_permissions?: string[];
|
||||
needs_approval: boolean;
|
||||
@@ -2343,6 +2344,7 @@ export namespace plugin {
|
||||
this.in_use = source["in_use"];
|
||||
this.error = source["error"];
|
||||
this.degraded_reason = source["degraded_reason"];
|
||||
this.runtime_warning = source["runtime_warning"];
|
||||
this.permissions = this.convertValues(source["permissions"], PermissionSpec);
|
||||
this.approved_permissions = source["approved_permissions"];
|
||||
this.needs_approval = source["needs_approval"];
|
||||
|
||||
@@ -632,11 +632,36 @@ func buildHostFacts(rep host.Report, rtCaps []string) map[string]any {
|
||||
func (a *App) ListPlugins() []plugin.PluginInfo {
|
||||
a.mu.Lock()
|
||||
mgr := a.pluginMgr
|
||||
cfg := *a.cfg
|
||||
a.mu.Unlock()
|
||||
if mgr == nil {
|
||||
return nil
|
||||
}
|
||||
return mgr.LocalizePluginInfos(mgr.Registry().ListVisible())
|
||||
reg := mgr.Registry()
|
||||
infos := mgr.LocalizePluginInfos(reg.ListVisible())
|
||||
active, _ := rt.ResolveActive(&cfg)
|
||||
if active == nil {
|
||||
return infos
|
||||
}
|
||||
caps := active.Manifest.EffectiveCapabilities()
|
||||
locale := i18n.EffectiveLocale(cfg.Locale)
|
||||
for i := range infos {
|
||||
info := &infos[i]
|
||||
if info.State != plugin.StateActive && info.State != plugin.StateDegraded {
|
||||
continue
|
||||
}
|
||||
manifest, ok := reg.ManifestOf(info.ID)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
manifest.Settings = info.Settings
|
||||
if settings := plugin.RuntimeUnavailableSettings(manifest, caps, locale); len(settings) > 0 {
|
||||
info.RuntimeWarning = i18n.M(locale, "plugin.runtime_settings_skipped", i18n.Vars{
|
||||
"settings": strings.Join(settings, ", "),
|
||||
})
|
||||
}
|
||||
}
|
||||
return infos
|
||||
}
|
||||
|
||||
func (a *App) InstallPluginArchive(archivePath string) (*plugin.PluginInfo, error) {
|
||||
|
||||
@@ -3,6 +3,8 @@ package plugin
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/i18n"
|
||||
)
|
||||
|
||||
func TestRuleEnvironmentUsesVisiblePluginSettings(t *testing.T) {
|
||||
@@ -32,3 +34,40 @@ func TestRuleEnvironmentUsesVisiblePluginSettings(t *testing.T) {
|
||||
t.Fatalf("unexpected environment: %#v", intent.Env)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeUnavailableSettings(t *testing.T) {
|
||||
const capability = "window-release-shortcut"
|
||||
manifest := Manifest{
|
||||
ID: "local.capture-shortcut",
|
||||
Settings: &SettingsSchema{Properties: map[string]SettingProperty{
|
||||
"releaseKeys": {Title: LocalizedText{ZhCN: "释放快捷键"}},
|
||||
"releaseOnLeave": {Title: LocalizedText{ZhCN: "鼠标移出时释放"}},
|
||||
"private": {Secret: true},
|
||||
}},
|
||||
Contributes: Contributes{RuleHooks: []RuleHook{{
|
||||
Event: EventVMBeforeStart,
|
||||
Conditions: []RuleCondition{{Fact: "runtime.capabilities", Operator: "contains", Value: capability}},
|
||||
Actions: []RuleAction{{Kind: "set_env", EnvFromSettings: map[string]string{
|
||||
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "releaseKeys",
|
||||
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "releaseOnLeave",
|
||||
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_PRIVATE": "private",
|
||||
}}},
|
||||
}}},
|
||||
}
|
||||
locale := i18n.Locale("zh-CN")
|
||||
if got := RuntimeUnavailableSettings(manifest, nil, locale); !reflect.DeepEqual(got, []string{"释放快捷键", "鼠标移出时释放"}) {
|
||||
t.Fatalf("unexpected settings for incompatible runtime: %v", got)
|
||||
}
|
||||
if got := RuntimeUnavailableSettings(manifest, []string{capability}, locale); len(got) != 0 {
|
||||
t.Fatalf("compatible runtime should have no warning: %v", got)
|
||||
}
|
||||
manifest.Contributes.RuleHooks = append(manifest.Contributes.RuleHooks, RuleHook{
|
||||
Event: EventVMBeforeStart,
|
||||
Actions: []RuleAction{{Kind: "set_env", EnvFromSettings: map[string]string{
|
||||
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "releaseKeys",
|
||||
}}},
|
||||
})
|
||||
if got := RuntimeUnavailableSettings(manifest, nil, locale); !reflect.DeepEqual(got, []string{"鼠标移出时释放"}) {
|
||||
t.Fatalf("settings covered by a fallback should have no warning: %v", got)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,10 @@ package plugin
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/i18n"
|
||||
)
|
||||
|
||||
// RuleCondition is a declarative, non-Turing-complete predicate over host, VM,
|
||||
@@ -145,6 +148,54 @@ func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHoo
|
||||
}
|
||||
}
|
||||
|
||||
// RuntimeUnavailableSettings reports settings guarded by rules that depend only
|
||||
// on runtime capabilities. VM-specific rules cannot be assessed on the plugin page.
|
||||
func RuntimeUnavailableSettings(m Manifest, caps []string, loc i18n.Locale) []string {
|
||||
if m.Settings == nil {
|
||||
return nil
|
||||
}
|
||||
facts := map[string]any{"runtime.capabilities": caps}
|
||||
unavailable := make(map[string]bool)
|
||||
available := make(map[string]bool)
|
||||
for _, rule := range m.Contributes.RuleHooks {
|
||||
if rule.Event != EventVMBeforeStart || slices.ContainsFunc(rule.Conditions, func(c RuleCondition) bool {
|
||||
return c.Fact != "runtime.capabilities"
|
||||
}) {
|
||||
continue
|
||||
}
|
||||
matches := EvaluateRuleConditions(rule.Conditions, facts)
|
||||
for _, action := range rule.Actions {
|
||||
if action.Kind != "set_env" {
|
||||
continue
|
||||
}
|
||||
for envKey, settingKey := range action.EnvFromSettings {
|
||||
prop, ok := m.Settings.Properties[settingKey]
|
||||
if !ok || prop.Secret || !ValidateEnvKey(m.ID, envKey) {
|
||||
continue
|
||||
}
|
||||
if matches {
|
||||
available[settingKey] = true
|
||||
} else {
|
||||
unavailable[settingKey] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var labels []string
|
||||
for key := range unavailable {
|
||||
if available[key] {
|
||||
continue
|
||||
}
|
||||
label := m.Settings.Properties[key].Title.Get(string(loc))
|
||||
if label == "" {
|
||||
label = key
|
||||
}
|
||||
labels = append(labels, label)
|
||||
}
|
||||
slices.Sort(labels)
|
||||
return slices.Compact(labels)
|
||||
}
|
||||
|
||||
func (r *Registry) applyRuleEnvSettings(intent *StartIntent, rule RuleHook, schema *SettingsSchema, vmName string) {
|
||||
if schema == nil || r.ruleSettings == nil {
|
||||
return
|
||||
|
||||
@@ -773,6 +773,7 @@ type PluginInfo struct {
|
||||
InUse bool `json:"in_use"`
|
||||
Error string `json:"error,omitempty"`
|
||||
DegradedReason string `json:"degraded_reason,omitempty"`
|
||||
RuntimeWarning string `json:"runtime_warning,omitempty"`
|
||||
Permissions []PermissionSpec `json:"permissions,omitempty"`
|
||||
ApprovedPermissions []string `json:"approved_permissions,omitempty"`
|
||||
NeedsApproval bool `json:"needs_approval"`
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"plugin.runtime_settings_skipped": "The current runtime cannot apply these settings: {settings}. Switch to a runtime that supports them.",
|
||||
"network.log.create_network_prepare": "Preparing network dependencies and checking the uplink",
|
||||
"network.log.mode_applied": "Network mode applied: {mode}",
|
||||
"network.log.uplink_switched": "Bridged uplink changed. The main adapter was notified to reconnect and renew DHCP",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"plugin.runtime_settings_skipped": "当前运行时无法应用以下设置:{settings}。切换到支持这些功能的运行时即可使用。",
|
||||
"network.log.create_network_prepare": "正在准备网络依赖并检查出口",
|
||||
"network.log.mode_applied": "网络模式已应用:{mode}",
|
||||
"network.log.uplink_switched": "桥接出口已更换,已通知主网卡重新连接并续租 DHCP",
|
||||
|
||||
Reference in new issue
Block a user