Skip to content

jsapi ​

go
import "github.com/tangoui/tango/jsapi"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package jsapi exposes Tango's system capabilities to the page: the window the page lives in, app information and control, clipboard, dialogs, notifications, shell, context menus and global shortcuts. The JS side is the Window, App, Clipboard, ... modules of @tangoui/runtime (window.tango.Window, ... in the injected IIFE); docs/runtime-api.md lists every function.

r := ipc.NewRouter()
app.AddBridgeScript(runtimejs.Script)
app.OnMessage(func(s *tango.Surface, m string) { r.HandleMessage(s, m) })
api := jsapi.Install(app, r, jsapi.Options{Allow: jsapi.ClipboardWrite | jsapi.Dialogs})

Install only registers ipc methods on r; feeding page messages to the router stays the app's job, as above.

Capabilities ​

Methods are grouped into capabilities (Cap). A page may only use the enabled ones; calling a method of a disabled capability fails with the error code "forbidden" (CodeForbidden). By default only Window (the calling window controls itself), Menu (context menus in the calling window) and Info (environment, theme, displays and app events, read only) are enabled; everything that reaches outside the window — the clipboard, dialogs, notifications, opening URLs or files, app-wide control, global shortcuts — must be turned on with Options.Allow.

Wire names ​

Methods are ipc calls named "tango:<module>.<function>" and events are named "tango:<module>.<event>", so they never collide with an app's own methods. Errors carry the codes of package ipc plus CodeForbidden, CodeUnsupported (the platform cannot do it) and CodeNotRunning (the app is quitting).

常量 ​

go
const (
	CodeForbidden   = "forbidden"   // the capability is not enabled in Options
	CodeUnsupported = "unsupported" // the platform cannot do it
	CodeNotRunning  = "not_running" // the app is not running (quitting)
)

Error codes added to the ones of package ipc.

go
const All = Window | Menu | Info | AppControl | ClipboardRead | ClipboardWrite |
	Dialogs | Notify | OpenURL | OpenPath | Hotkeys | WebAll

All is every capability defined by this package.

go
const Defaults = Window | Menu | Info

Defaults are the capabilities enabled unless Options.Deny removes them.

go
const Prefix = "tango:"

Prefix starts every method and event name of the runtime API.

类型 ​

API ​

go
type API struct {
	// contains filtered or unexported fields
}

API is the installed runtime API of one app.

func Install ​

go
func Install(app *tango.App, r *ipc.Router, opts Options) *API

Install registers the runtime API on r for app's pages and returns it. Call it once per app, before Run, so that a deep link or file that launches the app is kept until a page subscribes. Install listens to App.OnEvent, so launch events no longer wait for OnOpenURL / OnOpenFiles listeners registered after it: register those before Install.

func (*API) Allowed ​

go
func (api *API) Allowed(c Cap) bool

Allowed reports whether every capability in c is enabled.

func (*API) Broadcast ​

go
func (api *API) Broadcast(name string, data any)

Broadcast sends the event Prefix+name to every page that has talked to the API (or existed when it was installed).

func (*API) Emit ​

go
func (api *API) Emit(s *tango.Surface, name string, data any)

Emit sends the event Prefix+name to s's page.

func (*API) Handle ​

go
func (api *API) Handle(c Cap, name string, fn Handler)

Handle registers fn as the method Prefix+name, available when every capability in c is enabled (otherwise it fails with CodeForbidden). Other packages use it to add modules under the same switch board.

func (*API) InstallUpdater ​

go
func (api *API) InstallUpdater(u *updater.Updater, opts UpdaterOptions)

InstallUpdater exposes u to pages with the Updates capability:

tango:updater.check    () → release | null   {version, notes, pubDate, format, full, patch}
tango:updater.download () → {version, patched}   (after a check found a release)
tango:updater.restart  ()                       (UpdaterOptions.AllowRestart)
tango:updater.event    event: {kind, release?, patch?, done?, total?, error?}

