Skip to content

Alignment improvements #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/file_browser.conf
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ last_opened_directory_file=~~state/file_browser-last_opened_directory
# Uses the internal home-label addon.
home_label=yes

# force file-browser to use a specific text alignment (default: top-left)
# uses ass tag alignment numbers: https://aegi.vmoe.info/docs/3.0/ASS_Tags/#index23h3
# set to 0 to use the default mpv osd-align options
alignment=7
# force file-browser to use a specific alignment (default: top-left)
# set to auto to use the default mpv osd-align options
# Options: 'auto'|'top'|'center'|'bottom'
align_y=top
# Options: 'auto'|'left'|'center'|'right'
align_x=left

# The format string used for the header. Uses custom-keybind substitution codes to
# dynamically change the contents of the header (see: docs/custom-keybinds.md#codes)
Expand All @@ -207,6 +209,7 @@ format_string_bottomwrapper=...
# the `\h` character is a hard space to add padding
folder_icon={\p1}m 6.52 0 l 1.63 0 b 0.73 0 0.01 0.73 0.01 1.63 l 0 11.41 b 0 12.32 0.73 13.05 1.63 13.05 l 14.68 13.05 b 15.58 13.05 16.31 12.32 16.31 11.41 l 16.31 3.26 b 16.31 2.36 15.58 1.63 14.68 1.63 l 8.15 1.63{\p0}\h
cursor_icon={\p1}m 14.11 6.86 l 0.34 0.02 b 0.25 -0.02 0.13 -0 0.06 0.08 b -0.01 0.16 -0.02 0.28 0.04 0.36 l 3.38 5.55 l 3.38 5.55 3.67 6.15 3.81 6.79 3.79 7.45 3.61 8.08 3.39 8.5l 0.04 13.77 b -0.02 13.86 -0.01 13.98 0.06 14.06 b 0.11 14.11 0.17 14.13 0.24 14.13 b 0.27 14.13 0.31 14.13 0.34 14.11 l 14.11 7.28 b 14.2 7.24 14.25 7.16 14.25 7.07 b 14.25 6.98 14.2 6.9 14.11 6.86{\p0}\h
cursor_icon_flipped={\p1}m 0.13 6.86 l 13.9 0.02 b 14 -0.02 14.11 -0 14.19 0.08 b 14.26 0.16 14.27 0.28 14.21 0.36 l 10.87 5.55 l 10.87 5.55 10.44 6.79 10.64 8.08 10.86 8.5l 14.21 13.77 b 14.27 13.86 14.26 13.98 14.19 14.06 b 14.14 14.11 14.07 14.13 14.01 14.13 b 13.97 14.13 13.94 14.13 13.9 14.11 l 0.13 7.28 b 0.05 7.24 0 7.16 0 7.07 b 0 6.98 0.05 6.9 0.13 6.86{\p0}\h

# set the opacity of fonts in hexadecimal from 00 (opaque) to FF (transparent)
font_opacity_selection_marker=99
Expand Down
2 changes: 2 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mp.observe_property('path', 'string', observers.current_directory)
if o.map_dvd_device then mp.observe_property('dvd-device', 'string', observers.dvd_device) end
if o.map_bd_device then mp.observe_property('bluray-device', 'string', observers.bd_device) end
if o.map_cdda_device then mp.observe_property('cdda-device', 'string', observers.cd_device) end
if o.align_x == 'auto' then mp.observe_property('osd-align-x', 'string', observers.osd_align) end
if o.align_y == 'auto' then mp.observe_property('osd-align-y', 'string', observers.osd_align) end

-- scripts messages
mp.register_script_message('=>', script_messages.chain)
Expand Down
147 changes: 92 additions & 55 deletions modules/ass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,10 @@ end

local header_overrides = {['^'] = style.header}

--refreshes the ass text using the contents of the list
local function update_ass()
if state.hidden then state.flag_update = true ; return end

append(style.global)

append(style.header)
append(fb_utils.substitute_codes(o.format_string_header, header_overrides, nil, nil, function(str, code)
if code == '^' then return str end
return ass_escape(str, style.header)
end))
newline()

if #state.list < 1 then
append(state.empty_text)
flush_buffer()
draw()
return
end

---@return number start
---@return number finish
---@return boolean is_overflowing
local function calculate_view_window()
---@type number
local start = 1
---@type number
Expand All @@ -115,48 +99,101 @@ local function update_ass()
--this is necessary when the number of items in the dir is less than the max
if not overflow then finish = #state.list end

