Skip to content

Commit 06f4b93

Browse files
mouse-support: update code to use type annotations
1 parent 7243eec commit 06f4b93

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

modules/defs/mp/defaults.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ local mp = {}
2828
---@field error_string ''|'killed'|'init'
2929
---@field killed_by_us boolean
3030

31+
---@class MPVMousePos
32+
---@field x number
33+
---@field y number
34+
---@field hover boolean
35+
3136
---@param key string
3237
---@param name_or_fn string|function
3338
---@param fn? async fun()
@@ -145,4 +150,7 @@ function mp.set_property_number(name, value) end
145150
---@return string? err
146151
function mp.set_property_native(name, value) end
147152

153+
---@param fn function
154+
function mp.unobserve_property(fn) end
155+
148156
return mp

modules/defs/state.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
---@class (exact) State
66
---@field list List
77
---@field selected number
8+
---@field scroll_offset number
89
---@field hidden boolean
910
---@field flag_update boolean
1011
---@field keybinds KeybindTupleStrict[]?

modules/globals.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,14 @@ globals.state = {
8080
selection = {}
8181
}
8282

83+
---@type 'top'|'center'|'bottom'
84+
globals.osd_alignment = "top"
85+
8386
--if the alignment isn't automated then we'll store a static value
8487
--numbers defined here: https://aegi.vmoe.info/docs/3.0/ASS_Tags/#index23h3
85-
if o.alignment >= 7 then globals.state.osd_alignment = "top"
86-
elseif o.alignment >= 4 then globals.state.osd_alignment = "center"
87-
elseif o.alignment >= 1 then globals.state.osd_alignment = "bottom" end
88+
if o.alignment >= 7 then globals.osd_alignment = "top"
89+
elseif o.alignment >= 4 then globals.osd_alignment = "center"
90+
elseif o.alignment >= 1 then globals.osd_alignment = "bottom" end
8891

8992
---@class ParserRef
9093
---@field id string

modules/keybinds.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ g.state.keybinds = {
4444
---@type KeybindList
4545
local top_level_keys = {}
4646

47+
---@param key KeybindTupleStrict
4748
local function add_key(key)
4849
table.insert(g.state.keybinds, key)
4950
end

modules/navigation/cursor.lua

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ function cursor.toggle_select_mode()
131131
end
132132
end
133133

134+
---@param str string
135+
---@param substring string
136+
---@return number
134137
local function count_substrings(str, substring)
135138
local count = 0
136139
for match in string.gmatch(str, substring) do
@@ -147,7 +150,9 @@ local num_header_lines = count_substrings(o.format_string_header, '\\N') + 1
147150
local num_twrapper_lines = count_substrings(o.format_string_topwrapper, '\\N') + 1
148151
local num_bwrapper_lines = count_substrings(o.format_string_bottomwrapper, '\\N') + 1
149152

150-
--update the selected item based on the mouse position
153+
---update the selected item based on the mouse position
154+
---@param _? string
155+
---@param mouse_pos? MPVMousePos
151156
function cursor.update_mouse_pos(_, mouse_pos)
152157
if not o.mouse_mode or g.state.hidden or #g.state.list == 0 then return end
153158

@@ -158,10 +163,10 @@ function cursor.update_mouse_pos(_, mouse_pos)
158163
local scale = mp.get_property_number("osd-height", 0) / g.ass.res_y
159164
local osd_offset = scale * mp.get_property("osd-margin-y", 22)
160165

161-
msg.trace('calculating mouse pos for', g.state.osd_alignment, 'alignment')
166+
msg.trace('calculating mouse pos for', g.osd_alignment, 'alignment')
162167

163168
--calculate position when browser is aligned to the top of the screen
164-
if g.state.osd_alignment == "top" then
169+
if g.osd_alignment == "top" then
165170
local header_offset = osd_offset + (num_header_lines * scale * font_size_header)
166171
if g.state.scroll_offset > 0 then header_offset = header_offset + (num_twrapper_lines * scale * font_size_wrappers) end
167172
msg.trace('calculated header offset', header_offset)
@@ -171,7 +176,7 @@ function cursor.update_mouse_pos(_, mouse_pos)
171176
--calculate position when browser is aligned to the bottom of the screen
172177
--this calculation is slightly off when a bottom wrapper exists,
173178
--I do not know what causes this.
174-
elseif g.state.osd_alignment == "bottom" then
179+
elseif g.osd_alignment == "bottom" then
175180
mouse_pos.y = (mp.get_property_number("osd-height", 0)) - mouse_pos.y
176181

177182
local bottom = math.min(#g.state.list, g.state.scroll_offset + o.num_entries)
@@ -184,7 +189,8 @@ function cursor.update_mouse_pos(_, mouse_pos)
184189
ass.update_ass()
185190
end
186191

187-
-- scrolls the view window when using mouse mode
192+
---scrolls the view window when using mouse mode
193+
---@param direction number
188194
function cursor.wheel(direction)
189195
g.state.scroll_offset = g.state.scroll_offset + direction
190196
if (g.state.scroll_offset + o.num_entries) > #g.state.list then

modules/observers.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ function observers.osd_align_x()
4141
ass.update_ass()
4242
end
4343

44+
---@param _ string
45+
---@param alignment string
4446
function observers.osd_align_y(_, alignment)
45-
g.state.osd_alignment = alignment
47+
g.osd_alignment = alignment
4648
cursor.update_mouse_pos() -- calls ass.update_ass() internally
4749
end
4850

0 commit comments

Comments
 (0)