chore: rename RectShim to Offset

This commit is contained in:
Hanaasagi 2023-11-14 19:25:03 +09:00 committed by sxyazi
parent 6d8a5469bd
commit 279378e8f1
No known key found for this signature in database
5 changed files with 41 additions and 41 deletions

View file

@ -2,7 +2,7 @@ use crossterm::terminal::WindowSize;
use ratatui::prelude::Rect;
use yazi_shared::Term;
use crate::{completion::Completion, help::Help, input::Input, manager::Manager, select::Select, tasks::Tasks, which::Which, Position, RectShim};
use crate::{completion::Completion, help::Help, input::Input, manager::Manager, select::Select, tasks::Tasks, which::Which, Position, Offset};
pub struct Ctx {
pub manager: Manager,
@ -31,7 +31,7 @@ impl Ctx {
let WindowSize { columns, rows, .. } = Term::size();
let (x, y) = match *pos {
Position::TopLeft(RectShim { x_offset, y_offset, width, height }) => {
Position::TopLeft(Offset { x_offset, y_offset, width, height }) => {
let right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height);
@ -43,7 +43,7 @@ impl Ctx {
(x, y)
}
Position::TopRight(RectShim { x_offset, y_offset, width, height }) => {
Position::TopRight(Offset { x_offset, y_offset, width, height }) => {
let right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height);
@ -55,7 +55,7 @@ impl Ctx {
(x, y)
}
Position::TopCenter(RectShim { x_offset, y_offset, width, height }) => {
Position::TopCenter(Offset { x_offset, y_offset, width, height }) => {
let right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height);
@ -67,7 +67,7 @@ impl Ctx {
(x, y)
}
Position::Center(RectShim { x_offset, y_offset, width, height }) => {
Position::Center(Offset { x_offset, y_offset, width, height }) => {
let right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height);
@ -79,7 +79,7 @@ impl Ctx {
(x, y)
}
Position::BottomCenter(RectShim { x_offset, y_offset, width, height }) => {
Position::BottomCenter(Offset { x_offset, y_offset, width, height }) => {
let right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height);
@ -91,7 +91,7 @@ impl Ctx {
(x, y)
}
Position::BottomLeft(RectShim { x_offset, y_offset, width, height }) => {
Position::BottomLeft(Offset { x_offset, y_offset, width, height }) => {
let right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height);
@ -103,7 +103,7 @@ impl Ctx {
(x, y)
}
Position::BottomRight(RectShim { x_offset, y_offset, width, height }) => {
Position::BottomRight(Offset { x_offset, y_offset, width, height }) => {
let right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height);
@ -124,7 +124,7 @@ impl Ctx {
Position::TopCenter(rect_shim)
});
}
Position::Sticky(RectShim { x_offset, y_offset, width, height }, r) => {
Position::Sticky(Offset { x_offset, y_offset, width, height }, r) => {
// TODO:
unimplemented!("Position::Sticky is not implemented");
// let mut x = columns.saturating_sub(width);

View file

@ -1,6 +1,6 @@
use yazi_config::popup::{Offset as CfgOffset, Position as CfgPosition};
use crate::{Position, RectShim};
use crate::{Position, Offset};
#[derive(Default)]
pub struct InputOpt {
@ -14,7 +14,7 @@ pub struct InputOpt {
macro_rules! gen_method {
($func_name:ident, $position:ident) => {
pub fn $func_name(title: impl AsRef<str>, rect: RectShim) -> InputOpt {
pub fn $func_name(title: impl AsRef<str>, rect: Offset) -> InputOpt {
InputOpt {
title: title.as_ref().to_owned(),
position: Position::$position(rect),
@ -43,7 +43,7 @@ impl InputOpt {
pub fn from_cfg(title: impl AsRef<str>, pos: &CfgPosition, rect: &CfgOffset) -> Self {
let rect =
RectShim { x_offset: rect.x, y_offset: rect.y, width: rect.width, height: rect.height };
Offset { x_offset: rect.x, y_offset: rect.y, width: rect.width, height: rect.height };
match pos {
CfgPosition::TopLeft => Self::top_left(title, rect),

View file

@ -1,50 +1,50 @@
use ratatui::prelude::Rect;
#[derive(Debug, Clone, Copy)]
pub struct RectShim {
pub struct Offset {
pub x_offset: i16,
pub y_offset: i16,
pub width: u16,
pub height: u16,
}
impl Default for RectShim {
impl Default for Offset {
fn default() -> Self { Self { x_offset: 0, y_offset: 2, width: 50, height: 3 } }
}
#[derive(Debug, Clone)]
pub enum Position {
TopLeft(RectShim),
TopRight(RectShim),
TopCenter(RectShim),
Center(RectShim),
BottomCenter(RectShim),
BottomLeft(RectShim),
BottomRight(RectShim),
Hovered(RectShim),
Sticky(RectShim, Rect),
TopLeft(Offset),
TopRight(Offset),
TopCenter(Offset),
Center(Offset),
BottomCenter(Offset),
BottomLeft(Offset),
BottomRight(Offset),
Hovered(Offset),
Sticky(Offset, Rect),
}
impl Default for Position {
fn default() -> Self { Self::TopCenter(RectShim::default()) }
fn default() -> Self { Self::TopCenter(Offset::default()) }
}
impl Position {
#[inline]
pub fn rect(&self) -> &RectShim {
pub fn offset(&self) -> &Offset {
match self {
Position::TopLeft(rect) => rect,
Position::TopRight(rect) => rect,
Position::TopCenter(rect) => rect,
Position::Center(rect) => rect,
Position::BottomCenter(rect) => rect,
Position::BottomLeft(rect) => rect,
Position::BottomRight(rect) => rect,
Position::Hovered(rect) => rect,
Position::Sticky(rect, _) => rect,
Position::TopLeft(offset) => offset,
Position::TopRight(offset) => offset,
Position::TopCenter(offset) => offset,
Position::Center(offset) => offset,
Position::BottomCenter(offset) => offset,
Position::BottomLeft(offset) => offset,
Position::BottomRight(offset) => offset,
Position::Hovered(offset) => offset,
Position::Sticky(offset, _) => offset,
}
}
#[inline]
pub fn dimension(&self) -> (u16, u16) { (self.rect().width, self.rect().height) }
pub fn dimension(&self) -> (u16, u16) { (self.offset().width, self.offset().height) }
}

View file

@ -1,6 +1,6 @@
use yazi_config::popup::{Offset as CfgOffset, Position as CfgPosition};
use crate::{Position, RectShim};
use crate::{Position, Offset};
pub struct SelectOpt {
pub title: String,
@ -10,7 +10,7 @@ pub struct SelectOpt {
macro_rules! gen_method {
($func_name:ident, $position:ident) => {
pub fn $func_name(title: &str, items: Vec<String>, rect: RectShim) -> SelectOpt {
pub fn $func_name(title: &str, items: Vec<String>, rect: Offset) -> SelectOpt {
let height = 2
+ items.len().min(
5, // TODO: hardcode
@ -18,7 +18,7 @@ macro_rules! gen_method {
Self {
title: title.to_owned(),
items,
position: Position::$position(RectShim { height, ..rect }),
position: Position::$position(Offset { height, ..rect }),
}
}
};
@ -43,7 +43,7 @@ impl SelectOpt {
pub fn from_cfg(title: &str, items: Vec<String>, pos: &CfgPosition, rect: &CfgOffset) -> Self {
let rect =
RectShim { x_offset: rect.x, y_offset: rect.y, width: rect.width, height: rect.height };
Offset { x_offset: rect.x, y_offset: rect.y, width: rect.width, height: rect.height };
match pos {
CfgPosition::TopLeft => Self::top_left(title, items, rect),

View file

@ -2,7 +2,7 @@ use std::path::MAIN_SEPARATOR;
use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Clear, List, ListItem, Widget}};
use yazi_config::THEME;
use yazi_core::{Ctx, Position, RectShim};
use yazi_core::{Ctx, Position, Offset};
pub(crate) struct Completion<'a> {
cx: &'a Ctx,
@ -41,7 +41,7 @@ impl<'a> Widget for Completion<'a> {
let input_area = self.cx.area(&self.cx.input.position);
let mut area = self.cx.area(&Position::Sticky(
// TODO:
RectShim {
Offset {
x_offset: 1,
y_offset: 0,
width: input_area.width.saturating_sub(2),