nirish

config files for niri window manager
Log | Files | Refs | README | LICENSE

controls.lua (3188B)


      1 
      2 local mp = require 'mp'
      3 local msg = require 'mp.msg'
      4 local utils = require 'mp.utils'
      5 
      6 local o = require 'modules.options'
      7 local g = require 'modules.globals'
      8 local fb_utils = require 'modules.utils'
      9 local movement = require 'modules.navigation.directory-movement'
     10 local ass = require 'modules.ass'
     11 local cursor = require 'modules.navigation.cursor'
     12 
     13 ---@class controls
     14 local controls = {}
     15 
     16 --opens the browser
     17 function controls.open()
     18     if not g.state.hidden then return end
     19 
     20     for _,v in ipairs(g.state.keybinds) do
     21         mp.add_forced_key_binding(v[1], 'dynamic/'..v[2], v[3], v[4])
     22     end
     23 
     24     if o.set_shared_script_properties then utils.shared_script_property_set('file_browser-open', 'yes') end ---@diagnostic disable-line deprecated
     25     if o.set_user_data then mp.set_property_bool('user-data/file_browser/open', true) end
     26 
     27     if o.toggle_idlescreen then mp.commandv('script-message', 'osc-idlescreen', 'no', 'no_osd') end
     28     g.state.hidden = false
     29     if g.state.directory == nil then
     30         local path = mp.get_property('path')
     31         if path or o.default_to_working_directory then movement.goto_current_dir() else movement.goto_root() end
     32         return
     33     end
     34 
     35     if not g.state.flag_update then ass.draw()
     36     else g.state.flag_update = false ; ass.update_ass() end
     37 end
     38 
     39 --closes the list and sets the hidden flag
     40 function controls.close()
     41     if g.state.hidden then return end
     42 
     43     for _,v in ipairs(g.state.keybinds) do
     44         mp.remove_key_binding('dynamic/'..v[2])
     45     end
     46 
     47     if o.set_shared_script_properties then utils.shared_script_property_set("file_browser-open", "no") end ---@diagnostic disable-line deprecated
     48     if o.set_user_data then mp.set_property_bool('user-data/file_browser/open', false) end
     49 
     50     if o.toggle_idlescreen then mp.commandv('script-message', 'osc-idlescreen', 'yes', 'no_osd') end
     51     g.state.hidden = true
     52     ass.remove()
     53 end
     54 
     55 --toggles the list
     56 function controls.toggle()
     57     if g.state.hidden then controls.open()
     58     else controls.close() end
     59 end
     60 
     61 --run when the escape key is used
     62 function controls.escape()
     63     --if multiple items are selection cancel the
     64     --selection instead of closing the browser
     65     if next(g.state.selection) or g.state.multiselect_start then
     66         g.state.selection = {}
     67         cursor.disable_select_mode()
     68         ass.update_ass()
     69         return
     70     end
     71     controls.close()
     72 end
     73 
     74 ---opens a specific directory
     75 ---@param directory string
     76 ---@param open_browser? boolean
     77 ---@return thread|nil
     78 function controls.browse_directory(directory, open_browser)
     79     if not directory then return end
     80     if open_browser == nil then open_browser = true end
     81 
     82     directory = mp.command_native({"expand-path", directory}, '') --[[@as string]]
     83     -- directory = join_path( mp.get_property("working-directory", ""), directory )
     84 
     85     if directory ~= "" then directory = fb_utils.fix_path(directory, true) end
     86     msg.verbose('recieved directory from script message: '..directory)
     87 
     88     directory = fb_utils.resolve_directory_mapping(directory)
     89     local co = movement.goto_directory(directory, nil, nil, {cache={use=false}})
     90     if open_browser then controls.open() end
     91     return co
     92 end
     93 
     94 return controls