feat: added options to display alternative top bar title

Added option `top_bar_alt_title`, which accepts an alternative title template to be displayed, and `top_bar_alt_title_place` that controls where it should be displayed. It can be either `below` to place it below the main one, or `toggle` to show it in place of the main title when user clicks the top bar or calls the `toggle-title` binding.

The `top_bar_title` now also accepts a custom top bar title template if user wants it to be different from the one defined in `mpv.conf`.

closes #402
This commit is contained in:
tomasklaen
2023-01-18 12:06:10 +01:00
parent c5ccd1b4ef
commit 8183163f2a
4 changed files with 86 additions and 16 deletions

View File

@@ -132,6 +132,12 @@ Under the hood, `toggle-ui` is using `toggle-elements`, and that is in turn usin
Toggles the always visible portion of the timeline. You can look at it as switching `timeline_size_min` option between it's configured value and 0.
#### `toggle-title`
Toggles the top bar title between main and alternative title's. This can also be done by clicking on the top bar.
Only relevant if top bar is enabled, `top_bar_alt_title` is configured, and `top_bar_alt_title_place` is `toggle`.
#### `flash-ui`
Command(s) to briefly flash the whole UI. Elements are revealed for a second and then fade away.

View File

@@ -122,7 +122,16 @@ top_bar=no-border
top_bar_size=40
top_bar_size_fullscreen=46
top_bar_controls=yes
# Can be: `no` (hide), `yes` (inherit title from mpv.conf), or a custom template string
top_bar_title=yes
# Template string to enable alternative top bar title. If alt title matches main title,
# it'll be hidden. Tip: use `${media-title}` for main, and `${filename}` for alt title.
top_bar_alt_title=
# Can be:
# `below` => display alt title below the main one
# `toggle` => toggle the top bar title text between main and alt by clicking
# the top bar, or calling `toggle-title` binding
top_bar_alt_title_place=below
top_bar_title_opacity=0.8
top_bar_persistency=

View File

@@ -65,7 +65,9 @@ defaults = {
top_bar_size_fullscreen = 46,
top_bar_persistency = '',
top_bar_controls = true,
top_bar_title = true,
top_bar_title = 'yes',
top_bar_alt_title = '',
top_bar_alt_title_place = 'below',
top_bar_title_opacity = 0.8,
window_border_size = 1,
@@ -288,6 +290,7 @@ state = {
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
@@ -567,23 +570,50 @@ mp.register_event('end-file', function(event)
handle_file_end()
end
end)
-- Top bar titles
do
local template = nil
function update_title()
if template:sub(-6) == ' - mpv' then template = template:sub(1, -7) end
local function update_state_with_template(prop, template)
-- escape ASS, and strip newlines and trailing slashes and trim whitespace
local t = mp.command_native({'expand-text', template}):gsub('\\n', ' '):gsub('[\\%s]+$', ''):gsub('^%s+', '')
set_state('title', ass_escape(t))
local tmp = mp.command_native({'expand-text', template}):gsub('\\n', ' '):gsub('[\\%s]+$', ''):gsub('^%s+', '')
set_state(prop, ass_escape(tmp))
end
mp.observe_property('title', 'string', function(_, title)
mp.unobserve_property(update_title)
template = title
local props = get_expansion_props(title)
local function add_template_listener(template, callback)
local props = get_expansion_props(template)
for prop, _ in pairs(props) do
mp.observe_property(prop, 'native', update_title)
mp.observe_property(prop, 'native', callback)
end
if not next(props) then callback() end
end
local function remove_template_listener(callback) mp.unobserve_property(callback) end
-- 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
if not next(props) then 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
@@ -747,6 +777,7 @@ bind_command('toggle-progress', function()
timeline:tween_property('size_min_override', timeline.size_min, 0)
end
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)

View File

@@ -47,11 +47,11 @@ local TopBar = class(Element)
function TopBar:new() return Class.new(self) --[[@as TopBar]] end
function TopBar:init()
Element.init(self, 'top_bar')
self.pressed = false
self.size, self.size_max, self.size_min = 0, 0, 0
self.icon_size, self.spacing, self.font_size, self.title_bx = 1, 1, 1, 1
self.size_min_override = options.timeline_start_hidden and 0 or nil
self.top_border = options.timeline_border
self.show_alt_title = false
local function decide_maximized_command()
return state.border
@@ -99,6 +99,11 @@ function TopBar:update_dimensions()
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_border()
self:decide_enabled()
self:update_dimensions()
@@ -114,6 +119,10 @@ function TopBar:on_prop_maximized()
self:update_dimensions()
end
function TopBar:on_mbtn_left_down()
if cursor.x < self.title_bx then self:toggle_title() end
end
function TopBar:on_display() self:update_dimensions() end
function TopBar:render()
@@ -142,7 +151,7 @@ function TopBar:render()
end
-- Title
local text = state.title
local text = self.show_alt_title and state.alt_title or state.title
if max_bx - title_ax > self.font_size * 3 and text and text ~= '' then
local opts = {
size = self.font_size, wrap = 2, color = bgt, border = 1, border_color = bg, opacity = visibility,
@@ -157,10 +166,25 @@ function TopBar:render()
title_ay = by + 1
end
-- Alt title
if state.alt_title and options.top_bar_alt_title_place == 'below' and state.alt_title ~= state.title 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(state.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, state.alt_title, opts)
title_ay = by + 1
end
-- Subtitle: current chapter
if state.current_chapter and max_bx - title_ax > self.font_size * 3 then
local font_size = self.font_size * 0.8
local height = font_size * 1.5
local height = font_size * 1.3
local text = '' .. state.current_chapter.index .. ': ' .. state.current_chapter.title
local by = title_ay + height
local opts = {