summaryrefslogtreecommitdiff
path: root/mpv/scripts/file-browser/modules/controls.lua
blob: 6b124c212f2d140b5670ed552c6699255e920b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

local mp = require 'mp'
local msg = require 'mp.msg'
local utils = require 'mp.utils'

local o = require 'modules.options'
local g = require 'modules.globals'
local fb_utils = require 'modules.utils'
local movement = require 'modules.navigation.directory-movement'
local ass = require 'modules.ass'
local cursor = require 'modules.navigation.cursor'

---@class controls
local controls = {}

--opens the browser
function controls.open()
    if not g.state.hidden then return end

    for _,v in ipairs(g.state.keybinds) do
        mp.add_forced_key_binding(v[1], 'dynamic/'..v[2], v[3], v[4])
    end

    if o.set_shared_script_properties then utils.shared_script_property_set('file_browser-open', 'yes') end ---@diagnostic disable-line deprecated
    if o.set_user_data then mp.set_property_bool('user-data/file_browser/open', true) end

    if o.toggle_idlescreen then mp.commandv('script-message', 'osc-idlescreen', 'no', 'no_osd') end
    g.state.hidden = false
    if g.state.directory == nil then
        local path = mp.get_property('path')
        if path or o.default_to_working_directory then movement.goto_current_dir() else movement.goto_root() end
        return
    end

    if not g.state.flag_update then ass.draw()
    else g.state.flag_update = false ; ass.update_ass() end
end

--closes the list and sets the hidden flag
function controls.close()
    if g.state.hidden then return end

    for _,v in ipairs(g.state.keybinds) do
        mp.remove_key_binding('dynamic/'..v[2])
    end

    if o.set_shared_script_properties then utils.shared_script_property_set("file_browser-open", "no") end ---@diagnostic disable-line deprecated
    if o.set_user_data then mp.set_property_bool('user-data/file_browser/open', false) end

    if o.toggle_idlescreen then mp.commandv('script-message', 'osc-idlescreen', 'yes', 'no_osd') end
    g.state.hidden = true
    ass.remove()
end

--toggles the list
function controls.toggle()
    if g.state.hidden then controls.open()
    else controls.close() end
end

--run when the escape key is used
function controls.escape()
    --if multiple items are selection cancel the
    --selection instead of closing the browser
    if next(g.state.selection) or g.state.multiselect_start then
        g.state.selection = {}
        cursor.disable_select_mode()
        ass.update_ass()
        return
    end
    controls.close()
end

---opens a specific directory
---@param directory string
---@param open_browser? boolean
---@return thread|nil
function controls.browse_directory(directory, open_browser)
    if not directory then return end
    if open_browser == nil then open_browser = true end

    directory = mp.command_native({"expand-path", directory}, '') --[[@as string]]
    -- directory = join_path( mp.get_property("working-directory", ""), directory )

    if directory ~= "" then directory = fb_utils.fix_path(directory, true) end
    msg.verbose('recieved directory from script message: '..directory)

    directory = fb_utils.resolve_directory_mapping(directory)
    local co = movement.goto_directory(directory, nil, nil, {cache={use=false}})
    if open_browser then controls.open() end
    return co
end

return controls