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

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use spin::Mutex;
use crate::graphics::color::Color;
use crate::graphics::icons::IconType;
use crate::graphics::surface::Surface;

#[derive(Debug, Clone)]
pub struct SnapApp {
    pub name: &'static str,
    pub title: &'static str,
    pub publisher: &'static str,
    pub version: &'static str,
    pub size_mb: usize,
    pub is_installed: bool,
    pub category: &'static str,
}

pub static SNAP_CATALOG: Mutex<[SnapApp; 8]> = Mutex::new([
    SnapApp { name: "code", title: "Visual Studio Code", publisher: "Microsoft", version: "1.84.2", size_mb: 88, is_installed: false, category: "Development" },
    SnapApp { name: "firefox", title: "Firefox Web Browser", publisher: "Mozilla", version: "120.0", size_mb: 74, is_installed: false, category: "Productivity" },
    SnapApp { name: "discord", title: "Discord", publisher: "Discord Inc.", version: "0.0.35", size_mb: 62, is_installed: false, category: "Media" },
    SnapApp { name: "spotify", title: "Spotify Music", publisher: "Spotify Ltd.", version: "1.2.22", size_mb: 58, is_installed: false, category: "Media" },
    SnapApp { name: "vlc", title: "VLC Media Player", publisher: "VideoLAN", version: "3.0.19", size_mb: 42, is_installed: false, category: "Media" },
    SnapApp { name: "blender", title: "Blender 3D Suite", publisher: "Blender Foundation", version: "4.0.1", size_mb: 185, is_installed: false, category: "Development" },
    SnapApp { name: "retroarch", title: "RetroArch Emulator", publisher: "Libretro", version: "1.16.0", size_mb: 95, is_installed: false, category: "Games" },
    SnapApp { name: "docker", title: "Docker Platform", publisher: "Docker Inc.", version: "24.0.7", size_mb: 120, is_installed: false, category: "Development" },
]);

pub fn init() {
    // Initialize snapd daemon state and snap catalog
    let _catalog = SNAP_CATALOG.lock();
}

pub struct SnapStoreApp {
    pub selected_category: &'static str,
    pub status_message: String,
    pub scroll_offset: usize,
}

impl SnapStoreApp {
    pub fn new() -> Self {
        Self {
            selected_category: "Featured",
            status_message: String::from("Snap Store ready. Canonical snapd daemon active."),
            scroll_offset: 0,
        }
    }

    pub fn handle_scroll(&mut self, delta: i32) {
        if delta > 0 {
            self.scroll_offset = self.scroll_offset.saturating_sub(1);
        } else if delta < 0 {
            self.scroll_offset = (self.scroll_offset + 1).min(4);
        }
    }

    pub fn handle_key(&mut self, key: pc_keyboard::DecodedKey) {
        match key {
            pc_keyboard::DecodedKey::RawKey(pc_keyboard::KeyCode::PageUp) | pc_keyboard::DecodedKey::RawKey(pc_keyboard::KeyCode::ArrowUp) => {
                self.handle_scroll(1);
            }
            pc_keyboard::DecodedKey::RawKey(pc_keyboard::KeyCode::PageDown) | pc_keyboard::DecodedKey::RawKey(pc_keyboard::KeyCode::ArrowDown) => {
                self.handle_scroll(-1);
            }
            _ => {}
        }
    }

