Skip to content

Commit

Permalink
refactor: turn RectRef, PaddingRef, PositionRef into Rect, `P…
Browse files Browse the repository at this point in the history
…adding`, `Position` (#1730)
  • Loading branch information
sxyazi authored Oct 4, 2024
1 parent 8c5d8aa commit 3150fb1
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 238 deletions.
2 changes: 1 addition & 1 deletion yazi-fm/src/app/commands/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl App {
let Ok(evt) = yazi_plugin::bindings::MouseEvent::cast(&LUA, event) else { return };

let res = Lives::scope(&self.cx, move |_| {
let area = yazi_plugin::elements::Rect::cast(&LUA, size)?;
let area = yazi_plugin::elements::Rect::from(size);
let root = LUA.globals().raw_get::<_, Table>("Root")?.call_method::<_, Table>("new", area)?;

if matches!(event.kind, MouseEventKind::Down(_) if MANAGER.mouse_events.draggable()) {
Expand Down
4 changes: 2 additions & 2 deletions yazi-fm/src/root.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mlua::{Table, TableExt};
use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
use tracing::error;
use yazi_plugin::{LUA, bindings::Cast, elements::render_widgets};
use yazi_plugin::{LUA, elements::render_widgets};

use super::{completion, confirm, input, select, tasks, which};
use crate::{Ctx, components, help};
Expand All @@ -17,7 +17,7 @@ impl<'a> Root<'a> {
impl<'a> Widget for Root<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let mut f = || {
let area = yazi_plugin::elements::Rect::cast(&LUA, area)?;
let area = yazi_plugin::elements::Rect::from(area);
let root = LUA.globals().raw_get::<_, Table>("Root")?.call_method::<_, Table>("new", area)?;

render_widgets(root.call_method("render", ())?, buf);
Expand Down
14 changes: 7 additions & 7 deletions yazi-plugin/src/elements/bar.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use mlua::{AnyUserData, Lua, Table, UserData};
use ratatui::widgets::Borders;

use super::{RectRef, Renderable};
use super::{Rect, Renderable};

#[derive(Clone)]
pub struct Bar {
area: ratatui::layout::Rect,
area: Rect,

direction: ratatui::widgets::Borders,
symbol: String,
Expand All @@ -14,13 +14,13 @@ pub struct Bar {

impl Bar {
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
let new = lua.create_function(|_, (_, area, direction): (Table, RectRef, u8)| {
let new = lua.create_function(|_, (_, area, direction): (Table, Rect, u8)| {
Ok(Self {
area: *area,
area,

direction: Borders::from_bits_truncate(direction),
symbol: Default::default(),
style: Default::default(),
symbol: Default::default(),
style: Default::default(),
})
})?;

Expand Down Expand Up @@ -57,7 +57,7 @@ impl UserData for Bar {
}

impl Renderable for Bar {
fn area(&self) -> ratatui::layout::Rect { self.area }
fn area(&self) -> ratatui::layout::Rect { *self.area }

fn render(self: Box<Self>, buf: &mut ratatui::buffer::Buffer) {
if self.area.area() == 0 {
Expand Down
12 changes: 6 additions & 6 deletions yazi-plugin/src/elements/border.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mlua::{AnyUserData, Lua, Table, UserData};
use ratatui::widgets::{Borders, Widget};

use super::{RectRef, Renderable};
use super::{Rect, Renderable};

// Type
const PLAIN: u8 = 0;
Expand All @@ -13,7 +13,7 @@ const QUADRANT_OUTSIDE: u8 = 5;

#[derive(Clone, Default)]
pub struct Border {
area: ratatui::layout::Rect,
area: Rect,

position: ratatui::widgets::Borders,
type_: ratatui::widgets::BorderType,
Expand All @@ -22,9 +22,9 @@ pub struct Border {

impl Border {
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
let new = lua.create_function(|_, (_, area, position): (Table, RectRef, u8)| {
let new = lua.create_function(|_, (_, area, position): (Table, Rect, u8)| {
Ok(Border {
area: *area,
area,
position: ratatui::widgets::Borders::from_bits_truncate(position),
..Default::default()
})
Expand Down Expand Up @@ -77,14 +77,14 @@ impl UserData for Border {
}

impl Renderable for Border {
fn area(&self) -> ratatui::layout::Rect { self.area }
fn area(&self) -> ratatui::layout::Rect { *self.area }

fn render(self: Box<Self>, buf: &mut ratatui::buffer::Buffer) {
ratatui::widgets::Block::default()
.borders(self.position)
.border_type(self.type_)
.border_style(self.style)
.render(self.area, buf);
.render(*self.area, buf);
}

fn clone_render(&self, buf: &mut ratatui::buffer::Buffer) { Box::new(self.clone()).render(buf); }
Expand Down
21 changes: 10 additions & 11 deletions yazi-plugin/src/elements/clear.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use std::sync::atomic::{AtomicBool, Ordering};

use mlua::{Lua, Table, UserData};
use ratatui::layout::Rect;
use yazi_adapter::ADAPTOR;

use super::{RectRef, Renderable};
use super::{Rect, Renderable};

pub static COLLISION: AtomicBool = AtomicBool::new(false);

#[derive(Clone, Copy, Default)]
pub struct Clear {
pub area: ratatui::layout::Rect,
pub area: Rect,
}

impl Clear {
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
let new = lua.create_function(|_, (_, area): (Table, RectRef)| Ok(Clear { area: *area }))?;
let new = lua.create_function(|_, (_, area): (Table, Rect)| Ok(Clear { area }))?;

let clear = lua.create_table()?;
clear.set_metatable(Some(lua.create_table_from([("__call", new)])?));
Expand All @@ -25,13 +24,13 @@ impl Clear {
}

impl ratatui::widgets::Widget for Clear {
fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer)
fn render(self, area: ratatui::layout::Rect, buf: &mut ratatui::prelude::Buffer)
where
Self: Sized,
{
ratatui::widgets::Clear.render(area, buf);

let Some(r) = ADAPTOR.shown_load().and_then(|r| overlap(&area, &r)) else {
let Some(r) = ADAPTOR.shown_load().and_then(|r| overlap(area, r)) else {
return;
};

Expand All @@ -46,10 +45,10 @@ impl ratatui::widgets::Widget for Clear {
}

impl Renderable for Clear {
fn area(&self) -> ratatui::layout::Rect { self.area }
fn area(&self) -> ratatui::layout::Rect { *self.area }

fn render(self: Box<Self>, buf: &mut ratatui::buffer::Buffer) {
<Self as ratatui::widgets::Widget>::render(Default::default(), self.area, buf);
<Self as ratatui::widgets::Widget>::render(Default::default(), *self.area, buf);
}

fn clone_render(&self, buf: &mut ratatui::buffer::Buffer) { Box::new(*self).render(buf); }
Expand All @@ -62,11 +61,11 @@ impl UserData for Clear {
}

#[inline]
const fn is_overlapping(a: &Rect, b: &Rect) -> bool {
const fn is_overlapping(a: ratatui::layout::Rect, b: ratatui::layout::Rect) -> bool {
a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y
}

fn overlap(a: &Rect, b: &Rect) -> Option<Rect> {
fn overlap(a: ratatui::layout::Rect, b: ratatui::layout::Rect) -> Option<ratatui::layout::Rect> {
if !is_overlapping(a, b) {
return None;
}
Expand All @@ -75,5 +74,5 @@ fn overlap(a: &Rect, b: &Rect) -> Option<Rect> {
let y = a.y.max(b.y);
let width = (a.x + a.width).min(b.x + b.width) - x;
let height = (a.y + a.height).min(b.y + b.height) - y;
Some(Rect { x, y, width, height })
Some(ratatui::layout::Rect { x, y, width, height })
}
59 changes: 30 additions & 29 deletions yazi-plugin/src/elements/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ pub struct Constraint(pub(super) ratatui::layout::Constraint);

impl Constraint {
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
let constraint = lua.create_table()?;

constraint.raw_set(
"Min",
lua.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Min(n))))?,
)?;
constraint.raw_set(
"Max",
lua.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Max(n))))?,
)?;
constraint.raw_set(
"Length",
lua.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Length(n))))?,
)?;
constraint.raw_set(
"Percentage",
lua
.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Percentage(n))))?,
)?;
constraint.raw_set(
"Ratio",
lua.create_function(|_, (a, b): (u32, u32)| {
Ok(Constraint(ratatui::layout::Constraint::Ratio(a, b)))
})?,
)?;
constraint.raw_set(
"Fill",
lua.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Fill(n))))?,
)?;
let constraint = lua.create_table_from([
(
"Min",
lua.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Min(n))))?,
),
(
"Max",
lua.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Max(n))))?,
),
(
"Length",
lua.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Length(n))))?,
),
(
"Percentage",
lua.create_function(|_, n: u16| {
Ok(Constraint(ratatui::layout::Constraint::Percentage(n)))
})?,
),
(
"Ratio",
lua.create_function(|_, (a, b): (u32, u32)| {
Ok(Constraint(ratatui::layout::Constraint::Ratio(a, b)))
})?,
),
(
"Fill",
lua.create_function(|_, n: u16| Ok(Constraint(ratatui::layout::Constraint::Fill(n))))?,
),
])?;

ui.raw_set("Constraint", constraint)
}
Expand Down
4 changes: 0 additions & 4 deletions yazi-plugin/src/elements/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ use crate::cast_to_renderable;
pub fn pour(lua: &Lua) -> mlua::Result<()> {
let ui = lua.create_table()?;

// Register
super::Padding::register(lua)?;
super::Rect::register(lua)?;

// Install
super::Bar::install(lua, &ui)?;
super::Border::install(lua, &ui)?;
Expand Down
10 changes: 5 additions & 5 deletions yazi-plugin/src/elements/gauge.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataMethods, Value};
use ratatui::widgets::Widget;

use super::{RectRef, Renderable, Span};
use super::{Rect, Renderable, Span};
use crate::elements::Style;

#[derive(Clone, Default)]
pub struct Gauge {
area: ratatui::layout::Rect,
area: Rect,

ratio: f64,
label: Option<ratatui::text::Span<'static>>,
Expand All @@ -18,7 +18,7 @@ impl Gauge {
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
ui.raw_set(
"Gauge",
lua.create_function(|_, area: RectRef| Ok(Gauge { area: *area, ..Default::default() }))?,
lua.create_function(|_, area: Rect| Ok(Gauge { area, ..Default::default() }))?,
)
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl UserData for Gauge {
}

impl Renderable for Gauge {
fn area(&self) -> ratatui::layout::Rect { self.area }
fn area(&self) -> ratatui::layout::Rect { *self.area }

fn render(self: Box<Self>, buf: &mut ratatui::buffer::Buffer) {
let mut gauge = ratatui::widgets::Gauge::default()
Expand All @@ -71,7 +71,7 @@ impl Renderable for Gauge {
gauge = gauge.label(s)
}

gauge.render(self.area, buf);
gauge.render(*self.area, buf);
}

fn clone_render(&self, buf: &mut ratatui::buffer::Buffer) { Box::new(self.clone()).render(buf) }
Expand Down
11 changes: 3 additions & 8 deletions yazi-plugin/src/elements/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use mlua::{AnyUserData, Lua, Table, UserData, UserDataMethods};

use super::{Constraint, Rect, RectRef};
use crate::bindings::Cast;
use super::{Constraint, Rect};

const HORIZONTAL: bool = true;
const VERTICAL: bool = false;
Expand Down Expand Up @@ -61,7 +60,7 @@ impl UserData for Layout {
ud.borrow_mut::<Self>()?.constraints = value.into_iter().map(|c| c.0).collect();
Ok(ud)
});
methods.add_method("split", |lua, me, value: RectRef| {
methods.add_method("split", |lua, me, value: Rect| {
let mut layout = ratatui::layout::Layout::new(
if me.direction == VERTICAL {
ratatui::layout::Direction::Vertical
Expand All @@ -76,11 +75,7 @@ impl UserData for Layout {
layout = layout.vertical_margin(margin.vertical);
}

let mut chunks = Vec::with_capacity(me.constraints.len());
for chunk in &*layout.split(*value) {
chunks.push(Rect::cast(lua, *chunk)?);
}
Ok(chunks)
lua.create_sequence_from(layout.split(*value).iter().map(|chunk| Rect::from(*chunk)))
});
}
}
12 changes: 6 additions & 6 deletions yazi-plugin/src/elements/list.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use mlua::{ExternalError, FromLua, Lua, Table, UserData, Value};
use ratatui::widgets::Widget;

use super::{Line, RectRef, Renderable, Span};
use super::{Line, Rect, Renderable, Span};

// --- List
#[derive(Clone)]
pub struct List {
area: ratatui::layout::Rect,
area: Rect,

inner: ratatui::widgets::List<'static>,
}
Expand All @@ -15,8 +15,8 @@ impl List {
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
ui.raw_set(
"List",
lua.create_function(|_, (area, items): (RectRef, Vec<ListItem>)| {
Ok(Self { area: *area, inner: ratatui::widgets::List::new(items) })
lua.create_function(|_, (area, items): (Rect, Vec<ListItem>)| {
Ok(Self { area, inner: ratatui::widgets::List::new(items) })
})?,
)
}
Expand All @@ -29,10 +29,10 @@ impl UserData for List {
}

impl Renderable for List {
fn area(&self) -> ratatui::layout::Rect { self.area }
fn area(&self) -> ratatui::layout::Rect { *self.area }

fn render(self: Box<Self>, buf: &mut ratatui::buffer::Buffer) {
self.inner.render(self.area, buf);
self.inner.render(*self.area, buf);
}

fn clone_render(&self, buf: &mut ratatui::buffer::Buffer) { Box::new(self.clone()).render(buf) }
Expand Down
Loading

0 comments on commit 3150fb1

Please sign in to comment.