Skip to content

Commit 9b67948

Browse files
committed
osc: define and add support for "thumbnailer" api
this adds a "standard" api for ui scripts and thumbnailers to communicate with each other, based on the simple thumbfast api [1]. the api works as follows: * If a thumbnailer script is active, it will set `user-data/thumbnailer` property with the width and height of the thumbnail. UI scripts can observe this property to know the thumbnail w/h along with whether thumbnailer is active or not. * To issue a thumbnail draw command, the UI script will set the property `user-data/osc/thumbnailer` with `hover_sec`, `x` and `y` field set. * To clear the thumbnail, the UI script will set the previously mentioned property to `nil`. a more ideal api would make it so that the thumbnailer script only generates the thumbnail and doesn't need to draw at all. but this is a decent enough api that allows arbitrary thumbnailers and ui scripts to communicate between each other and work together. this change has been tested with work with thumbfast (using the "thumbfast-glue" script below). and for demonstration that this api can be useful outside of osc, it has also been tested to work on mfpbar's thumbnailer branch [3]. the code to determine thumbnail x,y is based on the osc fork inside of thumbfast [2]. [1]: https://github.com/po5/thumbfast?tab=readme-ov-file#for-ui-developers-how-to-add-thumbfast-support-to-your-script [2]: https://github.com/po5/thumbfast/tree/vanilla-osc [3]: https://codeberg.org/NRK/mpv-toolbox/src/branch/thumbnailer/mfpbar
1 parent 3b55bc9 commit 9b67948

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

player/lua/osc.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ local layouts = {}
191191
local is_december = os.date("*t").month == 12
192192
local UNICODE_MINUS = string.char(0xe2, 0x88, 0x92) -- UTF-8 for U+2212 MINUS SIGN
193193
local last_custom_button = 0
194+
local thumbnailer = { width = 0, height = 0 }
194195

195196
local function osc_color_convert(color)
196197
return color:sub(6,7) .. color:sub(4,5) .. color:sub(2,3)
@@ -1063,6 +1064,28 @@ local function render_elements(master_ass)
10631064
ass_append_alpha(elem_ass, slider_lo.alpha, 0)
10641065
elem_ass:append(tooltiplabel)
10651066

1067+
-- thumbnail
1068+
local osd_w = mp.get_property_number("osd-width")
1069+
if thumbnailer.width > 0 and thumbnailer.height > 0 and osd_w then
1070+
local r_w, r_h = get_virt_scale_factor()
1071+
1072+
local tooltip_font_size = (user_opts.layout == "box" or user_opts.layout == "slimbox") and 2 or 12
1073+
local thumb_ty = user_opts.layout ~= "topbar" and element.hitbox.y1 - 8 or element.hitbox.y2 + tooltip_font_size + 8
1074+
local thumb_tx = tx
1075+
local thumb_pad = 2
1076+
local thumb_margin_x = 20 / r_w
1077+
local thumb_margin_y = (4 + user_opts.tooltipborder) / r_h + thumb_pad
1078+
local thumb_x = math.min(osd_w - thumbnailer.width - thumb_margin_x, math.max(thumb_margin_x, thumb_tx / r_w - thumbnailer.width / 2))
1079+
local thumb_y = user_opts.layout ~= "topbar" and thumb_ty / r_h - thumbnailer.height - tooltip_font_size / r_h - thumb_margin_y or thumb_ty / r_h + thumb_margin_y
1080+
1081+
mp.set_property_native("user-data/osc/thumbnailer", {
1082+
hover_sec = mp.get_property_number("duration", 0) * (sliderpos / 100),
1083+
x = math.floor(thumb_x + 0.5), y = math.floor(thumb_y + 0.5)
1084+
})
1085+
end
1086+
1087+
else
1088+
mp.set_property_native("user-data/osc/thumbnailer", nil)
10661089
end
10671090
end
10681091

@@ -2734,6 +2757,13 @@ mp.observe_property("chapter-list", "native", function(_, list)
27342757
update_duration_watch()
27352758
request_init()
27362759
end)
2760+
mp.observe_property('user-data/thumbnailer', 'native', function(kind, data)
2761+
if data then
2762+
thumbnailer = data
2763+
else
2764+
thumbnailer = { width = 0, height = 0 }
2765+
end
2766+
end)
27372767

27382768
-- These are for backwards compatibility only.
27392769
mp.register_script_message("osc-message", function(message, dur)

0 commit comments

Comments
 (0)