    pub fn handle_click(&mut self, rel_x: i32, rel_y: i32) {
        // Category bar: y in 10..36
        if rel_y >= 10 && rel_y <= 36 {
            if rel_x >= 20 && rel_x <= 100 {
                self.selected_category = "Featured";
                self.scroll_offset = 0;
                return;
            } else if rel_x >= 110 && rel_x <= 210 {
                self.selected_category = "Development";
                self.scroll_offset = 0;
                return;
            } else if rel_x >= 220 && rel_x <= 310 {
                self.selected_category = "Media";
                self.scroll_offset = 0;
                return;
            } else if rel_x >= 320 && rel_x <= 400 {
                self.selected_category = "Games";
                self.scroll_offset = 0;
                return;
            }
        }

        // Install/Remove Buttons on cards
        let mut catalog = SNAP_CATALOG.lock();
        let mut matched = 0;
        let mut rendered = 0;

        for app in catalog.iter_mut() {
            if self.selected_category != "Featured" && app.category != self.selected_category {
                continue;
            }
            if matched < self.scroll_offset {
                matched += 1;
                continue;
            }
            if rendered >= 5 { break; }

            let row_y = 56 + (rendered as i32 * 48);
            if rel_y >= row_y + 4 && rel_y <= row_y + 38 && rel_x >= 360 && rel_x <= 485 {
                app.is_installed = !app.is_installed;
                if app.is_installed {
                    self.status_message = format!("Installed {} ({} MB squashfs mounted)", app.name, app.size_mb);
                } else {
                    self.status_message = format!("Removed snap package {}", app.name);
                }
                break;
            }
            rendered += 1;
        }
    }

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

        // 1. Category Bar
        let cats = ["Featured", "Development", "Media", "Games"];
        let mut cat_x = x + 20;

        for &cat in cats.iter() {
            let is_sel = cat == self.selected_category;
            let bg = if is_sel { Color::rgb(233, 84, 32) } else { Color::rgba(255, 255, 255, 10) };
            let btn_w = cat.len() as usize * 9 + 18;
            surface.draw_rounded_rect(cat_x, y + 10, btn_w, 26, 4, bg);
            surface.draw_text(cat, cat_x + 9, y + 16, Color::WHITE);
            cat_x += btn_w as i32 + 10;
        }

        // Divider
        surface.draw_rect(x, y + 44, w, 1, Color::rgba(255, 255, 255, 20));

        // 2. Apps List
        let catalog = SNAP_CATALOG.lock();
        let card_w = w.saturating_sub(44);
        let mut row_y = y + 54;
        let mut matched = 0;
        let mut rendered = 0;

        for app in catalog.iter() {
            if self.selected_category != "Featured" && app.category != self.selected_category {
                continue;
            }
            if matched < self.scroll_offset {
                matched += 1;
                continue;
            }
            if rendered >= 5 { break; }

            // Card background
            surface.draw_rounded_rect(x + 20, row_y, card_w, 42, 6, Color::rgb(32, 36, 48));
            surface.draw_rect_outline(x + 20, row_y, card_w, 42, Color::rgba(255, 255, 255, 14), 1);

            // Icon
            let icon_col = match app.category {
                "Development" => Color::ACCENT_CYAN,
                "Games" => Color::rgb(177, 70, 194),
                _ => Color::rgb(16, 185, 129),
            };
            surface.draw_icon_16(IconType::Terminal, x + 30, row_y + 13, icon_col);

            // App Title & Publisher
            surface.draw_text(app.title, x + 54, row_y + 8, Color::WHITE);
            let sub = format!("{} • v{} • {} MB", app.publisher, app.version, app.size_mb);
            surface.draw_text(&sub, x + 54, row_y + 24, Color::TEXT_MUTED);

            // Install/Remove Button
            let btn_x = x + card_w as i32 - 76;
            let (btn_txt, btn_bg) = if app.is_installed {
                ("Remove", Color::rgb(180, 40, 40))
            } else {
                ("Install", Color::rgb(233, 84, 32))
            };
            surface.draw_rounded_rect(btn_x, row_y + 8, 70, 26, 4, btn_bg);
            surface.draw_text(btn_txt, btn_x + 10, row_y + 14, Color::WHITE);

            row_y += 48;
            rendered += 1;
        }

        // Modern Scrollbar on the right
        let sb_x = x + w as i32 - 12;
        let sb_y = y + 54;
        let sb_h = (h as i32 - 90).max(30);
        surface.draw_rounded_rect(sb_x, sb_y, 6, sb_h as usize, 3, Color::rgba(255, 255, 255, 12));
        let thumb_y = sb_y + ((self.scroll_offset as i32 * (sb_h - 26)) / 4).clamp(0, sb_h - 26);
        surface.draw_rounded_rect(sb_x, thumb_y, 6, 26, 3, Color::rgba(233, 84, 32, 180));

