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, 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); } }