diff options
Diffstat (limited to 'osd.lua')
| -rw-r--r-- | osd.lua | 107 |
1 files changed, 64 insertions, 43 deletions
@@ -44,7 +44,6 @@ function osd.new(init) scale = 1, lines = lines, padding = math.floor((720 - (lines * config.font_size)) / 2), - img = nil, }, mt) t.bg.z = -1 @@ -174,13 +173,15 @@ end function mt:load_img() local f = mp_utils.file_info(self.img.path) if not f then - return false + return + end + + if self.magick_cmd_id then + mp.abort_async_command(self.magick_cmd_id) end - local cmd = { + local magick_cmd = { 'magick', self.img.path, - '-background', 'none', - '-gravity', 'northwest', '-depth', '8', '-resize', string.format( 'x%%[fx:max(%d, min(%d, %d*h/w))]', @@ -188,41 +189,49 @@ function mt:load_img() '-print', '%w %h', 'tmp.bgra' } - print('exec: ' .. mp_utils.to_string(cmd)) - local res = mp.command_native({ + print('exec: ' .. mp_utils.to_string(magick_cmd)) + self.img.magick_cmd = magick_cmd + self.magick_cmd = magick_cmd + self.magick_cmd_id = mp.command_native_async({ name = 'subprocess', - args = cmd, + args = magick_cmd, capture_stdout = true, playback_only = false, - }) - if not res or res.status ~= 0 then - return false - end + }, function (_, res) + if magick_cmd ~= self.magick_cmd then + return + end - local w, h = res.stdout:match('(.+) (.+)') - self.img.cmd = { - name = 'overlay-add', - id = 0, - file = 'tmp.bgra', - w = w, - h = h, - x = self.img.right - w, - y = self.img.top, - fmt = 'bgra', - offset = 0, - stride = 4*w, - } + self.magick_cmd = nil + self.magick_cmd_id = nil + + if not self.img or not res or res.status ~= 0 then + return + end + + local w, h = res.stdout:match('(.+) (.+)') + self.img.cmd = { + name = 'overlay-add', + id = 0, + file = 'tmp.bgra', + w = w, + h = h, + x = self.img.right - w, + y = self.img.top, + fmt = 'bgra', + offset = 0, + stride = 4*w, + } - return true + self:draw_img() + end) end function mt:clear_img(purge) - if not self.img then - return - end - - if self.img.cmd then - mp.command_native({name = 'overlay-remove', id = self.img.cmd.id}) + if self.img_overlay_id then + mp.command_native( + {name = 'overlay-remove', id = self.img_overlay_id}) + self.img_overlay_id = nil end if purge then @@ -235,16 +244,20 @@ function mt:draw_img() return end - if not (self.img.cmd or self:load_img()) then - self:clear_img() + if not self.img.magick_cmd then + self:load_img() return end - if not self:is_hidden() then + if self.img.cmd and not self:is_hidden() then mp.command_native(self.img.cmd) + self.img_overlay_id = self.img.cmd.id end end +-- returns (upd, new) +-- upd: if anything changed, e.g. position +-- new: if the image also changed, i.e. different path or size function mt:set_img(path, menu_res) -- images require *real* dimensions and coordinates, unlike other OSD -- functions which scale the given values automatically @@ -272,18 +285,21 @@ function mt:set_img(path, menu_res) local top = math.floor(padding) local right = math.floor(self.width - padding) - if self.img and self.img.cmd and path == self.img.path and - min_h == self.img.min_h and max_h == self.img.max_h and + if self.img and path == self.img.path and + min_h == self.img.min_h and + max_h == self.img.max_h and max_w == self.img.max_w then if top == self.img.top and right == self.img.right then - return false + return false, false end self.img.top = top - self.img.cmd.y = top self.img.right = right - self.img.cmd.x = right - self.img.cmd.w - return true + if self.img.cmd then + self.img.cmd.x = right - self.img.cmd.w + self.img.cmd.y = top + end + return true, false end self.img = { @@ -294,7 +310,7 @@ function mt:set_img(path, menu_res) top = top, right = right, } - return true + return true, true end function mt:draw_scrollbar(state) @@ -380,6 +396,7 @@ function mt:redraw(state) self.fg.compute_bounds = not not img local res = self.fg:update() + self.fg.compute_bounds = false self.bg:update() if img and self.img_path_func then local path = self.img_path_func(img, function(path) @@ -388,7 +405,11 @@ function mt:redraw(state) end end) - if self:set_img(path, res) then + local upd, new = self:set_img(path, res) + if upd then + if new then + self:clear_img() + end self:draw_img() end else |