-- these are the number values to place into the wrappers
local wrapper_overrides = {['<'] = tostring(start-1), ['>'] = tostring(#state.list-finish)}
return start, finish, overflow
end

---@param i number index
---@return string
local function calculate_item_style(i)
local is_playing_file = highlight_entry(state.list[i])

--sets the selection colour scheme
local multiselected = state.selection[i]

--sets the colour for the item
local item_style = style.body

if multiselected then item_style = item_style..style.multiselect
elseif i == state.selected then item_style = item_style..style.selected end

if is_playing_file then item_style = item_style..(multiselected and style.playing_selected or style.playing) end

return item_style
end

local function draw_header()
append(style.header)
append(fb_utils.substitute_codes(o.format_string_header, header_overrides, nil, nil, function(str, code)
if code == '^' then return str end
return ass_escape(str, style.header)
end))
newline()
end

---@param wrapper_overrides ReplacerTable
local function draw_top_wrapper(wrapper_overrides)
--adding a header to show there are items above in the list
if o.format_string_topwrapper ~= '' and start > 1 then
append(style.footer_header, fb_utils.substitute_codes(o.format_string_topwrapper, wrapper_overrides, nil, nil, function(str)
return ass_escape(str)
end))
newline()
end
append(style.footer_header)
append(fb_utils.substitute_codes(o.format_string_topwrapper, wrapper_overrides, nil, nil, function(str)
return ass_escape(str)
end))
newline()
end

for i=start, finish do
local v = state.list[i]
local playing_file = highlight_entry(v)
append(style.body)
---@param wrapper_overrides ReplacerTable
local function draw_bottom_wrapper(wrapper_overrides)
append(style.footer_header)
append(fb_utils.substitute_codes(o.format_string_bottomwrapper, wrapper_overrides, nil, nil, function(str)
return ass_escape(str)
end))
end

--handles custom styles for different entries
if i == state.selected or i == state.multiselect_start then
if not (i == state.selected) then append(style.selection_marker) end
---@param i number index
---@param cursor string
local function draw_cursor(i, cursor)
--handles custom styles for different entries
if i == state.selected or i == state.multiselect_start then
if not (i == state.selected) then append(style.selection_marker) end

if not state.multiselect_start then append(style.cursor)
else
if state.selection[state.multiselect_start] then append(style.cursor_select)
else append(style.cursor_deselect) end
end
append(o.cursor_icon, "\\h", style.body)
if not state.multiselect_start then append(style.cursor)
else
append(g.style.indent, o.cursor_icon, "\\h", style.body)
if state.selection[state.multiselect_start] then append(style.cursor_select)
else append(style.cursor_deselect) end
end
else
append(g.style.indent)
end
append(cursor, '\\h', style.body)
end

--sets the selection colour scheme
local multiselected = state.selection[i]
--refreshes the ass text using the contents of the list
local function update_ass()
if state.hidden then state.flag_update = true ; return end

--sets the colour for the item
local item_style = style.body
-- local function set_colour()
if multiselected then item_style = item_style..style.multiselect
elseif i == state.selected then item_style = item_style..style.selected end
append(style.global)
draw_header()

if #state.list < 1 then
append(state.empty_text)
flush_buffer()
draw()
return
end

local start, finish, overflow = calculate_view_window()

-- these are the number values to place into the wrappers
local wrapper_overrides = {['<'] = tostring(start-1), ['>'] = tostring(#state.list-finish)}
if o.format_string_topwrapper ~= '' and start > 1 then
draw_top_wrapper(wrapper_overrides)
end

for i=start, finish do
local v = state.list[i]
append(style.body)
if g.ALIGN_X ~= 'right' then draw_cursor(i, o.cursor_icon) end

if playing_file then item_style = item_style..(multiselected and style.playing_selected or style.playing) end
-- end
-- set_colour()
local item_style = calculate_item_style(i)
append(item_style)

--sets the folder icon
Expand All @@ -166,13 +203,13 @@ local function update_ass()
end

--adds the actual name of the item
append(v.ass or ass_escape(v.label or v.name, item_style))
append(v.ass or ass_escape(v.label or v.name, item_style), '\\h')
if g.ALIGN_X == 'right' then draw_cursor(i, o.cursor_icon_flipped) end
newline()
end

if o.format_string_bottomwrapper ~= '' and overflow then
append(style.footer_header)
append(fb_utils.substitute_codes(o.format_string_bottomwrapper, wrapper_overrides, nil, nil, ass_escape))
draw_bottom_wrapper(wrapper_overrides)
end

flush_buffer()
Expand Down
13 changes: 12 additions & 1 deletion modules/globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,19 @@ globals.ass.res_y = 720 / o.scaling_factor_base

local BASE_FONT_SIZE = 25

--force file-browser to use a specific text alignment (default: top-left)
--uses ass tag alignment numbers: https://aegi.vmoe.info/docs/3.0/ASS_Tags/#index23h3
globals.ASS_ALIGNMENT_MATRIX = {
top = {left = 7, center = 8, right = 9},
center = {left = 4, center = 5, right = 6},
bottom = {left = 1, center = 2, right = 3},
}

globals.ALIGN_X = o.align_x == 'auto' and mp.get_property('osd-align-x', 'left') or o.align_x
globals.ALIGN_Y = o.align_y == 'auto' and mp.get_property('osd-align-y', 'top') or o.align_y

globals.style = {
global = o.alignment == 0 and "" or ([[{\an%d}]]):format(o.alignment),
global = ([[{\an%d}]]):format(globals.ASS_ALIGNMENT_MATRIX[globals.ALIGN_Y][globals.ALIGN_X]),

-- full line styles
header = ([[{\r\q2\b%s\fs%d\fn%s\c&H%s&}]]):format((o.font_bold_header and "1" or "0"), o.scaling_factor_header*BASE_FONT_SIZE, o.font_name_header, o.font_colour_header),
Expand Down
13 changes: 12 additions & 1 deletion modules/observers.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

local g = require 'modules.globals'
local directory_movement = require 'modules.navigation.directory-movement'
local fb = require 'modules.apis.fb'
local fb_utils = require 'modules.utils'
local ass = require 'modules.ass'

---@class observers
local observers ={}
Expand Down Expand Up @@ -34,4 +35,14 @@ function observers.cd_device(_, device)
fb.register_directory_mapping(fb_utils.absolute_path(device), '^cdda://.*', true)
end

---@param property string
---@param alignment string
function observers.osd_align(property, alignment)
if property == 'osd-align-x' then g.ALIGN_X = alignment
elseif property == 'osd-align-y' then g.ALIGN_Y = alignment end

g.style.global = ([[{\an%d}]]):format(g.ASS_ALIGNMENT_MATRIX[g.ALIGN_Y][g.ALIGN_X])
ass.update_ass()
end

return observers
9 changes: 5 additions & 4 deletions modules/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ local o = {
--the `\h` character is a hard space to add padding between the symbol and the text
folder_icon = [[{\p1}m 6.52 0 l 1.63 0 b 0.73 0 0.01 0.73 0.01 1.63 l 0 11.41 b 0 12.32 0.73 13.05 1.63 13.05 l 14.68 13.05 b 15.58 13.05 16.31 12.32 16.31 11.41 l 16.31 3.26 b 16.31 2.36 15.58 1.63 14.68 1.63 l 8.15 1.63{\p0}\h]],
cursor_icon = [[{\p1}m 14.11 6.86 l 0.34 0.02 b 0.25 -0.02 0.13 -0 0.06 0.08 b -0.01 0.16 -0.02 0.28 0.04 0.36 l 3.38 5.55 l 3.38 5.55 3.67 6.15 3.81 6.79 3.79 7.45 3.61 8.08 3.39 8.5l 0.04 13.77 b -0.02 13.86 -0.01 13.98 0.06 14.06 b 0.11 14.11 0.17 14.13 0.24 14.13 b 0.27 14.13 0.31 14.13 0.34 14.11 l 14.11 7.28 b 14.2 7.24 14.25 7.16 14.25 7.07 b 14.25 6.98 14.2 6.9 14.11 6.86{\p0}\h]],
cursor_icon_flipped = [[{\p1}m 0.13 6.86 l 13.9 0.02 b 14 -0.02 14.11 -0 14.19 0.08 b 14.26 0.16 14.27 0.28 14.21 0.36 l 10.87 5.55 l 10.87 5.55 10.44 6.79 10.64 8.08 10.86 8.5l 14.21 13.77 b 14.27 13.86 14.26 13.98 14.19 14.06 b 14.14 14.11 14.07 14.13 14.01 14.13 b 13.97 14.13 13.94 14.13 13.9 14.11 l 0.13 7.28 b 0.05 7.24 0 7.16 0 7.07 b 0 6.98 0.05 6.9 0.13 6.86{\p0}\h]],

--enable addons
addons = true,
Expand Down Expand Up @@ -148,10 +149,10 @@ local o = {
--This property is deprecated. When it is removed in mpv v0.37 file-browser will automatically ignore this option.
set_shared_script_properties = false,

--force file-browser to use a specific text alignment (default: top-left)
--uses ass tag alignment numbers: https://aegi.vmoe.info/docs/3.0/ASS_Tags/#index23h3
--set to 0 to use the default mpv osd-align options
alignment = 7,
---@type 'auto'|'left'|'center'|'right'
align_x = 'left',
---@type 'auto'|'top'|'center'|'bottom'
align_y = 'top',

--style settings
format_string_header = [[{\fnMonospace}[%i/%x]%^ %q\N------------------------------------------------------------------]],
Expand Down