Skip to content

Commit dc0de40

Browse files
Merge pull request #137 from CogentRedTester/alignment-improvements
Alignment improvements
2 parents e40ce3a + d109705 commit dc0de40

File tree

6 files changed

+130
-65
lines changed

6 files changed

+130
-65
lines changed

docs/file_browser.conf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,12 @@ last_opened_directory_file=~~state/file_browser-last_opened_directory
183183
# Uses the internal home-label addon.
184184
home_label=yes
185185

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

191193
# The format string used for the header. Uses custom-keybind substitution codes to
192194
# dynamically change the contents of the header (see: docs/custom-keybinds.md#codes)
@@ -207,6 +209,7 @@ format_string_bottomwrapper=...
207209
# the `\h` character is a hard space to add padding
208210
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
209211
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
212+
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
210213

211214
# set the opacity of fonts in hexadecimal from 00 (opaque) to FF (transparent)
212215
font_opacity_selection_marker=99

main.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ mp.observe_property('path', 'string', observers.current_directory)
4141
if o.map_dvd_device then mp.observe_property('dvd-device', 'string', observers.dvd_device) end
4242
if o.map_bd_device then mp.observe_property('bluray-device', 'string', observers.bd_device) end
4343
if o.map_cdda_device then mp.observe_property('cdda-device', 'string', observers.cd_device) end
44+
if o.align_x == 'auto' then mp.observe_property('osd-align-x', 'string', observers.osd_align) end
45+
if o.align_y == 'auto' then mp.observe_property('osd-align-y', 'string', observers.osd_align) end
4446

4547
-- scripts messages
4648
mp.register_script_message('=>', script_messages.chain)

modules/ass.lua

Lines changed: 92 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,10 @@ end
6969

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

72-
--refreshes the ass text using the contents of the list
73-
local function update_ass()
74-
if state.hidden then state.flag_update = true ; return end
75-
76-
append(style.global)
77-
78-
append(style.header)
79-
append(fb_utils.substitute_codes(o.format_string_header, header_overrides, nil, nil, function(str, code)
80-
if code == '^' then return str end
81-
return ass_escape(str, style.header)
82-
end))
83-
newline()
84-
85-
if #state.list < 1 then
86-
append(state.empty_text)
87-
flush_buffer()
88-
draw()
89-
return
90-
end
91-
72+
---@return number start
73+
---@return number finish
74+
---@return boolean is_overflowing
75+
local function calculate_view_window()
9276
---@type number
9377
local start = 1
9478
---@type number
@@ -115,48 +99,101 @@ local function update_ass()
11599
--this is necessary when the number of items in the dir is less than the max
116100
if not overflow then finish = #state.list end
117101

