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