diff --git a/multimedia/.config/mpv/fonts/uosc_icons.otf b/multimedia/.config/mpv/fonts/uosc_icons.otf new file mode 100644 index 0000000..4c4e0dc Binary files /dev/null and b/multimedia/.config/mpv/fonts/uosc_icons.otf differ diff --git a/multimedia/.config/mpv/fonts/uosc_textures.ttf b/multimedia/.config/mpv/fonts/uosc_textures.ttf new file mode 100644 index 0000000..e89f1d8 Binary files /dev/null and b/multimedia/.config/mpv/fonts/uosc_textures.ttf differ diff --git a/multimedia/.config/mpv/input.conf b/multimedia/.config/mpv/input.conf index dbbdec6..888267d 100644 --- a/multimedia/.config/mpv/input.conf +++ b/multimedia/.config/mpv/input.conf @@ -37,8 +37,9 @@ I script-message playlistmanager show filename R cycle-values video-aspect "16:9" "4:3" "3.25:1" "no" "-1" # cycle aspect ratios # uosc definitions and menu -o script-binding uosc/peek-timeline -m script-binding uosc/menu +o script-message-to uosc toggle-elements timeline +O script-binding uosc/toggle-ui +m script-binding uosc/menu # script-binding uosc/open-file #! Open file # script-binding uosc/load-subtitles #! Load subtitles @@ -48,3 +49,4 @@ m script-binding uosc/menu # script-binding uosc/playlist #! Utils > Playlist # script-binding uosc/chapters #! Utils > Chapters # script-binding uosc/open-config-directory #! Utils > Open config directory + diff --git a/multimedia/.config/mpv/scripts/battery.lua b/multimedia/.config/mpv/scripts/battery.lua index f14be30..0477821 100644 --- a/multimedia/.config/mpv/scripts/battery.lua +++ b/multimedia/.config/mpv/scripts/battery.lua @@ -1,5 +1,8 @@ -- If the laptop is on battery, the profile 'lq' will be loaded; otherwise 'hq' is used --- +local mp = require 'mp' + +local SHOULD_ADJUST = false + local lqprofile = "lowquality" local hqprofile = "highquality" @@ -12,15 +15,19 @@ local function powerstate() end local function adjust() + if not SHOULD_ADJUST then return end + local state = powerstate() -- this actually overrides automatically applied profiles -- like 'protocol.http' if state == 0 then - mp.msg.info("Running on battery, setting low-quality options.") mp.set_property("profile", lqprofile) + mp.msg.info("[quality] running battery, setting low-quality options.") + mp.osd_message("[quality] LQ") else - mp.msg.info("Not running on battery, setting high-quality options.") mp.set_property("profile", hqprofile) + mp.msg.info("[quality] running ac, setting high-quality options.") + mp.osd_message("[quality] HQ") end end mp.add_hook("on_load", 1, adjust) diff --git a/multimedia/.config/mpv/scripts/copy_videotime.lua b/multimedia/.config/mpv/scripts/copy_videotime.lua new file mode 100644 index 0000000..9331a56 --- /dev/null +++ b/multimedia/.config/mpv/scripts/copy_videotime.lua @@ -0,0 +1,80 @@ +local mp = require 'mp' +require 'mp.msg' + +-- Copy the current time of the video to clipboard. + +WINDOWS = 2 +UNIX = 3 +KEY_BIND = "y" + +local function platform_type() + local utils = require 'mp.utils' + local workdir = utils.to_string(mp.get_property_native("working-directory")) + if string.find(workdir, "\\") then + return WINDOWS + else + return UNIX + end +end + +local function command_exists(cmd) + local pipe = io.popen("type " .. cmd .. " > /dev/null 2> /dev/null; printf \"$?\"", "r") + if not pipe then return end + local exists = pipe:read() == "0" + pipe:close() + return exists +end + +local function get_clipboard_cmd() + if command_exists("xclip") then + return "xclip -silent -in -selection clipboard" + elseif command_exists("wl-copy") then + return "wl-copy" + elseif command_exists("pbcopy") then + return "pbcopy" + else + mp.msg.error("No supported clipboard command found") + return false + end +end + +local function divmod(a, b) + return a / b, a % b +end + +local function set_clipboard(text) + if platform == WINDOWS then + mp.commandv("run", "powershell", "set-clipboard", text) + return true + elseif (platform == UNIX and clipboard_cmd) then + local pipe = io.popen(clipboard_cmd, "w") + if not pipe then return end + pipe:write(text) + pipe:close() + return true + else + mp.msg.error("Set_clipboard error") + return false + end +end + +local function copyTime() + local time_pos = mp.get_property_number("time-pos") + local minutes, remainder = divmod(time_pos, 60) + local hours, minutes = divmod(minutes, 60) + local seconds = math.floor(remainder) + local milliseconds = math.floor((remainder - seconds) * 1000) + local time = string.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds) + if set_clipboard(time) then + mp.osd_message(string.format("[copytime] %s", time)) + else + mp.osd_message("[copytime] failed") + end +end + + +platform = platform_type() +if platform == UNIX then + clipboard_cmd = get_clipboard_cmd() +end +mp.add_key_binding(KEY_BIND, "copyTime", copyTime) diff --git a/multimedia/.config/mpv/scripts/gallery-dl.lua b/multimedia/.config/mpv/scripts/gallery-dl.lua index d49c561..3cc21db 100644 --- a/multimedia/.config/mpv/scripts/gallery-dl.lua +++ b/multimedia/.config/mpv/scripts/gallery-dl.lua @@ -7,23 +7,24 @@ -- e.g. -- `mpv gallery-dl://https://imgur.com/....` +local mp = require 'mp' local utils = require 'mp.utils' local msg = require 'mp.msg' local function exec(args) - local ret = utils.subprocess({args = args}) + local ret = utils.subprocess({ args = args }) return ret.status, ret.stdout, ret end mp.add_hook("on_load", 15, function() - local url = mp.get_property("stream-open-filename", "") - if (url:find("gdl://") ~= 1) then - msg.debug("not a gdl:// url: " .. url) + local fn = mp.get_property("stream-open-filename", "") + if (fn:find("gdl://") ~= 1) then + msg.debug("not a gdl:// url: " .. fn) return end - local url = string.gsub(url,"gdl://","") + local url = string.gsub(url, "gdl://", "") - local es, urls, result = exec({"gallery-dl", "-g", url}) + local es, urls, result = exec({ "gallery-dl", "-g", url }) if (es < 0) or (urls == nil) or (urls == "") then msg.error("failed to get album list.") end diff --git a/multimedia/.config/mpv/scripts/sponsorblock_minimal.lua b/multimedia/.config/mpv/scripts/sponsorblock_minimal.lua index 1940bc8..907a191 100644 --- a/multimedia/.config/mpv/scripts/sponsorblock_minimal.lua +++ b/multimedia/.config/mpv/scripts/sponsorblock_minimal.lua @@ -5,6 +5,8 @@ -- -- original from https://codeberg.org/jouni/mpv_sponsorblock_minimal -- adapted for local playback skipping and some refactoring by me +local mp = require 'mp' + local options = { API = "https://sponsor.ajay.app/api/skipSegments", @@ -31,10 +33,9 @@ local function getranges() Ranges[k] = v end end - return end -local function skip_ads(name, pos) +local function skip_ads(_, pos) if pos ~= nil then for k, v in pairs(Ranges) do if tonumber(k) <= pos and tonumber(v) > pos then @@ -51,7 +52,6 @@ local function skip_ads(name, pos) end end end - return end local function file_loaded() diff --git a/multimedia/.config/mpv/scripts/thumbfast.lua b/multimedia/.config/mpv/scripts/thumbfast.lua new file mode 100644 index 0000000..e01ef4f --- /dev/null +++ b/multimedia/.config/mpv/scripts/thumbfast.lua @@ -0,0 +1,975 @@ +-- thumbfast.lua +-- +-- High-performance on-the-fly thumbnailer +-- +-- Built for easy integration in third-party UIs. + +local options = { + -- Socket path (leave empty for auto) + socket = "", + + -- Thumbnail path (leave empty for auto) + thumbnail = "", + + -- Maximum thumbnail size in pixels (scaled down to fit) + -- Values are scaled when hidpi is enabled + max_height = 200, + max_width = 200, + + -- Apply tone-mapping, no to disable + tone_mapping = "auto", + + -- Overlay id + overlay_id = 42, + + -- Spawn thumbnailer on file load for faster initial thumbnails + spawn_first = false, + + -- Close thumbnailer process after an inactivity period in seconds, 0 to disable + quit_after_inactivity = 0, + + -- Enable on network playback + network = false, + + -- Enable on audio playback + audio = false, + + -- Enable hardware decoding + hwdec = false, + + -- Windows only: use native Windows API to write to pipe (requires LuaJIT) + direct_io = false, + + -- Custom path to the mpv executable + mpv_path = "mpv" +} + +local mp = require "mp" +mp.utils = require "mp.utils" +mp.options = require "mp.options" +mp.options.read_options(options, "thumbfast") + +local properties = {} +local pre_0_30_0 = mp.command_native_async == nil +local pre_0_33_0 = true + +function subprocess(args, async, callback) + callback = callback or function() + end + + if not pre_0_30_0 then + if async then + return mp.command_native_async({ name = "subprocess", playback_only = true, args = args }, callback) + else + return mp.command_native({ name = "subprocess", playback_only = false, capture_stdout = true, args = args }) + end + else + if async then + return mp.utils.subprocess_detached({ args = args }, callback) + else + return mp.utils.subprocess({ args = args }) + end + end +end + +local winapi = {} +if options.direct_io then + local ffi_loaded, ffi = pcall(require, "ffi") + if ffi_loaded then + winapi = { + ffi = ffi, + C = ffi.C, + bit = require("bit"), + socket_wc = "", + + -- WinAPI constants + CP_UTF8 = 65001, + GENERIC_WRITE = 0x40000000, + OPEN_EXISTING = 3, + FILE_FLAG_WRITE_THROUGH = 0x80000000, + FILE_FLAG_NO_BUFFERING = 0x20000000, + PIPE_NOWAIT = ffi.new("unsigned long[1]", 0x00000001), + + INVALID_HANDLE_VALUE = ffi.cast("void*", -1), + + -- don't care about how many bytes WriteFile wrote, so allocate something to store the result once + _lpNumberOfBytesWritten = ffi.new("unsigned long[1]"), + } + -- cache flags used in run() to avoid bor() call + winapi._createfile_pipe_flags = winapi.bit.bor(winapi.FILE_FLAG_WRITE_THROUGH, winapi.FILE_FLAG_NO_BUFFERING) + + ffi.cdef [[ + void* __stdcall CreateFileW(const wchar_t *lpFileName, unsigned long dwDesiredAccess, unsigned long dwShareMode, void *lpSecurityAttributes, unsigned long dwCreationDisposition, unsigned long dwFlagsAndAttributes, void *hTemplateFile); + bool __stdcall WriteFile(void *hFile, const void *lpBuffer, unsigned long nNumberOfBytesToWrite, unsigned long *lpNumberOfBytesWritten, void *lpOverlapped); + bool __stdcall CloseHandle(void *hObject); + bool __stdcall SetNamedPipeHandleState(void *hNamedPipe, unsigned long *lpMode, unsigned long *lpMaxCollectionCount, unsigned long *lpCollectDataTimeout); + int __stdcall MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char *lpMultiByteStr, int cbMultiByte, wchar_t *lpWideCharStr, int cchWideChar); + ]] + + winapi.MultiByteToWideChar = function(MultiByteStr) + if MultiByteStr then + local utf16_len = winapi.C.MultiByteToWideChar(winapi.CP_UTF8, 0, MultiByteStr, -1, nil, 0) + if utf16_len > 0 then + local utf16_str = winapi.ffi.new("wchar_t[?]", utf16_len) + if winapi.C.MultiByteToWideChar(winapi.CP_UTF8, 0, MultiByteStr, -1, utf16_str, utf16_len) > 0 then + return utf16_str + end + end + end + return "" + end + else + options.direct_io = false + end +end + +local file = nil +local file_bytes = 0 +local spawned = false +local disabled = false +local force_disabled = false +local spawn_waiting = false +local spawn_working = false +local script_written = false + +local dirty = false + +local x = nil +local y = nil +local last_x = x +local last_y = y + +local last_seek_time = nil + +local effective_w = options.max_width +local effective_h = options.max_height +local real_w = nil +local real_h = nil +local last_real_w = nil +local last_real_h = nil + +local script_name = nil + +local show_thumbnail = false + +local filters_reset = { ["lavfi-crop"] = true, ["crop"] = true } +local filters_runtime = { ["hflip"] = true, ["vflip"] = true } +local filters_all = { ["hflip"] = true, ["vflip"] = true, ["lavfi-crop"] = true, ["crop"] = true } + +local tone_mappings = { + ["none"] = true, + ["clip"] = true, + ["linear"] = true, + ["gamma"] = true, + ["reinhard"] = true, + ["hable"] = true, + ["mobius"] = true +} +local last_tone_mapping = nil + +local last_vf_reset = "" +local last_vf_runtime = "" + +local last_rotate = 0 + +local par = "" +local last_par = "" + +local last_has_vid = 0 +local has_vid = 0 + +local file_timer = nil +local file_check_period = 1 / 60 + +local allow_fast_seek = true + +local client_script = [=[ +#!/usr/bin/env bash +MPV_IPC_FD=0; MPV_IPC_PATH="%s" +trap "kill 0" EXIT +while [[ $# -ne 0 ]]; do case $1 in --mpv-ipc-fd=*) MPV_IPC_FD=${1/--mpv-ipc-fd=/} ;; esac; shift; done +if echo "print-text thumbfast" >&"$MPV_IPC_FD"; then echo -n > "$MPV_IPC_PATH"; tail -f "$MPV_IPC_PATH" >&"$MPV_IPC_FD" & while read -r -u "$MPV_IPC_FD" 2>/dev/null; do :; done; fi +]=] + +local function get_os() + local raw_os_name = "" + + if jit and jit.os and jit.arch then + raw_os_name = jit.os + else + if package.config:sub(1, 1) == "\\" then + -- Windows + local env_OS = os.getenv("OS") + if env_OS then + raw_os_name = env_OS + end + else + raw_os_name = subprocess({ "uname", "-s" }).stdout + end + end + + raw_os_name = (raw_os_name):lower() + + local os_patterns = { + ["windows"] = "windows", + ["linux"] = "linux", + + ["osx"] = "darwin", + ["mac"] = "darwin", + ["darwin"] = "darwin", + + ["^mingw"] = "windows", + ["^cygwin"] = "windows", + + ["bsd$"] = "darwin", + ["sunos"] = "darwin" + } + + -- Default to linux + local str_os_name = "linux" + + for pattern, name in pairs(os_patterns) do + if raw_os_name:match(pattern) then + str_os_name = name + break + end + end + + return str_os_name +end + +local os_name = mp.get_property("platform") or get_os() + +local path_separator = os_name == "windows" and "\\" or "/" + +if options.socket == "" then + if os_name == "windows" then + options.socket = "thumbfast" + else + options.socket = "/tmp/thumbfast" + end +end + +if options.thumbnail == "" then + if os_name == "windows" then + options.thumbnail = os.getenv("TEMP") .. "\\thumbfast.out" + else + options.thumbnail = "/tmp/thumbfast.out" + end +end + +local unique = mp.utils.getpid() + +options.socket = options.socket .. unique +options.thumbnail = options.thumbnail .. unique + +if options.direct_io then + if os_name == "windows" then + winapi.socket_wc = winapi.MultiByteToWideChar("\\\\.\\pipe\\" .. options.socket) + end + + if winapi.socket_wc == "" then + options.direct_io = false + end +end + +local mpv_path = options.mpv_path + +if mpv_path == "mpv" and os_name == "darwin" and unique then + -- TODO: look into ~~osxbundle/ + mpv_path = string.gsub(subprocess({ "ps", "-o", "comm=", "-p", tostring(unique) }).stdout, "[\n\r]", "") + if mpv_path ~= "mpv" then + mpv_path = string.gsub(mpv_path, "/mpv%-bundle$", "/mpv") + local mpv_bin = mp.utils.file_info("/usr/local/mpv") + if mpv_bin and mpv_bin.is_file then + mpv_path = "/usr/local/mpv" + else + local mpv_app = mp.utils.file_info("/Applications/mpv.app/Contents/MacOS/mpv") + if mpv_app and mpv_app.is_file then + mp.msg.warn( + "symlink mpv to fix Dock icons: `sudo ln -s /Applications/mpv.app/Contents/MacOS/mpv /usr/local/mpv`") + else + mp.msg.warn( + "drag to your Applications folder and symlink mpv to fix Dock icons: `sudo ln -s /Applications/mpv.app/Contents/MacOS/mpv /usr/local/mpv`") + end + end + end +end + +local function vo_tone_mapping() + local passes = mp.get_property_native("vo-passes") + if passes and passes["fresh"] then + for _, v in pairs(passes["fresh"]) do + for k2, v2 in pairs(v) do + if k2 == "desc" and v2 then + local tone_mapping = string.match(v2, "([0-9a-z.-]+) tone map") + if tone_mapping then + return tone_mapping + end + end + end + end + end +end + +local function vf_string(filters, full) + local vf = "" + local vf_table = properties["vf"] + + if vf_table and #vf_table > 0 then + for i = #vf_table, 1, -1 do + if filters[vf_table[i].name] then + local args = "" + for key, value in pairs(vf_table[i].params) do + if args ~= "" then + args = args .. ":" + end + args = args .. key .. "=" .. value + end + vf = vf .. vf_table[i].name .. "=" .. args .. "," + end + end + end + + if (full and options.tone_mapping ~= "no") or options.tone_mapping == "auto" then + if properties["video-params"] and properties["video-params"]["primaries"] == "bt.2020" then + local tone_mapping = options.tone_mapping + if tone_mapping == "auto" then + tone_mapping = last_tone_mapping or properties["tone-mapping"] + if tone_mapping == "auto" and properties["current-vo"] == "gpu-next" then + tone_mapping = vo_tone_mapping() + end + end + if not tone_mappings[tone_mapping] then + tone_mapping = "hable" + end + last_tone_mapping = tone_mapping + vf = vf .. "zscale=transfer=linear,format=gbrpf32le,tonemap=" .. tone_mapping .. ",zscale=transfer=bt709," + end + end + + if full then + vf = vf .. + "scale=w=" .. + effective_w .. ":h=" .. effective_h .. par .. + ",pad=w=" .. effective_w .. ":h=" .. effective_h .. ":x=-1:y=-1,format=bgra" + end + + return vf +end + +local function calc_dimensions() + local width = properties["video-out-params"] and properties["video-out-params"]["dw"] + local height = properties["video-out-params"] and properties["video-out-params"]["dh"] + if not width or not height then return end + + local scale = properties["display-hidpi-scale"] or 1 + + if width / height > options.max_width / options.max_height then + effective_w = math.floor(options.max_width * scale + 0.5) + effective_h = math.floor(height / width * effective_w + 0.5) + else + effective_h = math.floor(options.max_height * scale + 0.5) + effective_w = math.floor(width / height * effective_h + 0.5) + end + + local v_par = properties["video-out-params"] and properties["video-out-params"]["par"] or 1 + if v_par == 1 then + par = ":force_original_aspect_ratio=decrease" + else + par = "" + end +end + +local info_timer = nil + +local function info(w, h) + local rotate = properties["video-params"] and properties["video-params"]["rotate"] + local image = properties["current-tracks"] and properties["current-tracks"]["video"] and + properties["current-tracks"]["video"]["image"] + local albumart = image and properties["current-tracks"]["video"]["albumart"] + + disabled = (w or 0) == 0 or (h or 0) == 0 or + has_vid == 0 or + (properties["demuxer-via-network"] and not options.network) or + (albumart and not options.audio) or + (image and not albumart) or + force_disabled + + if info_timer then + info_timer:kill() + info_timer = nil + elseif has_vid == 0 or (rotate == nil and not disabled) then + info_timer = mp.add_timeout(0.05, function() info(w, h) end) + end + + local json, _ = mp.utils.format_json({ + width = w, + height = h, + disabled = disabled, + available = true, + socket = options.socket, + thumbnail = options.thumbnail, + overlay_id = options.overlay_id + }) + if pre_0_30_0 then + mp.command_native({ "script-message", "thumbfast-info", json }) + else + mp.command_native_async({ "script-message", "thumbfast-info", json }, function() + end) + end +end + +local function remove_thumbnail_files() + if file then + file:close() + file = nil + file_bytes = 0 + end + os.remove(options.thumbnail) + os.remove(options.thumbnail .. ".bgra") +end + +local activity_timer + +local function spawn(time) + if disabled then return end + + local path = properties["path"] + if path == nil then return end + + if options.quit_after_inactivity > 0 then + if show_thumbnail or activity_timer:is_enabled() then + activity_timer:kill() + end + activity_timer:resume() + end + + local open_filename = properties["stream-open-filename"] + local ytdl = open_filename and properties["demuxer-via-network"] and path ~= open_filename + if ytdl then + path = open_filename + end + + remove_thumbnail_files() + + local vid = properties["vid"] + has_vid = vid or 0 + + local args = { + mpv_path, "--no-config", "--msg-level=all=no", "--idle", "--pause", "--keep-open=always", "--really-quiet", + "--no-terminal", + "--load-scripts=no", "--osc=no", "--ytdl=no", "--load-stats-overlay=no", "--load-osd-console=no", + "--load-auto-profiles=no", + "--edition=" .. (properties["edition"] or "auto"), "--vid=" .. (vid or "auto"), "--no-sub", "--no-audio", + "--start=" .. time, allow_fast_seek and "--hr-seek=no" or "--hr-seek=yes", + "--ytdl-format=worst", "--demuxer-readahead-secs=0", "--demuxer-max-bytes=128KiB", + "--vd-lavc-skiploopfilter=all", "--vd-lavc-software-fallback=1", "--vd-lavc-fast", "--vd-lavc-threads=2", + "--hwdec=" .. (options.hwdec and "auto" or "no"), + "--vf=" .. vf_string(filters_all, true), + "--sws-scaler=fast-bilinear", + "--video-rotate=" .. last_rotate, + "--ovc=rawvideo", "--of=image2", "--ofopts=update=1", "--o=" .. options.thumbnail + } + + if not pre_0_30_0 then + table.insert(args, "--sws-allow-zimg=no") + end + + if os_name == "darwin" and properties["macos-app-activation-policy"] then + table.insert(args, "--macos-app-activation-policy=accessory") + end + + if os_name == "windows" or pre_0_33_0 then + table.insert(args, "--input-ipc-server=" .. options.socket) + elseif not script_written then + local client_script_path = options.socket .. ".run" + local script = io.open(client_script_path, "w+") + if script == nil then + mp.msg.error("client script write failed") + return + else + script_written = true + script:write(string.format(client_script, options.socket)) + script:close() + subprocess({ "chmod", "+x", client_script_path }, true) + table.insert(args, "--scripts=" .. client_script_path) + end + else + local client_script_path = options.socket .. ".run" + table.insert(args, "--scripts=" .. client_script_path) + end + + table.insert(args, "--") + table.insert(args, path) + + spawned = true + spawn_waiting = true + + subprocess(args, true, + function(success, result) + if spawn_waiting and (success == false or (result.status ~= 0 and result.status ~= -2)) then + spawned = false + spawn_waiting = false + options.tone_mapping = "no" + mp.msg.error("mpv subprocess create failed") + if not spawn_working then -- notify users of required configuration + if options.mpv_path == "mpv" then + if properties["current-vo"] == "libmpv" then + if options.mpv_path == mpv_path then -- attempt to locate ImPlay + mpv_path = "ImPlay" + spawn(time) + else -- ImPlay not in path + if os_name ~= "darwin" then + force_disabled = true + info(real_w or effective_w, real_h or effective_h) + end + mp.commandv("show-text", "thumbfast: ERROR! cannot create mpv subprocess", 5000) + mp.commandv("script-message-to", "implay", "show-message", "thumbfast initial setup", + "Set mpv_path=PATH_TO_ImPlay in thumbfast config:\n" .. + string.gsub(mp.command_native({ "expand-path", "~~/script-opts/thumbfast.conf" }), + "[/\\]", path_separator) .. "\nand restart ImPlay") + end + else + mp.commandv("show-text", "thumbfast: ERROR! cannot create mpv subprocess", 5000) + if os_name == "windows" then + mp.commandv("script-message-to", "mpvnet", "show-text", + "thumbfast: ERROR! install standalone mpv, see README", 5000, 20) + mp.commandv("script-message", "mpv.net", "show-text", + "thumbfast: ERROR! install standalone mpv, see README", 5000, 20) + end + end + else + mp.commandv("show-text", "thumbfast: ERROR! cannot create mpv subprocess", 5000) + -- found ImPlay but not defined in config + mp.commandv("script-message-to", "implay", "show-message", "thumbfast", + "Set mpv_path=PATH_TO_ImPlay in thumbfast config:\n" .. + string.gsub(mp.command_native({ "expand-path", "~~/script-opts/thumbfast.conf" }), "[/\\]", + path_separator) .. "\nand restart ImPlay") + end + end + elseif success == true and (result.status == 0 or result.status == -2) then + if not spawn_working and properties["current-vo"] == "libmpv" and options.mpv_path ~= mpv_path then + mp.commandv("script-message-to", "implay", "show-message", "thumbfast initial setup", + "Set mpv_path=ImPlay in thumbfast config:\n" .. + string.gsub(mp.command_native({ "expand-path", "~~/script-opts/thumbfast.conf" }), "[/\\]", + path_separator) .. "\nand restart ImPlay") + end + spawn_working = true + spawn_waiting = false + end + end + ) +end + +local function run(command) + if not spawned then return end + + if options.direct_io then + local hPipe = winapi.C.CreateFileW(winapi.socket_wc, winapi.GENERIC_WRITE, 0, nil, winapi.OPEN_EXISTING, + winapi._createfile_pipe_flags, nil) + if hPipe ~= winapi.INVALID_HANDLE_VALUE then + local buf = command .. "\n" + winapi.C.SetNamedPipeHandleState(hPipe, winapi.PIPE_NOWAIT, nil, nil) + winapi.C.WriteFile(hPipe, buf, #buf + 1, winapi._lpNumberOfBytesWritten, nil) + winapi.C.CloseHandle(hPipe) + end + + return + end + + local command_n = command .. "\n" + + if os_name == "windows" then + if file and file_bytes + #command_n >= 4096 then + file:close() + file = nil + file_bytes = 0 + end + if not file then + file = io.open("\\\\.\\pipe\\" .. options.socket, "r+b") + end + elseif pre_0_33_0 then + subprocess({ "/usr/bin/env", "sh", "-c", "echo '" .. command .. "' | socat - " .. options.socket }) + return + elseif not file then + file = io.open(options.socket, "r+") + end + if file then + file_bytes = file:seek("end") + file:write(command_n) + file:flush() + end +end + +local function draw(w, h, script) + if not w or not show_thumbnail then return end + if x ~= nil then + if pre_0_30_0 then + mp.command_native({ "overlay-add", options.overlay_id, x, y, options.thumbnail .. ".bgra", 0, "bgra", w, h, + (4 * w) }) + else + mp.command_native_async( + { "overlay-add", options.overlay_id, x, y, options.thumbnail .. ".bgra", 0, "bgra", w, h, (4 * w) }, + function() + end) + end + elseif script then + local json, _ = mp.utils.format_json({ + width = w, + height = h, + x = x, + y = y, + socket = options.socket, + thumbnail = options.thumbnail, + overlay_id = options.overlay_id + }) + mp.commandv("script-message-to", script, "thumbfast-render", json) + end +end + +local function real_res(req_w, req_h, filesize) + local count = filesize / 4 + local diff = (req_w * req_h) - count + + if (properties["video-params"] and properties["video-params"]["rotate"] or 0) % 180 == 90 then + req_w, req_h = req_h, req_w + end + + if diff == 0 then + return req_w, req_h + else + local threshold = 5 -- throw out results that change too much + local long_side, short_side = req_w, req_h + if req_h > req_w then + long_side, short_side = req_h, req_w + end + for a = short_side, short_side - threshold, -1 do + if count % a == 0 then + local b = count / a + if long_side - b < threshold then + if req_h < req_w then return b, a else return a, b end + end + end + end + return nil + end +end + +local function move_file(from, to) + if os_name == "windows" then + os.remove(to) + end + -- move the file because it can get overwritten while overlay-add is reading it, and crash the player + os.rename(from, to) +end + +local function seek(fast) + if last_seek_time then + run("async seek " .. last_seek_time .. (fast and " absolute+keyframes" or " absolute+exact")) + end +end + +local seek_period = 3 / 60 +local seek_period_counter = 0 +local seek_timer +seek_timer = mp.add_periodic_timer(seek_period, function() + if seek_period_counter == 0 then + seek(allow_fast_seek) + seek_period_counter = 1 + else + if seek_period_counter == 2 then + if allow_fast_seek then + seek_timer:kill() + seek() + end + else + seek_period_counter = seek_period_counter + 1 + end + end +end) +seek_timer:kill() + +local function request_seek() + if seek_timer:is_enabled() then + seek_period_counter = 0 + else + seek_timer:resume() + seek(allow_fast_seek) + seek_period_counter = 1 + end +end + +local function check_new_thumb() + -- the slave might start writing to the file after checking existance and + -- validity but before actually moving the file, so move to a temporary + -- location before validity check to make sure everything stays consistant + -- and valid thumbnails don't get overwritten by invalid ones + local tmp = options.thumbnail .. ".tmp" + move_file(options.thumbnail, tmp) + local finfo = mp.utils.file_info(tmp) + if not finfo then return false end + spawn_waiting = false + local w, h = real_res(effective_w, effective_h, finfo.size) + if w then -- only accept valid thumbnails + move_file(tmp, options.thumbnail .. ".bgra") + + real_w, real_h = w, h + if real_w and (real_w ~= last_real_w or real_h ~= last_real_h) then + last_real_w, last_real_h = real_w, real_h + info(real_w, real_h) + end + if not show_thumbnail then + file_timer:kill() + end + return true + end + + return false +end + +file_timer = mp.add_periodic_timer(file_check_period, function() + if check_new_thumb() then + draw(real_w, real_h, script_name) + end +end) +file_timer:kill() + +local function clear() + file_timer:kill() + seek_timer:kill() + if options.quit_after_inactivity > 0 then + if show_thumbnail or activity_timer:is_enabled() then + activity_timer:kill() + end + activity_timer:resume() + end + last_seek_time = nil + show_thumbnail = false + last_x = nil + last_y = nil + if script_name then return end + if pre_0_30_0 then + mp.command_native({ "overlay-remove", options.overlay_id }) + else + mp.command_native_async({ "overlay-remove", options.overlay_id }, function() + end) + end +end + +local function quit() + activity_timer:kill() + if show_thumbnail then + activity_timer:resume() + return + end + run("quit") + spawned = false + real_w, real_h = nil, nil + clear() +end + +activity_timer = mp.add_timeout(options.quit_after_inactivity, quit) +activity_timer:kill() + +local function thumb(time, r_x, r_y, script) + if disabled then return end + + time = tonumber(time) + if time == nil then return end + + if r_x == "" or r_y == "" then + x, y = nil, nil + else + x, y = math.floor(r_x + 0.5), math.floor(r_y + 0.5) + end + + script_name = script + if last_x ~= x or last_y ~= y or not show_thumbnail then + show_thumbnail = true + last_x = x + last_y = y + draw(real_w, real_h, script) + end + + if options.quit_after_inactivity > 0 then + if show_thumbnail or activity_timer:is_enabled() then + activity_timer:kill() + end + activity_timer:resume() + end + + if time == last_seek_time then return end + last_seek_time = time + if not spawned then spawn(time) end + request_seek() + if not file_timer:is_enabled() then file_timer:resume() end +end + +local function watch_changes() + if not dirty or not properties["video-out-params"] then return end + dirty = false + + local old_w = effective_w + local old_h = effective_h + + calc_dimensions() + + local vf_reset = vf_string(filters_reset) + local rotate = properties["video-rotate"] or 0 + + local resized = old_w ~= effective_w or + old_h ~= effective_h or + last_vf_reset ~= vf_reset or + (last_rotate % 180) ~= (rotate % 180) or + par ~= last_par + + if resized then + last_rotate = rotate + info(effective_w, effective_h) + elseif last_has_vid ~= has_vid and has_vid ~= 0 then + info(effective_w, effective_h) + end + + if spawned then + if resized then + -- mpv doesn't allow us to change output size + local seek_time = last_seek_time + run("quit") + clear() + spawned = false + spawn(seek_time or mp.get_property_number("time-pos", 0)) + file_timer:resume() + else + if rotate ~= last_rotate then + run("set video-rotate " .. rotate) + end + local vf_runtime = vf_string(filters_runtime) + if vf_runtime ~= last_vf_runtime then + run("vf set " .. vf_string(filters_all, true)) + last_vf_runtime = vf_runtime + end + end + else + last_vf_runtime = vf_string(filters_runtime) + end + + last_vf_reset = vf_reset + last_rotate = rotate + last_par = par + last_has_vid = has_vid + + if not spawned and not disabled and options.spawn_first and resized then + spawn(mp.get_property_number("time-pos", 0)) + file_timer:resume() + end +end + +local function update_property(name, value) + properties[name] = value +end + +local function update_property_dirty(name, value) + properties[name] = value + dirty = true + if name == "tone-mapping" then + last_tone_mapping = nil + end +end + +local function update_tracklist(_, value) + -- current-tracks shim + for _, track in ipairs(value) do + if track.type == "video" and track.selected then + properties["current-tracks/video/image"] = track.image + properties["current-tracks/video/albumart"] = track.albumart + return + end + end +end + +local function sync_changes(prop, val) + update_property(prop, val) + if val == nil then return end + + if type(val) == "boolean" then + if prop == "vid" then + has_vid = 0 + last_has_vid = 0 + info(effective_w, effective_h) + clear() + return + end + val = val and "yes" or "no" + end + + if prop == "vid" then + has_vid = 1 + end + + if not spawned then return end + + run("set " .. prop .. " " .. val) + dirty = true +end + +local function file_load() + clear() + spawned = false + real_w, real_h = nil, nil + last_real_w, last_real_h = nil, nil + last_tone_mapping = nil + last_seek_time = nil + if info_timer then + info_timer:kill() + info_timer = nil + end + + calc_dimensions() + info(effective_w, effective_h) +end + +local function shutdown() + run("quit") + remove_thumbnail_files() + if os_name ~= "windows" then + os.remove(options.socket) + os.remove(options.socket .. ".run") + end +end + +local function on_duration(_, val) + allow_fast_seek = (val or 30) >= 30 +end + +mp.observe_property("current-tracks", "native", function(name, value) + if pre_0_33_0 then + mp.unobserve_property(update_tracklist) + pre_0_33_0 = false + end + update_property(name, value) +end) + +mp.observe_property("track-list", "native", update_tracklist) +mp.observe_property("display-hidpi-scale", "native", update_property_dirty) +mp.observe_property("video-out-params", "native", update_property_dirty) +mp.observe_property("video-params", "native", update_property_dirty) +mp.observe_property("vf", "native", update_property_dirty) +mp.observe_property("tone-mapping", "native", update_property_dirty) +mp.observe_property("demuxer-via-network", "native", update_property) +mp.observe_property("stream-open-filename", "native", update_property) +mp.observe_property("macos-app-activation-policy", "native", update_property) +mp.observe_property("current-vo", "native", update_property) +mp.observe_property("video-rotate", "native", update_property) +mp.observe_property("path", "native", update_property) +mp.observe_property("vid", "native", sync_changes) +mp.observe_property("edition", "native", sync_changes) +mp.observe_property("duration", "native", on_duration) + +mp.register_script_message("thumb", thumb) +mp.register_script_message("clear", clear) + +mp.register_event("file-loaded", file_load) +mp.register_event("shutdown", shutdown) + +mp.register_idle(watch_changes) diff --git a/multimedia/.config/mpv/scripts/uosc.lua b/multimedia/.config/mpv/scripts/uosc.lua index cdc3919..f8eeba9 100644 --- a/multimedia/.config/mpv/scripts/uosc.lua +++ b/multimedia/.config/mpv/scripts/uosc.lua @@ -1,3655 +1,1261 @@ ---[[ +--[[ uosc 4.7.0 - 2023-Apr-15 | https://github.com/tomasklaen/uosc ]] +local uosc_version = '4.7.0' -uosc 2.9.0 - 2020-May-11 | https://github.com/darsain/uosc +assdraw = require('mp.assdraw') +opt = require('mp.options') +utils = require('mp.utils') +msg = require('mp.msg') +osd = mp.create_osd_overlay('ass-events') +INFINITY = 1e309 +QUARTER_PI_SIN = math.sin(math.pi / 4) -Minimalist cursor proximity based UI for MPV player. +-- Enables relative requires from `scripts` directory +package.path = package.path .. ';' .. mp.find_config_file('scripts') .. '/?.lua' -uosc replaces the default osc UI, so that has to be disabled first. -Place these options into your `mpv.conf` file: +require('uosc_shared/lib/std') -``` -# required so that the 2 UIs don't fight each other -osc=no -# uosc provides its own seeking/volume indicators, so you also don't need this -osd-bar=no -# uosc will draw its own window controls if you disable window border -border=no -``` +--[[ OPTIONS ]] -Options go in `script-opts/uosc.conf`. Defaults: +defaults = { + timeline_style = 'line', + timeline_line_width = 2, + timeline_line_width_fullscreen = 3, + timeline_line_width_minimized_scale = 10, + timeline_size_min = 2, + timeline_size_max = 40, + timeline_size_min_fullscreen = 0, + timeline_size_max_fullscreen = 60, + timeline_start_hidden = false, + timeline_persistency = 'paused', + timeline_opacity = 0.9, + timeline_border = 1, + timeline_step = 5, + timeline_chapters_opacity = 0.8, + timeline_cache = true, -``` -# timeline size when fully retracted, 0 will hide it completely -timeline_size_min=2 -# timeline size when fully expanded, in pixels, 0 to disable -timeline_size_max=40 -# same as ^ but when in fullscreen -timeline_size_min_fullscreen=0 -timeline_size_max_fullscreen=60 -# same thing as calling toggle-progress command once on startup -timeline_start_hidden=no -# timeline opacity -timeline_opacity=0.8 -# top (and bottom in no-border mode) border of background color to help visually -# separate elapsed bar from a video of similar color or desktop background -timeline_border=1 -# when scrolling above timeline, wheel will seek by this amount of seconds -timeline_step=5 -# display seekable buffered ranges for streaming videos, syntax `color:opacity`, -# color is an BBGGRR hex code, set to `none` to disable -timeline_cached_ranges=345433:0.5 -# floating number font scale adjustment -timeline_font_scale=1 -# briefly show timeline on external changes (e.g. seeking with a hotkey) -timeline_flash=yes + controls = 'menu,gap,subtitles,audio,video,editions,stream-quality,gap,space,speed,space,shuffle,loop-playlist,loop-file,gap,prev,items,next,gap,fullscreen', + controls_size = 32, + controls_size_fullscreen = 40, + controls_margin = 8, + controls_spacing = 2, + controls_persistency = '', -# timeline chapters style: none, dots, lines, lines-top, lines-bottom -chapters=dots -chapters_opacity=0.3 + volume = 'right', + volume_size = 40, + volume_size_fullscreen = 52, + volume_persistency = '', + volume_opacity = 0.9, + volume_border = 1, + volume_step = 1, -# where to display volume controls: none, left, right -volume=right -volume_size=40 -volume_size_fullscreen=60 -volume_opacity=0.8 -volume_border=1 -volume_step=1 -volume_font_scale=1 -volume_flash=yes + speed_persistency = '', + speed_opacity = 0.6, + speed_step = 0.1, + speed_step_is_factor = false, -# playback speed widget: mouse drag or wheel to change, click to reset -speed=no -speed_size=46 -speed_size_fullscreen=68 -speed_opacity=1 -speed_step=0.1 -speed_font_scale=1 -speed_flash=yes + menu_item_height = 36, + menu_item_height_fullscreen = 50, + menu_min_width = 260, + menu_min_width_fullscreen = 360, + menu_opacity = 1, + menu_parent_opacity = 0.4, -# controls all menus, such as context menu, subtitle loader/selector, etc -menu_item_height=36 -menu_item_height_fullscreen=50 -menu_wasd_navigation=no -menu_hjkl_navigation=no -menu_opacity=0.8 -menu_font_scale=1 + top_bar = 'no-border', + top_bar_size = 40, + top_bar_size_fullscreen = 46, + top_bar_persistency = '', + top_bar_controls = true, + top_bar_title = 'yes', + top_bar_alt_title = '', + top_bar_alt_title_place = 'below', + top_bar_title_opacity = 0.8, -# top bar with window controls and media title shown only in no-border mode -top_bar_size=40 -top_bar_size_fullscreen=46 -top_bar_controls=yes -top_bar_title=yes + window_border_size = 1, + window_border_opacity = 0.8, -# pause video on clicks shorter than this number of milliseconds, 0 to disable -pause_on_click_shorter_than=0 -# for how long in milliseconds to show elements they're it's being flashed -flash_duration=400 -# distances in pixels below which elements are fully faded in/out -proximity_in=40 -proximity_out=120 -# BBGGRR - BLUE GREEN RED hex color codes -color_foreground=ffffff -color_foreground_text=000000 -color_background=000000 -color_background_text=ffffff -# use bold font weight throughout the whole UI -font_bold=no -# hide UI when mpv autohides the cursor -autohide=no -# can be: none, flash, static -pause_indicator=flash -# load first file when calling next on a last file in a directory and vice versa -directory_navigation_loops=no -# file types to look for when navigating media files -media_types=3gp,avi,bmp,flac,flv,gif,h264,h265,jpeg,jpg,m4a,m4v,mid,midi,mkv,mov,mp3,mp4,mp4a,mp4v,mpeg,mpg,oga,ogg,ogm,ogv,opus,png,rmvb,svg,tif,tiff,wav,weba,webm,webp,wma,wmv -# file types to look for when loading external subtitles -subtitle_types=aqt,gsub,jss,sub,ttxt,pjs,psb,rt,smi,slt,ssf,srt,ssa,ass,usf,idx,vt -# used to approximate text width -# if you are using some wide font and see a lot of right side clipping in menus, -# try bumping this up -font_height_to_letter_width_ratio=0.5 + autoload = false, + autoload_types = 'video,audio,image', + shuffle = false, -# `chapter_ranges` lets you transform chapter indicators into range indicators. -# -# Chapter range definition syntax: -# ``` -# start_patternend_pattern -# ``` -# -# Multiple start and end patterns can be defined by separating them with `|`: -# ``` -# p1|pNp1|pN -# ``` -# -# Multiple chapter ranges can be defined by separating them with comma: -# -# chapter_ranges=range1,rangeN -# -# One of `start_pattern`s can be a custom keyword `{bof}` that will match -# beginning of file when it makes sense. -# -# One of `end_pattern`s can be a custom keyword `{eof}` that will match end of -# file when it makes sense. -# -# Patterns are lua patterns (http://lua-users.org/wiki/PatternsTutorial). -# They only need to occur in a title, not match it completely. -# Matching is case insensitive. -# -# `color` is a `bbggrr` hexadecimal color code. -# `opacity` is a float number from 0 to 1. -# -# Examples: -# -# Display skippable youtube video sponsor blocks from https://github.com/po5/mpv_sponsorblock -# ``` -# chapter_ranges=sponsor start<3535a5:0.5>sponsor end -# ``` -# -# Display anime openings and endings as ranges: -# ``` -# chapter_ranges=^op| op$|opening<968638:0.5>.*, ^ed| ed$|^end|ending$<968638:0.5>.*|{eof} -# ``` -chapter_ranges=^op| op$|opening<968638:0.5>.*, ^ed| ed$|^end|ending$<968638:0.5>.*|{eof}, sponsor start<3535a5:.5>sponsor end -``` - -Available keybindings (place into `input.conf`): - -``` -Key script-binding uosc/peek-timeline -Key script-binding uosc/toggle-progress -Key script-binding uosc/menu -Key script-binding uosc/load-subtitles -Key script-binding uosc/subtitles -Key script-binding uosc/audio -Key script-binding uosc/video -Key script-binding uosc/playlist -Key script-binding uosc/chapters -Key script-binding uosc/open-file -Key script-binding uosc/next -Key script-binding uosc/prev -Key script-binding uosc/first -Key script-binding uosc/last -Key script-binding uosc/next-file -Key script-binding uosc/prev-file -Key script-binding uosc/first-file -Key script-binding uosc/last-file -Key script-binding uosc/delete-file-next -Key script-binding uosc/delete-file-quit -Key script-binding uosc/show-in-directory -Key script-binding uosc/open-config-directory -``` -]] if mp.get_property('osc') == 'yes' then - mp.msg.info('Disabled because original osc is enabled!') - return -end - -local assdraw = require('mp.assdraw') -local opt = require('mp.options') -local utils = require('mp.utils') -local msg = require('mp.msg') -local osd = mp.create_osd_overlay('ass-events') -local infinity = 1e309 - --- OPTIONS/CONFIG/STATE -local options = { - timeline_size_min = 2, - timeline_size_max = 40, - timeline_size_min_fullscreen = 0, - timeline_size_max_fullscreen = 60, - timeline_start_hidden = false, - timeline_opacity = 0.8, - timeline_border = 1, - timeline_step = 5, - timeline_cached_ranges = '345433:0.5', - timeline_font_scale = 1, - timeline_flash = true, - - chapters = 'dots', - chapters_opacity = 0.3, - - volume = 'right', - volume_size = 40, - volume_size_fullscreen = 60, - volume_opacity = 0.8, - volume_border = 1, - volume_step = 1, - volume_font_scale = 1, - volume_flash = true, - - speed = false, - speed_size = 46, - speed_size_fullscreen = 68, - speed_opacity = 1, - speed_step = 0.1, - speed_font_scale = 1, - speed_flash = true, - - menu_item_height = 36, - menu_item_height_fullscreen = 50, - menu_wasd_navigation = false, - menu_hjkl_navigation = false, - menu_opacity = 0.8, - menu_font_scale = 1, - - top_bar_size = 40, - top_bar_size_fullscreen = 46, - top_bar_controls = true, - top_bar_title = true, - - pause_on_click_shorter_than = 0, - flash_duration = 400, - proximity_in = 40, - proximity_out = 120, - color_foreground = 'ffffff', - color_foreground_text = '000000', - color_background = '000000', - color_background_text = 'ffffff', - font_bold = false, - autohide = false, - pause_indicator = 'flash', - directory_navigation_loops = false, - media_types = '3gp,avi,bmp,flac,flv,gif,h264,h265,jpeg,jpg,m4a,m4v,mid,midi,mkv,mov,mp3,mp4,mp4a,mp4v,mpeg,mpg,oga,ogg,ogm,ogv,opus,png,rmvb,svg,tif,tiff,wav,weba,webm,webp,wma,wmv', - subtitle_types = 'aqt,gsub,jss,sub,ttxt,pjs,psb,rt,smi,slt,ssf,srt,ssa,ass,usf,idx,vt', - font_height_to_letter_width_ratio = 0.5, - chapter_ranges = '^op| op$|opening<968638:0.5>.*, ^ed| ed$|^end|ending$<968638:0.5>.*|{eof}, sponsor start<3535a5:.5>sponsor end' + ui_scale = 1, + font_scale = 1, + text_border = 1.2, + text_width_estimation = true, + pause_on_click_shorter_than = 0, -- deprecated by below + click_threshold = 0, + click_command = 'cycle pause; script-binding uosc/flash-pause-indicator', + flash_duration = 1000, + proximity_in = 40, + proximity_out = 120, + foreground = 'ffffff', + foreground_text = '000000', + background = '000000', + background_text = 'ffffff', + total_time = false, -- deprecated by below + destination_time = 'playtime-remaining', + time_precision = 0, + font_bold = false, + autohide = false, + buffered_time_threshold = 60, + pause_indicator = 'flash', + curtain_opacity = 0.5, + stream_quality_options = '4320,2160,1440,1080,720,480,360,240,144', + video_types= '3g2,3gp,asf,avi,f4v,flv,h264,h265,m2ts,m4v,mkv,mov,mp4,mp4v,mpeg,mpg,ogm,ogv,rm,rmvb,ts,vob,webm,wmv,y4m', + audio_types= 'aac,ac3,aiff,ape,au,dsf,dts,flac,m4a,mid,midi,mka,mp3,mp4a,oga,ogg,opus,spx,tak,tta,wav,weba,wma,wv', + image_types= 'apng,avif,bmp,gif,j2k,jp2,jfif,jpeg,jpg,jxl,mj2,png,svg,tga,tif,tiff,webp', + subtitle_types = 'aqt,ass,gsub,idx,jss,lrc,mks,pgs,pjs,psb,rt,slt,smi,sub,sup,srt,ssa,ssf,ttxt,txt,usf,vt,vtt', + default_directory = '~/', + use_trash = false, + adjust_osd_margins = true, + chapter_ranges = 'openings:30abf964,endings:30abf964,ads:c54e4e80', + chapter_range_patterns = 'openings:オープニング;endings:エンディング', } +options = table_shallow_copy(defaults) opt.read_options(options, 'uosc') -local config = { - render_delay = 0.03, -- sets max rendering frequency - font = mp.get_property('options/osd-font'), - menu_parent_opacity = 0.4, - menu_min_width = 260 -} -local bold_tag = options.font_bold and '\\b1' or '' -local display = {width = 1280, height = 720, aspect = 1.77778} -local cursor = { - hidden = true, -- true when autohidden or outside of the player window - x = 0, - y = 0 -} -local state = { - os = (function() - if os.getenv('windir') ~= nil then return 'windows' end - local homedir = os.getenv('HOME') - if homedir ~= nil and string.sub(homedir, 1, 6) == '/Users' then - return 'macos' - end - return 'linux' - end)(), - cwd = mp.get_property('working-directory'), - media_title = '', - duration = nil, - position = nil, - pause = false, - chapters = nil, - chapter_ranges = nil, - fullscreen = mp.get_property_native('fullscreen'), - maximized = mp.get_property_native('window-maximized'), - render_timer = nil, - render_last_time = 0, - volume = nil, - volume_max = nil, - mute = nil, - cursor_autohide_timer = mp.add_timeout( - mp.get_property_native('cursor-autohide') / 1000, function() - if not options.autohide then return end - handle_mouse_leave() - end), - mouse_bindings_enabled = false, - cached_ranges = nil -} -local forced_key_bindings -- defined at the bottom next to events - --- HELPERS - -function round(number) - local modulus = number % 1 - return modulus < 0.5 and math.floor(number) or math.ceil(number) +-- Normalize values +options.proximity_out = math.max(options.proximity_out, options.proximity_in + 1) +if options.chapter_ranges:sub(1, 4) == '^op|' then options.chapter_ranges = defaults.chapter_ranges end +if options.pause_on_click_shorter_than > 0 and options.click_threshold == 0 then + msg.warn('`pause_on_click_shorter_than` is deprecated. Use `click_threshold` and `click_command` instead.') + options.click_threshold = options.pause_on_click_shorter_than end - -function call_me_maybe(fn, value1, value2, value3) - if fn then fn(value1, value2, value3) end +if options.total_time and options.destination_time == 'playtime-remaining' then + msg.warn('`total_time` is deprecated. Use `destination_time` instead.') + options.destination_time = 'total' +elseif not itable_index_of({'total', 'playtime-remaining', 'time-remaining'}, options.destination_time) then + options.destination_time = 'playtime-remaining' end +-- Ensure required environment configuration +if options.autoload then mp.commandv('set', 'keep-open-pause', 'no') end +-- Color shorthands +fg, bg = serialize_rgba(options.foreground).color, serialize_rgba(options.background).color +fgt, bgt = serialize_rgba(options.foreground_text).color, serialize_rgba(options.background_text).color -function split(str, pattern) - local list = {} - local full_pattern = '(.-)' .. pattern - local last_end = 1 - local start_index, end_index, capture = str:find(full_pattern, 1) - while start_index do - list[#list + 1] = capture - last_end = end_index + 1 - start_index, end_index, capture = str:find(full_pattern, last_end) - end - if last_end <= (#str + 1) then - capture = str:sub(last_end) - list[#list + 1] = capture - end - return list -end +--[[ CONFIG ]] -function itable_find(haystack, needle) - local is_needle = type(needle) == 'function' and needle or - function(index, value) return value == needle end - for index, value in ipairs(haystack) do - if is_needle(index, value) then return index, value end - end -end - -function itable_filter(haystack, needle) - local is_needle = type(needle) == 'function' and needle or - function(index, value) return value == needle end - local filtered = {} - for index, value in ipairs(haystack) do - if is_needle(index, value) then filtered[#filtered + 1] = value end - end - return filtered -end - -function itable_remove(haystack, needle) - local should_remove = type(needle) == 'function' and needle or - function(value) return value == needle end - local new_table = {} - for _, value in ipairs(haystack) do - if not should_remove(value) then - new_table[#new_table + 1] = value - end - end - return new_table -end - -function itable_slice(haystack, start_pos, end_pos) - start_pos = start_pos and start_pos or 1 - end_pos = end_pos and end_pos or #haystack - - if end_pos < 0 then end_pos = #haystack + end_pos + 1 end - if start_pos < 0 then start_pos = #haystack + start_pos + 1 end - - local new_table = {} - for index, value in ipairs(haystack) do - if index >= start_pos and index <= end_pos then - new_table[#new_table + 1] = value - end - end - return new_table -end - -function table_copy(table) - local new_table = {} - for key, value in pairs(table) do new_table[key] = value end - return new_table -end - --- Sorting comparator close to (but not exactly) how file explorers sort files -local word_order_comparator = (function() - local symbol_order - local default_order - - if state.os == 'win' then - symbol_order = { - ['!'] = 1, - ['#'] = 2, - ['$'] = 3, - ['%'] = 4, - ['&'] = 5, - ['('] = 6, - [')'] = 6, - [','] = 7, - ['.'] = 8, - ["'"] = 9, - ['-'] = 10, - [';'] = 11, - ['@'] = 12, - ['['] = 13, - [']'] = 13, - ['^'] = 14, - ['_'] = 15, - ['`'] = 16, - ['{'] = 17, - ['}'] = 17, - ['~'] = 18, - ['+'] = 19, - ['='] = 20 - } - default_order = 21 - else - symbol_order = { - ['`'] = 1, - ['^'] = 2, - ['~'] = 3, - ['='] = 4, - ['_'] = 5, - ['-'] = 6, - [','] = 7, - [';'] = 8, - ['!'] = 9, - ["'"] = 10, - ['('] = 11, - [')'] = 11, - ['['] = 12, - [']'] = 12, - ['{'] = 13, - ['}'] = 14, - ['@'] = 15, - ['$'] = 16, - ['*'] = 17, - ['&'] = 18, - ['%'] = 19, - ['+'] = 20, - ['.'] = 22, - ['#'] = 23 - } - default_order = 21 - end - - return function(a, b) - a = a:lower() - b = b:lower() - for i = 1, math.max(#a, #b) do - local ai = a:sub(i, i) - local bi = b:sub(i, i) - if ai == nil and bi then return true end - if bi == nil and ai then return false end - local a_order = symbol_order[ai] or default_order - local b_order = symbol_order[bi] or default_order - if a_order == b_order then - return a < b - else - return a_order < b_order - end - end - end -end)() - --- Creates in-between frames to animate value from `from` to `to` numbers. --- Returns function that terminates animation. --- `to` can be a function that returns target value, useful for movable targets. --- `speed` is an optional float between 1-instant and 0-infinite duration --- `callback` is called either on animation end, or when animation is canceled -function tween(from, to, setter, speed, callback) - if type(speed) ~= 'number' then - callback = speed - speed = 0.3 - end - local timeout - local getTo = type(to) == 'function' and to or function() return to end - local cutoff = math.abs(getTo() - from) * 0.01 - function tick() - from = from + ((getTo() - from) * speed) - local is_end = math.abs(getTo() - from) <= cutoff - setter(is_end and getTo() or from) - request_render() - if is_end then - call_me_maybe(callback) - else - timeout:resume() - end - end - timeout = mp.add_timeout(0.016, tick) - tick() - return function() - timeout:kill() - call_me_maybe(callback) - end -end - --- Kills ongoing animation if one is already running on this element. --- Killed animation will not get its `on_end` called. -function tween_element(element, from, to, setter, speed, callback) - if type(speed) ~= 'number' then - callback = speed - speed = 0.3 - end - - tween_element_stop(element) - - element.stop_current_animation = tween(from, to, function(value) - setter(element, value) - end, speed, function() - element.stop_current_animation = nil - call_me_maybe(callback, element) - end) -end - --- Stopped animation will not get its on_end called. -function tween_element_is_tweening(element) - return element and element.stop_current_animation -end - --- Stopped animation will not get its on_end called. -function tween_element_stop(element) - call_me_maybe(element and element.stop_current_animation) -end - --- Helper to automatically use an element property setter -function tween_element_property(element, prop, from, to, speed, callback) - tween_element(element, from, to, - function(_, value) element[prop] = value end, speed, callback) -end - -function get_point_to_rectangle_proximity(point, rect) - local dx = math.max(rect.ax - point.x, 0, point.x - rect.bx + 1) - local dy = math.max(rect.ay - point.y, 0, point.y - rect.by + 1) - return math.sqrt(dx * dx + dy * dy); -end - -function text_width_estimate(letters, font_size) - return letters and letters * font_size * - options.font_height_to_letter_width_ratio or 0 -end - -function opacity_to_alpha(opacity) return 255 - math.ceil(255 * opacity) end - -function ass_opacity(opacity, fraction) - fraction = fraction ~= nil and fraction or 1 - if type(opacity) == 'number' then - return string.format('{\\alpha&H%X&}', - opacity_to_alpha(opacity * fraction)) - else - return string.format('{\\1a&H%X&\\2a&H%X&\\3a&H%X&\\4a&H%X&}', - opacity_to_alpha((opacity[1] or 0) * fraction), - opacity_to_alpha((opacity[2] or 0) * fraction), - opacity_to_alpha((opacity[3] or 0) * fraction), - opacity_to_alpha((opacity[4] or 0) * fraction)) - end -end - --- Ensures path is absolute and normalizes slashes to the current platform -function normalize_path(path) - if not path or is_protocol(path) then return path end - - -- Ensure path is absolute - if not (path:match('^/') or path:match('^%a+:') or path:match('^\\\\')) then - path = utils.join_path(state.cwd, path) - end - - -- Use proper slashes - if state.os == 'windows' then - return path:gsub('/', '\\') - else - return path:gsub('\\', '/') - end -end - --- Check if path is a protocol, such as `http://...` -function is_protocol(path) return path:match('^%a[%a%d-_]+://') end - -function get_extension(path) - local parts = split(path, '%.') - return parts and #parts > 1 and parts[#parts] or nil -end - --- Serializes path into its semantic parts -function serialize_path(path) - if not path or is_protocol(path) then return end - path = normalize_path(path) - local parts = split(path, '[\\/]+') - if parts[#parts] == '' then table.remove(parts, #parts) end -- remove trailing separator - local basename = parts and parts[#parts] or path - local dirname = #parts > 1 and - table.concat(itable_slice(parts, 1, #parts - 1), - state.os == 'windows' and '\\' or '/') or - nil - local dot_split = split(basename, '%.') - return { - path = path:sub(-1) == ':' and state.os == 'windows' and path .. '\\' or - path, - is_root = dirname == nil, - dirname = dirname, - basename = basename, - filename = #dot_split > 1 and - table.concat(itable_slice(dot_split, 1, #dot_split - 1), '.') or - basename, - extension = #dot_split > 1 and dot_split[#dot_split] or nil - } -end - -function get_files_in_directory(directory, allowed_types) - local files, error = utils.readdir(directory, 'files') - - if not files then - msg.error('Retrieving files failed: ' .. (error or '')) - return - end - - -- Filter only requested file types - if allowed_types then - files = itable_filter(files, function(_, file) - local extension = get_extension(file) - return extension and itable_find(allowed_types, extension:lower()) - end) - end - - table.sort(files, word_order_comparator) - - return files -end - -function get_adjacent_file(file_path, direction, allowed_types) - local current_file = serialize_path(file_path) - local files = get_files_in_directory(current_file.dirname, allowed_types) - - if not files then return end - - for index, file in ipairs(files) do - if current_file.basename == file then - if direction == 'forward' then - if files[index + 1] then - return utils.join_path(current_file.dirname, - files[index + 1]) - end - if options.directory_navigation_loops and files[1] then - return utils.join_path(current_file.dirname, files[1]) - end - else - if files[index - 1] then - return utils.join_path(current_file.dirname, - files[index - 1]) - end - if options.directory_navigation_loops and files[#files] then - return utils.join_path(current_file.dirname, files[#files]) - end - end - - -- This is the only file in directory - return nil - end - end -end - --- Ensures chapters are in chronological order -function get_normalized_chapters() - local chapters = mp.get_property_native('chapter-list') - - if not chapters then return end - - -- Copy table - chapters = itable_slice(chapters) - - -- Ensure chronological order of chapters - table.sort(chapters, function(a, b) return a.time < b.time end) - - return chapters -end - --- Element ---[[ -Signature: -{ - -- enables capturing button groups for this element - captures = {mouse_buttons = true, wheel = true}, - -- element rectangle coordinates - ax = 0, ay = 0, bx = 0, by = 0, - -- cursor<>element relative proximity as a 0-1 floating number - -- where 0 = completely away, and 1 = touching/hovering - -- so it's easy to work with and throw into equations - proximity = 0, - -- raw cursor<>element proximity in pixels - proximity_raw = infinity, - -- called when element is created - ?init = function(this), - -- called manually when disposing of element - ?destroy = function(this), - -- triggered when event happens and cursor is above element - ?on_{event_name} = function(this), - -- triggered when any event happens anywhere on a page - ?on_global_{event_name} = function(this), - -- object - ?render = function(this_element), -} -]] -local Element = { - captures = nil, - ax = 0, - ay = 0, - bx = 0, - by = 0, - proximity = 0, - proximity_raw = infinity -} -Element.__index = Element - -function Element.new(props) - local element = setmetatable(props, Element) - element._eventListeners = {} - - -- Flash timer - element._flash_out_timer = mp.add_timeout(options.flash_duration / 1000, - function() - local getTo = function() return element.proximity end - element:tween_property('forced_proximity', 1, getTo, - function() element.forced_proximity = nil end) - end) - element._flash_out_timer:kill() - - element:init() - - return element -end - -function Element:init() end -function Element:destroy() end - --- Call method if it exists -function Element:maybe(name, ...) - if self[name] then return self[name](self, ...) end -end - --- Tween helpers -function Element:tween(...) tween_element(self, ...) end -function Element:tween_property(...) tween_element_property(self, ...) end -function Element:tween_stop() tween_element_stop(self) end -function Element:is_tweening() tween_element_is_tweening(self) end - --- Event listeners -function Element:on(name, handler) - if self._eventListeners[name] == nil then self._eventListeners[name] = {} end - local preexistingIndex = itable_find(self._eventListeners[name], handler) - if preexistingIndex then - return - else - self._eventListeners[name][#self._eventListeners[name] + 1] = handler - end -end -function Element:off(name, handler) - if self._eventListeners[name] == nil then return end - local index = itable_find(self._eventListeners, handler) - if index then table.remove(self._eventListeners, index) end -end -function Element:trigger(name, ...) - self:maybe('on_' .. name, ...) - if self._eventListeners[name] == nil then return end - for _, handler in ipairs(self._eventListeners[name]) do handler(...) end -end - --- Briefly flashes the element for `options.flash_duration` milliseconds. --- Useful to visualize changes of volume and timeline when changed via hotkeys. --- Implemented by briefly adding animated `forced_proximity` property to the element. -function Element:flash() - if options.flash_duration > 0 and - (self.proximity < 1 or self._flash_out_timer:is_enabled()) then - self:tween_stop() - self.forced_proximity = 1 - self._flash_out_timer:kill() - self._flash_out_timer:resume() - end -end - --- ELEMENTS - -local Elements = {itable = {}} -Elements.__index = Elements -local elements = setmetatable({}, Elements) - -function Elements:add(name, element) - local insert_index = #Elements.itable + 1 - - -- Replace if element already exists - if self:has(name) then - insert_index = itable_find(Elements.itable, function(_, element) - return element.name == name - end) - end - - element.name = name - Elements.itable[insert_index] = element - self[name] = element - - request_render() -end - -function Elements:remove(name, props) - Elements.itable = itable_remove(Elements.itable, self[name]) - self[name] = nil - request_render() -end - -function Elements:has(name) return self[name] ~= nil end -function Elements:ipairs() return ipairs(Elements.itable) end -function Elements:pairs(elements) return pairs(self) end - --- MENU ---[[ -Usage: -``` -local items = { - {title = 'Foo title', hint = 'Ctrl+F', value = 'foo'}, - {title = 'Bar title', hint = 'Ctrl+B', value = 'bar'}, - { - title = 'Submenu', - items = { - {title = 'Sub item 1', value = 'sub1'}, - {title = 'Sub item 2', value = 'sub2'} - } +function create_default_menu() + return { + {title = 'Subtitles', value = 'script-binding uosc/subtitles'}, + {title = 'Audio tracks', value = 'script-binding uosc/audio'}, + {title = 'Stream quality', value = 'script-binding uosc/stream-quality'}, + {title = 'Playlist', value = 'script-binding uosc/items'}, + {title = 'Chapters', value = 'script-binding uosc/chapters'}, + {title = 'Navigation', items = { + {title = 'Next', hint = 'playlist or file', value = 'script-binding uosc/next'}, + {title = 'Prev', hint = 'playlist or file', value = 'script-binding uosc/prev'}, + {title = 'Delete file & Next', value = 'script-binding uosc/delete-file-next'}, + {title = 'Delete file & Prev', value = 'script-binding uosc/delete-file-prev'}, + {title = 'Delete file & Quit', value = 'script-binding uosc/delete-file-quit'}, + {title = 'Open file', value = 'script-binding uosc/open-file'}, + },}, + {title = 'Utils', items = { + {title = 'Aspect ratio', items = { + {title = 'Default', value = 'set video-aspect-override "-1"'}, + {title = '16:9', value = 'set video-aspect-override "16:9"'}, + {title = '4:3', value = 'set video-aspect-override "4:3"'}, + {title = '2.35:1', value = 'set video-aspect-override "2.35:1"'}, + },}, + {title = 'Audio devices', value = 'script-binding uosc/audio-device'}, + {title = 'Editions', value = 'script-binding uosc/editions'}, + {title = 'Screenshot', value = 'async screenshot'}, + {title = 'Show in directory', value = 'script-binding uosc/show-in-directory'}, + {title = 'Open config folder', value = 'script-binding uosc/open-config-directory'}, + },}, + {title = 'Quit', value = 'quit'}, } +end + +config = { + version = uosc_version, + -- sets max rendering frequency in case the + -- native rendering frequency could not be detected + render_delay = 1 / 60, + font = mp.get_property('options/osd-font'), + osd_margin_x = mp.get_property('osd-margin-x'), + osd_margin_y = mp.get_property('osd-margin-y'), + osd_alignment_x = mp.get_property('osd-align-x'), + osd_alignment_y = mp.get_property('osd-align-y'), + types = { + video = split(options.video_types, ' *, *'), + audio = split(options.audio_types, ' *, *'), + image = split(options.image_types, ' *, *'), + subtitle = split(options.subtitle_types, ' *, *'), + media = split(options.video_types .. ',' .. options.audio_types .. ',' .. options.image_types, ' *, *'), + autoload = (function() + ---@type string[] + local option_values = {} + for _, name in ipairs(split(options.autoload_types, ' *, *')) do + local value = options[name .. '_types'] + if type(value) == 'string' then option_values[#option_values + 1] = value end + end + return split(table.concat(option_values, ','), ' *, *') + end)(), + }, + stream_quality_options = split(options.stream_quality_options, ' *, *'), + menu_items = (function() + local input_conf_property = mp.get_property_native('input-conf') + local input_conf_path = mp.command_native({ + 'expand-path', input_conf_property == '' and '~~/input.conf' or input_conf_property, + }) + local input_conf_meta, meta_error = utils.file_info(input_conf_path) + + -- File doesn't exist + if not input_conf_meta or not input_conf_meta.is_file then return create_default_menu() end + + local main_menu = {items = {}, items_by_command = {}} + local by_id = {} + + for line in io.lines(input_conf_path) do + local key, command, comment = string.match(line, '%s*([%S]+)%s+(.-)%s+#%s*(.-)%s*$') + local title = '' + if comment then + local comments = split(comment, '#') + local titles = itable_filter(comments, function(v, i) return v:match('^!') or v:match('^menu:') end) + if titles and #titles > 0 then + title = titles[1]:match('^!%s*(.*)%s*') or titles[1]:match('^menu:%s*(.*)%s*') + end + end + if title ~= '' then + local is_dummy = key:sub(1, 1) == '#' + local submenu_id = '' + local target_menu = main_menu + local title_parts = split(title or '', ' *> *') + + for index, title_part in ipairs(#title_parts > 0 and title_parts or {''}) do + if index < #title_parts then + submenu_id = submenu_id .. title_part + + if not by_id[submenu_id] then + local items = {} + by_id[submenu_id] = {items = items, items_by_command = {}} + target_menu.items[#target_menu.items + 1] = {title = title_part, items = items} + end + + target_menu = by_id[submenu_id] + else + if command == 'ignore' then break end + -- If command is already in menu, just append the key to it + if target_menu.items_by_command[command] then + local hint = target_menu.items_by_command[command].hint + target_menu.items_by_command[command].hint = hint and hint .. ', ' .. key or key + else + local item = { + title = title_part, + hint = not is_dummy and key or nil, + value = command, + } + target_menu.items_by_command[command] = item + target_menu.items[#target_menu.items + 1] = item + end + end + end + end + end + + if #main_menu.items > 0 then + return main_menu.items + else + -- Default context menu + return create_default_menu() + end + end)(), + chapter_ranges = (function() + ---@type table Alternative patterns. + local alt_patterns = {} + if options.chapter_range_patterns and options.chapter_range_patterns ~= '' then + for _, definition in ipairs(split(options.chapter_range_patterns, ';+ *')) do + local name_patterns = split(definition, ' *:') + local name, patterns = name_patterns[1], name_patterns[2] + if name and patterns then alt_patterns[name] = split(patterns, ',') end + end + end + + ---@type table + local ranges = {} + if options.chapter_ranges and options.chapter_ranges ~= '' then + for _, definition in ipairs(split(options.chapter_ranges, ' *,+ *')) do + local name_color = split(definition, ' *:+ *') + local name, color = name_color[1], name_color[2] + if name and color + and name:match('^[a-zA-Z0-9_]+$') and color:match('^[a-fA-F0-9]+$') + and (#color == 6 or #color == 8) then + local range = serialize_rgba(name_color[2]) + range.patterns = alt_patterns[name] + ranges[name_color[1]] = range + end + end + end + return ranges + end)(), } - -function open_item(value) - value -- value from `item.value` +-- Adds `{element}_persistency` property with table of flags when the element should be visible (`{paused = true}`) +for _, name in ipairs({'timeline', 'controls', 'volume', 'top_bar', 'speed'}) do + local option_name = name .. '_persistency' + local value, flags = options[option_name], {} + if type(value) == 'string' then + for _, state in ipairs(split(value, ' *, *')) do flags[state] = true end + end + config[option_name] = flags end -menu:open(items, open_item) -``` -]] -local Menu = {} -Menu.__index = Menu -local menu = setmetatable({key_bindings = {}, is_closing = false}, Menu) +--[[ STATE ]] -function Menu:is_open(menu_type) - return elements.menu ~= nil and - (not menu_type or elements.menu.type == menu_type) -end +display = {width = 1280, height = 720, scale_x = 1, scale_y = 1, initialized = false} +cursor = { + x = 0, + y = 0, + hidden = true, + hover_raw = false, + -- Event handlers that are only fired on cursor, bound during render loop. Guidelines: + -- - element activations (clicks) go to `on_primary_down` handler + -- - `on_primary_up` is only for clearing dragging/swiping, and prevents autohide when bound + on_primary_down = nil, + on_primary_up = nil, + on_wheel_down = nil, + on_wheel_up = nil, + -- Called at the beginning of each render + reset_handlers = function() + cursor.on_primary_down, cursor.on_primary_up = nil, nil + cursor.on_wheel_down, cursor.on_wheel_up = nil, nil + end, + -- Enables pointer key group captures needed by handlers (called at the end of each render) + mbtn_left_enabled = nil, + wheel_enabled = nil, + decide_keybinds = function() + local enable_mbtn_left = (cursor.on_primary_down or cursor.on_primary_up) ~= nil + local enable_wheel = (cursor.on_wheel_down or cursor.on_wheel_up) ~= nil + if enable_mbtn_left ~= cursor.mbtn_left_enabled then + mp[(enable_mbtn_left and 'enable' or 'disable') .. '_key_bindings']('mbtn_left') + cursor.mbtn_left_enabled = enable_mbtn_left + end + if enable_wheel ~= cursor.wheel_enabled then + mp[(enable_wheel and 'enable' or 'disable') .. '_key_bindings']('wheel') + cursor.wheel_enabled = enable_wheel + end + end, + -- Cursor auto-hiding after period of inactivity + autohide = function() + if not cursor.on_primary_up and not Menu:is_open() then handle_mouse_leave() end + end, + autohide_timer = (function() + local timer = mp.add_timeout(mp.get_property_native('cursor-autohide') / 1000, function() cursor.autohide() end) + timer:kill() + return timer + end)(), + queue_autohide = function() + if options.autohide and not cursor.on_primary_up then + cursor.autohide_timer:kill() + cursor.autohide_timer:resume() + end + end +} +state = { + platform = (function() + local platform = mp.get_property_native('platform') + if platform then + if itable_index_of({'windows', 'darwin'}, platform) then return platform end + else + if os.getenv('windir') ~= nil then return 'windows' end + local homedir = os.getenv('HOME') + if homedir ~= nil and string.sub(homedir, 1, 6) == '/Users' then return 'darwin' end + end + return 'linux' + end)(), + cwd = mp.get_property('working-directory'), + path = nil, -- current file path or URL + title = nil, + alt_title = nil, + time = nil, -- current media playback time + speed = 1, + duration = nil, -- current media duration + time_human = nil, -- current playback time in human format + destination_time_human = nil, -- depends on options.destination_time + pause = mp.get_property_native('pause'), + chapters = {}, + current_chapter = nil, + chapter_ranges = {}, + border = mp.get_property_native('border'), + fullscreen = mp.get_property_native('fullscreen'), + maximized = mp.get_property_native('window-maximized'), + fullormaxed = mp.get_property_native('fullscreen') or mp.get_property_native('window-maximized'), + render_timer = nil, + render_last_time = 0, + volume = nil, + volume_max = nil, + mute = nil, + is_idle = false, + is_video = false, + is_audio = false, -- true if file is audio only (mp3, etc) + is_image = false, + is_stream = false, + has_audio = false, + has_sub = false, + has_chapter = false, + has_playlist = false, + shuffle = options.shuffle, + mouse_bindings_enabled = false, + uncached_ranges = nil, + cache = nil, + cache_buffering = 100, + cache_underrun = false, + core_idle = false, + eof_reached = false, + render_delay = config.render_delay, + first_real_mouse_move_received = false, + playlist_count = 0, + playlist_pos = 0, + margin_top = 0, + margin_bottom = 0, + margin_left = 0, + margin_right = 0, + hidpi_scale = 1, +} +thumbnail = {width = 0, height = 0, disabled = false} +external = {} -- Properties set by external scripts +key_binding_overwrites = {} -- Table of key_binding:mpv_command +Elements = require('uosc_shared/elements/Elements') +Menu = require('uosc_shared/elements/Menu') -function Menu:open(items, open_item, opts) - opts = opts or {} +-- State dependent utilities +require('uosc_shared/lib/utils') +require('uosc_shared/lib/text') +require('uosc_shared/lib/ass') +require('uosc_shared/lib/menus') - if menu:is_open() then - if not opts.parent_menu then - menu:close(true, function() - menu:open(items, open_item, opts) - end) - return - end - else - menu:enable_key_bindings() - elements.curtain:fadein() - end - - elements:add('menu', Element.new({ - captures = {mouse_buttons = true}, - type = nil, -- menu type such as `menu`, `chapters`, ... - title = nil, - width = nil, - height = nil, - offset_x = 0, -- used to animated from/to left when submenu - item_height = nil, - item_spacing = 1, - item_content_spacing = nil, - font_size = nil, - scroll_step = nil, - scroll_height = nil, - scroll_y = 0, - opacity = 0, - relative_parent_opacity = 0.4, - items = items, - active_item = nil, - selected_item = nil, - open_item = open_item, - parent_menu = nil, - init = function(this) - -- Already initialized - if this.width ~= nil then return end - - -- Apply options - for key, value in pairs(opts) do this[key] = value end - this.selected_item = this.active_item - - -- Set initial dimensions - this:on_display_resize() - - -- Scroll to active item - this:scroll_to_item(this.active_item) - - -- Transition in animation - menu.transition = {to = 'child', target = this} - local start_offset = this.parent_menu and - (this.parent_menu.width + this.width) / 2 or - 0 - - tween_element(menu.transition.target, 0, 1, function(_, pos) - this:set_offset_x(round(start_offset * (1 - pos))) - this.opacity = pos - this:set_parent_opacity(1 - - ((1 - config.menu_parent_opacity) * - pos)) - end, function() - menu.transition = nil - update_proximities() - end) - end, - destroy = function(this) request_render() end, - on_display_resize = function(this) - this.item_height = (state.fullscreen or state.maximized) and - options.menu_item_height_fullscreen or - options.menu_item_height - this.font_size = round(this.item_height * 0.48 * - options.menu_font_scale) - this.item_content_spacing = round( - (this.item_height - this.font_size) * - 0.6) - this.scroll_step = this.item_height + this.item_spacing - - -- Estimate width of a widest item - local estimated_max_width = 0 - for _, item in ipairs(items) do - local item_text_length = - ((item.title and item.title:len() or 0) + - (item.hint and item.hint:len() or 0)) - local spacings_in_item = item.hint and 3 or 2 - local estimated_width = text_width_estimate(item_text_length, - this.font_size) + - (this.item_content_spacing * - spacings_in_item) - if estimated_width > estimated_max_width then - estimated_max_width = estimated_width - end - end - - -- Also check menu title - local menu_title_length = this.title and this.title:len() or 0 - local estimated_menu_title_width = - text_width_estimate(menu_title_length, this.font_size) - if estimated_menu_title_width > estimated_max_width then - estimated_max_width = estimated_menu_title_width - end - - -- Coordinates and sizes are of the scrollable area to make - -- consuming values in rendering easier. Title drawn above this, so - -- we need to account for that in max_height and ay position. - this.width = round(math.min(math.max(estimated_max_width, - config.menu_min_width), - display.width * 0.9)) - local title_height = this.title and this.scroll_step or 0 - local max_height = round(display.height * 0.9) - title_height - this.height = math.min(round(this.scroll_step * #items) - - this.item_spacing, max_height) - this.scroll_height = math.max( - (this.scroll_step * #this.items) - - this.height - this.item_spacing, 0) - this.ax = round((display.width - this.width) / 2) + this.offset_x - this.ay = round((display.height - this.height) / 2 + - (title_height / 2)) - this.bx = round(this.ax + this.width) - this.by = round(this.ay + this.height) - - if this.parent_menu then - this.parent_menu:on_display_resize() - end - end, - set_items = function(this, items, props) - this.items = items - this.selected_item = nil - this.active_item = nil - if props then - for key, value in pairs(props) do - this[key] = value - end - end - this:on_display_resize() - request_render() - end, - set_offset_x = function(this, offset) - local delta = offset - this.offset_x - this.offset_x = offset - this.ax = this.ax + delta - this.bx = this.bx + delta - if this.parent_menu then - this.parent_menu:set_offset_x( - offset - ((this.width + this.parent_menu.width) / 2) - - this.item_spacing) - else - update_proximities() - end - end, - fadeout = function(this, callback) - this:tween(1, 0, function(this, pos) - this.opacity = pos - this:set_parent_opacity(pos * config.menu_parent_opacity) - end, callback) - end, - set_parent_opacity = function(this, opacity) - if this.parent_menu then - this.parent_menu.opacity = opacity - this.parent_menu:set_parent_opacity( - opacity * config.menu_parent_opacity) - end - end, - get_item_index_below_cursor = function(this) - return math.ceil((cursor.y - this.ay + this.scroll_y) / - this.scroll_step) - end, - get_first_visible_index = function(this) - return round(this.scroll_y / this.scroll_step) + 1 - end, - get_last_visible_index = function(this) - return round((this.scroll_y + this.height) / this.scroll_step) - end, - get_centermost_visible_index = function(this) - return round((this.scroll_y + (this.height / 2)) / this.scroll_step) - end, - scroll_to = function(this, pos) - this.scroll_y = math.max(math.min(pos, this.scroll_height), 0) - request_render() - end, - scroll_to_item = function(this, index) - if (index and index >= 1 and index <= #this.items) then - this:scroll_to(round((this.scroll_step * (index - 1)) - - ((this.height - this.scroll_step) / 2))) - end - end, - select_index = function(this, index) - this.selected_item = - (index and index >= 1 and index <= #this.items) and index or nil - request_render() - end, - select_value = function(this, value) - this:select_index(itable_find(this.items, function(_, item) - return item.value == value - end)) - end, - activate_index = function(this, index) - this.active_item = - (index and index >= 1 and index <= #this.items) and index or nil - request_render() - end, - activate_value = function(this, value) - this:activate_index(itable_find(this.items, function(_, item) - return item.value == value - end)) - end, - delete_index = function(this, index) - if (index and index >= 1 and index <= #this.items) then - local previous_active_value = - this.active_index and this.items[this.active_index].value or - nil - table.remove(this.items, index) - this:on_display_resize() - if previous_active_value then - this:activate_value(previous_active_value) - end - this:scroll_to_item(this.selected_item) - end - end, - delete_value = function(this, value) - this:delete_index(itable_find(this.items, function(_, item) - return item.value == value - end)) - end, - prev = function(this) - local default_anchor = this.scroll_height > this.scroll_step and - this:get_centermost_visible_index() or - this:get_last_visible_index() - local current_index = this.selected_item or default_anchor + 1 - this.selected_item = math.max(current_index - 1, 1) - this:scroll_to_item(this.selected_item) - end, - next = function(this) - local default_anchor = this.scroll_height > this.scroll_step and - this:get_centermost_visible_index() or - this:get_first_visible_index() - local current_index = this.selected_item or default_anchor - 1 - this.selected_item = math.min(current_index + 1, #this.items) - this:scroll_to_item(this.selected_item) - end, - back = function(this) - if menu.transition then - local transition_target = menu.transition.target - local transition_target_type = menu.transition.target - tween_element_stop(transition_target) - if transition_target_type == 'parent' then - elements:add('menu', transition_target) - end - menu.transition = nil - transition_target:back() - return - else - menu.transition = {to = 'parent', target = this.parent_menu} - end - - if menu.transition.target == nil then - menu:close() - return - end - - local target = menu.transition.target - local to_offset = -target.offset_x + this.offset_x - - tween_element(target, 0, 1, function(_, pos) - this:set_offset_x(round(to_offset * pos)) - this.opacity = 1 - pos - this:set_parent_opacity(config.menu_parent_opacity + - ((1 - config.menu_parent_opacity) * - pos)) - end, function() - menu.transition = nil - elements:add('menu', target) - update_proximities() - end) - end, - open_selected_item = function(this) - -- If there is a transition active and this method got called, it - -- means we are animating from this menu to parent menu, and all - -- calls to this method should be relayed to the parent menu. - if menu.transition and menu.transition.to == 'parent' then - local target = menu.transition.target - tween_element_stop(target) - menu.transition = nil - target:open_selected_item() - return - end - - if this.selected_item then - local item = this.items[this.selected_item] - -- Is submenu - if item.items then - local opts = table_copy(opts) - opts.parent_menu = this - menu:open(item.items, this.open_item, opts) - else - menu:close(true) - this.open_item(item.value) - end - end - end, - close = function(this) menu:close() end, - on_global_mbtn_left_down = function(this) - if this.proximity_raw == 0 then - this.selected_item = this:get_item_index_below_cursor() - this:open_selected_item() - else - -- check if this is clicking on any parent menus - local parent_menu = this.parent_menu - repeat - if parent_menu then - if get_point_to_rectangle_proximity(cursor, parent_menu) == - 0 then - this:back() - return - end - parent_menu = parent_menu.parent_menu - end - until parent_menu == nil - - menu:close() - end - end, - on_global_mouse_move = function(this) - if this.proximity_raw == 0 then - this.selected_item = this:get_item_index_below_cursor() - else - if this.selected_item then - this.selected_item = nil - end - end - request_render() - end, - on_wheel_up = function(this) - this.selected_item = nil - this:scroll_to(this.scroll_y - this.scroll_step) - -- Selects item below cursor - this:on_global_mouse_move() - request_render() - end, - on_wheel_down = function(this) - this.selected_item = nil - this:scroll_to(this.scroll_y + this.scroll_step) - -- Selects item below cursor - this:on_global_mouse_move() - request_render() - end, - on_pgup = function(this) - this.selected_item = nil - this:scroll_to(this.scroll_y - this.height) - end, - on_pgdwn = function(this) - this.selected_item = nil - this:scroll_to(this.scroll_y + this.height) - end, - on_home = function(this) - this.selected_item = nil - this:scroll_to(0) - end, - on_end = function(this) - this.selected_item = nil - this:scroll_to(this.scroll_height) - end, - render = render_menu - })) - - elements.menu:maybe('on_open') -end - -function Menu:add_key_binding(key, name, fn, flags) - menu.key_bindings[#menu.key_bindings + 1] = name - mp.add_forced_key_binding(key, name, fn, flags) -end - -function Menu:enable_key_bindings() - menu.key_bindings = {} - -- The `mp.set_key_bindings()` method would be easier here, but that - -- doesn't support 'repeatable' flag, so we are stuck with this monster. - menu:add_key_binding('up', 'menu-prev', self:create_action('prev'), - 'repeatable') - menu:add_key_binding('down', 'menu-next', self:create_action('next'), - 'repeatable') - menu:add_key_binding('left', 'menu-back', self:create_action('back')) - menu:add_key_binding('right', 'menu-select', - self:create_action('open_selected_item')) - - if options.menu_wasd_navigation then - menu:add_key_binding('w', 'menu-prev-alt', self:create_action('prev'), - 'repeatable') - menu:add_key_binding('a', 'menu-back-alt', self:create_action('back')) - menu:add_key_binding('s', 'menu-next-alt', self:create_action('next'), - 'repeatable') - menu:add_key_binding('d', 'menu-select-alt', - self:create_action('open_selected_item')) - end - - if options.menu_hjkl_navigation then - menu:add_key_binding('h', 'menu-back-alt2', self:create_action('back')) - menu:add_key_binding('j', 'menu-next-alt2', self:create_action('next'), - 'repeatable') - menu:add_key_binding('k', 'menu-prev-alt2', self:create_action('prev'), - 'repeatable') - menu:add_key_binding('l', 'menu-select-alt2', - self:create_action('open_selected_item')) - end - - menu:add_key_binding('mbtn_back', 'menu-back-alt3', - self:create_action('back')) - menu:add_key_binding('bs', 'menu-back-alt4', self:create_action('back')) - menu:add_key_binding('enter', 'menu-select-alt3', - self:create_action('open_selected_item')) - menu:add_key_binding('kp_enter', 'menu-select-alt4', - self:create_action('open_selected_item')) - menu:add_key_binding('esc', 'menu-close', self:create_action('close')) - menu:add_key_binding('pgup', 'menu-page-up', self:create_action('on_pgup')) - menu:add_key_binding('pgdwn', 'menu-page-down', - self:create_action('on_pgdwn')) - menu:add_key_binding('home', 'menu-home', self:create_action('on_home')) - menu:add_key_binding('end', 'menu-end', self:create_action('on_end')) -end - -function Menu:disable_key_bindings() - for _, name in ipairs(menu.key_bindings) do mp.remove_key_binding(name) end - menu.key_bindings = {} -end - -function Menu:create_action(name) - return function(...) - if elements.menu then elements.menu:maybe(name, ...) end - end -end - -function Menu:close(immediate, callback) - if type(immediate) ~= 'boolean' then callback = immediate end - - if elements:has('menu') and not menu.is_closing then - function close() - elements.menu:maybe('on_close') - elements.menu:destroy() - elements:remove('menu') - menu.is_closing = false - update_proximities() - menu:disable_key_bindings() - call_me_maybe(callback) - end - - menu.is_closing = true - elements.curtain:fadeout() - - if immediate then - close() - else - elements.menu:fadeout(close) - end - end -end - --- ICONS ---[[ -ASS \shadN shadows are drawn also below the element, which when there is an -opacity in play, blends icon colors into ugly greys. The mess below is an -attempt to fix it by rendering shadows for icons with clipping. - -Add icons by adding functions to render them to `icons` table. - -Signature: function(pos_x, pos_y, size) => string - -Function has to return ass path coordinates to draw the icon centered at pox_x -and pos_y of passed size. -]] -local icons = {} -function icon(name, icon_x, icon_y, icon_size, shad_x, shad_y, shad_size, - backdrop, opacity, clip) - local ass = assdraw.ass_new() - local icon_path = icons[name](icon_x, icon_y, icon_size) - local icon_color = options['color_' .. backdrop .. '_text'] - local shad_color = options['color_' .. backdrop] - local use_border = (shad_x + shad_y) == 0 - local icon_border = use_border and shad_size or 0 - - -- clip can't clip out shadows, a very annoying limitation I can't work - -- around without going back to ugly default ass shadows, but atm I actually - -- don't need clipping of icons with shadows, so I'm choosing to ignore this - if not clip then clip = '' end - - if not use_border then - ass:new_event() - ass:append('{\\blur0\\bord0\\shad0\\1c&H' .. shad_color .. '\\iclip(' .. - ass.scale .. ', ' .. icon_path .. ')}') - ass:append(ass_opacity(opacity)) - ass:pos(shad_x + shad_size, shad_y + shad_size) - ass:draw_start() - ass:append(icon_path) - ass:draw_stop() - end - - ass:new_event() - ass:append( - '{\\blur0\\bord' .. icon_border .. '\\shad0\\1c&H' .. icon_color .. - '\\3c&H' .. shad_color .. clip .. '}') - ass:append(ass_opacity(opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:append(icon_path) - ass:draw_stop() - - return ass.text -end - -function icons._volume(muted, pos_x, pos_y, size) - local ass = assdraw.ass_new() - local scale = size / 200 - function x(number) return pos_x + (number * scale) end - function y(number) return pos_y + (number * scale) end - ass:move_to(x(-85), y(-35)) - ass:line_to(x(-50), y(-35)) - ass:line_to(x(-5), y(-75)) - ass:line_to(x(-5), y(75)) - ass:line_to(x(-50), y(35)) - ass:line_to(x(-85), y(35)) - if muted then - ass:move_to(x(76), y(-35)) - ass:line_to(x(50), y(-9)) - ass:line_to(x(24), y(-35)) - ass:line_to(x(15), y(-26)) - ass:line_to(x(41), y(0)) - ass:line_to(x(15), y(26)) - ass:line_to(x(24), y(35)) - ass:line_to(x(50), y(9)) - ass:line_to(x(76), y(35)) - ass:line_to(x(85), y(26)) - ass:line_to(x(59), y(0)) - ass:line_to(x(85), y(-26)) - else - ass:move_to(x(20), y(-30)) - ass:line_to(x(20), y(30)) - ass:line_to(x(35), y(30)) - ass:line_to(x(35), y(-30)) - - ass:move_to(x(55), y(-60)) - ass:line_to(x(55), y(60)) - ass:line_to(x(70), y(60)) - ass:line_to(x(70), y(-60)) - end - return ass.text -end -function icons.volume(pos_x, pos_y, size) - return icons._volume(false, pos_x, pos_y, size) -end -function icons.volume_muted(pos_x, pos_y, size) - return icons._volume(true, pos_x, pos_y, size) -end - -function icons.arrow_right(pos_x, pos_y, size) - local ass = assdraw.ass_new() - local scale = size / 200 - function x(number) return pos_x + (number * scale) end - function y(number) return pos_y + (number * scale) end - ass:move_to(x(-22), y(-80)) - ass:line_to(x(-45), y(-57)) - ass:line_to(x(12), y(0)) - ass:line_to(x(-45), y(57)) - ass:line_to(x(-22), y(80)) - ass:line_to(x(58), y(0)) - return ass.text -end - --- STATE UPDATES +--[[ STATE UPDATERS ]] function update_display_dimensions() - local o = mp.get_property_native('osd-dimensions') - display.width = o.w - display.height = o.h - display.aspect = o.aspect + local scale = (state.hidpi_scale or 1) * options.ui_scale + local real_width, real_height = mp.get_osd_size() + if real_width <= 0 then return end + local scaled_width, scaled_height = round(real_width / scale), round(real_height / scale) + display.width, display.height = scaled_width, scaled_height + display.scale_x, display.scale_y = real_width / scaled_width, real_height / scaled_height + display.initialized = true - -- Tell elements about this - for _, element in elements:ipairs() do - if element.on_display_resize ~= nil then - element.on_display_resize(element) - end - end + -- Tell elements about this + Elements:trigger('display') + + -- Some elements probably changed their rectangles as a reaction to `display` + Elements:update_proximities() + request_render() end -function update_element_cursor_proximity(element) - if cursor.hidden then - element.proximity_raw = infinity - element.proximity = 0 - else - local range = options.proximity_out - options.proximity_in - element.proximity_raw = - get_point_to_rectangle_proximity(cursor, element) - element.proximity = menu:is_open() and 0 or 1 - - (math.min( - math.max( - element.proximity_raw - - options.proximity_in, 0), range) / - range) - end +function update_fullormaxed() + state.fullormaxed = state.fullscreen or state.maximized + update_display_dimensions() + Elements:trigger('prop_fullormaxed', state.fullormaxed) + update_cursor_position(INFINITY, INFINITY) end -function update_proximities() - local capture_mouse_buttons = false - local capture_wheel = false - local menu_only = menu:is_open() - local mouse_left_elements = {} - local mouse_entered_elements = {} - - -- Calculates proximities and opacities for defined elements - for _, element in elements:ipairs() do - local previous_proximity_raw = element.proximity_raw - - -- If menu is open, all other elements have to be disabled - if menu_only then - if element.name == 'menu' then - capture_mouse_buttons = true - capture_wheel = true - update_element_cursor_proximity(element) - else - element.proximity_raw = infinity - element.proximity = 0 - end - else - update_element_cursor_proximity(element) - end - - if element.proximity_raw == 0 then - -- Mouse is over element - if element.captures and element.captures.mouse_buttons then - capture_mouse_buttons = true - end - if element.captures and element.captures.wheel then - capture_wheel = true - end - - -- Mouse entered element area - if previous_proximity_raw ~= 0 then - mouse_entered_elements[#mouse_entered_elements + 1] = element - end - else - -- Mouse left element area - if previous_proximity_raw == 0 then - mouse_left_elements[#mouse_left_elements + 1] = element - end - end - end - - -- Enable key group captures elements request. - if capture_mouse_buttons then - forced_key_bindings.mouse_buttons:enable() - else - forced_key_bindings.mouse_buttons:disable() - end - if capture_wheel then - forced_key_bindings.wheel:enable() - else - forced_key_bindings.wheel:disable() - end - - -- Trigger `mouse_leave` and `mouse_enter` events - for _, element in ipairs(mouse_left_elements) do - element:trigger('mouse_leave') - end - for _, element in ipairs(mouse_entered_elements) do - element:trigger('mouse_enter') - end +function update_human_times() + if state.time then + state.time_human = format_time(state.time, state.duration) + if state.duration then + local speed = state.speed or 1 + if options.destination_time == 'playtime-remaining' then + state.destination_time_human = format_time((state.time - state.duration) / speed, state.duration) + elseif options.destination_time == 'total' then + state.destination_time_human = format_time(state.duration, state.duration) + else + state.destination_time_human = format_time(state.time - state.duration, state.duration) + end + else + state.destination_time_human = nil + end + else + state.time_human = nil + end end --- ELEMENT RENDERERS +-- Notifies other scripts such as console about where the unoccupied parts of the screen are. +function update_margins() + if display.height == 0 then return end -function render_timeline(this) - if this.size_max == 0 or state.duration == nil or state.position == nil then - return - end + local function is_persistent(element) return element and element.enabled and element:is_persistent() end + local timeline, top_bar, controls, volume = Elements.timeline, Elements.top_bar, Elements.controls, Elements.volume + -- margins are normalized to window size + local left, right, top, bottom = 0, 0, 0, 0 - local size_min = this:get_effective_size_min() - local size = this:get_effective_size() + if is_persistent(controls) then bottom = (display.height - controls.ay) / display.height + elseif is_persistent(timeline) then bottom = (display.height - timeline.ay) / display.height end - if size < 1 then return end + if is_persistent(top_bar) then top = top_bar.title_by / display.height end - local ass = assdraw.ass_new() + if is_persistent(volume) then + if options.volume == 'left' then left = volume.bx / display.width + elseif options.volume == 'right' then right = volume.ax / display.width end + end - -- Text opacity rapidly drops to 0 just before it starts overflowing, or before it reaches timeline.size_min - local hide_text_below = math.max(this.font_size * 0.7, size_min * 2) - local hide_text_ramp = hide_text_below / 2 - local text_opacity = math.max(math.min(size - hide_text_below, - hide_text_ramp), 0) / hide_text_ramp + if top == state.margin_top and bottom == state.margin_bottom and + left == state.margin_left and right == state.margin_right then return end - local spacing = math.max(math.floor((this.size_max - this.font_size) / 2.5), - 4) - local progress = state.position / state.duration + state.margin_top = top + state.margin_bottom = bottom + state.margin_left = left + state.margin_right = right - -- Background bar coordinates - local bax = 0 - local bay = display.height - size - this.bottom_border - this.top_border - local bbx = display.width - local bby = display.height + utils.shared_script_property_set('osc-margins', string.format('%f,%f,%f,%f', 0, 0, top, bottom)) + mp.set_property_native('user-data/osc/margins', { l = left, r = right, t = top, b = bottom }) - -- Foreground bar coordinates - local fax = bax - local fay = bay + this.top_border - local fbx = bbx * progress - local fby = bby - this.bottom_border - local foreground_size = bby - bay - local foreground_coordinates = fax .. ',' .. fay .. ',' .. fbx .. ',' .. fby -- for clipping - - -- Background - ass:new_event() - ass:append( - '{\\blur0\\bord0\\1c&H' .. options.color_background .. '\\iclip(' .. - foreground_coordinates .. ')}') - ass:append(ass_opacity(math.max(options.timeline_opacity - 0.1, 0))) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(bax, bay, bbx, bby) - ass:draw_stop() - - -- Foreground - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. options.color_foreground .. '}') - ass:append(ass_opacity(options.timeline_opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(fax, fay, fbx, fby) - ass:draw_stop() - - -- Seekable ranges - if options.timeline_cached_ranges and state.cached_ranges then - local range_height = math.max(foreground_size / 8, size_min) - local range_ay = fby - range_height - for _, range in ipairs(state.cached_ranges) do - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. - options.timeline_cached_ranges.color .. '}') - ass:append(ass_opacity(options.timeline_cached_ranges.opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(bbx * (range['start'] / state.duration), range_ay, - bbx * (range['end'] / state.duration), - range_ay + range_height) - ass:draw_stop() - end - end - - -- Custom ranges - if state.chapter_ranges ~= nil then - for i, chapter_range in ipairs(state.chapter_ranges) do - for i, range in ipairs(chapter_range.ranges) do - local rax = display.width * - (range['start'].time / state.duration) - local rbx = display.width * (range['end'].time / state.duration) - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. chapter_range.color .. '}') - ass:append(ass_opacity(chapter_range.opacity)) - ass:pos(0, 0) - ass:draw_start() - -- for 1px chapter size, use the whole size of the bar including padding - if size <= 1 then - ass:rect_cw(rax, bay, rbx, bby) - else - ass:rect_cw(rax, fay, rbx, fby) - end - ass:draw_stop() - end - end - end - - -- Chapters - if options.chapters ~= 'none' and state.chapters ~= nil and #state.chapters > - 0 then - local half_size = size / 2 - local dots = false - local chapter_size, chapter_y - if options.chapters == 'dots' then - dots = true - chapter_size = math.min(6, (foreground_size / 2) + 2) - chapter_y = math.min(fay + chapter_size, fay + half_size) - elseif options.chapters == 'lines' then - chapter_size = size - chapter_y = fay + (chapter_size / 2) - elseif options.chapters == 'lines-top' then - chapter_size = math.min(this.size_max / 3.5, size) - chapter_y = fay + (chapter_size / 2) - elseif options.chapters == 'lines-bottom' then - chapter_size = math.min(this.size_max / 3.5, size) - chapter_y = fay + size - (chapter_size / 2) - end - - if chapter_size ~= nil then - -- for 1px chapter size, use the whole size of the bar including padding - chapter_size = size <= 1 and foreground_size or chapter_size - local chapter_half_size = chapter_size / 2 - - for i, chapter in ipairs(state.chapters) do - local chapter_x = display.width * - (chapter.time / state.duration) - local color = chapter_x > fbx and options.color_foreground or - options.color_background - - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. color .. '}') - ass:append(ass_opacity(options.chapters_opacity)) - ass:pos(0, 0) - ass:draw_start() - - if dots then - local bezier_stretch = chapter_size * 0.67 - ass:move_to(chapter_x - chapter_half_size, chapter_y) - ass:bezier_curve(chapter_x - chapter_half_size, - chapter_y - bezier_stretch, - chapter_x + chapter_half_size, - chapter_y - bezier_stretch, - chapter_x + chapter_half_size, chapter_y) - ass:bezier_curve(chapter_x + chapter_half_size, - chapter_y + bezier_stretch, - chapter_x - chapter_half_size, - chapter_y + bezier_stretch, - chapter_x - chapter_half_size, chapter_y) - else - ass:rect_cw(chapter_x, chapter_y - chapter_half_size, - chapter_x + 1, chapter_y + chapter_half_size) - end - - ass:draw_stop() - end - end - end - - if text_opacity > 0 then - -- Elapsed time - if state.elapsed_seconds then - ass:new_event() - ass:append('{\\blur0\\bord0\\shad0\\1c&H' .. - options.color_foreground_text .. '\\fn' .. - config.font .. '\\fs' .. this.font_size .. bold_tag .. - '\\clip(' .. foreground_coordinates .. ')') - ass:append(ass_opacity(math.min(options.timeline_opacity + 0.1, 1), - text_opacity)) - ass:pos(spacing, fay + (size / 2)) - ass:an(4) - ass:append(state.elapsed_time) - ass:new_event() - ass:append('{\\blur0\\bord0\\shad1\\1c&H' .. - options.color_background_text .. '\\4c&H' .. - options.color_background .. '\\fn' .. config.font .. - '\\fs' .. this.font_size .. bold_tag .. '\\iclip(' .. - foreground_coordinates .. ')') - ass:append(ass_opacity(math.min(options.timeline_opacity + 0.1, 1), - text_opacity)) - ass:pos(spacing, fay + (size / 2)) - ass:an(4) - ass:append(state.elapsed_time) - end - - -- Remaining time - if state.remaining_seconds then - ass:new_event() - ass:append('{\\blur0\\bord0\\shad0\\1c&H' .. - options.color_foreground_text .. '\\fn' .. - config.font .. '\\fs' .. this.font_size .. bold_tag .. - '\\clip(' .. foreground_coordinates .. ')') - ass:append(ass_opacity(math.min(options.timeline_opacity + 0.1, 1), - text_opacity)) - ass:pos(display.width - spacing, fay + (size / 2)) - ass:an(6) - ass:append('-' .. state.remaining_time) - ass:new_event() - ass:append('{\\blur0\\bord0\\shad1\\1c&H' .. - options.color_background_text .. '\\4c&H' .. - options.color_background .. '\\fn' .. config.font .. - '\\fs' .. this.font_size .. bold_tag .. '\\iclip(' .. - foreground_coordinates .. ')') - ass:append(ass_opacity(math.min(options.timeline_opacity + 0.1, 1), - text_opacity)) - ass:pos(display.width - spacing, fay + (size / 2)) - ass:an(6) - ass:append('-' .. state.remaining_time) - end - end - - if (this.proximity_raw == 0 or this.pressed) and - not (elements.speed and elements.speed.dragging) then - -- Hovered time - local hovered_seconds = state.duration * (cursor.x / display.width) - local box_half_width_guesstimate = (this.font_size * 4.2) / 2 - ass:new_event() - ass:append('{\\blur0\\bord1\\shad0\\1c&H' .. - options.color_background_text .. '\\3c&H' .. - options.color_background .. '\\fn' .. config.font .. - '\\fs' .. this.font_size .. bold_tag .. '') - ass:append(ass_opacity(math.min(options.timeline_opacity + 0.1, 1))) - ass:pos(math.min(math.max(cursor.x, box_half_width_guesstimate), - display.width - box_half_width_guesstimate), fay) - ass:an(2) - ass:append(mp.format_time(hovered_seconds)) - - -- Cursor line - ass:new_event() - ass:append('{\\blur0\\bord0\\xshad-1\\yshad0\\1c&H' .. - options.color_foreground .. '\\4c&H' .. - options.color_background .. '}') - ass:append(ass_opacity(0.2)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(cursor.x, fay, cursor.x + 1, fby) - ass:draw_stop() - end - - return ass + if not options.adjust_osd_margins then return end + local osd_margin_y, osd_margin_x, osd_factor_x = 0, 0, display.width / display.height * 720 + if config.osd_alignment_y == 'bottom' then osd_margin_y = round(bottom * 720) + elseif config.osd_alignment_y == 'top' then osd_margin_y = round(top * 720) end + if config.osd_alignment_x == 'left' then osd_margin_x = round(left * osd_factor_x) + elseif config.osd_alignment_x == 'right' then osd_margin_x = round(right * osd_factor_x) end + mp.set_property_native('osd-margin-y', osd_margin_y + config.osd_margin_y) + mp.set_property_native('osd-margin-x', osd_margin_x + config.osd_margin_x) +end +function create_state_setter(name, callback) + return function(_, value) + set_state(name, value) + if callback then callback() end + request_render() + end end -function render_top_bar(this) - local opacity = this:get_effective_proximity() - - if not this.enabled or opacity == 0 then return end - - local ass = assdraw.ass_new() - - if options.top_bar_controls then - -- Close button - local close = elements.window_controls_close - if close.proximity_raw == 0 then - -- Background on hover - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H2311e8}') - ass:append(ass_opacity(this.button_opacity, opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(close.ax, close.ay, close.bx, close.by) - ass:draw_stop() - end - ass:new_event() - ass:append('{\\blur0\\bord1\\shad1\\3c&HFFFFFF\\4c&H000000}') - ass:append(ass_opacity(this.button_opacity, opacity)) - ass:pos(close.ax + (this.button_width / 2), (this.size / 2)) - ass:draw_start() - ass:move_to(-this.icon_size, this.icon_size) - ass:line_to(this.icon_size, -this.icon_size) - ass:move_to(-this.icon_size, -this.icon_size) - ass:line_to(this.icon_size, this.icon_size) - ass:draw_stop() - - -- Maximize button - local maximize = elements.window_controls_maximize - if maximize.proximity_raw == 0 then - -- Background on hover - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H222222}') - ass:append(ass_opacity(this.button_opacity, opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(maximize.ax, maximize.ay, maximize.bx, maximize.by) - ass:draw_stop() - end - ass:new_event() - ass:append('{\\blur0\\bord2\\shad0\\1c\\3c&H000000}') - ass:append(ass_opacity({[3] = this.button_opacity}, opacity)) - ass:pos(maximize.ax + (this.button_width / 2), (this.size / 2)) - ass:draw_start() - ass:rect_cw(-this.icon_size + 1, -this.icon_size + 1, - this.icon_size + 1, this.icon_size + 1) - ass:draw_stop() - ass:new_event() - ass:append('{\\blur0\\bord2\\shad0\\1c\\3c&HFFFFFF}') - ass:append(ass_opacity({[3] = this.button_opacity}, opacity)) - ass:pos(maximize.ax + (this.button_width / 2), (this.size / 2)) - ass:draw_start() - ass:rect_cw(-this.icon_size, -this.icon_size, this.icon_size, - this.icon_size) - ass:draw_stop() - - -- Minimize button - local minimize = elements.window_controls_minimize - if minimize.proximity_raw == 0 then - -- Background on hover - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H222222}') - ass:append(ass_opacity(this.button_opacity, opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(minimize.ax, minimize.ay, minimize.bx, minimize.by) - ass:draw_stop() - end - ass:new_event() - ass:append('{\\blur0\\bord1\\shad1\\3c&HFFFFFF\\4c&H000000}') - ass:append(ass_opacity(this.button_opacity, opacity)) - ass:append('{\\1a&HFF&}') - ass:pos(minimize.ax + (this.button_width / 2), (this.size / 2)) - ass:draw_start() - ass:move_to(-this.icon_size, 0) - ass:line_to(this.icon_size, 0) - ass:draw_stop() - end - - -- Window title - if options.top_bar_title and state.media_title then - local clip_coordinates = - '0,0,' .. (this.title_bx - this.spacing) .. ',' .. this.size - - ass:new_event() - ass:append('{\\q2\\blur0\\bord1\\shad0\\1c&HFFFFFF\\3c&H000000\\fn' .. - config.font .. '\\fs' .. this.font_size .. bold_tag .. - '\\clip(' .. clip_coordinates .. ')') - ass:append(ass_opacity(1, opacity)) - ass:pos(0 + this.spacing, this.size / 2) - ass:an(4) - ass:append(state.media_title) - end - - return ass +function set_state(name, value) + state[name] = value + Elements:trigger('prop_' .. name, value) end -function render_volume(this) - local slider = elements.volume_slider - local opacity = this:get_effective_proximity() - - if this.width == 0 or opacity == 0 then return end - - local ass = assdraw.ass_new() - - if slider.height > 0 then - -- Background bar coordinates - local bax = slider.ax - local bay = slider.ay - local bbx = slider.bx - local bby = slider.by - - -- Foreground bar coordinates - local height_without_border = slider.height - - (options.volume_border * 2) - local fax = slider.ax + options.volume_border - local fay = slider.ay + - (height_without_border * - (1 - math.min(state.volume / state.volume_max, 1))) + - options.volume_border - local fbx = slider.bx - options.volume_border - local fby = slider.by - options.volume_border - - -- Path to draw a foreground bar with a 100% volume indicator, already - -- clipped by volume level. Can't just clip it with rectangle, as it itself - -- also needs to be used as a path to clip the background bar and volume - -- number. - local fpath = assdraw.ass_new() - fpath:move_to(fbx, fby) - fpath:line_to(fax, fby) - local nudge_bottom_y = slider.nudge_y + slider.nudge_size - if fay <= nudge_bottom_y and slider.draw_nudge then - fpath:line_to(fax, math.min(nudge_bottom_y)) - if fay <= slider.nudge_y then - fpath:line_to((fax + slider.nudge_size), slider.nudge_y) - local nudge_top_y = slider.nudge_y - slider.nudge_size - if fay <= nudge_top_y then - fpath:line_to(fax, nudge_top_y) - fpath:line_to(fax, fay) - fpath:line_to(fbx, fay) - fpath:line_to(fbx, nudge_top_y) - else - local triangle_side = fay - nudge_top_y - fpath:line_to((fax + triangle_side), fay) - fpath:line_to((fbx - triangle_side), fay) - end - fpath:line_to((fbx - slider.nudge_size), slider.nudge_y) - else - local triangle_side = nudge_bottom_y - fay - fpath:line_to((fax + triangle_side), fay) - fpath:line_to((fbx - triangle_side), fay) - end - fpath:line_to(fbx, nudge_bottom_y) - else - fpath:line_to(fax, fay) - fpath:line_to(fbx, fay) - end - fpath:line_to(fbx, fby) - - -- Background - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. options.color_background .. - '\\iclip(' .. fpath.scale .. ', ' .. fpath.text .. ')}') - ass:append(ass_opacity(math.max(options.volume_opacity - 0.1, 0), - opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:move_to(bax, bay) - ass:line_to(bbx, bay) - local half_border = options.volume_border / 2 - if slider.draw_nudge then - ass:line_to(bbx, math.max(slider.nudge_y - slider.nudge_size + - half_border, bay)) - ass:line_to(bbx - slider.nudge_size + half_border, slider.nudge_y) - ass:line_to(bbx, slider.nudge_y + slider.nudge_size - half_border) - end - ass:line_to(bbx, bby) - ass:line_to(bax, bby) - if slider.draw_nudge then - ass:line_to(bax, slider.nudge_y + slider.nudge_size - half_border) - ass:line_to(bax + slider.nudge_size - half_border, slider.nudge_y) - ass:line_to(bax, math.max(slider.nudge_y - slider.nudge_size + - half_border, bay)) - end - ass:line_to(bax, bay) - ass:draw_stop() - - -- Foreground - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. options.color_foreground .. '}') - ass:append(ass_opacity(options.volume_opacity, opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:append(fpath.text) - ass:draw_stop() - - -- Current volume value - local volume_string = tostring(round(state.volume * 10) / 10) - local font_size = round(((this.width * 0.6) - - (#volume_string * (this.width / 20))) * - options.volume_font_scale) - if fay < slider.by - slider.spacing then - ass:new_event() - ass:append('{\\blur0\\bord0\\shad0\\1c&H' .. - options.color_foreground_text .. '\\fn' .. - config.font .. '\\fs' .. font_size .. bold_tag .. - '\\clip(' .. fpath.scale .. ', ' .. fpath.text .. - ')}') - ass:append(ass_opacity(math.min(options.volume_opacity + 0.1, 1), - opacity)) - ass:pos(slider.ax + (slider.width / 2), slider.by - slider.spacing) - ass:an(2) - ass:append(volume_string) - end - if fay > slider.by - slider.spacing - font_size then - ass:new_event() - ass:append('{\\blur0\\bord0\\shad1\\1c&H' .. - options.color_background_text .. '\\4c&H' .. - options.color_background .. '\\fn' .. config.font .. - '\\fs' .. font_size .. bold_tag .. '\\iclip(' .. - fpath.scale .. ', ' .. fpath.text .. ')}') - ass:append(ass_opacity(math.min(options.volume_opacity + 0.1, 1), - opacity)) - ass:pos(slider.ax + (slider.width / 2), slider.by - slider.spacing) - ass:an(2) - ass:append(volume_string) - end - end - - -- Mute button - local mute = elements.volume_mute - local icon_name = state.mute and 'volume_muted' or 'volume' - ass:new_event() - ass:append(icon(icon_name, mute.ax + (mute.width / 2), - mute.ay + (mute.height / 2), mute.width * 0.7, -- x, y, size - 0, 0, options.volume_border, -- shadow_x, shadow_y, shadow_size - 'background', options.volume_opacity * opacity -- backdrop, opacity - )) - return ass -end - -function render_speed(this) - if not this.dragging and (elements.curtain.opacity > 0) then return end - - local timeline = elements.timeline - local proximity = timeline:get_effective_proximity() - local opacity = this.forced_proximity and this.forced_proximity or - (this.dragging and 1 or proximity) - - if opacity == 0 then return end - - local ass = assdraw.ass_new() - - -- Coordinates - local ax = this.ax - local ay = this.ay + timeline.size_max - timeline:get_effective_size() - - timeline.top_border - timeline.bottom_border - local bx = this.bx - local by = ay + this.height - local half_width = (this.width / 2) - local half_x = ax + half_width - - -- Notches - local speed_at_center = state.speed - if this.dragging then - speed_at_center = this.dragging.start_speed + - ((-this.dragging.distance / this.step_distance) * - options.speed_step) - speed_at_center = math.min(math.max(speed_at_center, 0.01), 100) - end - local nearest_notch_speed = round(speed_at_center / this.notch_every) * - this.notch_every - local nearest_notch_x = half_x + - (((nearest_notch_speed - speed_at_center) / - this.notch_every) * this.notch_spacing) - local guide_size = math.floor(this.height / 7.5) - local notch_by = by - guide_size - local notch_ay_big = ay + round(this.font_size * 1.1) - local notch_ay_medium = notch_ay_big + ((notch_by - notch_ay_big) * 0.2) - local notch_ay_small = notch_ay_big + ((notch_by - notch_ay_big) * 0.4) - local from_to_index = math.floor(this.notches / 2) - - for i = -from_to_index, from_to_index do - local notch_speed = nearest_notch_speed + (i * this.notch_every) - - if notch_speed < 0 or notch_speed > 100 then goto continue end - - local notch_x = nearest_notch_x + (i * this.notch_spacing) - local notch_thickness = 1 - local notch_ay = notch_ay_small - if (notch_speed % (this.notch_every * 10)) < 0.00000001 then - notch_ay = notch_ay_big - notch_thickness = 1 - elseif (notch_speed % (this.notch_every * 5)) < 0.00000001 then - notch_ay = notch_ay_medium - end - - ass:new_event() - ass:append('{\\blur0\\bord1\\shad0\\1c&HFFFFFF\\3c&H000000}') - ass:append(ass_opacity(math.min(1.2 - - (math.abs( - (notch_x - ax - half_width) / - half_width)), 1), opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:move_to(notch_x - notch_thickness, notch_ay) - ass:line_to(notch_x + notch_thickness, notch_ay) - ass:line_to(notch_x + notch_thickness, notch_by) - ass:line_to(notch_x - notch_thickness, notch_by) - ass:draw_stop() - - ::continue:: - end - - -- Center guide - ass:new_event() - ass:append('{\\blur0\\bord1\\shad0\\1c&HFFFFFF\\3c&H000000}') - ass:append(ass_opacity(options.speed_opacity, opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:move_to(half_x, by - 2 - guide_size) - ass:line_to(half_x + guide_size, by - 2) - ass:line_to(half_x - guide_size, by - 2) - ass:draw_stop() - - -- Speed value - local speed_text = (round(state.speed * 100) / 100) .. 'x' - ass:new_event() - ass:append( - '{\\blur0\\bord1\\shad0\\1c&H' .. options.color_background_text .. - '\\3c&H' .. options.color_background .. '\\fn' .. config.font .. - '\\fs' .. this.font_size .. bold_tag .. '}') - ass:append(ass_opacity(options.speed_opacity, opacity)) - ass:pos(half_x, ay) - ass:an(8) - ass:append(speed_text) - - return ass -end - -function render_menu(this) - local ass = assdraw.ass_new() - - if this.parent_menu then ass:merge(this.parent_menu:render()) end - - -- Menu title - if this.title then - -- Background - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. options.color_background .. '}') - ass:append(ass_opacity(options.menu_opacity, this.opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(this.ax, this.ay - this.item_height, this.bx, this.ay - 1) - ass:draw_stop() - - -- Title - ass:new_event() - ass:append('{\\blur0\\bord0\\shad1\\b1\\1c&H' .. - options.color_background_text .. '\\4c&H' .. - options.color_background .. '\\fn' .. config.font .. - '\\fs' .. this.font_size .. '\\q2\\clip(' .. this.ax .. - ',' .. this.ay - this.item_height .. ',' .. this.bx .. - ',' .. this.ay .. ')}') - ass:append(ass_opacity(options.menu_opacity, this.opacity)) - ass:pos(display.width / 2, this.ay - (this.item_height * 0.5)) - ass:an(5) - ass:append(this.title) - end - - local scroll_area_clip = '\\clip(' .. this.ax .. ',' .. this.ay .. ',' .. - this.bx .. ',' .. this.by .. ')' - - for index, item in ipairs(this.items) do - local item_ay = this.ay - this.scroll_y + - (this.item_height * (index - 1) + this.item_spacing * - (index - 1)) - local item_by = item_ay + this.item_height - local item_clip = '' - - -- Clip items overflowing scroll area - if item_ay <= this.ay or item_by >= this.by then - item_clip = scroll_area_clip - end - - if item_by < this.ay or item_ay > this.by then goto continue end - - local is_active = this.active_item == index - local font_color, background_color, ass_shadow, ass_shadow_color - local icon_size = this.font_size - - if is_active then - font_color, background_color = options.color_foreground_text, - options.color_foreground - ass_shadow, ass_shadow_color = '\\shad0', '' - else - font_color, background_color = options.color_background_text, - options.color_background - ass_shadow, ass_shadow_color = '\\shad1', - '\\4c&H' .. background_color - end - - local has_submenu = item.items ~= nil - local hint_width = 0 - if item.hint then - hint_width = text_width_estimate(item.hint:len(), this.font_size) + - this.item_content_spacing - elseif has_submenu then - hint_width = icon_size + this.item_content_spacing - end - - -- Background - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. background_color .. item_clip .. - '}') - ass:append(ass_opacity(options.menu_opacity, this.opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(this.ax, item_ay, this.bx, item_by) - ass:draw_stop() - - -- Selected highlight - if this.selected_item == index then - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. options.color_foreground .. - item_clip .. '}') - ass:append(ass_opacity(0.1, this.opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(this.ax, item_ay, this.bx, item_by) - ass:draw_stop() - end - - -- Title - if item.title then - item.ass_save_title = item.ass_save_title or - item.title:gsub("([{}])", "\\%1") - local title_clip_x = (this.bx - hint_width - - this.item_content_spacing) - local title_clip = '\\clip(' .. this.ax .. ',' .. - math.max(item_ay, this.ay) .. ',' .. - title_clip_x .. ',' .. - math.min(item_by, this.by) .. ')' - ass:new_event() - ass:append( - '{\\blur0\\bord0\\shad1\\1c&H' .. font_color .. '\\4c&H' .. - background_color .. '\\fn' .. config.font .. '\\fs' .. - this.font_size .. bold_tag .. title_clip .. '\\q2}') - ass:append(ass_opacity(options.menu_opacity, this.opacity)) - ass:pos(this.ax + this.item_content_spacing, - item_ay + (this.item_height / 2)) - ass:an(4) - ass:append(item.ass_save_title) - end - - -- Hint - if item.hint then - item.ass_save_hint = item.ass_save_hint or - item.hint:gsub("([{}])", "\\%1") - ass:new_event() - ass:append( - '{\\blur0\\bord0' .. ass_shadow .. '\\1c&H' .. font_color .. '' .. - ass_shadow_color .. '\\fn' .. config.font .. '\\fs' .. - (this.font_size - 1) .. bold_tag .. item_clip .. '}') - ass:append(ass_opacity(options.menu_opacity * - (has_submenu and 1 or 0.5), this.opacity)) - ass:pos(this.bx - this.item_content_spacing, - item_ay + (this.item_height / 2)) - ass:an(6) - ass:append(item.ass_save_hint) - elseif has_submenu then - ass:new_event() - ass:append(icon('arrow_right', - this.bx - this.item_content_spacing - - (icon_size / 2), -- x - item_ay + (this.item_height / 2), -- y - icon_size, -- size - 0, 0, 1, -- shadow_x, shadow_y, shadow_size - is_active and 'foreground' or 'background', this.opacity, -- backdrop, opacity - item_clip)) - end - - ::continue:: - end - - -- Scrollbar - if this.scroll_height > 0 then - local groove_height = this.height - 2 - local thumb_height = math.max((this.height / - (this.scroll_height + this.height)) * - groove_height, 40) - local thumb_y = this.ay + 1 + - ((this.scroll_y / this.scroll_height) * - (groove_height - thumb_height)) - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. options.color_foreground .. '}') - ass:append(ass_opacity(options.menu_opacity, this.opacity * 0.8)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(this.bx - 3, thumb_y, this.bx - 1, thumb_y + thumb_height) - ass:draw_stop() - end - - return ass -end - --- MAIN RENDERING - --- Request that render() is called. --- The render is then either executed immediately, or rate-limited if it was --- called a small time ago. -function request_render() - if state.render_timer == nil then - state.render_timer = mp.add_timeout(0, render) - end - - if not state.render_timer:is_enabled() then - local now = mp.get_time() - local timeout = config.render_delay - (now - state.render_last_time) - if timeout < 0 then timeout = 0 end - state.render_timer.timeout = timeout - state.render_timer:resume() - end -end - -function render() - state.render_last_time = mp.get_time() - - -- Actual rendering - local ass = assdraw.ass_new() - - for _, element in elements.ipairs() do - local result = element:maybe('render') - if result then - ass:new_event() - ass:merge(result) - end - end - - -- submit - if osd.res_x == display.width and osd.res_y == display.height and osd.data == - ass.text then return end - - osd.res_x = display.width - osd.res_y = display.height - osd.data = ass.text - osd.z = 2000 - osd:update() -end - --- STATIC ELEMENTS - -if itable_find({'flash', 'static'}, options.pause_indicator) then - elements:add('pause_indicator', Element.new( - { - base_icon_opacity = options.pause_indicator == 'flash' and 1 or 0.8, - paused = false, - is_flash = options.pause_indicator == 'flash', - is_static = options.pause_indicator == 'static', - opacity = 0, - init = function(this) - local initial_call = true - mp.observe_property('pause', 'bool', function(_, paused) - if initial_call then - initial_call = false - return - end - - this.paused = paused - - if options.pause_indicator == 'flash' then - this.opacity = 1 - this:tween_property('opacity', 1, 0, 0.15) - else - this.opacity = paused and 1 or 0 - request_render() - end - - end) - end, - render = function(this) - if this.opacity == 0 then return end - - local ass = assdraw.ass_new() - - -- Background fadeout - if this.is_static then - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. - options.color_background .. '}') - ass:append(ass_opacity(0.3, this.opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(0, 0, display.width, display.height) - ass:draw_stop() - end - - -- Icon - local size = round((math.min(display.width, display.height) * - (this.is_static and 0.20 or 0.15)) / 2) - - size = size + size * (1 - this.opacity) - - if this.paused then - ass:new_event() - ass:append('{\\blur0\\bord1\\1c&H' .. - options.color_foreground .. '\\3c&H' .. - options.color_background .. '}') - ass:append(ass_opacity(this.base_icon_opacity, this.opacity)) - ass:pos(display.width / 2, display.height / 2) - ass:draw_start() - ass:rect_cw(-size, -size, -size / 3, size) - ass:draw_stop() - - ass:new_event() - ass:append('{\\blur0\\bord1\\1c&H' .. - options.color_foreground .. '\\3c&H' .. - options.color_background .. '}') - ass:append(ass_opacity(this.base_icon_opacity, this.opacity)) - ass:pos(display.width / 2, display.height / 2) - ass:draw_start() - ass:rect_cw(size / 3, -size, size, size) - ass:draw_stop() - elseif this.is_flash then - ass:new_event() - ass:append('{\\blur0\\bord1\\1c&H' .. - options.color_foreground .. '\\3c&H' .. - options.color_background .. '}') - ass:append(ass_opacity(this.base_icon_opacity, this.opacity)) - ass:pos(display.width / 2, display.height / 2) - ass:draw_start() - ass:move_to(-size * 0.6, -size) - ass:line_to(size, 0) - ass:line_to(-size * 0.6, size) - ass:draw_stop() - end - - return ass - end - })) -end -elements:add('timeline', Element.new({ - captures = {mouse_buttons = true, wheel = true}, - pressed = false, - size_max = 0, - size_min = 0, -- set in `on_display_resize` handler based on `state.fullscreen` - size_min_override = options.timeline_start_hidden and 0 or nil, -- used for toggle-progress command - font_size = 0, -- calculated in on_display_resize - top_border = options.timeline_border, - bottom_border = 0, -- set dynamically in `border` property observer - init = function(this) - -- Toggle 1px bottom border for timeline in no-border mode - mp.observe_property('border', 'bool', function(_, border) - this.bottom_border = not border and options.timeline_border or 0 - request_render() - end) - - -- Flash on external changes - if options.timeline_flash then - mp.register_event('seek', function() - local position = mp.get_property_native('playback-time') - if position and state.position then - local seek_length = math.abs(position - state.position) - -- Don't flash on video looping (seek to 0) or tiny seeks (frame-step) - if position > 0.5 and seek_length > 0.5 then - this:flash() - end - end - end) - end - end, - get_effective_proximity = function(this) - if (elements.volume_slider and elements.volume_slider.pressed) then - return 0 - end - if this.pressed then return 1 end - return this.forced_proximity and this.forced_proximity or this.proximity - end, - get_effective_size_min = function(this) - return this.size_min_override or this.size_min - end, - get_effective_size = function(this) - if elements.speed and elements.speed.dragging then - return this.size_max - end - local size_min = this:get_effective_size_min() - return size_min + - math.ceil((this.size_max - size_min) * - this:get_effective_proximity()) - end, - on_display_resize = function(this) - if state.fullscreen or state.maximized then - this.size_min = options.timeline_size_min_fullscreen - this.size_max = options.timeline_size_max_fullscreen - else - this.size_min = options.timeline_size_min - this.size_max = options.timeline_size_max - end - this.font_size = math.floor(math.min((this.size_max + 60) * 0.2, - this.size_max * 0.96) * - options.timeline_font_scale) - this.ax = 0 - this.ay = display.height - this.size_max - this.top_border - - this.bottom_border - this.bx = display.width - this.by = display.height - end, - set_from_cursor = function(this) - mp.commandv('seek', ((cursor.x / display.width) * 100), - 'absolute-percent+exact') - end, - on_mbtn_left_down = function(this) - this.pressed = true - this:set_from_cursor() - end, - on_global_mbtn_left_up = function(this) this.pressed = false end, - on_global_mouse_leave = function(this) this.pressed = false end, - on_global_mouse_move = function(this) - if this.pressed then this:set_from_cursor() end - end, - on_wheel_up = function(this) - if options.timeline_step > 0 then - mp.commandv('seek', -options.timeline_step) - end - end, - on_wheel_down = function(this) - if options.timeline_step > 0 then - mp.commandv('seek', options.timeline_step) - end - end, - render = render_timeline -})) -if options.top_bar_controls or options.top_bar_title then - elements:add('top_bar', Element.new({ - button_opacity = 0.8, - enabled = false, - init = function(this) - mp.observe_property('border', 'bool', function(_, border) - this.enabled = not border - end) - end, - get_effective_proximity = function(this) - if (elements.volume_slider and elements.volume_slider.pressed) or - elements.curtain.opacity > 0 then return 0 end - return this.forced_proximity and this.forced_proximity or - this.proximity - end, - on_display_resize = function(this) - this.size = (state.fullscreen or state.maximized) and - options.top_bar_size_fullscreen or - options.top_bar_size - this.icon_size = round(this.size / 8) - this.spacing = math.ceil(this.size * 0.25) - this.font_size = math.floor(this.size - (this.spacing * 2)) - this.button_width = round(this.size * 1.15) - this.title_bx = display.width - - (options.top_bar_controls and - (this.button_width * 3) or 0) - this.ax = options.top_bar_title and 0 or this.title_bx - this.ay = 0 - this.bx = display.width - this.by = this.size - end, - render = render_top_bar - })) -end -if options.top_bar_controls then - elements:add('window_controls_minimize', Element.new( - { - captures = {mouse_buttons = true}, - on_display_resize = function(this) - this.ax = display.width - (elements.top_bar.button_width * 3) - this.ay = 0 - this.bx = this.ax + elements.top_bar.button_width - this.by = elements.top_bar.size - end, - on_mbtn_left_down = function() - mp.commandv('cycle', 'window-minimized') - end - })) - elements:add('window_controls_maximize', Element.new( - { - captures = {mouse_buttons = true}, - on_display_resize = function(this) - this.ax = display.width - (elements.top_bar.button_width * 2) - this.ay = 0 - this.bx = this.ax + elements.top_bar.button_width - this.by = elements.top_bar.size - end, - on_mbtn_left_down = function() - mp.commandv('cycle', 'window-maximized') - end - })) - elements:add('window_controls_close', Element.new( - { - captures = {mouse_buttons = true}, - on_display_resize = function(this) - this.ax = display.width - elements.top_bar.button_width - this.ay = 0 - this.bx = this.ax + elements.top_bar.button_width - this.by = elements.top_bar.size - end, - on_mbtn_left_down = function() mp.commandv('quit') end - })) -end -if itable_find({'left', 'right'}, options.volume) then - elements:add('volume', Element.new({ - width = nil, -- set in `on_display_resize` handler based on `state.fullscreen` - height = nil, -- set in `on_display_resize` handler based on `state.fullscreen` - margin = nil, -- set in `on_display_resize` handler based on `state.fullscreen` - init = function(this) - -- FLash on external changes - if options.volume_flash then - local is_initial_volume_call = true - mp.observe_property('volume', 'number', function(_, value) - if not is_initial_volume_call then - this:flash() - end - is_initial_volume_call = false - end) - local is_initial_mute_call = true - mp.observe_property('mute', 'bool', function(_, value) - if not is_initial_mute_call then - this:flash() - end - is_initial_mute_call = false - end) - end - end, - get_effective_proximity = function(this) - if elements.volume_slider.pressed then return 1 end - if elements.timeline.proximity_raw == 0 or elements.curtain.opacity > - 0 then return 0 end - return this.forced_proximity and this.forced_proximity or - this.proximity - end, - on_display_resize = function(this) - this.width = (state.fullscreen or state.maximized) and - options.volume_size_fullscreen or - options.volume_size - this.height = round(math.min(this.width * 8, (elements.timeline.ay - - elements.top_bar.size) * 0.8)) - -- Don't bother rendering this if too small - if this.height < (this.width * 2) then this.height = 0 end - this.margin = this.width / 2 - this.ax = round(options.volume == 'left' and this.margin or - display.width - this.margin - this.width) - this.ay = round((display.height - this.height) / 2) - this.bx = round(this.ax + this.width) - this.by = round(this.ay + this.height) - end, - render = render_volume - })) - elements:add('volume_mute', Element.new( - { - captures = {mouse_buttons = true}, - width = 0, - height = 0, - on_display_resize = function(this) - this.width = elements.volume.width - this.height = this.width - this.ax = elements.volume.ax - this.ay = elements.volume.by - this.height - this.bx = elements.volume.bx - this.by = elements.volume.by - end, - on_mbtn_left_down = function(this) - mp.commandv('cycle', 'mute') - end - })) - elements:add('volume_slider', Element.new( - { - captures = {mouse_buttons = true, wheel = true}, - pressed = false, - width = 0, - height = 0, - nudge_y = 0, -- vertical position where volume overflows 100 - nudge_size = nil, -- set on resize - font_size = nil, - spacing = nil, - on_display_resize = function(this) - this.ax = elements.volume.ax - this.ay = elements.volume.ay - this.bx = elements.volume.bx - this.by = elements.volume_mute.ay - this.width = this.bx - this.ax - this.height = this.by - this.ay - this.nudge_y = this.by - - round(this.height * (100 / state.volume_max)) - this.nudge_size = round(elements.volume.width * 0.18) - this.draw_nudge = this.ay < this.nudge_y - this.spacing = round(this.width * 0.2) - end, - set_from_cursor = function(this) - local volume_fraction = (this.by - cursor.y - - options.volume_border) / - (this.height - options.volume_border) - local new_volume = math.min(math.max(volume_fraction, 0), 1) * - state.volume_max - new_volume = round(new_volume / options.volume_step) * - options.volume_step - if state.volume ~= new_volume then - mp.commandv('set', 'volume', - math.min(new_volume, state.volume_max)) - end - end, - on_mbtn_left_down = function(this) - this.pressed = true - this:set_from_cursor() - end, - on_global_mbtn_left_up = function(this) - this.pressed = false - end, - on_global_mouse_leave = function(this) - this.pressed = false - end, - on_global_mouse_move = function(this) - if this.pressed then this:set_from_cursor() end - end, - on_wheel_up = function(this) - local current_rounded_volume = - round(state.volume / options.volume_step) * - options.volume_step - mp.commandv('set', 'volume', math.min( - current_rounded_volume + options.volume_step, - state.volume_max)) - end, - on_wheel_down = function(this) - local current_rounded_volume = - round(state.volume / options.volume_step) * - options.volume_step - mp.commandv('set', 'volume', math.min( - current_rounded_volume - options.volume_step, - state.volume_max)) - end - })) -end -if options.speed then - elements:add('speed', Element.new({ - captures = {mouse_buttons = true, wheel = true}, - dragging = nil, - width = 0, - height = 0, - notches = 10, - notch_every = 0.1, - step_distance = nil, - font_size = nil, - init = function(this) - -- Fade out/in on timeline mouse enter/leave - elements.timeline:on('mouse_enter', function() - if not this.dragging then this:fadeout() end - end) - elements.timeline:on('mouse_leave', function() - if not this.dragging then this:fadein() end - end) - - -- Flash on external changes - if options.speed_flash then - local initial_call = true - mp.observe_property('speed', 'number', function() - if not initial_call and not this.dragging then - this:flash() - end - initial_call = false - end) - end - end, - fadeout = function(this) - this:tween_property('forced_proximity', 1, 0, - function(this) - this.forced_proximity = 0 - end) - end, - fadein = function(this) - local get_current_proximity = - function() return this.proximity end - this:tween_property('forced_proximity', 0, get_current_proximity, - function(this) - this.forced_proximity = nil - end) - end, - on_display_resize = function(this) - this.height = (state.fullscreen or state.maximized) and - options.speed_size_fullscreen or - options.speed_size - this.width = round(this.height * 3.6) - this.notch_spacing = this.width / this.notches - this.step_distance = this.notch_spacing * - (options.speed_step / this.notch_every) - this.ax = (display.width - this.width) / 2 - this.by = display.height - elements.timeline.size_max - this.ay = this.by - this.height - this.bx = this.ax + this.width - this.font_size = - round(this.height * 0.48 * options.speed_font_scale) - end, - set_from_cursor = function(this) - local volume_fraction = - (this.by - cursor.y - options.volume_border) / - (this.height - options.volume_border) - local new_volume = math.min(math.max(volume_fraction, 0), 1) * - state.volume_max - new_volume = round(new_volume / options.volume_step) * - options.volume_step - if state.volume ~= new_volume then - mp.commandv('set', 'volume', new_volume) - end - end, - on_mbtn_left_down = function(this) - this:tween_stop() -- Stop and cleanup possible ongoing animations - this.dragging = { - start_time = mp.get_time(), - start_x = cursor.x, - distance = 0, - start_speed = state.speed - } - end, - on_global_mouse_move = function(this) - if not this.dragging then return end - - this.dragging.distance = cursor.x - this.dragging.start_x - local steps_dragged = round(-this.dragging.distance / - this.step_distance) - local new_speed = this.dragging.start_speed + - (steps_dragged * options.speed_step) - mp.set_property_native('speed', round(new_speed * 100) / 100) - end, - on_mbtn_left_up = function(this) - -- Reset speed on short clicks - if this.dragging and math.abs(this.dragging.distance) < 6 and - mp.get_time() - this.dragging.start_time < 0.15 then - mp.set_property_native('speed', 1) - end - end, - on_global_mbtn_left_up = function(this) - if this.dragging and elements.timeline.proximity_raw == 0 then - this:fadeout() - end - this.dragging = nil - request_render() - end, - on_global_mouse_leave = function(this) - this.dragging = nil - request_render() - end, - on_wheel_up = function(this) - mp.set_property_native('speed', state.speed - options.speed_step) - end, - on_wheel_down = function(this) - mp.set_property_native('speed', state.speed + options.speed_step) - end, - render = render_speed - })) -end -elements:add('curtain', Element.new({ - opacity = 0, - fadeout = function(this) this:tween_property('opacity', this.opacity, 0); end, - fadein = function(this) this:tween_property('opacity', this.opacity, 1); end, - render = function(this) - if this.opacity > 0 then - local ass = assdraw.ass_new() - ass:new_event() - ass:append('{\\blur0\\bord0\\1c&H' .. options.color_background .. - '}') - ass:append(ass_opacity(0.4, this.opacity)) - ass:pos(0, 0) - ass:draw_start() - ass:rect_cw(0, 0, display.width, display.height) - ass:draw_stop() - return ass - end - end -})) - --- CHAPTERS SERIALIZATION - --- Parse `chapter_ranges` option into workable data structure -for _, definition in ipairs(split(options.chapter_ranges, ' *,+ *')) do - local start_patterns, color, opacity, end_patterns = - string.match(definition, '([^<]+)<(%x%x%x%x%x%x):(%d?%.?%d*)>([^>]+)') - - -- Invalid definition - if start_patterns == nil then goto continue end - - start_patterns = start_patterns:lower() - end_patterns = end_patterns:lower() - local uses_bof = start_patterns:find('{bof}') ~= nil - local uses_eof = end_patterns:find('{eof}') ~= nil - local chapter_range = { - start_patterns = split(start_patterns, '|'), - end_patterns = split(end_patterns, '|'), - color = color, - opacity = tonumber(opacity), - ranges = {} - } - - -- Filter out special keywords so we don't use them when matching titles - if uses_bof then - chapter_range.start_patterns = itable_remove( - chapter_range.start_patterns, '{bof}') - end - if uses_eof and chapter_range.end_patterns then - chapter_range.end_patterns = itable_remove(chapter_range.end_patterns, - '{eof}') - end - - chapter_range['serialize'] = function(chapters) - chapter_range.ranges = {} - local current_range = nil - -- bof and eof should be used only once per timeline - -- eof is only used when last range is missing end - local bof_used = false - - function start_range(chapter) - -- If there is already a range started, should we append or overwrite? - -- I chose overwrite here. - current_range = {['start'] = chapter} - end - - function end_range(chapter) - current_range['end'] = chapter - chapter_range.ranges[#chapter_range.ranges + 1] = current_range - -- Mark both chapter objects - current_range['start']._uosc_used_as_range_point = true - current_range['end']._uosc_used_as_range_point = true - -- Clear for next range - current_range = nil - end - - for _, chapter in ipairs(chapters) do - if type(chapter.title) == 'string' then - local lowercase_title = chapter.title:lower() - local is_end = false - local is_start = false - - -- Is ending check and handling - if chapter_range.end_patterns then - for _, end_pattern in ipairs(chapter_range.end_patterns) do - is_end = is_end or lowercase_title:find(end_pattern) ~= - nil - end - - if is_end then - if current_range == nil and uses_bof and not bof_used then - bof_used = true - start_range({time = 0}) - end - if current_range ~= nil then - end_range(chapter) - else - is_end = false - end - end - end - - -- Is start check and handling - for _, start_pattern in ipairs(chapter_range.start_patterns) do - is_start = - is_start or lowercase_title:find(start_pattern) ~= nil - end - - if is_start then start_range(chapter) end - end - end - - -- If there is an unfinished range and range type accepts eof, use it - if current_range ~= nil and uses_eof then - end_range({time = state.duration or infinity}) - end - end - - state.chapter_ranges = state.chapter_ranges or {} - state.chapter_ranges[#state.chapter_ranges + 1] = chapter_range - - ::continue:: -end - -function parse_chapters() - -- Sometimes state.duration is not initialized yet for some reason - state.duration = mp.get_property_native('duration') - - local chapters = get_normalized_chapters() - - if not chapters or not state.duration then return end - - -- Reset custom ranges - for _, chapter_range in ipairs(state.chapter_ranges or {}) do - chapter_range.serialize(chapters) - end - - -- Filter out chapters that were used as ranges - state.chapters = itable_remove(chapters, function(chapter) - return chapter._uosc_used_as_range_point == true - end) - - request_render() -end - --- CONTEXT MENU SERIALIZATION - -state.context_menu_items = (function() - local input_conf_path = mp.command_native({'expand-path', '~~/input.conf'}) - local input_conf_meta, meta_error = utils.file_info(input_conf_path) - - -- File doesn't exist - if not input_conf_meta or not input_conf_meta.is_file then return end - - local items = {} - local items_by_command = {} - local submenus_by_id = {} - - for line in io.lines(input_conf_path) do - local key, command, title = string.match(line, - ' *([%S]+) +(.*) #! *(.*)') - if key then - local is_dummy = key:sub(1, 1) == '#' - local submenu_id = '' - local target_menu = items - local title_parts = split(title or '', ' *> *') - - for index, title_part in ipairs( - #title_parts > 0 and title_parts or - {''}) do - if index < #title_parts then - submenu_id = submenu_id .. title_part - - if not submenus_by_id[submenu_id] then - submenus_by_id[submenu_id] = - {title = title_part, items = {}} - target_menu[#target_menu + 1] = - submenus_by_id[submenu_id] - end - - target_menu = submenus_by_id[submenu_id].items - else - -- If command is already in menu, just append the key to it - if items_by_command[command] then - items_by_command[command].hint = - items_by_command[command].hint .. ', ' .. key - else - items_by_command[command] = - { - title = title_part, - hint = not is_dummy and key or nil, - value = command - } - target_menu[#target_menu + 1] = - items_by_command[command] - end - end - end - end - end - - if #items > 0 then return items end -end)() - --- EVENT HANDLERS - -function create_state_setter(name) - return function(_, value) - state[name] = value - dispatch_event_to_elements('prop_' .. name, value) - request_render() - end -end - -function dispatch_event_to_elements(name, ...) - for _, element in pairs(elements) do - if element.proximity_raw == 0 then - element:maybe('on_' .. name, ...) - end - element:maybe('on_global_' .. name, ...) - end -end - -function create_event_to_elements_dispatcher(name, ...) - return function(...) dispatch_event_to_elements(name, ...) end +function update_cursor_position(x, y) + local old_x, old_y = cursor.x, cursor.y + + -- mpv reports initial mouse position on linux as (0, 0), which always + -- displays the top bar, so we hardcode cursor position as infinity until + -- we receive a first real mouse move event with coordinates other than 0,0. + if not state.first_real_mouse_move_received then + if x > 0 and y > 0 then state.first_real_mouse_move_received = true + else x, y = INFINITY, INFINITY end + end + + -- add 0.5 to be in the middle of the pixel + cursor.x, cursor.y = (x + 0.5) / display.scale_x, (y + 0.5) / display.scale_y + + if old_x ~= cursor.x or old_y ~= cursor.y then + Elements:update_proximities() + + if cursor.x == INFINITY or cursor.y == INFINITY then + cursor.hidden = true + Elements:trigger('global_mouse_leave') + elseif cursor.hidden then + cursor.hidden = false + Elements:trigger('global_mouse_enter') + end + + Elements:proximity_trigger('mouse_move') + cursor.queue_autohide() + end + + request_render() end function handle_mouse_leave() - -- Slowly fadeout elements that are currently visible - for _, element_name in ipairs({'timeline', 'volume', 'top_bar'}) do - local element = elements[element_name] - if element and element.proximity > 0 then - element:tween_property('forced_proximity', - element:get_effective_proximity(), 0, - function() - element.forced_proximity = nil - end) - end - end + -- Slowly fadeout elements that are currently visible + for _, element_name in ipairs({'timeline', 'volume', 'top_bar'}) do + local element = Elements[element_name] + if element and element.proximity > 0 then + element:tween_property('forced_visibility', element:get_visibility(), 0, function() + element.forced_visibility = nil + end) + end + end - cursor.hidden = true - update_proximities() - dispatch_event_to_elements('mouse_leave') + update_cursor_position(INFINITY, INFINITY) end -function handle_mouse_enter() - cursor.hidden = false - cursor.x, cursor.y = mp.get_mouse_pos() - tween_element_stop(state) - dispatch_event_to_elements('mouse_enter') +function handle_file_end() + local resume = false + if not state.loop_file then + if state.has_playlist then resume = state.shuffle and navigate_playlist(1) + else resume = options.autoload and navigate_directory(1) end + end + -- Resume only when navigation happened + if resume then mp.command('set pause no') end +end +local file_end_timer = mp.add_timeout(1, handle_file_end) +file_end_timer:kill() + +function load_file_index_in_current_directory(index) + if not state.path or is_protocol(state.path) then return end + + local serialized = serialize_path(state.path) + if serialized and serialized.dirname then + local files = read_directory(serialized.dirname, config.types.autoload) + + if not files then return end + sort_filenames(files) + if index < 0 then index = #files + index + 1 end + + if files[index] then + mp.commandv('loadfile', join_path(serialized.dirname, files[index])) + end + end end -function handle_mouse_move() - -- Handle case when we are in cursor hidden state but not left the actual - -- window (i.e. when autohide simulates mouse_leave). - if cursor.hidden then - handle_mouse_enter() - return - end - - cursor.x, cursor.y = mp.get_mouse_pos() - update_proximities() - dispatch_event_to_elements('mouse_move') - request_render() - - -- Restart timer that hides UI when mouse is autohidden - if options.autohide then - state.cursor_autohide_timer:kill() - state.cursor_autohide_timer:resume() - end +function update_render_delay(name, fps) + if fps then state.render_delay = 1 / fps end end -function navigate_directory(direction) - local path = mp.get_property_native("path") - - if not path or is_protocol(path) then return end - - local next_file = get_adjacent_file(path, direction, options.media_types) - - if next_file then - mp.commandv("loadfile", - utils.join_path(serialize_path(path).dirname, next_file)) - end +function observe_display_fps(name, fps) + if fps then + mp.unobserve_property(update_render_delay) + mp.unobserve_property(observe_display_fps) + mp.observe_property('display-fps', 'native', update_render_delay) + end end -function load_file_in_current_directory(index) - local path = mp.get_property_native("path") - - if not path or is_protocol(path) then return end - - local dirname = serialize_path(path).dirname - local files = get_files_in_directory(dirname, options.media_types) - - if not files then return end - if index < 0 then index = #files + index + 1 end - - if files[index] then - mp.commandv("loadfile", utils.join_path(dirname, files[index])) - end +function select_current_chapter() + local current_chapter + if state.time and state.chapters then + _, current_chapter = itable_find(state.chapters, function(c) return state.time >= c.time end, true) + end + set_state('current_chapter', current_chapter) end --- MENUS +--[[ STATE HOOKS ]] -function create_select_tracklist_type_menu_opener(menu_title, track_type, - track_prop) - return function() - if menu:is_open(track_type) then - menu:close() - return - end - - local items = {} - local active_item = nil - - for index, track in ipairs(mp.get_property_native('track-list')) do - if track.type == track_type then - if track.selected then active_item = track.id end - - items[#items + 1] = { - title = (track.title and track.title or 'Track ' .. track.id), - hint = track.lang and track.lang:upper() or nil, - value = track.id - } - end - end - - -- Add option to disable a subtitle track. This works for all tracks, - -- but why would anyone want to disable audio or video? Better to not - -- let people mistakenly select what is unwanted 99.999% of the time. - -- If I'm mistaken and there is an active need for this, feel free to - -- open an issue. - if track_type == 'sub' then - active_item = active_item and active_item + 1 or 1 - table.insert(items, 1, {hint = 'disabled', value = nil}) - end - - menu:open(items, function(id) - mp.commandv('set', track_prop, id and id or 'no') - - -- If subtitle track was selected, assume user also wants to see it - if id and track_type == 'sub' then - mp.commandv('set', 'sub-visibility', 'yes') - end - - menu:close() - end, {type = track_type, title = menu_title, active_item = active_item}) - end +-- Click detection +if options.click_threshold > 0 then + -- Executes custom command for clicks shorter than `options.click_threshold` + -- while filtering out double clicks. + local click_time = options.click_threshold / 1000 + local doubleclick_time = mp.get_property_native('input-doubleclick-time') / 1000 + local last_down, last_up = 0, 0 + local click_timer = mp.add_timeout(math.max(click_time, doubleclick_time), function() + local delta = last_up - last_down + if delta > 0 and delta < click_time and delta > 0.02 then mp.command(options.click_command) end + end) + click_timer:kill() + mp.set_key_bindings({{'mbtn_left', + function() last_up = mp.get_time() end, + function() + last_down = mp.get_time() + if click_timer:is_enabled() then click_timer:kill() else click_timer:resume() end + end, + },}, 'mouse_movement', 'force') + mp.enable_key_bindings('mouse_movement', 'allow-vo-dragging+allow-hide-cursor') end --- `menu_options`: --- **allowed_types** - table with file extensions to display --- **active_path** - full path of a file to preselect --- Rest of the options are passed to `menu:open()` -function open_file_navigation_menu(directory, handle_select, menu_options) - directory = serialize_path(directory) - local directories, error = utils.readdir(directory.path, 'dirs') - local files, error = get_files_in_directory(directory.path, - menu_options.allowed_types) - - if not files or not directories then - msg.error('Retrieving files from ' .. directory .. ' failed: ' .. - (error or '')) - return - end - - -- Files are already sorted - table.sort(directories, word_order_comparator) - - -- Pre-populate items with parent directory selector if not at root - local items = not directory.dirname and {} or - { - {title = '..', hint = 'parent dir', value = directory.dirname} - } - - for _, dir in ipairs(directories) do - local serialized = serialize_path(utils.join_path(directory.path, dir)) - items[#items + 1] = { - title = serialized.basename, - value = serialized.path, - hint = '/' - } - end - - menu_options.active_item = nil - - for _, file in ipairs(files) do - local serialized = serialize_path(utils.join_path(directory.path, file)) - local item_index = #items + 1 - - items[item_index] = { - title = serialized.basename, - value = serialized.path - } - - if menu_options.active_path == serialized.path then - menu_options.active_item = item_index - end - end - - menu_options.title = directory.basename .. '/' - - menu:open(items, function(path) - local meta, error = utils.file_info(path) - - if not meta then - msg.error('Retrieving file info for ' .. path .. ' failed: ' .. - (error or '')) - return - end - - if meta.is_dir then - open_file_navigation_menu(path, handle_select, menu_options) - else - handle_select(path) - menu:close() - end - end, menu_options) +function handle_mouse_pos(_, mouse) + if not mouse then return end + if cursor.hover_raw and not mouse.hover then + handle_mouse_leave() + else + update_cursor_position(mouse.x, mouse.y) + end + cursor.hover_raw = mouse.hover end +mp.observe_property('mouse-pos', 'native', handle_mouse_pos) +mp.observe_property('osc', 'bool', function(name, value) if value == true then mp.set_property('osc', 'no') end end) +mp.register_event('file-loaded', function() + set_state('path', normalize_path(mp.get_property_native('path'))) + Elements:flash({'top_bar'}) +end) +mp.register_event('end-file', function(event) + set_state('path', nil) + if event.reason == 'eof' then + file_end_timer:kill() + handle_file_end() + end +end) +-- Top bar titles +do + local function update_state_with_template(prop, template) + -- escape ASS, and strip newlines and trailing slashes and trim whitespace + local tmp = mp.command_native({'expand-text', template}):gsub('\\n', ' '):gsub('[\\%s]+$', ''):gsub('^%s+', '') + set_state(prop, ass_escape(tmp)) + end --- VALUE SERIALIZATION/NORMALIZATION + local function add_template_listener(template, callback) + local props = get_expansion_props(template) + for prop, _ in pairs(props) do + mp.observe_property(prop, 'native', callback) + end + if not next(props) then callback() end + end -options.proximity_out = - math.max(options.proximity_out, options.proximity_in + 1) -options.chapters = itable_find({'dots', 'lines', 'lines-top', 'lines-bottom'}, - options.chapters) and options.chapters or 'none' -options.media_types = split(options.media_types, ' *, *') -options.subtitle_types = split(options.subtitle_types, ' *, *') -options.timeline_cached_ranges = (function() - if options.timeline_cached_ranges == '' or options.timeline_cached_ranges == - 'no' then return nil end - local parts = split(options.timeline_cached_ranges, ':') - return parts[1] and {color = parts[1], opacity = tonumber(parts[2])} or nil -end)() + local function remove_template_listener(callback) mp.unobserve_property(callback) end --- HOOKS -mp.register_event('file-loaded', parse_chapters) -mp.observe_property('chapter-list', 'native', parse_chapters) -mp.observe_property('duration', 'number', create_state_setter('duration')) -mp.observe_property('media-title', 'string', create_state_setter('media_title')) -mp.observe_property('fullscreen', 'bool', create_state_setter('fullscreen')) -mp.observe_property('window-maximized', 'bool', create_state_setter('maximized')) -mp.observe_property('idle-active', 'bool', create_state_setter('idle')) -mp.observe_property('speed', 'number', create_state_setter('speed')) -mp.observe_property('pause', 'bool', create_state_setter('pause')) + -- Main title + if #options.top_bar_title > 0 and options.top_bar_title ~= 'no' then + if options.top_bar_title == 'yes' then + local template = nil + local function update_title() update_state_with_template('title', template) end + mp.observe_property('title', 'string', function(_, title) + remove_template_listener(update_title) + template = title + if template then + if template:sub(-6) == ' - mpv' then template = template:sub(1, -7) end + add_template_listener(template, update_title) + end + end) + elseif type(options.top_bar_title) == 'string' then + add_template_listener(options.top_bar_title, function() + update_state_with_template('title', options.top_bar_title) + end) + end + end + + -- Alt title + if #options.top_bar_alt_title > 0 and options.top_bar_alt_title ~= 'no' then + add_template_listener(options.top_bar_alt_title, function() + update_state_with_template('alt_title', options.top_bar_alt_title) + end) + end +end +mp.observe_property('playback-time', 'number', create_state_setter('time', function() + -- Create a file-end event that triggers right before file ends + file_end_timer:kill() + if state.duration and state.time and not state.pause then + local remaining = (state.duration - state.time) / state.speed + if remaining < 5 then + local timeout = remaining - 0.02 + if timeout > 0 then + file_end_timer.timeout = timeout + file_end_timer:resume() + else handle_file_end() end + end + end + + update_human_times() + select_current_chapter() +end)) +mp.observe_property('duration', 'number', create_state_setter('duration', update_human_times)) +mp.observe_property('speed', 'number', create_state_setter('speed', update_human_times)) +mp.observe_property('track-list', 'native', function(name, value) + -- checks the file dispositions + local types = {sub = 0, image = 0, audio = 0, video = 0} + for _, track in ipairs(value) do + if track.type == 'video' then + if track.image or track.albumart then types.image = types.image + 1 + else types.video = types.video + 1 end + elseif types[track.type] then types[track.type] = types[track.type] + 1 end + end + set_state('is_audio', types.video == 0 and types.audio > 0) + set_state('is_image', types.image > 0 and types.video == 0 and types.audio == 0) + set_state('has_audio', types.audio > 0) + set_state('has_many_audio', types.audio > 1) + set_state('has_sub', types.sub > 0) + set_state('has_many_sub', types.sub > 1) + set_state('is_video', types.video > 0) + set_state('has_many_video', types.video > 1) + Elements:trigger('dispositions') +end) +mp.observe_property('editions', 'number', function(_, editions) + if editions then set_state('has_many_edition', editions > 1) end + Elements:trigger('dispositions') +end) +mp.observe_property('chapter-list', 'native', function(_, chapters) + local chapters, chapter_ranges = serialize_chapters(chapters), {} + if chapters then chapters, chapter_ranges = serialize_chapter_ranges(chapters) end + set_state('chapters', chapters) + set_state('chapter_ranges', chapter_ranges) + set_state('has_chapter', #chapters > 0) + select_current_chapter() + Elements:trigger('dispositions') +end) +mp.observe_property('border', 'bool', create_state_setter('border')) +mp.observe_property('loop-file', 'native', create_state_setter('loop_file')) +mp.observe_property('ab-loop-a', 'number', create_state_setter('ab_loop_a')) +mp.observe_property('ab-loop-b', 'number', create_state_setter('ab_loop_b')) +mp.observe_property('playlist-pos-1', 'number', create_state_setter('playlist_pos')) +mp.observe_property('playlist-count', 'number', function(_, value) + set_state('playlist_count', value) + set_state('has_playlist', value > 1) + Elements:trigger('dispositions') +end) +mp.observe_property('fullscreen', 'bool', create_state_setter('fullscreen', update_fullormaxed)) +mp.observe_property('window-maximized', 'bool', create_state_setter('maximized', update_fullormaxed)) +mp.observe_property('idle-active', 'bool', function(_, idle) + set_state('is_idle', idle) + Elements:trigger('dispositions') +end) +mp.observe_property('pause', 'bool', create_state_setter('pause', function() file_end_timer:kill() end)) mp.observe_property('volume', 'number', create_state_setter('volume')) mp.observe_property('volume-max', 'number', create_state_setter('volume_max')) mp.observe_property('mute', 'bool', create_state_setter('mute')) -mp.observe_property('playback-time', 'number', function(name, val) - -- Ignore the initial call with nil value - if val == nil then return end - - state.position = val - state.elapsed_seconds = val - state.elapsed_time = state.elapsed_seconds and - mp.format_time(state.elapsed_seconds) or nil - state.remaining_seconds = mp.get_property_native('playtime-remaining') - state.remaining_time = state.remaining_seconds and - mp.format_time(state.remaining_seconds) or nil - - request_render() -end) mp.observe_property('osd-dimensions', 'native', function(name, val) - update_display_dimensions() - request_render() + update_display_dimensions() + request_render() end) +mp.observe_property('display-hidpi-scale', 'native', create_state_setter('hidpi_scale', update_display_dimensions)) +mp.observe_property('cache', 'string', create_state_setter('cache')) +mp.observe_property('cache-buffering-state', 'number', create_state_setter('cache_buffering')) +mp.observe_property('demuxer-via-network', 'native', create_state_setter('is_stream', function() + Elements:trigger('dispositions') +end)) mp.observe_property('demuxer-cache-state', 'native', function(prop, cache_state) - if cache_state == nil then - state.cached_ranges = nil - return - end - local cache_ranges = cache_state['seekable-ranges'] - state.cached_ranges = #cache_ranges > 0 and cache_ranges or nil + local cached_ranges, bof, eof, uncached_ranges = nil, nil, nil, nil + if cache_state then + cached_ranges, bof, eof = cache_state['seekable-ranges'], cache_state['bof-cached'], cache_state['eof-cached'] + set_state('cache_underrun', cache_state['underrun']) + else cached_ranges = {} end + + if not (state.duration and (#cached_ranges > 0 or state.cache == 'yes' or + (state.cache == 'auto' and state.is_stream))) then + if state.uncached_ranges then set_state('uncached_ranges', nil) end + return + end + + -- Normalize + local ranges = {} + for _, range in ipairs(cached_ranges) do + ranges[#ranges + 1] = { + math.max(range['start'] or 0, 0), + math.min(range['end'] or state.duration, state.duration), + } + end + table.sort(ranges, function(a, b) return a[1] < b[1] end) + if bof then ranges[1][1] = 0 end + if eof then ranges[#ranges][2] = state.duration end + -- Invert cached ranges into uncached ranges, as that's what we're rendering + local inverted_ranges = {{0, state.duration}} + for _, cached in pairs(ranges) do + inverted_ranges[#inverted_ranges][2] = cached[1] + inverted_ranges[#inverted_ranges + 1] = {cached[2], state.duration} + end + uncached_ranges = {} + local last_range = nil + for _, range in ipairs(inverted_ranges) do + if last_range and last_range[2] + 0.5 > range[1] then -- fuse ranges + last_range[2] = range[2] + else + if range[2] - range[1] > 0.5 then -- skip short ranges + uncached_ranges[#uncached_ranges + 1] = range + last_range = range + end + end + end + + set_state('uncached_ranges', uncached_ranges) end) +mp.observe_property('display-fps', 'native', observe_display_fps) +mp.observe_property('estimated-display-fps', 'native', update_render_delay) +mp.observe_property('eof-reached', 'native', create_state_setter('eof_reached')) +mp.observe_property('core-idle', 'native', create_state_setter('core_idle')) --- CONTROLS +--[[ KEY BINDS ]] --- Mouse movement key binds -local base_keybinds = { - {'mouse_move', handle_mouse_move}, {'mouse_leave', handle_mouse_leave}, - {'mouse_enter', handle_mouse_enter} -} -if options.pause_on_click_shorter_than > 0 then - -- Cycles pause when click is shorter than `options.pause_on_click_shorter_than` - -- while filtering out double clicks. - local duration_seconds = options.pause_on_click_shorter_than / 1000 - local last_down_event; - local click_timer = mp.add_timeout(duration_seconds, - function() mp.command('cycle pause') end); - click_timer:kill() - base_keybinds[#base_keybinds + 1] = { - 'mbtn_left', function() - if mp.get_time() - last_down_event < duration_seconds then - click_timer:resume() - end - end, function() - if click_timer:is_enabled() then - click_timer:kill() - last_down_event = 0 - else - last_down_event = mp.get_time() - end - end - } +-- Pointer related binding groups +function make_cursor_handler(event, cb) + return function(...) + call_maybe(cursor[event], ...) + call_maybe(cb, ...) + cursor.queue_autohide() -- refresh cursor autohide timer + end end -mp.set_key_bindings(base_keybinds, 'mouse_movement', 'force') -mp.enable_key_bindings('mouse_movement', 'allow-vo-dragging+allow-hide-cursor') +mp.set_key_bindings({ + { + 'mbtn_left', + make_cursor_handler('on_primary_up'), + make_cursor_handler('on_primary_down', function(...) + handle_mouse_pos(nil, mp.get_property_native('mouse-pos')) + end), + }, + {'mbtn_left_dbl', 'ignore'}, +}, 'mbtn_left', 'force') +mp.set_key_bindings({ + {'wheel_up', make_cursor_handler('on_wheel_up')}, + {'wheel_down', make_cursor_handler('on_wheel_down')}, +}, 'wheel', 'force') --- Context based key bind groups +-- Adds a key binding that respects rerouting set by `key_binding_overwrites` table. +---@param name string +---@param callback fun(event: table) +---@param flags nil|string +function bind_command(name, callback, flags) + mp.add_key_binding(nil, name, function(...) + if key_binding_overwrites[name] then mp.command(key_binding_overwrites[name]) + else callback(...) end + end, flags) +end -forced_key_bindings = (function() - mp.set_key_bindings({ - { - 'mbtn_left', create_event_to_elements_dispatcher('mbtn_left_up'), - create_event_to_elements_dispatcher('mbtn_left_down') - }, {'mbtn_left_dbl', 'ignore'} - }, 'mouse_buttons', 'force') - mp.set_key_bindings({ - {'wheel_up', create_event_to_elements_dispatcher('wheel_up')}, - {'wheel_down', create_event_to_elements_dispatcher('wheel_down')} - }, 'wheel', 'force') - - local groups = {} - for _, group in ipairs({'mouse_buttons', 'wheel'}) do - groups[group] = { - is_enabled = false, - enable = function(this) - if this.is_enabled then return end - this.is_enabled = true - mp.enable_key_bindings(group) - end, - disable = function(this) - if not this.is_enabled then return end - this.is_enabled = false - mp.disable_key_bindings(group) - end - } - end - return groups -end)() - --- KEY BINDABLE FEATURES - -mp.add_key_binding(nil, 'peek-timeline', function() - if elements.timeline.proximity > 0.5 then - elements.timeline:tween_property('proximity', - elements.timeline.proximity, 0) - else - elements.timeline:tween_property('proximity', - elements.timeline.proximity, 1) - end +bind_command('toggle-ui', function() Elements:toggle({'timeline', 'controls', 'volume', 'top_bar'}) end) +bind_command('flash-ui', function() Elements:flash({'timeline', 'controls', 'volume', 'top_bar'}) end) +bind_command('flash-timeline', function() Elements:flash({'timeline'}) end) +bind_command('flash-top-bar', function() Elements:flash({'top_bar'}) end) +bind_command('flash-volume', function() Elements:flash({'volume'}) end) +bind_command('flash-speed', function() Elements:flash({'speed'}) end) +bind_command('flash-pause-indicator', function() Elements:flash({'pause_indicator'}) end) +bind_command('toggle-progress', function() + local timeline = Elements.timeline + if timeline.size_min_override then + timeline:tween_property('size_min_override', timeline.size_min_override, timeline.size_min, function() + timeline.size_min_override = nil + end) + else + timeline:tween_property('size_min_override', timeline.size_min, 0) + end end) -mp.add_key_binding(nil, 'toggle-progress', function() - local timeline = elements.timeline - if timeline.size_min_override then - timeline:tween_property('size_min_override', timeline.size_min_override, - timeline.size_min, - function() - timeline.size_min_override = nil - end) - else - timeline:tween_property('size_min_override', timeline.size_min, 0) - end +bind_command('toggle-title', function() Elements.top_bar:toggle_title() end) +bind_command('decide-pause-indicator', function() Elements.pause_indicator:decide() end) +bind_command('menu', function() toggle_menu_with_items() end) +bind_command('menu-blurred', function() toggle_menu_with_items({mouse_nav = true}) end) +local track_loaders = { + {name = 'subtitles', prop = 'sub', allowed_types = itable_join(config.types.video, config.types.subtitle)}, + {name = 'audio', prop = 'audio', allowed_types = itable_join(config.types.video, config.types.audio)}, + {name = 'video', prop = 'video', allowed_types = config.types.video}, +} +for _, loader in ipairs(track_loaders) do + local menu_type = 'load-' .. loader.name + bind_command(menu_type, function() + if Menu:is_open(menu_type) then Menu:close() return end + + local path = state.path + if path then + if is_protocol(path) then + path = false + else + local serialized_path = serialize_path(path) + path = serialized_path ~= nil and serialized_path.dirname or false + end + end + if not path then + path = get_default_directory() + end + open_file_navigation_menu( + path, + function(path) mp.commandv(loader.prop .. '-add', path) end, + {type = menu_type, title = 'Load ' .. loader.name, allowed_types = loader.allowed_types} + ) + end) +end +bind_command('subtitles', create_select_tracklist_type_menu_opener( + 'Subtitles', 'sub', 'sid', 'script-binding uosc/load-subtitles' +)) +bind_command('audio', create_select_tracklist_type_menu_opener( + 'Audio', 'audio', 'aid', 'script-binding uosc/load-audio' +)) +bind_command('video', create_select_tracklist_type_menu_opener( + 'Video', 'video', 'vid', 'script-binding uosc/load-video' +)) +bind_command('playlist', create_self_updating_menu_opener({ + title = 'Playlist', + type = 'playlist', + list_prop = 'playlist', + serializer = function(playlist) + local items = {} + for index, item in ipairs(playlist) do + local is_url = item.filename:find('://') + local item_title = type(item.title) == 'string' and #item.title > 0 and item.title or false + items[index] = { + title = item_title or (is_url and item.filename or serialize_path(item.filename).basename), + hint = tostring(index), + active = item.current, + value = index, + } + end + return items + end, + on_select = function(index) mp.commandv('set', 'playlist-pos-1', tostring(index)) end, + on_move_item = function(from, to) + mp.commandv('playlist-move', tostring(math.max(from, to) - 1), tostring(math.min(from, to) - 1)) + end, + on_delete_item = function(index) mp.commandv('playlist-remove', tostring(index - 1)) end, +})) +bind_command('chapters', create_self_updating_menu_opener({ + title = 'Chapters', + type = 'chapters', + list_prop = 'chapter-list', + active_prop = 'chapter', + serializer = function(chapters, current_chapter) + local items = {} + chapters = normalize_chapters(chapters) + for index, chapter in ipairs(chapters) do + items[index] = { + title = chapter.title or '', + hint = format_time(chapter.time, state.duration), + value = index, + active = index - 1 == current_chapter, + } + end + return items + end, + on_select = function(index) mp.commandv('set', 'chapter', tostring(index - 1)) end, +})) +bind_command('editions', create_self_updating_menu_opener({ + title = 'Editions', + type = 'editions', + list_prop = 'edition-list', + active_prop = 'current-edition', + serializer = function(editions, current_id) + local items = {} + for _, edition in ipairs(editions or {}) do + items[#items + 1] = { + title = edition.title or 'Edition', + hint = tostring(edition.id + 1), + value = edition.id, + active = edition.id == current_id, + } + end + return items + end, + on_select = function(id) mp.commandv('set', 'edition', id) end, +})) +bind_command('show-in-directory', function() + -- Ignore URLs + if not state.path or is_protocol(state.path) then return end + + if state.platform == 'windows' then + utils.subprocess_detached({args = {'explorer', '/select,', state.path}, cancellable = false}) + elseif state.platform == 'macos' then + utils.subprocess_detached({args = {'open', '-R', state.path}, cancellable = false}) + elseif state.platform == 'linux' then + local result = utils.subprocess({args = {'nautilus', state.path}, cancellable = false}) + + -- Fallback opens the folder with xdg-open instead + if result.status ~= 0 then + utils.subprocess({args = {'xdg-open', serialize_path(state.path).dirname}, cancellable = false}) + end + end end) -mp.add_key_binding(nil, 'menu', function() - if menu:is_open('menu') then - menu:close() - elseif state.context_menu_items then - menu:open(state.context_menu_items, - function(command) mp.command(command) end, {type = 'menu'}) - end +bind_command('stream-quality', function() + if Menu:is_open('stream-quality') then Menu:close() return end + + local ytdl_format = mp.get_property_native('ytdl-format') + local items = {} + + for _, height in ipairs(config.stream_quality_options) do + local format = 'bestvideo[height<=?' .. height .. ']+bestaudio/best[height<=?' .. height .. ']' + items[#items + 1] = {title = height .. 'p', value = format, active = format == ytdl_format} + end + + Menu:open({type = 'stream-quality', title = 'Stream quality', items = items}, function(format) + mp.set_property('ytdl-format', format) + + -- Reload the video to apply new format + -- This is taken from https://github.com/jgreco/mpv-youtube-quality + -- which is in turn taken from https://github.com/4e6/mpv-reload/ + -- Dunno if playlist_pos shenanigans below are necessary. + local playlist_pos = mp.get_property_number('playlist-pos') + local duration = mp.get_property_native('duration') + local time_pos = mp.get_property('time-pos') + + mp.set_property_number('playlist-pos', playlist_pos) + + -- Tries to determine live stream vs. pre-recorded VOD. VOD has non-zero + -- duration property. When reloading VOD, to keep the current time position + -- we should provide offset from the start. Stream doesn't have fixed start. + -- Decent choice would be to reload stream from it's current 'live' position. + -- That's the reason we don't pass the offset when reloading streams. + if duration and duration > 0 then + local function seeker() + mp.commandv('seek', time_pos, 'absolute') + mp.unregister_event(seeker) + end + mp.register_event('file-loaded', seeker) + end + end) end) -mp.add_key_binding(nil, 'load-subtitles', function() - if menu:is_open('load-subtitles') then - menu:close() - return - end +bind_command('open-file', function() + if Menu:is_open('open-file') then Menu:close() return end - local path = mp.get_property_native('path') - if path and not is_protocol(path) then - open_file_navigation_menu(serialize_path(path).dirname, function(path) - mp.commandv('sub-add', path) - end, {type = 'load-subtitles', allowed_types = options.subtitle_types}) - end + local directory + local active_file + + if state.path == nil or is_protocol(state.path) then + local serialized = serialize_path(get_default_directory()) + if serialized then + directory = serialized.path + active_file = nil + end + else + local serialized = serialize_path(state.path) + if serialized then + directory = serialized.dirname + active_file = serialized.path + end + end + + if not directory then + msg.error('Couldn\'t serialize path "' .. state.path .. '".') + return + end + + -- Update active file in directory navigation menu + local function handle_file_loaded() + if Menu:is_open('open-file') then + Elements.menu:activate_one_value(normalize_path(mp.get_property_native('path'))) + end + end + + open_file_navigation_menu( + directory, + function(path) mp.commandv('loadfile', path) end, + { + type = 'open-file', + allowed_types = config.types.media, + active_path = active_file, + on_open = function() mp.register_event('file-loaded', handle_file_loaded) end, + on_close = function() mp.unregister_event(handle_file_loaded) end, + } + ) end) -mp.add_key_binding(nil, 'subtitles', create_select_tracklist_type_menu_opener( - 'Subtitles', 'sub', 'sid')) -mp.add_key_binding(nil, 'audio', create_select_tracklist_type_menu_opener( - 'Audio', 'audio', 'aid')) -mp.add_key_binding(nil, 'video', create_select_tracklist_type_menu_opener( - 'Video', 'video', 'vid')) -mp.add_key_binding(nil, 'playlist', function() - if menu:is_open('playlist') then - menu:close() - return - end - - function serialize_playlist() - local pos = mp.get_property_number('playlist-pos-1', 0) - local items = {} - local active_item - for index, item in ipairs(mp.get_property_native('playlist')) do - local is_url = item.filename:find('://') - items[index] = { - title = is_url and item.filename or - serialize_path(item.filename).basename, - hint = tostring(index), - value = index - } - - if index == pos then active_item = index end - end - return items, active_item - end - - -- Update active index and playlist content on playlist changes - function handle_playlist_change() - if menu:is_open('playlist') then - local items, active_item = serialize_playlist() - elements.menu:set_items(items, { - active_item = active_item, - selected_item = active_item - }) - end - end - - local items, active_item = serialize_playlist() - - menu:open(items, function(index) - mp.commandv('set', 'playlist-pos-1', tostring(index)) - end, { - type = 'playlist', - title = 'Playlist', - active_item = active_item, - on_open = function() - mp.observe_property('playlist', 'native', handle_playlist_change) - mp.observe_property('playlist-pos-1', 'native', - handle_playlist_change) - end, - on_close = function() - mp.unobserve_property(handle_playlist_change) - end - }) +bind_command('shuffle', function() set_state('shuffle', not state.shuffle) end) +bind_command('items', function() + if state.has_playlist then + mp.command('script-binding uosc/playlist') + else + mp.command('script-binding uosc/open-file') + end end) -mp.add_key_binding(nil, 'chapters', function() - if menu:is_open('chapters') then - menu:close() - return - end - - local items = {} - local chapters = get_normalized_chapters() - - for index, chapter in ipairs(chapters) do - items[#items + 1] = { - title = chapter.title or '', - hint = mp.format_time(chapter.time), - value = chapter.time - } - end - - -- Select first chapter from the end with time lower - -- than current playing position (with 100ms leeway). - function get_selected_chapter_index() - local position = mp.get_property_native('playback-time') - if not position then return nil end - for index = #items, 1, -1 do - if position - 0.1 > items[index].value then return index end - end - end - - -- Update selected chapter in chapter navigation menu - function seek_handler() - if menu:is_open('chapters') then - elements.menu:activate_index(get_selected_chapter_index()) - end - end - - menu:open(items, function(time) - mp.commandv('seek', tostring(time), 'absolute') - end, { - type = 'chapters', - title = 'Chapters', - active_item = get_selected_chapter_index(), - on_open = function() mp.register_event('seek', seek_handler) end, - on_close = function() mp.unregister_event(seek_handler) end - }) +bind_command('next', function() navigate_item(1) end) +bind_command('prev', function() navigate_item(-1) end) +bind_command('next-file', function() navigate_directory(1) end) +bind_command('prev-file', function() navigate_directory(-1) end) +bind_command('first', function() + if state.has_playlist then + mp.commandv('set', 'playlist-pos-1', '1') + else + load_file_index_in_current_directory(1) + end end) -mp.add_key_binding(nil, 'show-in-directory', function() - local path = mp.get_property_native('path') - - -- Ignore URLs - if not path or is_protocol(path) then return end - - path = normalize_path(path) - - if state.os == 'windows' then - utils.subprocess_detached({ - args = {'explorer', '/select,', path}, - cancellable = false - }) - elseif state.os == 'macos' then - utils.subprocess_detached({ - args = {'open', '-R', path}, - cancellable = false - }) - elseif state.os == 'linux' then - local result = utils.subprocess({ - args = {'nautilus', path}, - cancellable = false - }) - - -- Fallback opens the folder with xdg-open instead - if result.status ~= 0 then - utils.subprocess({ - args = {'xdg-open', serialize_path(path).dirname}, - cancellable = false - }) - end - end +bind_command('last', function() + if state.has_playlist then + mp.commandv('set', 'playlist-pos-1', tostring(state.playlist_count)) + else + load_file_index_in_current_directory(-1) + end end) -mp.add_key_binding(nil, 'open-file', function() - if menu:is_open('open-file') then - menu:close() - return - end +bind_command('first-file', function() load_file_index_in_current_directory(1) end) +bind_command('last-file', function() load_file_index_in_current_directory(-1) end) +bind_command('delete-file-next', function() + local next_file = nil + local is_local_file = state.path and not is_protocol(state.path) - local path = mp.get_property_native('path') - local directory - local active_file + if is_local_file then + if Menu:is_open('open-file') then Elements.menu:delete_value(state.path) end + end - if path == nil or is_protocol(path) then - local path = serialize_path(mp.command_native({'expand-path', '~/'})) - directory = path.path - active_file = nil - else - local path = serialize_path(path) - directory = path.dirname - active_file = path.path - end + if state.has_playlist then + mp.commandv('playlist-remove', 'current') + else + if is_local_file then + local paths, current_index = get_adjacent_files(state.path, config.types.autoload) + if paths and current_index then + local index, path = decide_navigation_in_list(paths, current_index, 1) + if path then next_file = path end + end + end - -- Update selected file in directory navigation menu - function handle_file_loaded() - if menu:is_open('open-file') then - local path = normalize_path(mp.get_property_native('path')) - elements.menu:activate_value(path) - elements.menu:select_value(path) - end - end + if next_file then mp.commandv('loadfile', next_file) + else mp.commandv('stop') end + end - open_file_navigation_menu(directory, - function(path) mp.commandv('loadfile', path) end, - { - type = 'open-file', - allowed_types = options.media_types, - active_path = active_file, - on_open = function() - mp.register_event('file-loaded', handle_file_loaded) - end, - on_close = function() mp.unregister_event(handle_file_loaded) end - }) + if is_local_file then delete_file(state.path) end end) -mp.add_key_binding(nil, 'next', function() - if mp.get_property_native('playlist-count') > 1 then - mp.command('playlist-next') - else - navigate_directory('forward') - end +bind_command('delete-file-quit', function() + mp.command('stop') + if state.path and not is_protocol(state.path) then delete_file(state.path) end + mp.command('quit') end) -mp.add_key_binding(nil, 'prev', function() - if mp.get_property_native('playlist-count') > 1 then - mp.command('playlist-prev') - else - navigate_directory('backward') - end +bind_command('audio-device', create_self_updating_menu_opener({ + title = 'Audio devices', + type = 'audio-device-list', + list_prop = 'audio-device-list', + active_prop = 'audio-device', + serializer = function(audio_device_list, current_device) + current_device = current_device or 'auto' + local ao = mp.get_property('current-ao') or '' + local items = {} + for _, device in ipairs(audio_device_list) do + if device.name == 'auto' or string.match(device.name, '^' .. ao) then + local hint = string.match(device.name, ao .. '/(.+)') + if not hint then hint = device.name end + items[#items + 1] = { + title = device.description, + hint = hint, + active = device.name == current_device, + value = device.name, + } + end + end + return items + end, + on_select = function(name) mp.commandv('set', 'audio-device', name) end, +})) +bind_command('open-config-directory', function() + local config_path = mp.command_native({'expand-path', '~~/mpv.conf'}) + local config = serialize_path(normalize_path(config_path)) + + if config then + local args + + if state.platform == 'windows' then + args = {'explorer', '/select,', config.path} + elseif state.platform == 'macos' then + args = {'open', '-R', config.path} + elseif state.platform == 'linux' then + args = {'xdg-open', config.dirname} + end + + utils.subprocess_detached({args = args, cancellable = false}) + else + msg.error('Couldn\'t serialize config path "' .. config_path .. '".') + end end) -mp.add_key_binding(nil, 'next-file', - function() navigate_directory('forward') end) -mp.add_key_binding(nil, 'prev-file', - function() navigate_directory('backward') end) -mp.add_key_binding(nil, 'first', function() - if mp.get_property_native('playlist-count') > 1 then - mp.commandv('set', 'playlist-pos-1', '1') - else - load_file_in_current_directory(1) - end + +--[[ MESSAGE HANDLERS ]] + +mp.register_script_message('show-submenu', function(id) toggle_menu_with_items({submenu = id}) end) +mp.register_script_message('show-submenu-blurred', function(id) + toggle_menu_with_items({submenu = id, mouse_nav = true}) end) -mp.add_key_binding(nil, 'last', function() - local playlist_count = mp.get_property_native('playlist-count') - if playlist_count > 1 then - mp.commandv('set', 'playlist-pos-1', tostring(playlist_count)) - else - load_file_in_current_directory(-1) - end +mp.register_script_message('get-version', function(script) + mp.commandv('script-message-to', script, 'uosc-version', config.version) end) -mp.add_key_binding(nil, 'first-file', - function() load_file_in_current_directory(1) end) -mp.add_key_binding(nil, 'last-file', - function() load_file_in_current_directory(-1) end) -mp.add_key_binding(nil, 'delete-file-next', function() - local path = mp.get_property_native('path') - - if not path or is_protocol(path) then return end - - path = normalize_path(path) - local playlist_count = mp.get_property_native('playlist-count') - - if playlist_count > 1 then - mp.commandv('playlist-remove', 'current') - else - local next_file = - get_adjacent_file(path, 'forward', options.media_types) - - if menu:is_open('open-file') then - elements.menu:delete_value(path) - end - - if next_file then - mp.commandv('loadfile', next_file) - else - mp.commandv('stop') - end - end - - os.remove(path) +mp.register_script_message('open-menu', function(json, submenu_id) + local data = utils.parse_json(json) + if type(data) ~= 'table' or type(data.items) ~= 'table' then + msg.error('open-menu: received json didn\'t produce a table with menu configuration') + else + if data.type and Menu:is_open(data.type) then Menu:close() + else open_command_menu(data, {submenu = submenu_id, on_close = data.on_close}) end + end end) -mp.add_key_binding(nil, 'delete-file-quit', function() - local path = mp.get_property_native('path') - if not path or is_protocol(path) then return end - os.remove(normalize_path(path)) - mp.command('quit') +mp.register_script_message('update-menu', function(json) + local data = utils.parse_json(json) + if type(data) ~= 'table' or type(data.items) ~= 'table' then + msg.error('update-menu: received json didn\'t produce a table with menu configuration') + else + local menu = data.type and Menu:is_open(data.type) + if menu then menu:update(data) + else open_command_menu(data) end + end end) -mp.add_key_binding(nil, 'open-config-directory', function() - local config = serialize_path(mp.command_native( - {'expand-path', '~~/mpv.conf'})) - local args - - if state.os == 'windows' then - args = {'explorer', '/select,', config.path} - elseif state.os == 'macos' then - args = {'open', '-R', config.path} - elseif state.os == 'linux' then - args = {'xdg-open', config.dirname} - end - - utils.subprocess_detached({args = args, cancellable = false}) +mp.register_script_message('thumbfast-info', function(json) + local data = utils.parse_json(json) + if type(data) ~= 'table' or not data.width or not data.height then + thumbnail.disabled = true + msg.error('thumbfast-info: received json didn\'t produce a table with thumbnail information') + else + thumbnail = data + request_render() + end end) +mp.register_script_message('set', function(name, value) + external[name] = value + Elements:trigger('external_prop_' .. name, value) +end) +mp.register_script_message('toggle-elements', function(elements) Elements:toggle(split(elements, ' *, *')) end) +mp.register_script_message('set-min-visibility', function(visibility, elements) + local fraction = tonumber(visibility) + local ids = split(elements and elements ~= '' and elements or 'timeline,controls,volume,top_bar', ' *, *') + if fraction then Elements:set_min_visibility(clamp(0, fraction, 1), ids) end +end) +mp.register_script_message('flash-elements', function(elements) Elements:flash(split(elements, ' *, *')) end) +mp.register_script_message('overwrite-binding', function(name, command) key_binding_overwrites[name] = command end) + +--[[ ELEMENTS ]] + +require('uosc_shared/elements/WindowBorder'):new() +require('uosc_shared/elements/BufferingIndicator'):new() +require('uosc_shared/elements/PauseIndicator'):new() +require('uosc_shared/elements/TopBar'):new() +require('uosc_shared/elements/Timeline'):new() +if options.controls and options.controls ~= 'never' then require('uosc_shared/elements/Controls'):new() end +if itable_index_of({'left', 'right'}, options.volume) then require('uosc_shared/elements/Volume'):new() end +require('uosc_shared/elements/Curtain'):new() diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/BufferingIndicator.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/BufferingIndicator.lua new file mode 100644 index 0000000..e2aa071 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/BufferingIndicator.lua @@ -0,0 +1,37 @@ +local Element = require('uosc_shared/elements/Element') + +---@class BufferingIndicator : Element +local BufferingIndicator = class(Element) + +function BufferingIndicator:new() return Class.new(self) --[[@as BufferingIndicator]] end +function BufferingIndicator:init() + Element.init(self, 'buffer_indicator') + self.ignores_menu = true + self.enabled = false +end + +function BufferingIndicator:decide_enabled() + local cache = state.cache_underrun or state.cache_buffering and state.cache_buffering < 100 + local player = state.core_idle and not state.eof_reached + if self.enabled then + if not player or (state.pause and not cache) then self.enabled = false end + elseif player and cache and state.uncached_ranges then self.enabled = true end +end + +function BufferingIndicator:on_prop_pause() self:decide_enabled() end +function BufferingIndicator:on_prop_core_idle() self:decide_enabled() end +function BufferingIndicator:on_prop_eof_reached() self:decide_enabled() end +function BufferingIndicator:on_prop_uncached_ranges() self:decide_enabled() end +function BufferingIndicator:on_prop_cache_buffering() self:decide_enabled() end +function BufferingIndicator:on_prop_cache_underrun() self:decide_enabled() end + +function BufferingIndicator:render() + local ass = assdraw.ass_new() + ass:rect(0, 0, display.width, display.height, {color = bg, opacity = 0.3}) + local size = round(30 + math.min(display.width, display.height) / 10) + local opacity = (Elements.menu and not Elements.menu.is_closing) and 0.3 or 0.8 + ass:spinner(display.width / 2, display.height / 2, size, {color = fg, opacity = opacity}) + return ass +end + +return BufferingIndicator diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Button.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Button.lua new file mode 100644 index 0000000..e57d614 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Button.lua @@ -0,0 +1,90 @@ +local Element = require('uosc_shared/elements/Element') + +---@alias ButtonProps {icon: string; on_click: function; anchor_id?: string; active?: boolean; badge?: string|number; foreground?: string; background?: string; tooltip?: string} + +---@class Button : Element +local Button = class(Element) + +---@param id string +---@param props ButtonProps +function Button:new(id, props) return Class.new(self, id, props) --[[@as Button]] end +---@param id string +---@param props ButtonProps +function Button:init(id, props) + self.icon = props.icon + self.active = props.active + self.tooltip = props.tooltip + self.badge = props.badge + self.foreground = props.foreground or fg + self.background = props.background or bg + ---@type fun() + self.on_click = props.on_click + Element.init(self, id, props) +end + +function Button:on_coordinates() self.font_size = round((self.by - self.ay) * 0.7) end +function Button:handle_cursor_down() + -- We delay the callback to next tick, otherwise we are risking race + -- conditions as we are in the middle of event dispatching. + -- For example, handler might add a menu to the end of the element stack, and that + -- than picks up this click event we are in right now, and instantly closes itself. + mp.add_timeout(0.01, self.on_click) +end + +function Button:render() + local visibility = self:get_visibility() + if visibility <= 0 then return end + if self.proximity_raw == 0 then + cursor.on_primary_down = function() self:handle_cursor_down() end + end + + local ass = assdraw.ass_new() + local is_hover = self.proximity_raw == 0 + local is_hover_or_active = is_hover or self.active + local foreground = self.active and self.background or self.foreground + local background = self.active and self.foreground or self.background + + -- Background + if is_hover_or_active then + ass:rect(self.ax, self.ay, self.bx, self.by, { + color = self.active and background or foreground, radius = 2, + opacity = visibility * (self.active and 1 or 0.3), + }) + end + + -- Tooltip on hover + if is_hover and self.tooltip then ass:tooltip(self, self.tooltip) end + + -- Badge + local icon_clip + if self.badge then + local badge_font_size = self.font_size * 0.6 + local badge_opts = {size = badge_font_size, color = background, opacity = visibility} + local badge_width = text_width(self.badge, badge_opts) + local width, height = math.ceil(badge_width + (badge_font_size / 7) * 2), math.ceil(badge_font_size * 0.93) + local bx, by = self.bx - 1, self.by - 1 + ass:rect(bx - width, by - height, bx, by, { + color = foreground, radius = 2, opacity = visibility, + border = self.active and 0 or 1, border_color = background, + }) + ass:txt(bx - width / 2, by - height / 2, 5, self.badge, badge_opts) + + local clip_border = math.max(self.font_size / 20, 1) + local clip_path = assdraw.ass_new() + clip_path:round_rect_cw( + math.floor((bx - width) - clip_border), math.floor((by - height) - clip_border), bx, by, 3 + ) + icon_clip = '\\iclip(' .. clip_path.scale .. ', ' .. clip_path.text .. ')' + end + + -- Icon + local x, y = round(self.ax + (self.bx - self.ax) / 2), round(self.ay + (self.by - self.ay) / 2) + ass:icon(x, y, self.font_size, self.icon, { + color = foreground, border = self.active and 0 or options.text_border, border_color = background, + opacity = visibility, clip = icon_clip, + }) + + return ass +end + +return Button diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Controls.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Controls.lua new file mode 100644 index 0000000..9a6be72 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Controls.lua @@ -0,0 +1,329 @@ +local Element = require('uosc_shared/elements/Element') +local Button = require('uosc_shared/elements/Button') +local CycleButton = require('uosc_shared/elements/CycleButton') +local Speed = require('uosc_shared/elements/Speed') + +-- `scale` - `options.controls_size` scale factor. +-- `ratio` - Width/height ratio of a static or dynamic element. +-- `ratio_min` Min ratio for 'dynamic' sized element. +---@alias ControlItem {element?: Element; kind: string; sizing: 'space' | 'static' | 'dynamic'; scale: number; ratio?: number; ratio_min?: number; hide: boolean; dispositions?: table} + +---@class Controls : Element +local Controls = class(Element) + +function Controls:new() return Class.new(self) --[[@as Controls]] end +function Controls:init() + Element.init(self, 'controls') + ---@type ControlItem[] All control elements serialized from `options.controls`. + self.controls = {} + ---@type ControlItem[] Only controls that match current dispositions. + self.layout = {} + + -- Serialize control elements + local shorthands = { + menu = 'command:menu:script-binding uosc/menu-blurred?Menu', + subtitles = 'command:subtitles:script-binding uosc/subtitles#sub>0?Subtitles', + audio = 'command:graphic_eq:script-binding uosc/audio#audio>1?Audio', + ['audio-device'] = 'command:speaker:script-binding uosc/audio-device?Audio device', + video = 'command:theaters:script-binding uosc/video#video>1?Video', + playlist = 'command:list_alt:script-binding uosc/playlist?Playlist', + chapters = 'command:bookmark:script-binding uosc/chapters#chapters>0?Chapters', + ['editions'] = 'command:bookmarks:script-binding uosc/editions#editions>1?Editions', + ['stream-quality'] = 'command:high_quality:script-binding uosc/stream-quality?Stream quality', + ['open-file'] = 'command:file_open:script-binding uosc/open-file?Open file', + ['items'] = 'command:list_alt:script-binding uosc/items?Playlist/Files', + prev = 'command:arrow_back_ios:script-binding uosc/prev?Previous', + next = 'command:arrow_forward_ios:script-binding uosc/next?Next', + first = 'command:first_page:script-binding uosc/first?First', + last = 'command:last_page:script-binding uosc/last?Last', + ['loop-playlist'] = 'cycle:repeat:loop-playlist:no/inf!?Loop playlist', + ['loop-file'] = 'cycle:repeat_one:loop-file:no/inf!?Loop file', + shuffle = 'toggle:shuffle:shuffle?Shuffle', + fullscreen = 'cycle:crop_free:fullscreen:no/yes=fullscreen_exit!?Fullscreen', + } + + -- Parse out disposition/config pairs + local items = {} + local in_disposition = false + local current_item = nil + for c in options.controls:gmatch('.') do + if not current_item then current_item = {disposition = '', config = ''} end + if c == '<' and #current_item.config == 0 then in_disposition = true + elseif c == '>' and #current_item.config == 0 then in_disposition = false + elseif c == ',' and not in_disposition then + items[#items + 1] = current_item + current_item = nil + else + local prop = in_disposition and 'disposition' or 'config' + current_item[prop] = current_item[prop] .. c + end + end + items[#items + 1] = current_item + + -- Create controls + self.controls = {} + for i, item in ipairs(items) do + local config = shorthands[item.config] and shorthands[item.config] or item.config + local config_tooltip = split(config, ' *%? *') + local tooltip = config_tooltip[2] + config = shorthands[config_tooltip[1]] + and split(shorthands[config_tooltip[1]], ' *%? *')[1] or config_tooltip[1] + local config_badge = split(config, ' *# *') + config = config_badge[1] + local badge = config_badge[2] + local parts = split(config, ' *: *') + local kind, params = parts[1], itable_slice(parts, 2) + + -- Serialize dispositions + local dispositions = {} + for _, definition in ipairs(split(item.disposition, ' *, *')) do + if #definition > 0 then + local value = definition:sub(1, 1) ~= '!' + local name = not value and definition:sub(2) or definition + local prop = name:sub(1, 4) == 'has_' and name or 'is_' .. name + dispositions[prop] = value + end + end + + -- Convert toggles into cycles + if kind == 'toggle' then + kind = 'cycle' + params[#params + 1] = 'no/yes!' + end + + -- Create a control element + local control = {dispositions = dispositions, kind = kind} + + if kind == 'space' then + control.sizing = 'space' + elseif kind == 'gap' then + table_assign(control, {sizing = 'dynamic', scale = 1, ratio = params[1] or 0.3, ratio_min = 0}) + elseif kind == 'command' then + if #params ~= 2 then + mp.error(string.format( + 'command button needs 2 parameters, %d received: %s', #params, table.concat(params, '/') + )) + else + local element = Button:new('control_' .. i, { + icon = params[1], + anchor_id = 'controls', + on_click = function() mp.command(params[2]) end, + tooltip = tooltip, + count_prop = 'sub', + }) + table_assign(control, {element = element, sizing = 'static', scale = 1, ratio = 1}) + if badge then self:register_badge_updater(badge, element) end + end + elseif kind == 'cycle' then + if #params ~= 3 then + mp.error(string.format( + 'cycle button needs 3 parameters, %d received: %s', + #params, table.concat(params, '/') + )) + else + local state_configs = split(params[3], ' */ *') + local states = {} + + for _, state_config in ipairs(state_configs) do + local active = false + if state_config:sub(-1) == '!' then + active = true + state_config = state_config:sub(1, -2) + end + local state_params = split(state_config, ' *= *') + local value, icon = state_params[1], state_params[2] or params[1] + states[#states + 1] = {value = value, icon = icon, active = active} + end + + local element = CycleButton:new('control_' .. i, { + prop = params[2], anchor_id = 'controls', states = states, tooltip = tooltip, + }) + table_assign(control, {element = element, sizing = 'static', scale = 1, ratio = 1}) + if badge then self:register_badge_updater(badge, element) end + end + elseif kind == 'speed' then + if not Elements.speed then + local element = Speed:new({anchor_id = 'controls'}) + table_assign(control, { + element = element, sizing = 'dynamic', scale = params[1] or 1.3, ratio = 3.5, ratio_min = 2, + }) + else + msg.error('there can only be 1 speed slider') + end + else + msg.error('unknown element kind "' .. kind .. '"') + break + end + + self.controls[#self.controls + 1] = control + end + + self:reflow() +end + +function Controls:reflow() + -- Populate the layout only with items that match current disposition + self.layout = {} + for _, control in ipairs(self.controls) do + local matches = true + for prop, value in pairs(control.dispositions) do + if state[prop] ~= value then + matches = false + break + end + end + if control.element then control.element.enabled = matches end + if matches then self.layout[#self.layout + 1] = control end + end + + self:update_dimensions() + Elements:trigger('controls_reflow') +end + +---@param badge string +---@param element Element An element that supports `badge` property. +function Controls:register_badge_updater(badge, element) + local prop_and_limit = split(badge, ' *> *') + local prop, limit = prop_and_limit[1], tonumber(prop_and_limit[2] or -1) + local observable_name, serializer, is_external_prop = prop, nil, false + + if itable_index_of({'sub', 'audio', 'video'}, prop) then + observable_name = 'track-list' + serializer = function(value) + local count = 0 + for _, track in ipairs(value) do if track.type == prop then count = count + 1 end end + return count + end + else + local parts = split(prop, '@') + -- Support both new `prop@owner` and old `@prop` syntaxes + if #parts > 1 then prop, is_external_prop = parts[1] ~= '' and parts[1] or parts[2], true end + serializer = function(value) return value and (type(value) == 'table' and #value or tostring(value)) or nil end + end + + local function handler(_, value) + local new_value = serializer(value) --[[@as nil|string|integer]] + local value_number = tonumber(new_value) + if value_number then new_value = value_number > limit and value_number or nil end + element.badge = new_value + request_render() + end + + if is_external_prop then element['on_external_prop_' .. prop] = function(_, value) handler(prop, value) end + else mp.observe_property(observable_name, 'native', handler) end +end + +function Controls:get_visibility() + return (Elements.speed and Elements.speed.dragging) and 1 or Elements.timeline:get_is_hovered() + and -1 or Element.get_visibility(self) +end + +function Controls:update_dimensions() + local window_border = Elements.window_border.size + local size = state.fullormaxed and options.controls_size_fullscreen or options.controls_size + local spacing = options.controls_spacing + local margin = options.controls_margin + + -- Disable when not enough space + local available_space = display.height - Elements.window_border.size * 2 + if Elements.top_bar.enabled then available_space = available_space - Elements.top_bar.size end + if Elements.timeline.enabled then available_space = available_space - Elements.timeline.size_max end + self.enabled = available_space > size + 10 + + -- Reset hide/enabled flags + for c, control in ipairs(self.layout) do + control.hide = false + if control.element then control.element.enabled = self.enabled end + end + + if not self.enabled then return end + + -- Container + self.bx = display.width - window_border - margin + self.by = (Elements.timeline.enabled and Elements.timeline.ay or display.height - window_border) - margin + self.ax, self.ay = window_border + margin, self.by - size + + -- Controls + local available_width = self.bx - self.ax + local statics_width = (#self.layout - 1) * spacing + local min_content_width = statics_width + local max_dynamics_width, dynamic_units, spaces = 0, 0, 0 + + -- Calculate statics_width, min_content_width, and count spaces + for c, control in ipairs(self.layout) do + if control.sizing == 'space' then + spaces = spaces + 1 + elseif control.sizing == 'static' then + local width = size * control.scale * control.ratio + statics_width = statics_width + width + min_content_width = min_content_width + width + elseif control.sizing == 'dynamic' then + min_content_width = min_content_width + size * control.scale * control.ratio_min + max_dynamics_width = max_dynamics_width + size * control.scale * control.ratio + dynamic_units = dynamic_units + control.scale * control.ratio + end + end + + -- Hide & disable elements in the middle until we fit into available width + if min_content_width > available_width then + local i = math.ceil(#self.layout / 2 + 0.1) + for a = 0, #self.layout - 1, 1 do + i = i + (a * (a % 2 == 0 and 1 or -1)) + local control = self.layout[i] + + if control.kind ~= 'gap' and control.kind ~= 'space' then + control.hide = true + if control.element then control.element.enabled = false end + if control.sizing == 'static' then + local width = size * control.scale * control.ratio + min_content_width = min_content_width - width - spacing + statics_width = statics_width - width - spacing + elseif control.sizing == 'dynamic' then + min_content_width = min_content_width - size * control.scale * control.ratio_min - spacing + max_dynamics_width = max_dynamics_width - size * control.scale * control.ratio + dynamic_units = dynamic_units - control.scale * control.ratio + end + + if min_content_width < available_width then break end + end + end + end + + -- Lay out the elements + local current_x = self.ax + local width_for_dynamics = available_width - statics_width + local space_width = (width_for_dynamics - max_dynamics_width) / spaces + + for c, control in ipairs(self.layout) do + if not control.hide then + local sizing, element, scale, ratio = control.sizing, control.element, control.scale, control.ratio + local width, height = 0, 0 + + if sizing == 'space' then + if space_width > 0 then width = space_width end + elseif sizing == 'static' then + height = size * scale + width = height * ratio + elseif sizing == 'dynamic' then + height = size * scale + width = max_dynamics_width < width_for_dynamics + and height * ratio or width_for_dynamics * ((scale * ratio) / dynamic_units) + end + + local bx = current_x + width + if element then element:set_coordinates(round(current_x), round(self.by - height), bx, self.by) end + current_x = bx + spacing + end + end + + Elements:update_proximities() + request_render() +end + +function Controls:on_dispositions() self:reflow() end +function Controls:on_display() self:update_dimensions() end +function Controls:on_prop_border() self:update_dimensions() end +function Controls:on_prop_fullormaxed() self:update_dimensions() end +function Controls:on_timeline_enabled() self:update_dimensions() end + +return Controls diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Curtain.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Curtain.lua new file mode 100644 index 0000000..99b9f14 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Curtain.lua @@ -0,0 +1,35 @@ +local Element = require('uosc_shared/elements/Element') + +---@class Curtain : Element +local Curtain = class(Element) + +function Curtain:new() return Class.new(self) --[[@as Curtain]] end +function Curtain:init() + Element.init(self, 'curtain', {ignores_menu = true}) + self.opacity = 0 + ---@type string[] + self.dependents = {} +end + +---@param id string +function Curtain:register(id) + self.dependents[#self.dependents + 1] = id + if #self.dependents == 1 then self:tween_property('opacity', self.opacity, 1) end +end + +---@param id string +function Curtain:unregister(id) + self.dependents = itable_filter(self.dependents, function(item) return item ~= id end) + if #self.dependents == 0 then self:tween_property('opacity', self.opacity, 0) end +end + +function Curtain:render() + if self.opacity == 0 or options.curtain_opacity == 0 then return end + local ass = assdraw.ass_new() + ass:rect(0, 0, display.width, display.height, { + color = '000000', opacity = options.curtain_opacity * self.opacity, + }) + return ass +end + +return Curtain diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/CycleButton.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/CycleButton.lua new file mode 100644 index 0000000..7f1c02f --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/CycleButton.lua @@ -0,0 +1,64 @@ +local Button = require('uosc_shared/elements/Button') + +---@alias CycleState {value: any; icon: string; active?: boolean} +---@alias CycleButtonProps {prop: string; states: CycleState[]; anchor_id?: string; tooltip?: string} + +---@class CycleButton : Button +local CycleButton = class(Button) + +---@param id string +---@param props CycleButtonProps +function CycleButton:new(id, props) return Class.new(self, id, props) --[[@as CycleButton]] end +---@param id string +---@param props CycleButtonProps +function CycleButton:init(id, props) + local is_state_prop = itable_index_of({'shuffle'}, props.prop) + self.prop = props.prop + self.states = props.states + + Button.init(self, id, props) + + self.icon = self.states[1].icon + self.active = self.states[1].active + self.current_state_index = 1 + self.on_click = function() + local new_state = self.states[self.current_state_index + 1] or self.states[1] + local new_value = new_state.value + if self.owner then + mp.commandv('script-message-to', self.owner, 'set', self.prop, new_value) + elseif is_state_prop then + if itable_index_of({'yes', 'no'}, new_value) then new_value = new_value == 'yes' end + set_state(self.prop, new_value) + else + mp.set_property(self.prop, new_value) + end + end + + self.handle_change = function(name, value) + if is_state_prop and type(value) == 'boolean' then value = value and 'yes' or 'no' end + local index = itable_find(self.states, function(state) return state.value == value end) + self.current_state_index = index or 1 + self.icon = self.states[self.current_state_index].icon + self.active = self.states[self.current_state_index].active + request_render() + end + + local prop_parts = split(self.prop, '@') + if #prop_parts == 2 then -- External prop with a script owner + self.prop, self.owner = prop_parts[1], prop_parts[2] + self['on_external_prop_' .. self.prop] = function(_, value) self.handle_change(self.prop, value) end + self.handle_change(self.prop, external[self.prop]) + elseif is_state_prop then -- uosc's state props + self['on_prop_' .. self.prop] = function(self, value) self.handle_change(self.prop, value) end + self.handle_change(self.prop, state[self.prop]) + else + mp.observe_property(self.prop, 'string', self.handle_change) + end +end + +function CycleButton:destroy() + Button.destroy(self) + mp.unobserve_property(self.handle_change) +end + +return CycleButton diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Element.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Element.lua new file mode 100644 index 0000000..1bcbe08 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Element.lua @@ -0,0 +1,154 @@ +---@alias ElementProps {enabled?: boolean; ax?: number; ay?: number; bx?: number; by?: number; ignores_menu?: boolean; anchor_id?: string;} + +-- Base class all elements inherit from. +---@class Element : Class +local Element = class() + +---@param id string +---@param props? ElementProps +function Element:init(id, props) + self.id = id + -- `false` means element won't be rendered, or receive events + self.enabled = true + -- Element coordinates + self.ax, self.ay, self.bx, self.by = 0, 0, 0, 0 + -- Relative proximity from `0` - mouse outside `proximity_max` range, to `1` - mouse within `proximity_min` range. + self.proximity = 0 + -- Raw proximity in pixels. + self.proximity_raw = INFINITY + ---@type number `0-1` factor to force min visibility. Used for toggling element's permanent visibility. + self.min_visibility = 0 + ---@type number `0-1` factor to force a visibility value. Used for flashing, fading out, and other animations + self.forced_visibility = nil + ---@type boolean Render this element even when menu is open. + self.ignores_menu = false + ---@type nil|string ID of an element from which this one should inherit visibility. + self.anchor_id = nil + + if props then table_assign(self, props) end + + -- Flash timer + self._flash_out_timer = mp.add_timeout(options.flash_duration / 1000, function() + local function getTo() return self.proximity end + local function onTweenEnd() self.forced_visibility = nil end + if self.enabled then self:tween_property('forced_visibility', 1, getTo, onTweenEnd) + else onTweenEnd() end + end) + self._flash_out_timer:kill() + + Elements:add(self) +end + +function Element:destroy() + self.destroyed = true + Elements:remove(self) +end + +function Element:reset_proximity() self.proximity, self.proximity_raw = 0, INFINITY end + +---@param ax number +---@param ay number +---@param bx number +---@param by number +function Element:set_coordinates(ax, ay, bx, by) + self.ax, self.ay, self.bx, self.by = ax, ay, bx, by + Elements:update_proximities() + self:maybe('on_coordinates') +end + +function Element:update_proximity() + if cursor.hidden then + self:reset_proximity() + else + local range = options.proximity_out - options.proximity_in + self.proximity_raw = get_point_to_rectangle_proximity(cursor, self) + self.proximity = 1 - (clamp(0, self.proximity_raw - options.proximity_in, range) / range) + end +end + +function Element:is_persistent() + local persist = config[self.id .. '_persistency'] + return persist and ( + (persist.audio and state.is_audio) + or (persist.paused and state.pause and (not Elements.timeline.pressed or Elements.timeline.pressed.pause)) + or (persist.video and state.is_video) + or (persist.image and state.is_image) + or (persist.idle and state.is_idle) + ) +end + +-- Decide elements visibility based on proximity and various other factors +function Element:get_visibility() + -- Hide when menu is open, unless this is a menu + ---@diagnostic disable-next-line: undefined-global + if not self.ignores_menu and Menu and Menu:is_open() then return 0 end + + -- Persistency + if self:is_persistent() then return 1 end + + -- Forced visibility + if self.forced_visibility then return math.max(self.forced_visibility, self.min_visibility) end + + -- Anchor inheritance + -- If anchor returns -1, it means all attached elements should force hide. + local anchor = self.anchor_id and Elements[self.anchor_id] + local anchor_visibility = anchor and anchor:get_visibility() or 0 + + return anchor_visibility == -1 and 0 or math.max(self.proximity, anchor_visibility, self.min_visibility) +end + +-- Call method if it exists +function Element:maybe(name, ...) + if self[name] then return self[name](self, ...) end +end + +-- Attach a tweening animation to this element +---@param from number +---@param to number|fun():number +---@param setter fun(value: number) +---@param factor_or_callback? number|fun() +---@param callback? fun() Called either on animation end, or when animation is killed. +function Element:tween(from, to, setter, factor_or_callback, callback) + self:tween_stop() + self._kill_tween = self.enabled and tween( + from, to, setter, factor_or_callback, + function() + self._kill_tween = nil + if callback then callback() end + end + ) +end + +function Element:is_tweening() return self and self._kill_tween end +function Element:tween_stop() self:maybe('_kill_tween') end + +-- Animate an element property between 2 values. +---@param prop string +---@param from number +---@param to number|fun():number +---@param factor_or_callback? number|fun() +---@param callback? fun() Called either on animation end, or when animation is killed. +function Element:tween_property(prop, from, to, factor_or_callback, callback) + self:tween(from, to, function(value) self[prop] = value end, factor_or_callback, callback) +end + +---@param name string +function Element:trigger(name, ...) + local result = self:maybe('on_' .. name, ...) + request_render() + return result +end + +-- Briefly flashes the element for `options.flash_duration` milliseconds. +-- Useful to visualize changes of volume and timeline when changed via hotkeys. +function Element:flash() + if self.enabled and options.flash_duration > 0 and (self.proximity < 1 or self._flash_out_timer:is_enabled()) then + self:tween_stop() + self.forced_visibility = 1 + request_render() + self._flash_out_timer:kill() + self._flash_out_timer:resume() + end +end + +return Element diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Elements.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Elements.lua new file mode 100644 index 0000000..489819a --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Elements.lua @@ -0,0 +1,125 @@ +local Elements = {itable = {}} + +---@param element Element +function Elements:add(element) + if not element.id then + msg.error('attempt to add element without "id" property') + return + end + + if self:has(element.id) then Elements:remove(element.id) end + + self.itable[#self.itable + 1] = element + self[element.id] = element + + request_render() +end + +function Elements:remove(idOrElement) + if not idOrElement then return end + local id = type(idOrElement) == 'table' and idOrElement.id or idOrElement + local element = Elements[id] + if element then + if not element.destroyed then element:destroy() end + element.enabled = false + self.itable = itable_remove(self.itable, self[id]) + self[id] = nil + request_render() + end +end + +function Elements:update_proximities() + local menu_only = Elements.menu ~= nil + local mouse_leave_elements = {} + local mouse_enter_elements = {} + + -- Calculates proximities and opacities for defined elements + for _, element in self:ipairs() do + if element.enabled then + local previous_proximity_raw = element.proximity_raw + + -- If menu is open, all other elements have to be disabled + if menu_only then + if element.ignores_menu then element:update_proximity() + else element:reset_proximity() end + else + element:update_proximity() + end + + if element.proximity_raw == 0 then + -- Mouse entered element area + if previous_proximity_raw ~= 0 then + mouse_enter_elements[#mouse_enter_elements + 1] = element + end + else + -- Mouse left element area + if previous_proximity_raw == 0 then + mouse_leave_elements[#mouse_leave_elements + 1] = element + end + end + end + end + + -- Trigger `mouse_leave` and `mouse_enter` events + for _, element in ipairs(mouse_leave_elements) do element:trigger('mouse_leave') end + for _, element in ipairs(mouse_enter_elements) do element:trigger('mouse_enter') end +end + +-- Toggles passed elements' min visibilities between 0 and 1. +---@param ids string[] IDs of elements to peek. +function Elements:toggle(ids) + local has_invisible = itable_find(ids, function(id) return Elements[id] and Elements[id]:get_visibility() ~= 1 end) + self:set_min_visibility(has_invisible and 1 or 0, ids) + -- Reset proximities when toggling off. Has to happen after `set_min_visibility`, + -- as that is using proximity as a tween starting point. + if not has_invisible then + for _, id in ipairs(ids) do + if Elements[id] then Elements[id]:reset_proximity() end + end + end +end + +-- Set (animate) elements' min visibilities to passed value. +---@param visibility number 0-1 floating point. +---@param ids string[] IDs of elements to peek. +function Elements:set_min_visibility(visibility, ids) + for _, id in ipairs(ids) do + local element = Elements[id] + if element then + local from = math.max(0, element:get_visibility()) + element:tween_property('min_visibility', from, visibility) + end + end +end + +-- Flash passed elements. +---@param ids string[] IDs of elements to peek. +function Elements:flash(ids) + local elements = itable_filter(self.itable, function(element) return itable_index_of(ids, element.id) ~= nil end) + for _, element in ipairs(elements) do element:flash() end +end + +---@param name string Event name. +function Elements:trigger(name, ...) + for _, element in self:ipairs() do element:trigger(name, ...) end +end + +-- Trigger two events, `name` and `global_name`, depending on element-cursor proximity. +-- Disabled elements don't receive these events. +---@param name string Event name. +function Elements:proximity_trigger(name, ...) + for i = #self.itable, 1, -1 do + local element = self.itable[i] + if element.enabled then + if element.proximity_raw == 0 then + if element:trigger(name, ...) == 'stop_propagation' then break end + end + if element:trigger('global_' .. name, ...) == 'stop_propagation' then break end + end + end +end + +function Elements:has(id) return self[id] ~= nil end +function Elements:ipairs() return ipairs(self.itable) end + +return Elements diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Menu.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Menu.lua new file mode 100644 index 0000000..1830647 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Menu.lua @@ -0,0 +1,854 @@ +local Element = require('uosc_shared/elements/Element') + +-- Menu data structure accepted by `Menu:open(menu)`. +---@alias MenuData {type?: string; title?: string; hint?: string; keep_open?: boolean; separator?: boolean; items?: MenuDataItem[]; selected_index?: integer;} +---@alias MenuDataItem MenuDataValue|MenuData +---@alias MenuDataValue {title?: string; hint?: string; icon?: string; value: any; bold?: boolean; italic?: boolean; muted?: boolean; active?: boolean; keep_open?: boolean; separator?: boolean;} +---@alias MenuOptions {mouse_nav?: boolean; on_open?: fun(); on_close?: fun(); on_back?: fun(); on_move_item?: fun(from_index: integer, to_index: integer, submenu_path: integer[]); on_delete_item?: fun(index: integer, submenu_path: integer[])} + +-- Internal data structure created from `Menu`. +---@alias MenuStack {id?: string; type?: string; title?: string; hint?: string; selected_index?: number; keep_open?: boolean; separator?: boolean; items: MenuStackItem[]; parent_menu?: MenuStack; submenu_path: integer[]; active?: boolean; width: number; height: number; top: number; scroll_y: number; scroll_height: number; title_width: number; hint_width: number; max_width: number; is_root?: boolean; fling?: Fling} +---@alias MenuStackItem MenuStackValue|MenuStack +---@alias MenuStackValue {title?: string; hint?: string; icon?: string; value: any; active?: boolean; bold?: boolean; italic?: boolean; muted?: boolean; keep_open?: boolean; separator?: boolean; title_width: number; hint_width: number} +---@alias Fling {y: number, distance: number, time: number, easing: fun(x: number), duration: number, update_cursor?: boolean} + +---@alias Modifiers {shift?: boolean, ctrl?: boolean, alt?: boolean} +---@alias MenuCallbackMeta {modifiers: Modifiers} +---@alias MenuCallback fun(value: any, meta: MenuCallbackMeta) + +---@class Menu : Element +local Menu = class(Element) + +---@param data MenuData +---@param callback MenuCallback +---@param opts? MenuOptions +function Menu:open(data, callback, opts) + local open_menu = self:is_open() + if open_menu then + open_menu.is_being_replaced = true + open_menu:close(true) + end + return Menu:new(data, callback, opts) +end + +---@param menu_type? string +---@return Menu|nil +function Menu:is_open(menu_type) + return Elements.menu and (not menu_type or Elements.menu.type == menu_type) and Elements.menu or nil +end + +---@param immediate? boolean Close immediately without fadeout animation. +---@param callback? fun() Called after the animation (if any) ends and element is removed and destroyed. +---@overload fun(callback: fun()) +function Menu:close(immediate, callback) + if type(immediate) ~= 'boolean' then callback = immediate end + + local menu = self == Menu and Elements.menu or self + + if menu and not menu.destroyed then + if menu.is_closing then + menu:tween_stop() + return + end + + local function close() + Elements:remove('menu') + menu.is_closing, menu.stack, menu.current, menu.all, menu.by_id = false, nil, nil, {}, {} + menu:disable_key_bindings() + Elements:update_proximities() + cursor.queue_autohide() + if callback then callback() end + request_render() + end + + menu.is_closing = true + + if immediate then close() + else menu:fadeout(close) end + end +end + +---@param data MenuData +---@param callback MenuCallback +---@param opts? MenuOptions +---@return Menu +function Menu:new(data, callback, opts) return Class.new(self, data, callback, opts) --[[@as Menu]] end +---@param data MenuData +---@param callback MenuCallback +---@param opts? MenuOptions +function Menu:init(data, callback, opts) + Element.init(self, 'menu', {ignores_menu = true}) + + -----@type fun() + self.callback = callback + self.opts = opts or {} + self.offset_x = 0 -- Used for submenu transition animation. + self.mouse_nav = self.opts.mouse_nav -- Stops pre-selecting items + ---@type Modifiers|nil + self.modifiers = nil + self.item_height = nil + self.item_spacing = 1 + self.item_padding = nil + self.font_size = nil + self.font_size_hint = nil + self.scroll_step = nil -- Item height + item spacing. + self.scroll_height = nil -- Items + spacings - container height. + self.opacity = 0 -- Used to fade in/out. + self.type = data.type + ---@type MenuStack Root MenuStack. + self.root = nil + ---@type MenuStack Current MenuStack. + self.current = nil + ---@type MenuStack[] All menus in a flat array. + self.all = nil + ---@type table Map of submenus by their ids, such as `'Tools > Aspect ratio'`. + self.by_id = {} + self.key_bindings = {} + self.is_being_replaced = false + self.is_closing, self.is_closed = false, false + ---@type {y: integer, time: number}[] + self.drag_data = nil + self.is_dragging = false + + self:update(data) + + if self.mouse_nav then + if self.current then self.current.selected_index = nil end + else + for _, menu in ipairs(self.all) do self:scroll_to_index(menu.selected_index, menu) end + end + + self:tween_property('opacity', 0, 1) + self:enable_key_bindings() + Elements.curtain:register('menu') + if self.opts.on_open then self.opts.on_open() end +end + +function Menu:destroy() + Element.destroy(self) + self:disable_key_bindings() + self.is_closed = true + if not self.is_being_replaced then Elements.curtain:unregister('menu') end + if self.opts.on_close then self.opts.on_close() end +end + +---@param data MenuData +function Menu:update(data) + self.type = data.type + + local new_root = {is_root = true, submenu_path = {}} + local new_all = {} + local new_by_id = {} + local menus_to_serialize = {{new_root, data}} + local old_current_id = self.current and self.current.id + + table_assign(new_root, data, {'type', 'title', 'hint', 'keep_open'}) + + local i = 0 + while i < #menus_to_serialize do + i = i + 1 + local menu, menu_data = menus_to_serialize[i][1], menus_to_serialize[i][2] + local parent_id = menu.parent_menu and not menu.parent_menu.is_root and menu.parent_menu.id + if not menu.is_root then + menu.id = (parent_id and parent_id .. ' > ' or '') .. (menu_data.title or i) + end + menu.icon = 'chevron_right' + + -- Update items + local first_active_index = nil + menu.items = {} + + for i, item_data in ipairs(menu_data.items or {}) do + if item_data.active and not first_active_index then first_active_index = i end + + local item = {} + table_assign(item, item_data, { + 'title', 'icon', 'hint', 'active', 'bold', 'italic', 'muted', 'value', 'keep_open', 'separator', + }) + if item.keep_open == nil then item.keep_open = menu.keep_open end + + -- Submenu + if item_data.items then + item.parent_menu = menu + item.submenu_path = itable_join(menu.submenu_path, {i}) + menus_to_serialize[#menus_to_serialize + 1] = {item, item_data} + end + + menu.items[i] = item + end + + if menu.is_root then menu.selected_index = menu_data.selected_index or first_active_index end + + -- Retain old state + local old_menu = self.by_id[menu.is_root and '__root__' or menu.id] + if old_menu then table_assign(menu, old_menu, {'selected_index', 'scroll_y', 'fling'}) end + + new_all[#new_all + 1] = menu + new_by_id[menu.is_root and '__root__' or menu.id] = menu + end + + self.root, self.all, self.by_id = new_root, new_all, new_by_id + self.current = self.by_id[old_current_id] or self.root + + self:update_content_dimensions() + self:reset_navigation() +end + +---@param items MenuDataItem[] +function Menu:update_items(items) + local data = table_shallow_copy(self.root) + data.items = items + self:update(data) +end + +function Menu:update_content_dimensions() + self.item_height = state.fullormaxed and options.menu_item_height_fullscreen or options.menu_item_height + self.font_size = round(self.item_height * 0.48 * options.font_scale) + self.font_size_hint = self.font_size - 1 + self.item_padding = round((self.item_height - self.font_size) * 0.6) + self.scroll_step = self.item_height + self.item_spacing + + local title_opts = {size = self.font_size, italic = false, bold = false} + local hint_opts = {size = self.font_size_hint} + + for _, menu in ipairs(self.all) do + title_opts.bold, title_opts.italic = true, false + local max_width = text_width(menu.title, title_opts) + 2 * self.item_padding + + -- Estimate width of a widest item + for _, item in ipairs(menu.items) do + local icon_width = item.icon and self.font_size or 0 + item.title_width = text_width(item.title, title_opts) + item.hint_width = text_width(item.hint, hint_opts) + local spacings_in_item = 1 + (item.title_width > 0 and 1 or 0) + + (item.hint_width > 0 and 1 or 0) + (icon_width > 0 and 1 or 0) + local estimated_width = item.title_width + item.hint_width + icon_width + + (self.item_padding * spacings_in_item) + if estimated_width > max_width then max_width = estimated_width end + end + + menu.max_width = max_width + end + + self:update_dimensions() +end + +function Menu:update_dimensions() + -- Coordinates and sizes are of the scrollable area to make + -- consuming values in rendering and collisions easier. Title is rendered + -- above it, so we need to account for that in max_height and ay position. + local min_width = state.fullormaxed and options.menu_min_width_fullscreen or options.menu_min_width + + for _, menu in ipairs(self.all) do + menu.width = round(clamp(min_width, menu.max_width, display.width * 0.9)) + local title_height = (menu.is_root and menu.title) and self.scroll_step or 0 + local max_height = round((display.height - title_height) * 0.9) + local content_height = self.scroll_step * #menu.items + menu.height = math.min(content_height - self.item_spacing, max_height) + menu.top = round(math.max((display.height - menu.height) / 2, title_height * 1.5)) + menu.scroll_height = math.max(content_height - menu.height - self.item_spacing, 0) + menu.scroll_y = menu.scroll_y or 0 + self:scroll_to(menu.scroll_y, menu) -- clamps scroll_y to scroll limits + end + + self:update_coordinates() +end + +-- Updates element coordinates to match currently open (sub)menu. +function Menu:update_coordinates() + local ax = round((display.width - self.current.width) / 2) + self.offset_x + self:set_coordinates(ax, self.current.top, ax + self.current.width, self.current.top + self.current.height) +end + +function Menu:reset_navigation() + local menu = self.current + + -- Reset indexes and scroll + self:scroll_to(menu.scroll_y) -- clamps scroll_y to scroll limits + if self.mouse_nav then + self:select_item_below_cursor() + else + self:select_index((menu.items and #menu.items > 0) and clamp(1, menu.selected_index or 1, #menu.items) or nil) + end + + -- Walk up the parent menu chain and activate items that lead to current menu + local parent = menu.parent_menu + while parent do + parent.selected_index = itable_index_of(parent.items, menu) + menu, parent = parent, parent.parent_menu + end + + request_render() +end + +function Menu:set_offset_x(offset) + local delta = offset - self.offset_x + self.offset_x = offset + self:set_coordinates(self.ax + delta, self.ay, self.bx + delta, self.by) +end + +function Menu:fadeout(callback) self:tween_property('opacity', 1, 0, callback) end + +function Menu:get_item_index_below_cursor() + local menu = self.current + if #menu.items < 1 or self.proximity_raw > 0 then return nil end + return math.max(1, math.min(math.ceil((cursor.y - self.ay + menu.scroll_y) / self.scroll_step), #menu.items)) +end + +function Menu:get_first_active_index(menu) + menu = menu or self.current + for index, item in ipairs(self.current.items) do + if item.active then return index end + end +end + +---@param pos? number +---@param menu? MenuStack +function Menu:set_scroll_to(pos, menu) + menu = menu or self.current + menu.scroll_y = clamp(0, pos or 0, menu.scroll_height) + request_render() +end + +---@param delta? number +---@param menu? MenuStack +function Menu:set_scroll_by(delta, menu) + menu = menu or self.current + self:set_scroll_to(menu.scroll_y + delta, menu) +end + +---@param pos? number +---@param menu? MenuStack +---@param fling_options? table +function Menu:scroll_to(pos, menu, fling_options) + menu = menu or self.current + menu.fling = { + y = menu.scroll_y, distance = clamp(-menu.scroll_y, pos - menu.scroll_y, menu.scroll_height - menu.scroll_y), + time = mp.get_time(), duration = 0.1, easing = ease_out_sext, + } + if fling_options then table_assign(menu.fling, fling_options) end + request_render() +end + +---@param delta? number +---@param menu? MenuStack +---@param fling_options? Fling +function Menu:scroll_by(delta, menu, fling_options) + menu = menu or self.current + self:scroll_to((menu.fling and (menu.fling.y + menu.fling.distance) or menu.scroll_y) + delta, menu, fling_options) +end + +---@param index? integer +---@param menu? MenuStack +---@param immediate? boolean +function Menu:scroll_to_index(index, menu, immediate) + menu = menu or self.current + if (index and index >= 1 and index <= #menu.items) then + local position = round((self.scroll_step * (index - 1)) - ((menu.height - self.scroll_step) / 2)) + if immediate then self:set_scroll_to(position, menu) + else self:scroll_to(position, menu) end + end +end + +---@param index? integer +---@param menu? MenuStack +function Menu:select_index(index, menu) + menu = menu or self.current + menu.selected_index = (index and index >= 1 and index <= #menu.items) and index or nil + request_render() +end + +---@param value? any +---@param menu? MenuStack +function Menu:select_value(value, menu) + menu = menu or self.current + local index = itable_find(menu.items, function(item) return item.value == value end) + self:select_index(index) +end + +---@param menu? MenuStack +function Menu:deactivate_items(menu) + menu = menu or self.current + for _, item in ipairs(menu.items) do item.active = false end + request_render() +end + +---@param index? integer +---@param menu? MenuStack +function Menu:activate_index(index, menu) + menu = menu or self.current + if index and index >= 1 and index <= #menu.items then menu.items[index].active = true end + request_render() +end + +---@param index? integer +---@param menu? MenuStack +function Menu:activate_one_index(index, menu) + self:deactivate_items(menu) + self:activate_index(index, menu) +end + +---@param value? any +---@param menu? MenuStack +function Menu:activate_value(value, menu) + menu = menu or self.current + local index = itable_find(menu.items, function(item) return item.value == value end) + self:activate_index(index, menu) +end + +---@param value? any +---@param menu? MenuStack +function Menu:activate_one_value(value, menu) + menu = menu or self.current + local index = itable_find(menu.items, function(item) return item.value == value end) + self:activate_one_index(index, menu) +end + +---@param menu MenuStack One of menus in `self.all`. +function Menu:activate_menu(menu) + if itable_index_of(self.all, menu) then + self.current = menu + self:update_coordinates() + self:reset_navigation() + request_render() + else + msg.error('Attempt to open a menu not in `self.all` list.') + end +end + +---@param id string +function Menu:activate_submenu(id) + local submenu = self.by_id[id] + if submenu then self:activate_menu(submenu) + else msg.error(string.format('Requested submenu id "%s" doesn\'t exist', id)) end +end + +---@param index? integer +---@param menu? MenuStack +function Menu:delete_index(index, menu) + menu = menu or self.current + if (index and index >= 1 and index <= #menu.items) then + table.remove(menu.items, index) + self:update_content_dimensions() + self:scroll_to_index(menu.selected_index, menu) + end +end + +---@param value? any +---@param menu? MenuStack +function Menu:delete_value(value, menu) + menu = menu or self.current + local index = itable_find(menu.items, function(item) return item.value == value end) + self:delete_index(index) +end + +---@param menu? MenuStack +function Menu:prev(menu) + menu = menu or self.current + menu.selected_index = math.max(menu.selected_index and menu.selected_index - 1 or #menu.items, 1) + self:scroll_to_index(menu.selected_index, menu, true) +end + +---@param menu? MenuStack +function Menu:next(menu) + menu = menu or self.current + menu.selected_index = math.min(menu.selected_index and menu.selected_index + 1 or 1, #menu.items) + self:scroll_to_index(menu.selected_index, menu, true) +end + +function Menu:back() + if self.opts.on_back then + self.opts.on_back() + if self.is_closed then return end + end + + local menu = self.current + local parent = menu.parent_menu + + if parent then + menu.selected_index = nil + self:activate_menu(parent) + self:tween(self.offset_x - menu.width / 2, 0, function(offset) self:set_offset_x(offset) end) + self.opacity = 1 -- in case tween above canceled fade in animation + else + self:close() + end +end + +---@param opts? {keep_open?: boolean, preselect_submenu_item?: boolean} +function Menu:open_selected_item(opts) + opts = opts or {} + local menu = self.current + if menu.selected_index then + local item = menu.items[menu.selected_index] + -- Is submenu + if item.items then + if opts.preselect_submenu_item then + item.selected_index = #item.items > 0 and 1 or nil + end + self:activate_menu(item) + self:tween(self.offset_x + menu.width / 2, 0, function(offset) self:set_offset_x(offset) end) + self.opacity = 1 -- in case tween above canceled fade in animation + else + self.callback(item.value, {modifiers = self.modifiers or {}}) + if not item.keep_open and not opts.keep_open then self:close() end + end + end +end + +function Menu:open_selected_item_soft() self:open_selected_item({keep_open = true}) end +function Menu:open_selected_item_preselect() self:open_selected_item({preselect_submenu_item = true}) end +function Menu:select_item_below_cursor() self.current.selected_index = self:get_item_index_below_cursor() end + +---@param index integer +function Menu:move_selected_item_to(index) + local from, callback = self.current.selected_index, self.opts.on_move_item + if callback and from and from ~= index and index >= 1 and index <= #self.current.items then + callback(from, index, self.current.submenu_path) + self.current.selected_index = index + request_render() + end +end + +function Menu:move_selected_item_up() + if self.current.selected_index then self:move_selected_item_to(self.current.selected_index - 1) end +end + +function Menu:move_selected_item_down() + if self.current.selected_index then self:move_selected_item_to(self.current.selected_index + 1) end +end + +function Menu:delete_selected_item() + local index, callback = self.current.selected_index, self.opts.on_delete_item + if callback and index then callback(index, self.current.submenu_path) end +end + +function Menu:on_display() self:update_dimensions() end +function Menu:on_prop_fullormaxed() self:update_content_dimensions() end + +function Menu:handle_cursor_down() + if self.proximity_raw == 0 then + self.drag_data = {{y = cursor.y, time = mp.get_time()}} + self.current.fling = nil + else + if cursor.x < self.ax and self.current.parent_menu then self:back() + else self:close() end + end +end + +function Menu:fling_distance() + local first, last = self.drag_data[1], self.drag_data[#self.drag_data] + if mp.get_time() - last.time > 0.05 then return 0 end + for i = #self.drag_data - 1, 1, -1 do + local drag = self.drag_data[i] + if last.time - drag.time > 0.03 then return ((drag.y - last.y) / ((last.time - drag.time) / 0.03)) * 10 end + end + return #self.drag_data < 2 and 0 or ((first.y - last.y) / ((first.time - last.time) / 0.03)) * 10 +end + +function Menu:handle_cursor_up() + if self.proximity_raw == 0 and self.drag_data and not self.is_dragging then + self:select_item_below_cursor() + self:open_selected_item({preselect_submenu_item = false, keep_open = self.modifiers and self.modifiers.shift}) + end + if self.is_dragging then + local distance = self:fling_distance() + if math.abs(distance) > 50 then + self.current.fling = { + y = self.current.scroll_y, distance = distance, time = self.drag_data[#self.drag_data].time, + easing = ease_out_quart, duration = 0.5, update_cursor = true, + } + end + end + self.is_dragging = false + self.drag_data = nil +end + + +function Menu:on_global_mouse_move() + self.mouse_nav = true + if self.drag_data then + self.is_dragging = self.is_dragging or math.abs(cursor.y - self.drag_data[1].y) >= 10 + local distance = self.drag_data[#self.drag_data].y - cursor.y + if distance ~= 0 then self:set_scroll_by(distance) end + self.drag_data[#self.drag_data + 1] = {y = cursor.y, time = mp.get_time()} + end + if self.proximity_raw == 0 or self.is_dragging then self:select_item_below_cursor() + else self.current.selected_index = nil end + request_render() +end + +function Menu:handle_wheel_up() self:scroll_by(self.scroll_step * -3, nil, {update_cursor = true}) end +function Menu:handle_wheel_down() self:scroll_by(self.scroll_step * 3, nil, {update_cursor = true}) end + +function Menu:on_pgup() + local menu = self.current + local items_per_page = round((menu.height / self.scroll_step) * 0.4) + local paged_index = (menu.selected_index and menu.selected_index or #menu.items) - items_per_page + menu.selected_index = clamp(1, paged_index, #menu.items) + if menu.selected_index > 0 then self:scroll_to_index(menu.selected_index) end +end + +function Menu:on_pgdwn() + local menu = self.current + local items_per_page = round((menu.height / self.scroll_step) * 0.4) + local paged_index = (menu.selected_index and menu.selected_index or 1) + items_per_page + menu.selected_index = clamp(1, paged_index, #menu.items) + if menu.selected_index > 0 then self:scroll_to_index(menu.selected_index) end +end + +function Menu:on_home() + self.current.selected_index = math.min(1, #self.current.items) + if self.current.selected_index > 0 then self:scroll_to_index(self.current.selected_index) end +end + +function Menu:on_end() + self.current.selected_index = #self.current.items + if self.current.selected_index > 0 then self:scroll_to_index(self.current.selected_index) end +end + +function Menu:add_key_binding(key, name, fn, flags) + self.key_bindings[#self.key_bindings + 1] = name + mp.add_forced_key_binding(key, name, fn, flags) +end + +function Menu:enable_key_bindings() + -- The `mp.set_key_bindings()` method would be easier here, but that + -- doesn't support 'repeatable' flag, so we are stuck with this monster. + self:add_key_binding('up', 'menu-prev1', self:create_key_action('prev'), 'repeatable') + self:add_key_binding('down', 'menu-next1', self:create_key_action('next'), 'repeatable') + self:add_key_binding('ctrl+up', 'menu-move-up', self:create_key_action('move_selected_item_up'), 'repeatable') + self:add_key_binding('ctrl+down', 'menu-move-down', self:create_key_action('move_selected_item_down'), 'repeatable') + self:add_key_binding('left', 'menu-back1', self:create_key_action('back')) + self:add_key_binding('right', 'menu-select1', self:create_key_action('open_selected_item_preselect')) + self:add_key_binding('shift+right', 'menu-select-soft1', + self:create_key_action('open_selected_item_soft', {shift = true})) + self:add_key_binding('shift+mbtn_left', 'menu-select3', self:create_modified_mbtn_left_handler({shift = true})) + self:add_key_binding('ctrl+mbtn_left', 'menu-select4', self:create_modified_mbtn_left_handler({ctrl = true})) + self:add_key_binding('mbtn_back', 'menu-back-alt3', self:create_key_action('back')) + self:add_key_binding('bs', 'menu-back-alt4', self:create_key_action('back')) + self:add_key_binding('enter', 'menu-select-alt3', self:create_key_action('open_selected_item_preselect')) + self:add_key_binding('kp_enter', 'menu-select-alt4', self:create_key_action('open_selected_item_preselect')) + self:add_key_binding('ctrl+enter', 'menu-select-ctrl1', + self:create_key_action('open_selected_item_preselect', {ctrl = true})) + self:add_key_binding('ctrl+kp_enter', 'menu-select-ctrl2', + self:create_key_action('open_selected_item_preselect', {ctrl = true})) + self:add_key_binding('shift+enter', 'menu-select-alt5', + self:create_key_action('open_selected_item_soft', {shift = true})) + self:add_key_binding('shift+kp_enter', 'menu-select-alt6', + self:create_key_action('open_selected_item_soft', {shift = true})) + self:add_key_binding('esc', 'menu-close', self:create_key_action('close')) + self:add_key_binding('pgup', 'menu-page-up', self:create_key_action('on_pgup'), 'repeatable') + self:add_key_binding('pgdwn', 'menu-page-down', self:create_key_action('on_pgdwn'), 'repeatable') + self:add_key_binding('home', 'menu-home', self:create_key_action('on_home')) + self:add_key_binding('end', 'menu-end', self:create_key_action('on_end')) + self:add_key_binding('del', 'menu-delete-item', self:create_key_action('delete_selected_item')) +end + +function Menu:disable_key_bindings() + for _, name in ipairs(self.key_bindings) do mp.remove_key_binding(name) end + self.key_bindings = {} +end + +---@param modifiers Modifiers +function Menu:create_modified_mbtn_left_handler(modifiers) + return function() + self.mouse_nav = true + self.modifiers = modifiers + self:handle_cursor_down() + self:handle_cursor_up() + self.modifiers = nil + end +end + +---@param name string +---@param modifiers? Modifiers +function Menu:create_key_action(name, modifiers) + return function() + self.mouse_nav = false + self.modifiers = modifiers + self:maybe(name) + self.modifiers = nil + end +end + +function Menu:render() + local update_cursor = false + for _, menu in ipairs(self.all) do + if menu.fling then + update_cursor = update_cursor or menu.fling.update_cursor or false + local time_delta = state.render_last_time - menu.fling.time + local progress = menu.fling.easing(math.min(time_delta / menu.fling.duration, 1)) + self:set_scroll_to(round(menu.fling.y + menu.fling.distance * progress), menu) + if progress < 1 then request_render() else menu.fling = nil end + end + end + if update_cursor then self:select_item_below_cursor() end + + cursor.on_primary_down = function() self:handle_cursor_down() end + cursor.on_primary_up = function() self:handle_cursor_up() end + if self.proximity_raw == 0 then + cursor.on_wheel_down = function() self:handle_wheel_down() end + cursor.on_wheel_up = function() self:handle_wheel_up() end + end + + local ass = assdraw.ass_new() + local opacity = options.menu_opacity * self.opacity + local spacing = self.item_padding + local icon_size = self.font_size + + function draw_menu(menu, x, y, opacity) + local ax, ay, bx, by = x, y, x + menu.width, y + menu.height + local draw_title = menu.is_root and menu.title + local scroll_clip = '\\clip(0,' .. ay .. ',' .. display.width .. ',' .. by .. ')' + local start_index = math.floor(menu.scroll_y / self.scroll_step) + 1 + local end_index = math.ceil((menu.scroll_y + menu.height) / self.scroll_step) + local selected_index = menu.selected_index or -1 + -- remove menu_opacity to start off with full opacity, but still decay for parent menus + local text_opacity = opacity / options.menu_opacity + + -- Background + ass:rect(ax, ay - (draw_title and self.item_height or 0) - 2, bx, by + 2, { + color = bg, opacity = opacity, radius = 4, + }) + + for index = start_index, end_index, 1 do + local item = menu.items[index] + local next_item = menu.items[index + 1] + local is_highlighted = selected_index == index or item.active + local next_is_active = next_item and next_item.active + local next_is_highlighted = selected_index == index + 1 or next_is_active + + if not item then break end + + local item_ay = ay - menu.scroll_y + self.scroll_step * (index - 1) + local item_by = item_ay + self.item_height + local item_center_y = item_ay + (self.item_height / 2) + local item_clip = (item_ay < ay or item_by > by) and scroll_clip or nil + local content_ax, content_bx = ax + spacing, bx - spacing + local font_color = item.active and fgt or bgt + local shadow_color = item.active and fg or bg + + -- Separator + local separator_ay = item.separator and item_by - 1 or item_by + local separator_by = item_by + (item.separator and 2 or 1) + if is_highlighted then separator_ay = item_by + 1 end + if next_is_highlighted then separator_by = item_by end + if separator_by - separator_ay > 0 and item_by < by then + ass:rect(ax + spacing / 2, separator_ay, bx - spacing / 2, separator_by, { + color = fg, opacity = opacity * (item.separator and 0.08 or 0.06), + }) + end + + -- Highlight + local highlight_opacity = 0 + (item.active and 0.8 or 0) + (selected_index == index and 0.15 or 0) + if highlight_opacity > 0 then + ass:rect(ax + 2, item_ay, bx - 2, item_by, { + radius = 2, color = fg, opacity = highlight_opacity * text_opacity, + clip = item_clip, + }) + end + + -- Icon + if item.icon then + local x, y = content_bx - (icon_size / 2), item_center_y + if item.icon == 'spinner' then + ass:spinner(x, y, icon_size * 1.5, {color = font_color, opacity = text_opacity * 0.8}) + else + ass:icon(x, y, icon_size * 1.5, item.icon, { + color = font_color, opacity = text_opacity, clip = item_clip, + shadow = 1, shadow_color = shadow_color, + }) + end + content_bx = content_bx - icon_size - spacing + end + + local title_cut_x = content_bx + if item.hint_width > 0 then + -- controls title & hint clipping proportional to the ratio of their widths + local title_content_ratio = item.title_width / (item.title_width + item.hint_width) + title_cut_x = round(content_ax + (content_bx - content_ax - spacing) * title_content_ratio + + (item.title_width > 0 and spacing / 2 or 0)) + end + + -- Hint + if item.hint then + item.ass_safe_hint = item.ass_safe_hint or ass_escape(item.hint) + local clip = '\\clip(' .. title_cut_x .. ',' .. + math.max(item_ay, ay) .. ',' .. bx .. ',' .. math.min(item_by, by) .. ')' + ass:txt(content_bx, item_center_y, 6, item.ass_safe_hint, { + size = self.font_size_hint, color = font_color, wrap = 2, opacity = 0.5 * opacity, clip = clip, + shadow = 1, shadow_color = shadow_color, + }) + end + + -- Title + if item.title then + item.ass_safe_title = item.ass_safe_title or ass_escape(item.title) + local clip = '\\clip(' .. ax .. ',' .. math.max(item_ay, ay) .. ',' + .. title_cut_x .. ',' .. math.min(item_by, by) .. ')' + ass:txt(content_ax, item_center_y, 4, item.ass_safe_title, { + size = self.font_size, color = font_color, italic = item.italic, bold = item.bold, wrap = 2, + opacity = text_opacity * (item.muted and 0.5 or 1), clip = clip, + shadow = 1, shadow_color = shadow_color, + }) + end + end + + -- Menu title + if draw_title then + local title_ay = ay - self.item_height + local title_height = self.item_height - 3 + menu.ass_safe_title = menu.ass_safe_title or ass_escape(menu.title) + + -- Background + ass:rect(ax + 2, title_ay, bx - 2, title_ay + title_height, { + color = fg, opacity = opacity * 0.8, radius = 2, + }) + ass:texture(ax + 2, title_ay, bx - 2, title_ay + title_height, 'n', { + size = 80, color = bg, opacity = opacity * 0.1, + }) + + -- Title + ass:txt(ax + menu.width / 2, title_ay + (title_height / 2), 5, menu.ass_safe_title, { + size = self.font_size, bold = true, color = bg, wrap = 2, opacity = opacity, + clip = '\\clip(' .. ax .. ',' .. title_ay .. ',' .. bx .. ',' .. ay .. ')', + }) + end + + -- Scrollbar + if menu.scroll_height > 0 then + local groove_height = menu.height - 2 + local thumb_height = math.max((menu.height / (menu.scroll_height + menu.height)) * groove_height, 40) + local thumb_y = ay + 1 + ((menu.scroll_y / menu.scroll_height) * (groove_height - thumb_height)) + ass:rect(bx - 3, thumb_y, bx - 1, thumb_y + thumb_height, {color = fg, opacity = opacity * 0.8}) + end + end + + -- Main menu + draw_menu(self.current, self.ax, self.ay, opacity) + + -- Parent menus + local parent_menu = self.current.parent_menu + local parent_offset_x = self.ax + local parent_opacity_factor = options.menu_parent_opacity + local menu_gap = 2 + + while parent_menu do + parent_offset_x = parent_offset_x - parent_menu.width - menu_gap + draw_menu(parent_menu, parent_offset_x, parent_menu.top, parent_opacity_factor * opacity) + parent_opacity_factor = parent_opacity_factor * parent_opacity_factor + parent_menu = parent_menu.parent_menu + end + + -- Selected menu + local selected_menu = self.current.items[self.current.selected_index] + + if selected_menu and selected_menu.items then + draw_menu(selected_menu, self.bx + menu_gap, selected_menu.top, options.menu_parent_opacity * opacity) + end + + return ass +end + +return Menu diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/PauseIndicator.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/PauseIndicator.lua new file mode 100644 index 0000000..82a7e43 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/PauseIndicator.lua @@ -0,0 +1,80 @@ +local Element = require('uosc_shared/elements/Element') + +---@class PauseIndicator : Element +local PauseIndicator = class(Element) + +function PauseIndicator:new() return Class.new(self) --[[@as PauseIndicator]] end +function PauseIndicator:init() + Element.init(self, 'pause_indicator') + self.ignores_menu = true + self.base_icon_opacity = options.pause_indicator == 'flash' and 1 or 0.8 + self.paused = state.pause + self.type = options.pause_indicator + self.is_manual = options.pause_indicator == 'manual' + self.fadeout_requested = false + self.opacity = 0 + + mp.observe_property('pause', 'bool', function(_, paused) + if Elements.timeline.pressed then return end + if options.pause_indicator == 'flash' then + if self.paused == paused then return end + self:flash() + elseif options.pause_indicator == 'static' then + self:decide() + end + end) +end + +function PauseIndicator:flash() + if not self.is_manual and self.type ~= 'flash' then return end + -- can't wait for pause property event listener to set this, because when this is used inside a binding like: + -- cycle pause; script-binding uosc/flash-pause-indicator + -- the pause event is not fired fast enough, and indicator starts rendering with old icon + self.paused = mp.get_property_native('pause') + if self.is_manual then self.type = 'flash' end + self.opacity = 1 + self:tween_property('opacity', 1, 0, 0.15) +end + +-- decides whether static indicator should be visible or not +function PauseIndicator:decide() + if not self.is_manual and self.type ~= 'static' then return end + self.paused = mp.get_property_native('pause') -- see flash() for why this line is necessary + if self.is_manual then self.type = 'static' end + self.opacity = self.paused and 1 or 0 + request_render() + + -- Workaround for an mpv race condition bug during pause on windows builds, which causes osd updates to be ignored. + -- .03 was still loosing renders, .04 was fine, but to be safe I added 10ms more + mp.add_timeout(.05, function() osd:update() end) +end + +function PauseIndicator:render() + if self.opacity == 0 then return end + + local ass = assdraw.ass_new() + local is_static = self.type == 'static' + + -- Background fadeout + if is_static then + ass:rect(0, 0, display.width, display.height, {color = bg, opacity = self.opacity * 0.3}) + end + + -- Icon + local size = round(math.min(display.width, display.height) * (is_static and 0.20 or 0.15)) + size = size + size * (1 - self.opacity) + + if self.paused then + ass:icon(display.width / 2, display.height / 2, size, 'pause', + {border = 1, opacity = self.base_icon_opacity * self.opacity} + ) + else + ass:icon(display.width / 2, display.height / 2, size * 1.2, 'play_arrow', + {border = 1, opacity = self.base_icon_opacity * self.opacity} + ) + end + + return ass +end + +return PauseIndicator diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Speed.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Speed.lua new file mode 100644 index 0000000..6ea5097 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Speed.lua @@ -0,0 +1,192 @@ +local Element = require('uosc_shared/elements/Element') + +---@alias Dragging { start_time: number; start_x: number; distance: number; speed_distance: number; start_speed: number; } + +---@class Speed : Element +local Speed = class(Element) + +---@param props? ElementProps +function Speed:new(props) return Class.new(self, props) --[[@as Speed]] end +function Speed:init(props) + Element.init(self, 'speed', props) + + self.width = 0 + self.height = 0 + self.notches = 10 + self.notch_every = 0.1 + ---@type number + self.notch_spacing = nil + ---@type number + self.font_size = nil + ---@type Dragging|nil + self.dragging = nil +end + +function Speed:on_coordinates() + self.height, self.width = self.by - self.ay, self.bx - self.ax + self.notch_spacing = self.width / (self.notches + 1) + self.font_size = round(self.height * 0.48 * options.font_scale) +end + +function Speed:speed_step(speed, up) + if options.speed_step_is_factor then + if up then + return speed * options.speed_step + else + return speed * 1 / options.speed_step + end + else + if up then + return speed + options.speed_step + else + return speed - options.speed_step + end + end +end + +function Speed:handle_cursor_down() + self:tween_stop() -- Stop and cleanup possible ongoing animations + self.dragging = { + start_time = mp.get_time(), + start_x = cursor.x, + distance = 0, + speed_distance = 0, + start_speed = state.speed, + } +end + +function Speed:on_global_mouse_move() + if not self.dragging then return end + + self.dragging.distance = cursor.x - self.dragging.start_x + self.dragging.speed_distance = (-self.dragging.distance / self.notch_spacing * self.notch_every) + + local speed_current = state.speed + local speed_drag_current = self.dragging.start_speed + self.dragging.speed_distance + speed_drag_current = clamp(0.01, speed_drag_current, 100) + local drag_dir_up = speed_drag_current > speed_current + + local speed_step_next = speed_current + local speed_drag_diff = math.abs(speed_drag_current - speed_current) + while math.abs(speed_step_next - speed_current) < speed_drag_diff do + speed_step_next = self:speed_step(speed_step_next, drag_dir_up) + end + local speed_step_prev = self:speed_step(speed_step_next, not drag_dir_up) + + local speed_new = speed_step_prev + local speed_next_diff = math.abs(speed_drag_current - speed_step_next) + local speed_prev_diff = math.abs(speed_drag_current - speed_step_prev) + if speed_next_diff < speed_prev_diff then + speed_new = speed_step_next + end + + if speed_new ~= speed_current then + mp.set_property_native('speed', speed_new) + end +end + +function Speed:handle_cursor_up() + if self.proximity_raw == 0 then + -- Reset speed on short clicks + if self.dragging and math.abs(self.dragging.distance) < 6 and mp.get_time() - self.dragging.start_time < 0.15 then + mp.set_property_native('speed', 1) + end + end + self.dragging = nil + request_render() +end + +function Speed:on_global_mouse_leave() + self.dragging = nil + request_render() +end + +function Speed:handle_wheel_up() mp.set_property_native('speed', self:speed_step(state.speed, true)) end +function Speed:handle_wheel_down() mp.set_property_native('speed', self:speed_step(state.speed, false)) end + +function Speed:render() + local visibility = self:get_visibility() + local opacity = self.dragging and 1 or visibility + + if opacity <= 0 then return end + + if self.proximity_raw == 0 then + cursor.on_primary_down = function() + self:handle_cursor_down() + cursor.on_primary_up = function() self:handle_cursor_up() end + end + cursor.on_wheel_down = function() self:handle_wheel_down() end + cursor.on_wheel_up = function() self:handle_wheel_up() end + end + if self.dragging then + cursor.on_primary_up = function() self:handle_cursor_up() end + end + + local ass = assdraw.ass_new() + + -- Background + ass:rect(self.ax, self.ay, self.bx, self.by, {color = bg, radius = 2, opacity = opacity * options.speed_opacity}) + + -- Coordinates + local ax, ay = self.ax, self.ay + local bx, by = self.bx, ay + self.height + local half_width = (self.width / 2) + local half_x = ax + half_width + + -- Notches + local speed_at_center = state.speed + if self.dragging then + speed_at_center = self.dragging.start_speed + self.dragging.speed_distance + speed_at_center = clamp(0.01, speed_at_center, 100) + end + local nearest_notch_speed = round(speed_at_center / self.notch_every) * self.notch_every + local nearest_notch_x = half_x + (((nearest_notch_speed - speed_at_center) / self.notch_every) * self.notch_spacing) + local guide_size = math.floor(self.height / 7.5) + local notch_by = by - guide_size + local notch_ay_big = ay + round(self.font_size * 1.1) + local notch_ay_medium = notch_ay_big + ((notch_by - notch_ay_big) * 0.2) + local notch_ay_small = notch_ay_big + ((notch_by - notch_ay_big) * 0.4) + local from_to_index = math.floor(self.notches / 2) + + for i = -from_to_index, from_to_index do + local notch_speed = nearest_notch_speed + (i * self.notch_every) + + if notch_speed >= 0 and notch_speed <= 100 then + local notch_x = nearest_notch_x + (i * self.notch_spacing) + local notch_thickness = 1 + local notch_ay = notch_ay_small + if (notch_speed % (self.notch_every * 10)) < 0.00000001 then + notch_ay = notch_ay_big + notch_thickness = 1.5 + elseif (notch_speed % (self.notch_every * 5)) < 0.00000001 then + notch_ay = notch_ay_medium + end + + ass:rect(notch_x - notch_thickness, notch_ay, notch_x + notch_thickness, notch_by, { + color = fg, border = 1, border_color = bg, + opacity = math.min(1.2 - (math.abs((notch_x - ax - half_width) / half_width)), 1) * opacity, + }) + end + end + + -- Center guide + ass:new_event() + ass:append('{\\rDefault\\an7\\blur0\\bord1\\shad0\\1c&H' .. fg .. '\\3c&H' .. bg .. '}') + ass:opacity(opacity) + ass:pos(0, 0) + ass:draw_start() + ass:move_to(half_x, by - 2 - guide_size) + ass:line_to(half_x + guide_size, by - 2) + ass:line_to(half_x - guide_size, by - 2) + ass:draw_stop() + + -- Speed value + local speed_text = (round(state.speed * 100) / 100) .. 'x' + ass:txt(half_x, ay + (notch_ay_big - ay) / 2, 5, speed_text, { + size = self.font_size, color = bgt, border = options.text_border, border_color = bg, opacity = opacity, + }) + + return ass +end + +return Speed diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Timeline.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Timeline.lua new file mode 100644 index 0000000..8dfda9f --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Timeline.lua @@ -0,0 +1,430 @@ +local Element = require('uosc_shared/elements/Element') + +---@class Timeline : Element +local Timeline = class(Element) + +function Timeline:new() return Class.new(self) --[[@as Timeline]] end +function Timeline:init() + Element.init(self, 'timeline') + ---@type false|{pause: boolean, distance: number, last: {x: number, y: number}} + self.pressed = false + self.obstructed = false + self.size_max = 0 + self.size_min = 0 + self.size_min_override = options.timeline_start_hidden and 0 or nil + self.font_size = 0 + self.top_border = options.timeline_border + self.is_hovered = false + self.has_thumbnail = false + + -- Delayed seeking timer + self.seek_timer = mp.add_timeout(0.05, function() self:set_from_cursor() end) + self.seek_timer:kill() + + -- Release any dragging when file gets unloaded + mp.register_event('end-file', function() self.pressed = false end) +end + +function Timeline:get_visibility() + return Elements.controls and math.max(Elements.controls.proximity, Element.get_visibility(self)) + or Element.get_visibility(self) +end + +function Timeline:decide_enabled() + local previous = self.enabled + self.enabled = not self.obstructed and state.duration ~= nil and state.duration > 0 and state.time ~= nil + if self.enabled ~= previous then Elements:trigger('timeline_enabled', self.enabled) end +end + +function Timeline:get_effective_size_min() + return self.size_min_override or self.size_min +end + +function Timeline:get_effective_size() + if Elements.speed and Elements.speed.dragging then return self.size_max end + local size_min = self:get_effective_size_min() + return size_min + math.ceil((self.size_max - size_min) * self:get_visibility()) +end + +function Timeline:get_effective_line_width() + return state.fullormaxed and options.timeline_line_width_fullscreen or options.timeline_line_width +end + +function Timeline:get_is_hovered() return self.enabled and self.is_hovered end + +function Timeline:update_dimensions() + if state.fullormaxed then + self.size_min = options.timeline_size_min_fullscreen + self.size_max = options.timeline_size_max_fullscreen + else + self.size_min = options.timeline_size_min + self.size_max = options.timeline_size_max + end + self.font_size = math.floor(math.min((self.size_max + 60) * 0.2, self.size_max * 0.96) * options.font_scale) + self.ax = Elements.window_border.size + self.ay = display.height - Elements.window_border.size - self.size_max - self.top_border + self.bx = display.width - Elements.window_border.size + self.by = display.height - Elements.window_border.size + self.width = self.bx - self.ax + self.chapter_size = math.max((self.by - self.ay) / 10, 3) + self.chapter_size_hover = self.chapter_size * 2 + + -- Disable if not enough space + local available_space = display.height - Elements.window_border.size * 2 + if Elements.top_bar.enabled then available_space = available_space - Elements.top_bar.size end + self.obstructed = available_space < self.size_max + 10 + self:decide_enabled() +end + +function Timeline:get_time_at_x(x) + local line_width = (options.timeline_style == 'line' and self:get_effective_line_width() - 1 or 0) + local time_width = self.width - line_width - 1 + local fax = (time_width) * state.time / state.duration + local fbx = fax + line_width + -- time starts 0.5 pixels in + x = x - self.ax - 0.5 + if x > fbx then x = x - line_width + elseif x > fax then x = fax end + local progress = clamp(0, x / time_width, 1) + return state.duration * progress +end + +---@param fast? boolean +function Timeline:set_from_cursor(fast) + if state.time and state.duration then + mp.commandv('seek', self:get_time_at_x(cursor.x), fast and 'absolute+keyframes' or 'absolute+exact') + end +end + +function Timeline:clear_thumbnail() + mp.commandv('script-message-to', 'thumbfast', 'clear') + self.has_thumbnail = false +end + +function Timeline:handle_cursor_down() + self.pressed = {pause = state.pause, distance = 0, last = {x = cursor.x, y = cursor.y}} + mp.set_property_native('pause', true) + self:set_from_cursor() + cursor.on_primary_up = function() self:handle_cursor_up() end +end +function Timeline:on_prop_duration() self:decide_enabled() end +function Timeline:on_prop_time() self:decide_enabled() end +function Timeline:on_prop_border() self:update_dimensions() end +function Timeline:on_prop_fullormaxed() self:update_dimensions() end +function Timeline:on_display() self:update_dimensions() end +function Timeline:handle_cursor_up() + self.seek_timer:kill() + if self.pressed then + mp.set_property_native('pause', self.pressed.pause) + self.pressed = false + end +end +function Timeline:on_global_mouse_leave() + self.pressed = false +end + +function Timeline:on_global_mouse_move() + if self.pressed then + self.pressed.distance = self.pressed.distance + get_point_to_point_proximity(self.pressed.last, cursor) + self.pressed.last.x, self.pressed.last.y = cursor.x, cursor.y + if self.width / state.duration < 10 then + self:set_from_cursor(true) + self.seek_timer:kill() + self.seek_timer:resume() + else self:set_from_cursor() end + end +end +function Timeline:handle_wheel_up() mp.commandv('seek', options.timeline_step) end +function Timeline:handle_wheel_down() mp.commandv('seek', -options.timeline_step) end + +function Timeline:render() + if self.size_max == 0 then return end + + local size_min = self:get_effective_size_min() + local size = self:get_effective_size() + local visibility = self:get_visibility() + self.is_hovered = false + + if size < 1 then + if self.has_thumbnail then self:clear_thumbnail() end + return + end + + if self.proximity_raw == 0 then + self.is_hovered = true + cursor.on_primary_down = function() self:handle_cursor_down() end + cursor.on_wheel_down = function() self:handle_wheel_down() end + cursor.on_wheel_up = function() self:handle_wheel_up() end + end + + if self.pressed then + cursor.on_primary_up = function() self:handle_cursor_up() end + end + + local ass = assdraw.ass_new() + + -- Text opacity rapidly drops to 0 just before it starts overflowing, or before it reaches timeline.size_min + local hide_text_below = math.max(self.font_size * 0.8, size_min * 2) + local hide_text_ramp = hide_text_below / 2 + local text_opacity = clamp(0, size - hide_text_below, hide_text_ramp) / hide_text_ramp + + local spacing = math.max(math.floor((self.size_max - self.font_size) / 2.5), 4) + local progress = state.time / state.duration + local is_line = options.timeline_style == 'line' + + -- Foreground & Background bar coordinates + local bax, bay, bbx, bby = self.ax, self.by - size - self.top_border, self.bx, self.by + local fax, fay, fbx, fby = 0, bay + self.top_border, 0, bby + local fcy = fay + (size / 2) + + local line_width = 0 + + if is_line then + local minimized_fraction = 1 - math.min((size - size_min) / ((self.size_max - size_min) / 8), 1) + local line_width_max = self:get_effective_line_width() + local max_min_width_delta = size_min > 0 + and line_width_max - line_width_max * options.timeline_line_width_minimized_scale + or 0 + line_width = line_width_max - (max_min_width_delta * minimized_fraction) + fax = bax + (self.width - line_width) * progress + fbx = fax + line_width + line_width = line_width - 1 + else + fax, fbx = bax, bax + self.width * progress + end + + local foreground_size = fby - fay + local foreground_coordinates = round(fax) .. ',' .. fay .. ',' .. round(fbx) .. ',' .. fby -- for clipping + + -- time starts 0.5 pixels in + local time_ax = bax + 0.5 + local time_width = self.width - line_width - 1 + + -- time to x: calculates x coordinate so that it never lies inside of the line + local function t2x(time) + local x = time_ax + time_width * time / state.duration + return time <= state.time and x or x + line_width + end + + -- Background + ass:new_event() + ass:pos(0, 0) + ass:append('{\\rDefault\\an7\\blur0\\bord0\\1c&H' .. bg .. '}') + ass:opacity(options.timeline_opacity) + ass:draw_start() + ass:rect_cw(bax, bay, fax, bby) --left of progress + ass:rect_cw(fbx, bay, bbx, bby) --right of progress + ass:rect_cw(fax, bay, fbx, fay) --above progress + ass:draw_stop() + + -- Progress + ass:rect(fax, fay, fbx, fby, {opacity = options.timeline_opacity}) + + -- Uncached ranges + local buffered_playtime = nil + if state.uncached_ranges then + local opts = {size = 80, anchor_y = fby} + local texture_char = visibility > 0 and 'b' or 'a' + local offset = opts.size / (visibility > 0 and 24 or 28) + for _, range in ipairs(state.uncached_ranges) do + if not buffered_playtime and (range[1] > state.time or range[2] > state.time) then + buffered_playtime = (range[1] - state.time) / (state.speed or 1) + end + if options.timeline_cache then + local ax = range[1] < 0.5 and bax or math.floor(t2x(range[1])) + local bx = range[2] > state.duration - 0.5 and bbx or math.ceil(t2x(range[2])) + opts.color, opts.opacity, opts.anchor_x = 'ffffff', 0.4 - (0.2 * visibility), bax + ass:texture(ax, fay, bx, fby, texture_char, opts) + opts.color, opts.opacity, opts.anchor_x = '000000', 0.6 - (0.2 * visibility), bax + offset + ass:texture(ax, fay, bx, fby, texture_char, opts) + end + end + end + + -- Custom ranges + for _, chapter_range in ipairs(state.chapter_ranges) do + local rax = chapter_range.start < 0.1 and bax or t2x(chapter_range.start) + local rbx = chapter_range['end'] > state.duration - 0.1 and bbx + or t2x(math.min(chapter_range['end'], state.duration)) + ass:rect(rax, fay, rbx, fby, {color = chapter_range.color, opacity = chapter_range.opacity}) + end + + -- Chapters + local hovered_chapter = nil + if (options.timeline_chapters_opacity > 0 + and (#state.chapters > 0 or state.ab_loop_a or state.ab_loop_b) + ) then + local diamond_radius = foreground_size < 3 and foreground_size or self.chapter_size + local diamond_radius_hovered = diamond_radius * 2 + local diamond_border = options.timeline_border and math.max(options.timeline_border, 1) or 1 + + if diamond_radius > 0 then + local function draw_chapter(time, radius) + local chapter_x, chapter_y = t2x(time), fay - 1 + ass:new_event() + ass:append(string.format( + '{\\pos(0,0)\\rDefault\\an7\\blur0\\yshad0.01\\bord%f\\1c&H%s\\3c&H%s\\4c&H%s\\1a&H%X&\\3a&H00&\\4a&H00&}', + diamond_border, fg, bg, bg, opacity_to_alpha(options.timeline_opacity * options.timeline_chapters_opacity) + )) + ass:draw_start() + ass:move_to(chapter_x - radius, chapter_y) + ass:line_to(chapter_x, chapter_y - radius) + ass:line_to(chapter_x + radius, chapter_y) + ass:line_to(chapter_x, chapter_y + radius) + ass:draw_stop() + end + + if #state.chapters > 0 then + -- Find hovered chapter indicator + local closest_delta = INFINITY + + if self.proximity_raw < diamond_radius_hovered then + for i, chapter in ipairs(state.chapters) do + local chapter_x, chapter_y = t2x(chapter.time), fay - 1 + local cursor_chapter_delta = math.sqrt((cursor.x - chapter_x) ^ 2 + (cursor.y - chapter_y) ^ 2) + if cursor_chapter_delta <= diamond_radius_hovered and cursor_chapter_delta < closest_delta then + hovered_chapter, closest_delta = chapter, cursor_chapter_delta + self.is_hovered = true + cursor.on_primary_down = function() + mp.commandv('seek', hovered_chapter.time, 'absolute+exact') + end + end + end + end + + for i, chapter in ipairs(state.chapters) do + if chapter ~= hovered_chapter then draw_chapter(chapter.time, diamond_radius) end + end + + -- Render hovered chapter above others + if hovered_chapter then draw_chapter(hovered_chapter.time, diamond_radius_hovered) end + end + + -- A-B loop indicators + local has_a, has_b = state.ab_loop_a and state.ab_loop_a >= 0, state.ab_loop_b and state.ab_loop_b > 0 + local ab_radius = round(math.min(math.max(8, foreground_size * 0.25), foreground_size)) + + ---@param time number + ---@param kind 'a'|'b' + local function draw_ab_indicator(time, kind) + local x = t2x(time) + ass:new_event() + ass:append(string.format( + '{\\pos(0,0)\\rDefault\\an7\\blur0\\yshad0.01\\bord%f\\1c&H%s\\3c&H%s\\4c&H%s\\1a&H%X&\\3a&H00&\\4a&H00&}', + diamond_border, fg, bg, bg, opacity_to_alpha(options.timeline_opacity * options.timeline_chapters_opacity) + )) + ass:draw_start() + ass:move_to(x, fby - ab_radius) + if kind == 'b' then ass:line_to(x + 3, fby - ab_radius) end + ass:line_to(x + (kind == 'a' and 0 or ab_radius), fby) + ass:line_to(x - (kind == 'b' and 0 or ab_radius), fby) + if kind == 'a' then ass:line_to(x - 3, fby - ab_radius) end + ass:draw_stop() + end + + if has_a then draw_ab_indicator(state.ab_loop_a, 'a') end + if has_b then draw_ab_indicator(state.ab_loop_b, 'b') end + end + end + + local function draw_timeline_text(x, y, align, text, opts) + opts.color, opts.border_color = fgt, fg + opts.clip = '\\clip(' .. foreground_coordinates .. ')' + ass:txt(x, y, align, text, opts) + opts.color, opts.border_color = bgt, bg + opts.clip = '\\iclip(' .. foreground_coordinates .. ')' + ass:txt(x, y, align, text, opts) + end + + -- Time values + if text_opacity > 0 then + local time_opts = {size = self.font_size, opacity = text_opacity, border = 2} + -- Upcoming cache time + if buffered_playtime and options.buffered_time_threshold > 0 + and buffered_playtime < options.buffered_time_threshold then + local x, align = fbx + 5, 4 + local cache_opts = {size = self.font_size * 0.8, opacity = text_opacity * 0.6, border = 1} + local human = round(math.max(buffered_playtime, 0)) .. 's' + local width = text_width(human, cache_opts) + local time_width = timestamp_width(state.time_human, time_opts) + local time_width_end = timestamp_width(state.destination_time_human, time_opts) + local min_x, max_x = bax + spacing + 5 + time_width, bbx - spacing - 5 - time_width_end + if x < min_x then x = min_x elseif x + width > max_x then x, align = max_x, 6 end + draw_timeline_text(x, fcy, align, human, cache_opts) + end + + -- Elapsed time + if state.time_human then + draw_timeline_text(bax + spacing, fcy, 4, state.time_human, time_opts) + end + + -- End time + if state.destination_time_human then + draw_timeline_text(bbx - spacing, fcy, 6, state.destination_time_human, time_opts) + end + end + + -- Hovered time and chapter + local rendered_thumbnail = false + if (self.proximity_raw == 0 or self.pressed or hovered_chapter) and + not (Elements.speed and Elements.speed.dragging) then + local cursor_x = hovered_chapter and t2x(hovered_chapter.time) or cursor.x + local hovered_seconds = hovered_chapter and hovered_chapter.time or self:get_time_at_x(cursor.x) + + -- Cursor line + -- 0.5 to switch when the pixel is half filled in + local color = ((fax - 0.5) < cursor_x and cursor_x < (fbx + 0.5)) and bg or fg + local ax, ay, bx, by = cursor_x - 0.5, fay, cursor_x + 0.5, fby + ass:rect(ax, ay, bx, by, {color = color, opacity = 0.2}) + local tooltip_anchor = {ax = ax, ay = ay, bx = bx, by = by} + + -- Timestamp + local offset = #state.chapters > 0 and 10 or 4 + local opts = {size = self.font_size, offset = offset} + local hovered_time_human = format_time(hovered_seconds, state.duration) + opts.width_overwrite = timestamp_width(hovered_time_human, opts) + ass:tooltip(tooltip_anchor, hovered_time_human, opts) + tooltip_anchor.ay = tooltip_anchor.ay - self.font_size - offset + + -- Thumbnail + if not thumbnail.disabled + and (not self.pressed or self.pressed.distance < 5) + and thumbnail.width ~= 0 + and thumbnail.height ~= 0 + then + local scale_x, scale_y = display.scale_x, display.scale_y + local border, margin_x, margin_y = math.ceil(2 * scale_x), round(10 * scale_x), round(5 * scale_y) + local thumb_x_margin, thumb_y_margin = border + margin_x, border + margin_y + local thumb_width, thumb_height = thumbnail.width, thumbnail.height + local thumb_x = round(clamp( + thumb_x_margin, cursor_x * scale_x - thumb_width / 2, + display.width * scale_x - thumb_width - thumb_x_margin + )) + local thumb_y = round(tooltip_anchor.ay * scale_y - thumb_y_margin - thumb_height) + local ax, ay = (thumb_x - border) / scale_x, (thumb_y - border) / scale_y + local bx, by = (thumb_x + thumb_width + border) / scale_x, (thumb_y + thumb_height + border) / scale_y + ass:rect(ax, ay, bx, by, {color = bg, border = 1, border_color = fg, border_opacity = 0.08, radius = 2}) + mp.commandv('script-message-to', 'thumbfast', 'thumb', hovered_seconds, thumb_x, thumb_y) + self.has_thumbnail, rendered_thumbnail = true, true + tooltip_anchor.ax, tooltip_anchor.bx, tooltip_anchor.ay = ax, bx, ay + end + + -- Chapter title + if #state.chapters > 0 then + local _, chapter = itable_find(state.chapters, function(c) return hovered_seconds >= c.time end, true) + if chapter and not chapter.is_end_only then + ass:tooltip(tooltip_anchor, chapter.title_wrapped, { + size = self.font_size, offset = 10, responsive = false, bold = true, + width_overwrite = chapter.title_wrapped_width * self.font_size, + }) + end + end + end + + -- Clear thumbnail + if not rendered_thumbnail and self.has_thumbnail then self:clear_thumbnail() end + + return ass +end + +return Timeline diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/TopBar.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/TopBar.lua new file mode 100644 index 0000000..514def2 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/TopBar.lua @@ -0,0 +1,253 @@ +local Element = require('uosc_shared/elements/Element') + +---@alias TopBarButtonProps {icon: string; background: string; anchor_id?: string; command: string|fun()} + +---@class TopBarButton : Element +local TopBarButton = class(Element) + +---@param id string +---@param props TopBarButtonProps +function TopBarButton:new(id, props) return Class.new(self, id, props) --[[@as TopBarButton]] end +function TopBarButton:init(id, props) + Element.init(self, id, props) + self.anchor_id = 'top_bar' + self.icon = props.icon + self.background = props.background + self.command = props.command +end + +function TopBarButton:handle_cursor_down() + mp.command(type(self.command) == 'function' and self.command() or self.command) +end + +function TopBarButton:render() + local visibility = self:get_visibility() + if visibility <= 0 then return end + local ass = assdraw.ass_new() + + -- Background on hover + if self.proximity_raw == 0 then + ass:rect(self.ax, self.ay, self.bx, self.by, {color = self.background, opacity = visibility}) + cursor.on_primary_down = function() self:handle_cursor_down() end + end + + local width, height = self.bx - self.ax, self.by - self.ay + local icon_size = math.min(width, height) * 0.5 + ass:icon(self.ax + width / 2, self.ay + height / 2, icon_size, self.icon, { + opacity = visibility, border = options.text_border, + }) + + return ass +end + +--[[ TopBar ]] + +---@class TopBar : Element +local TopBar = class(Element) + +function TopBar:new() return Class.new(self) --[[@as TopBar]] end +function TopBar:init() + Element.init(self, 'top_bar') + self.size = 0 + self.icon_size, self.spacing, self.font_size, self.title_bx, self.title_by = 1, 1, 1, 1, 1 + self.show_alt_title = false + self.main_title, self.alt_title = nil, nil + + local function get_maximized_command() + return state.border + and (state.fullscreen and 'set fullscreen no;cycle window-maximized' or 'cycle window-maximized') + or 'set window-maximized no;cycle fullscreen' + end + + -- Order aligns from right to left + self.buttons = { + TopBarButton:new('tb_close', {icon = 'close', background = '2311e8', command = 'quit'}), + TopBarButton:new('tb_max', {icon = 'crop_square', background = '222222', command = get_maximized_command}), + TopBarButton:new('tb_min', {icon = 'minimize', background = '222222', command = 'cycle window-minimized'}), + } + + self:decide_titles() +end + +function TopBar:decide_enabled() + if options.top_bar == 'no-border' then + self.enabled = not state.border or state.fullscreen + else + self.enabled = options.top_bar == 'always' + end + self.enabled = self.enabled and (options.top_bar_controls or options.top_bar_title) + for _, element in ipairs(self.buttons) do + element.enabled = self.enabled and options.top_bar_controls + end +end + +function TopBar:decide_titles() + self.alt_title = state.alt_title ~= '' and state.alt_title or nil + self.main_title = state.title ~= '' and state.title or nil + + -- Fall back to alt title if main is empty + if not self.main_title then + self.main_title, self.alt_title = self.alt_title, nil + end + + -- Deduplicate the main and alt titles by checking if one completely + -- contains the other, and using only the longer one. + if self.main_title and self.alt_title and not self.show_alt_title then + local longer_title, shorter_title + if #self.main_title < #self.alt_title then + longer_title, shorter_title = self.alt_title, self.main_title + else + longer_title, shorter_title = self.main_title, self.alt_title + end + + local escaped_shorter_title = string.gsub(shorter_title --[[@as string]], "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") + if string.match(longer_title --[[@as string]], escaped_shorter_title) then + self.main_title, self.alt_title = longer_title, nil + end + end +end + +function TopBar:update_dimensions() + self.size = state.fullormaxed and options.top_bar_size_fullscreen or options.top_bar_size + self.icon_size = round(self.size * 0.5) + self.spacing = math.ceil(self.size * 0.25) + self.font_size = math.floor((self.size - (self.spacing * 2)) * options.font_scale) + self.button_width = round(self.size * 1.15) + self.ay = Elements.window_border.size + self.bx = display.width - Elements.window_border.size + self.by = self.size + Elements.window_border.size + self.title_bx = self.bx - (options.top_bar_controls and (self.button_width * 3) or 0) + self.ax = options.top_bar_title and Elements.window_border.size or self.title_bx + + local button_bx = self.bx + for _, element in pairs(self.buttons) do + element.ax, element.bx = button_bx - self.button_width, button_bx + element.ay, element.by = self.ay, self.by + button_bx = button_bx - self.button_width + end +end + +function TopBar:toggle_title() + if options.top_bar_alt_title_place ~= 'toggle' then return end + self.show_alt_title = not self.show_alt_title +end + +function TopBar:on_prop_title() self:decide_titles() end +function TopBar:on_prop_alt_title() self:decide_titles() end + +function TopBar:on_prop_border() + self:decide_enabled() + self:update_dimensions() +end + +function TopBar:on_prop_fullscreen() + self:decide_enabled() + self:update_dimensions() +end + +function TopBar:on_prop_maximized() + self:decide_enabled() + self:update_dimensions() +end + +function TopBar:on_display() self:update_dimensions() end + +function TopBar:render() + local visibility = self:get_visibility() + if visibility <= 0 then return end + local ass = assdraw.ass_new() + + -- Window title + if options.top_bar_title and (state.title or state.has_playlist) then + local bg_margin = math.floor((self.size - self.font_size) / 4) + local padding = self.font_size / 2 + local title_ax = self.ax + bg_margin + local title_ay = self.ay + bg_margin + local max_bx = self.title_bx - self.spacing + + -- Playlist position + if state.has_playlist then + local text = state.playlist_pos .. '' .. state.playlist_count + local formatted_text = '{\\b1}' .. state.playlist_pos .. '{\\b0\\fs' .. self.font_size * 0.9 .. '}/' + .. state.playlist_count + local opts = {size = self.font_size, wrap = 2, color = fgt, opacity = visibility} + local bx = round(title_ax + text_width(text, opts) + padding * 2) + ass:rect(title_ax, title_ay, bx, self.by - bg_margin, {color = fg, opacity = visibility, radius = 2}) + ass:txt(title_ax + (bx - title_ax) / 2, self.ay + (self.size / 2), 5, formatted_text, opts) + title_ax = bx + bg_margin + local rect = {ax = self.ax, ay = self.ay, bx = bx, by = self.by} + + if get_point_to_rectangle_proximity(cursor, rect) == 0 then + cursor.on_primary_down = function() mp.command('script-binding uosc/playlist') end + end + end + + -- Skip rendering titles if there's not enough horizontal space + if max_bx - title_ax > self.font_size * 3 then + -- Main title + local main_title = self.show_alt_title and self.alt_title or self.main_title + if main_title then + local opts = { + size = self.font_size, wrap = 2, color = bgt, border = 1, border_color = bg, opacity = visibility, + clip = string.format('\\clip(%d, %d, %d, %d)', self.ax, self.ay, max_bx, self.by), + } + local bx = math.min(max_bx, title_ax + text_width(main_title, opts) + padding * 2) + local by = self.by - bg_margin + local rect = {ax = title_ax, ay = self.ay, bx = self.title_bx, by = self.by} + + if get_point_to_rectangle_proximity(cursor, rect) == 0 then + cursor.on_primary_down = function() self:toggle_title() end + end + + ass:rect(title_ax, title_ay, bx, by, { + color = bg, opacity = visibility * options.top_bar_title_opacity, radius = 2, + }) + ass:txt(title_ax + padding, self.ay + (self.size / 2), 4, main_title, opts) + title_ay = by + 1 + end + + -- Alt title + if self.alt_title and options.top_bar_alt_title_place == 'below' then + local font_size = self.font_size * 0.9 + local height = font_size * 1.3 + local by = title_ay + height + local opts = { + size = font_size, wrap = 2, color = bgt, border = 1, border_color = bg, opacity = visibility + } + local bx = math.min(max_bx, title_ax + text_width(self.alt_title, opts) + padding * 2) + opts.clip = string.format('\\clip(%d, %d, %d, %d)', title_ax, title_ay, bx, by) + ass:rect(title_ax, title_ay, bx, by, { + color = bg, opacity = visibility * options.top_bar_title_opacity, radius = 2, + }) + ass:txt(title_ax + padding, title_ay + height / 2, 4, self.alt_title, opts) + title_ay = by + 1 + end + + -- Subtitle: current chapter + if state.current_chapter then + local font_size = self.font_size * 0.8 + local height = font_size * 1.3 + local text = '└ ' .. state.current_chapter.index .. ': ' .. state.current_chapter.title + local by = title_ay + height + local opts = { + size = font_size, italic = true, wrap = 2, color = bgt, + border = 1, border_color = bg, opacity = visibility * 0.8, + } + local bx = math.min(max_bx, title_ax + text_width(text, opts) + padding * 2) + opts.clip = string.format('\\clip(%d, %d, %d, %d)', title_ax, title_ay, bx, by) + ass:rect(title_ax, title_ay, bx, by, { + color = bg, opacity = visibility * options.top_bar_title_opacity, radius = 2, + }) + ass:txt(title_ax + padding, title_ay + height / 2, 4, text, opts) + title_ay = by + 1 + end + end + self.title_by = title_ay - 1 + else + self.title_by = self.ay + end + + return ass +end + +return TopBar diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/Volume.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/Volume.lua new file mode 100644 index 0000000..2f591b6 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/Volume.lua @@ -0,0 +1,252 @@ +local Element = require('uosc_shared/elements/Element') + +--[[ MuteButton ]] + +---@class MuteButton : Element +local MuteButton = class(Element) +---@param props? ElementProps +function MuteButton:new(props) return Class.new(self, 'volume_mute', props) --[[@as MuteButton]] end +function MuteButton:get_visibility() return Elements.volume:get_visibility(self) end +function MuteButton:render() + local visibility = self:get_visibility() + if visibility <= 0 then return end + if self.proximity_raw == 0 then + cursor.on_primary_down = function() mp.commandv('cycle', 'mute') end + end + local ass = assdraw.ass_new() + local icon_name = state.mute and 'volume_off' or 'volume_up' + local width = self.bx - self.ax + ass:icon(self.ax + (width / 2), self.by, width * 0.7, icon_name, + {border = options.text_border, opacity = options.volume_opacity * visibility, align = 2} + ) + return ass +end + +--[[ VolumeSlider ]] + +---@class VolumeSlider : Element +local VolumeSlider = class(Element) +---@param props? ElementProps +function VolumeSlider:new(props) return Class.new(self, props) --[[@as VolumeSlider]] end +function VolumeSlider:init(props) + Element.init(self, 'volume_slider', props) + self.pressed = false + self.nudge_y = 0 -- vertical position where volume overflows 100 + self.nudge_size = 0 + self.draw_nudge = false + self.spacing = 0 + self.radius = 1 +end + +function VolumeSlider:get_visibility() return Elements.volume:get_visibility(self) end + +function VolumeSlider:set_volume(volume) + volume = round(volume / options.volume_step) * options.volume_step + if state.volume == volume then return end + mp.commandv('set', 'volume', clamp(0, volume, state.volume_max)) +end + +function VolumeSlider:set_from_cursor() + local volume_fraction = (self.by - cursor.y - options.volume_border) / (self.by - self.ay - options.volume_border) + self:set_volume(volume_fraction * state.volume_max) +end + +function VolumeSlider:on_coordinates() + if type(state.volume_max) ~= 'number' or state.volume_max <= 0 then return end + local width = self.bx - self.ax + self.nudge_y = self.by - round((self.by - self.ay) * (100 / state.volume_max)) + self.nudge_size = round(width * 0.18) + self.draw_nudge = self.ay < self.nudge_y + self.spacing = round(width * 0.2) + self.radius = math.max(2, (self.bx - self.ax) / 10) +end +function VolumeSlider:on_global_mouse_move() + if self.pressed then self:set_from_cursor() end +end +function VolumeSlider:handle_wheel_up() self:set_volume(state.volume + options.volume_step) end +function VolumeSlider:handle_wheel_down() self:set_volume(state.volume - options.volume_step) end + +function VolumeSlider:render() + local visibility = self:get_visibility() + local ax, ay, bx, by = self.ax, self.ay, self.bx, self.by + local width, height = bx - ax, by - ay + + if width <= 0 or height <= 0 or visibility <= 0 then return end + + if self.proximity_raw == 0 then + cursor.on_primary_down = function() + self.pressed = true + self:set_from_cursor() + cursor.on_primary_up = function() self.pressed = false end + end + cursor.on_wheel_down = function() self:handle_wheel_down() end + cursor.on_wheel_up = function() self:handle_wheel_up() end + end + if self.pressed then cursor.on_primary_up = function() + self.pressed = false end + end + + local ass = assdraw.ass_new() + local nudge_y, nudge_size = self.draw_nudge and self.nudge_y or -INFINITY, self.nudge_size + local volume_y = self.ay + options.volume_border + + ((height - (options.volume_border * 2)) * (1 - math.min(state.volume / state.volume_max, 1))) + + -- Draws a rectangle with nudge at requested position + ---@param p number Padding from slider edges. + ---@param cy? number A y coordinate where to clip the path from the bottom. + function create_nudged_path(p, cy) + cy = cy or ay + p + local ax, bx, by = ax + p, bx - p, by - p + local r = math.max(1, self.radius - p) + local d, rh = r * 2, r / 2 + local nudge_size = ((QUARTER_PI_SIN * (nudge_size - p)) + p) / QUARTER_PI_SIN + local path = assdraw.ass_new() + path:move_to(bx - r, by) + path:line_to(ax + r, by) + if cy > by - d then + local subtracted_radius = (d - (cy - (by - d))) / 2 + local xbd = (r - subtracted_radius * 1.35) -- x bezier delta + path:bezier_curve(ax + xbd, by, ax + xbd, cy, ax + r, cy) + path:line_to(bx - r, cy) + path:bezier_curve(bx - xbd, cy, bx - xbd, by, bx - r, by) + else + path:bezier_curve(ax + rh, by, ax, by - rh, ax, by - r) + local nudge_bottom_y = nudge_y + nudge_size + + if cy + rh <= nudge_bottom_y then + path:line_to(ax, nudge_bottom_y) + if cy <= nudge_y then + path:line_to((ax + nudge_size), nudge_y) + local nudge_top_y = nudge_y - nudge_size + if cy <= nudge_top_y then + local r, rh = r, rh + if cy > nudge_top_y - r then + r = nudge_top_y - cy + rh = r / 2 + end + path:line_to(ax, nudge_top_y) + path:line_to(ax, cy + r) + path:bezier_curve(ax, cy + rh, ax + rh, cy, ax + r, cy) + path:line_to(bx - r, cy) + path:bezier_curve(bx - rh, cy, bx, cy + rh, bx, cy + r) + path:line_to(bx, nudge_top_y) + else + local triangle_side = cy - nudge_top_y + path:line_to((ax + triangle_side), cy) + path:line_to((bx - triangle_side), cy) + end + path:line_to((bx - nudge_size), nudge_y) + else + local triangle_side = nudge_bottom_y - cy + path:line_to((ax + triangle_side), cy) + path:line_to((bx - triangle_side), cy) + end + path:line_to(bx, nudge_bottom_y) + else + path:line_to(ax, cy + r) + path:bezier_curve(ax, cy + rh, ax + rh, cy, ax + r, cy) + path:line_to(bx - r, cy) + path:bezier_curve(bx - rh, cy, bx, cy + rh, bx, cy + r) + end + path:line_to(bx, by - r) + path:bezier_curve(bx, by - rh, bx - rh, by, bx - r, by) + end + return path + end + + -- BG & FG paths + local bg_path = create_nudged_path(0) + local fg_path = create_nudged_path(options.volume_border, volume_y) + + -- Background + ass:new_event() + ass:append('{\\rDefault\\an7\\blur0\\bord0\\1c&H' .. bg .. + '\\iclip(' .. fg_path.scale .. ', ' .. fg_path.text .. ')}') + ass:opacity(options.volume_opacity, visibility) + ass:pos(0, 0) + ass:draw_start() + ass:append(bg_path.text) + ass:draw_stop() + + -- Foreground + ass:new_event() + ass:append('{\\rDefault\\an7\\blur0\\bord0\\1c&H' .. fg .. '}') + ass:opacity(options.volume_opacity, visibility) + ass:pos(0, 0) + ass:draw_start() + ass:append(fg_path.text) + ass:draw_stop() + + -- Current volume value + local volume_string = tostring(round(state.volume * 10) / 10) + local font_size = round(((width * 0.6) - (#volume_string * (width / 20))) * options.font_scale) + if volume_y < self.by - self.spacing then + ass:txt(self.ax + (width / 2), self.by - self.spacing, 2, volume_string, { + size = font_size, color = fgt, opacity = visibility, + clip = '\\clip(' .. fg_path.scale .. ', ' .. fg_path.text .. ')', + }) + end + if volume_y > self.by - self.spacing - font_size then + ass:txt(self.ax + (width / 2), self.by - self.spacing, 2, volume_string, { + size = font_size, color = bgt, opacity = visibility, + clip = '\\iclip(' .. fg_path.scale .. ', ' .. fg_path.text .. ')', + }) + end + + -- Disabled stripes for no audio + if not state.has_audio then + local fg_100_path = create_nudged_path(options.volume_border) + local texture_opts = { + size = 200, color = 'ffffff', opacity = visibility * 0.1, anchor_x = ax, + clip = '\\clip(' .. fg_100_path.scale .. ',' .. fg_100_path.text .. ')', + } + ass:texture(ax, ay, bx, by, 'a', texture_opts) + texture_opts.color = '000000' + texture_opts.anchor_x = ax + texture_opts.size / 28 + ass:texture(ax, ay, bx, by, 'a', texture_opts) + end + + return ass +end + +--[[ Volume ]] + +---@class Volume : Element +local Volume = class(Element) + +function Volume:new() return Class.new(self) --[[@as Volume]] end +function Volume:init() + Element.init(self, 'volume') + self.mute = MuteButton:new({anchor_id = 'volume'}) + self.slider = VolumeSlider:new({anchor_id = 'volume'}) +end + +function Volume:get_visibility() + return self.slider.pressed and 1 or Elements.timeline:get_is_hovered() and -1 or Element.get_visibility(self) +end + +function Volume:update_dimensions() + local width = state.fullormaxed and options.volume_size_fullscreen or options.volume_size + local controls, timeline, top_bar = Elements.controls, Elements.timeline, Elements.top_bar + local min_y = top_bar.enabled and top_bar.by or 0 + local max_y = (controls and controls.enabled and controls.ay) or (timeline.enabled and timeline.ay) + or display.height - top_bar.size + local available_height = max_y - min_y + local max_height = available_height * 0.8 + local height = round(math.min(width * 8, max_height)) + self.enabled = height > width * 2 -- don't render if too small + local margin = (width / 2) + Elements.window_border.size + self.ax = round(options.volume == 'left' and margin or display.width - margin - width) + self.ay = min_y + round((available_height - height) / 2) + self.bx = round(self.ax + width) + self.by = round(self.ay + height) + self.mute.enabled, self.slider.enabled = self.enabled, self.enabled + self.mute:set_coordinates(self.ax, self.by - round(width * 0.8), self.bx, self.by) + self.slider:set_coordinates(self.ax, self.ay, self.bx, self.mute.ay) +end + +function Volume:on_display() self:update_dimensions() end +function Volume:on_prop_border() self:update_dimensions() end +function Volume:on_controls_reflow() self:update_dimensions() end + +return Volume diff --git a/multimedia/.config/mpv/scripts/uosc_shared/elements/WindowBorder.lua b/multimedia/.config/mpv/scripts/uosc_shared/elements/WindowBorder.lua new file mode 100644 index 0000000..c5544f5 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/elements/WindowBorder.lua @@ -0,0 +1,33 @@ +local Element = require('uosc_shared/elements/Element') + +---@class WindowBorder : Element +local WindowBorder = class(Element) + +function WindowBorder:new() return Class.new(self) --[[@as WindowBorder]] end +function WindowBorder:init() + Element.init(self, 'window_border') + self.ignores_menu = true + self.size = 0 +end + +function WindowBorder:decide_enabled() + self.enabled = options.window_border_size > 0 and not state.fullormaxed and not state.border + self.size = self.enabled and options.window_border_size or 0 +end + +function WindowBorder:on_prop_border() self:decide_enabled() end +function WindowBorder:on_prop_fullormaxed() self:decide_enabled() end + +function WindowBorder:render() + if self.size > 0 then + local ass = assdraw.ass_new() + local clip = '\\iclip(' .. self.size .. ',' .. self.size .. ',' .. + (display.width - self.size) .. ',' .. (display.height - self.size) .. ')' + ass:rect(0, 0, display.width + 1, display.height + 1, { + color = bg, clip = clip, opacity = options.window_border_opacity, + }) + return ass + end +end + +return WindowBorder diff --git a/multimedia/.config/mpv/scripts/uosc_shared/lib/ass.lua b/multimedia/.config/mpv/scripts/uosc_shared/lib/ass.lua new file mode 100644 index 0000000..108953f --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/lib/ass.lua @@ -0,0 +1,170 @@ +--[[ ASSDRAW EXTENSIONS ]] + +local ass_mt = getmetatable(assdraw.ass_new()) + +-- Opacity. +---@param opacity number|number[] Opacity of all elements, or an array of [primary, secondary, border, shadow] opacities. +---@param fraction? number Optionally adjust the above opacity by this fraction. +function ass_mt:opacity(opacity, fraction) + fraction = fraction ~= nil and fraction or 1 + if type(opacity) == 'number' then + self.text = self.text .. string.format('{\\alpha&H%X&}', opacity_to_alpha(opacity * fraction)) + else + self.text = self.text .. string.format( + '{\\1a&H%X&\\2a&H%X&\\3a&H%X&\\4a&H%X&}', + opacity_to_alpha((opacity[1] or 0) * fraction), + opacity_to_alpha((opacity[2] or 0) * fraction), + opacity_to_alpha((opacity[3] or 0) * fraction), + opacity_to_alpha((opacity[4] or 0) * fraction) + ) + end +end + +-- Icon. +---@param x number +---@param y number +---@param size number +---@param name string +---@param opts? {color?: string; border?: number; border_color?: string; opacity?: number; clip?: string; align?: number} +function ass_mt:icon(x, y, size, name, opts) + opts = opts or {} + opts.font, opts.size, opts.bold = 'MaterialIconsRound-Regular', size, false + self:txt(x, y, opts.align or 5, name, opts) +end + +-- Text. +-- Named `txt` because `ass.text` is a value. +---@param x number +---@param y number +---@param align number +---@param value string|number +---@param opts {size: number; font?: string; color?: string; bold?: boolean; italic?: boolean; border?: number; border_color?: string; shadow?: number; shadow_color?: string; rotate?: number; wrap?: number; opacity?: number; clip?: string} +function ass_mt:txt(x, y, align, value, opts) + local border_size = opts.border or 0 + local shadow_size = opts.shadow or 0 + local tags = '\\pos(' .. x .. ',' .. y .. ')\\rDefault\\an' .. align .. '\\blur0' + -- font + tags = tags .. '\\fn' .. (opts.font or config.font) + -- font size + tags = tags .. '\\fs' .. opts.size + -- bold + if opts.bold or (opts.bold == nil and options.font_bold) then tags = tags .. '\\b1' end + -- italic + if opts.italic then tags = tags .. '\\i1' end + -- rotate + if opts.rotate then tags = tags .. '\\frz' .. opts.rotate end + -- wrap + if opts.wrap then tags = tags .. '\\q' .. opts.wrap end + -- border + tags = tags .. '\\bord' .. border_size + -- shadow + tags = tags .. '\\shad' .. shadow_size + -- colors + tags = tags .. '\\1c&H' .. (opts.color or bgt) + if border_size > 0 then tags = tags .. '\\3c&H' .. (opts.border_color or bg) end + if shadow_size > 0 then tags = tags .. '\\4c&H' .. (opts.shadow_color or bg) end + -- opacity + if opts.opacity then tags = tags .. string.format('\\alpha&H%X&', opacity_to_alpha(opts.opacity)) end + -- clip + if opts.clip then tags = tags .. opts.clip end + -- render + self:new_event() + self.text = self.text .. '{' .. tags .. '}' .. value +end + +-- Tooltip. +---@param element {ax: number; ay: number; bx: number; by: number} +---@param value string|number +---@param opts? {size?: number; offset?: number; bold?: boolean; italic?: boolean; width_overwrite?: number, responsive?: boolean} +function ass_mt:tooltip(element, value, opts) + opts = opts or {} + opts.size = opts.size or 16 + opts.border = options.text_border + opts.border_color = bg + local offset = opts.offset or opts.size / 2 + local align_top = opts.responsive == false or element.ay - offset > opts.size * 2 + local x = element.ax + (element.bx - element.ax) / 2 + local y = align_top and element.ay - offset or element.by + offset + local margin = (opts.width_overwrite or text_width(value, opts)) / 2 + 10 + self:txt(clamp(margin, x, display.width - margin), y, align_top and 2 or 8, value, opts) +end + +-- Rectangle. +---@param ax number +---@param ay number +---@param bx number +---@param by number +---@param opts? {color?: string; border?: number; border_color?: string; opacity?: number; border_opacity?: number; clip?: string, radius?: number} +function ass_mt:rect(ax, ay, bx, by, opts) + opts = opts or {} + local border_size = opts.border or 0 + local tags = '\\pos(0,0)\\rDefault\\an7\\blur0' + -- border + tags = tags .. '\\bord' .. border_size + -- colors + tags = tags .. '\\1c&H' .. (opts.color or fg) + if border_size > 0 then tags = tags .. '\\3c&H' .. (opts.border_color or bg) end + -- opacity + if opts.opacity then tags = tags .. string.format('\\alpha&H%X&', opacity_to_alpha(opts.opacity)) end + if opts.border_opacity then tags = tags .. string.format('\\3a&H%X&', opacity_to_alpha(opts.border_opacity)) end + -- clip + if opts.clip then + tags = tags .. opts.clip + end + -- draw + self:new_event() + self.text = self.text .. '{' .. tags .. '}' + self:draw_start() + if opts.radius then + self:round_rect_cw(ax, ay, bx, by, opts.radius) + else + self:rect_cw(ax, ay, bx, by) + end + self:draw_stop() +end + +-- Circle. +---@param x number +---@param y number +---@param radius number +---@param opts? {color?: string; border?: number; border_color?: string; opacity?: number; clip?: string} +function ass_mt:circle(x, y, radius, opts) + opts = opts or {} + opts.radius = radius + self:rect(x - radius, y - radius, x + radius, y + radius, opts) +end + +-- Texture. +---@param ax number +---@param ay number +---@param bx number +---@param by number +---@param char string Texture font character. +---@param opts {size?: number; color: string; opacity?: number; clip?: string; anchor_x?: number, anchor_y?: number} +function ass_mt:texture(ax, ay, bx, by, char, opts) + opts = opts or {} + local anchor_x, anchor_y = opts.anchor_x or ax, opts.anchor_y or ay + local clip = opts.clip or ('\\clip(' .. ax .. ',' .. ay .. ',' .. bx .. ',' .. by .. ')') + local tile_size, opacity = opts.size or 100, opts.opacity or 0.2 + local x, y = ax - (ax - anchor_x) % tile_size, ay - (ay - anchor_y) % tile_size + local width, height = bx - x, by - y + local line = string.rep(char, math.ceil((width / tile_size))) + local lines = '' + for i = 1, math.ceil(height / tile_size), 1 do lines = lines .. (lines == '' and '' or '\\N') .. line end + self:txt( + x, y, 7, lines, + {font = 'uosc_textures', size = tile_size, color = opts.color, bold = false, opacity = opacity, clip = clip}) +end + +-- Rotating spinner icon. +---@param x number +---@param y number +---@param size number +---@param opts? {color?: string; opacity?: number; clip?: string; border?: number; border_color?: string;} +function ass_mt:spinner(x, y, size, opts) + opts = opts or {} + opts.rotate = (state.render_last_time * 1.75 % 1) * -360 + opts.color = opts.color or fg + self:icon(x, y, size, 'autorenew', opts) + request_render() +end diff --git a/multimedia/.config/mpv/scripts/uosc_shared/lib/menus.lua b/multimedia/.config/mpv/scripts/uosc_shared/lib/menus.lua new file mode 100644 index 0000000..5b7b790 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/lib/menus.lua @@ -0,0 +1,292 @@ +---@param data MenuData +---@param opts? {submenu?: string; mouse_nav?: boolean; on_close?: string | string[]} +function open_command_menu(data, opts) + local function run_command(command) + if type(command) == 'string' then + mp.command(command) + else + ---@diagnostic disable-next-line: deprecated + mp.commandv(unpack(command)) + end + end + ---@type MenuOptions + local menu_opts = {} + if opts then + menu_opts.mouse_nav = opts.mouse_nav + if opts.on_close then menu_opts.on_close = function() run_command(opts.on_close) end end + end + local menu = Menu:open(data, run_command, menu_opts) + if opts and opts.submenu then menu:activate_submenu(opts.submenu) end + return menu +end + +---@param opts? {submenu?: string; mouse_nav?: boolean; on_close?: string | string[]} +function toggle_menu_with_items(opts) + if Menu:is_open('menu') then Menu:close() + else open_command_menu({type = 'menu', items = config.menu_items}, opts) end +end + +---@param options {type: string; title: string; list_prop: string; active_prop?: string; serializer: fun(list: any, active: any): MenuDataItem[]; on_select: fun(value: any); on_move_item?: fun(from_index: integer, to_index: integer, submenu_path: integer[]); on_delete_item?: fun(index: integer, submenu_path: integer[])} +function create_self_updating_menu_opener(options) + return function() + if Menu:is_open(options.type) then Menu:close() return end + local list = mp.get_property_native(options.list_prop) + local active = options.active_prop and mp.get_property_native(options.active_prop) or nil + local menu + + local function update() menu:update_items(options.serializer(list, active)) end + + local ignore_initial_list = true + local function handle_list_prop_change(name, value) + if ignore_initial_list then ignore_initial_list = false + else list = value update() end + end + + local ignore_initial_active = true + local function handle_active_prop_change(name, value) + if ignore_initial_active then ignore_initial_active = false + else active = value update() end + end + + local initial_items, selected_index = options.serializer(list, active) + + -- Items and active_index are set in the handle_prop_change callback, since adding + -- a property observer triggers its handler immediately, we just let that initialize the items. + menu = Menu:open( + {type = options.type, title = options.title, items = initial_items, selected_index = selected_index}, + options.on_select, { + on_open = function() + mp.observe_property(options.list_prop, 'native', handle_list_prop_change) + if options.active_prop then + mp.observe_property(options.active_prop, 'native', handle_active_prop_change) + end + end, + on_close = function() + mp.unobserve_property(handle_list_prop_change) + mp.unobserve_property(handle_active_prop_change) + end, + on_move_item = options.on_move_item, + on_delete_item = options.on_delete_item, + }) + end +end + +function create_select_tracklist_type_menu_opener(menu_title, track_type, track_prop, load_command) + local function serialize_tracklist(tracklist) + local items = {} + + if load_command then + items[#items + 1] = { + title = 'Load', bold = true, italic = true, hint = 'open file', value = '{load}', separator = true, + } + end + + local first_item_index = #items + 1 + local active_index = nil + local disabled_item = nil + + -- Add option to disable a subtitle track. This works for all tracks, + -- but why would anyone want to disable audio or video? Better to not + -- let people mistakenly select what is unwanted 99.999% of the time. + -- If I'm mistaken and there is an active need for this, feel free to + -- open an issue. + if track_type == 'sub' then + disabled_item = {title = 'Disabled', italic = true, muted = true, hint = '—', value = nil, active = true} + items[#items + 1] = disabled_item + end + + for _, track in ipairs(tracklist) do + if track.type == track_type then + local hint_values = {} + local function h(value) hint_values[#hint_values + 1] = value end + + if track.lang then h(track.lang:upper()) end + if track['demux-h'] then + h(track['demux-w'] and (track['demux-w'] .. 'x' .. track['demux-h']) or (track['demux-h'] .. 'p')) + end + if track['demux-fps'] then h(string.format('%.5gfps', track['demux-fps'])) end + h(track.codec) + if track['audio-channels'] then h(track['audio-channels'] .. ' channels') end + if track['demux-samplerate'] then h(string.format('%.3gkHz', track['demux-samplerate'] / 1000)) end + if track.forced then h('forced') end + if track.default then h('default') end + if track.external then h('external') end + + items[#items + 1] = { + title = (track.title and track.title or 'Track ' .. track.id), + hint = table.concat(hint_values, ', '), + value = track.id, + active = track.selected, + } + + if track.selected then + if disabled_item then disabled_item.active = false end + active_index = #items + end + end + end + + return items, active_index or first_item_index + end + + local function selection_handler(value) + if value == '{load}' then + mp.command(load_command) + else + mp.commandv('set', track_prop, value and value or 'no') + + -- If subtitle track was selected, assume user also wants to see it + if value and track_type == 'sub' then + mp.commandv('set', 'sub-visibility', 'yes') + end + end + end + + return create_self_updating_menu_opener({ + title = menu_title, + type = track_type, + list_prop = 'track-list', + serializer = serialize_tracklist, + on_select = selection_handler, + }) +end + +---@alias NavigationMenuOptions {type: string, title?: string, allowed_types?: string[], active_path?: string, selected_path?: string; on_open?: fun(); on_close?: fun()} + +-- Opens a file navigation menu with items inside `directory_path`. +---@param directory_path string +---@param handle_select fun(path: string): nil +---@param opts NavigationMenuOptions +function open_file_navigation_menu(directory_path, handle_select, opts) + directory = serialize_path(normalize_path(directory_path)) + opts = opts or {} + + if not directory then + msg.error('Couldn\'t serialize path "' .. directory_path .. '.') + return + end + + local files, directories = read_directory(directory.path, opts.allowed_types) + local is_root = not directory.dirname + local path_separator = path_separator(directory.path) + + if not files or not directories then return end + + sort_filenames(directories) + sort_filenames(files) + + -- Pre-populate items with parent directory selector if not at root + -- Each item value is a serialized path table it points to. + local items = {} + + if is_root then + if state.platform == 'windows' then + items[#items + 1] = {title = '..', hint = 'Drives', value = '{drives}', separator = true} + end + else + items[#items + 1] = {title = '..', hint = 'parent dir', value = directory.dirname, separator = true} + end + + local back_path = items[#items] and items[#items].value + local selected_index = #items + 1 + + for _, dir in ipairs(directories) do + items[#items + 1] = {title = dir, value = join_path(directory.path, dir), hint = path_separator} + end + + for _, file in ipairs(files) do + items[#items + 1] = {title = file, value = join_path(directory.path, file)} + end + + for index, item in ipairs(items) do + if not item.value.is_to_parent and opts.active_path == item.value then + item.active = true + if not opts.selected_path then selected_index = index end + end + + if opts.selected_path == item.value then selected_index = index end + end + + ---@type MenuCallback + local function open_path(path, meta) + local is_drives = path == '{drives}' + local is_to_parent = is_drives or #path < #directory_path + local inheritable_options = { + type = opts.type, title = opts.title, allowed_types = opts.allowed_types, active_path = opts.active_path, + } + + if is_drives then + open_drives_menu(function(drive_path) + open_file_navigation_menu(drive_path, handle_select, inheritable_options) + end, { + type = inheritable_options.type, title = inheritable_options.title, selected_path = directory.path, + on_open = opts.on_open, on_close = opts.on_close, + }) + return + end + + local info, error = utils.file_info(path) + + if not info then + msg.error('Can\'t retrieve path info for "' .. path .. '". Error: ' .. (error or '')) + return + end + + if info.is_dir and not meta.modifiers.ctrl then + -- Preselect directory we are coming from + if is_to_parent then + inheritable_options.selected_path = directory.path + end + + open_file_navigation_menu(path, handle_select, inheritable_options) + else + handle_select(path) + end + end + + local function handle_back() + if back_path then open_path(back_path, {modifiers = {}}) end + end + + local menu_data = { + type = opts.type, title = opts.title or directory.basename .. path_separator, items = items, + selected_index = selected_index, + } + local menu_options = {on_open = opts.on_open, on_close = opts.on_close, on_back = handle_back} + + return Menu:open(menu_data, open_path, menu_options) +end + +-- Opens a file navigation menu with Windows drives as items. +---@param handle_select fun(path: string): nil +---@param opts? NavigationMenuOptions +function open_drives_menu(handle_select, opts) + opts = opts or {} + local process = mp.command_native({ + name = 'subprocess', + capture_stdout = true, + playback_only = false, + args = {'wmic', 'logicaldisk', 'get', 'name', '/value'}, + }) + local items, selected_index = {}, 1 + + if process.status == 0 then + for _, value in ipairs(split(process.stdout, '\n')) do + local drive = string.match(value, 'Name=([A-Z]:)') + if drive then + local drive_path = normalize_path(drive) + items[#items + 1] = { + title = drive, hint = 'drive', value = drive_path, active = opts.active_path == drive_path, + } + if opts.selected_path == drive_path then selected_index = #items end + end + end + else + msg.error(process.stderr) + end + + return Menu:open( + {type = opts.type, title = opts.title or 'Drives', items = items, selected_index = selected_index}, + handle_select + ) +end diff --git a/multimedia/.config/mpv/scripts/uosc_shared/lib/std.lua b/multimedia/.config/mpv/scripts/uosc_shared/lib/std.lua new file mode 100644 index 0000000..1261666 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/lib/std.lua @@ -0,0 +1,181 @@ +--[[ Stateless utilities missing in lua standard library ]] + +---@param number number +function round(number) return math.floor(number + 0.5) end + +---@param min number +---@param value number +---@param max number +function clamp(min, value, max) return math.max(min, math.min(value, max)) end + +---@param rgba string `rrggbb` or `rrggbbaa` hex string. +function serialize_rgba(rgba) + local a = rgba:sub(7, 8) + return { + color = rgba:sub(5, 6) .. rgba:sub(3, 4) .. rgba:sub(1, 2), + opacity = clamp(0, tonumber(#a == 2 and a or 'ff', 16) / 255, 1), + } +end + +-- Trim any `char` from the end of the string. +---@param str string +---@param char string +---@return string +function trim_end(str, char) + local char, end_i = char:byte(), 0 + for i = #str, 1, -1 do + if str:byte(i) ~= char then + end_i = i + break + end + end + return str:sub(1, end_i) +end + +---@param str string +---@param pattern string +---@return string[] +function split(str, pattern) + local list = {} + local full_pattern = '(.-)' .. pattern + local last_end = 1 + local start_index, end_index, capture = str:find(full_pattern, 1) + while start_index do + list[#list + 1] = capture + last_end = end_index + 1 + start_index, end_index, capture = str:find(full_pattern, last_end) + end + if last_end <= (#str + 1) then + capture = str:sub(last_end) + list[#list + 1] = capture + end + return list +end + +-- Get index of the last appearance of `sub` in `str`. +---@param str string +---@param sub string +---@return integer|nil +function string_last_index_of(str, sub) + local sub_length = #sub + for i = #str, 1, -1 do + for j = 1, sub_length do + if str:byte(i + j - 1) ~= sub:byte(j) then break end + if j == sub_length then return i end + end + end +end + +---@param itable table +---@param value any +---@return integer|nil +function itable_index_of(itable, value) + for index, item in ipairs(itable) do + if item == value then return index end + end +end + +---@param itable table +---@param compare fun(value: any, index: number) +---@param from_end? boolean Search from the end of the table. +---@return number|nil index +---@return any|nil value +function itable_find(itable, compare, from_end) + local from, to, step = from_end and #itable or 1, from_end and 1 or #itable, from_end and -1 or 1 + for index = from, to, step do + if compare(itable[index], index) then return index, itable[index] end + end +end + +---@param itable table +---@param decider fun(value: any, index: number) +function itable_filter(itable, decider) + local filtered = {} + for index, value in ipairs(itable) do + if decider(value, index) then filtered[#filtered + 1] = value end + end + return filtered +end + +---@param itable table +---@param value any +function itable_remove(itable, value) + return itable_filter(itable, function(item) return item ~= value end) +end + +---@param itable table +---@param start_pos? integer +---@param end_pos? integer +function itable_slice(itable, start_pos, end_pos) + start_pos = start_pos and start_pos or 1 + end_pos = end_pos and end_pos or #itable + + if end_pos < 0 then end_pos = #itable + end_pos + 1 end + if start_pos < 0 then start_pos = #itable + start_pos + 1 end + + local new_table = {} + for index, value in ipairs(itable) do + if index >= start_pos and index <= end_pos then + new_table[#new_table + 1] = value + end + end + return new_table +end + +---@generic T +---@param a T[]|nil +---@param b T[]|nil +---@return T[] +function itable_join(a, b) + local result = {} + if a then for _, value in ipairs(a) do result[#result + 1] = value end end + if b then for _, value in ipairs(b) do result[#result + 1] = value end end + return result +end + +---@param target any[] +---@param source any[] +function itable_append(target, source) + for _, value in ipairs(source) do target[#target + 1] = value end + return target +end + +---@param target any[] +---@param source any[] +---@param props? string[] +function table_assign(target, source, props) + if props then + for _, name in ipairs(props) do target[name] = source[name] end + else + for prop, value in pairs(source) do target[prop] = value end + end + return target +end + +---@generic T +---@param table T +---@return T +function table_shallow_copy(table) + local result = {} + for key, value in pairs(table) do result[key] = value end + return result +end + +--[[ EASING FUNCTIONS ]] + +function ease_out_quart(x) return 1 - ((1 - x) ^ 4) end +function ease_out_sext(x) return 1 - ((1 - x) ^ 6) end + +--[[ CLASSES ]] + +---@class Class +Class = {} +function Class:new(...) + local object = setmetatable({}, {__index = self}) + object:init(...) + return object +end +function Class:init() end +function Class:destroy() end + +function class(parent) return setmetatable({}, {__index = parent or Class}) end diff --git a/multimedia/.config/mpv/scripts/uosc_shared/lib/text.lua b/multimedia/.config/mpv/scripts/uosc_shared/lib/text.lua new file mode 100644 index 0000000..d573b81 --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/lib/text.lua @@ -0,0 +1,461 @@ +-- https://en.wikipedia.org/wiki/Unicode_block +---@alias CodePointRange {[1]: integer; [2]: integer} + +---@type CodePointRange[] +local zero_width_blocks = { + {0x0000, 0x001F}, -- C0 + {0x007F, 0x009F}, -- Delete + C1 + {0x034F, 0x034F}, -- combining grapheme joiner + {0x061C, 0x061C}, -- Arabic Letter Strong + {0x200B, 0x200F}, -- {zero-width space, zero-width non-joiner, zero-width joiner, left-to-right mark, right-to-left mark} + {0x2028, 0x202E}, -- {line separator, paragraph separator, Left-to-Right Embedding, Right-to-Left Embedding, Pop Directional Format, Left-to-Right Override, Right-to-Left Override} + {0x2060, 0x2060}, -- word joiner + {0x2066, 0x2069}, -- {Left-to-Right Isolate, Right-to-Left Isolate, First Strong Isolate, Pop Directional Isolate} + {0xFEFF, 0xFEFF}, -- zero-width non-breaking space + -- Some other characters can also be combined https://en.wikipedia.org/wiki/Combining_character + {0x0300, 0x036F}, -- Combining Diacritical Marks 0 BMP Inherited + {0x1AB0, 0x1AFF}, -- Combining Diacritical Marks Extended 0 BMP Inherited + {0x1DC0, 0x1DFF}, -- Combining Diacritical Marks Supplement 0 BMP Inherited + {0x20D0, 0x20FF}, -- Combining Diacritical Marks for Symbols 0 BMP Inherited + {0xFE20, 0xFE2F}, -- Combining Half Marks 0 BMP Cyrillic (2 characters), Inherited (14 characters) + -- Egyptian Hieroglyph Format Controls and Shorthand format Controls + {0x13430, 0x1345F}, -- Egyptian Hieroglyph Format Controls 1 SMP Egyptian Hieroglyphs + {0x1BCA0, 0x1BCAF}, -- Shorthand Format Controls 1 SMP Common + -- not sure how to deal with those https://en.wikipedia.org/wiki/Spacing_Modifier_Letters + {0x02B0, 0x02FF}, -- Spacing Modifier Letters 0 BMP Bopomofo (2 characters), Latin (14 characters), Common (64 characters) +} + +-- All characters have the same width as the first one +---@type CodePointRange[] +local same_width_blocks = { + {0x3400, 0x4DBF}, -- CJK Unified Ideographs Extension A 0 BMP Han + {0x4E00, 0x9FFF}, -- CJK Unified Ideographs 0 BMP Han + {0x20000, 0x2A6DF}, -- CJK Unified Ideographs Extension B 2 SIP Han + {0x2A700, 0x2B73F}, -- CJK Unified Ideographs Extension C 2 SIP Han + {0x2B740, 0x2B81F}, -- CJK Unified Ideographs Extension D 2 SIP Han + {0x2B820, 0x2CEAF}, -- CJK Unified Ideographs Extension E 2 SIP Han + {0x2CEB0, 0x2EBEF}, -- CJK Unified Ideographs Extension F 2 SIP Han + {0x2F800, 0x2FA1F}, -- CJK Compatibility Ideographs Supplement 2 SIP Han + {0x30000, 0x3134F}, -- CJK Unified Ideographs Extension G 3 TIP Han + {0x31350, 0x323AF}, -- CJK Unified Ideographs Extension H 3 TIP Han +} + +local width_length_ratio = 0.5 + +---@type integer, integer +local osd_width, osd_height = 100, 100 + +---Get byte count of utf-8 character at index i in str +---@param str string +---@param i integer? +---@return integer +local function utf8_char_bytes(str, i) + local char_byte = str:byte(i) + if char_byte < 0xC0 then return 1 + elseif char_byte < 0xE0 then return 2 + elseif char_byte < 0xF0 then return 3 + elseif char_byte < 0xF8 then return 4 + else return 1 end +end + +---Creates an iterator for an utf-8 encoded string +---Iterates over utf-8 characters instead of bytes +---@param str string +---@return fun(): integer?, string? +local function utf8_iter(str) + local byte_start = 1 + return function() + local start = byte_start + if #str < start then return nil end + local byte_count = utf8_char_bytes(str, start) + byte_start = start + byte_count + return start, str:sub(start, start + byte_count - 1) + end +end + +---Extract Unicode code point from utf-8 character at index i in str +---@param str string +---@param i integer +---@return integer +local function utf8_to_unicode(str, i) + local byte_count = utf8_char_bytes(str, i) + local char_byte = str:byte(i) + local unicode = char_byte + if byte_count ~= 1 then + local shift = 2 ^ (8 - byte_count) + char_byte = char_byte - math.floor(0xFF / shift) * shift + unicode = char_byte * (2 ^ 6) ^ (byte_count - 1) + end + for j = 2, byte_count do + char_byte = str:byte(i + j - 1) - 0x80 + unicode = unicode + char_byte * (2 ^ 6) ^ (byte_count - j) + end + return round(unicode) +end + +---Convert Unicode code point to utf-8 string +---@param unicode integer +---@return string? +local function unicode_to_utf8(unicode) + if unicode < 0x80 then return string.char(unicode) + else + local byte_count + if unicode < 0x800 then byte_count = 2 + elseif unicode < 0x10000 then byte_count = 3 + elseif unicode < 0x110000 then byte_count = 4 + else return end -- too big + + local res = {} + local shift = 2 ^ 6 + local after_shift = unicode + for _ = byte_count, 2, -1 do + local before_shift = after_shift + after_shift = math.floor(before_shift / shift) + table.insert(res, 1, before_shift - after_shift * shift + 0x80) + end + shift = 2 ^ (8 - byte_count) + table.insert(res, 1, after_shift + math.floor(0xFF / shift) * shift) + ---@diagnostic disable-next-line: deprecated + return string.char(unpack(res)) + end +end + +---Update osd resolution if valid +---@param width integer +---@param height integer +local function update_osd_resolution(width, height) + if width > 0 and height > 0 then osd_width, osd_height = width, height end +end + +mp.observe_property('osd-dimensions', 'native', function (_, dim) + if dim then update_osd_resolution(dim.w, dim.h) end +end) + +local measure_bounds +do + local text_osd = mp.create_osd_overlay("ass-events") + text_osd.compute_bounds, text_osd.hidden = true, true + + ---@param ass_text string + ---@return integer, integer, integer, integer + measure_bounds = function(ass_text) + update_osd_resolution(mp.get_osd_size()) + text_osd.res_x, text_osd.res_y = osd_width, osd_height + text_osd.data = ass_text + local res = text_osd:update() + return res.x0, res.y0, res.x1, res.y1 + end +end + +local normalized_text_width +do + ---@type {wrap: integer; bold: boolean; italic: boolean, rotate: number; size: number} + local bounds_opts = {wrap = 2, bold = false, italic = false, rotate = 0, size = 0} + + ---Measure text width and normalize to a font size of 1 + ---text has to be ass safe + ---@param text string + ---@param size number + ---@param bold boolean + ---@param italic boolean + ---@param horizontal boolean + ---@return number, integer + normalized_text_width = function(text, size, bold, italic, horizontal) + bounds_opts.bold, bounds_opts.italic, bounds_opts.rotate = bold, italic, horizontal and 0 or -90 + local x1, y1 = nil, nil + size = size / 0.8 + -- prevent endless loop + local repetitions_left = 5 + repeat + size = size * 0.8 + bounds_opts.size = size + local ass = assdraw.ass_new() + ass:txt(0, 0, horizontal and 7 or 1, text, bounds_opts) + _, _, x1, y1 = measure_bounds(ass.text) + repetitions_left = repetitions_left - 1 + -- make sure nothing got clipped + until (x1 and x1 < osd_width and y1 < osd_height) or repetitions_left == 0 + local width = (repetitions_left == 0 and not x1) and 0 or (horizontal and x1 or y1) + return width / size, horizontal and osd_width or osd_height + end +end + +---Estimates character length based on utf8 byte count +---1 character length is roughly the size of a latin character +---@param char string +---@return number +local function char_length(char) + return #char > 2 and 2 or 1 +end + +---Estimates string length based on utf8 byte count +---Note: Making a string in the iterator with the character is a waste here, +---but as this function is only used when measuring whole string widths it's fine +---@param text string +---@return number +local function text_length(text) + if not text or text == '' then return 0 end + local text_length = 0 + for _, char in utf8_iter(tostring(text)) do text_length = text_length + char_length(char) end + return text_length +end + +---Finds the best orientation of text on screen and returns the estimated max size +---and if the text should be drawn horizontally +---@param text string +---@return number, boolean +local function fit_on_screen(text) + local estimated_width = text_length(text) * width_length_ratio + if osd_width >= osd_height then + -- Fill the screen as much as we can, bigger is more accurate. + return math.min(osd_width / estimated_width, osd_height), true + else + return math.min(osd_height / estimated_width, osd_width), false + end +end + +---Gets next stage from cache +---@param cache {[any]: table} +---@param value any +local function get_cache_stage(cache, value) + local stage = cache[value] + if not stage then + stage = {} + cache[value] = stage + end + return stage +end + +---Is measured resolution sufficient +---@param px integer +---@return boolean +local function no_remeasure_required(px) + return px >= 800 or (px * 1.1 >= osd_width and px * 1.1 >= osd_height) +end + +local character_width +do + ---@type {[boolean]: {[string]: {[1]: number, [2]: integer}}} + local char_width_cache = {} + + ---Get measured width of character + ---@param char string + ---@param bold boolean + ---@return number, integer + character_width = function(char, bold) + ---@type {[string]: {[1]: number, [2]: integer}} + local char_widths = get_cache_stage(char_width_cache, bold) + local width_px = char_widths[char] + if width_px and no_remeasure_required(width_px[2]) then return width_px[1], width_px[2] end + + local unicode = utf8_to_unicode(char, 1) + for _, block in ipairs(zero_width_blocks) do + if unicode >= block[1] and unicode <= block[2] then + char_widths[char] = {0, INFINITY} + return 0, INFINITY + end + end + + local measured_char = nil + for _, block in ipairs(same_width_blocks) do + if unicode >= block[1] and unicode <= block[2] then + measured_char = unicode_to_utf8(block[1]) + width_px = char_widths[measured_char] + if width_px and no_remeasure_required(width_px[2]) then + char_widths[char] = width_px + return width_px[1], width_px[2] + end + break + end + end + + if not measured_char then measured_char = char end + -- half as many repetitions for wide characters + local char_count = 10 / char_length(char) + local max_size, horizontal = fit_on_screen(measured_char:rep(char_count)) + local size = math.min(max_size * 0.9, 50) + char_count = math.min(math.floor(char_count * max_size / size * 0.8), 100) + local enclosing_char, enclosing_width, next_char_count = '|', 0, char_count + if measured_char == enclosing_char then enclosing_char = '' + else enclosing_width = 2 * character_width(enclosing_char, bold) end + local width_ratio, width, px = nil, nil, nil + repeat + char_count = next_char_count + local str = enclosing_char .. measured_char:rep(char_count) .. enclosing_char + width, px = normalized_text_width(str, size, bold, false, horizontal) + width = width - enclosing_width + width_ratio = width * size / (horizontal and osd_width or osd_height) + next_char_count = math.min(math.floor(char_count / width_ratio * 0.9), 100) + until width_ratio < 0.05 or width_ratio > 0.5 or char_count == next_char_count + width = width / char_count + + width_px = {width, px} + if char ~= measured_char then char_widths[measured_char] = width_px end + char_widths[char] = width_px + return width, px + end +end + +---Calculate text width from individual measured characters +---@param text string|number +---@param bold boolean +---@return number, integer +local function character_based_width(text, bold) + local max_width = 0 + local min_px = INFINITY + for line in tostring(text):gmatch("([^\n]*)\n?") do + local total_width = 0 + for _, char in utf8_iter(line) do + local width, px = character_width(char, bold) + total_width = total_width + width + if px < min_px then min_px = px end + end + if total_width > max_width then max_width = total_width end + end + return max_width, min_px +end + +---Measure width of whole text +---@param text string|number +---@param bold boolean +---@param italic boolean +---@return number, integer +local function whole_text_width(text, bold, italic) + text = tostring(text) + local size, horizontal = fit_on_screen(text) + return normalized_text_width(ass_escape(text), size * 0.9, bold, italic, horizontal) +end + +---Scale normalized width to real width based on font size and italic +---@param opts {size: number; italic?: boolean} +---@return number, number +local function opts_factor_offset(opts) + return opts.size, opts.italic and opts.size * 0.2 or 0 +end + +---Scale normalized width to real width based on font size and italic +---@param opts {size: number; italic?: boolean} +---@return number +local function normalized_to_real(width, opts) + local factor, offset = opts_factor_offset(opts) + return factor * width + offset +end + +do + ---@type {[boolean]: {[boolean]: {[string|number]: {[1]: number, [2]: integer}}}} | {[boolean]: {[string|number]: {[1]: number, [2]: integer}}} + local width_cache = {} + + ---Calculate width of text with the given opts + ---@param text string|number + ---@return number + ---@param opts {size: number; bold?: boolean; italic?: boolean} + function text_width(text, opts) + if not text or text == '' then return 0 end + + ---@type boolean, boolean + local bold, italic = opts.bold or options.font_bold, opts.italic or false + + if options.text_width_estimation then + ---@type {[string|number]: {[1]: number, [2]: integer}} + local text_width = get_cache_stage(width_cache, bold) + local width_px = text_width[text] + if width_px and no_remeasure_required(width_px[2]) then return normalized_to_real(width_px[1], opts) end + + local width, px = character_based_width(text, bold) + width_cache[bold][text] = {width, px} + return normalized_to_real(width, opts) + else + ---@type {[string|number]: {[1]: number, [2]: integer}} + local text_width = get_cache_stage(get_cache_stage(width_cache, bold), italic) + local width_px = text_width[text] + if width_px and no_remeasure_required(width_px[2]) then return width_px[1] * opts.size end + + local width, px = whole_text_width(text, bold, italic) + width_cache[bold][italic][text] = {width, px} + return width * opts.size + end + end +end + +do + ---@type {[string]: string} + local cache = {} + + ---Get width of formatted timestamp as if all the digits were replaced with 0 + ---@param timestamp string + ---@param opts {size: number; bold?: boolean; italic?: boolean} + ---@return number + function timestamp_width(timestamp, opts) + local substitute = cache[#timestamp] + if not substitute then + substitute = timestamp:gsub('%d', '0') + cache[#timestamp] = substitute + end + return text_width(substitute, opts) + end +end + +---Wrap the text at the closest opportunity to target_line_length +---@param text string +---@param opts {size: number; bold?: boolean; italic?: boolean} +---@param target_line_length number +---@return string +function wrap_text(text, opts, target_line_length) + local target_line_width = target_line_length * width_length_ratio * opts.size + local bold, scale_factor, scale_offset = opts.bold or false, opts_factor_offset(opts) + local wrap_at_chars = {' ', ' ', '-', '–'} + local remove_when_wrap = {' ', ' '} + local lines = {} + for text_line in text:gmatch("([^\n]*)\n?") do + local line_width = scale_offset + local line_start = 1 + local before_end = nil + local before_width = scale_offset + local before_line_start = 0 + local before_removed_width = 0 + for char_start, char in utf8_iter(text_line) do + local char_end = char_start + #char - 1 + local can_wrap = false + for _, c in ipairs(wrap_at_chars) do + if char == c then + can_wrap = true + break + end + end + local char_width = character_width(char, bold) * scale_factor + line_width = line_width + char_width + if can_wrap or (char_end == #text_line) then + local remove = false + for _, c in ipairs(remove_when_wrap) do + if char == c then + remove = true + break + end + end + local line_width_after_remove = line_width - (remove and char_width or 0) + if line_width_after_remove < target_line_width then + before_end = remove and char_start - 1 or char_end + before_width = line_width_after_remove + before_line_start = char_end + 1 + before_removed_width = remove and char_width or 0 + else + if (target_line_width - before_width) < + (line_width_after_remove - target_line_width) then + lines[#lines + 1] = text_line:sub(line_start, before_end) + line_start = before_line_start + line_width = line_width - before_width - before_removed_width + scale_offset + else + lines[#lines + 1] = text_line:sub(line_start, remove and char_start - 1 or char_end) + line_start = char_end + 1 + line_width = scale_offset + end + before_end = line_start + before_width = scale_offset + end + end + end + if #text_line >= line_start then lines[#lines + 1] = text_line:sub(line_start) + elseif text_line == '' then lines[#lines + 1] = '' end + end + return table.concat(lines, '\n') +end diff --git a/multimedia/.config/mpv/scripts/uosc_shared/lib/utils.lua b/multimedia/.config/mpv/scripts/uosc_shared/lib/utils.lua new file mode 100644 index 0000000..f64485c --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/lib/utils.lua @@ -0,0 +1,609 @@ +--[[ UI specific utilities that might or might not depend on its state or options ]] + +-- Sorting comparator close to (but not exactly) how file explorers sort files. +sort_filenames = (function() + local symbol_order + local default_order + + if state.platform == 'windows' then + symbol_order = { + ['!'] = 1, ['#'] = 2, ['$'] = 3, ['%'] = 4, ['&'] = 5, ['('] = 6, [')'] = 6, [','] = 7, + ['.'] = 8, ["'"] = 9, ['-'] = 10, [';'] = 11, ['@'] = 12, ['['] = 13, [']'] = 13, ['^'] = 14, + ['_'] = 15, ['`'] = 16, ['{'] = 17, ['}'] = 17, ['~'] = 18, ['+'] = 19, ['='] = 20, + } + default_order = 21 + else + symbol_order = { + ['`'] = 1, ['^'] = 2, ['~'] = 3, ['='] = 4, ['_'] = 5, ['-'] = 6, [','] = 7, [';'] = 8, + ['!'] = 9, ["'"] = 10, ['('] = 11, [')'] = 11, ['['] = 12, [']'] = 12, ['{'] = 13, ['}'] = 14, + ['@'] = 15, ['$'] = 16, ['*'] = 17, ['&'] = 18, ['%'] = 19, ['+'] = 20, ['.'] = 22, ['#'] = 23, + } + default_order = 21 + end + + -- Alphanumeric sorting for humans in Lua + -- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua + local function pad_number(n, d) + return #d > 0 and ("%03d%s%.12f"):format(#n, n, tonumber(d) / (10 ^ #d)) + or ("%03d%s"):format(#n, n) + end + + --- In place sorting of filenames + ---@param filenames string[] + return function(filenames) + local tuples = {} + for i, filename in ipairs(filenames) do + local first_char = filename:sub(1, 1) + local order = symbol_order[first_char] or default_order + local formatted = filename:lower():gsub('0*(%d+)%.?(%d*)', pad_number) + tuples[i] = {order, formatted, filename} + end + table.sort(tuples, function(a, b) + if a[1] ~= b[1] then return a[1] < b[1] end + return a[2] == b[2] and #b[3] < #a[3] or a[2] < b[2] + end) + for i, tuple in ipairs(tuples) do filenames[i] = tuple[3] end + end +end)() + +-- Creates in-between frames to animate value from `from` to `to` numbers. +---@param from number +---@param to number|fun():number +---@param setter fun(value: number) +---@param factor_or_callback? number|fun() +---@param callback? fun() Called either on animation end, or when animation is killed. +function tween(from, to, setter, factor_or_callback, callback) + local factor = factor_or_callback + if type(factor_or_callback) == 'function' then callback = factor_or_callback end + if type(factor) ~= 'number' then factor = 0.3 end + + local current, done, timeout = from, false, nil + local get_to = type(to) == 'function' and to or function() return to --[[@as number]] end + local cutoff = math.abs(get_to() - from) * 0.01 + + local function finish() + if not done then + done = true + timeout:kill() + if callback then callback() end + end + end + + local function tick() + local to = get_to() + current = current + ((to - current) * factor) + local is_end = math.abs(to - current) <= cutoff + setter(is_end and to or current) + request_render() + if is_end then finish() + else timeout:resume() end + end + + timeout = mp.add_timeout(state.render_delay, tick) + tick() + + return finish +end + +---@param point {x: number; y: number} +---@param rect {ax: number; ay: number; bx: number; by: number} +function get_point_to_rectangle_proximity(point, rect) + local dx = math.max(rect.ax - point.x, 0, point.x - rect.bx) + local dy = math.max(rect.ay - point.y, 0, point.y - rect.by) + return math.sqrt(dx * dx + dy * dy) +end + +---@param point_a {x: number; y: number} +---@param point_b {x: number; y: number} +function get_point_to_point_proximity(point_a, point_b) + local dx, dy = point_a.x - point_b.x, point_a.y - point_b.y + return math.sqrt(dx * dx + dy * dy) +end + +-- Call function with args if it exists +function call_maybe(fn, ...) + if type(fn) == 'function' then fn(...) end +end + +-- Extracts the properties used by property expansion of that string. +---@param str string +---@param res { [string] : boolean } | nil +---@return { [string] : boolean } +function get_expansion_props(str, res) + res = res or {} + for str in str:gmatch('%$(%b{})') do + local name, str = str:match('^{[?!]?=?([^:]+):?(.*)}$') + if name then + local s = name:find('==') or nil + if s then name = name:sub(0, s - 1) end + res[name] = true + if str and str ~= '' then get_expansion_props(str, res) end + end + end + return res +end + +-- Escape a string for verbatim display on the OSD. +---@param str string +function ass_escape(str) + -- There is no escape for '\' in ASS (I think?) but '\' is used verbatim if + -- it isn't followed by a recognized character, so add a zero-width + -- non-breaking space + str = str:gsub('\\', '\\\239\187\191') + str = str:gsub('{', '\\{') + str = str:gsub('}', '\\}') + -- Precede newlines with a ZWNBSP to prevent ASS's weird collapsing of + -- consecutive newlines + str = str:gsub('\n', '\239\187\191\\N') + -- Turn leading spaces into hard spaces to prevent ASS from stripping them + str = str:gsub('\\N ', '\\N\\h') + str = str:gsub('^ ', '\\h') + return str +end + +---@param seconds number +---@param max_seconds number|nil Trims unnecessary `00:` if time is not expected to reach it. +---@return string +function format_time(seconds, max_seconds) + local human = mp.format_time(seconds) + if options.time_precision > 0 then + local formatted = string.format('%.' .. options.time_precision .. 'f', math.abs(seconds) % 1) + human = human .. '.' .. string.sub(formatted, 3) + end + if max_seconds then + local trim_length = (max_seconds < 60 and 7 or (max_seconds < 3600 and 4 or 0)) + if trim_length > 0 then + local has_minus = seconds < 0 + human = string.sub(human, trim_length + (has_minus and 1 or 0)) + if has_minus then human = '-' .. human end + end + end + return human +end + +---@param opacity number 0-1 +function opacity_to_alpha(opacity) + return 255 - math.ceil(255 * opacity) +end + +path_separator = (function() + local os_separator = state.platform == 'windows' and '\\' or '/' + + -- Get appropriate path separator for the given path. + ---@param path string + ---@return string + return function(path) + return path:sub(1, 2) == '\\\\' and '\\' or os_separator + end +end)() + +-- Joins paths with the OS aware path separator or UNC separator. +---@param p1 string +---@param p2 string +---@return string +function join_path(p1, p2) + local p1, separator = trim_trailing_separator(p1) + -- Prevents joining drive letters with a redundant separator (`C:\\foo`), + -- as `trim_trailing_separator()` doesn't trim separators from drive letters. + return p1:sub(#p1) == separator and p1 .. p2 or p1 .. separator.. p2 +end + +-- Check if path is absolute. +---@param path string +---@return boolean +function is_absolute(path) + if path:sub(1, 2) == '\\\\' then return true + elseif state.platform == 'windows' then return path:find('^%a+:') ~= nil + else return path:sub(1, 1) == '/' end +end + +-- Ensure path is absolute. +---@param path string +---@return string +function ensure_absolute(path) + if is_absolute(path) then return path end + return join_path(state.cwd, path) +end + +-- Remove trailing slashes/backslashes. +---@param path string +---@return string path, string trimmed_separator_type +function trim_trailing_separator(path) + local separator = path_separator(path) + path = trim_end(path, separator) + if state.platform == 'windows' then + -- Drive letters on windows need trailing backslash + if path:sub(#path) == ':' then path = path .. '\\' end + else + if path == '' then path = '/' end + end + return path, separator +end + +-- Ensures path is absolute, remove trailing slashes/backslashes. +-- Lightweight version of normalize_path for performance critical parts. +---@param path string +---@return string +function normalize_path_lite(path) + if not path or is_protocol(path) then return path end + path = trim_trailing_separator(ensure_absolute(path)) + return path +end + +-- Ensures path is absolute, remove trailing slashes/backslashes, normalization of path separators and deduplication. +---@param path string +---@return string +function normalize_path(path) + if not path or is_protocol(path) then return path end + + path = ensure_absolute(path) + local is_unc = path:sub(1, 2) == '\\\\' + if state.platform == 'windows' or is_unc then path = path:gsub('/', '\\') end + path = trim_trailing_separator(path) + + --Deduplication of path separators + if is_unc then path = path:gsub('(.\\)\\+', '%1') + elseif state.platform == 'windows' then path = path:gsub('\\\\+', '\\') + else path = path:gsub('//+', '/') end + + return path +end + +-- Check if path is a protocol, such as `http://...`. +---@param path string +function is_protocol(path) + return type(path) == 'string' and (path:find('^%a[%a%d-_]+://') ~= nil or path:find('^%a[%a%d-_]+:\\?') ~= nil) +end + +---@param path string +---@param extensions string[] Lowercase extensions without the dot. +function has_any_extension(path, extensions) + local path_last_dot_index = string_last_index_of(path, '.') + if not path_last_dot_index then return false end + local path_extension = path:sub(path_last_dot_index + 1):lower() + for _, extension in ipairs(extensions) do + if path_extension == extension then return true end + end + return false +end + +---@return string +function get_default_directory() + return mp.command_native({'expand-path', options.default_directory}) +end + +-- Serializes path into its semantic parts. +---@param path string +---@return nil|{path: string; is_root: boolean; dirname?: string; basename: string; filename: string; extension?: string;} +function serialize_path(path) + if not path or is_protocol(path) then return end + + local normal_path = normalize_path_lite(path) + local dirname, basename = utils.split_path(normal_path) + if basename == '' then basename, dirname = dirname:sub(1, #dirname - 1), nil end + local dot_i = string_last_index_of(basename, '.') + + return { + path = normal_path, + is_root = dirname == nil, + dirname = dirname, + basename = basename, + filename = dot_i and basename:sub(1, dot_i - 1) or basename, + extension = dot_i and basename:sub(dot_i + 1) or nil, + } +end + +-- Reads items in directory and splits it into directories and files tables. +---@param path string +---@param allowed_types? string[] Filter `files` table to contain only files with these extensions. +---@return string[]|nil files +---@return string[]|nil directories +function read_directory(path, allowed_types) + local items, error = utils.readdir(path, 'all') + + if not items then + msg.error('Reading files from "' .. path .. '" failed: ' .. error) + return nil, nil + end + + local files, directories = {}, {} + + for _, item in ipairs(items) do + if item ~= '.' and item ~= '..' then + local info = utils.file_info(join_path(path, item)) + if info then + if info.is_file then + if not allowed_types or has_any_extension(item, allowed_types) then + files[#files + 1] = item + end + else directories[#directories + 1] = item end + end + end + end + + return files, directories +end + +-- Returns full absolute paths of files in the same directory as `file_path`, +-- and index of the current file in the table. +-- Returned table will always contain `file_path`, regardless of `allowed_types`. +---@param file_path string +---@param allowed_types? string[] Filter adjacent file types. Does NOT filter out the `file_path`. +function get_adjacent_files(file_path, allowed_types) + local current_meta = serialize_path(file_path) + if not current_meta then return end + local files = read_directory(current_meta.dirname) + if not files then return end + sort_filenames(files) + local current_file_index + local paths = {} + for _, file in ipairs(files) do + local is_current_file = current_meta.basename == file + if is_current_file or not allowed_types or has_any_extension(file, allowed_types) then + paths[#paths + 1] = join_path(current_meta.dirname, file) + if is_current_file then current_file_index = #paths end + end + end + if not current_file_index then return end + return paths, current_file_index +end + +-- Navigates in a list, using delta or, when `state.shuffle` is enabled, +-- randomness to determine the next item. Loops around if `loop-playlist` is enabled. +---@param list table +---@param current_index number +---@param delta number +function decide_navigation_in_list(list, current_index, delta) + if #list < 2 then return #list, list[#list] end + + if state.shuffle then + local new_index = current_index + math.randomseed(os.time()) + while current_index == new_index do new_index = math.random(#list) end + return new_index, list[new_index] + end + + local new_index = current_index + delta + if mp.get_property_native('loop-playlist') then + if new_index > #list then new_index = new_index % #list + elseif new_index < 1 then new_index = #list - new_index end + elseif new_index < 1 or new_index > #list then + return + end + + return new_index, list[new_index] +end + +---@param delta number +function navigate_directory(delta) + if not state.path or is_protocol(state.path) then return false end + local paths, current_index = get_adjacent_files(state.path, config.types.autoload) + if paths and current_index then + local _, path = decide_navigation_in_list(paths, current_index, delta) + if path then mp.commandv('loadfile', path) return true end + end + return false +end + +---@param delta number +function navigate_playlist(delta) + local playlist, pos = mp.get_property_native('playlist'), mp.get_property_native('playlist-pos-1') + if playlist and #playlist > 1 and pos then + local index = decide_navigation_in_list(playlist, pos, delta) + if index then mp.commandv('playlist-play-index', index - 1) return true end + end + return false +end + +---@param delta number +function navigate_item(delta) + if state.has_playlist then return navigate_playlist(delta) else return navigate_directory(delta) end +end + +-- Can't use `os.remove()` as it fails on paths with unicode characters. +-- Returns `result, error`, result is table of: +-- `status:number(<0=error), stdout, stderr, error_string, killed_by_us:boolean` +---@param path string +function delete_file(path) + if state.platform == 'windows' then + if options.use_trash then + local ps_code = [[ + Add-Type -AssemblyName Microsoft.VisualBasic + [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('__path__', 'OnlyErrorDialogs', 'SendToRecycleBin') + ]] + + local escaped_path = string.gsub(path, "'", "''") + escaped_path = string.gsub(escaped_path, "’", "’’") + escaped_path = string.gsub(escaped_path, "%%", "%%%%") + ps_code = string.gsub(ps_code, "__path__", escaped_path) + args = { 'powershell', '-NoProfile', '-Command', ps_code } + else + args = { 'cmd', '/C', 'del', path } + end + else + if options.use_trash then + --On Linux and Macos the app trash-cli/trash must be installed first. + args = { 'trash', path } + else + args = { 'rm', path } + end + end + return mp.command_native({ + name = 'subprocess', + args = args, + playback_only = false, + capture_stdout = true, + capture_stderr = true, + }) +end + +function serialize_chapter_ranges(normalized_chapters) + local ranges = {} + local simple_ranges = { + {name = 'openings', patterns = { + '^op ', '^op$', ' op$', + '^opening$', ' opening$' + }, requires_next_chapter = true}, + {name = 'intros', patterns = { + '^intro$', ' intro$', + '^avant$', '^prologue$' + }, requires_next_chapter = true}, + {name = 'endings', patterns = { + '^ed ', '^ed$', ' ed$', + '^ending ', '^ending$', ' ending$', + }}, + {name = 'outros', patterns = { + '^outro$', ' outro$', + '^closing$', '^closing ', + '^preview$', '^pv$', + }}, + } + local sponsor_ranges = {} + + -- Extend with alt patterns + for _, meta in ipairs(simple_ranges) do + local alt_patterns = config.chapter_ranges[meta.name] and config.chapter_ranges[meta.name].patterns + if alt_patterns then meta.patterns = itable_join(meta.patterns, alt_patterns) end + end + + -- Clone chapters + local chapters = {} + for i, normalized in ipairs(normalized_chapters) do chapters[i] = table_shallow_copy(normalized) end + + for i, chapter in ipairs(chapters) do + -- Simple ranges + for _, meta in ipairs(simple_ranges) do + if config.chapter_ranges[meta.name] then + local match = itable_find(meta.patterns, function(p) return chapter.lowercase_title:find(p) end) + if match then + local next_chapter = chapters[i + 1] + if next_chapter or not meta.requires_next_chapter then + ranges[#ranges + 1] = table_assign({ + start = chapter.time, + ['end'] = next_chapter and next_chapter.time or INFINITY, + }, config.chapter_ranges[meta.name]) + end + end + end + end + + -- Sponsor blocks + if config.chapter_ranges.ads then + local id = chapter.lowercase_title:match('segment start *%(([%w]%w-)%)') + if id then -- ad range from sponsorblock + for j = i + 1, #chapters, 1 do + local end_chapter = chapters[j] + local end_match = end_chapter.lowercase_title:match('segment end *%(' .. id .. '%)') + if end_match then + local range = table_assign({ + start_chapter = chapter, end_chapter = end_chapter, + start = chapter.time, ['end'] = end_chapter.time, + }, config.chapter_ranges.ads) + ranges[#ranges + 1], sponsor_ranges[#sponsor_ranges + 1] = range, range + end_chapter.is_end_only = true + break + end + end -- single chapter for ad + elseif not chapter.is_end_only and + (chapter.lowercase_title:find('%[sponsorblock%]:') or chapter.lowercase_title:find('^sponsors?')) then + local next_chapter = chapters[i + 1] + ranges[#ranges + 1] = table_assign({ + start = chapter.time, + ['end'] = next_chapter and next_chapter.time or INFINITY, + }, config.chapter_ranges.ads) + end + end + end + + -- Fix overlapping sponsor block segments + for index, range in ipairs(sponsor_ranges) do + local next_range = sponsor_ranges[index + 1] + if next_range then + local delta = next_range.start - range['end'] + if delta < 0 then + local mid_point = range['end'] + delta / 2 + range['end'], range.end_chapter.time = mid_point - 0.01, mid_point - 0.01 + next_range.start, next_range.start_chapter.time = mid_point, mid_point + end + end + end + table.sort(chapters, function(a, b) return a.time < b.time end) + + return chapters, ranges +end + +-- Ensures chapters are in chronological order +function normalize_chapters(chapters) + if not chapters then return {} end + -- Ensure chronological order + table.sort(chapters, function(a, b) return a.time < b.time end) + -- Ensure titles + for index, chapter in ipairs(chapters) do + chapter.title = chapter.title or ('Chapter ' .. index) + chapter.lowercase_title = chapter.title:lower() + end + return chapters +end + +function serialize_chapters(chapters) + chapters = normalize_chapters(chapters) + if not chapters then return end + --- timeline font size isn't accessible here, so normalize to size 1 and then scale during rendering + local opts = {size = 1, bold = true} + for index, chapter in ipairs(chapters) do + chapter.index = index + chapter.title_wrapped = wrap_text(chapter.title, opts, 25) + chapter.title_wrapped_width = text_width(chapter.title_wrapped, opts) + chapter.title_wrapped = ass_escape(chapter.title_wrapped) + end + return chapters +end + +--[[ RENDERING ]] + +function render() + if not display.initialized then return end + state.render_last_time = mp.get_time() + + cursor.reset_handlers() + + -- Actual rendering + local ass = assdraw.ass_new() + + for _, element in Elements:ipairs() do + if element.enabled then + local result = element:maybe('render') + if result then + ass:new_event() + ass:merge(result) + end + end + end + + cursor.decide_keybinds() + + -- submit + if osd.res_x == display.width and osd.res_y == display.height and osd.data == ass.text then + return + end + + osd.res_x = display.width + osd.res_y = display.height + osd.data = ass.text + osd.z = 2000 + osd:update() + + update_margins() +end + +-- Request that render() is called. +-- The render is then either executed immediately, or rate-limited if it was +-- called a small time ago. +state.render_timer = mp.add_timeout(0, render) +state.render_timer:kill() +function request_render() + if state.render_timer:is_enabled() then return end + local timeout = math.max(0, state.render_delay - (mp.get_time() - state.render_last_time)) + state.render_timer.timeout = timeout + state.render_timer:resume() +end diff --git a/multimedia/.config/mpv/scripts/uosc_shared/main.lua b/multimedia/.config/mpv/scripts/uosc_shared/main.lua new file mode 100644 index 0000000..323027f --- /dev/null +++ b/multimedia/.config/mpv/scripts/uosc_shared/main.lua @@ -0,0 +1,5 @@ +--[[ +File required for compatibility between mpv: +- 0.32 - doesn't support `dir/main.lua`, so we need `uosc.lua` in root +- 0.33 - requires `main.lua` in directories +]]