File size: 4,842 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
extern crate alloc;

use alloc::format;
use alloc::string::String;
use crate::fs::vfs::VFS;
use crate::graphics::color::Color;
use crate::graphics::icons::IconType;
use crate::graphics::surface::Surface;

pub struct FileManagerApp {
    pub current_path: String,
    pub preview_text: String,
}

impl FileManagerApp {
    pub fn new() -> Self {
        Self {
            current_path: String::from("/user"),
            preview_text: String::from("Select a file to preview its contents."),
        }
    }

    pub fn handle_click(&mut self, rel_x: i32, rel_y: i32) {
        // Sidebar navigation: rel_x < 120
        if rel_x < 120 {
            if rel_y >= 50 && rel_y <= 70 {
                self.current_path = String::from("/");
            } else if rel_y > 70 && rel_y <= 90 {
                self.current_path = String::from("/user");
            } else if rel_y > 90 && rel_y <= 110 {
                self.current_path = String::from("/system");
            } else if rel_y > 110 && rel_y <= 130 {
                self.current_path = String::from("/docs");
            }
        } else {
            // File list click: load file preview
            if let Some(ref fs) = *VFS.lock() {
                if let Some(entries) = fs.list_dir(&self.current_path) {
                    let clicked_row = (rel_y - 50) / 24;
                    if clicked_row >= 0 && (clicked_row as usize) < entries.len() {
                        let (name, is_dir, _) = &entries[clicked_row as usize];
                        if *is_dir {
                            if self.current_path == "/" {
                                self.current_path = format!("/{}", name);
                            } else {
                                self.current_path = format!("{}/{}", self.current_path, name);
                            }
                        } else {
                            let file_path = if self.current_path == "/" {
                                format!("/{}", name)
                            } else {
                                format!("{}/{}", self.current_path, name)
                            };
                            if let Some(bytes) = fs.read_file(&file_path) {
                                if let Ok(s) = core::str::from_utf8(&bytes) {
                                    self.preview_text = s.chars().take(80).collect();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    pub fn render(&self, surface: &mut Surface, x: i32, y: i32, w: usize, h: usize) {
        surface.draw_rect(x, y, w, h, Color::rgb(28, 30, 40));

        // Header bar
        surface.draw_rect(x, y, w, 36, Color::rgb(35, 38, 50));
        surface.draw_icon_16(IconType::FileManager, x + 12, y + 10, Color::rgb(254, 188, 46));
        let path_label = format!("Path: {}", self.current_path);
        surface.draw_text(&path_label, x + 36, y + 11, Color::WHITE);

        // Sidebar (macOS style translucent panel)
        let sidebar_w = 120;
        surface.draw_rect(x, y + 36, sidebar_w, h.saturating_sub(36), Color::rgb(22, 24, 32));
        surface.draw_text("FAVORITES", x + 10, y + 46, Color::TEXT_MUTED);
        surface.draw_text("Root (/)", x + 16, y + 66, Color::TEXT_SECONDARY);
        surface.draw_text("User (/user)", x + 16, y + 86, Color::TEXT_SECONDARY);
        surface.draw_text("System", x + 16, y + 106, Color::TEXT_SECONDARY);
        surface.draw_text("Docs", x + 16, y + 126, Color::TEXT_SECONDARY);

        // Files listing
        let list_x = x + sidebar_w as i32 + 10;
        let mut list_y = y + 46;

        if let Some(ref fs) = *VFS.lock() {
            if let Some(entries) = fs.list_dir(&self.current_path) {
                for (name, is_dir, size) in entries {
                    let icon_color = if is_dir { Color::rgb(254, 188, 46) } else { Color::ACCENT_CYAN };
                    let icon = if is_dir { IconType::FileManager } else { IconType::Editor };
                    surface.draw_icon_16(icon, list_x, list_y, icon_color);

                    let entry_label = if is_dir {
                        format!("{}/", name)
                    } else {
                        format!("{} ({} B)", name, size)
                    };
                    surface.draw_text(&entry_label, list_x + 24, list_y + 1, Color::WHITE);
                    list_y += 24;
                }
            }
        }

        // Preview box at bottom
        let prev_y = y + h as i32 - 40;
        surface.draw_rect(x + sidebar_w as i32, prev_y, w.saturating_sub(sidebar_w), 40, Color::rgb(20, 22, 30));
        surface.draw_text("Preview:", list_x, prev_y + 6, Color::TEXT_MUTED);
        surface.draw_text(&self.preview_text, list_x, prev_y + 22, Color::TEXT_SECONDARY);
    }
}