yazi/yazi-plugin/preset/components/current.lua
2026-05-28 12:05:06 +08:00

103 lines
2.3 KiB
Lua

Current = {
_id = "current",
_dragging = false,
_dropping = false,
}
function Current:new(area, tab)
return setmetatable({
_area = area,
_tab = tab,
_folder = tab.current,
}, { __index = self })
end
function Current:empty()
local s
if self._folder.files.filter then
s = "No filter results"
else
local done, err = self._folder.stage()
s = not done and "Loading..." or not err and "No items" or string.format("Error: %s", err)
end
return {
ui.Text(s):area(self._area):align(ui.Align.CENTER):wrap(ui.Wrap.YES),
}
end
function Current:dropping()
if Current._dropping then
return Tip:new(self._area, "Drop to move here…"):redraw()
else
return {}
end
end
function Current:reflow() return { self } end
function Current:redraw()
local files = self._folder.window
if #files == 0 then
return self:empty()
end
local left, right = {}, {}
for _, f in ipairs(files) do
local entity = Entity:new(f)
left[#left + 1], right[#right + 1] = entity:redraw(), Linemode:new(f):redraw()
local max = math.max(0, self._area.w - right[#right]:width())
left[#left]:truncate { max = max, ellipsis = entity:ellipsis(max) }
end
return {
ui.List(left):area(self._area),
ui.Text(right):area(self._area):align(ui.Align.RIGHT),
table.unpack(self:dropping()),
}
end
-- Mouse events
function Current:click(event, up)
if up or event.is_middle then
return
end
local y = event.y - self._area.y + 1
if self._folder.window[y] then
Entity:new(self._folder.window[y]):click(event, up)
end
end
function Current:scroll(event, step) ya.emit("arrow", { step }) end
function Current:touch(event, step) end
function Current:drag(event)
if event.type == "offer" then
Current._dragging = require("dnd").offer_uri_list()
elseif event.type == "end" or event.type == "error" then
Current._dragging = false
end
end
function Current:drop(event)
if Current._dragging then
return
elseif event.type == "enter" then
rt.tty:queue("ConfirmDrop", { type = "move", mimes = { "text/uri-list" } })
elseif event.type == "ready" then
rt.tty:queue("StartDrop", { idx = 1 })
elseif event.type == "arrive" then
rt.tty:queue("FinishDrop", { type = "move" })
require("dnd").cut_uri_list(event.data)
end
rt.tty:flush()
local d = event.type == "enter"
if Current._dropping ~= d then
Current._dropping = d
ui.render()
end
end