import libvendetta import os import ui2 // This example is a full host application with vendetta embedded as a widget. // The host owns the window, the GL surface, the layout and the event loop; it // forwards raw input into vendetta and folds vendetta's element into its own // tree. The sidebar on the right is host chrome: it drives vendetta through // its public API to prove the host, not the widget, decides what the app does. // // To run from the vendetta repo root: // v examples/host_chrome/main.v // The example finds assets/video/test.mp4 relative to the executable or the // working directory, and exits with a message when the clip is missing. // find_test_video locates the test clip relative to the executable or the // working directory, so the example runs from any directory. fn find_test_video() string { roots := [os.dir(os.executable()), '.'] for root in roots { for candidate in ['assets/video/test.mp4', '../../assets/video/test.mp4'] { path := os.join_path(root, candidate) if os.exists(path) { return os.abs_path(path) } } } return '' } // AppState is the host's own state. The player is a field, not a global: the // host could own several players, each mounted in its own panel. struct AppState { mut: // Host-side state for the sidebar checkboxes. overlay_checked bool = true show_sidebar bool = true // The embeddable player. player &libvendetta.Player video_path string } // main is the host. Everything here is the host's own window and loop; no // vendetta code touches the window directly. fn main() { video_path := find_test_video() if video_path == '' { eprintln('no test video found (expected assets/video/test.mp4)') exit(1) } // Build the embeddable player. The host picks the knobs: the controls float // over the video, 30 second seeks, 5 per wheel notch. cfg := libvendetta.Config{ overlay: true seek_step: 30 volume_step: 5 } mut app := &AppState{ player: libvendetta.new(cfg) video_path: video_path } // Register the GL surface renderer once for the whole window and route by // element id. Vendetta only answers for its own surface, so other GL // elements in the host's tree keep working. ui2.set_gl_surface_renderer(fn [mut app] (id string, frame ui2.Rect, scale f64, height f64) { if id == libvendetta.video_surface_id { app.player.render(id, frame, scale, height) } }) // Forward the input vendetta needs: pointer motion keeps its controls // awake, right-click over the video pauses, the wheel steps volume. ui2.on_pointer_move(fn [mut app] (x f64, y f64) { app.player.pointer_moved(x, y) }) ui2.on_mouse_button(fn [mut app] (button int, down bool, x f64, y f64) { app.player.mouse_button(button, down, x, y) }) ui2.on_wheel(fn [mut app] (dx f64, dy f64) { app.player.wheel(dx, dy) }) // Keyboard: space pauses, arrows seek and step volume, m mutes. q toggles // the sidebar, which is host chrome the widget knows nothing about. ui2.on_key(fn [mut app] (key string) { if key == 'q' { app.show_sidebar = !app.show_sidebar ui2.request_refresh() return } app.player.key(key) }) // Queue the test video. libvendetta buffers the load until its GL surface // exists, so this runs fine before the window opens. app.player.load(app.video_path) // Open the window. The build callback assembles the host's tree with // vendetta's element folded into the left pane. It runs every frame, so // it only reads state and builds elements. ui2.run_window_with_options(ui2.WindowOptions{ title: 'Vendetta in a host app' width: 1024 height: 640 refresh: .on_demand }, fn [mut app] () ui2.Element { return app.view() }, fn [mut app] (event string) { // Every element event arrives here. Host chrome first, vendetta's // events forwarded: the vd_ prefix is the routing key. match event { 'host_stop' { app.player.stop() } 'host_replay' { app.player.load(app.video_path) } 'host_overlay' { app.overlay_checked = !app.overlay_checked app.player.set_overlay(app.overlay_checked) } else {} } if event.starts_with('vd_') { app.player.handle(event) } }) // The window is closed. Free vendetta's mpv context and render machinery; // nothing here may touch the GL context any more. app.player.destroy() } // view is the host's frame. Vendetta gets the left pane; the right sidebar is // host chrome with vendetta's state mirrored in host-side labels. fn (mut app AppState) view() ui2.Element { // The window frame, in window coordinates. b := ui2.bounds() sidebar_w := if app.show_sidebar { 260.0 } else { 0.0 } player_frame := ui2.rect(0, 0, b.width - sidebar_w, b.height) // Vendetta builds its subtree into the frame the host gives it. It reads // its own state (pos, volume, paused), so the host does nothing besides // rebuilding the tree each frame; the widget updates itself. player := app.player.build(player_frame) mut children := [player] if app.show_sidebar { children << app.sidebar(b.width - sidebar_w, sidebar_w) } return ui2.view('host_root', b, ui2.BoxStyle{ transparent: true }, children) } // sidebar lays out the host's own controls. The buttons and checkboxes send // host_ events; the labels mirror vendetta's playback state so the host can // display what the widget is doing without reaching into its internals. fn (mut app AppState) sidebar(x f64, width f64) ui2.Element { b := ui2.bounds() frame := ui2.rect(x, 0, width, b.height) label_style := ui2.TextStyle{ size: 13 color: 0xe2e8f0 } button_box := ui2.BoxStyle{ bg: 0x3b82f6 radius: 6 } button_style := ui2.TextStyle{ size: 14 color: 0xffffff valign: .middle align: .center } children := [ ui2.label('', 'Host sidebar', ui2.rect(16, 16, width - 32, 24), label_style), ui2.checkbox('host_overlay', 'Floating controls', app.overlay_checked, ui2.rect(16, 48, width - 32, 24), label_style), ui2.button('host_replay', 'Replay video', ui2.rect(16, 80, width - 32, 28), button_box, button_style), ui2.button('host_stop', 'Stop', ui2.rect(16, 116, width - 32, 28), button_box, button_style), ui2.label('', 'pos: ${libvendetta.fmt_time(app.player.pos)} / ${libvendetta.fmt_time(app.player.duration)}', ui2.rect(16, 156, width - 32, 24), label_style), ui2.label('', 'volume: ${app.player.volume:3.0}${if app.player.muted { ' (muted)' } else { '' }}', ui2.rect(16, 184, width - 32, 24), label_style), ui2.label('', 'mode: ${if app.player.cfg.overlay { 'overlay' } else { 'docked' }}', ui2.rect(16, 212, width - 32, 24), label_style), ] return ui2.view('host_sidebar', frame, ui2.BoxStyle{ bg: 0x0f172a opacity: 0.95 }, children) }