Skip to content

Commit d87a235

Browse files
mouse-mode: add support for custom scrolling (#22)
Implements a new scrolling system for mouse-mode and properly calculates the hovered item when scrolling the list. This hover should work with custom font sizes, but currently the calculations are not adding up for the header, so that is hardcoded. See the next commit for keybinds.
1 parent 39848af commit d87a235

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

Diff for: file-browser.lua

+36-17
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,12 @@ local style = {
186186
}
187187

188188
local state = {
189-
list = {},
190-
selected = 1,
191-
hidden = true,
192-
flag_update = false,
193-
keybinds = nil,
189+
list = {}, -- list of items
190+
selected = 1, -- currently selected item
191+
scroll_offset = 0,
192+
hidden = true, -- whether the browser is hidden
193+
flag_update = false, -- should we redraw the browser when next opened
194+
keybinds = nil, -- array of dynamic deybinds
194195

195196
parser = nil,
196197
directory = nil,
@@ -710,18 +711,13 @@ local function update_ass()
710711
local start = 1
711712
local finish = start+o.num_entries-1
712713

713-
--handling cursor positioning
714-
local mid = math.ceil(o.num_entries/2)+1
715-
if state.selected+mid > finish then
716-
local offset = state.selected - finish + mid
717-
718-
--if we've overshot the end of the list then undo some of the offset
719-
if finish + offset > #state.list then
720-
offset = offset - ((finish+offset) - #state.list)
714+
--handling the offset caused by scrolling
715+
if state.scroll_offset > 0 then
716+
if finish + state.scroll_offset > #state.list then
717+
state.scroll_offset = #state.list - finish
721718
end
722-
723-
start = start + offset
724-
finish = finish + offset
719+
start = start + state.scroll_offset
720+
finish = finish + state.scroll_offset
725721
end
726722

727723
--making sure that we don't overstep the boundaries
@@ -832,6 +828,12 @@ local function scroll(n, wrap)
832828
end
833829

834830
if state.multiselect_start then drag_select(original_pos, state.selected) end
831+
832+
--moves the scroll window down so that the selected item is in the middle of the screen
833+
state.scroll_offset = state.selected - (math.ceil(o.num_entries/2)-1)
834+
if state.scroll_offset < 0 or (state.scroll_offset + o.num_entries) > #state.list then
835+
state.scroll_offset = 0
836+
end
835837
update_ass()
836838
end
837839

@@ -863,13 +865,30 @@ end
863865

864866
--update the selected item based on the mouse position
865867
local function update_mouse_pos(_, mouse_pos)
868+
if not mouse_pos then mouse_pos = mp.get_property_native("mouse-pos") end
866869
if not mouse_pos.hover then return end
867870
local scale = mp.get_property_number("osd-height", 0) / 720
871+
872+
--this will currently not work with different sized headers
873+
--for some reason the scaling calculation works differently for the header
868874
local header_offset = 65
869-
state.selected = math.ceil((mouse_pos.y-header_offset) / (25* scale))
875+
if state.scroll_offset > 0 then
876+
header_offset = header_offset + o.font_size_wrappers*scale
877+
end
878+
879+
state.selected = math.ceil((mouse_pos.y-header_offset) / (o.font_size_body* scale)) + state.scroll_offset
870880
update_ass()
871881
end
872882

883+
-- scrolls the view window when using mouse mode
884+
local function wheel(direction)
885+
state.scroll_offset = state.scroll_offset + direction
886+
if state.scroll_offset < 0 or (state.scroll_offset + o.num_entries) > #state.list then
887+
state.scroll_offset = 0
888+
end
889+
update_mouse_pos()
890+
end
891+
873892
--------------------------------------------------------------------------------------------------------
874893
-----------------------------------------Directory Movement---------------------------------------------
875894
--------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)