From 53077f157cef239798c487a305336720906b3282 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 8 Apr 2025 15:33:23 +0800 Subject: [PATCH] feat: new `ui.Text:scroll()` API for setting text to scroll horizontally or vertically --- yazi-plugin/src/elements/text.rs | 14 +++++++++----- yazi-plugin/src/utils/preview.rs | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/yazi-plugin/src/elements/text.rs b/yazi-plugin/src/elements/text.rs index d9014dbe..5aa0b355 100644 --- a/yazi-plugin/src/elements/text.rs +++ b/yazi-plugin/src/elements/text.rs @@ -23,9 +23,9 @@ pub struct Text { pub area: Area, // TODO: block - pub inner: ratatui::text::Text<'static>, - pub wrap: u8, - // TODO: scroll + pub inner: ratatui::text::Text<'static>, + pub wrap: u8, + pub scroll: ratatui::layout::Position, } impl Text { @@ -58,7 +58,7 @@ impl Text { trans: impl Fn(yazi_config::popup::Position) -> ratatui::layout::Rect, ) { let rect = self.area.transform(trans); - if self.wrap == WRAP_NO { + if self.wrap == WRAP_NO && self.scroll == Default::default() { self.inner.render(rect, buf); } else { ratatui::widgets::Paragraph::from(self).render(rect, buf); @@ -130,7 +130,7 @@ impl From for ratatui::widgets::Paragraph<'static> { if value.wrap != WRAP_NO { p = p.wrap(ratatui::widgets::Wrap { trim: value.wrap == WRAP_TRIM }); } - p + p.scroll((value.scroll.y, value.scroll.x)) } } @@ -155,6 +155,10 @@ impl UserData for Text { }; Ok(ud) }); + methods.add_function_mut("scroll", |_, (ud, x, y): (AnyUserData, u16, u16)| { + ud.borrow_mut::()?.scroll = ratatui::layout::Position { x, y }; + Ok(ud) + }); methods.add_method("max_width", |_, me, ()| { Ok(me.inner.lines.iter().take(me.area.size().height as usize).map(|l| l.width()).max()) }); diff --git a/yazi-plugin/src/utils/preview.rs b/yazi-plugin/src/utils/preview.rs index 4452b4dd..2136d1c2 100644 --- a/yazi-plugin/src/utils/preview.rs +++ b/yazi-plugin/src/utils/preview.rs @@ -52,6 +52,7 @@ impl Utils { area, inner, wrap: if YAZI.preview.wrap == PreviewWrap::Yes { WRAP } else { WRAP_NO }, + scroll: Default::default(), })]; emit!(Call(Cmd::new("mgr:update_peeked").with_any("lock", lock)));