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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use crate::charset::Charset;
use crate::theme::Theme;
use crate::video_buffer::{self, Cell, VideoBuffer};
/// Represents a window in the UI
#[derive(Clone, Debug)]
pub struct Window {
pub id: u32,
pub x: u16,
pub y: u16,
pub width: u16,
pub height: u16,
pub title: String,
// State
pub is_focused: bool,
pub is_minimized: bool,
pub is_maximized: bool,
// Pre-maximize state (for restore)
pre_maximize_x: u16,
pre_maximize_y: u16,
pre_maximize_width: u16,
pre_maximize_height: u16,
}
impl Window {
/// Create a new window
pub fn new(id: u32, x: u16, y: u16, width: u16, height: u16, title: String) -> Self {
// Minimum size to accommodate buttons and resize handle
let width = width.max(20);
let height = height.max(5);
Self {
id,
x,
y,
width,
height,
title,
is_focused: false,
is_minimized: false,
is_maximized: false,
pre_maximize_x: x,
pre_maximize_y: y,
pre_maximize_width: width,
pre_maximize_height: height,
}
}
/// Check if point is in title bar (not including border)
pub fn is_in_title_bar(&self, x: u16, y: u16) -> bool {
x > self.x && x < self.x + self.width - 1 && y == self.y
}
/// Check if point is in close button [X]
pub fn is_in_close_button(&self, x: u16, y: u16) -> bool {
// [X] is at position x+1
y == self.y && x > self.x && x <= self.x + 3
}
/// Check if point is in maximize button [+]
#[allow(dead_code)]
pub fn is_in_maximize_button(&self, x: u16, y: u16) -> bool {
// [+] is at position x+4
y == self.y && x >= self.x + 4 && x <= self.x + 6
}
/// Check if point is in minimize button [_]
#[allow(dead_code)]
pub fn is_in_minimize_button(&self, x: u16, y: u16) -> bool {
// [_] is at position x+7
y == self.y && x >= self.x + 7 && x <= self.x + 9
}
/// Check if point is in resize handle (bottom-right corner)
pub fn is_in_resize_handle(&self, x: u16, y: u16) -> bool {
let corner_x = self.x + self.width - 1;
let corner_y = self.y + self.height - 1;
x == corner_x && y == corner_y
}
/// Check if point is within window bounds (including border)
pub fn contains_point(&self, x: u16, y: u16) -> bool {
x >= self.x && x < self.x + self.width && y >= self.y && y < self.y + self.height
}
/// Maximize the window to fill the screen (except top bar)
pub fn maximize(&mut self, buffer_width: u16, buffer_height: u16) {
if !self.is_maximized {
// Save current position and size
self.pre_maximize_x = self.x;
self.pre_maximize_y = self.y;
self.pre_maximize_width = self.width;
self.pre_maximize_height = self.height;
// Set to full screen (leaving top bar at row 0)
self.x = 0;
self.y = 1;
self.width = buffer_width;
self.height = buffer_height - 1;
self.is_maximized = true;
}
}
/// Restore the window to its pre-maximize state
pub fn restore_from_maximize(&mut self) {
if self.is_maximized {
self.x = self.pre_maximize_x;
self.y = self.pre_maximize_y;
self.width = self.pre_maximize_width;
self.height = self.pre_maximize_height;
self.is_maximized = false;
}
}
/// Toggle maximize state
pub fn toggle_maximize(&mut self, buffer_width: u16, buffer_height: u16) {
if self.is_maximized {
self.restore_from_maximize();
} else {
self.maximize(buffer_width, buffer_height);
}
}
/// Minimize the window (hide it from view)
pub fn minimize(&mut self) {
self.is_minimized = true;
}
/// Restore the window from minimized state
pub fn restore_from_minimize(&mut self) {
self.is_minimized = false;
}
/// Toggle minimize state
#[allow(dead_code)]
pub fn toggle_minimize(&mut self) {
if self.is_minimized {
self.restore_from_minimize();
} else {
self.minimize();
}
}
/// Render the window to the video buffer
pub fn render(
&self,
buffer: &mut VideoBuffer,
is_resizing: bool,
charset: &Charset,
theme: &Theme,
) {
if self.is_minimized {
return;
}
// Draw the window frame
self.render_frame(buffer, is_resizing, charset, theme);
// Draw the title bar with buttons
self.render_title_bar(buffer, theme);
// Draw the content area
self.render_content(buffer, theme);
// Draw the shadow
video_buffer::render_shadow(
buffer,
self.x,
self.y,
self.width,
self.height,
charset,
theme,
);
}
fn render_frame(
&self,
buffer: &mut VideoBuffer,
is_resizing: bool,
charset: &Charset,
theme: &Theme,
) {
// Change title bar background color based on focus
let title_bg = if self.is_focused {
theme.window_title_bg_focused
} else {
theme.window_title_bg
};
// Top border (title bar) - no border characters, just background
for x in 0..self.width {
buffer.set(
self.x + x,
self.y,
Cell::new(' ', theme.window_border, title_bg),
);
}
// Side borders and content
for y in 1..self.height - 1 {
// Left border
buffer.set(
self.x,
self.y + y,
Cell::new(
charset.border_vertical,
theme.window_border,
theme.window_content_bg,
),
);
// Right border
buffer.set(
self.x + self.width - 1,
self.y + y,
Cell::new(
charset.border_vertical,
theme.window_border,
theme.window_content_bg,
),
);
}
// Bottom border
buffer.set(
self.x,
self.y + self.height - 1,
Cell::new(
charset.border_bottom_left,
theme.window_border,
theme.window_content_bg,
),
);
for x in 1..self.width - 1 {
buffer.set(
self.x + x,
self.y + self.height - 1,
Cell::new(
charset.border_horizontal,
theme.window_border,
theme.window_content_bg,
),
);
}
// Bottom-right corner with resize handle
// Change colors based on whether we're actively resizing
let (resize_fg, resize_bg) = if is_resizing {
(theme.resize_handle_active_fg, theme.resize_handle_active_bg) // Bright colors during interaction
} else {
(theme.resize_handle_normal_fg, theme.resize_handle_normal_bg) // Normal state - background matches title bar (black)
};
buffer.set(
self.x + self.width - 1,
self.y + self.height - 1,
Cell::new(charset.resize_handle, resize_fg, resize_bg),
);
}
fn render_title_bar(&self, buffer: &mut VideoBuffer, theme: &Theme) {
// Change title bar background color based on focus
let title_bg = if self.is_focused {
theme.window_title_bg_focused
} else {
theme.window_title_bg
};
// Buttons: [X][+][_] followed by title
let buttons = "[X][+][_] ";
let mut x_offset = 1;
// Render buttons with colored characters and consistent background
for (i, ch) in buttons.chars().enumerate() {
let (fg_color, bg_color) = match ch {
'X' => (theme.button_close_color, theme.button_bg),
'+' => (theme.button_maximize_color, theme.button_bg),
'_' => (theme.button_minimize_color, theme.button_bg),
'[' | ']' => (theme.window_title_fg, theme.button_bg),
_ => (theme.window_title_fg, title_bg), // Space between buttons uses title background
};
buffer.set(self.x + x_offset, self.y, Cell::new(ch, fg_color, bg_color));
x_offset += 1;
// After each button group, there's a space
if i == 2 || i == 5 || i == 8 {
// These are the closing brackets
continue;
}
}
// Render title text
let title_start = self.x + x_offset;
let title_space = (self.width as i32 - x_offset as i32 - 1) as u16;
for (i, ch) in self.title.chars().take(title_space as usize).enumerate() {
buffer.set(
title_start + i as u16,
self.y,
Cell::new(ch, theme.window_title_fg, title_bg),
);
}
}
fn render_content(&self, buffer: &mut VideoBuffer, theme: &Theme) {
// Fill content area with solid background color (no pattern)
let content_char = ' ';
for y in 1..self.height - 1 {
for x in 1..self.width - 1 {
buffer.set(
self.x + x,
self.y + y,
Cell::new(
content_char,
theme.window_content_fg,
theme.window_content_bg,
),
);
}
}
}
}