        // 3. Footer status
        surface.draw_rect(x, y + h as i32 - 28, w, 28, Color::rgb(20, 22, 30));
        surface.draw_text(&self.status_message, x + 16, y + h as i32 - 20, Color::rgb(0, 190, 255));
    }
}

pub fn run_snap_cli(args: &[&str]) -> Vec<String> {
    let mut out = Vec::new();

    if args.is_empty() {
        out.push(String::from("snap 2.58.3 (x86_64) - Snap Store Client"));
        out.push(String::from("Usage: snap [command] [snap_name]"));
        out.push(String::from("Commands:"));
        out.push(String::from("  install <snap>   - Install a snap from Snap Store"));
        out.push(String::from("  remove <snap>    - Remove an installed snap"));
        out.push(String::from("  list             - List installed snaps"));
        out.push(String::from("  find <query>     - Find snaps in the store"));
        return out;
    }

    match args[0] {
        "install" => {
            if args.len() < 2 {
                out.push(String::from("error: please specify a snap name to install"));
                return out;
            }
            let target = args[1];
            let mut catalog = SNAP_CATALOG.lock();
            let mut found = false;

            for app in catalog.iter_mut() {
                if app.name == target {
                    found = true;
                    if app.is_installed {
                        out.push(format!("snap \"{}\" is already installed, see 'snap refresh --help'", target));
                    } else {
                        out.push(format!("Download snap \"{}\" ({} MB) from channel \"stable\"... Done", app.name, app.size_mb));
                        out.push(format!("Fetch and check assertions for snap \"{}\" ({})... Done", app.name, app.version));
                        out.push(format!("Mount snap \"{}\" ({}) to /snap/{}... Done", app.name, app.version, app.name));
                        out.push(format!("Set up security profiles for snap \"{}\"... Done", app.name));
                        out.push(format!("[+] {} {} from {} installed", app.name, app.version, app.publisher));
                        app.is_installed = true;
                    }
                    break;
                }
            }
            if !found {
                out.push(format!("error: snap \"{}\" not found in stable channel", target));
            }
        }
        "remove" => {
            if args.len() < 2 {
                out.push(String::from("error: please specify a snap name to remove"));
                return out;
            }
            let target = args[1];
            let mut catalog = SNAP_CATALOG.lock();
            let mut found = false;

            for app in catalog.iter_mut() {
                if app.name == target {
                    found = true;
                    if !app.is_installed {
                        out.push(format!("snap \"{}\" is not installed", target));
                    } else {
                        out.push(format!("Save data of snap \"{}\"... Done", target));
                        out.push(format!("Disconnect interfaces for snap \"{}\"... Done", target));
                        out.push(format!("Remove snap \"{}\"... Done", target));
                        out.push(format!("[+] {} removed", target));
                        app.is_installed = false;
                    }
                    break;
                }
            }
            if !found {
                out.push(format!("error: snap \"{}\" not found", target));
            }
        }
        "list" => {
            out.push(String::from("Name         Version    Rev    Tracking       Publisher     Notes"));
            let catalog = SNAP_CATALOG.lock();
            for app in catalog.iter() {
                if app.is_installed {
                    out.push(format!("{:<12} {:<10} 1042   latest/stable  {:<13} -", app.name, app.version, app.publisher));
                }
            }
        }
        "find" => {
            let query = if args.len() > 1 { args[1] } else { "" };
            out.push(String::from("Name         Version    Publisher            Summary"));
            let catalog = SNAP_CATALOG.lock();
            for app in catalog.iter() {
                if query.is_empty() || app.name.contains(query) || app.title.contains(query) {
                    out.push(format!("{:<12} {:<10} {:<20} {}", app.name, app.version, app.publisher, app.title));
                }
            }
        }
        cmd => {
            out.push(format!("snap: unknown command '{}'", cmd));
            out.push(String::from("See 'snap --help' for available commands."));
        }
    }

    out
}