Skip to content

Commit 07cc4c7

Browse files
authored
Merge pull request #182 from Samillion/dev_indicator_reducecalls
pause-indicator: add fallback color, reduce callbacks and only run with videos
2 parents 6c1b147 + 593f6fa commit 07cc4c7

File tree

2 files changed

+100
-116
lines changed

2 files changed

+100
-116
lines changed

extras/pause-indicator-lite/README.md

+21-22
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,33 @@
22
| ![pause-icon](https://github.com/user-attachments/assets/cd41333c-8fdd-4de9-8977-15eea95798dc) | ![play-icon](https://github.com/user-attachments/assets/0d1671f8-9b1b-4f10-ade3-82d1748b2d93) |
33
|:---:|:---:|
44

5-
A simple script that displays an indicator on pause, with options to adjust icon, color, height, width, opacity and whether to toggle pause with a keybind or not.
5+
A simple script that displays an indicator on pause, with options to adjust icon type, color, height, width, opacity and whether to toggle pause with a keybind or not.
66

77
I only decided to write this because the ones I found were either too complicated or too simple. The alternatives are great, this one just meets my simple use case scenario.
88

99
**Script:** [Pause Indicator Lite](./pause_indicator_lite.lua)
1010

1111
### Indicator Options
12-
Below is the full list for indicator options and their default values. To adjust them, simply change their values in `local opts` within the script.
13-
14-
| Option | Value | Description |
15-
|--------------------------|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
16-
| `indicator_icon` | "pause" | Which icon to show as indicator `pause`, `play` |
17-
| `indicator_stay` | true | Should the pause indicator stay visible during pause? |
18-
| `indicator_stay_timeout` | 0.6 | If it doesn't stay, how long should it last? (seconds) |
19-
| `keybind_allow` | true | Allow keybind to toggle pause |
20-
| `keybind_set` | "mbtn_left" | The set keybind to toggle pause [[reference](https://github.com/mpv-player/mpv/blob/master/etc/input.conf)] |
21-
| `keybind_mode` | "onpause" | Mode to activate keybind. <br />`onpause`: only active when video is paused, to unpause <br />`always`: always active to toggle pause/unpause |
22-
| `icon_color` | "#FFFFFF" | Icon fill color |
23-
| `icon_border_color` | "#111111" | Icon border color |
24-
| `icon_opacity` | 40 | Icon opacity (0-100) |
25-
| `rectangles_width` | 30 | Width of rectangles (pause icon) |
26-
| `rectangles_height` | 80 | Height of rectangles (pause icon) |
27-
| `rectangles_spacing` | 20 | Spacing between the two rectangles (pause icon) |
28-
| `triangle_width` | 80 | Width of triangle (play icon) |
29-
| `triangle_height` | 80 | Height of triangle (play icon) |
30-
| `flash_play_icon` | true | Flash play icon on unpause? (best with pause indicator icon) |
31-
| `flash_icon_timeout` | 0.3 | How long should the flash last? (seconds) |
32-
| `flash_icon_bigger_by` | 0 | Increase flash icon size from default by (0-100) |
12+
Below is the full list for indicator options and their default values. To adjust them, simply change their values in `local options` within the script.
13+
14+
| Option | Value | Description |
15+
|--------------------------|-------------|--------------------------------------------------------------------------------------------------------------------------------------|
16+
| `indicator_icon` | "pause" | indicator icon type. `pause`, `play` |
17+
| `indicator_stay` | true | keep indicator visibile during pause |
18+
| `indicator_timeout` | 0.6 | timeout (seconds) if indicator doesn't stay |
19+
| `keybind_allow` | true | allow keybind to toggle pause |
20+
| `keybind_set` | "mbtn_left" | the used keybind to toggle pause [[reference](https://github.com/mpv-player/mpv/blob/master/etc/input.conf)] |
21+
| `keybind_mode` | "onpause" | mode to activate keybind. <br>`onpause`: only active when paused, to unpause <br>`always`: always active to toggle pause/unpause |
22+
| `icon_color` | "#FFFFFF" | icon fill color |
23+
| `icon_border_color` | "#111111" | icon border color |
24+
| `icon_opacity` | 40 | icon opacity (0-100) |
25+
| `rectangles_width` | 30 | width of rectangles (pause icon) |
26+
| `rectangles_height` | 80 | height of rectangles (pause icon) |
27+
| `rectangles_spacing` | 20 | spacing between the two rectangles (pause icon) |
28+
| `triangle_width` | 80 | width of triangle (play icon) |
29+
| `triangle_height` | 80 | height of triangle (play icon) |
30+
| `flash_play_icon` | true | flash play icon on unpause (best with pause indicator icon) |
31+
| `flash_icon_timeout` | 0.3 | timeout (seconds) for flash icon |
3332

3433
### How to install
3534

extras/pause-indicator-lite/pause_indicator_lite.lua

+79-94
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,45 @@
55
66
--]]
77

8-
-- options
9-
local opts = {
8+
local options = {
109
-- indicator icon type
11-
indicator_icon = "pause", -- Which icon to show as indicator? pause, play
12-
indicator_stay = true, -- Should the pause indicator stay visible during pause?
13-
indicator_stay_timeout = 0.6, -- If it doesn't stay, how long should it last? (seconds)
10+
indicator_icon = "pause", -- indicator icon type. "pause", "play"
11+
indicator_stay = true, -- keep indicator visibile during pause
12+
indicator_timeout = 0.6, -- timeout (seconds) if indicator doesn't stay
1413

1514
-- keybind
16-
keybind_allow = true, -- Allow keybind to toggle pause
17-
keybind_set = "mbtn_left", -- The set keybind to toggle pause
18-
keybind_mode = "onpause", -- Mode to activate keybind. onpause, always
15+
keybind_allow = true, -- allow keybind to toggle pause
16+
keybind_set = "mbtn_left", -- the used keybind to toggle pause
17+
keybind_mode = "onpause", -- mode to activate keybind. "onpause", "always"
1918

2019
-- icon colors & opacity
21-
icon_color = "#FFFFFF", -- Icon fill color
22-
icon_border_color = "#111111", -- Icon border color
23-
icon_opacity = 40, -- Icon opacity (0-100)
20+
icon_color = "#FFFFFF", -- icon fill color
21+
icon_border_color = "#111111", -- icon border color
22+
icon_opacity = 40, -- icon opacity (0-100)
2423

2524
-- pause icon
26-
rectangles_width = 30, -- Width of rectangles
27-
rectangles_height = 80, -- Height of rectangles
28-
rectangles_spacing = 20, -- Spacing between the two rectangles
25+
rectangles_width = 30, -- width of rectangles
26+
rectangles_height = 80, -- height of rectangles
27+
rectangles_spacing = 20, -- spacing between the two rectangles
2928

3029
-- play icon
31-
triangle_width = 80, -- Width of triangle
30+
triangle_width = 80, -- width of triangle
3231
triangle_height = 80, -- height of triangle
3332

3433
-- best with pause icon
35-
flash_play_icon = true, -- Flash play icon on unpause?
36-
flash_icon_timeout = 0.3, -- How long should the flash last?
37-
flash_icon_bigger_by = 0 -- Increase flash icon size from default by (0-100)
34+
flash_play_icon = true, -- flash play icon on unpause
35+
flash_icon_timeout = 0.3, -- timeout (seconds) for flash icon
3836
}
3937

40-
local msg = require 'mp.msg'
38+
local msg = require "mp.msg"
4139

4240
-- convert color from hex (credit to mpv/osc.lua)
4341
local function convert_color(color)
4442
if color:find("^#%x%x%x%x%x%x$") == nil then
45-
msg.warn("'" .. color .. "' is not a valid color")
43+
msg.warn("'" .. color .. "' is not a valid color, using default '#FFFFFF'")
44+
return "FFFFFF" -- color fallback
4645
end
47-
return color:sub(6,7) .. color:sub(4,5) .. color:sub(2,3)
46+
return color:sub(6,7) .. color:sub(4,5) .. color:sub(2,3)
4847
end
4948

5049
-- convert percentage opacity (0-100) to ASS alpha values
@@ -54,109 +53,95 @@ local function convert_opacity(value)
5453
end
5554

5655
-- colors and opaicty
57-
local icon_color = convert_color(opts.icon_color)
58-
local icon_border_color = convert_color(opts.icon_border_color)
59-
local icon_opacity = convert_opacity(opts.icon_opacity)
60-
61-
-- rectangles parameters
62-
local rect_width = opts.rectangles_width
63-
local rect_height = opts.rectangles_height
64-
local rect_spacing = opts.rectangles_spacing
65-
66-
-- triangle parameters
67-
local triangle_width = opts.triangle_width
68-
local triangle_height = opts.triangle_height
69-
70-
-- draw rectangles
71-
local rectangles = string.format([[{\an5\p1\alpha&H%s\1c&H%s&\3c&H%s&}]],
72-
icon_opacity, icon_color, icon_border_color) ..
73-
string.format([[m 0 0 l %d 0 l %d %d l 0 %d m %d 0 l %d 0 l %d %d l %d %d]],
74-
rect_width, rect_width, rect_height, rect_height, rect_width + rect_spacing,
75-
rect_width + rect_spacing + rect_width, rect_width + rect_spacing + rect_width,
76-
rect_height, rect_width + rect_spacing, rect_height)
77-
78-
-- draw triangle
79-
local triangle = string.format([[{\an5\p1\alpha&H%s\1c&H%s&\3c&H%s&}]],
80-
icon_opacity, icon_color, icon_border_color) ..
81-
string.format([[m 0 0 l %d %d l 0 %d]],
82-
triangle_width, triangle_height / 2, triangle_height)
56+
local icon_color = convert_color(options.icon_color)
57+
local icon_border_color = convert_color(options.icon_border_color)
58+
local icon_opacity = convert_opacity(options.icon_opacity)
59+
60+
-- pause icon
61+
local function draw_rectangles()
62+
return string.format([[{\an5\p1\alpha&H%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]],
63+
icon_opacity, icon_color, icon_border_color, options.rectangles_width, options.rectangles_width, options.rectangles_height, options.rectangles_height,
64+
options.rectangles_width + options.rectangles_spacing, options.rectangles_width * 2 + options.rectangles_spacing, options.rectangles_width * 2 + options.rectangles_spacing, options.rectangles_height,
65+
options.rectangles_width + options.rectangles_spacing, options.rectangles_height)
66+
end
67+
68+
-- play icon
69+
local function draw_triangle()
70+
return string.format([[{\an5\p1\alpha&H%s\1c&H%s&\3c&H%s&}m 0 0 l %d %d l 0 %d]],
71+
icon_opacity, icon_color, icon_border_color, options.triangle_width, options.triangle_height / 2, options.triangle_height)
72+
end
8373

8474
-- init
8575
local indicator = mp.create_osd_overlay("ass-events")
8676
local flash = mp.create_osd_overlay("ass-events")
8777

88-
-- draw and update position based on window size
89-
local function update_pause_indicator_position()
78+
-- keep track of pause toggle
79+
local toggled = false
80+
81+
-- draw and update indicator
82+
local function update_indicator()
9083
local _, _, display_aspect = mp.get_osd_size()
91-
if display_aspect == 0 then return end
84+
if display_aspect == 0 or (indicator.visible and not toggled) then return end
85+
86+
indicator.data = options.indicator_icon == "play" and draw_triangle() or draw_rectangles()
87+
indicator:update()
9288

93-
local icon = opts.indicator_icon == "play" and triangle or rectangles
94-
indicator.data = icon
89+
if not options.indicator_stay then
90+
mp.add_timeout(options.indicator_timeout, function() indicator:remove() end)
91+
end
9592
end
9693

9794
-- flash play icon
9895
local function flash_icon()
99-
if not opts.flash_play_icon then return flash:remove() end
100-
101-
local mod = opts.flash_icon_bigger_by
102-
-- set parameters for the flash play icon
103-
local flash_play = string.format([[{\an5\p1\alpha&H%s\1c&H%s&\3c&H%s&}]],
104-
icon_opacity, icon_color, icon_border_color) ..
105-
string.format([[m 0 0 l %d %d l 0 %d]],
106-
triangle_width + mod, (triangle_height + mod) / 2, triangle_height + mod)
107-
108-
flash.data = flash_play
96+
if not options.flash_play_icon then return end
97+
flash.data = draw_triangle()
10998
flash:update()
99+
mp.add_timeout(options.flash_icon_timeout, function() flash:remove() end)
100+
end
110101

111-
-- set timeout for the flash icon
112-
mp.add_timeout(opts.flash_icon_timeout, function()
102+
-- check if file is video
103+
local function is_video()
104+
local t = mp.get_property_native("current-tracks/video")
105+
if t and not (t.image or t.albumart) then
106+
return true
107+
else
108+
indicator:remove()
113109
flash:remove()
114-
end)
110+
return false
111+
end
115112
end
116113

117-
-- keep track of pause toggle
118-
local toggled = false
119-
120114
-- observe when pause state changes
121115
mp.observe_property("pause", "bool", function(_, paused)
122-
mp.add_timeout(0.1, function()
123-
if paused then
124-
update_pause_indicator_position()
125-
indicator:update()
126-
toggled = true
127-
if not opts.indicator_stay then
128-
mp.add_timeout(opts.indicator_stay_timeout, function() indicator:remove() end)
129-
end
130-
if opts.flash_play_icon then
131-
flash:remove()
132-
end
133-
else
134-
indicator:remove()
135-
if toggled then
136-
flash_icon()
137-
toggled = false
138-
end
116+
if not is_video() then return mp.unobserve_property("pause") end
117+
if paused then
118+
update_indicator()
119+
toggled = true
120+
else
121+
indicator:remove()
122+
if toggled then
123+
flash_icon()
124+
toggled = false
139125
end
140-
end)
126+
end
141127

142-
-- set keybind (only if opts allow it)
143-
if opts.keybind_allow == true then
128+
-- keybind setup (if options allow it)
129+
if options.keybind_allow == true then
144130
mp.set_key_bindings({
145-
{opts.keybind_set, function() mp.commandv("cycle", "pause") end}
131+
{options.keybind_set, function() mp.commandv("cycle", "pause") end}
146132
}, "pause-indicator", "force")
147133

148-
if opts.keybind_mode == "always" or (opts.keybind_mode == "onpause" and paused) then
134+
if options.keybind_mode == "always" or (options.keybind_mode == "onpause" and paused) then
149135
mp.enable_key_bindings("pause-indicator")
150-
elseif opts.keybind_mode == "onpause" and not paused then
136+
else
151137
mp.disable_key_bindings("pause-indicator")
152138
end
153139
end
154140
end)
155141

156-
-- update pause indicator position when window size changes
142+
-- update pause indicator position if window size changes
157143
mp.observe_property("osd-dimensions", "native", function()
158144
if indicator and indicator.visible then
159-
update_pause_indicator_position()
160-
indicator:update()
145+
update_indicator()
161146
end
162147
end)

0 commit comments

Comments
 (0)