use crate::config::{Config, Mode};
use crate::waker::Waker;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::Rect;
use ratatui::Frame;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Flow {
Continue,
Quit,
}
#[derive(Debug, Clone, Default)]
pub struct ViewSlot {
placed: Vec<(String, Rect)>,
overlays: Vec<Rect>,
cell_px: (f32, f32),
describe: String,
}
impl ViewSlot {
pub fn new(cell_px: (f32, f32), describe: String) -> ViewSlot {
ViewSlot {
placed: Vec::new(),
overlays: Vec::new(),
cell_px,
describe,
}
}
pub fn place(&mut self, name: &str, area: Rect) {
self.placed.retain(|(n, _)| n != name);
if area.width > 0 && area.height > 0 {
self.placed.push((name.to_string(), area));
}
}
pub fn rect(&self, name: &str) -> Option<Rect> {
self.placed.iter().find(|(n, _)| n == name).map(|(_, r)| *r)
}
pub fn placed(&self) -> &[(String, Rect)] {
&self.placed
}
pub fn overlay(&mut self, area: Rect) {
if area.width > 0 && area.height > 0 {
self.overlays.push(area);
}
}
pub fn overlays(&self) -> &[Rect] {
&self.overlays
}
pub(crate) fn clear_views(&mut self) {
self.placed.clear();
self.overlays.clear();
}
pub fn cell_px(&self) -> (f32, f32) {
self.cell_px
}
pub fn px_size(&self, area: Rect) -> (u32, u32) {
(
(f32::from(area.width) * self.cell_px.0).round() as u32,
(f32::from(area.height) * self.cell_px.1).round() as u32,
)
}
pub fn describe(&self) -> &str {
&self.describe
}
}
pub trait App {
fn start(&mut self, waker: Waker) {
let _ = waker;
}
fn draw(&mut self, frame: &mut Frame, views: &mut ViewSlot);
fn event(&mut self, event: Event) -> Flow;
fn tick(&mut self) -> bool {
false
}
fn tick_interval(&self) -> Option<Duration> {
None
}
fn captures_text(&self) -> bool {
false
}
fn gesture(&mut self, gesture: crate::gesture::Gesture) -> Flow {
let _ = gesture;
Flow::Continue
}
fn message(&mut self, text: &str) {
let _ = text;
}
fn help(&self) -> Option<crate::help::Help> {
None
}
fn mode_changed(&mut self, mode: Mode) {
let _ = mode;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum End {
Quit,
Switch,
}
pub(crate) fn is_help_key(config: &Config, app: &dyn App, key: &KeyEvent) -> bool {
let Some(want) = config.help_key else {
return false;
};
let plain = (key.modifiers - KeyModifiers::SHIFT).is_empty();
plain && key.code == KeyCode::Char(want) && !app.captures_text() && app.help().is_some()
}
pub(crate) fn is_copy_key(config: &Config, app: &dyn App, key: &KeyEvent) -> bool {
let Some(want) = config.copy_key.filter(|_| cfg!(feature = "clipboard")) else {
return false;
};
let plain = (key.modifiers - KeyModifiers::SHIFT).is_empty();
plain && key.code == KeyCode::Char(want) && !app.captures_text()
}
pub(crate) fn is_switch_key(config: &Config, app: &dyn App, key: &KeyEvent) -> bool {
let Some(want) = config
.switch_key
.filter(|_| cfg!(all(feature = "terminal", feature = "window")))
else {
return false;
};
let plain = (key.modifiers - KeyModifiers::SHIFT).is_empty();
let matches = match key.code {
KeyCode::Char(c) => c == want || c.eq_ignore_ascii_case(&want),
_ => false,
};
plain && matches && !app.captures_text()
}
#[cfg(test)]
mod tests {
use super::*;
struct Typing(bool);
impl App for Typing {
fn draw(&mut self, _: &mut Frame, _: &mut ViewSlot) {}
fn event(&mut self, _: Event) -> Flow {
Flow::Continue
}
fn captures_text(&self) -> bool {
self.0
}
}
#[test]
fn switch_key_rules() {
let key = |c, m| KeyEvent::new(KeyCode::Char(c), m);
let off = Config::default();
assert!(!is_switch_key(
&off,
&Typing(false),
&key('p', KeyModifiers::NONE)
));
let on = Config {
switch_key: Some('p'),
..Config::default()
};
let both = cfg!(all(feature = "terminal", feature = "window"));
assert_eq!(
is_switch_key(&on, &Typing(false), &key('p', KeyModifiers::NONE)),
both
);
assert_eq!(
is_switch_key(&on, &Typing(false), &key('p', KeyModifiers::SHIFT)),
both
);
assert!(!is_switch_key(
&on,
&Typing(false),
&key('p', KeyModifiers::CONTROL)
));
assert!(!is_switch_key(
&on,
&Typing(false),
&key('q', KeyModifiers::NONE)
));
assert_eq!(
is_switch_key(&on, &Typing(false), &key('P', KeyModifiers::SHIFT)),
both
);
assert!(!is_switch_key(
&on,
&Typing(true),
&key('p', KeyModifiers::NONE)
));
}
}