Skip to main content

Module plugin

Module plugin 

Source
Expand description

Plugin system for extending Revue applications

Plugins provide a modular way to extend app functionality with lifecycle hooks, custom styles, and shared state.

§Features

FeatureDescription
Lifecycle HooksInit, tick, event, shutdown callbacks
Shared StateGlobal state accessible to all plugins
Custom StylesAdd CSS variables and rules
Event HandlingSubscribe to and emit events
Async SupportAsync lifecycle operations

§Quick Start

§Create a Plugin

use revue::plugin::{Plugin, PluginContext};
use std::time::Duration;

struct LoggerPlugin {
    event_count: usize,
}

impl Plugin for LoggerPlugin {
    fn name(&self) -> &str { "logger" }

    fn on_init(&mut self, ctx: &mut PluginContext) -> revue::Result<()> {
        println!("[{}] Plugin initialized", self.name());
        Ok(())
    }

    fn on_tick(&mut self, _ctx: &mut PluginContext, delta: Duration) -> revue::Result<()> {
        self.event_count += 1;
        Ok(())
    }
}

§Register a Plugin

use revue::App;

let app = App::builder()
    .plugin(LoggerPlugin { event_count: 0 })
    .build();

§Lifecycle Hooks

HookWhen CalledUse Case
on_initAfter app creationSetup, initialization
on_mountWhen view is mountedUI setup, subscriptions
on_tickEvery frameUpdate logic, animation
on_eventOn input eventEvent handling
on_shutdownBefore app exitCleanup, saving

§Plugin Context

The PluginContext provides access to:

impl Plugin for MyPlugin {
    fn on_init(&mut self, ctx: &mut PluginContext) -> Result<()> {
        // Access app state
        let state = ctx.state();

        // Emit events
        ctx.emit("my_event", data);

        // Subscribe to events
        ctx.subscribe("other_event", |data| {
            println!("Got event: {:?}", data);
        });

        Ok(())
    }
}

§Built-in Plugins

§LoggerPlugin

Logs app lifecycle events and errors:

use revue::plugin::LoggerPlugin;

let app = App::builder()
    .plugin(LoggerPlugin::new())
    .build();

§PerformancePlugin

Tracks FPS, frame time, and memory usage:

use revue::plugin::PerformancePlugin;

let app = App::builder()
    .plugin(PerformancePlugin::new())
    .build();

§Plugin Registry

Access and manage plugins:

use revue::plugin::PluginRegistry;

// Get plugin by name
if let Some(logger) = registry.get("logger") {
    // Use plugin...
}

// List all plugins
for name in registry.plugin_names() {
    println!("{}", name);
}

Structs§

LoggerPlugin
Simple logging plugin for debugging
PerformancePlugin
Performance monitoring plugin
PluginContext
Context provided to plugins during lifecycle hooks
PluginRegistry
Registry for managing plugins

Traits§

Plugin
Plugin trait for extending Revue applications