New Markers component

This commit is contained in:
sxyazi 2026-04-22 19:03:09 +08:00
parent 835a213379
commit 4de2a9cbe2
No known key found for this signature in database
10 changed files with 64 additions and 25 deletions

View file

@ -48,6 +48,15 @@ impl Rect {
r.height = r.height.saturating_sub(pad.top + pad.bottom);
Self(r)
}
fn patch(self, t: Table) -> mlua::Result<Self> {
Ok(Self(ratatui::layout::Rect {
x: t.raw_get::<Option<_>>("x")?.unwrap_or(self.x),
y: t.raw_get::<Option<_>>("y")?.unwrap_or(self.y),
width: t.raw_get::<Option<_>>("w")?.unwrap_or(self.width),
height: t.raw_get::<Option<_>>("h")?.unwrap_or(self.height),
}))
}
}
impl UserData for Rect {
@ -71,5 +80,7 @@ impl UserData for Rect {
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
methods.add_method("pad", |_, me, pad: Pad| Ok(me.pad(pad)));
methods.add_method("contains", |_, me, Self(rect)| Ok(me.contains(rect.into())));
methods.add_meta_method(MetaMethod::Call, |_, me, t: Table| me.patch(t));
}
}

View file

@ -24,7 +24,7 @@ function Marker:redraw()
local y = math.min(self._area.y + last[1], self._area.y + self._area.h) - 1
local rect = ui.Rect {
x = math.max(0, self._area.x - 1),
x = self._area.x,
y = y,
w = 1,
h = math.min(1 + last[2] - last[1], self._area.y + self._area.h - y),

View file

@ -0,0 +1,33 @@
Markers = {
_id = "markers",
}
function Markers:new(chunks, tab)
local me = setmetatable({ _chunks = chunks, _tab = tab }, { __index = self })
me:build()
return me
end
function Markers:build()
self._children = {
Marker:new(self._chunks[1], self._tab.parent),
Marker:new(self._chunks[2], self._tab.current),
}
end
function Markers:reflow() return {} end
function Markers:redraw()
local elements = {}
for _, child in ipairs(self._children) do
elements = ya.list_merge(elements, ui.redraw(child))
end
return elements
end
-- Mouse events
function Markers:click(event, up) end
function Markers:scroll(event, step) end
function Markers:touch(event, step) end

View file

@ -19,15 +19,19 @@ end
-- Mouse events
function Rail:click(event, up) end
function Rail:scroll(event, step) end
function Rail:touch(event, step) end
function Rail:drag(event)
local c, parent, current, preview = self._chunks, 0, 0, 0
local c, x, parent, current, preview = self._chunks, 0, 0, 0, 0
if self._id == "rail-left" then
local x = math.min(event.x, c[2].right - 2)
x = math.min(event.x, c[2].right - 2)
parent = math.max(1, x - c[1].x)
current = math.max(1, c[1].w + c[2].w - parent)
preview = math.max(1, c[3].w)
else
local x = math.max(event.x, c[2].x + 2)
x = math.max(event.x, c[2].x + 2)
preview = math.max(1, c[3].right - x)
current = math.max(1, c[2].w + c[3].w - preview)
parent = math.max(1, c[1].w)
@ -39,7 +43,3 @@ function Rail:drag(event)
ui.render()
end
end
function Rail:scroll(event, step) end
function Rail:touch(event, step) end

View file

@ -11,15 +11,11 @@ end
function Rails:build()
local c, children = self._chunks, {}
if c[1].w > 0 then
children[#children + 1] =
Rail:new("rail-left", ui.Rect { x = c[2].x, y = c[2].y, w = math.min(1, c[2].w), h = c[2].h }, c)
children[#children + 1] = Rail:new("rail-left", c[2] { w = math.min(1, c[2].w) }, c)
end
if c[3].w > 0 then
children[#children + 1] = Rail:new(
"rail-right",
ui.Rect { x = math.max(0, c[2].right - 1), y = c[2].y, w = math.min(1, c[2].w), h = c[2].h },
c
)
children[#children + 1] =
Rail:new("rail-right", c[2] { x = math.max(0, c[2].right - 1), w = math.min(1, c[2].w) }, c)
end
self._children = children
end

View file

@ -29,6 +29,7 @@ function Tab:build()
Current:new(c[2]:pad(ui.Pad.x(1)), self._tab),
Preview:new(c[3]:pad(ui.Pad(0, 1, 0, p)), self._tab),
Rails:new(c, self._tab),
Markers:new(c, self._tab),
}
end

View file

@ -29,12 +29,7 @@ function Tasks:redraw()
break
end
elements[#elements + 1] = ui.Line({ self:icon(snap), snap.title }):area(ui.Rect {
x = self._area.x,
y = y,
w = self._area.w,
h = 1,
})
elements[#elements + 1] = ui.Line({ self:icon(snap), snap.title }):area(self._area { y = y, h = 1 })
if i == cx.tasks.cursor + 1 then
elements[#elements] = elements[#elements]:style(th.tasks.hovered)
@ -107,14 +102,14 @@ function Tasks:progress_redraw(snap, y)
return {
ui.Gauge()
:area(ui.Rect { x = self._chunks[1].x, y = y, w = self._chunks[1].w, h = 1 })
:area(self._chunks[1] { y = y, h = 1 })
:percent(snap.percent)
:label(ui.Span(label):style(th.status.progress_label))
:gauge_style(style),
ui.Line(string.format("%d/%d", snap.prog.success_files, snap.prog.total_files))
:fg("gray")
:area(ui.Rect { x = self._chunks[2].x, y = y, w = self._chunks[2].w, h = 1 })
:area(self._chunks[2] { y = y, h = 1 })
:align(ui.Align.RIGHT),
}
else
@ -127,7 +122,7 @@ function Tasks:progress_redraw(snap, y)
text = "Failed, press Enter to view log…"
end
return {
ui.Line(text):fg("gray"):area(ui.Rect { x = self._chunks[1].x, y = y, w = self._chunks[1].w, h = 1 }),
ui.Line(text):fg("gray"):area(self._chunks[1] { y = y, h = 1 }),
}
end
end

View file

@ -30,10 +30,11 @@ function M:peek(job)
left[#left]:truncate { max = max, ellipsis = entity:ellipsis(max) }
end
local marker_area = job.area { x = math.max(0, job.area.x - 1) }
ya.preview_widget(job, {
ui.List(left):area(job.area),
ui.Text(right):area(job.area):align(ui.Align.RIGHT),
table.unpack(Marker:new(job.area, folder):redraw()),
table.unpack(Marker:new(marker_area, folder):redraw()),
})
end

View file

@ -47,6 +47,7 @@ fn stage_1(lua: &Lua) -> Result<()> {
lua.load(preset!("components/linemode")).set_name("linemode.lua").exec()?;
lua.load(preset!("components/marker")).set_name("marker.lua").exec()?;
lua.load(preset!("components/markers")).set_name("markers.lua").exec()?;
lua.load(preset!("components/modal")).set_name("modal.lua").exec()?;
lua.load(preset!("components/parent")).set_name("parent.lua").exec()?;
lua.load(preset!("components/preview")).set_name("preview.lua").exec()?;

View file

@ -60,6 +60,7 @@ impl Default for Loader {
("header".to_owned(), [][..].into()),
("linemode".to_owned(), [][..].into()),
("marker".to_owned(), [][..].into()),
("markers".to_owned(), [][..].into()),
("modal".to_owned(), [][..].into()),
("parent".to_owned(), [][..].into()),
("preview".to_owned(), [][..].into()),