vendetta / examples / host_chrome / main.v
198 lines · 183 sloc · 6.5 KB · 77754ad9e6dc749afefba326ff89abc5ddd8c9c9
Raw
1import libvendetta
2import os
3import ui2
4
5// This example is a full host application with vendetta embedded as a widget.
6// The host owns the window, the GL surface, the layout and the event loop; it
7// forwards raw input into vendetta and folds vendetta's element into its own
8// tree. The sidebar on the right is host chrome: it drives vendetta through
9// its public API to prove the host, not the widget, decides what the app does.
10//
11// To run from the vendetta repo root:
12// v examples/embed_host/main.v
13// The example finds assets/video/test.mp4 relative to its own location.
14
15// find_test_video locates the test clip relative to the executable or the
16// working directory, so the example runs from any directory.
17fn find_test_video() string {
18 roots := [os.dir(os.executable()), '.']
19 for root in roots {
20 for candidate in ['assets/video/test.mp4', '../../assets/video/test.mp4'] {
21 path := os.join_path(root, candidate)
22 if os.exists(path) {
23 return os.abs_path(path)
24 }
25 }
26 }
27 return ''
28}
29
30// AppState is the host's own state. The player is a field, not a global: the
31// host could own several players, each mounted in its own panel.
32struct AppState {
33mut:
34 // Host-side state for the sidebar checkboxes.
35 overlay_checked bool = true
36 show_sidebar bool = true
37 // The embeddable player.
38 player &libvendetta.Player
39 video_path string
40}
41
42// main is the host. Everything here is the host's own window and loop; no
43// vendetta code touches the window directly.
44fn main() {
45 video_path := find_test_video()
46 if video_path == '' {
47 eprintln('no test video found (expected assets/video/test.mp4)')
48 exit(1)
49 }
50
51 // Build the embeddable player. The host picks the knobs: the controls float
52 // over the video, 30 second seeks, 5 per wheel notch.
53 cfg := libvendetta.Config{
54 overlay: true
55 seek_step: 30
56 volume_step: 5
57 }
58 mut app := &AppState{
59 player: libvendetta.new(cfg)
60 video_path: video_path
61 }
62
63 // Register the GL surface renderer once for the whole window and route by
64 // element id. Vendetta only answers for its own surface, so other GL
65 // elements in the host's tree keep working.
66 ui2.set_gl_surface_renderer(fn [mut app] (id string, frame ui2.Rect, scale f64, height f64) {
67 if id == libvendetta.video_surface_id {
68 app.player.render(id, frame, scale, height)
69 }
70 })
71
72 // Forward the input vendetta needs: pointer motion keeps its controls
73 // awake, right-click over the video pauses, the wheel steps volume.
74 ui2.on_pointer_move(fn [mut app] (x f64, y f64) {
75 app.player.pointer_moved(x, y)
76 })
77 ui2.on_mouse_button(fn [mut app] (button int, down bool, x f64, y f64) {
78 app.player.mouse_button(button, down, x, y)
79 })
80 ui2.on_wheel(fn [mut app] (dx f64, dy f64) {
81 app.player.wheel(dx, dy)
82 })
83
84 // Keyboard: space pauses, arrows seek and step volume, m mutes. q toggles
85 // the sidebar, which is host chrome the widget knows nothing about.
86 ui2.on_key(fn [mut app] (key string) {
87 if key == 'q' {
88 app.show_sidebar = !app.show_sidebar
89 ui2.request_refresh()
90 return
91 }
92 app.player.key(key)
93 })
94
95 // Queue the test video. libvendetta buffers the load until its GL surface
96 // exists, so this runs fine before the window opens.
97 app.player.load(app.video_path)
98
99 // Open the window. The build callback assembles the host's tree with
100 // vendetta's element folded into the left pane. It runs every frame, so
101 // it only reads state and builds elements.
102 ui2.run_window_with_options(ui2.WindowOptions{
103 title: 'Vendetta in a host app'
104 width: 1024
105 height: 640
106 refresh: .on_demand
107 }, fn [mut app] () ui2.Element {
108 return app.view()
109 }, fn [mut app] (event string) {
110 // Every element event arrives here. Host chrome first, vendetta's
111 // events forwarded: the vd_ prefix is the routing key.
112 match event {
113 'host_stop' {
114 app.player.stop()
115 }
116 'host_replay' {
117 app.player.load(app.video_path)
118 }
119 'host_overlay' {
120 app.overlay_checked = !app.overlay_checked
121 app.player.set_overlay(app.overlay_checked)
122 }
123 else {}
124 }
125 if event.starts_with('vd_') {
126 app.player.handle(event)
127 }
128 })
129
130 // The window is closed. Free vendetta's mpv context and render machinery;
131 // nothing here may touch the GL context any more.
132 app.player.destroy()
133}
134
135// view is the host's frame. Vendetta gets the left pane; the right sidebar is
136// host chrome with vendetta's state mirrored in host-side labels.
137fn (mut app AppState) view() ui2.Element {
138 // The window frame, in window coordinates.
139 b := ui2.bounds()
140 sidebar_w := if app.show_sidebar { 260.0 } else { 0.0 }
141 player_frame := ui2.rect(0, 0, b.width - sidebar_w, b.height)
142
143 // Vendetta builds its subtree into the frame the host gives it. It reads
144 // its own state (pos, volume, paused), so the host does nothing besides
145 // rebuilding the tree each frame; the widget updates itself.
146 player := app.player.build(player_frame)
147
148 mut children := [player]
149 if app.show_sidebar {
150 children << app.sidebar(b.width - sidebar_w, sidebar_w)
151 }
152 return ui2.view('host_root', b, ui2.BoxStyle{ transparent: true }, children)
153}
154
155// sidebar lays out the host's own controls. The buttons and checkboxes send
156// host_ events; the labels mirror vendetta's playback state so the host can
157// display what the widget is doing without reaching into its internals.
158fn (mut app AppState) sidebar(x f64, width f64) ui2.Element {
159 b := ui2.bounds()
160 frame := ui2.rect(x, 0, width, b.height)
161 label_style := ui2.TextStyle{
162 size: 13
163 color: 0xe2e8f0
164 }
165 button_box := ui2.BoxStyle{
166 bg: 0x3b82f6
167 radius: 6
168 }
169 button_style := ui2.TextStyle{
170 size: 14
171 color: 0xffffff
172 valign: .middle
173 align: .center
174 }
175 children := [
176 ui2.label('', 'Host sidebar', ui2.rect(16, 16, width - 32, 24), label_style),
177 ui2.checkbox('host_overlay', 'Floating controls', app.overlay_checked,
178 ui2.rect(16, 48, width - 32, 24), label_style),
179 ui2.button('host_replay', 'Replay video', ui2.rect(16, 80, width - 32, 28), button_box,
180 button_style),
181 ui2.button('host_stop', 'Stop', ui2.rect(16, 116, width - 32, 28), button_box,
182 button_style),
183 ui2.label('', 'pos: ${libvendetta.fmt_time(app.player.pos)} / ${libvendetta.fmt_time(app.player.duration)}',
184 ui2.rect(16, 156, width - 32, 24), label_style),
185 ui2.label('', 'volume: ${app.player.volume:3.0}${if app.player.muted {
186 ' (muted)'
187 } else {
188 ''
189 }}',
190 ui2.rect(16, 184, width - 32, 24), label_style),
191 ui2.label('', 'mode: ${if app.player.cfg.overlay { 'overlay' } else { 'docked' }}',
192 ui2.rect(16, 212, width - 32, 24), label_style),
193 ]
194 return ui2.view('host_sidebar', frame, ui2.BoxStyle{
195 bg: 0x0f172a
196 opacity: 0.95
197 }, children)
198}
199