File size: 12,447 Bytes
dcbeeb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
extern crate alloc;

use alloc::string::String;
use alloc::vec::Vec;
use pc_keyboard::DecodedKey;

use core::sync::atomic::Ordering;
use crate::apps::AppType;
use crate::drivers::ps2_mouse::{SCREEN_HEIGHT, SCREEN_WIDTH};
use crate::graphics::color::Color;
use crate::graphics::surface::Surface;
use crate::window_manager::window::Window;

pub const TITLE_BAR_HEIGHT: usize = 32;
pub const MENUBAR_HEIGHT: usize = 22;

pub struct Compositor {
    pub windows: Vec<Window>,
    pub next_id: usize,
    pub dragging: Option<(usize, i32, i32)>, // (window_id, offset_x, offset_y)
}

impl Compositor {
    pub fn new() -> Self {
        Self {
            windows: Vec::new(),
            next_id: 1,
            dragging: None,
        }
    }

    pub fn open_window(&mut self, title: &str, x: i32, y: i32, w: usize, h: usize, app_type: AppType) -> usize {
        // If already open, unminimize and bring to front
        for win in self.windows.iter_mut() {
            if win.app_type == app_type {
                win.is_minimized = false;
                let id = win.id;
                self.bring_to_front(id);
                return id;
            }
        }

        let id = self.next_id;
        self.next_id += 1;
        let win = Window::new(id, String::from(title), x, y, w, h, app_type);
        self.windows.push(win);
        id
    }

    pub fn bring_to_front(&mut self, id: usize) {
        if let Some(pos) = self.windows.iter().position(|w| w.id == id) {
            let win = self.windows.remove(pos);
            self.windows.push(win);
        }
    }

    pub fn handle_mouse_down(&mut self, mouse_x: i32, mouse_y: i32, is_left: bool) -> bool {
        if !is_left {
            return false;
        }

        let screen_w = SCREEN_WIDTH.load(Ordering::Relaxed) as usize;
        let screen_h = SCREEN_HEIGHT.load(Ordering::Relaxed) as usize;

        // Hit test from top-most window to bottom
        for i in (0..self.windows.len()).rev() {
            let win = &self.windows[i];
            if win.is_minimized {
                continue;
            }

            let in_win = mouse_x >= win.x
                && mouse_x < win.x + win.width as i32
                && mouse_y >= win.y
                && mouse_y < win.y + win.height as i32;

            if in_win {
                let id = win.id;
                self.bring_to_front(id);
                let win = self.windows.last_mut().unwrap();

                // 1. Title bar interactions (Close, Maximize, Minimize, Drag)
                if mouse_y < win.y + TITLE_BAR_HEIGHT as i32 {
                    // A. Windows 11 Top-Right Close Button [ X ] (46px wide x 32px high)
                    if mouse_x >= win.x + win.width as i32 - 46 {
                        self.windows.pop();
                        return true;
                    }

                    // B. Windows 11 Maximize / Restore Button [ □ ] (40px wide)
                    if mouse_x >= win.x + win.width as i32 - 86 {
                        if win.is_maximized {
                            let (px, py, pw, ph) = win.prev_rect;
                            win.x = px;
                            win.y = py;
                            win.width = pw;
                            win.height = ph;
                            win.is_maximized = false;
                        } else {
                            win.prev_rect = (win.x, win.y, win.width, win.height);
                            win.x = 0;
                            win.y = 0;
                            win.width = screen_w as usize;
                            win.height = (screen_h as usize).saturating_sub(44); // Above taskbar
                            win.is_maximized = true;
                        }
                        return true;
                    }

                    // C. Windows 11 Minimize Button [ ─ ] (40px wide)
                    if mouse_x >= win.x + win.width as i32 - 126 {
                        win.is_minimized = true;
                        return true;
                    }

                    // D. Titlebar dragging (mouse grabbed the titlebar)
                    self.dragging = Some((win.id, mouse_x - win.x, mouse_y - win.y));
                    return true;
                }

                // 2. Menu Bar interactions (height 22px)
                if mouse_y < win.y + (TITLE_BAR_HEIGHT + MENUBAR_HEIGHT) as i32 {
                    return true;
                }

                // 3. Inside client area
                let client_top = win.y + (TITLE_BAR_HEIGHT + MENUBAR_HEIGHT) as i32;
                let rel_x = mouse_x - win.x;
                let rel_y = mouse_y - client_top;
                win.handle_client_click(rel_x, rel_y);
                return true;
            }
        }
        false
    }

