Skip to content

Commit

Permalink
console.lua: give a different background color to the selected item
Browse files Browse the repository at this point in the history
The default item also has the same background color but with
transparency.

Also stop bolding selections since inverted black and white backgrounds
should be visible even with color blindness. It was annoying with
proportional fonts because it misalignes similar strings.

As mpv's default text colors are white on black border or background,
--osd-selected-color's default of a bright yellow meant to be used with
a black border becomes unreadable with the inverted white background.

We could default to a dark --osd-selected-color and a a light
--osd-selected-outline-color and use --osd-selected-outline-color as the
selected back color. However in show-text commands having only the
selected item with a different white border doesn't look good.

This therefore adds indipendent selected_color and selected_back_color
script-opts. --osd-selected-color is only used for completions and for
the selected item when searching the command history with
outline-and-shadow.
  • Loading branch information
guidocella committed Feb 12, 2025
1 parent f1b9d1f commit 2f16e28
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DOCS/interface-changes/console-menu.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
add `console-background_alpha` `console-menu_outline_size` `console-menu_outline_color` `console-corner_radius` script-opts
add `console-background_alpha` `console-menu_outline_size` `console-menu_outline_color` `console-corner_radius` `console-selected_color` and `console-selected_back_color` script-opts
10 changes: 10 additions & 0 deletions DOCS/man/console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ Configurable Options
Whether to scale the console with the window height. Can be ``yes``, ``no``,
or ``auto``, which follows the value of ``--osd-scale-by-window``.

``selected_color``
Default: ``#222222``

The color of the selected item.

``selected_back_color``
Default: ``#FFFFFF``

The background color of the selected item.

``case_sensitive``
Default: no on Windows, yes on other platforms.

Expand Down
35 changes: 28 additions & 7 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ local opts = {
margin_x = -1,
margin_y = -1,
scale_with_window = "auto",
selected_color = '#222222',
selected_back_color = '#FFFFFF',
case_sensitive = platform ~= 'windows' and true or false,
history_dedup = true,
font_hw_ratio = 'auto',
Expand Down Expand Up @@ -331,7 +333,7 @@ local function calculate_max_item_width()
width_overlay.res_x = osd_w
width_overlay.res_y = osd_h
width_overlay.data = '{\\fs' .. opts.font_size ..
(font and '\\fn' .. font or '') .. '\\b1\\q2}' ..
(font and '\\fn' .. font or '') .. '\\q2}' ..
ass_escape(longest_item)
local result = width_overlay:update()
max_item_width = math.min(result.x1 - result.x0,
Expand All @@ -356,7 +358,7 @@ local function get_selected_ass()
local color, alpha = mpv_color_to_ass(mp.get_property('osd-selected-color'))
local outline_color, outline_alpha =
mpv_color_to_ass(mp.get_property('osd-selected-outline-color'))
return '{\\1c&H' .. color .. '&\\1a&H' .. alpha ..
return '{\\b1\\1c&H' .. color .. '&\\1a&H' .. alpha ..
'&\\3c&H' .. outline_color .. '&\\3a&H' .. outline_alpha .. '&}'
end

Expand Down Expand Up @@ -442,7 +444,7 @@ local function format_grid(list, width_max, rows_max)
columns[column] = ass_escape(string.format(format_string, list[i]))

if should_highlight_completion(i) then
columns[column] = '{\\b1}' .. get_selected_ass() .. columns[column] ..
columns[column] = get_selected_ass() .. columns[column] ..
'{\\b\\1a&\\3a&}' .. styles.completion
end
end
Expand Down Expand Up @@ -490,14 +492,16 @@ local function populate_log_with_matches()
local style = ''
local terminal_style = ''

if i == selected_match or matches[i].index == default_item then
style = get_selected_ass()
end
if matches[i].index == default_item then
terminal_style = terminal_styles.default_item
end
if i == selected_match then
style = style .. '{\\b1}'
if searching_history and
mp.get_property('osd-border-style') == 'outline-and-shadow' then
style = get_selected_ass()
else
style = '{\\1c&H' .. option_color_to_ass(opts.selected_color) .. '&}'
end
terminal_style = terminal_style .. terminal_styles.selected_completion
end

Expand Down Expand Up @@ -708,6 +712,23 @@ local function render()
local item_y = alignment == 7
and y + (1 + i) * line_height
or y - (1.5 + #log_buffer - i) * line_height

if (first_match_to_print - 1 + i == selected_match or
matches[first_match_to_print - 1 + i].index == default_item)
and (not searching_history or border_style == 'background-box') then
ass:new_event()
ass:an(4)
ass:pos(x, item_y)
ass:append('{\\blur0\\bord0\\4aH&ff&\\1c&H' ..
option_color_to_ass(opts.selected_back_color) .. '&}')
if first_match_to_print - 1 + i ~= selected_match then
ass:append('{\\1aH&dd&}')
end
ass:draw_start()
ass:rect_cw(-opts.padding, 0, max_item_width + opts.padding, line_height)
ass:draw_stop()
end

ass:new_event()
ass:an(4)
ass:pos(x, item_y)
Expand Down

0 comments on commit 2f16e28

Please sign in to comment.