The updater's OnEvent hook is wrapped so its events also reach every page as "tango:updater.event"; a hook set before stays called. Only one check or download runs at a time; a second call fails with code "busy".

Cap ​

go
type Cap uint64

Cap is a set of capabilities a page may use.

go
const (
	// Window lets a page control its own window: title, size, position,
	// state, always-on-top, zoom, reload, show/hide/focus/close; and
	// receive its window events. On by default.
	Window Cap = 1 << iota
	// Menu lets a page pop up context menus it describes, in its own
	// window. Only edit roles (copy, paste, ...) are accepted. On by
	// default.
	Menu
	// Info lets a page read the environment, the system theme and the
	// displays, and receive theme, display and app events (OpenURL,
	// OpenFiles, Reopen, second instance). On by default.
	Info
	// AppControl lets a page quit the app, hide/show it, set the dock
	// badge and progress and request attention.
	AppControl
	// ClipboardRead lets a page read the clipboard (text, image, files).
	ClipboardRead
	// ClipboardWrite lets a page write the clipboard (text, image, files).
	ClipboardWrite
	// Dialogs lets a page show message, open and save dialogs.
	Dialogs
	// Notify lets a page ask for notification permission, send
	// notifications and receive their clicks.
	Notify
	// OpenURL lets a page open URLs with the default handler; schemes are
	// still limited by shell.AllowSchemes (http, https, mailto by default).
	OpenURL
	// OpenPath lets a page open files and folders with their default app
	// and reveal them in the file manager.
	OpenPath
	// Hotkeys lets a page register system-wide shortcuts.
	Hotkeys
)
go
const (
	// WebFind lets a page search itself: find, findNext, findPrevious,
	// stopFind.
	WebFind Cap = 1 << (20 + iota)
	// WebCapture lets a page render itself as PDF or PNG (printToPDF,
	// screenshot).
	WebCapture
	// WebCookies lets a page read and change every cookie of the app's
	// WebView store, HttpOnly ones and other sites' included, and clear
	// website data.
	WebCookies
	// WebDownloads lets a page receive the progress of its downloads
	// ("web.download" events) and cancel them.
	WebDownloads
	// WebControl lets a page change its User-Agent.
	WebControl

	// WebAll is every Web capability.
	WebAll = WebFind | WebCapture | WebCookies | WebDownloads | WebControl
)

Web module: the calling page's WebView — find in page, PDF and PNG capture, cookies and website data, downloads, user agent. Every part is off by default. (The bits are kept clear of the iota list in jsapi.go.)

Permission requests, HTTP authentication, new windows and the navigation policy are decided in Go only (tango.App / Surface handlers); a page cannot answer them for itself.

go
const Updates Cap = 1 << 30

Updates lets a page drive the app's updater: check for a release, download it and restart into it, and receive the updater's progress events. Off by default; it needs both Options.Allow and a call to API.InstallUpdater. (The bit is kept clear of the iota list in jsapi.go.)

Handler ​

go
type Handler func(ctx context.Context, s *tango.Surface, args json.RawMessage) (any, error)

Handler serves one runtime API method. s is the calling surface (nil when the caller is not a surface of this app, which Handle-registered methods may accept or refuse). args is the raw JSON argument array.

Options ​

go
type Options struct {

	// Allow adds capabilities to Defaults.
	Allow Cap
	// Deny removes capabilities (from Defaults or Allow).
	Deny Cap
	// contains filtered or unexported fields
}

Options configures Install.

UpdaterOptions ​

go
type UpdaterOptions struct {
	// AllowRestart lets the page install a downloaded update and quit the
	// app into it ("updater.restart"). Without it the page can check and
	// download, and the app restarts when it decides to, applying the
	// payload OnDownloaded receives.
	AllowRestart bool
	// OnDownloaded, if set, receives each verified download.
	OnDownloaded func(*updater.Payload)
}

UpdaterOptions configures API.InstallUpdater.

以 MIT 许可发布