1 use crate::starry_toolkit::traits::focus::Focus; 2 use starry_client::base::color::Color; 3 use starry_server::core::{SCREEN_HEIGHT, SCREEN_WIDTH}; 4 use starry_toolkit::{ 5 base::{panel::Panel, rect::Rect}, 6 layout::grid::{Grid, GridArrangeType}, 7 widgets::{image::Image, widget_add_child}, 8 }; 9 use std::{cell::RefCell, fs, sync::Arc}; 10 11 use self::asset_item::AssetItem; 12 use crate::starry_server::base::image::Image as ImageResource; 13 14 pub mod asset_item; 15 16 const DESKTOP_BG_PATH: &[u8] = include_bytes!("../resource/desktop_bg.png"); 17 18 pub struct AssetViewer { 19 cur_path: String, 20 asset_grid: RefCell<Arc<Grid>>, 21 } 22 23 impl AssetViewer { 24 pub fn new() -> Self { 25 AssetViewer { 26 cur_path: String::from("/"), 27 asset_grid: RefCell::new(Grid::new()), 28 } 29 } 30 31 pub fn init(&self) { 32 let grid = self.asset_grid.borrow(); 33 grid.set_upper_limit(5); 34 grid.set_space(20, 20); 35 grid.set_arrange_type(GridArrangeType::Horizontal); 36 } 37 38 pub fn refresh(&self) { 39 // 读取目录中的文件列表 40 if let Ok(entries) = fs::read_dir(&self.cur_path) { 41 for entry in entries { 42 if let Ok(item) = entry { 43 let item = AssetItem::new( 44 item.file_name().to_str().unwrap(), 45 item.metadata().unwrap().is_dir(), 46 ); 47 self.asset_grid.borrow_mut().add(&item); 48 widget_add_child(self.asset_grid.borrow().clone(), item); 49 } 50 } 51 } 52 53 // TODO 代码整理 54 let grid = self.asset_grid.borrow_mut(); 55 let elements = grid.elements.borrow(); 56 if let Some(widget) = elements.get(&(0, 0)) { 57 grid.focused_id.set(Some((0, 0))); 58 grid.focus(widget); 59 } 60 } 61 62 pub fn draw(&self) { 63 let panel = Panel::new( 64 Rect::new(0, 0, SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32), 65 "Title", 66 Color::rgb(0, 0, 0), 67 ); 68 69 panel.add_child(&Image::from_image( 70 ImageResource::from_path(DESKTOP_BG_PATH).unwrap(), 71 )); 72 73 panel.add_child(&(self.asset_grid.borrow())); 74 panel.draw(); 75 } 76 } 77