    pub fn handle_mouse_move(&mut self, mouse_x: i32, mouse_y: i32) {
        if let Some((id, off_x, off_y)) = self.dragging {
            if let Some(win) = self.windows.iter_mut().find(|w| w.id == id) {
                if !win.is_maximized {
                    win.x = mouse_x - off_x;
                    win.y = (mouse_y - off_y).max(0);
                }
            }
        }
    }

    pub fn handle_mouse_up(&mut self) {
        self.dragging = None;
    }

    pub fn handle_key(&mut self, key: DecodedKey) {
        // Escape closes the active window
        match key {
            DecodedKey::Unicode('\u{1b}') | DecodedKey::RawKey(pc_keyboard::KeyCode::Escape) => {
                if !self.windows.is_empty() {
                    self.windows.pop();
                    return;
                }
            }
            DecodedKey::RawKey(pc_keyboard::KeyCode::F6) => {
                if self.windows.len() > 1 {
                    let first = self.windows.remove(0);
                    self.windows.push(first);
                    return;
                }
            }
            _ => {}
        }
        if let Some(active) = self.windows.last_mut() {
            if !active.is_minimized {
                active.handle_key(key);
            }
        }
    }

    pub fn handle_scroll(&mut self, mouse_x: i32, mouse_y: i32, delta: i32) -> bool {
        // First check window under mouse cursor (from front to back)
        for win in self.windows.iter_mut().rev() {
            if win.is_minimized {
                continue;
            }
            if mouse_x >= win.x && mouse_x < win.x + win.width as i32
                && mouse_y >= win.y && mouse_y < win.y + win.height as i32
            {
                win.handle_scroll(delta);
                return true;
            }
        }
        // Fallback: scroll active focused window
        if let Some(active) = self.windows.last_mut() {
            if !active.is_minimized {
                active.handle_scroll(delta);
                return true;
            }
        }
        false
    }

    pub fn render(&mut self, surface: &mut Surface) {
        let active_id = self.windows.last().map(|w| w.id).unwrap_or(0);

        for win in self.windows.iter_mut() {
            if win.is_minimized {
                continue;
            }
            let is_active = win.id == active_id;
            Self::draw_window(win, surface, is_active);
        }
    }

    pub fn render_active(&mut self, surface: &mut Surface) -> Option<(i32, i32, usize, usize)> {
        if let Some(win) = self.windows.last_mut() {
            if !win.is_minimized {
                Self::draw_window(win, surface, true);
                return Some((win.x, win.y, win.width, win.height));
            }
        }
        None
    }

    /// Ultra-fast client-only rendering pass for typing and scrolling:
    /// Completely skips window borders, shadows, titlebars, and menu bars.
    pub fn render_active_client(&mut self, surface: &mut Surface) -> Option<(i32, i32, usize, usize)> {
        if let Some(win) = self.windows.last_mut() {
            if !win.is_minimized {
                let client_x = win.x + 1;
                let client_y = win.y + (TITLE_BAR_HEIGHT + MENUBAR_HEIGHT) as i32;
                let client_w = win.width.saturating_sub(2);
                let client_h = win.height.saturating_sub(TITLE_BAR_HEIGHT + MENUBAR_HEIGHT + 1);

                win.render_content(surface, client_x, client_y, client_w, client_h);
                return Some((client_x, client_y, client_w, client_h));
            }
        }
        None
    }

