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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! Render-time and event-time context types for components.
//!
//! This module provides [`RenderContext`] and [`EventContext`], which carry
//! per-render state (frame, area, theme, focus, disabled) into component
//! `view` and `handle_event` methods respectively.
use ratatui::prelude::{Frame, Rect};
use crate::theme::Theme;
/// Context passed to [`Component::handle_event`](crate::component::Component::handle_event).
///
/// Carries focus and disabled state from the parent so the component
/// can decide whether and how to handle events. Use [`RenderContext`]
/// for `view()`.
///
/// # Example
///
/// ```rust
/// use envision::component::EventContext;
///
/// let ctx = EventContext::default();
/// assert!(!ctx.focused);
///
/// let ctx = EventContext::new().focused(true).disabled(false);
/// assert!(ctx.focused);
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EventContext {
/// Whether this component currently has keyboard focus.
pub focused: bool,
/// Whether this component is currently disabled.
pub disabled: bool,
}
impl EventContext {
/// Creates a new default EventContext (unfocused, enabled).
pub fn new() -> Self {
Self::default()
}
/// Sets the focused state (builder pattern).
#[must_use]
pub fn focused(mut self, focused: bool) -> Self {
self.focused = focused;
self
}
/// Sets the disabled state (builder pattern).
#[must_use]
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
/// Context passed to [`Component::view`](crate::component::Component::view).
///
/// Bundles the frame, area, theme, and focus/disabled state into a
/// single value so component view signatures stay short and adding
/// new render-time fields is non-breaking.
///
/// # Lifetimes
///
/// `RenderContext` carries two lifetime parameters:
/// - `'frame` is the lifetime of the borrow on the [`Frame`] reference.
/// This is the lifetime that shortens during reborrows via [`with_area`](Self::with_area).
/// - `'buf` is the lifetime of the frame's internal buffer (the underlying terminal cells).
/// This stays stable across reborrows.
///
/// Most callers can write `RenderContext<'_, '_>` and let lifetime elision
/// handle both. For example, [`Component::view`](crate::component::Component::view)
/// takes `ctx: &mut RenderContext<'_, '_>`.
///
/// # Example
///
/// ```rust,no_run
/// use envision::component::{Component, RenderContext};
/// use envision::theme::Theme;
/// use envision::backend::CaptureBackend;
/// use ratatui::Terminal;
///
/// let backend = CaptureBackend::new(80, 24);
/// let mut terminal = Terminal::new(backend).unwrap();
/// let theme = Theme::default();
/// terminal.draw(|frame| {
/// let area = frame.area();
/// let mut ctx = RenderContext::new(frame, area, &theme).focused(true);
/// // Pass `&mut ctx` to a component's `view` method.
/// }).unwrap();
/// ```
pub struct RenderContext<'frame, 'buf> {
/// The ratatui frame to render into.
pub frame: &'frame mut Frame<'buf>,
/// The area within the frame to render to.
pub area: Rect,
/// The theme to use for styling.
pub theme: &'frame Theme,
/// Whether the component currently has keyboard focus.
pub focused: bool,
/// Whether the component is currently disabled.
pub disabled: bool,
}
impl<'frame, 'buf> RenderContext<'frame, 'buf> {
/// Constructs a new RenderContext with `focused` and `disabled` both `false`.
pub fn new(frame: &'frame mut Frame<'buf>, area: Rect, theme: &'frame Theme) -> Self {
Self {
frame,
area,
theme,
focused: false,
disabled: false,
}
}
/// Sets the focused state (builder pattern).
#[must_use]
pub fn focused(mut self, focused: bool) -> Self {
self.focused = focused;
self
}
/// Sets the disabled state (builder pattern).
#[must_use]
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// Returns a context with the same frame, theme, and focus state but
/// a different area.
///
/// The returned context borrows the frame for a shorter lifetime, so
/// the parent context becomes valid again after the child context
/// goes out of scope.
pub fn with_area(&mut self, area: Rect) -> RenderContext<'_, 'buf> {
RenderContext {
frame: self.frame,
area,
theme: self.theme,
focused: self.focused,
disabled: self.disabled,
}
}
/// Convenience: render a widget into this context's area.
///
/// Equivalent to `self.frame.render_widget(widget, self.area)`.
pub fn render_widget<W: ratatui::widgets::Widget>(&mut self, widget: W) {
self.frame.render_widget(widget, self.area);
}
/// Returns the [`EventContext`] slice of this RenderContext.
pub fn event_context(&self) -> EventContext {
EventContext {
focused: self.focused,
disabled: self.disabled,
}
}
}
impl From<&RenderContext<'_, '_>> for EventContext {
fn from(ctx: &RenderContext<'_, '_>) -> Self {
EventContext {
focused: ctx.focused,
disabled: ctx.disabled,
}
}
}
#[cfg(test)]
mod render_context_tests {
use super::*;
use crate::component::test_utils::setup_render;
#[test]
fn test_event_context_default() {
let ctx = EventContext::default();
assert!(!ctx.focused);
assert!(!ctx.disabled);
}
#[test]
fn test_event_context_builder() {
let ctx = EventContext::new().focused(true);
assert!(ctx.focused);
assert!(!ctx.disabled);
let ctx = EventContext::new().focused(true).disabled(true);
assert!(ctx.focused);
assert!(ctx.disabled);
}
#[test]
fn test_render_context_construction() {
let (mut terminal, theme) = setup_render(60, 5);
terminal
.draw(|frame| {
let area = frame.area();
let ctx = RenderContext::new(frame, area, &theme);
assert!(!ctx.focused);
assert!(!ctx.disabled);
assert_eq!(ctx.area, area);
})
.unwrap();
}
#[test]
fn test_render_context_builder() {
let (mut terminal, theme) = setup_render(60, 5);
terminal
.draw(|frame| {
let area = frame.area();
let ctx = RenderContext::new(frame, area, &theme)
.focused(true)
.disabled(true);
assert!(ctx.focused);
assert!(ctx.disabled);
})
.unwrap();
}
#[test]
fn test_render_context_with_area() {
use ratatui::widgets::Paragraph;
let (mut terminal, theme) = setup_render(60, 10);
terminal
.draw(|frame| {
let parent_area = frame.area();
let mut ctx = RenderContext::new(frame, parent_area, &theme).focused(true);
let parent_theme_ptr = ctx.theme as *const Theme;
let child_area = ratatui::layout::Rect::new(5, 2, 20, 3);
{
let mut child_ctx = ctx.with_area(child_area);
assert_eq!(child_ctx.area, child_area);
assert!(child_ctx.focused);
// Verify child shares parent's theme reference (pointer equality)
assert_eq!(child_ctx.theme as *const Theme, parent_theme_ptr);
child_ctx.render_widget(Paragraph::new("child"));
}
// Critical: render through the parent ctx after child scope.
// This line would NOT compile if the reborrow were broken.
ctx.render_widget(Paragraph::new("parent"));
assert_eq!(ctx.area, parent_area);
assert!(ctx.focused);
})
.unwrap();
}
#[test]
fn test_render_context_render_widget() {
use ratatui::widgets::Paragraph;
let (mut terminal, theme) = setup_render(60, 5);
terminal
.draw(|frame| {
let area = frame.area();
let mut ctx = RenderContext::new(frame, area, &theme);
ctx.render_widget(Paragraph::new("hello"));
})
.unwrap();
let display = terminal.backend().to_string();
assert!(display.contains("hello"));
}
#[test]
fn test_event_context_from_render_context() {
let (mut terminal, theme) = setup_render(60, 5);
terminal
.draw(|frame| {
let area = frame.area();
let ctx = RenderContext::new(frame, area, &theme)
.focused(true)
.disabled(false);
let event_ctx: EventContext = (&ctx).into();
assert!(event_ctx.focused);
assert!(!event_ctx.disabled);
})
.unwrap();
}
#[test]
fn test_render_context_event_context_method() {
let (mut terminal, theme) = setup_render(60, 5);
terminal
.draw(|frame| {
let area = frame.area();
let ctx = RenderContext::new(frame, area, &theme)
.focused(true)
.disabled(true);
let event_ctx = ctx.event_context();
assert!(event_ctx.focused);
assert!(event_ctx.disabled);
})
.unwrap();
}
}