Skip to content
playactor
Esc
↑↓navigate↵open⌘Jpreview
On this page

Native half

When a plugin needs Rust, it ships its own Tauri plugin crate, compiled into the app and called through generated bindings.

A plugin that needs more than HTTPS (a peer-to-peer protocol, a long-running session, the file system) ships a native half: a Tauri plugin crate next to its TypeScript. The Soulseek plugin is the working example.

  • plugins/soulseek/
    • native/
      • Cargo.toml
      • build.rs one allow-<command> permission per command
      • permissions/default.toml
      • src/lib.rs init, bindings, export_bindings
    • src/
      • manifest.ts native: 'soulseek'
      • native.ts generated bindings
      • index.ts

The contract

Every native half implements three commands the host calls itself:

Command Called
start Before activate. The half stays idle until then.
stop When the plugin is disabled or the app quits.
status For the account row: whether it is signed in, and as whom.

Errors are a tagged enum ({ kind: string }). A vpnDown error means the vpn guard is down: the plugin waits for it, as it does when the host sees the tunnel down. Any other kind fails the plugin.

Wiring it in

Name it in the manifest

defineManifest({
  id: 'soulseek',
  name: 'Soulseek',
  native: 'soulseek',
  services: {
    soulseek: {
      title: 'Soulseek',
      guard: 'vpn',
      account: native({
        username: field.text({ title: 'Username' }),
        password: field.password({ title: 'Password' }),
      }),
    },
  },
  preferences: {},
})

native(…) account values go to the keychain for the crate to read. Plugin TypeScript never sees them.

guard: 'vpn' keeps the whole plugin off while the VPN is down: start isn’t called, and a drop stops it. It starts again when the VPN is back.

The NativeHost the crate gets holds its keychain entries and the VPN tunnel, nothing else. Folders it reads or shares come from the user through a field.folders preference that the TypeScript half passes on.

Compile it into the app

Add the crate to apps/desktop/src-tauri/Cargo.toml, register it in src-tauri/src/lib.rs with .plugin(playactor_plugin_soulseek::init(native_host::for_plugin("soulseek"))), add "soulseek:default" to capabilities/default.json, and call its export_bindings from src-tauri/src/bindings.rs.

Generate the bindings

pnpm desktop:bindings

This writes the crate’s tauri-specta commands into the plugin’s src/native.ts.

Hand them to definePlugin

import { commands } from './native'

export default definePlugin(manifest, {
  native: commands,
  activate(api) {
    const configure = () => {
      const { uploadSlots, sharedFolders } = api.data.preferences.get()
      void api.native.configure({ uploadSlots, sharedFolders: [...sharedFolders] })
    }
    configure()
    api.onDispose(api.data.preferences.subscribe(configure))
  },
})

api.native is the bindings without start and stop, which belong to the host. Its type is inferred from native.

A page or component reaches the same handle with usePlugin(manifest, schemas, commands); the third argument only types api.native (the release after 0.1.45).

Was this page helpful?