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

use alloc::string::String;
use alloc::vec::Vec;
use pc_keyboard::DecodedKey;
use crate::graphics::color::Color;
use crate::graphics::surface::Surface;

pub struct EditorApp {
    pub content: Vec<String>,
    pub filename: String,
}

impl EditorApp {
    pub fn new() -> Self {
        let mut ed = Self {
            content: Vec::new(),
            filename: String::from("/user/welcome.txt"),
        };
        ed.content.push(String::from("AeroOS Notepad - Native Text Editor"));
        ed.content.push(String::from("-----------------------------------"));
        ed.content.push(String::from("AeroOS is built completely in safe Rust."));
        ed.content.push(String::from("Direct UEFI GOP 32-bit linear framebuffer."));
        ed.content.push(String::from("Full VirtIO-Net smoltcp networking active!"));
        ed.content.push(String::from(""));
        ed.content.push(String::from("Type anywhere to edit this document..."));
        ed
    }

    pub fn handle_key(&mut self, key: DecodedKey) {
        match key {
            DecodedKey::Unicode('\n') => {
                self.content.push(String::new());
            }
            DecodedKey::Unicode('\u{08}') => {
                if let Some(last_line) = self.content.last_mut() {
                    if !last_line.is_empty() {
                        last_line.pop();
                    } else if self.content.len() > 1 {
                        self.content.pop();
                    }
                }
            }
            DecodedKey::Unicode(c) => {
                if c >= ' ' && c <= '~' {
                    if let Some(last_line) = self.content.last_mut() {
                        if last_line.len() < 50 {
                            last_line.push(c);
                        }
                    } else {
                        let mut s = String::new();
                        s.push(c);
                        self.content.push(s);
                    }
                }
            }
            _ => {}
        }
    }

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

        // Subheader file info
        surface.draw_rect(x, y, w, 24, Color::rgb(22, 24, 32));
        surface.draw_text(&self.filename, x + 10, y + 4, Color::TEXT_SECONDARY);

        // Lines
        let mut cur_y = y + 32;
        for (i, line) in self.content.iter().enumerate() {
            if cur_y + 16 > y + h as i32 - 10 {
                break;
            }
            // Line numbers
            let line_num = alloc::format!("{:2} ", i + 1);
            surface.draw_text(&line_num, x + 8, cur_y, Color::TEXT_MUTED);
            surface.draw_text(line, x + 36, cur_y, Color::WHITE);
            cur_y += 18;
        }

        // Active blinking cursor
        if let Some(last) = self.content.last() {
            let cx = x + 36 + (last.len() as i32 * 8);
            let cy = (cur_y - 18).max(y + 32);
            surface.draw_rect(cx, cy, 2, 14, Color::ACCENT_CYAN);
        }
    }
}