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