1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Shared chrome for the family of modals built on [`crate::ui::ModalView`].
//!
//! `ModalChrome` owns the [`ModalState`] (scroll, focus, cached button / esc rects), the
//! [`ModalKind`], and the `dismissable` flag, and centralizes the input plumbing. A concrete
//! modal supplies only its `title` / `body` / `buttons` and its mapping from [`ModalResponse`]
//! to [`super::ModalOutcome`]; both the key and click paths funnel through that one mapping, so
//! mouse and keyboard behave identically and footer buttons are clickable for free.
//!
//! Modals with bespoke key handling intercept the keys they care about and delegate the rest to
//! `on_key`. A modal needing a pinned control row above the buttons instead builds on the
//! `scroll_container` primitives directly; see [`crate::ui::diff_intro_modal`].
use crossterm::event::KeyEvent;
use ratatui::layout::Rect;
use ratatui::text::Line;
use ratatui::Frame;
use super::types::{ModalKind, ModalRenderCtx};
use crate::ui::modal::LinkableResponse;
use crate::ui::modal_links::ModalLink;
use crate::ui::{ModalButton, ModalResponse, ModalState, ModalView};
/// Ceiling on the columns a chrome-backed body can use, given the whole terminal `area`.
///
/// The modal sizes itself to its content, so the real width is unknown until the content
/// exists, but the ceiling is not: [`ModalView`] clamps to the terminal and floors the padding
/// at [`crate::ui::MIN_PAD_H`] per side. Bodies built for their width use this.
pub fn body_columns(area: Rect) -> u16 {
area.width.saturating_sub(2 * crate::ui::MIN_PAD_H)
}
/// Shared state + input plumbing for [`ModalView`]-backed modals.
pub struct ModalChrome {
/// Scroll / focus / cached hit-rects. `pub` for the rare caller (and the tests) that need
/// the raw rects.
pub state: ModalState,
kind: ModalKind,
dismissable: bool,
/// Optional prose width cap forwarded to [`ModalView`] each frame; set once at
/// construction, like `kind` and `dismissable`.
max_content_w: Option<u16>,
}
impl ModalChrome {
/// Build chrome with the given visual urgency and dismissability. Both values feed
/// [`ModalView`], the `handle_key` gate, and the `Modal` accessors from this one place, so
/// they can't drift apart.
pub fn new(kind: ModalKind, dismissable: bool) -> Self {
Self {
state: ModalState::new(),
kind,
dismissable,
max_content_w: None,
}
}
/// Cap the body's content width so prose wraps at a readable measure. Chain onto `new()`;
/// see [`crate::ui::ModalView::with_max_content_width`].
pub fn with_max_content_width(mut self, width: u16) -> Self {
self.max_content_w = Some(width);
self
}
// ── Accessors ──────────────────────────────────────────────────────────
pub fn kind(&self) -> ModalKind {
self.kind
}
pub fn dismissable(&self) -> bool {
self.dismissable
}
// ── Render ─────────────────────────────────────────────────────────────
/// Render the framed modal. `title` / `body` / `buttons` are supplied each frame so modals
/// with live content stay in control of their text; the chrome owns only the frame, scroll,
/// and hit-rects.
pub fn render(
&mut self,
frame: &mut Frame<'_>,
area: Rect,
ctx: &ModalRenderCtx<'_>,
title: &str,
body: &[Line<'_>],
buttons: &[ModalButton],
) {
self.render_with_links(frame, area, ctx, title, body, buttons, &[]);
}
/// [`Self::render`] for a modal carrying inline body links. `links` is rebuilt each frame
/// alongside `body`; the modal reads [`Self::focused_link`] to style the focused one. An
/// empty slice is exactly `render`.
#[allow(clippy::too_many_arguments)]
pub(crate) fn render_with_links(
&mut self,
frame: &mut Frame<'_>,
area: Rect,
ctx: &ModalRenderCtx<'_>,
title: &str,
body: &[Line<'_>],
buttons: &[ModalButton],
links: &[ModalLink],
) {
let mut view = ModalView::new(title, body, buttons, ctx.theme, self.kind, self.dismissable);
if let Some(w) = self.max_content_w {
view = view.with_max_content_width(w);
}
if !links.is_empty() {
view = view.with_links(links);
}
frame.render_stateful_widget(view, area, &mut self.state);
}
// ── Input ──────────────────────────────────────────────────────────────
/// Translate a key event into a [`ModalResponse`]. Scroll keys are absorbed and reported
/// as `Continue`.
pub fn on_key(&mut self, key: &KeyEvent, num_buttons: usize) -> ModalResponse {
self.state.handle_key(key, num_buttons, self.dismissable)
}
/// [`Self::on_key`] for a link-bearing modal: Tab walks one ring over links then buttons,
/// and Enter on a link reports [`LinkableResponse::Link`].
pub(crate) fn on_key_linkable(
&mut self,
key: &KeyEvent,
num_links: usize,
num_buttons: usize,
) -> LinkableResponse {
self.state
.handle_key_linkable(key, num_links, num_buttons, self.dismissable)
}
/// [`Self::on_click`] for a link-bearing modal: a click inside a link's rect reports
/// [`LinkableResponse::Link`].
pub(crate) fn on_click_linkable(&self, col: u16, row: u16) -> LinkableResponse {
self.state.handle_click_linkable(col, row, self.dismissable)
}
/// Which body link holds focus, for the render pass to style.
pub(crate) fn focused_link(&self) -> Option<usize> {
self.state.focused_link
}
/// Translate a left-click into a [`ModalResponse`] — a footer button, the `esc` affordance,
/// or `Continue`.
pub fn on_click(&self, col: u16, row: u16) -> ModalResponse {
self.state.handle_click(col, row, self.dismissable)
}
/// Scroll the body by a mouse-wheel `delta`.
pub fn on_wheel(&mut self, delta: i32) {
self.state.scroll_by(delta);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ui::PROSE_CONTENT_WIDTH;
#[test]
fn the_width_cap_is_opt_in() {
// This is the only production `ModalView::new` call site, so the default must stay
// `None` (size-to-content) for every modal that never asks for a cap.
assert_eq!(
ModalChrome::new(ModalKind::Normal, true).max_content_w,
None
);
}
#[test]
fn the_width_cap_builder_is_chainable_onto_new() {
let chrome =
ModalChrome::new(ModalKind::Warning, false).with_max_content_width(PROSE_CONTENT_WIDTH);
assert_eq!(chrome.max_content_w, Some(PROSE_CONTENT_WIDTH));
assert_eq!(chrome.kind(), ModalKind::Warning);
assert!(!chrome.dismissable());
}
}