1- use mlua:: { Table , TableExt } ;
2- use ratatui:: { buffer:: Buffer , widgets:: Widget } ;
1+ use crossterm:: event:: MouseEventKind ;
2+ use mlua:: { IntoLua , Table , TableExt , Value } ;
3+ use ratatui:: { buffer:: Buffer , layout:: Position , widgets:: Widget } ;
34use tracing:: error;
5+ use yazi_config:: Layout ;
46use yazi_plugin:: { bindings:: { Cast , MouseEvent } , elements:: { render_widgets, Rect } , LUA } ;
57
68use crate :: lives:: Lives ;
79
810pub ( crate ) struct Manager ;
911
1012#[ derive( PartialEq , Eq ) ]
11- pub enum ManagerComponent {
12- Parent = 0 ,
13- Current = 1 ,
14- Preview = 2 ,
13+ enum ManagerComponent {
14+ Parent ,
15+ Current ,
16+ Preview ,
17+ Outside ,
1518}
1619
1720impl Widget for Manager {
@@ -29,20 +32,79 @@ impl Widget for Manager {
2932}
3033
3134impl Manager {
32- pub fn mouse ( self , mouse : crossterm:: event:: MouseEvent , area : ratatui:: layout:: Rect , comp : ManagerComponent , cx : & crate :: context:: Ctx ) {
35+ pub fn mouse (
36+ self ,
37+ event : crossterm:: event:: MouseEvent ,
38+ layout : & Layout ,
39+ cx : & crate :: context:: Ctx ,
40+ ) {
41+ let position = Position { x : event. column , y : event. row } ;
42+ let comp = if layout. preview . contains ( position) {
43+ ManagerComponent :: Preview
44+ } else if layout. current . contains ( position) {
45+ ManagerComponent :: Current
46+ } else if layout. parent . contains ( position) {
47+ ManagerComponent :: Parent
48+ } else {
49+ ManagerComponent :: Outside
50+ } ;
51+
52+ let area = match event. kind {
53+ MouseEventKind :: Moved | MouseEventKind :: Drag ( _) => layout. manager ,
54+ _ => match comp {
55+ ManagerComponent :: Parent => layout. parent ,
56+ ManagerComponent :: Current => layout. current ,
57+ ManagerComponent :: Preview => layout. preview ,
58+ ManagerComponent :: Outside => return ,
59+ } ,
60+ } ;
61+ let event = crossterm:: event:: MouseEvent {
62+ kind : event. kind ,
63+ column : event. column - area. x ,
64+ row : event. row - area. y ,
65+ modifiers : event. modifiers ,
66+ } ;
3367 let f = || {
34- let mouse = crossterm:: event:: MouseEvent {
35- kind : mouse. kind ,
36- column : mouse. column - area. x ,
37- row : mouse. row - area. y ,
38- modifiers : mouse. modifiers ,
68+ let kind = event. kind ;
69+ let event = MouseEvent :: cast ( & LUA , event) ?;
70+ let comp: Table = match comp {
71+ ManagerComponent :: Parent => LUA . globals ( ) . raw_get ( "Parent" ) ?,
72+ ManagerComponent :: Current => LUA . globals ( ) . raw_get ( "Current" ) ?,
73+ ManagerComponent :: Preview => LUA . globals ( ) . raw_get ( "Preview" ) ?,
74+ ManagerComponent :: Outside => return Ok ( ( ) ) ,
3975 } ;
40- let mouse = MouseEvent :: cast ( & LUA , mouse) ?;
41- let lua_comp: Table = LUA . globals ( ) . raw_get ( "Manager" ) ?;
42- _ = Lives :: scope ( cx, |_| {
43- lua_comp. call_method ( "mouse" , ( mouse, comp as u8 ) ) ?;
76+ let manager: Table = LUA . globals ( ) . raw_get ( "Manager" ) ?;
77+ Lives :: scope ( cx, |_| {
78+ match kind {
79+ // invoke `click(event, up)`
80+ MouseEventKind :: Down ( _) => {
81+ comp. call_method ( "click" , ( event. clone ( ) , false ) ) ?;
82+ manager. raw_set ( "drag_start" , event) ?; // store the event for dragging
83+ }
84+ MouseEventKind :: Up ( _) => {
85+ comp. call_method ( "click" , ( event, true ) ) ?;
86+ manager. raw_set ( "drag_start" , Value :: Nil ) ?;
87+ }
88+ // invoke `scroll(event, step)`, 1 for down, -1 for up
89+ MouseEventKind :: ScrollDown => comp. call_method ( "scroll" , ( event, 1 ) ) ?,
90+ MouseEventKind :: ScrollUp => comp. call_method ( "scroll" , ( event, -1 ) ) ?,
91+ // invoke `touch(event, step)`, 1 for right, -1 for left
92+ MouseEventKind :: ScrollRight => comp. call_method ( "touch" , ( event, 1 ) ) ?,
93+ MouseEventKind :: ScrollLeft => comp. call_method ( "touch" , ( event, -1 ) ) ?,
94+ // invoke `move(event)`
95+ MouseEventKind :: Moved => manager. call_method ( "move" , event) ?,
96+ // invoke `drag(event, start)`
97+ MouseEventKind :: Drag ( _) => {
98+ let mut start: Value = manager. raw_get ( "drag_start" ) ?;
99+ if start. is_nil ( ) {
100+ start = event. clone ( ) . into_lua ( & LUA ) ?;
101+ manager. raw_set ( "drag_start" , start. clone ( ) ) ?;
102+ }
103+ manager. call_method ( "drag" , ( event, start) ) ?;
104+ }
105+ }
44106 Ok ( ( ) )
45- } ) ;
107+ } ) ? ;
46108 Ok :: < _ , anyhow:: Error > ( ( ) )
47109 } ;
48110 if let Err ( e) = f ( ) {
0 commit comments