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 ratatui::prelude::Rect;
use yazi_shared::Term; 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 struct Ctx {
pub manager: Manager, pub manager: Manager,
@ -31,7 +31,7 @@ impl Ctx {
let WindowSize { columns, rows, .. } = Term::size(); let WindowSize { columns, rows, .. } = Term::size();
let (x, y) = match *pos { 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 right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height); let bottom_max = rows.saturating_sub(height);
@ -43,7 +43,7 @@ impl Ctx {
(x, y) (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 right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height); let bottom_max = rows.saturating_sub(height);
@ -55,7 +55,7 @@ impl Ctx {
(x, y) (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 right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height); let bottom_max = rows.saturating_sub(height);
@ -67,7 +67,7 @@ impl Ctx {
(x, y) (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 right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height); let bottom_max = rows.saturating_sub(height);
@ -79,7 +79,7 @@ impl Ctx {
(x, y) (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 right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height); let bottom_max = rows.saturating_sub(height);
@ -91,7 +91,7 @@ impl Ctx {
(x, y) (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 right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height); let bottom_max = rows.saturating_sub(height);
@ -103,7 +103,7 @@ impl Ctx {
(x, y) (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 right_max = columns.saturating_sub(width);
let bottom_max = rows.saturating_sub(height); let bottom_max = rows.saturating_sub(height);
@ -124,7 +124,7 @@ impl Ctx {
Position::TopCenter(rect_shim) 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: // TODO:
unimplemented!("Position::Sticky is not implemented"); unimplemented!("Position::Sticky is not implemented");
// let mut x = columns.saturating_sub(width); // let mut x = columns.saturating_sub(width);

View file

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

View file

@ -1,50 +1,50 @@
use ratatui::prelude::Rect; use ratatui::prelude::Rect;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct RectShim { pub struct Offset {
pub x_offset: i16, pub x_offset: i16,
pub y_offset: i16, pub y_offset: i16,
pub width: u16, pub width: u16,
pub height: 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 } } fn default() -> Self { Self { x_offset: 0, y_offset: 2, width: 50, height: 3 } }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Position { pub enum Position {
TopLeft(RectShim), TopLeft(Offset),
TopRight(RectShim), TopRight(Offset),
TopCenter(RectShim), TopCenter(Offset),
Center(RectShim), Center(Offset),
BottomCenter(RectShim), BottomCenter(Offset),
BottomLeft(RectShim), BottomLeft(Offset),
BottomRight(RectShim), BottomRight(Offset),
Hovered(RectShim), Hovered(Offset),
Sticky(RectShim, Rect), Sticky(Offset, Rect),
} }
impl Default for Position { impl Default for Position {
fn default() -> Self { Self::TopCenter(RectShim::default()) } fn default() -> Self { Self::TopCenter(Offset::default()) }
} }
impl Position { impl Position {
#[inline] #[inline]
pub fn rect(&self) -> &RectShim { pub fn offset(&self) -> &Offset {
match self { match self {
Position::TopLeft(rect) => rect, Position::TopLeft(offset) => offset,
Position::TopRight(rect) => rect, Position::TopRight(offset) => offset,
Position::TopCenter(rect) => rect, Position::TopCenter(offset) => offset,
Position::Center(rect) => rect, Position::Center(offset) => offset,
Position::BottomCenter(rect) => rect, Position::BottomCenter(offset) => offset,
Position::BottomLeft(rect) => rect, Position::BottomLeft(offset) => offset,
Position::BottomRight(rect) => rect, Position::BottomRight(offset) => offset,
Position::Hovered(rect) => rect, Position::Hovered(offset) => offset,
Position::Sticky(rect, _) => rect, Position::Sticky(offset, _) => offset,
} }
} }
#[inline] #[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 yazi_config::popup::{Offset as CfgOffset, Position as CfgPosition};
use crate::{Position, RectShim}; use crate::{Position, Offset};
pub struct SelectOpt { pub struct SelectOpt {
pub title: String, pub title: String,
@ -10,7 +10,7 @@ pub struct SelectOpt {
macro_rules! gen_method { macro_rules! gen_method {
($func_name:ident, $position:ident) => { ($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 let height = 2
+ items.len().min( + items.len().min(
5, // TODO: hardcode 5, // TODO: hardcode
@ -18,7 +18,7 @@ macro_rules! gen_method {
Self { Self {
title: title.to_owned(), title: title.to_owned(),
items, 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 { pub fn from_cfg(title: &str, items: Vec<String>, pos: &CfgPosition, rect: &CfgOffset) -> Self {
let rect = 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 { match pos {
CfgPosition::TopLeft => Self::top_left(title, items, rect), 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 ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Clear, List, ListItem, Widget}};
use yazi_config::THEME; use yazi_config::THEME;
use yazi_core::{Ctx, Position, RectShim}; use yazi_core::{Ctx, Position, Offset};
pub(crate) struct Completion<'a> { pub(crate) struct Completion<'a> {
cx: &'a Ctx, cx: &'a Ctx,
@ -41,7 +41,7 @@ impl<'a> Widget for Completion<'a> {
let input_area = self.cx.area(&self.cx.input.position); let input_area = self.cx.area(&self.cx.input.position);
let mut area = self.cx.area(&Position::Sticky( let mut area = self.cx.area(&Position::Sticky(
// TODO: // TODO:
RectShim { Offset {
x_offset: 1, x_offset: 1,
y_offset: 0, y_offset: 0,
width: input_area.width.saturating_sub(2), width: input_area.width.saturating_sub(2),