You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.0 KiB
105 lines
3.0 KiB
local M = {}
|
|
|
|
local WINDOW_WIDTH = 960
|
|
local WINDOW_HEIGHT = 480
|
|
local POSITION_HORIZONTAL = 800
|
|
local VISIBLE_VERTIVCAL = 40
|
|
local HIDDEN_VERTICAL = -WINDOW_HEIGHT
|
|
|
|
local WS_HIDE = 'special:wsDropdown'
|
|
local TAG_NEW = 'dropdownNew'
|
|
|
|
local window_addrs = {}
|
|
local new_id = nil
|
|
|
|
hl.on('window.open', function (w)
|
|
for _, v in ipairs(w.tags) do
|
|
if v == TAG_NEW .. '*' and new_id then
|
|
window_addrs[new_id] = w.address
|
|
hl.dispatch(hl.dsp.window.tag { tag = '-' .. TAG_NEW, window = 'address:' .. w.address })
|
|
new_id = nil
|
|
return
|
|
end
|
|
end
|
|
end)
|
|
|
|
|
|
local function generate_id(cmd)
|
|
local id = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
|
|
|
local id_idx = 1
|
|
local id_len = #id
|
|
for i = 1, #cmd do
|
|
local byteVal = string.byte(cmd, i)
|
|
id[id_idx] = id[id_idx] ~ byteVal
|
|
id_idx = id_idx % id_len + 1
|
|
end
|
|
|
|
local CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
local CHARS_LEN = #CHARS
|
|
for i = 1, id_len do
|
|
local pos = id[i] % CHARS_LEN + 1
|
|
id[i] = CHARS:sub(pos, pos)
|
|
end
|
|
|
|
return table.concat(id)
|
|
end
|
|
|
|
local function hide_window(window)
|
|
hl.dispatch(hl.dsp.window.pin { action = 'off', window = window })
|
|
hl.dispatch(hl.dsp.window.move { x = POSITION_HORIZONTAL, y = HIDDEN_VERTICAL, relative = false, window = window })
|
|
hl.dispatch(hl.dsp.window.move { workspace = WS_HIDE, follow = false, window = window })
|
|
end
|
|
|
|
local function hide_others_and_show(window)
|
|
for id, addr in pairs(window_addrs) do
|
|
local current_window = 'address:' .. addr
|
|
if current_window ~= window then
|
|
local hl_window = hl.get_window(current_window)
|
|
if hl_window then
|
|
if hl.get_window(current_window).pinned then
|
|
hide_window(current_window)
|
|
end
|
|
else
|
|
window_addrs[id] = nil
|
|
end
|
|
else
|
|
hl.dispatch(hl.dsp.window.move { workspace = 'm+0', follow = true, window = window })
|
|
hl.dispatch(hl.dsp.window.move { x = POSITION_HORIZONTAL, y = VISIBLE_VERTIVCAL, relative = false, window = window })
|
|
hl.dispatch(hl.dsp.window.pin { action = 'on', window = window })
|
|
end
|
|
end
|
|
end
|
|
|
|
M.dsp = {}
|
|
function M.dsp.toggle(cmd)
|
|
assert(cmd)
|
|
return function ()
|
|
local id = generate_id(cmd)
|
|
local addr = window_addrs[id]
|
|
if addr then
|
|
local window = 'address:' .. addr
|
|
local hl_window = hl.get_window(window)
|
|
if hl_window then
|
|
if hl_window.pinned then
|
|
hide_window(window)
|
|
else
|
|
hide_others_and_show(window)
|
|
end
|
|
return
|
|
end
|
|
end
|
|
|
|
hide_others_and_show('')
|
|
new_id = id
|
|
hl.exec_cmd(cmd, {
|
|
float = true,
|
|
size = { WINDOW_WIDTH, WINDOW_HEIGHT },
|
|
move = { POSITION_HORIZONTAL, VISIBLE_VERTIVCAL },
|
|
pin = true,
|
|
tag = TAG_NEW,
|
|
})
|
|
end
|
|
end
|
|
|
|
return M
|
|
|