use std::{ any::Any, cell::{Cell, RefCell}, str::FromStr, sync::{Arc, Weak}, }; use starry_client::base::{color::Color, renderer::Renderer}; use starry_toolkit::{ base::{panel::Panel, rect::Rect, vector2::Vector2}, traits::text::Text, widgets::{ image::Image, label::{Label, LabelOverflowType}, PivotType, Widget, }, }; pub struct AssetItemList { self_ref: RefCell>, pub rect: Cell, pivot: Cell, pivot_offset: Cell, parent: RefCell>>, children: RefCell>>, panel: RefCell>>, /// 缓存值 cache_focused: Cell, pub file_path: RefCell, } impl AssetItemList { pub const ITEM_WIDTH: u32 = 1024; pub const ITEM_HEIGHT: u32 = 32; pub fn new(file_name: &str, is_dir: bool) -> Arc { let item = Arc::new(AssetItemList { self_ref: RefCell::new(Weak::default()), rect: Cell::new(Rect::new(0, 0, Self::ITEM_WIDTH, Self::ITEM_HEIGHT)), pivot: Cell::new(PivotType::TopLeft), pivot_offset: Cell::new(Vector2::new(0, 0)), children: RefCell::new(Vec::new()), parent: RefCell::new(None), panel: RefCell::new(None), cache_focused: Cell::new(false), file_path: RefCell::new(String::from_str(file_name).unwrap()), }); (*item.self_ref.borrow_mut()) = Arc::downgrade(&item); // 背景Image let bg = Image::new_from_color( Self::ITEM_WIDTH, Self::ITEM_HEIGHT, Color::rgba(0, 255, 255, 64), ); bg.set_pivot_type(PivotType::Center); item.add_child(bg); // 文件名Label let name = Label::new(); name.set_adapt_type(LabelOverflowType::Omit); name.resize(256, 16); name.set_text(file_name); name.set_pivot_type(PivotType::Left); name.set_pivot_offset(Vector2::new(20, 0)); name.set_text_pivot_type(PivotType::Left); item.add_child(name); // 文件类型Label let file_type = Label::new(); file_type.set_adapt_type(LabelOverflowType::ShinkToFit); let type_name = if is_dir { "direction" } else { "file" }; file_type.set_text(type_name); file_type.set_pivot_type(PivotType::Center); item.add_child(file_type); return item; } } impl Widget for AssetItemList { fn self_ref(&self) -> Arc { self.self_ref.borrow().upgrade().unwrap() } fn as_any_ref(&self) -> &dyn Any { self } fn name(&self) -> &str { "AssetItem_List" } fn rect(&self) -> &Cell { &self.rect } fn pivot(&self) -> &Cell { &self.pivot } fn pivot_offset(&self) -> &Cell { &self.pivot_offset } fn parent(&self) -> &RefCell>> { &self.parent } fn children(&self) -> &RefCell>> { &self.children } fn panel(&self) -> &RefCell>> { &self.panel } fn draw(&self, renderer: &mut dyn Renderer, focused: bool) { if focused != self.cache_focused.get() { self.cache_focused.set(focused); } for child in self.children.borrow().iter() { child.draw(renderer, focused); } } }