118-
-- these are the number values to place into the wrappers
119-
local wrapper_overrides = {['<'] = tostring(start-1), ['>'] = tostring(#state.list-finish)}
102+
return start, finish, overflow
103+
end
104+
105+
---@param i number index
106+
---@return string
107+
local function calculate_item_style(i)
108+
local is_playing_file = highlight_entry(state.list[i])
109+
110+
--sets the selection colour scheme
111+
local multiselected = state.selection[i]
112+
113+
--sets the colour for the item
114+
local item_style = style.body
115+
116+
if multiselected then item_style = item_style..style.multiselect
117+
elseif i == state.selected then item_style = item_style..style.selected end
118+
119+
if is_playing_file then item_style = item_style..(multiselected and style.playing_selected or style.playing) end
120+
121+
return item_style
122+
end
123+
124+
local function draw_header()
125+
append(style.header)
126+
append(fb_utils.substitute_codes(o.format_string_header, header_overrides, nil, nil, function(str, code)
127+
if code == '^' then return str end
128+
return ass_escape(str, style.header)
129+
end))
130+
newline()
131+
end
120132

133+
---@param wrapper_overrides ReplacerTable
134+
local function draw_top_wrapper(wrapper_overrides)
121135
--adding a header to show there are items above in the list
122-
if o.format_string_topwrapper ~= '' and start > 1 then
123-
append(style.footer_header, fb_utils.substitute_codes(o.format_string_topwrapper, wrapper_overrides, nil, nil, function(str)
124-
return ass_escape(str)
125-
end))
126-
newline()
127-
end
136+
append(style.footer_header)
137+
append(fb_utils.substitute_codes(o.format_string_topwrapper, wrapper_overrides, nil, nil, function(str)
138+
return ass_escape(str)
139+
end))
140+
newline()
141+
end
128142

129-
for i=start, finish do
130-
local v = state.list[i]
131-
local playing_file = highlight_entry(v)
132-
append(style.body)
143+
---@param wrapper_overrides ReplacerTable
144+
local function draw_bottom_wrapper(wrapper_overrides)
145+
append(style.footer_header)
146+
append(fb_utils.substitute_codes(o.format_string_bottomwrapper, wrapper_overrides, nil, nil, function(str)
147+
return ass_escape(str)
148+
end))
149+
end
133150

134-
--handles custom styles for different entries
135-
if i == state.selected or i == state.multiselect_start then
136-
if not (i == state.selected) then append(style.selection_marker) end
151+
---@param i number index
152+
---@param cursor string
153+
local function draw_cursor(i, cursor)
154+
--handles custom styles for different entries
155+
if i == state.selected or i == state.multiselect_start then
156+
if not (i == state.selected) then append(style.selection_marker) end
137157

138-
if not state.multiselect_start then append(style.cursor)
139-
else
140-
if state.selection[state.multiselect_start] then append(style.cursor_select)
141-
else append(style.cursor_deselect) end
142-
end
143-
append(o.cursor_icon, "\\h", style.body)
158+
if not state.multiselect_start then append(style.cursor)
144159
else
145-
append(g.style.indent, o.cursor_icon, "\\h", style.body)
160+
if state.selection[state.multiselect_start] then append(style.cursor_select)
161+
else append(style.cursor_deselect) end
146162
end
163+
else
164+
append(g.style.indent)
165+
end
166+
append(cursor, '\\h', style.body)
167+
end
147168

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

151-
--sets the colour for the item
152-
local item_style = style.body
153-
-- local function set_colour()
154-
if multiselected then item_style = item_style..style.multiselect
155-
elseif i == state.selected then item_style = item_style..style.selected end
173+
append(style.global)
174+
draw_header()
175+
176+
if #state.list < 1 then
177+
append(state.empty_text)
178+
flush_buffer()
179+
draw()
180+
return
181+
end
182+
183+
local start, finish, overflow = calculate_view_window()
184+
185+
-- these are the number values to place into the wrappers
186+
local wrapper_overrides = {['<'] = tostring(start-1), ['>'] = tostring(#state.list-finish)}
187+
if o.format_string_topwrapper ~= '' and start > 1 then
188+
draw_top_wrapper(wrapper_overrides)
189+
end
190+
191+
for i=start, finish do
192+
local v = state.list[i]
193+
append(style.body)
194+
if g.ALIGN_X ~= 'right' then draw_cursor(i, o.cursor_icon) end
156195

157-
if playing_file then item_style = item_style..(multiselected and style.playing_selected or style.playing) end
158-
-- end
159-
-- set_colour()
196+
local item_style = calculate_item_style(i)
160197
append(item_style)
161198

162199
--sets the folder icon
@@ -166,13 +203,13 @@ local function update_ass()
166203
end
167204

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

173211
if o.format_string_bottomwrapper ~= '' and overflow then
174-
append(style.footer_header)
175-
append(fb_utils.substitute_codes(o.format_string_bottomwrapper, wrapper_overrides, nil, nil, ass_escape))
212+
draw_bottom_wrapper(wrapper_overrides)
176213
end
177214

178215
flush_buffer()

modules/globals.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,19 @@ globals.ass.res_y = 720 / o.scaling_factor_base
3434

3535
local BASE_FONT_SIZE = 25
3636

37+
--force file-browser to use a specific text alignment (default: top-left)
38+
--uses ass tag alignment numbers: https://aegi.vmoe.info/docs/3.0/ASS_Tags/#index23h3
39+
globals.ASS_ALIGNMENT_MATRIX = {
40+
top = {left = 7, center = 8, right = 9},
41+
center = {left = 4, center = 5, right = 6},
42+
bottom = {left = 1, center = 2, right = 3},
43+
}
44+
45+
globals.ALIGN_X = o.align_x == 'auto' and mp.get_property('osd-align-x', 'left') or o.align_x
46+
globals.ALIGN_Y = o.align_y == 'auto' and mp.get_property('osd-align-y', 'top') or o.align_y
47+
3748
globals.style = {
38-
global = o.alignment == 0 and "" or ([[{\an%d}]]):format(o.alignment),
49+
global = ([[{\an%d}]]):format(globals.ASS_ALIGNMENT_MATRIX[globals.ALIGN_Y][globals.ALIGN_X]),
3950

4051
-- full line styles
4152
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),

modules/observers.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
1+
local g = require 'modules.globals'
22
local directory_movement = require 'modules.navigation.directory-movement'
33
local fb = require 'modules.apis.fb'
44
local fb_utils = require 'modules.utils'
5+
local ass = require 'modules.ass'
56

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

38+
---@param property string
39+
---@param alignment string
40+
function observers.osd_align(property, alignment)
41+
if property == 'osd-align-x' then g.ALIGN_X = alignment
42+
elseif property == 'osd-align-y' then g.ALIGN_Y = alignment end
43+
44+
g.style.global = ([[{\an%d}]]):format(g.ASS_ALIGNMENT_MATRIX[g.ALIGN_Y][g.ALIGN_X])
45+
ass.update_ass()
46+
end
47+
3748
return observers

modules/options.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ local o = {
119119
--the `\h` character is a hard space to add padding between the symbol and the text
120120
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]],
121121
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]],
122+
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]],
122123

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

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

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

0 commit comments

Comments
 (0)