Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osc.lua: allow adding custom buttons #15517

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DOCS/interface-changes/osc-buttons.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ change several OSC mouse bindings to select.lua functions
add script-opts to configure what OSC buttons do when clicked
remove `osc-playlist_osd` script-opt and behave as if it was off by default; `playlist_osd=yes` can be replicated with `osc-playlist_prev_mbtn_left_command=playlist-prev; show-text ${playlist} 3000` and `osc-playlist_next_mbtn_left_command=playlist-next; show-text ${playlist} 3000`
remove `osc-chapters_osd` script-opt and behave as if it was off by default; `chapter_osd=yes` can be replicated with `osc-chapter_prev_mbtn_left_command=no-osd add chapter -1; show-text ${chapter-list} 3000` and `osc-chapter_next_mbtn_left_command=no-osd add chapter 1; show-text ${chapter-list} 3000`
add script-opts to define custom buttons
15 changes: 15 additions & 0 deletions DOCS/man/osc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,21 @@ clicked. ``mbtn_mid`` commands are also triggered with ``shift+mbtn_left``.

``fullscreen_mbtn_right_command="cycle window-maximized"``

Custom Buttons
~~~~~~~~~~~~~~

Additional script-opts are available to define custom buttons in ``bottombar``
and ``topbar`` layouts.

.. admonition:: Example to add loop and shuffle buttons

custom_button_1_content=🔁
custom_button_1_mbtn_left_command=cycle-values loop-file inf no
custom_button_1_mbtn_right_command=cycle-values loop-playlist inf no

custom_button_2_content=🔀
custom_button_2_mbtn_left_command=playlist-shuffle

Script Commands
~~~~~~~~~~~~~~~

Expand Down
63 changes: 44 additions & 19 deletions player/lua/osc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ local user_opts = {
-- luacheck: pop
}

for i = 1, 99 do
user_opts["custom_button_" .. i .. "_content"] = ""
user_opts["custom_button_" .. i .. "_mbtn_left_command"] = ""
user_opts["custom_button_" .. i .. "_mbtn_mid_command"] = ""
user_opts["custom_button_" .. i .. "_mbtn_right_command"] = ""
end

local osc_param = { -- calculated by osc_init()
playresy = 0, -- canvas size Y
playresx = 0, -- canvas size X
Expand All @@ -144,6 +151,7 @@ local window_control_box_width = 80
local layouts = {}
local is_december = os.date("*t").month == 12
local UNICODE_MINUS = string.char(0xe2, 0x88, 0x92) -- UTF-8 for U+2212 MINUS SIGN
local last_custom_button = 0

local function osc_color_convert(color)
return color:sub(6,7) .. color:sub(4,5) .. color:sub(2,3)
Expand Down Expand Up @@ -451,6 +459,10 @@ local function get_touchtimeout()
return state.touchtime + (get_hidetimeout() / 1000) - mp.get_time()
end

local function cache_enabled()
return state.cache_state and #state.cache_state["seekable-ranges"] > 0
end

local function reset_margins()
if state.using_video_margins then
for _, mopt in ipairs(margins_opts) do
Expand Down Expand Up @@ -1570,15 +1582,26 @@ local function bar_layout(direction)

local t_l = geo.x + geo.w + padX

-- Custom buttons
local t_r = osc_geo.x + osc_geo.w

for i = last_custom_button, 1, -1 do
t_r = t_r - padX
geo = { x = t_r, y = geo.y, an = 6, w = geo.w, h = geo.h }
t_r = t_r - geo.w
lo = add_layout("custom_button_" .. i)
lo.geometry = geo
lo.style = osc_styles.vidtitleBar
end

-- Cache
geo = { x = osc_geo.x + osc_geo.w - padX, y = geo.y,
an = 6, w = 150, h = geo.h }
t_r = t_r - padX
geo = { x = t_r, y = geo.y, an = 6, w = 150, h = geo.h }
t_r = t_r - geo.w - padX
lo = add_layout("cache")
lo.geometry = geo
lo.style = osc_styles.vidtitleBar

local t_r = geo.x - geo.w - padX*2

-- Title
geo = { x = t_l, y = geo.y, an = 4,
w = t_r - t_l, h = geo.h }
Expand Down Expand Up @@ -1912,23 +1935,15 @@ local function osc_init()
end
end
ne.slider.seekRangesF = function()
if user_opts.seekrangestyle == "none" then
return nil
end
local cache_state = state.cache_state
if not cache_state then
if user_opts.seekrangestyle == "none" or not cache_enabled() then
return nil
end
local duration = mp.get_property_number("duration")
if duration == nil or duration <= 0 then
return nil
end
local ranges = cache_state["seekable-ranges"]
if #ranges == 0 then
return nil
end
local nranges = {}
for _, range in pairs(ranges) do
for _, range in pairs(state.cache_state["seekable-ranges"]) do
nranges[#nranges + 1] = {
["start"] = 100 * range["start"] / duration,
["end"] = 100 * range["end"] / duration,
Expand Down Expand Up @@ -2035,13 +2050,10 @@ local function osc_init()
ne = new_element("cache", "button")

ne.content = function ()
local cache_state = state.cache_state
if not (cache_state and cache_state["seekable-ranges"] and
#cache_state["seekable-ranges"] > 0) then
-- probably not a network stream
if not cache_enabled() then
return ""
end
local dmx_cache = cache_state and cache_state["cache-duration"]
local dmx_cache = state.cache_state["cache-duration"]
local thresh = math.min(state.dmx_cache * 0.05, 5) -- 5% or 5s
if dmx_cache and math.abs(dmx_cache - state.dmx_cache) >= thresh then
state.dmx_cache = dmx_cache
Expand Down Expand Up @@ -2072,6 +2084,19 @@ local function osc_init()
bind_mouse_buttons("volume")


-- custom buttons
for i = 1, math.huge do
local content = user_opts["custom_button_" .. i .. "_content"]
if not content or content == "" then
break
end
ne = new_element("custom_button_" .. i, "button")
ne.content = content
bind_mouse_buttons("custom_button_" .. i)
last_custom_button = i
end


-- load layout
layouts[user_opts.layout]()

Expand Down
Loading