-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
202 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod accept_payload; | ||
mod mouse; | ||
mod notify; | ||
mod plugin; | ||
mod quit; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crossterm::event::{MouseEvent, MouseEventKind}; | ||
use ratatui::layout::{Position, Rect}; | ||
use tracing::error; | ||
use yazi_config::LAYOUT; | ||
|
||
use crate::{app::App, components, lives::Lives}; | ||
|
||
pub struct Opt { | ||
event: MouseEvent, | ||
} | ||
|
||
impl From<MouseEvent> for Opt { | ||
fn from(event: MouseEvent) -> Self { Self { event } } | ||
} | ||
|
||
impl App { | ||
pub(crate) fn mouse(&mut self, opt: impl Into<Opt>) { | ||
let event = (opt.into() as Opt).event; | ||
|
||
let layout = LAYOUT.load(); | ||
let position = Position { x: event.column, y: event.row }; | ||
|
||
if matches!(event.kind, MouseEventKind::Moved | MouseEventKind::Drag(_)) { | ||
if layout.manager.contains(position) { | ||
self.mouse_do(components::Manager::mouse, event, layout.manager); | ||
} | ||
return; | ||
} | ||
|
||
if layout.current.contains(position) { | ||
self.mouse_do(components::Current::mouse, event, layout.current); | ||
} else if layout.preview.contains(position) { | ||
self.mouse_do(components::Preview::mouse, event, layout.preview); | ||
} else if layout.parent.contains(position) { | ||
self.mouse_do(components::Parent::mouse, event, layout.parent); | ||
} else if layout.header.contains(position) { | ||
self.mouse_do(components::Header::mouse, event, layout.header); | ||
} else if layout.status.contains(position) { | ||
self.mouse_do(components::Status::mouse, event, layout.status); | ||
} | ||
} | ||
|
||
#[inline] | ||
fn mouse_do( | ||
&self, | ||
f: impl FnOnce(MouseEvent) -> mlua::Result<()>, | ||
mut event: MouseEvent, | ||
rect: Rect, | ||
) { | ||
event.row -= rect.y; | ||
event.column -= rect.x; | ||
|
||
if let Err(e) = Lives::scope(&self.cx, move |_| f(event)) { | ||
error!("{:?}", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,37 @@ | ||
use crossterm::event::MouseEventKind; | ||
use mlua::{Table, TableExt, Value}; | ||
use ratatui::layout::Rect; | ||
use tracing::error; | ||
use yazi_plugin::{bindings::{Cast, MouseEvent}, LUA}; | ||
|
||
use crate::lives::Lives; | ||
|
||
pub(crate) struct Current; | ||
|
||
impl Current { | ||
pub fn mouse(self, event: crossterm::event::MouseEvent, area: Rect, cx: &crate::context::Ctx) { | ||
// Calculate the relative position of the mouse event | ||
let event = crossterm::event::MouseEvent { | ||
kind: event.kind, | ||
column: event.column - area.x, | ||
row: event.row - area.y, | ||
modifiers: event.modifiers, | ||
}; | ||
let f = || { | ||
let kind = event.kind; | ||
let event = MouseEvent::cast(&LUA, event)?; | ||
let comp: Table = LUA.globals().raw_get("Current")?; | ||
let manager: Table = LUA.globals().raw_get("Manager")?; | ||
Lives::scope(cx, |_| { | ||
match kind { | ||
// invoke `click(event, up)` | ||
MouseEventKind::Down(_) => { | ||
comp.call_method("click", (event.clone(), false))?; | ||
manager.raw_set("drag_start", event)?; // store the event for dragging | ||
} | ||
MouseEventKind::Up(_) => { | ||
comp.call_method("click", (event, true))?; | ||
manager.raw_set("drag_start", Value::Nil)?; | ||
} | ||
// invoke `scroll(event, step)`, 1 for down, -1 for up | ||
MouseEventKind::ScrollDown => comp.call_method("scroll", (event, 1))?, | ||
MouseEventKind::ScrollUp => comp.call_method("scroll", (event, -1))?, | ||
// invoke `touch(event, step)`, 1 for right, -1 for left | ||
MouseEventKind::ScrollRight => comp.call_method("touch", (event, 1))?, | ||
MouseEventKind::ScrollLeft => comp.call_method("touch", (event, -1))?, | ||
_ => (), | ||
} | ||
Ok(()) | ||
})?; | ||
Ok::<_, anyhow::Error>(()) | ||
}; | ||
if let Err(e) = f() { | ||
error!("{:?}", e); | ||
pub fn mouse(event: crossterm::event::MouseEvent) -> mlua::Result<()> { | ||
let kind = event.kind; | ||
let event = MouseEvent::cast(&LUA, event)?; | ||
let comp: Table = LUA.globals().raw_get("Current")?; | ||
|
||
match kind { | ||
// Invoke `click(event, up)` | ||
MouseEventKind::Down(_) => { | ||
comp.call_method("click", (event.clone(), false))?; | ||
LUA.globals().raw_get::<_, Table>("Manager")?.raw_set("drag_start", event)?; | ||
} | ||
MouseEventKind::Up(_) => { | ||
comp.call_method("click", (event, true))?; | ||
LUA.globals().raw_get::<_, Table>("Manager")?.raw_set("drag_start", Value::Nil)?; | ||
} | ||
|
||
// Invoke `scroll(event, step)`, 1 for down, -1 for up | ||
MouseEventKind::ScrollDown => comp.call_method("scroll", (event, 1))?, | ||
MouseEventKind::ScrollUp => comp.call_method("scroll", (event, -1))?, | ||
|
||
// Invoke `touch(event, step)`, 1 for right, -1 for left | ||
MouseEventKind::ScrollRight => comp.call_method("touch", (event, 1))?, | ||
MouseEventKind::ScrollLeft => comp.call_method("touch", (event, -1))?, | ||
|
||
_ => (), | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.