From 16473af4e605b8cec34e65ba2359c836e30829c2 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 17 Nov 2023 09:03:30 +0800 Subject: [PATCH] Fix `Select` height --- yazi-config/preset/yazi.toml | 2 +- yazi-config/src/popup/input.rs | 5 +++++ yazi-config/src/popup/offset.rs | 7 +++++-- yazi-config/src/popup/options.rs | 23 +++++++++++++---------- yazi-config/src/popup/select.rs | 5 +++++ yazi-core/src/manager/commands/open.rs | 4 +--- yazi-core/src/select/mod.rs | 2 -- yazi-core/src/select/select.rs | 6 ++++-- 8 files changed, 34 insertions(+), 20 deletions(-) diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index ea11a450..349b836b 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -127,7 +127,7 @@ quit_offset = [ 0, 2, 50, 3 ] [select] open_title = "Open with:" open_origin = "hovered" -open_offset = [ 0, 1, 50, 3 ] +open_offset = [ 0, 1, 50, 7 ] [log] enabled = false diff --git a/yazi-config/src/popup/input.rs b/yazi-config/src/popup/input.rs index d1f87da1..6453b11f 100644 --- a/yazi-config/src/popup/input.rs +++ b/yazi-config/src/popup/input.rs @@ -66,3 +66,8 @@ impl Default for Input { toml::from_str::(&MERGED_YAZI).unwrap().input } } + +impl Input { + #[inline] + pub const fn border(&self) -> u16 { 2 } +} diff --git a/yazi-config/src/popup/offset.rs b/yazi-config/src/popup/offset.rs index 43a2c29c..d8524321 100644 --- a/yazi-config/src/popup/offset.rs +++ b/yazi-config/src/popup/offset.rs @@ -15,10 +15,13 @@ impl TryFrom> for Offset { fn try_from(values: Vec) -> Result { if values.len() != 4 { - bail!("invalid offset: {:?}", values); + bail!("offset must have 4 values: {:?}", values); } if values[2] < 0 || values[3] < 0 { - bail!("invalid offset: {:?}", values); + bail!("offset width and height must be positive: {:?}", values); + } + if values[3] < 3 { + bail!("offset height must be at least 3: {:?}", values); } Ok(Self { diff --git a/yazi-config/src/popup/options.rs b/yazi-config/src/popup/options.rs index 45c2c15b..472d45b5 100644 --- a/yazi-config/src/popup/options.rs +++ b/yazi-config/src/popup/options.rs @@ -1,4 +1,4 @@ -use super::Position; +use super::{Offset, Position}; use crate::{INPUT, SELECT}; #[derive(Default)] @@ -124,17 +124,20 @@ impl InputOpt { impl SelectOpt { #[inline] - pub fn open() -> Self { - Self { - title: SELECT.open_title.to_owned(), - position: Position::new(SELECT.open_origin, SELECT.open_offset), - ..Default::default() - } + fn max_height(len: usize) -> u16 { + SELECT.open_offset.height.min(SELECT.border().saturating_add(len as u16)) } #[inline] - pub fn with_items(mut self, items: Vec) -> Self { - self.items = items; - self + pub fn open(items: Vec) -> Self { + let max_height = Self::max_height(items.len()); + Self { + title: SELECT.open_title.to_owned(), + items, + position: Position::new(SELECT.open_origin, Offset { + height: max_height, + ..SELECT.open_offset + }), + } } } diff --git a/yazi-config/src/popup/select.rs b/yazi-config/src/popup/select.rs index 076c8303..05d24a19 100644 --- a/yazi-config/src/popup/select.rs +++ b/yazi-config/src/popup/select.rs @@ -21,3 +21,8 @@ impl Default for Select { toml::from_str::(&MERGED_YAZI).unwrap().select } } + +impl Select { + #[inline] + pub const fn border(&self) -> u16 { 2 } +} diff --git a/yazi-core/src/manager/commands/open.rs b/yazi-core/src/manager/commands/open.rs index dd221da6..d436c3f6 100644 --- a/yazi-core/src/manager/commands/open.rs +++ b/yazi-core/src/manager/commands/open.rs @@ -20,9 +20,7 @@ impl Manager { return; } - let result = - emit!(Select(SelectOpt::open().with_items(openers.iter().map(|o| o.desc.clone()).collect()))); - + let result = emit!(Select(SelectOpt::open(openers.iter().map(|o| o.desc.clone()).collect()))); if let Ok(choice) = result.await { emit!(Open(files, Some(openers[choice].clone()))); } diff --git a/yazi-core/src/select/mod.rs b/yazi-core/src/select/mod.rs index ef266cba..f98d4db8 100644 --- a/yazi-core/src/select/mod.rs +++ b/yazi-core/src/select/mod.rs @@ -2,5 +2,3 @@ mod commands; mod select; pub use select::*; - -pub const SELECT_PADDING: u16 = 2; diff --git a/yazi-core/src/select/select.rs b/yazi-core/src/select/select.rs index a76afe21..5946c42b 100644 --- a/yazi-core/src/select/select.rs +++ b/yazi-core/src/select/select.rs @@ -1,6 +1,6 @@ use anyhow::Result; use tokio::sync::oneshot::Sender; -use yazi_config::popup::{Position, SelectOpt}; +use yazi_config::{popup::{Position, SelectOpt}, SELECT}; #[derive(Default)] pub struct Select { @@ -34,7 +34,9 @@ impl Select { } #[inline] - pub fn limit(&self) -> usize { self.items.len().min(5) } + pub(super) fn limit(&self) -> usize { + self.position.offset.height.saturating_sub(SELECT.border()) as usize + } } impl Select {