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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// (C) 2025 - Enzo Lombardi
//! Desktop view - main workspace for managing application windows.
use crate::core::geometry::Rect;
use crate::core::event::Event;
use crate::terminal::Terminal;
use super::view::{View, ViewId};
use super::group::Group;
use super::background::Background;
pub struct Desktop {
bounds: Rect,
children: Group,
palette_chain: Option<crate::core::palette_chain::PaletteChainNode>,
}
impl Desktop {
pub fn new(bounds: Rect) -> Self {
let mut children = Group::new(bounds);
// Add background as first child (matches Borland's TDeskTop::TDeskTop)
// Background fills the entire desktop area
// NOTE: Must use relative coordinates (0, 0) because Group.add() converts to absolute
let width = bounds.width();
let height = bounds.height();
let background_bounds = Rect::new(0, 0, width, height);
let background = Box::new(Background::new(background_bounds, 'â–‘', crate::core::palette::colors::DESKTOP));
children.add(background);
Self {
bounds,
children,
palette_chain: None,
}
}
/// Initialize the palette chain after Desktop is in its final memory location.
/// Must be called after Desktop is constructed and in a stable location (not moved).
/// Matches Borland: Desktop is the root of the palette chain with CP_APP_COLOR.
pub fn init_palette_chain(&mut self) {
// Set children's owner to this Desktop (for palette chain)
// Desktop provides CP_APP_COLOR palette, making it the palette root
// NOTE: We don't set owner pointer to avoid unsafe casting
}
pub fn add(&mut self, mut view: Box<dyn View>) -> ViewId {
use crate::core::state::{OF_CENTERED, OF_CENTER_X, OF_CENTER_Y};
// Set parent bounds for safe drag limit resolution
view.set_parent_bounds(self.bounds());
// Apply automatic centering if OF_CENTERED flags are set
// Matches Borland: TView with ofCentered is centered when inserted
let options = view.options();
if (options & OF_CENTERED) != 0 || (options & OF_CENTER_X) != 0 || (options & OF_CENTER_Y) != 0 {
self.center_view(&mut *view, options);
}
// Constrain window to Desktop bounds (prevents centering from placing window out of bounds)
// This ensures windows with shadows don't extend below status bar
// Matches Borland: TView::locate() constrains position to owner bounds
view.constrain_to_parent_bounds();
let view_id = self.children.add(view);
// Initialize internal owner pointers after view is in final position
// This is critical for views like Dialog that contain Groups by value
let num_children = self.children.len();
if num_children > 0 {
let last_idx = num_children - 1;
self.children.child_at_mut(last_idx).init_after_add();
}
// Focus on the newly added window (last child)
if num_children > 0 {
let last_idx = num_children - 1;
if self.children.child_at(last_idx).can_focus() {
// Clear focus from all children first
self.children.clear_all_focus();
// Then give focus to the new window
self.children.set_focus_to(last_idx);
}
}
view_id
}
/// Center a view within the desktop bounds based on its option flags
/// Matches Borland: Views with ofCentered are automatically centered
fn center_view(&self, view: &mut dyn View, options: u16) {
use crate::core::state::{OF_CENTER_X, OF_CENTER_Y};
let view_bounds = view.bounds();
let desktop_bounds = self.bounds;
let mut new_bounds = view_bounds;
// Center horizontally if OF_CENTER_X is set
if (options & OF_CENTER_X) != 0 {
let view_width = view_bounds.width();
let desktop_width = desktop_bounds.width();
let center_x = (desktop_width - view_width) / 2;
new_bounds.a.x = center_x;
new_bounds.b.x = center_x + view_width;
}
// Center vertically if OF_CENTER_Y is set
if (options & OF_CENTER_Y) != 0 {
let view_height = view_bounds.height();
let desktop_height = desktop_bounds.height();
let center_y = (desktop_height - view_height) / 2;
new_bounds.a.y = center_y;
new_bounds.b.y = center_y + view_height;
}
view.set_bounds(new_bounds);
}
/// Get the number of child views (windows) on the desktop
/// Note: Subtracts 1 because the background is also a child
pub fn child_count(&self) -> usize {
self.children.len().saturating_sub(1)
}
/// Get a reference to a child view by index
/// Note: Index 0 refers to the first window (background is at internal index 0)
pub fn child_at(&self, index: usize) -> &dyn View {
self.children.child_at(index + 1) // +1 to skip background
}
/// Get a mutable reference to a child view by index
/// Note: Index 0 refers to the first window (background is at internal index 0)
pub fn child_at_mut(&mut self, index: usize) -> &mut dyn View {
self.children.child_at_mut(index + 1) // +1 to skip background
}
/// Remove a child view by index
/// Note: Index 0 refers to the first window (background is at internal index 0)
/// Used by Application::exec_view() to remove modal dialogs after they close
pub fn remove_child(&mut self, index: usize) {
self.children.remove(index + 1); // +1 to skip background
}
/// Draw views in the affected rectangle (Borland's drawUnderRect pattern)
/// This is called when a window moves to redraw only the affected area
/// Matches Borland: TView::drawUnderRect() (tview.cc:304-308)
pub fn draw_under_rect(&mut self, terminal: &mut Terminal, rect: Rect, start_from_window: usize) {
// +1 to account for background being at index 0
let start_index = start_from_window + 1;
// Draw background in the affected rect first
terminal.push_clip(rect);
self.children.child_at_mut(0).draw(terminal);
terminal.pop_clip();
// Then draw all windows from start_index onwards in the affected rect
self.children.draw_sub_views(terminal, start_index, rect);
}
/// Check for moved windows and redraw affected areas
/// Matches Borland: TProgram::idle() checks for moved views and calls drawUnderRect
/// This is called after event handling to redraw areas exposed by window movement
/// Returns true if any windows were moved and redrawn
pub fn handle_moved_windows(&mut self, terminal: &mut Terminal) -> bool {
let mut had_movement = false;
// Check each window (skip background at index 0)
// We iterate in reverse because we need to check from front to back (z-order)
for i in 1..self.children.len() {
// Check if this view has moved
if let Some(union_rect) = self.children.child_at(i).get_redraw_union() {
// This window moved - redraw the union rect area
// Start from the moved window's position (all views behind it)
// Matches Borland: TView::locate() → TView::drawUnderRect()
self.draw_under_rect(terminal, union_rect, i - 1); // -1 because Desktop uses window indices, not internal indices
// Clear the movement tracking after redrawing
self.children.child_at_mut(i).clear_move_tracking();
had_movement = true;
}
}
had_movement
}
}
impl Desktop {
/// Get desktop bounds for window operations
/// Used by windows to determine maximum zoom size
pub fn get_bounds(&self) -> Rect {
self.bounds
}
/// Check if any child window is tileable
/// Used for enabling/disabling tile/cascade commands
/// Matches Borland: deskTop->firstThat(isTileable, 0) != 0
pub fn has_tileable_windows(&self) -> bool {
use crate::core::state::OF_TILEABLE;
// Skip background (index 0)
for i in 1..self.children.len() {
let child = self.children.child_at(i);
if (child.options() & OF_TILEABLE) != 0 {
return true;
}
}
false
}
/// Count tileable windows on desktop
/// Used for tile/cascade algorithms
pub fn count_tileable_windows(&self) -> usize {
use crate::core::state::OF_TILEABLE;
let mut count = 0;
for i in 1..self.children.len() {
let child = self.children.child_at(i);
if (child.options() & OF_TILEABLE) != 0 {
count += 1;
}
}
count
}
/// Cascade windows in a staircase pattern (using full desktop bounds)
/// Matches Borland: TDesktop::cascade(const TRect &r)
pub fn cascade(&mut self) {
self.cascade_with_rect(self.bounds);
}
/// Cascade windows in a staircase pattern within specified rect
/// Matches Borland: TDesktop::cascade(const TRect &r)
pub fn cascade_with_rect(&mut self, rect: Rect) {
use crate::core::state::OF_TILEABLE;
// Count tileable windows (skip background at index 0)
let mut count = 0;
for i in 1..self.children.len() {
let child = self.children.child_at(i);
let options = child.options();
if (options & OF_TILEABLE) != 0 {
count += 1;
}
}
if count == 0 {
return;
}
// Calculate cascade bounds (leave room for offset)
let cascade_bounds = rect;
// Position windows in cascade (staircase) pattern
// Bottom window (first in z-order) has no offset (leftmost)
// Top window (last in z-order) has maximum offset (rightmost)
let mut cascade_index: usize = 0;
for i in 1..self.children.len() {
let child = self.children.child_at(i);
let options = child.options();
if (options & OF_TILEABLE) != 0 {
// Calculate new bounds with cascade offset
let mut new_bounds = cascade_bounds;
new_bounds.a.x += cascade_index as i16;
new_bounds.a.y += cascade_index as i16;
// Adjust size to account for offset (so window fits in desktop)
new_bounds.b.x -= (count - 1 - cascade_index) as i16;
new_bounds.b.y -= (count - 1 - cascade_index) as i16;
self.children.child_at_mut(i).set_bounds(new_bounds);
cascade_index += 1;
}
}
}
/// Tile windows in a grid pattern (using full desktop bounds)
/// Matches Borland: TDesktop::tile(const TRect &r)
pub fn tile(&mut self) {
self.tile_with_rect(self.bounds);
}
/// Tile windows in a grid pattern within specified rect
/// Matches Borland: TDesktop::tile(const TRect &r)
pub fn tile_with_rect(&mut self, rect: Rect) {
use crate::core::state::OF_TILEABLE;
// Count tileable windows (skip background at index 0)
let mut count = 0;
for i in 1..self.children.len() {
let child = self.children.child_at(i);
let options = child.options();
if (options & OF_TILEABLE) != 0 {
count += 1;
}
}
if count == 0 {
return;
}
// Calculate grid dimensions (most square layout)
let (cols, rows) = Self::calculate_grid_layout(count);
let tile_bounds = rect;
let cell_width = tile_bounds.width() / cols as i16;
let cell_height = tile_bounds.height() / rows as i16;
// Position windows in grid
let mut tile_index = 0;
for i in 1..self.children.len() {
let child = self.children.child_at(i);
let options = child.options();
if (options & OF_TILEABLE) != 0 {
let col = tile_index % cols;
let row = tile_index / cols;
let new_bounds = Rect::new(
tile_bounds.a.x + (col as i16 * cell_width),
tile_bounds.a.y + (row as i16 * cell_height),
tile_bounds.a.x + ((col + 1) as i16 * cell_width),
tile_bounds.a.y + ((row + 1) as i16 * cell_height),
);
self.children.child_at_mut(i).set_bounds(new_bounds);
tile_index += 1;
}
}
}
/// Calculate grid layout (rows x cols) that's most square
/// Matches Borland: mostEqualDivisors()
fn calculate_grid_layout(count: usize) -> (usize, usize) {
if count == 0 {
return (1, 1);
}
// Find the square root (approximately)
let sqrt = (count as f64).sqrt() as usize;
// Find divisors closest to square root
let mut cols = sqrt;
while count % cols != 0 && cols > 1 {
cols -= 1;
}
if cols == 1 {
// Prime number or couldn't find good divisor
cols = sqrt;
if cols * cols < count {
cols += 1;
}
}
let rows = (count + cols - 1) / cols; // Ceiling division
(cols, rows)
}
/// Cycle to the next window (Borland: selectNext)
/// Moves the current top window to the back, bringing the next window forward
/// Matches Borland: cmNext command calls selectNext(False)
pub fn select_next(&mut self) {
use crate::core::state::OF_TOP_SELECT;
// Need at least 2 windows (plus background) to cycle
if self.children.len() <= 2 {
return;
}
// Get the current top window (last in children list, excluding background)
let top_window_idx = self.children.len() - 1;
// Check if top window has OF_TOP_SELECT flag
let has_top_select = {
let options = self.children.child_at(top_window_idx).options();
(options & OF_TOP_SELECT) != 0
};
if has_top_select {
// Move top window behind all others (after background)
// This is equivalent to Borland's: current->putInFrontOf(background)
self.children.send_to_back(top_window_idx);
}
}
/// Cycle to the previous window (Borland: selectPrev)
/// Brings the bottom window to the top
/// Matches Borland: cmPrev command calls current->putInFrontOf(background)
pub fn select_prev(&mut self) {
use crate::core::state::OF_TOP_SELECT;
// Need at least 2 windows (plus background) to cycle
if self.children.len() <= 2 {
return;
}
// Get the bottom window (right after background)
let bottom_window_idx = 1;
// Check if it has OF_TOP_SELECT flag
let has_top_select = {
let options = self.children.child_at(bottom_window_idx).options();
(options & OF_TOP_SELECT) != 0
};
if has_top_select {
// Bring bottom window to front
self.children.bring_to_front(bottom_window_idx);
}
}
/// Get a mutable reference to a window by index (for movement tracking)
/// Returns None if index is out of bounds
/// Index 0 refers to first window (background is at internal index 0)
pub fn window_at_mut(&mut self, index: usize) -> Option<&mut dyn View> {
let internal_index = index + 1; // +1 to skip background
if internal_index < self.children.len() {
Some(self.children.child_at_mut(internal_index))
} else {
None
}
}
/// Zoom the topmost window
/// Matches Borland: Desktop handles cmZoom and calls window->zoom()
/// In Borland, TWindow::zoom() calls sizeLimits() which gets owner->size
pub fn zoom_top_window(&mut self) {
// Get the topmost window (last in children list, excluding background)
if self.children.len() <= 1 {
return; // No windows to zoom
}
let top_window_idx = self.children.len() - 1;
// Call zoom on the topmost view (typically a Window)
// This matches Borland: owner handles cmZoom, calls window->zoom()
// window->zoom() uses sizeLimits() which returns owner->size as max
// We pass desktop bounds (equivalent to owner->size in Borland)
let desktop_bounds = self.bounds;
self.children.child_at_mut(top_window_idx).zoom(desktop_bounds);
}
/// Remove closed windows (those with SF_CLOSED flag)
/// In Borland, views call CLY_destroy() which removes them from the owner
/// In Rust, views set SF_CLOSED flag and the parent removes them
/// This is called after event handling in the main loop
/// Returns true if any windows were removed
pub fn remove_closed_windows(&mut self) -> bool {
use crate::core::state::SF_CLOSED;
let mut had_removals = false;
// Remove windows marked as closed (skip background at index 0)
// We need to iterate in reverse to avoid index shifting issues
let mut i = self.children.len();
while i > 1 { // Don't remove background at index 0
i -= 1;
if (self.children.child_at(i).state() & SF_CLOSED) != 0 {
self.children.remove(i);
had_removals = true;
}
}
had_removals
}
}
impl View for Desktop {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
self.children.set_bounds(bounds);
}
fn draw(&mut self, terminal: &mut Terminal) {
// Just draw all children (background is the first child, windows come after)
// This matches Borland's TDeskTop which is a TGroup with TBackground as first child
self.children.draw(terminal);
}
fn handle_event(&mut self, event: &mut Event) {
use crate::core::event::EventType;
use crate::core::state::SF_MODAL;
// Check if the topmost window is modal
// Modal windows capture all events - clicks on other windows have no effect
// Matches Borland: TGroup::execView() creates modal scope
let has_modal = if self.children.len() > 1 {
let top_window_idx = self.children.len() - 1;
(self.children.child_at(top_window_idx).state() & SF_MODAL) != 0
} else {
false
};
// Handle z-order changes on mouse down (only when no modal window is present)
// When a window is clicked, bring it to the front if it has OF_TOP_SELECT flag
// Matches Borland: TView::handleEvent() calls focus() -> select() -> makeFirst() if ofTopSelect set
if !has_modal && event.what == EventType::MouseDown {
use crate::core::state::OF_TOP_SELECT;
let mouse_pos = event.mouse.pos;
// Find which window was clicked (search in reverse z-order, skip background at 0)
let mut clicked_window: Option<usize> = None;
for i in (1..self.children.len()).rev() {
let child_bounds = self.children.child_at(i).bounds();
if child_bounds.contains(mouse_pos) {
clicked_window = Some(i);
break;
}
}
// If a window was clicked and it's not already on top, bring it to front
// Only if the window has OF_TOP_SELECT flag set (matches Borland: ofTopSelect)
if let Some(window_idx) = clicked_window {
let last_idx = self.children.len() - 1;
if window_idx != last_idx {
let window_options = self.children.child_at(window_idx).options();
if (window_options & OF_TOP_SELECT) != 0 {
// Bring window to front (Borland: makeFirst())
self.children.bring_to_front(window_idx);
// Note: We don't return here - let the event propagate to the window
}
}
}
}
// Handle desktop-level commands
// Matches Borland: TDesktop::handleEvent (tdesktop.cc:103-133)
if event.what == EventType::Command {
use crate::core::command::{CM_NEXT, CM_PREV};
use crate::core::state::SF_FOCUSED;
match event.command {
CM_NEXT => {
// Cycle to next window (send top window to back)
// Matches Borland: cmNext command calls selectNext(False)
if self.children.len() > 2 {
// Clear focus from current top window
let old_top_idx = self.children.len() - 1;
let old_state = self.children.child_at(old_top_idx).state();
if (old_state & SF_FOCUSED) != 0 {
self.children.child_at_mut(old_top_idx).set_focus(false);
}
// Move top window to back
self.children.send_to_back(old_top_idx);
// Focus the new top window
let new_top_idx = self.children.len() - 1;
self.children.child_at_mut(new_top_idx).set_focus(true);
}
event.clear();
return;
}
CM_PREV => {
// Cycle to previous window (bring bottom window to front)
// Matches Borland: cmPrev calls current->putInFrontOf(background)
if self.children.len() > 2 {
// Clear focus from current top window
let old_top_idx = self.children.len() - 1;
let old_state = self.children.child_at(old_top_idx).state();
if (old_state & SF_FOCUSED) != 0 {
self.children.child_at_mut(old_top_idx).set_focus(false);
}
// Bring bottom window (after background) to front
self.children.bring_to_front(1);
// Focus the new top window
let new_top_idx = self.children.len() - 1;
self.children.child_at_mut(new_top_idx).set_focus(true);
}
event.clear();
return;
}
_ => {}
}
}
// If there's a modal window, only send events to it
// Matches Borland: modal views block events to views behind them
if has_modal {
let modal_idx = self.children.len() - 1;
self.children.child_at_mut(modal_idx).handle_event(event);
} else {
self.children.handle_event(event);
}
}
fn set_palette_chain(&mut self, node: Option<crate::core::palette_chain::PaletteChainNode>) {
self.palette_chain = node;
}
fn get_palette_chain(&self) -> Option<&crate::core::palette_chain::PaletteChainNode> {
self.palette_chain.as_ref()
}
fn get_palette(&self) -> Option<crate::core::palette::Palette> {
use crate::core::palette::{Palette, palettes};
// Desktop uses the application palette directly (no remapping)
let app_palette_data = palettes::get_app_palette();
Some(Palette::from_slice(&app_palette_data))
}
}
/// Builder for creating desktops with a fluent API.
pub struct DesktopBuilder {
bounds: Option<Rect>,
}
impl DesktopBuilder {
pub fn new() -> Self {
Self { bounds: None }
}
#[must_use]
pub fn bounds(mut self, bounds: Rect) -> Self {
self.bounds = Some(bounds);
self
}
pub fn build(self) -> Desktop {
let bounds = self.bounds.expect("Desktop bounds must be set");
Desktop::new(bounds)
}
pub fn build_boxed(self) -> Box<Desktop> {
Box::new(self.build())
}
}
impl Default for DesktopBuilder {
fn default() -> Self {
Self::new()
}
}