1
1
use crossterm:: event:: { MouseEvent , MouseEventKind } ;
2
- use mlua:: Table ;
3
- use ratatui:: layout:: { Position , Rect } ;
2
+ use mlua:: { Table , TableExt } ;
4
3
use tracing:: error;
5
- use yazi_config:: { LAYOUT , MANAGER } ;
4
+ use yazi_config:: MANAGER ;
6
5
use yazi_plugin:: { bindings:: Cast , LUA } ;
7
6
8
- use crate :: { app:: App , components , lives:: Lives } ;
7
+ use crate :: { app:: App , lives:: Lives } ;
9
8
10
9
pub struct Opt {
11
10
event : MouseEvent ,
@@ -19,47 +18,36 @@ impl App {
19
18
pub ( crate ) fn mouse ( & mut self , opt : impl Into < Opt > ) {
20
19
let event = ( opt. into ( ) as Opt ) . event ;
21
20
22
- let layout = LAYOUT . load ( ) ;
23
- let position = Position { x : event. column , y : event . row } ;
21
+ let Some ( size ) = self . term . as_ref ( ) . and_then ( |t| t . size ( ) . ok ( ) ) else { return } ;
22
+ let Ok ( evt ) = yazi_plugin :: bindings :: MouseEvent :: cast ( & LUA , event) else { return } ;
24
23
25
- if matches ! ( event. kind, MouseEventKind :: Moved | MouseEventKind :: Drag ( _) ) {
26
- self . mouse_do ( crate :: Root :: mouse, event, None ) ;
27
- return ;
28
- }
24
+ let res = Lives :: scope ( & self . cx , move |_| {
25
+ let area = yazi_plugin:: elements:: Rect :: cast ( & LUA , size) ?;
26
+ let root = LUA . globals ( ) . raw_get :: < _ , Table > ( "Root" ) ?. call_method :: < _ , Table > ( "new" , area) ?;
29
27
30
- if layout. current . contains ( position) {
31
- self . mouse_do ( components:: Current :: mouse, event, Some ( layout. current ) ) ;
32
- } else if layout. preview . contains ( position) {
33
- self . mouse_do ( components:: Preview :: mouse, event, Some ( layout. preview ) ) ;
34
- } else if layout. parent . contains ( position) {
35
- self . mouse_do ( components:: Parent :: mouse, event, Some ( layout. parent ) ) ;
36
- } else if layout. header . contains ( position) {
37
- self . mouse_do ( components:: Header :: mouse, event, Some ( layout. header ) ) ;
38
- } else if layout. status . contains ( position) {
39
- self . mouse_do ( components:: Status :: mouse, event, Some ( layout. status ) ) ;
40
- }
41
- }
28
+ if matches ! ( event. kind, MouseEventKind :: Down ( _) if MANAGER . mouse_events. draggable( ) ) {
29
+ root. raw_set ( "_drag_start" , evt. clone ( ) ) ?;
30
+ }
31
+
32
+ match event. kind {
33
+ MouseEventKind :: Down ( _) => root. call_method ( "click" , ( evt, false ) ) ?,
34
+ MouseEventKind :: Up ( _) => root. call_method ( "click" , ( evt, true ) ) ?,
42
35
43
- fn mouse_do (
44
- & self ,
45
- f : impl FnOnce ( MouseEvent ) -> mlua:: Result < ( ) > ,
46
- mut event : MouseEvent ,
47
- rect : Option < Rect > ,
48
- ) {
49
- if matches ! ( event. kind, MouseEventKind :: Down ( _) if MANAGER . mouse_events. draggable( ) ) {
50
- let evt = yazi_plugin:: bindings:: MouseEvent :: cast ( & LUA , event) ;
51
- if let ( Ok ( evt) , Ok ( root) ) = ( evt, LUA . globals ( ) . raw_get :: < _ , Table > ( "Root" ) ) {
52
- root. raw_set ( "drag_start" , evt) . ok ( ) ;
36
+ MouseEventKind :: ScrollDown => root. call_method ( "scroll" , ( evt, 1 ) ) ?,
37
+ MouseEventKind :: ScrollUp => root. call_method ( "scroll" , ( evt, -1 ) ) ?,
38
+
39
+ MouseEventKind :: ScrollRight => root. call_method ( "touch" , ( evt, 1 ) ) ?,
40
+ MouseEventKind :: ScrollLeft => root. call_method ( "touch" , ( evt, -1 ) ) ?,
41
+
42
+ MouseEventKind :: Moved => root. call_method ( "move" , evt) ?,
43
+ MouseEventKind :: Drag ( _) => root. call_method ( "drag" , evt) ?,
53
44
}
54
- }
55
45
56
- if let Some ( rect) = rect {
57
- event. row -= rect. y ;
58
- event. column -= rect. x ;
59
- }
46
+ Ok ( ( ) )
47
+ } ) ;
60
48
61
- if let Err ( e) = Lives :: scope ( & self . cx , move |_| f ( event ) ) {
62
- error ! ( "{:?}" , e ) ;
49
+ if let Err ( e) = res {
50
+ error ! ( "{e}" ) ;
63
51
}
64
52
}
65
53
}
0 commit comments