Skip to main content

fresh/view/controls/
mod.rs

1//! Reusable form controls for settings and dialogs
2//!
3//! This module provides a set of form controls that can be used across
4//! the application for settings screens, dialogs, and other interactive UI.
5//!
6//! ## Available Controls
7//! - `Toggle` - Boolean on/off switch
8//! - `NumberInput` - Numeric input with increment/decrement
9//! - `Dropdown` - Selection from a list of options
10//! - `TextInput` - Single-line text entry
11//! - `TextList` - List of strings with add/remove
12//! - `MapInput` - Key-value map with expandable entries
13//! - `Button` - Clickable action button
14//!
15//! ## Pattern
16//! Each control follows a consistent pattern:
17//! - `*State` struct containing the control's data
18//! - `*Colors` struct for theming
19//! - `render_*` function that renders to a frame and returns hit areas
20
21pub mod button;
22pub mod dropdown;
23pub mod keybinding_list;
24pub mod map_input;
25pub mod number_input;
26pub mod text_input;
27pub mod text_list;
28pub mod toggle;
29
30pub use button::{
31    render_button, render_button_row, ButtonColors, ButtonEvent, ButtonLayout, ButtonState,
32};
33pub use dropdown::{
34    render_dropdown, render_dropdown_aligned, DropdownColors, DropdownEvent, DropdownLayout,
35    DropdownState,
36};
37pub use keybinding_list::{
38    render_keybinding_list, KeybindingListColors, KeybindingListEvent, KeybindingListLayout,
39    KeybindingListState,
40};
41pub use map_input::{render_map, MapColors, MapEvent, MapLayout, MapState};
42pub use number_input::{
43    render_number_input, render_number_input_aligned, NumberInputColors, NumberInputEvent,
44    NumberInputLayout, NumberInputState,
45};
46pub use text_input::{
47    render_text_input, render_text_input_aligned, TextInputColors, TextInputEvent, TextInputLayout,
48    TextInputState,
49};
50pub use text_list::{
51    render_text_list, TextListColors, TextListEvent, TextListLayout, TextListState,
52};
53pub use toggle::{
54    render_toggle, render_toggle_aligned, ToggleColors, ToggleEvent, ToggleLayout, ToggleState,
55};
56
57use ratatui::style::Color;
58
59/// Common colors shared across controls
60#[derive(Debug, Clone, Copy)]
61pub struct ControlColors {
62    /// Background color
63    pub bg: Color,
64    /// Foreground/text color
65    pub fg: Color,
66    /// Border color
67    pub border: Color,
68    /// Focused/active accent color
69    pub accent: Color,
70    /// Disabled text color
71    pub disabled: Color,
72}
73
74impl Default for ControlColors {
75    fn default() -> Self {
76        Self {
77            bg: Color::Black,
78            fg: Color::White,
79            border: Color::DarkGray,
80            accent: Color::Cyan,
81            disabled: Color::DarkGray,
82        }
83    }
84}
85
86impl ControlColors {
87    /// Create control colors from a theme
88    pub fn from_theme(theme: &crate::view::theme::Theme) -> Self {
89        Self {
90            bg: theme.editor_bg,
91            fg: theme.editor_fg,
92            border: theme.split_separator_fg,
93            accent: theme.selection_bg,
94            disabled: theme.line_number_fg,
95        }
96    }
97}
98
99/// Focus state for controls
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
101pub enum FocusState {
102    #[default]
103    Normal,
104    Focused,
105    Hovered,
106    Disabled,
107}