AeroOS / kernel /src /apps /snapstore.rs
Jaswanth-2008's picture
Upload AeroOS complete Rust source code
dcbeeb2 verified
Raw History Blame Contribute Delete
12.7 kB
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
}