Files
Reims-Go/internal/plugin/builtin/app_window.go
T

81 lines
2.8 KiB
Go

package builtin
import (
"context"
"errors"
"fmt"
"runtime"
"strings"
"github.com/Nwflower/reims-go/internal/appwindow"
"github.com/Nwflower/reims-go/internal/guesttools"
"github.com/Nwflower/reims-go/internal/plugin"
)
// IDAppWindow is the optional built-in extension for macOS application windows.
const IDAppWindow = "builtin.macos.app-window"
func defAppWindow(ver string, cb HostCallbacks, desiredFn func(string, bool) bool) plugin.PluginDef {
const commandID = "open"
title := plugin.LocalizedText{ZhCN: "打开独立应用窗口", EnUS: "Open app in separate window"}
return plugin.PluginDef{
Manifest: plugin.Manifest{
Schema: 1,
ID: IDAppWindow,
Version: ver,
Name: plugin.LocalizedText{ZhCN: "macOS 应用窗口", EnUS: "macOS App Windows"},
Description: plugin.LocalizedText{
ZhCN: "在 Windows 桌面上显示和操作当前 macOS 应用窗口",
EnUS: "View and control the active macOS app in a separate Windows window",
},
Publisher: "Reims-Go",
License: "Apache-2.0",
Platforms: []string{"windows-x64"},
Requires: []plugin.ServiceRequirement{
{Service: ServiceGuestToolsTransport, Version: "^1.0.0"},
{Service: ServiceComputerUse, Version: "^1.0.0"},
},
Permissions: []string{"vm:screen", "qmp:write", "ui:slot"},
},
Source: plugin.SourceBuiltin,
Trust: plugin.TrustOfficial,
Desired: desiredFn(IDAppWindow, false),
Activate: func(_ context.Context, sc *plugin.Scope) error {
// The CLI uses this definition to manage plugin state. Only the
// supported desktop host can expose the native window command.
if cb.OpenAppWindow == nil || runtime.GOOS != "windows" || runtime.GOARCH != "amd64" {
return nil
}
rawTools, _ := sc.Lookup(ServiceGuestToolsTransport)
tools, ok := rawTools.(*guesttools.Server)
if !ok || tools == nil {
return fmt.Errorf("invalid %q service instance", ServiceGuestToolsTransport)
}
rawConsole, _ := sc.Lookup(ServiceComputerUse)
console, ok := rawConsole.(ComputerUseService)
if !ok || console == nil {
return fmt.Errorf("invalid %q service instance", ServiceComputerUse)
}
sessions := new(appwindow.Sessions)
sc.OnDispose(sessions.Close)
sc.Registry().RegisterCommand(sc, plugin.CommandContribution{ID: commandID, Title: title},
func(ctx context.Context, args map[string]any) (any, error) {
if sc.Disposed() {
return nil, appwindow.ErrSessionsClosed
}
name, _ := args["vm"].(string)
name = strings.TrimSpace(name)
if name == "" {
return nil, errors.New("a VM name is required")
}
return nil, cb.OpenAppWindow(ctx, sessions, tools, console, name)
})
sc.Registry().RegisterMenu(sc, plugin.MenuContribution{
ID: commandID, Slot: "vm.toolbar", Command: commandID, Title: title, When: "vm.macos.running",
})
return nil
},
}
}