IDesktopPlugin.kt 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package org.mjdev.desktop.plugins
  2. import androidx.compose.runtime.Composable
  3. import org.mjdev.desktop.context.IDesktopContext
  4. import org.mjdev.desktop.plugins.remote.RemoteDocument
  5. import org.mjdev.desktop.plugins.remote.RemoteVariables
  6. /**
  7. * Contract implemented by every desktop widget plugin.
  8. *
  9. * A plugin describes its look as a [RemoteDocument] (data, not code) and feeds it live values
  10. * through [variables]; the desktop renders both with its own player. External plugins are jars
  11. * that register an implementation in `META-INF/services/org.mjdev.desktop.plugins.IDesktopPlugin`
  12. * and carry a `plugin.json` ([PluginMetadata]) at the jar root. One plugin per jar.
  13. */
  14. interface IDesktopPlugin {
  15. /** The widget layout. Read once when the plugin is enabled. */
  16. val document: RemoteDocument
  17. /** Period in milliseconds between two calls of [variables]. */
  18. val refreshMs: Long
  19. get() = PluginDefaults.DEFAULT_REFRESH_MS
  20. /**
  21. * Optional interactive UI. When not null the desktop draws it instead of the [document] (the
  22. * document then only supplies size and anchor); used by built-in widgets that need buttons.
  23. */
  24. val content: (@Composable (IDesktopContext) -> Unit)?
  25. get() = null
  26. /** Produces the current values the [document] binds to. Called off the UI thread. */
  27. fun variables(context: IDesktopContext): RemoteVariables
  28. /** Called after the user enabled the plugin. */
  29. fun onEnabled() {}
  30. /** Called after the user disabled the plugin; release resources here. */
  31. fun onDisabled() {}
  32. }