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

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

use crate::apps::calculator::CalculatorApp;
use crate::apps::editor::EditorApp;
use crate::apps::file_manager::FileManagerApp;
use crate::apps::net_manager::NetManagerApp;
use crate::apps::settings::SettingsApp;
use crate::apps::snapstore::SnapStoreApp;
use crate::apps::snake::SnakeApp;
use crate::apps::sysinfo::SysInfoApp;
use crate::apps::terminal::TerminalApp;
use crate::apps::wifi_app::WifiManagerApp;
use crate::apps::AppType;
use crate::graphics::icons::IconType;
use crate::graphics::surface::Surface;

pub struct Window {
    pub id: usize,
    pub title: String,
    pub x: i32,
    pub y: i32,
    pub width: usize,
    pub height: usize,
    pub is_minimized: bool,
    pub is_maximized: bool,
    pub prev_rect: (i32, i32, usize, usize),
    pub app_type: AppType,
    pub icon: IconType,

    // App state
    pub terminal: Option<TerminalApp>,
    pub net_manager: Option<NetManagerApp>,
    pub sysinfo: Option<SysInfoApp>,
    pub file_manager: Option<FileManagerApp>,
    pub calculator: Option<CalculatorApp>,
    pub editor: Option<EditorApp>,
    pub settings: Option<SettingsApp>,
    pub snake: Option<SnakeApp>,
    pub snapstore: Option<SnapStoreApp>,
    pub wifi_manager: Option<WifiManagerApp>,
}

impl Window {
    pub fn new(id: usize, title: String, x: i32, y: i32, width: usize, height: usize, app_type: AppType) -> Self {
        let mut term = None;
        let mut net = None;
        let mut sys = None;
        let mut fm = None;
        let mut calc = None;
        let mut ed = None;
        let mut set = None;
        let mut snake = None;
        let mut snap = None;
        let mut wifi = None;
        let icon;

        match app_type {
            AppType::Terminal => {
                icon = IconType::Terminal;
                term = Some(TerminalApp::new());
            }
            AppType::NetManager => {
                icon = IconType::Network;
                net = Some(NetManagerApp::new());
            }
            AppType::WifiManager => {
                icon = IconType::Network;
                wifi = Some(WifiManagerApp::new());
            }
            AppType::SysInfo => {
                icon = IconType::TaskManager;
                sys = Some(SysInfoApp::new());
            }
            AppType::FileManager => {
                icon = IconType::FileManager;
                fm = Some(FileManagerApp::new());
            }
            AppType::Calculator => {
                icon = IconType::Calculator;
                calc = Some(CalculatorApp::new());
            }
            AppType::Editor => {
                icon = IconType::Editor;
                ed = Some(EditorApp::new());
            }
            AppType::Settings => {
                icon = IconType::Settings;
                set = Some(SettingsApp::new());
            }
            AppType::Snake => {
                icon = IconType::TaskManager;
                snake = Some(SnakeApp::new());
            }
            AppType::SnapStore => {
                icon = IconType::OsLogo;
                snap = Some(SnapStoreApp::new());
            }
        }

        Self {
            id,
            title,
            x,
            y,
            width,
            height,
            is_minimized: false,
            is_maximized: false,
            prev_rect: (x, y, width, height),
            app_type,
            icon,
            terminal: term,
            net_manager: net,
            sysinfo: sys,
            file_manager: fm,
            calculator: calc,
            editor: ed,
            settings: set,
            snake,
            snapstore: snap,
            wifi_manager: wifi,
        }
    }

    pub fn handle_key(&mut self, key: DecodedKey) {
        if let Some(ref mut term) = self.terminal {
            term.handle_key(key);
        } else if let Some(ref mut ed) = self.editor {
            ed.handle_key(key);
        } else if let Some(ref mut snake) = self.snake {
            snake.handle_key(key);
        } else if let Some(ref mut snap) = self.snapstore {
            snap.handle_key(key);
        }
    }

    pub fn handle_scroll(&mut self, delta: i32) {
        if let Some(ref mut term) = self.terminal {
            term.handle_scroll(delta);
        } else if let Some(ref mut snap) = self.snapstore {
            snap.handle_scroll(delta);
        }
    }

    pub fn handle_client_click(&mut self, rel_x: i32, rel_y: i32) {
        if let Some(ref mut wifi) = self.wifi_manager {
            wifi.handle_click(rel_x, rel_y);
        } else if let Some(ref mut net) = self.net_manager {
            net.handle_click(rel_x, rel_y);
        } else if let Some(ref mut fm) = self.file_manager {
            fm.handle_click(rel_x, rel_y);
        } else if let Some(ref mut calc) = self.calculator {
            calc.handle_click(rel_x, rel_y);
        } else if let Some(ref mut snap) = self.snapstore {
            snap.handle_click(rel_x, rel_y);
        } else if let Some(ref mut set) = self.settings {
            set.handle_click(rel_x, rel_y);
        }
    }

    pub fn render_content(&mut self, surface: &mut Surface, client_x: i32, client_y: i32, client_w: usize, client_h: usize) {
        if let Some(ref term) = self.terminal {
            term.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref wifi) = self.wifi_manager {
            wifi.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref net) = self.net_manager {
            net.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref sys) = self.sysinfo {
            sys.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref fm) = self.file_manager {
            fm.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref calc) = self.calculator {
            calc.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref ed) = self.editor {
            ed.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref set) = self.settings {
            set.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref mut snake) = self.snake {
            snake.render(surface, client_x, client_y, client_w, client_h);
        } else if let Some(ref snap) = self.snapstore {
            snap.render(surface, client_x, client_y, client_w, client_h);
        }
    }
}