Skip to content

Commit

Permalink
Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Jun 3, 2024
1 parent c55e9aa commit de3b24d
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 278 deletions.
1 change: 1 addition & 0 deletions yazi-adaptor/src/chafa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Chafa {
height: lines.len() as u16,
};

Adaptor::Chafa.image_hide()?;
Adaptor::shown_store(area);
Emulator::move_lock((max.x, max.y), |stderr| {
for (i, line) in lines.into_iter().enumerate() {
Expand Down
12 changes: 4 additions & 8 deletions yazi-config/preset/yazi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ fetchers = [
]
preloaders = [
# Image
{ mime = "image/svg+xml", run = "magick" },
{ mime = "image/heic", run = "magick" },
{ mime = "image/jxl", run = "magick" },
{ mime = "image/*", run = "image" },
{ mime = "image/{heic,jxl,svg+xml}", run = "magick" },
{ mime = "image/*", run = "image" },
# Video
{ mime = "video/*", run = "video" },
# PDF
Expand All @@ -107,10 +105,8 @@ previewers = [
# JSON
{ mime = "application/json", run = "json" },
# Image
{ mime = "image/svg+xml", run = "magick" },
{ mime = "image/heic", run = "magick" },
{ mime = "image/jxl", run = "magick" },
{ mime = "image/*", run = "image" },
{ mime = "image/{heic,jxl,svg+xml}", run = "magick" },
{ mime = "image/*", run = "image" },
# Video
{ mime = "video/*", run = "video" },
# PDF
Expand Down
41 changes: 4 additions & 37 deletions yazi-fm/src/app/app.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::{collections::VecDeque, sync::atomic::Ordering};

use anyhow::Result;
use components::{Current, Header, Manager, Parent, Preview, Status};
use crossterm::event::{KeyEvent, MouseEvent, MouseEventKind};
use ratatui::layout::Position;
use yazi_config::{keymap::Key, LAYOUT};
use crossterm::event::KeyEvent;
use yazi_config::keymap::Key;
use yazi_core::input::InputMode;
use yazi_shared::{emit, event::{Cmd, Event, NEED_RENDER}, term::Term, Layer};

use crate::{components, lives::Lives, Ctx, Executor, Router, Signals};
use crate::{lives::Lives, Ctx, Executor, Router, Signals};

pub(crate) struct App {
pub(crate) cx: Ctx,
Expand Down Expand Up @@ -58,7 +56,7 @@ impl App {
Event::Seq(cmds, layer) => self.dispatch_seq(cmds, layer),
Event::Render => self.dispatch_render(),
Event::Key(key) => self.dispatch_key(key),
Event::Mouse(mouse) => self.dispatch_mouse(mouse),
Event::Mouse(mouse) => self.mouse(mouse),
Event::Resize => self.resize(()),
Event::Paste(str) => self.dispatch_paste(str),
Event::Quit(opt) => self.quit(opt),
Expand All @@ -85,37 +83,6 @@ impl App {
#[inline]
fn dispatch_key(&mut self, key: KeyEvent) { Router::new(self).route(Key::from(key)); }

#[inline]
fn dispatch_mouse(&self, mouse: MouseEvent) {
let mouse_position = Position { x: mouse.column, y: mouse.row };
let layout = LAYOUT.load();

if layout.header.contains(mouse_position) {
Header.mouse(mouse, layout.header, &self.cx)
} else if layout.manager.contains(mouse_position) {
match mouse.kind {
MouseEventKind::Moved | MouseEventKind::Drag(_) => {
Manager.mouse(mouse, layout.manager, &self.cx)
}
_ => {
if layout.preview.contains(mouse_position) {
Preview::new(&self.cx).mouse(mouse, layout.preview)
} else if layout.current.contains(mouse_position) {
Current.mouse(mouse, layout.current, &self.cx)
} else if layout.parent.contains(mouse_position) {
Parent.mouse(mouse, layout.parent, &self.cx)
} else {
// Out of bounds, ignore
}
}
}
} else if layout.status.contains(mouse_position) {
Status.mouse(mouse, layout.status, &self.cx)
} else {
// Out of bounds, ignore
}
}

#[inline]
fn dispatch_paste(&mut self, str: String) {
if self.cx.input.visible {
Expand Down
1 change: 1 addition & 0 deletions yazi-fm/src/app/commands/mod.rs
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;
Expand Down
57 changes: 57 additions & 0 deletions yazi-fm/src/app/commands/mouse.rs
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);
}
}
}
69 changes: 27 additions & 42 deletions yazi-fm/src/components/current.rs
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(())
}
}
48 changes: 14 additions & 34 deletions yazi-fm/src/components/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use ratatui::{buffer::Buffer, widgets::Widget};
use tracing::error;
use yazi_plugin::{bindings::{Cast, MouseEvent}, elements::{render_widgets, Rect}, LUA};

use crate::lives::Lives;

pub(crate) struct Header;

impl Widget for Header {
Expand All @@ -23,38 +21,20 @@ impl Widget for Header {
}

impl Header {
pub fn mouse(
self,
event: crossterm::event::MouseEvent,
area: ratatui::layout::Rect,
cx: &crate::context::Ctx,
) {
let f = || {
let event = crossterm::event::MouseEvent {
kind: event.kind,
column: event.column - area.x,
row: event.row - area.y,
modifiers: event.modifiers,
};
let kind = event.kind;
let event = MouseEvent::cast(&LUA, event)?;
let comp: Table = LUA.globals().raw_get("Header")?;
_ = Lives::scope(cx, |_| {
match kind {
MouseEventKind::Down(_) => comp.call_method("click", (event, false))?,
MouseEventKind::Up(_) => comp.call_method("click", (event, true))?,
MouseEventKind::ScrollDown => comp.call_method("scroll", (event, 1))?,
MouseEventKind::ScrollUp => comp.call_method("scroll", (event, -1))?,
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("Header")?;

match kind {
MouseEventKind::Down(_) => comp.call_method("click", (event, false))?,
MouseEventKind::Up(_) => comp.call_method("click", (event, true))?,
MouseEventKind::ScrollDown => comp.call_method("scroll", (event, 1))?,
MouseEventKind::ScrollUp => comp.call_method("scroll", (event, -1))?,
MouseEventKind::ScrollRight => comp.call_method("touch", (event, 1))?,
MouseEventKind::ScrollLeft => comp.call_method("touch", (event, -1))?,
_ => (),
}
Ok(())
}
}
59 changes: 21 additions & 38 deletions yazi-fm/src/components/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use ratatui::{buffer::Buffer, widgets::Widget};
use tracing::error;
use yazi_plugin::{bindings::{Cast, MouseEvent}, elements::{render_widgets, Rect}, LUA};

use crate::lives::Lives;

pub(crate) struct Manager;

impl Widget for Manager {
Expand All @@ -23,43 +21,28 @@ impl Widget for Manager {
}

impl Manager {
pub fn mouse(
self,
event: crossterm::event::MouseEvent,
area: ratatui::layout::Rect,
cx: &crate::context::Ctx,
) {
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 manager: Table = LUA.globals().raw_get("Manager")?;
Lives::scope(cx, |_| {
match kind {
// invoke `move(event)`
MouseEventKind::Moved => manager.call_method("move", event)?,
// invoke `drag(event, start)`
MouseEventKind::Drag(_) => {
let mut start: Value = manager.raw_get("drag_start")?;
if start.is_nil() {
start = event.clone().into_lua(&LUA)?;
manager.raw_set("drag_start", start.clone())?;
}
manager.call_method("drag", (event, start))?;
}
_ => (),
pub fn mouse(event: crossterm::event::MouseEvent) -> mlua::Result<()> {
let kind = event.kind;
let event = MouseEvent::cast(&LUA, event)?;
let manager: Table = LUA.globals().raw_get("Manager")?;

match kind {
// Invoke `move(event)`
MouseEventKind::Moved => manager.call_method("move", event)?,

// Invoke `drag(event, start)`
MouseEventKind::Drag(_) => {
let mut start: Value = manager.raw_get("drag_start")?;
if start.is_nil() {
start = event.clone().into_lua(&LUA)?;
manager.raw_set("drag_start", start.clone())?;
}
Ok(())
})?;
Ok::<_, anyhow::Error>(())
};
if let Err(e) = f() {
error!("{:?}", e);
manager.call_method("drag", (event, start))?;
}

_ => (),
}

Ok(())
}
}
Loading

0 comments on commit de3b24d

Please sign in to comment.