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
//! Window frame rendering for terminal display.
//!
//! Draws the chrome ring around the simulation viewport. The windowed layout
//! ([`crate::render::window`]) reserves a ring (`ring_cols` columns left/right,
//! `ring_rows` rows top/bottom, set via
//! `SimConfig::frame_matte_cols`/`frame_matte_rows`) and insets the simulation
//! by it, so the simulation never renders under the border. The horizontal ring
//! is wider than the vertical one to offset the ~1:2 (width:height) terminal
//! cell aspect. The ring is filled with an outer accent band plus an inner
//! background-colored matte; the matte also keeps trail content from bleeding
//! past the frame in the blit path (`render_window_frame_at` skips blank cells).
use crate::render::palette::RgbColor;
use crate::simulation::config::WindowFrame;
use crate::terminal::frame_buffer::{Cell, FrameBuffer};
/// Renders window frames on the frame buffer.
pub struct WindowFrameRenderer {
mode: WindowFrame,
accent_color: RgbColor,
/// Background color for the inner separator ring (and negative space).
/// `None` leaves those cells blank (transparent in the blit path).
background_color: Option<RgbColor>,
/// Frame ring thickness in columns per side (border + matte).
ring_cols: usize,
/// Frame ring thickness in rows per side (border + matte).
ring_rows: usize,
}
impl WindowFrameRenderer {
/// Creates a new window frame renderer with the specified mode, accent
/// color, optional background color, and per-side frame ring thickness
/// (`ring_cols`/`ring_rows` = border + background matte).
pub fn new(
mode: WindowFrame,
accent_color: RgbColor,
background_color: Option<RgbColor>,
ring_cols: usize,
ring_rows: usize,
) -> Self {
Self {
mode,
accent_color,
background_color,
ring_cols: ring_cols.max(1),
ring_rows: ring_rows.max(1),
}
}
/// A background-fill cell: a space carrying the configured background color
/// so it counts as non-blank (and thus blits over simulation content). When
/// no background is configured, a plain blank space is used.
fn bg_cell(&self) -> Cell {
match self.background_color {
Some(c) => Cell::new(' ').with_bg(c),
None => Cell::new(' '),
}
}
/// Renders the window frame onto the frame buffer.
pub fn render(&self, buffer: &mut FrameBuffer) {
match self.mode {
WindowFrame::None => {}
WindowFrame::Accented => self.render_accented(buffer),
WindowFrame::Glow => self.render_glow(buffer),
WindowFrame::Frame => self.render_frame(buffer),
}
}
/// Minimum buffer width to draw a ring (two rings + at least 2 sim cols).
fn min_width(&self) -> usize {
self.ring_cols * 2 + 2
}
/// Minimum buffer height to draw a ring (two rings + at least 2 sim rows).
fn min_height(&self) -> usize {
self.ring_rows * 2 + 2
}
/// True when `(x, y)` falls inside the frame ring (not the sim interior).
fn in_ring(&self, x: usize, y: usize, width: usize, height: usize) -> bool {
x < self.ring_cols
|| x >= width - self.ring_cols
|| y < self.ring_rows
|| y >= height - self.ring_rows
}
/// Renders a solid accent border with a background separator just inside it.
/// The ring splits into an outer accent band (half the ring thickness on
/// each axis) and an inner background separator filling the rest.
fn render_accented(&self, buffer: &mut FrameBuffer) {
let width = buffer.width();
let height = buffer.height();
if width < self.min_width() || height < self.min_height() {
return;
}
let color = self.accent_color;
let bg = self.bg_cell();
// Outer accent band thickness (the remaining ring is separator).
let acc_c = (self.ring_cols / 2).max(1);
let acc_r = (self.ring_rows / 2).max(1);
for y in 0..height {
for x in 0..width {
if !self.in_ring(x, y, width, height) {
continue;
}
let is_accent = x < acc_c || x >= width - acc_c || y < acc_r || y >= height - acc_r;
if is_accent {
buffer.set_cell(x, y, Cell::new('█').with_fg(color));
} else {
buffer.set_cell(x, y, bg);
}
}
}
}
/// Renders a gradient border fading from accent color inward, filling the
/// ring from the outer edge toward the simulation.
fn render_glow(&self, buffer: &mut FrameBuffer) {
let width = buffer.width();
let height = buffer.height();
if width < self.min_width() || height < self.min_height() {
return;
}
for y in 0..height {
for x in 0..width {
if !self.in_ring(x, y, width, height) {
continue;
}
// Normalized depth from the outer edge along each axis; the
// shallower axis wins so corners read as the outer (bright) band.
let dc = x.min(width - 1 - x) as f32 / self.ring_cols as f32;
let dr = y.min(height - 1 - y) as f32 / self.ring_rows as f32;
let depth = dc.min(dr).clamp(0.0, 1.0);
let alpha = 1.0 - depth * 0.7;
let ch = if depth < 0.34 {
'█'
} else if depth < 0.67 {
'▓'
} else {
'▒'
};
buffer.set_cell(
x,
y,
Cell::new(ch).with_fg(self.accent_color.with_alpha(alpha)),
);
}
}
}
/// Renders a thin-line box at the outer ring edge, with a background matte
/// (the rest of the ring) between the box and the simulation so trail
/// content does not touch the border.
fn render_frame(&self, buffer: &mut FrameBuffer) {
let width = buffer.width();
let height = buffer.height();
let color = self.accent_color;
if width < self.min_width() || height < self.min_height() {
return;
}
let bg = self.bg_cell();
// Fill the whole ring with the background matte first.
for y in 0..height {
for x in 0..width {
if self.in_ring(x, y, width, height) {
buffer.set_cell(x, y, bg);
}
}
}
// Box at the outer ring edge (the matte sits between it and the sim).
buffer.set_cell(0, 0, Cell::new('┌').with_fg(color));
buffer.set_cell(width - 1, 0, Cell::new('┐').with_fg(color));
buffer.set_cell(0, height - 1, Cell::new('└').with_fg(color));
buffer.set_cell(width - 1, height - 1, Cell::new('┘').with_fg(color));
for x in 1..width - 1 {
buffer.set_cell(x, 0, Cell::new('─').with_fg(color));
buffer.set_cell(x, height - 1, Cell::new('─').with_fg(color));
}
for y in 1..height - 1 {
buffer.set_cell(0, y, Cell::new('│').with_fg(color));
buffer.set_cell(width - 1, y, Cell::new('│').with_fg(color));
}
}
/// Returns true if this mode reduces the available simulation area.
pub fn reduces_display_area(&self) -> bool {
self.mode.reduces_display_area()
}
/// Returns the window frame thickness in cells.
pub fn thickness(&self) -> usize {
self.mode.thickness()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ColorMode;
const RING_C: usize = 5;
const RING_R: usize = 2;
#[test]
fn test_window_frame_renderer_creation() {
let color = RgbColor::new(255, 0, 0);
let renderer = WindowFrameRenderer::new(WindowFrame::Accented, color, None, RING_C, RING_R);
assert_eq!(renderer.mode, WindowFrame::Accented);
assert_eq!(renderer.thickness(), 1);
assert!(!renderer.reduces_display_area());
}
#[test]
fn test_frame_thickness() {
let color = RgbColor::new(255, 0, 0);
let renderer = WindowFrameRenderer::new(WindowFrame::Frame, color, None, RING_C, RING_R);
assert_eq!(renderer.thickness(), 2);
assert!(!renderer.reduces_display_area());
}
#[test]
fn test_glow_thickness() {
let color = RgbColor::new(255, 0, 0);
let renderer = WindowFrameRenderer::new(WindowFrame::Glow, color, None, RING_C, RING_R);
assert_eq!(renderer.thickness(), 3);
assert!(!renderer.reduces_display_area());
}
/// The simulation interior (inset by the frame ring on every side) must
/// never be touched by the Frame border. This is the regression guard for
/// the frame-escape bug: the box sits at the outer ring edge with a matte
/// between it and the sim.
#[test]
fn test_frame_does_not_draw_into_sim_interior() {
let accent = RgbColor::new(255, 0, 0);
let (w, h) = (24usize, 14usize);
let mut buffer = FrameBuffer::new(w, h, ColorMode::TrueColor, None);
// Seed the whole buffer with a sentinel glyph so any frame write shows.
for y in 0..h {
for x in 0..w {
buffer.set_cell(x, y, Cell::new('S'));
}
}
let renderer = WindowFrameRenderer::new(WindowFrame::Frame, accent, None, RING_C, RING_R);
renderer.render(&mut buffer);
// Interior = inset by the ring on every side; must remain the sentinel.
for y in RING_R..h - RING_R {
for x in RING_C..w - RING_C {
assert_eq!(
buffer.get_cell(x, y).char,
'S',
"frame wrote into sim interior at ({x},{y})"
);
}
}
// The box sits at the outer ring edge (top-left corner); a matte
// separates it from the sim interior.
assert_eq!(buffer.get_cell(0, 0).char, '┌');
}
/// With a background color set, the inner matte is filled with a non-blank
/// background cell (so it covers trail content in the blit path).
#[test]
fn test_accented_separator_uses_background_color() {
let accent = RgbColor::new(255, 0, 0);
let bg = RgbColor::new(10, 20, 30);
let mut buffer = FrameBuffer::new(24, 14, ColorMode::TrueColor, None);
let renderer =
WindowFrameRenderer::new(WindowFrame::Accented, accent, Some(bg), RING_C, RING_R);
renderer.render(&mut buffer);
// A matte cell (inside the accent band) carries the background color.
let acc_c = (RING_C / 2).max(1);
let sep = buffer.get_cell(acc_c, RING_R - 1);
assert_eq!(sep.bg_color_rgb, Some(bg), "matte should carry bg color");
}
}