//! Help widget for displaying keybinding lists. //! //! Renders a styled list of key/description pairs for showing available //! keyboard shortcuts in a TUI application. //! //! # Example //! //! ``` //! use ftui_widgets::help::{Help, HelpEntry}; //! //! let help = Help::new() //! .entry("q", "quit") //! .entry("^s", "save") //! .entry("?", "toggle help"); //! //! assert_eq!(help.entries().len(), 3); //! ``` use crate::hint_ranker::{HintContext, HintRanker, RankingEvidence}; use crate::{StatefulWidget, Widget, clear_text_area, draw_text_span}; use ftui_core::geometry::Rect; use ftui_core::keybinding::{ContextId, KeyMap}; use ftui_render::budget::DegradationLevel; use ftui_render::buffer::Buffer; use ftui_render::cell::{Cell, PackedRgba}; use ftui_render::frame::Frame; use ftui_style::Style; use ftui_style::StyleFlags; use ftui_text::wrap::display_width; use std::hash::{Hash, Hasher}; /// Category for organizing help entries into logical groups. #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] pub enum HelpCategory { /// General/uncategorized keybinding. #[default] General, /// Navigation keys (arrows, page up/down, home/end, etc.). Navigation, /// Editing actions (cut, copy, paste, undo, redo, etc.). Editing, /// File operations (save, open, close, new, etc.). File, /// View/display controls (zoom, scroll, toggle panels, etc.). View, /// Application-level shortcuts (quit, settings, help, etc.). Global, /// Custom category with a user-defined label. Custom(String), } impl HelpCategory { /// Return a display label for this category. #[must_use] pub fn label(&self) -> &str { match self { Self::General => "General", Self::Navigation => "Navigation", Self::Editing => "Editing", Self::File => "File", Self::View => "View", Self::Global => "Global", Self::Custom(s) => s, } } } /// A single keybinding entry in the help view. #[derive(Debug, Clone, PartialEq, Eq)] pub struct HelpEntry { /// The key or key combination (e.g. "^C", "↑/k"). pub key: String, /// Description of what the key does. pub desc: String, /// Whether this entry is enabled (disabled entries are hidden). pub enabled: bool, /// Category for grouping related entries. pub category: HelpCategory, } impl HelpEntry { /// Create a new enabled help entry. #[must_use] pub fn new(key: impl Into, desc: impl Into) -> Self { Self { key: key.into(), desc: desc.into(), enabled: true, category: HelpCategory::default(), } } /// Set whether this entry is enabled. #[must_use] pub fn with_enabled(mut self, enabled: bool) -> Self { self.enabled = enabled; self } /// Set the category for this entry. #[must_use] pub fn with_category(mut self, category: HelpCategory) -> Self { self.category = category; self } } /// Display mode for the help widget. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] pub enum HelpMode { /// Short inline mode: entries separated by a bullet on one line. #[default] Short, /// Full mode: entries stacked vertically with aligned columns. Full, } /// Help widget that renders keybinding entries. /// /// In [`HelpMode::Short`] mode, entries are shown inline separated by a bullet /// character, truncated with an ellipsis if they exceed the available width. /// /// In [`HelpMode::Full`] mode, entries are rendered in a vertical list with /// keys and descriptions in aligned columns. #[derive(Debug, Clone)] pub struct Help { entries: Vec, mode: HelpMode, /// Separator between entries in short mode. separator: String, /// Ellipsis shown when truncated. ellipsis: String, /// Style for key text. key_style: Style, /// Style for description text. desc_style: Style, /// Style for separator/ellipsis. separator_style: Style, /// Hint ids assigned by [`Help::register_with_ranker`], parallel to /// `entries`; empty until a ranker has been attached. hint_ids: Vec, } /// Cached render state for [`Help`], enabling incremental layout reuse and /// dirty-rect updates for keybinding hint panels. /// /// # Invariants /// - Layout is reused only when entry count and slot widths remain compatible. /// - Dirty rects always cover the full prior slot width for changed entries. /// - Layout rebuilds on any change that could cause reflow. /// /// # Failure Modes /// - If a changed entry exceeds its cached slot width, we rebuild the layout. /// - If enabled entry count changes, we rebuild the layout. #[derive(Debug, Default)] pub struct HelpRenderState { cache: Option, enabled_indices: Vec, dirty_indices: Vec, dirty_rects: Vec, stats: HelpCacheStats, } /// Cache hit/miss statistics for [`HelpRenderState`]. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct HelpCacheStats { pub hits: u64, pub misses: u64, pub dirty_updates: u64, pub layout_rebuilds: u64, } impl HelpRenderState { /// Return cache statistics. #[must_use] pub fn stats(&self) -> HelpCacheStats { self.stats } /// Clear recorded dirty rects. pub fn clear_dirty_rects(&mut self) { self.dirty_rects.clear(); } /// Take dirty rects for logging/inspection. #[must_use] pub fn take_dirty_rects(&mut self) -> Vec { std::mem::take(&mut self.dirty_rects) } /// Read dirty rects without clearing. #[must_use] pub fn dirty_rects(&self) -> &[Rect] { &self.dirty_rects } /// Reset cache stats (useful for perf logging). pub fn reset_stats(&mut self) { self.stats = HelpCacheStats::default(); } } #[derive(Debug)] struct HelpCache { buffer: Buffer, layout: HelpLayout, key: LayoutKey, entry_hashes: Vec, enabled_count: usize, } #[derive(Debug, Clone)] struct HelpLayout { mode: HelpMode, width: u16, entries: Vec, ellipsis: Option, max_key_width: usize, separator_width: usize, } #[derive(Debug, Clone)] struct EntrySlot { x: u16, y: u16, width: u16, key_width: usize, } #[derive(Debug, Clone)] struct EllipsisSlot { x: u16, width: u16, prefix_space: bool, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] struct StyleKey { fg: Option, bg: Option, attrs: Option, } impl From