    pub fn draw_window(win: &mut Window, surface: &mut Surface, is_active: bool) {
        // 1. Soft Windows 11 Drop Shadow
        surface.draw_drop_shadow(win.x, win.y, win.width, win.height, 10, 8);

        // 2. Fluent Acrylic Window Body with Rounded Corners
        surface.draw_rounded_rect(win.x, win.y, win.width, win.height, 8, Color::MICA_SURFACE);

        // 3. Window Border Outline
        let border_color = if is_active {
            Color::rgba(255, 255, 255, 45)
        } else {
            Color::rgba(255, 255, 255, 18)
        };
        surface.draw_rect_outline(win.x, win.y, win.width, win.height, border_color, 1);

        // 4. Titlebar
        let titlebar_color = if is_active {
            Color::rgb(32, 36, 46)
        } else {
            Color::rgb(24, 26, 34)
        };
        surface.draw_rounded_rect(win.x, win.y, win.width, TITLE_BAR_HEIGHT, 8, titlebar_color);
        // Flatten bottom corners of titlebar
        surface.draw_rect(win.x, win.y + TITLE_BAR_HEIGHT as i32 - 6, win.width, 6, titlebar_color);

        // Authentic Windows 11 Titlebar: Left App Icon + Window Title
        let icon_color = match win.app_type {
            AppType::Terminal => Color::ACCENT_CYAN,
            AppType::FileManager => Color::rgb(255, 185, 0),
            AppType::NetManager => Color::rgb(0, 120, 215),
            AppType::WifiManager => Color::rgb(0, 198, 255),
            AppType::SysInfo => Color::rgb(16, 124, 65),
            AppType::Editor => Color::rgb(0, 153, 188),
            AppType::Calculator => Color::rgb(142, 142, 147),
            AppType::Settings => Color::rgb(200, 200, 200),
            AppType::Snake => Color::rgb(177, 70, 194),
            AppType::SnapStore => Color::rgb(233, 84, 32),
        };
        surface.draw_icon_16(win.icon, win.x + 12, win.y + 8, icon_color);
        let title_color = if is_active { Color::WHITE } else { Color::TEXT_MUTED };
        surface.draw_text(&win.title, win.x + 36, win.y + 9, title_color);

        // Windows 11 Top-Right Window Controls
        // Minimize [ ─ ] (40px)
        surface.draw_rounded_rect(win.x + win.width as i32 - 126, win.y + 2, 38, 28, 4, Color::rgba(255, 255, 255, 10));
        surface.draw_rect(win.x + win.width as i32 - 113, win.y + 18, 12, 2, Color::WHITE);

        // Maximize [ □ ] (40px)
        surface.draw_rounded_rect(win.x + win.width as i32 - 86, win.y + 2, 38, 28, 4, Color::rgba(255, 255, 255, 10));
        surface.draw_rect_outline(win.x + win.width as i32 - 73, win.y + 10, 12, 12, Color::WHITE, 1);

        // Close [ ✕ ] (46px) - Bright red highlight
        let close_bg = if is_active { Color::rgb(196, 43, 28) } else { Color::rgba(255, 255, 255, 12) };
        surface.draw_rounded_rect(win.x + win.width as i32 - 46, win.y + 2, 44, 28, 4, close_bg);
        surface.draw_close_cross(win.x + win.width as i32 - 24, win.y + 16, 5, 2, Color::WHITE);

        // 5. Window Menu Bar (File    Edit    View    Help)
        let menu_y = win.y + TITLE_BAR_HEIGHT as i32;
        surface.draw_rect(win.x + 1, menu_y, win.width.saturating_sub(2), MENUBAR_HEIGHT, Color::rgb(28, 31, 40));
        surface.draw_rect(win.x + 1, menu_y + MENUBAR_HEIGHT as i32 - 1, win.width.saturating_sub(2), 1, Color::rgba(255, 255, 255, 15));

        surface.draw_text("File", win.x + 16, menu_y + 4, Color::rgb(210, 215, 225));
        surface.draw_text("Edit", win.x + 60, menu_y + 4, Color::rgb(210, 215, 225));
        surface.draw_text("View", win.x + 104, menu_y + 4, Color::rgb(210, 215, 225));
        surface.draw_text("Help", win.x + 148, menu_y + 4, Color::rgb(210, 215, 225));

        // 6. Client Area Content
        let client_x = win.x + 1;
        let client_y = win.y + (TITLE_BAR_HEIGHT + MENUBAR_HEIGHT) as i32;
        let client_w = win.width.saturating_sub(2);
        let client_h = win.height.saturating_sub(TITLE_BAR_HEIGHT + MENUBAR_HEIGHT + 1);

        win.render_content(surface, client_x, client_y, client_w, client_h);
    }
}