me

/

vendetta Public
17 commits 0 issues 0 pull requests 1 contributor Discussions Projects Snippets LOC history Platform CI

fix findings: frame leak, option replies, hit-tests

diviocity committed last Oct 31 · e566477c350a80f9666b93ab0d991ad14f06df91 View patch
Unverified signature 16 files changed +258 -109
assets/fonts/README.md +3 -3

@@ -11,9 +11,9 @@ and Noto Emoji 3.002 from

1111[google/fonts](https://github.com/google/fonts/tree/main/ofl/notoemoji).

1212

1313The custom renderer draws with these files unless the application overrides them

14-(see "Fonts and text sizes" in the top-level README), so a `gg` window looks the

15-same on Linux, the BSDs, Android, and macOS or Windows built with

16-`-d ui2_custom_rendering`.

14+(see "Fonts and text sizes" in the ui2 README, which ships with the ui2 module),

15+so a `gg` window looks the same on Linux, the BSDs, Android, and macOS or Windows

16+built with `-d ui2_custom_rendering`.

1717

1818`RobotoMono-Regular.ttf` has to keep that name and stay in this directory: gg

1919finds its mono face by rewriting `-Regular` to `Mono-Regular` in the path it was

examples/embed_host/README.md +3 -0

@@ -4,6 +4,9 @@ The minimal embed: one window, the widget, and the forwarding the widget

44needs. Every control — the hover control box, the seek bar, volume — is the

55widget's own; this file adds no chrome of its own.

66

7+The repository does not ship a clip: put any video at `assets/video/test.mp4`

8+first. Without one the example exits with a message.

9+

710 cd ~/Projects/vendetta

811 v run examples/embed_host/main.v

912

examples/embed_host/main.v +7 -0

@@ -1,10 +1,17 @@

11import libvendetta

2+import os

23import ui2

34

45// A browser-style <video> element: the whole host is one window with

56// vendetta's player filling it. Hover shows the controls, and the buttons and

67// sliders are vendetta's own elements — their clicks arrive here as vd_ events.

78fn main() {

9+ // The repository ships no clip. Without one the window would stay empty, so

10+ // say what to provide and stop.

11+ if !os.exists('assets/video/test.mp4') {

12+ eprintln('no test video found (expected assets/video/test.mp4 in the working directory)')

13+ exit(1)

14+ }

815 mut p := libvendetta.new(libvendetta.Config{})

916 p.load('assets/video/test.mp4')

1017

examples/host_chrome/README.md +4 -2

@@ -11,8 +11,10 @@ plus a set of methods the host calls.

1111v examples/host_chrome/main.v

1212```

1313

14-The example locates `assets/video/test.mp4` relative to its own file, so it

15-runs from any directory. It queues that clip at startup.

14+The example looks for `assets/video/test.mp4` next to the executable and in the

15+working directory, so it runs from either. The repository does not ship that

16+clip: put any video there first, or the example exits with a message. It queues

17+the clip at startup.

1618

1719## What to try

1820

examples/host_chrome/main.v +3 -2

@@ -9,8 +9,9 @@ import ui2

99// its public API to prove the host, not the widget, decides what the app does.

1010//

1111// 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.

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.

1415

1516// find_test_video locates the test clip relative to the executable or the

1617// working directory, so the example runs from any directory.

examples/host_proto/main.v +7 -0

@@ -3,6 +3,9 @@

33// widget's input scope between its frame and the whole window.

44//

55// v examples/host_proto/main.v [docked]

6+//

7+// It plays assets/video/test.mp4 from the working directory. The repository

8+// ships no clip, so put one there first.

69import libvendetta

710import os

811import ui2

@@ -76,6 +79,10 @@ fn (mut h Host) handle(event string) {

7679}

7780

7881fn main() {

82+ if !os.exists('assets/video/test.mp4') {

83+ eprintln('no test video found (expected assets/video/test.mp4 in the working directory)')

84+ exit(1)

85+ }

7986 mut player := libvendetta.new(libvendetta.Config{})

8087 player.load('assets/video/test.mp4')

8188 if os.args.contains('docked') {

libvendetta/draw.v +3 -2

@@ -71,8 +71,9 @@ $if android {

7171// which never calls the gl surface renderer the video arrives through;

7272// without darwin_sokol_glcore33, macOS defaults sokol to Metal, which

7373// cannot wrap the GL textures mpv draws into. Linux takes the custom

74-// renderer without a define, and Windows builds pass both defines the same

75-// way the macOS command does.

74+// renderer without a define. Windows keeps sokol's default GL backend, so its

75+// build passes only ui2_custom_rendering: darwin_sokol_glcore33 picks GL over

76+// Metal on macOS and means nothing there.

7677$if macos && !ui2_custom_rendering ? {

7778 $compile_error("the macOS build needs -d ui2_custom_rendering: vendetta's video surface reaches ui2's custom renderer, and macOS defaults ui2 to its AppKit backend")

7879}

libvendetta/mpv/mpv.v +7 -0

@@ -104,6 +104,7 @@ pub const format_double = 5

104104

105105pub const event_none = 0

106106pub const event_get_property_reply = 3

107+pub const event_set_property_reply = 4

107108pub const event_log_message = 2

108109pub const event_shutdown = 1

109110pub const event_start_file = 6

@@ -560,6 +561,12 @@ pub fn event_reply_userdata(ev voidptr) u64 {

560561 return unsafe { *(&u64(u64(ev) + 8)) }

561562}

562563

564+// event_error reads mpv_event.error, the status of a reply: 0 or more on

565+// success, a negative mpv error code otherwise. It sits at offset 4.

566+pub fn event_error(ev voidptr) i32 {

567+ return unsafe { *(&i32(u64(ev) + 4)) }

568+}

569+

563570// event_id reads mpv_event.event_id, the first field of every event.

564571pub fn event_id(ev voidptr) i32 {

565572 return unsafe { *(&i32(ev)) }

src/main.v +5 -2

@@ -366,8 +366,8 @@ fn main() {

366366 player.load_or_prompt(ev.paths)

367367 }

368368 })

369- ui2.on_wheel(fn [mut player] (x f64, dy f64) {

370- player.wheel_volume(x, dy)

369+ ui2.on_wheel(fn [mut player] (_ f64, dy f64) {

370+ player.wheel_volume(dy)

371371 })

372372 ui2.on_mouse_button(fn [mut player] (button int, down bool, x f64, y f64) {

373373 player.mouse_button(button, down, x, y)

@@ -476,5 +476,8 @@ fn main() {

476476 if player.thumb_ctx != unsafe { nil } {

477477 mpv.destroy(player.thumb_ctx)

478478 }

479+ // The thumbnail instance is gone, so no capture can still write a frame

480+ // here; the run's frames go with its directory.

481+ os.rmdir_all(thumb_dir()) or {}

479482 }

480483}

src/media.v +35 -9

@@ -331,7 +331,8 @@ fn (mut p Player) add_to_queue(paths []string) {

331331 for file in paths {

332332 p.queue << file

333333 p.queue_keys << p.queue_new_key()

334- mpv.command_async(p.mpv_ctx, u64(13 + p.queue.len), ['loadfile', file, 'append-play'])

334+ mpv.command_async(p.mpv_ctx, loadfile_base + u64(p.queue.len), ['loadfile', file,

335+ 'append-play'])

335336 }

336337 // The desktop's tracklist reads the playlist mirror, not the queue.

337338 p.playlist = p.queue.clone()

@@ -366,7 +367,7 @@ fn (mut p Player) load_paths(paths []string) {

366367 mpv.command_async(p.mpv_ctx, u64(80), ['playlist-clear'])

367368 for i, file in paths {

368369 flag := if i == 0 { 'replace' } else { 'append-play' }

369- mpv.command_async(p.mpv_ctx, u64(13 + i), ['loadfile', file, flag])

370+ mpv.command_async(p.mpv_ctx, loadfile_base + u64(i), ['loadfile', file, flag])

370371 }

371372}

372373

@@ -395,6 +396,26 @@ fn (mut p Player) init_thumb() {

395396 }

396397}

397398

399+// thumb_dir is where this run's seek-preview frames go: a directory named for

400+// the process, so exit can remove every frame the run left behind, the ones

401+// from abandoned captures included.

402+fn thumb_dir() string {

403+ return os.join_path(os.temp_dir(), 'vendetta-thumbs-${os.getpid()}')

404+}

405+

406+// drop_thumb deletes a captured frame that nothing shows any more.

407+fn drop_thumb(path string) {

408+ os.rm(path) or {}

409+}

410+

411+// drop_shown_thumb deletes the frame on show and clears its path, so the popup

412+// has nothing to draw. It runs when the frame stops matching what plays: a new

413+// file, a closed file, or the preview switched off.

414+fn (mut p Player) drop_shown_thumb() {

415+ drop_thumb(p.preview_show_path)

416+ p.preview_show_path = ''

417+}

418+

398419// display_name is what the panels call a source: a file contributes its path

399420// basename, a link the URL's target with the host standing in when there is

400421// none, so the queue sidebar, the open prompt, the statistics line and the

@@ -424,6 +445,8 @@ fn (mut p Player) load_thumb_file() {

424445 if !p.show_preview || p.thumb_ctx == unsafe { nil } || p.thumb_path == p.current_path {

425446 return

426447 }

448+ // The frame on show was taken from the file that just stopped playing.

449+ p.drop_shown_thumb()

427450 // A live stream never earns a preview (it has no duration to seek to),

428451 // and the instance would download the live playlist alongside the real

429452 // player for as long as both play, starving its demuxer cache. Leave it

@@ -445,8 +468,7 @@ fn (mut p Player) update_preview() {

445468 p.preview_visible = false

446469 return

447470 }

448- b := ui2.bounds()

449- seek := libvendetta.control_bar(b, p.duration).seek

471+ seek := libvendetta.control_bar(p.main_area(ui2.bounds()), p.duration).seek

450472 // The widget's pointer is already in the layout's space, below the menu bar.

451473 over := p.pointer_y >= seek.y && p.pointer_y <= seek.y + seek.height

452474 && p.pointer_x >= seek.x && p.pointer_x <= seek.x + seek.width

@@ -482,7 +504,10 @@ fn (mut p Player) update_preview() {

482504// moving; see drain_thumb.

483505fn (mut p Player) request_thumb() {

484506 p.preview_index++

485- p.preview_pending_path = os.join_path(os.temp_dir(), 'vendetta_thumb_${p.preview_index}.jpg')

507+ // Every capture gets a file of its own: ui2 keeps a decoded image per path,

508+ // so a reused name would keep showing the first frame.

509+ os.mkdir_all(thumb_dir(), os.MkdirParams{}) or {}

510+ p.preview_pending_path = os.join_path(thumb_dir(), 'thumb_${p.preview_index}.jpg')

486511 p.preview_pending_time = p.preview_time

487512 p.preview_t0 = time.ticks()

488513 if p.thumb_path != p.current_path {

@@ -495,10 +520,6 @@ fn (mut p Player) request_thumb() {

495520 'exact'])

496521}

497522

498-// drain_thumb reads the hidden thumbnail instance's queue so it never fills

499-// up. Replies advance the capture pipeline; p.preview_state says what is

500-// happening: 1 loading the file, 2 waiting for the seek to be queued, 3

501-// waiting for the seeked frame to reach the render API, 0 idle.

502523// drain_thumb reads the hidden thumbnail instance's queue so it never fills

503524// up. Replies advance the capture pipeline; p.preview_state says what is

504525// happening: 1 loading the file, 2 waiting for the seek, 3 waiting for the

@@ -552,6 +573,11 @@ fn (mut p Player) show_thumb() {

552573 p.preview_show_h = size.height

553574 }

554575 p.preview_show_time = p.preview_pending_time

576+ // The frame this one replaces is no longer on show, so its file goes. The

577+ // popup takes the new path from the next build, which runs after this draw.

578+ if p.preview_show_path != p.preview_pending_path {

579+ drop_thumb(p.preview_show_path)

580+ }

555581 p.preview_show_path = p.preview_pending_path

556582 if p.preview_over && math.abs(p.preview_time - p.preview_pending_time) <= 0.25 {

557583 p.preview_visible = true

src/mpris_d_mpris.v +13 -14

@@ -889,16 +889,16 @@ fn (mut m Mpris) props_set(msg voidptr) i32 {

889889 return m.reply_empty(msg)

890890 }

891891 'Shuffle' {

892- value := dbus_iter_double_from(var) or {

893- // Not a double: read the boolean instead.

894- b := dbus_iter_flag_from(var) or {

895- return m.reply_error(msg, dbus_error_invalid_args, 'Shuffle wants a boolean')

896- }

897- m.queue(MprisCommand{ kind: .shuffle, arg: if b { 1.0 } else { 0.0 } })

898- return m.reply_empty(msg)

892+ // A number is refused on purpose: the spec's type is a boolean, and

893+ // a double-encoded value is not one.

894+ if _ := dbus_iter_double_from(var) {

895+ return m.reply_error(msg, dbus_error_invalid_args, 'Shuffle wants a boolean')

899896 }

900- _ = value

901- return m.reply_error(msg, dbus_error_invalid_args, 'Shuffle wants a boolean')

897+ b := dbus_iter_flag_from(var) or {

898+ return m.reply_error(msg, dbus_error_invalid_args, 'Shuffle wants a boolean')

899+ }

900+ m.queue(MprisCommand{ kind: .shuffle, arg: if b { 1.0 } else { 0.0 } })

901+ return m.reply_empty(msg)

902902 }

903903 'Volume' {

904904 value := dbus_iter_double_from(var) or {

@@ -1282,7 +1282,7 @@ fn mpris_emit_diff(mut m Mpris, last &MprisSnapshot, next &MprisSnapshot) {

12821282 }

12831283 }

12841284 if changed.len > 0 {

1285- m.emit_properties_changed(mpris_player_iface, changed, [])

1285+ m.emit_properties_changed(mpris_player_iface, mut changed, [])

12861286 }

12871287 // The capability flags move with the file state: a stop takes Play away,

12881288 // a load brings it back, the playlist edges grey the step buttons.

@@ -1296,19 +1296,18 @@ fn mpris_emit_diff(mut m Mpris, last &MprisSnapshot, next &MprisSnapshot) {

12961296 caps << PropEntry{ key: 'CanSeek', value: prop_bool(next.can_seek) }

12971297 caps << PropEntry{ key: 'CanGoNext', value: prop_bool(next.can_go_next) }

12981298 caps << PropEntry{ key: 'CanGoPrevious', value: prop_bool(next.can_go_prev) }

1299- m.emit_properties_changed(mpris_player_iface, caps, [])

1299+ m.emit_properties_changed(mpris_player_iface, mut caps, [])

13001300 }

13011301}

13021302

13031303// emit_properties_changed sends one PropertiesChanged signal: the interface,

13041304// the changed properties as a{sv}, and the invalidated names as as. The

13051305// signal rides the session bus to whoever subscribed.

1306-fn (mut m Mpris) emit_properties_changed(iface string, changed []PropEntry, _ []string) {

1306+fn (mut m Mpris) emit_properties_changed(iface string, mut changed []PropEntry, _ []string) {

13071307 conn := m.conn

13081308 if conn == unsafe { nil } || changed.len == 0 {

13091309 return

13101310 }

1311- mut entries := changed.clone()

13121311 msg := C.dbus_message_new_signal(mpris_path.str, dbus_props_iface.str, c'PropertiesChanged')

13131312 if msg == unsafe { nil } {

13141313 return

@@ -1323,7 +1322,7 @@ fn (mut m Mpris) emit_properties_changed(iface string, changed []PropEntry, _ []

13231322 C.dbus_message_unref(msg)

13241323 return

13251324 }

1326- for mut entry in entries {

1325+ for mut entry in changed {

13271326 mut de := DBusMessageIter{}

13281327 opened = C.dbus_message_iter_open_container(&arr, dbus_type_dict_entry, unsafe { nil }, &de)

13291328 if opened == 0 {

src/mpris_glue_d_mpris.v +2 -2

@@ -179,7 +179,7 @@ fn (mut p Player) mpris_on_volume() {

179179// hook. The flag rides the command value: the write below and the property

180180// events it produces are all the UI thread's work, so no lock is needed.

181181fn (mut p Player) mpris_set_position(value f64) {

182- mpv.set_double_async(p.mpv_ctx, 71, 'time-pos', value)

182+ mpv.set_double_async(p.mpv_ctx, 74, 'time-pos', value)

183183}

184184

185185// mpris_execute runs one command from the service, on the UI thread, where

@@ -285,7 +285,7 @@ fn (mut p Player) mpris_execute(cmd MprisCommand) {

285285 // property would lie: the flag stays where the queue order

286286 // leaves it and the desktop's toggle shuffles once.

287287 if cmd.arg > 0 {

288- mpv.command_async(p.mpv_ctx, 84, ['playlist-shuffle'])

288+ mpv.command_async(p.mpv_ctx, 85, ['playlist-shuffle'])

289289 }

290290 p.mpris_push()

291291 }

src/player.v +43 -41

@@ -82,6 +82,17 @@ const obs_dither = u64(1042)

8282

8383const obs_blend_subtitles = u64(1043)

8484

85+// Reply userdata on the player's mpv context. Almost every write is fire and

86+// forget, and nothing matches its reply by id, so an id only says what the

87+// write was for and a family may share one (the picture-quality writes all use

88+// 97). The runtime option writes are the exception: option_written matches their

89+// replies, so they take ids from opt_write_base up, and the queue's loadfile

90+// appends count up from loadfile_base, out of reach of any queue. Matching a

91+// reply by id anywhere else needs a range reserved here first.

92+const opt_write_base = u64(1) << 32

93+const loadfile_base = u64(1) << 20

94+const option_unset_userdata = u64(86)

95+

8596// The hidden thumbnail instance's async command ids.

8697const thumb_load_userdata = u64(210)

8798const thumb_seek_userdata = u64(211)

@@ -245,6 +256,9 @@ mut:

245256 // in flight, so the on-demand loop asks for frames until it finishes.

246257 thumb_wake u64

247258 save_timer u64

259+ // The settings file's text as last written, so an unchanged state is not

260+ // written again (save_settings).

261+ settings_text string

248262 // The queue snapshot's debounced write timer (settings_changed's shape).

249263 queue_timer u64

250264 // Whether the restored queue still waits for the first frame to hand its

@@ -275,7 +289,11 @@ mut:

275289 // mpv options the user configured through --mpv-opt or the Options

276290 // menu. They persist through the settings file and are mirrored into mpv

277291 // on start, after everything mpv itself needs.

278- mpv_opts map[string]string

292+ mpv_opts map[string]string

293+ // Runtime option writes mpv has not answered yet, by their reply userdata

294+ // (send_mpv_option, option_written).

295+ opt_writes map[u64]OptionWrite

296+ opt_write_serial u64

279297 mpv_option_open bool

280298 mpv_option_name_text string

281299 mpv_option_value_text string

@@ -346,6 +364,9 @@ fn (mut p Player) on_mpv_event(ev voidptr) {

346364 mpv.event_property_change {

347365 p.apply_app_property(ev)

348366 }

367+ mpv.event_set_property_reply {

368+ p.option_written(ev)

369+ }

349370 mpv.event_seek {

350371 p.mpris_seek_started()

351372 }

@@ -733,7 +754,7 @@ fn (mut p Player) resume_pending_queue() {

733754 p.queue_resume_pending = false

734755 mpv.command_async(p.mpv_ctx, u64(80), ['playlist-clear'])

735756 for index, file in p.queue {

736- mpv.command_async(p.mpv_ctx, u64(13 + index), ['loadfile', file, 'append'])

757+ mpv.command_async(p.mpv_ctx, loadfile_base + u64(index), ['loadfile', file, 'append'])

737758 }

738759}

739760

@@ -1038,8 +1059,7 @@ fn (mut p Player) handle(event string) {

10381059 p.queue_keys = []string{}

10391060 p.queue_state = ui2.ReorderState{}

10401061 p.queue_scroll = 0

1041- // The mirror follows: the desktop's tracklist and the dead pl_

1042- // paths read the playlist.

1062+ // The mirror follows: the desktop's tracklist reads the playlist.

10431063 p.playlist = p.queue.clone()

10441064 p.queue_state_changed()

10451065 p.notify('Queue cleared.')

@@ -1083,14 +1103,12 @@ fn (mut p Player) handle(event string) {

10831103 ui2.set_menu_bar(p.menus())

10841104 return

10851105 }

1086- if complaint := p.set_mpv_option(name, value) {

1087- p.notify('mpv rejected ${name}=${value}: ${complaint}')

1088- return

1089- }

1090- p.mpv_opts[name] = value

1091- p.mpv_option_open = false

1092- p.settings_changed()

1093- ui2.set_menu_bar(p.menus())

1106+ // The dialog stays open until mpv answers: a rejection is reported

1107+ // there, and the typed name and value stay put for a correction.

1108+ p.send_mpv_option(OptionWrite{

1109+ name: name

1110+ value: value

1111+ })

10941112 }

10951113 'preset_save' {

10961114 // Options > Presets > Save current options...: the name starts

@@ -1478,7 +1496,7 @@ fn (mut p Player) stats_tick() {

14781496// set_show_preview turns the seek-bar hover preview on or off. Turning it

14791497// on after a --no-preview start builds the thumbnail instance here; turning

14801498// it off only stops using the instance, which idles without decoding until

1481-// the next start without the setting frees it.

1499+// the next start without the setting frees it, and removes the frame on show.

14821500fn (mut p Player) set_show_preview(on bool) {

14831501 if p.show_preview == on {

14841502 return

@@ -1486,6 +1504,8 @@ fn (mut p Player) set_show_preview(on bool) {

14861504 p.show_preview = on

14871505 if on {

14881506 p.init_thumb()

1507+ } else {

1508+ p.drop_shown_thumb()

14891509 }

14901510}

14911511

@@ -1572,6 +1592,7 @@ fn (mut p Player) close_file() {

15721592 p.has_file = false

15731593 p.pos = 0

15741594 p.clear_next = true

1595+ p.drop_shown_thumb()

15751596}

15761597

15771598fn (p &Player) has_track(kind string) bool {

@@ -1695,13 +1716,6 @@ fn (mut p Player) handle_media(event string) bool {

16951716 ui2.set_menu_bar(p.menus())

16961717 return true

16971718 }

1698- if event.starts_with('pl_') {

1699- index := event.all_after('pl_').int()

1700- if index < p.playlist.len {

1701- mpv.command_async(p.mpv_ctx, u64(60 + index), ['loadfile', p.playlist[index], 'replace'])

1702- }

1703- return true

1704- }

17051719 match event {

17061720 'playlist_next' {

17071721 mpv.command_async(p.mpv_ctx, 52, ['playlist-next'])

@@ -2352,17 +2366,16 @@ fn (p &Player) theme_menu_items() []ui2.MenuItem {

23522366}

23532367

23542368// wheel_volume raises or lowers the volume on a scroll wheel notch. Only the

2355-// vertical axis counts; a notch steps 5 like the volume keys. Over the theme

2356-// editor's list the wheel scrolls the colours instead, and over the queue

2357-// sidebar the wheel scrolls the rows.

2358-fn (mut p Player) wheel_volume(x f64, dy f64) {

2369+// vertical axis counts; a notch steps 5 like the volume keys. Over a panel (the

2370+// statistics popup, the queue sidebar, the theme editor) the wheel belongs to

2371+// the panel: the colours and the queue rows scroll instead. The wheel reports

2372+// its deltas, not where it happened, so the panels are tested at the pointer's

2373+// tracked position.

2374+fn (mut p Player) wheel_volume(dy f64) {

23592375 if dy == 0 {

23602376 return

23612377 }

2362- if p.theme_editor_open && x < 348 {

2363- return

2364- }

2365- if p.show_queue && x >= ui2.bounds().width - queue_sidebar_w {

2378+ if p.over_panel(p.pointer_x, p.pointer_y) {

23662379 return

23672380 }

23682381 step := if dy > 0 { 5.0 } else { -5.0 }

@@ -2388,23 +2401,12 @@ fn (mut p Player) mouse_button(button int, down bool, x f64, y f64) {

23882401// pointer_over_video reports whether the window point lands on the video

23892402// rather than on the menu, the controls or a floating panel.

23902403fn (p &Player) pointer_over_video(x f64, y f64) bool {

2391- b := ui2.bounds()

2392- bar := libvendetta.control_bar(b, p.duration)

2404+ bar := libvendetta.control_bar(ui2.bounds(), p.duration)

23932405 // Pointer events are window coordinates; the layout starts below the

23942406 // menu bar strip.

23952407 menu_top := content_top()

23962408 if y >= menu_top + bar.bar.y {

23972409 return false

23982410 }

2399- if p.show_stats || p.show_queue {

2400- // The statistics panel sits top left, the queue top right.

2401- in_stats := p.show_stats && x < 348 + 16 && y - menu_top < 36 + p.stats_lines().len * 18 + 8

2402- // The queue sidebar is a full-height opaque panel (its rows scroll in

2403- // a ui2 reorder container), so its whole width is not the video.

2404- in_queue := p.show_queue && x >= b.width - queue_sidebar_w

2405- if in_stats || in_queue {

2406- return false

2407- }

2408- }

2409- return true

2411+ return !p.over_panel(x, y - menu_top)

24102412}

src/presets.v +11 -14

@@ -87,8 +87,9 @@ fn (mut p Player) save_preset(name string) bool {

8787// apply_preset loads a saved preset and writes its options into mpv one by

8888// one. The preset names the whole configured set: options set earlier but

8989// absent from it stop being carried, so switching presets replaces the set

90-// instead of adding to it. Rejected writes are reported but do not stop the

91-// rest, and an option mpv refuses leaves the configured set as it was.

90+// instead of adding to it. Each write answers on its own (see option_written):

91+// a rejected one is reported and kept out of the configured set, and the rest

92+// go on.

9293fn (mut p Player) apply_preset(name string) {

9394 opts := load_preset_file(name) or {

9495 p.notify('Could not load the preset ${name}.')

@@ -100,7 +101,9 @@ fn (mut p Player) apply_preset(name string) {

100101 p.unset_mpv_option(opt)

101102 }

102103 }

103- mut rejected := []string{}

104+ p.preset_name = name

105+ // Cleared before the writes go out: a rejection can be reported at once.

106+ p.clear_notices()

104107 for opt, value in opts {

105108 // An empty value in the file unsets: the same rule as the dialog

106109 // and the command line.

@@ -108,17 +111,11 @@ fn (mut p Player) apply_preset(name string) {

108111 p.unset_mpv_option(opt)

109112 continue

110113 }

111- if complaint := p.set_mpv_option(opt, value) {

112- rejected << '${opt}=${value}: ${complaint}'

113- continue

114- }

115- p.mpv_opts[opt] = value

116- }

117- p.preset_name = name

118- if rejected.len > 0 {

119- p.notify('Preset ${name}: mpv rejected ${rejected.join('; ')}')

120- } else {

121- p.clear_notices()

114+ p.send_mpv_option(OptionWrite{

115+ name: opt

116+ value: value

117+ preset: name

118+ })

122119 }

123120 p.settings_changed()

124121}

src/settings.v +69 -8

@@ -182,13 +182,21 @@ fn (p &Player) collect_settings() Settings {

182182 }

183183}

184184

185-// save_settings writes the current player state out.

186-fn (p &Player) save_settings() {

185+// save_settings writes the current player state out, unless the file already

186+// holds it. Most keys change nothing that is saved, and the debounced save they

187+// schedule then costs an encode instead of a rewrite.

188+fn (mut p Player) save_settings() {

189+ text := json2.encode(p.collect_settings(), json2.EncoderOptions{})

190+ if text == p.settings_text {

191+ return

192+ }

187193 dir := os.dir(settings_path())

188194 os.mkdir_all(dir, os.MkdirParams{}) or {}

189- os.write_file(settings_path(), json2.encode(p.collect_settings(), json2.EncoderOptions{})) or {

195+ os.write_file(settings_path(), text) or {

190196 eprintln('vendetta: could not save settings: ${err}')

197+ return

191198 }

199+ p.settings_text = text

192200}

193201

194202// settings_changed schedules a save a second out, cancelling a pending one,

@@ -407,10 +415,11 @@ fn (mut p Player) apply_cli(cfg Config) {

407415 }

408416}

409417

410-// set_mpv_option sets one mpv option on the playing instance and returns

411-// mpv's complaint, or none when the option was accepted. Properties and

412-// options share one namespace, so option names given like mpv takes them on

413-// the command line work both before and after initialize.

418+// set_mpv_option sets one mpv option and returns mpv's complaint, or none when

419+// the option was accepted. The call waits for mpv's core, so it is for start-up

420+// only, before the first frame; a runtime write goes through send_mpv_option.

421+// Properties and options share one namespace, so option names given like mpv

422+// takes them on the command line work both before and after initialize.

414423fn (mut p Player) set_mpv_option(name string, value string) ?string {

415424 rc := mpv.set_property_string(p.mpv_ctx, name, value)

416425 if rc < 0 {

@@ -426,6 +435,58 @@ fn (mut p Player) set_mpv_option(name string, value string) ?string {

426435// what undoing a mis-set option through the menu needs, so the write's

427436// outcome is not worth an error.

428437fn (mut p Player) unset_mpv_option(name string) {

429- mpv.set_property_string(p.mpv_ctx, name, '')

438+ mpv.set_string_async(p.mpv_ctx, option_unset_userdata, name, '')

430439 p.mpv_opts.delete(name)

431440}

441+

442+// OptionWrite is a runtime option write that mpv has not answered yet.

443+struct OptionWrite {

444+ name string

445+ value string

446+ // preset is the preset the write came from; empty for the Set option dialog.

447+ preset string

448+}

449+

450+// send_mpv_option queues a runtime option write without waiting for mpv's core,

451+// which the render thread must not do. mpv answers with a SET_PROPERTY_REPLY

452+// carrying the id the write was sent with, and option_written reads that answer,

453+// so a rejection still reaches the user.

454+fn (mut p Player) send_mpv_option(w OptionWrite) {

455+ p.opt_write_serial++

456+ ud := opt_write_base + p.opt_write_serial

457+ rc := mpv.set_string_async(p.mpv_ctx, ud, w.name, w.value)

458+ if rc < 0 {

459+ p.option_rejected(w, mpv.error_string(rc))

460+ return

461+ }

462+ p.opt_writes[ud] = w

463+}

464+

465+// option_written applies mpv's answer to a runtime option write. A rejection is

466+// reported where the write came from. An accepted option joins the configured

467+// set, and an accepted dialog write closes the dialog.

468+fn (mut p Player) option_written(ev voidptr) {

469+ ud := mpv.event_reply_userdata(ev)

470+ w := p.opt_writes[ud] or { return }

471+ p.opt_writes.delete(ud)

472+ code := mpv.event_error(ev)

473+ if code < 0 {

474+ p.option_rejected(w, mpv.error_string(code))

475+ return

476+ }

477+ p.mpv_opts[w.name] = w.value

478+ if w.preset.len == 0 {

479+ p.mpv_option_open = false

480+ }

481+ p.settings_changed()

482+ ui2.set_menu_bar(p.menus())

483+}

484+

485+// option_rejected tells the user mpv refused a runtime option write.

486+fn (mut p Player) option_rejected(w OptionWrite, complaint string) {

487+ if w.preset.len > 0 {

488+ p.notify('Preset ${w.preset}: mpv rejected ${w.name}=${w.value}: ${complaint}')

489+ } else {

490+ p.notify('mpv rejected ${w.name}=${w.value}: ${complaint}')

491+ }

492+}

src/view.v +43 -10

@@ -7,6 +7,9 @@ import time

77// and the video hit test read the same width.

88const queue_sidebar_w = 348.0

99

10+// theme_editor_w is the theme editor sidebar's width, the same as the queue's.

11+const theme_editor_w = 348.0

12+

1013// queue_list_id names the sidebar's reorder container: the view builds the

1114// tree with it, and the pointer / key handlers and the on_scroll feedback

1215// find the same one through it.

@@ -67,6 +70,39 @@ fn (p &Player) gc_line() string {

6770 return 'V GC: heap ${p.stats_heap / 1048576.0:.0f} MiB / unmapped ${p.stats_unmapped / 1048576.0:.0f} MiB'

6871}

6972

73+// main_area is the rect the player is laid out in: the window less the side

74+// panels, which shrink the player instead of floating over it. The theme editor

75+// takes the left, the queue the right.

76+fn (p &Player) main_area(b ui2.Rect) ui2.Rect {

77+ editor_w := if p.theme_editor_open { theme_editor_w } else { 0.0 }

78+ queue_w := if p.show_queue { queue_sidebar_w } else { 0.0 }

79+ return ui2.rect(editor_w, 0, b.width - editor_w - queue_w, b.height)

80+}

81+

82+// stats_popup_rect is the statistics popup's frame in the layout's coordinates.

83+fn (p &Player) stats_popup_rect() ui2.Rect {

84+ return ui2.rect(16, 16, 348, 36 + p.stats_lines().len * 18 + 8)

85+}

86+

87+// over_panel reports whether a point, in the layout's coordinates, lands on a

88+// side panel or on the statistics popup: the video does not take the pointer

89+// there.

90+fn (p &Player) over_panel(x f64, y f64) bool {

91+ b := ui2.bounds()

92+ if p.theme_editor_open && x < theme_editor_w {

93+ return true

94+ }

95+ if p.show_queue && x >= b.width - queue_sidebar_w {

96+ return true

97+ }

98+ if p.show_stats {

99+ stats := p.stats_popup_rect()

100+ return x >= stats.x && x < stats.x + stats.width && y >= stats.y

101+ && y < stats.y + stats.height

102+ }

103+ return false

104+}

105+

70106// view lays the window out from its current bounds. The widget builds the video

71107// and its control strip; the app's panels, popups and dialogs sit on top of it.

72108fn (mut p Player) view() ui2.Element {

@@ -78,11 +114,7 @@ fn (mut p Player) view() ui2.Element {

78114 build_t0 = time.sys_mono_now()

79115 }

80116 b := ui2.bounds()

81- // Both side panels shrink the player: the theme editor from the left, the

82- // queue from the right.

83- editor_w := if p.theme_editor_open { 348.0 } else { 0.0 }

84- queue_w := if p.show_queue { 348.0 } else { 0.0 }

85- main_b := ui2.rect(editor_w, 0, b.width - editor_w - queue_w, b.height)

117+ main_b := p.main_area(b)

86118 bar := libvendetta.control_bar(main_b, p.duration)

87119 button_box := ui2.BoxStyle{

88120 bg: p.theme.btn_primary

@@ -115,7 +147,7 @@ fn (mut p Player) view() ui2.Element {

115147 for i, line in p.stats_lines() {

116148 stat_items << ui2.label('stats_line_${i}', line, ui2.rect(12, 36 + i * 18, 324, 14), stats_body)

117149 }

118- children << ui2.view('stats_popup', ui2.rect(16, 16, 348, 36 + p.stats_lines().len * 18 + 8), ui2.BoxStyle{

150+ children << ui2.view('stats_popup', p.stats_popup_rect(), ui2.BoxStyle{

119151 bg: p.theme.bg_dark

120152 opacity: 0.8

121153 radius: 8

@@ -638,12 +670,13 @@ fn (mut p Player) view() ui2.Element {

638670 thumb_h = 90

639671 thumb_w = 90 * p.preview_show_w / p.preview_show_h

640672 }

673+ // The popup stays inside the player's area, clear of the side panels.

641674 mut x := p.pointer_x - thumb_w / 2

642- if x < 0 {

643- x = 0

675+ if x < main_b.x {

676+ x = main_b.x

644677 }

645- if x + thumb_w > b.width {

646- x = b.width - thumb_w

678+ if x + thumb_w > main_b.x + main_b.width {

679+ x = main_b.x + main_b.width - thumb_w

647680 }

648681 // The popup sits just above the control line, centred on the pointer.

649682 y := bar.seek.y - thumb_h - 24