-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathpause_indicator_lite.lua
More file actions
376 lines (329 loc) · 14.2 KB
/
pause_indicator_lite.lua
File metadata and controls
376 lines (329 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
--[[
A simple script that shows a pause indicator, on pause
https://github.com/Samillion/ModernZ/tree/main/extras/pause-indicator-lite
--]]
local options = {
-- indicator icon type
indicator_icon = "pause", -- indicator icon type. "pause", "play"
indicator_stay = true, -- keep indicator visibile during pause
indicator_timeout = 0.6, -- timeout (seconds) if indicator doesn't stay
indicator_pos = "middle_center", -- position of indicator. top_left, top_right, top_center
-- also: middle_*, bottom_* same as top_* (ie: bottom_right)
-- keybind
keybind_allow = false, -- allow keybind to toggle pause
keybind_set = "ctrl+mbtn_left", -- the used keybind to toggle pause
keybind_mode = "onpause", -- mode to activate keybind. "onpause", "always"
keybind_eof_disable = true, -- disable keybind on eof (end of file)
-- icon colors & opacity
icon_color = "#FFFFFF", -- icon fill color
icon_border_color = "#111111", -- icon border color
icon_border_width = 1.5, -- icon border width
icon_opacity = 30, -- icon opacity (0-100)
-- pause icon
rectangles_width = 20, -- width of rectangles
rectangles_height = 60, -- height of rectangles
rectangles_spacing = 12, -- spacing between the two rectangles
-- play icon
triangle_width = 60, -- width of triangle
triangle_height = 60, -- height of triangle
-- best with pause icon
flash_play_icon = true, -- flash play icon on unpause
flash_icon_timeout = 0.3, -- timeout (seconds) for flash icon
-- icon style used in ModernZ osc
icon_theme = "fluent", -- set icon theme. accepts "fluent" or "material"
theme_style = "outline", -- set theme style. accepts "outline" or "filled"
themed_icons = false, -- requires fonts/modernz-icons.ttf
themed_icon_size = 80, -- themed icon size
-- mute options
mute_indicator = false, -- show a mute indicator
mute_indicator_pos = "top_right", -- position of mute indicator. top_left, top_right, top_center
-- also: middle_*, bottom_* same as top_* (ie: bottom_right)
mute_icon_size = 35, -- size of the mute speaker icon
}
local msg = require "mp.msg"
require 'mp.options'.read_options(options, "pause_indicator_lite")
local state = {
indicator_overlay = mp.create_osd_overlay("ass-events"),
flash_overlay = mp.create_osd_overlay("ass-events"),
mute_overlay = mp.create_osd_overlay("ass-events"),
aspect = 0,
indicator_pos = 5,
mute_pos = 9,
indicator_visible = false,
mute_visible = false,
indicator_timer = nil,
flash_timer = nil,
paused = false,
toggled = false,
eof = false,
keybinds_registered = false,
indicator_dismissed = false,
}
local icon_theme = {
fluent = {
filled = {
pause_icon = "fluent_pause_filled",
play_icon = "fluent_play_arrow_filled",
mute_icon = "fluent_no_sound_filled"
},
outline = {
pause_icon = "fluent_pause",
play_icon = "fluent_play_arrow",
mute_icon = "fluent_no_sound"
}
},
material = {
filled = {
pause_icon = "material_pause_filled",
play_icon = "material_play_arrow_filled",
mute_icon = "material_volume_off_filled"
},
outline = {
pause_icon = "material_pause",
play_icon = "material_play_arrow",
mute_icon = "material_volume_off"
}
},
font = "modernz-icons"
}
-- convert color from hex (adjusted from mpv/osc.lua)
local function convert_color(color)
if color:find("^#%x%x%x%x%x%x$") == nil then
msg.warn("'" .. color .. "' is not a valid color, using default '#FFFFFF'")
return "FFFFFF" -- color fallback
end
return color:sub(6,7) .. color:sub(4,5) .. color:sub(2,3)
end
-- convert percentage opacity (0-100) to ASS alpha values
local function convert_opacity(value)
value = math.max(0, math.min(100, value))
return string.format("%02X", (255 - (value * 2.55)))
end
local icon_style = {
color = convert_color(options.icon_color),
border_color = convert_color(options.icon_border_color),
opacity = convert_opacity(options.icon_opacity),
theme = icon_theme[options.icon_theme] and icon_theme[options.icon_theme][options.theme_style] or icon_theme["fluent"]["outline"],
font = icon_theme.font,
}
-- indicator position
local function icon_pos(pos_opt)
local pos = {
top_left = 7, top_center = 8, top_right = 9,
middle_left = 4, middle_center = 5, middle_right = 6,
bottom_left = 1, bottom_center = 2, bottom_right = 3,
}
return pos[(pos_opt or ""):lower()] or 5
end
state.indicator_pos = icon_pos(options.indicator_pos)
state.mute_pos = icon_pos(options.mute_indicator_pos)
-- prevent duplicate positions
if state.indicator_pos == state.mute_pos then
state.mute_pos = (state.mute_pos % 9) + 1
end
-- pause icon
local function draw_rectangles()
if options.themed_icons then
return string.format([[{\\rDefault\\an%s\\alpha&H%s\\bord%s\\1c&H%s&\\3c&H%s&\\fs%s\\fn%s}%s]],
state.indicator_pos, icon_style.opacity, options.icon_border_width, icon_style.color, icon_style.border_color, options.themed_icon_size, icon_style.font, icon_style.theme.pause_icon)
end
return string.format([[{\\rDefault\\p1\\an%s\\alpha&H%s\\bord%s\\1c&H%s&\\3c&H%s&}m 0 0 l %d 0 l %d %d l 0 %d m %d 0 l %d 0 l %d %d l %d %d{\\p0}]],
state.indicator_pos, icon_style.opacity, options.icon_border_width, icon_style.color, icon_style.border_color, options.rectangles_width, options.rectangles_width,
options.rectangles_height, options.rectangles_height, options.rectangles_width + options.rectangles_spacing,
options.rectangles_width * 2 + options.rectangles_spacing, options.rectangles_width * 2 + options.rectangles_spacing,
options.rectangles_height, options.rectangles_width + options.rectangles_spacing, options.rectangles_height)
end
-- play icon
local function draw_triangle()
if options.themed_icons then
return string.format([[{\\rDefault\\an%s\\alpha&H%s\\bord%s\\1c&H%s&\\3c&H%s&\\fs%s\\fn%s}%s]],
state.indicator_pos, icon_style.opacity, options.icon_border_width, icon_style.color, icon_style.border_color, options.themed_icon_size, icon_style.font, icon_style.theme.play_icon)
end
return string.format([[{\\rDefault\\p1\\an%s\\alpha&H%s\\bord%s\\1c&H%s&\\3c&H%s&}m 0 0 l %d %d l 0 %d{\\p0}]],
state.indicator_pos, icon_style.opacity, options.icon_border_width, icon_style.color, icon_style.border_color, options.triangle_width, options.triangle_height / 2, options.triangle_height)
end
-- mute icon
local function draw_mute()
if options.themed_icons then
return string.format([[{\\rDefault\\an%s\\alpha&H%s\\bord%s\\1c&H%s&\\3c&H%s&\\fs%s\\fn%s}%s]],
state.mute_pos, icon_style.opacity, options.icon_border_width,
icon_style.color, icon_style.border_color, options.mute_icon_size, icon_style.font, icon_style.theme.mute_icon)
end
-- path drawn on a 509.47x430.82 canvas
-- fscx/fscy are %, so (target / 509.47 * 100) scales to mute_icon_size
local scale = math.floor(options.mute_icon_size / 509.47 * 100 + 0.5)
-- from Fticons - MIT License
-- https://github.com/Financial-Times/fticons
local vol_mute = "{\\p1}" ..
-- bounding box
"m 0 0 m 509.47 430.82 " ..
-- speaker shape
"m 287.33 0 " .. "l 106.77 135.6 " .. "l 104.06 138.85 " .. "l 0 138.85 " ..
"l 0 292.05 " .. "l 104.06 292.05 " .. "l 106.77 295.3 " .. "l 287.33 430.82 " ..
"l 304.67 422.16 " .. "l 304.67 8.66 " .. "l 287.33 0 " ..
-- X mark
"m 487.07 305.01 " .. "l 509.47 282.6 " .. "l 442.27 215.4 " .. "l 509.47 148.2 " ..
"l 487.07 125.8 " .. "l 419.87 193 " .. "l 352.67 125.8 " .. "l 330.27 148.2 " ..
"l 397.47 215.4 " .. "l 330.27 282.6 " .. "l 352.67 305 " .. "l 419.87 237.8 " ..
"l 487.07 305 " .. "{\\p0}"
return string.format([[{\\rDefault\\an%s\\alpha&H%s\\bord%s\\1c&H%s&\\3c&H%s&\\fscx%s\\fscy%s}%s]],
state.mute_pos, icon_style.opacity, options.icon_border_width,
icon_style.color, icon_style.border_color, scale, scale, vol_mute)
end
local function kill_timer(key)
if state[key] then
state[key]:kill()
state[key] = nil
end
end
local function update_indicator(force)
if not state.aspect or state.aspect == 0 then return end
if not force and state.indicator_visible then
return
end
state.indicator_overlay:remove()
state.indicator_overlay.data = (options.indicator_icon == "play") and draw_triangle() or draw_rectangles()
state.indicator_overlay:update()
state.indicator_visible = true
if not options.indicator_stay then
kill_timer("indicator_timer")
state.indicator_timer = mp.add_timeout(options.indicator_timeout, function()
state.indicator_overlay:remove()
state.indicator_visible = false
state.indicator_dismissed = true
end)
end
end
local function update_flash_icon()
if not state.aspect or state.aspect == 0 or not options.flash_play_icon then return end
kill_timer("flash_timer")
state.flash_overlay:remove()
state.flash_overlay.data = draw_triangle()
state.flash_overlay:update()
state.flash_timer = mp.add_timeout(options.flash_icon_timeout, function()
state.flash_overlay:remove()
end)
end
local function update_mute_icon()
if not state.aspect or state.aspect == 0 then return end
state.mute_overlay:remove()
state.mute_overlay.data = draw_mute()
state.mute_overlay:update()
end
local function is_video()
local t = mp.get_property_native("current-tracks/video")
return t ~= nil and not t.image and not t.albumart
end
local function keybind_should_enable()
if options.keybind_eof_disable and state.eof then return false end
return options.keybind_mode == "always" or (options.keybind_mode == "onpause" and state.paused)
end
local function setup_keybinds()
if state.keybinds_registered then return end
mp.set_key_bindings({
{options.keybind_set, function()
mp.commandv("cycle", "pause")
end}
}, "pause-indicator", "force")
state.keybinds_registered = true
end
local pause_observer = function(_, paused)
state.paused = paused
if paused then
update_indicator()
state.toggled = true
kill_timer("flash_timer")
state.flash_overlay:remove()
if options.keybind_allow and options.keybind_mode == "onpause" then
if keybind_should_enable() then
mp.enable_key_bindings("pause-indicator", "allow-vo-dragging+allow-hide-cursor")
end
end
else
kill_timer("indicator_timer")
state.indicator_overlay:remove()
state.indicator_visible = false
state.indicator_dismissed = false
if state.toggled then
update_flash_icon()
state.toggled = false
end
if options.keybind_allow and options.keybind_mode == "onpause" then
mp.disable_key_bindings("pause-indicator")
end
end
end
local dimensions_observer = function()
local _, _, aspect = mp.get_osd_size()
if not aspect or aspect == 0 then return end
state.aspect = aspect
if state.paused and not state.indicator_visible and not state.indicator_dismissed then
update_indicator(true)
state.toggled = true
elseif state.indicator_visible then
update_indicator(true)
end
if state.mute_visible then
update_mute_icon()
end
end
local mute_observer = function(_, val)
if val then
update_mute_icon()
state.mute_visible = true
else
state.mute_overlay:remove()
state.mute_visible = false
end
end
local eof_observer = function(_, val)
state.eof = val
if val and options.keybind_eof_disable then
mp.disable_key_bindings("pause-indicator")
end
end
local function unobserve()
mp.unobserve_property(pause_observer)
mp.unobserve_property(dimensions_observer)
mp.unobserve_property(mute_observer)
mp.unobserve_property(eof_observer)
end
local function shutdown()
kill_timer("indicator_timer")
kill_timer("flash_timer")
state.flash_overlay:remove()
state.indicator_overlay:remove()
state.mute_overlay:remove()
state.indicator_visible = false
state.mute_visible = false
state.toggled = false
mp.disable_key_bindings("pause-indicator")
unobserve()
end
mp.register_event("file-loaded", function()
unobserve()
if is_video() then
local _, _, aspect = mp.get_osd_size()
state.aspect = aspect
state.eof = false
state.indicator_dismissed = false
state.paused = mp.get_property_bool("pause") or false
state.indicator_visible = false
mp.observe_property("pause", "bool", pause_observer)
mp.observe_property("osd-dimensions", "native", dimensions_observer)
if options.mute_indicator then
mp.observe_property("mute", "bool", mute_observer)
end
if options.keybind_allow then
mp.observe_property("eof-reached", "bool", eof_observer)
setup_keybinds()
if keybind_should_enable() then
mp.enable_key_bindings("pause-indicator", "allow-vo-dragging+allow-hide-cursor")
else
mp.disable_key_bindings("pause-indicator")
end
end
else
shutdown()
end
end)