use ratatui::{
buffer::Buffer,
style::{Color, Modifier, Style},
};
use crate::{Key, WhichKeyState, render};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PopupPosition {
BottomLeft,
#[default]
BottomRight,
TopLeft,
TopRight,
}
#[derive(Debug, Clone)]
pub struct WhichKey {
pub max_height: u16,
pub position: PopupPosition,
pub border_style: Style,
pub key_style: Style,
pub description_style: Style,
pub category_style: Style,
}
impl Default for WhichKey {
fn default() -> Self {
Self {
max_height: 10,
position: PopupPosition::default(),
border_style: Style::default().fg(Color::Yellow),
key_style: Style::default().fg(Color::Cyan),
description_style: Style::default(),
category_style: Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::BOLD),
}
}
}
impl WhichKey {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn max_height(mut self, height: u16) -> Self {
self.max_height = height;
self
}
#[must_use]
pub fn position(mut self, position: PopupPosition) -> Self {
self.position = position;
self
}
#[must_use]
pub fn border_style(mut self, style: Style) -> Self {
self.border_style = style;
self
}
}
impl WhichKey {
pub fn render<K, S, A, C>(self, buf: &mut Buffer, state: &WhichKeyState<K, S, A, C>)
where
K: Key + Clone + PartialEq,
S: Clone + Ord + PartialEq + Send + Sync,
A: Clone + Send + Sync,
C: Clone + std::fmt::Display,
{
if !state.active && state.current_sequence.is_empty() {
return;
}
render::render_popup(&self, buf, state);
}
}