Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Jul 8, 2024
1 parent 427d117 commit c7cf294
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions yazi-plugin/preset/components/current.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function Current:click(event, up)
return
end

-- TODO
ya.manager_emit("arrow", { event.y + f.offset - f.hovered.idx })
if event.is_right then
ya.manager_emit("open", {})
Expand Down
1 change: 1 addition & 0 deletions yazi-plugin/preset/components/header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function Header:render()
}
end

-- Mouse events
function Header:click(event, up) end

function Header:scroll(event, step) end
Expand Down
1 change: 1 addition & 0 deletions yazi-plugin/preset/components/parent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function Parent:click(event, up)
return
end

-- TODO
local window = self._folder and self._folder.window or {}
if window[event.y] then
ya.manager_emit("reveal", { window[event.y].url })
Expand Down
1 change: 1 addition & 0 deletions yazi-plugin/preset/components/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Preview:click(event, up)
return
end

-- TODO
local window = self._folder and self._folder.window or {}
if window[event.y] then
ya.manager_emit("reveal", { window[event.y].url })
Expand Down
7 changes: 7 additions & 0 deletions yazi-plugin/preset/components/rail.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ function Rail:render()
end
return children
end

-- Mouse events
function Rail:click(event, up) end

function Rail:scroll(event, step) end

function Rail:touch(event, step) end
15 changes: 15 additions & 0 deletions yazi-plugin/preset/components/root.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ function Root:render()
end

-- Mouse events
function Root:click(event, up)
local c = ya.child_at(ui.Position { x = event.x, y = event.y }, self._children)
return c and c:click(event, up)
end

function Root:scroll(event, step)
local c = ya.child_at(ui.Position { x = event.x, y = event.y }, self._children)
return c and c:scroll(event, step)
end

function Root:touch(event, step)
local c = ya.child_at(ui.Position { x = event.x, y = event.y }, self._children)
return c and c:touch(event, step)
end

function Root:move(event) end

function Root:drag(event) end
16 changes: 16 additions & 0 deletions yazi-plugin/preset/components/tab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ function Tab:layout()
})
:split(self._area)
end

-- Mouse events
function Tab:click(event, up)
local c = ya.child_at(ui.Position { x = event.x, y = event.y }, self._children)
return c and c:click(event, up)
end

function Tab:scroll(event, step)
local c = ya.child_at(ui.Position { x = event.x, y = event.y }, self._children)
return c and c:scroll(event, step)
end

function Tab:touch(event, step)
local c = ya.child_at(ui.Position { x = event.x, y = event.y }, self._children)
return c and c:touch(event, step)
end
8 changes: 8 additions & 0 deletions yazi-plugin/preset/ya.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ function ya.readable_path(path)
return path
end
end

function child_at(position, children)
for i = children, 1, -1 do
if children[i]._area:contains(position) then
return children[i]
end
end
end
4 changes: 2 additions & 2 deletions yazi-plugin/src/bindings/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub struct MouseEvent;
impl MouseEvent {
pub fn register(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<crossterm::event::MouseEvent>(|reg| {
reg.add_field_method_get("x", |_, me| Ok(me.column as u32 + 1));
reg.add_field_method_get("y", |_, me| Ok(me.row as u32 + 1));
reg.add_field_method_get("x", |_, me| Ok(me.column));
reg.add_field_method_get("y", |_, me| Ok(me.row));
reg.add_field_method_get("is_left", |_, me| {
use crossterm::event::MouseEventKind as K;
Ok(matches!(me.kind, K::Down(b) | K::Up(b) | K::Drag(b) if b == MouseButton::Left))
Expand Down
1 change: 1 addition & 0 deletions yazi-plugin/src/elements/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn pour(lua: &Lua) -> mlua::Result<()> {
super::ListItem::install(lua, &ui)?;
super::Padding::install(lua, &ui)?;
super::Paragraph::install(lua, &ui)?;
super::Position::install(lua, &ui)?;
super::Rect::install(lua, &ui)?;
super::Span::install(lua, &ui)?;
super::Style::install(lua, &ui)?;
Expand Down
2 changes: 2 additions & 0 deletions yazi-plugin/src/elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod line;
mod list;
mod padding;
mod paragraph;
mod position;
mod rect;
mod span;
mod style;
Expand All @@ -26,6 +27,7 @@ pub use line::*;
pub use list::*;
pub use padding::*;
pub use paragraph::*;
pub use position::*;
pub use rect::*;
pub use span::*;
pub use style::*;
38 changes: 38 additions & 0 deletions yazi-plugin/src/elements/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use mlua::{AnyUserData, Lua, Table, UserDataFields, UserDataRef};

use crate::bindings::Cast;

pub type PositionRef<'lua> = UserDataRef<'lua, ratatui::layout::Position>;

pub struct Position;

impl Position {
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
let new = lua.create_function(|lua, (_, args): (Table, Table)| {
Position::cast(lua, ratatui::layout::Position {
x: args.raw_get("x")?,
y: args.raw_get("y")?,
})
})?;

let position =
lua.create_table_from([("default", Position::cast(lua, Default::default())?)])?;

position.set_metatable(Some(lua.create_table_from([("__call", new)])?));

ui.raw_set("Position", position)
}

pub fn register(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<ratatui::layout::Position>(|reg| {
reg.add_field_method_get("x", |_, me| Ok(me.x));
reg.add_field_method_get("y", |_, me| Ok(me.y));
})
}
}

impl Cast<ratatui::layout::Position> for Position {
fn cast(lua: &Lua, data: ratatui::layout::Position) -> mlua::Result<AnyUserData> {
lua.create_any_userdata(data)
}
}
4 changes: 3 additions & 1 deletion yazi-plugin/src/elements/rect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use mlua::{AnyUserData, Lua, Table, UserDataFields, UserDataMethods, UserDataRef};

use super::PaddingRef;
use super::{PaddingRef, Position, PositionRef};
use crate::bindings::Cast;

pub type RectRef<'lua> = UserDataRef<'lua, ratatui::layout::Rect>;
Expand Down Expand Up @@ -46,6 +46,8 @@ impl Rect {
r.height = r.height.saturating_sub(padding.top + padding.bottom);
Rect::cast(lua, r)
});
reg.add_method("position", |lua, me, ()| Position::cast(lua, me.as_position()));
reg.add_method("contains", |_, me, position: PositionRef| Ok(me.contains(*position)));
})
}
}
Expand Down

0 comments on commit c7cf294

Please sign in to comment.