1*45e17157SR0ronoa use std::{ 2*45e17157SR0ronoa any::Any, 3*45e17157SR0ronoa cell::{Cell, RefCell}, 4*45e17157SR0ronoa sync::Arc, 5*45e17157SR0ronoa }; 6*45e17157SR0ronoa 7*45e17157SR0ronoa use starry_client::base::renderer::Renderer; 8*45e17157SR0ronoa 9*45e17157SR0ronoa use crate::base::{point::Point, rect::Rect}; 10*45e17157SR0ronoa 11*45e17157SR0ronoa pub mod image; 12*45e17157SR0ronoa pub mod label; 13*45e17157SR0ronoa 14*45e17157SR0ronoa /// 组件的纵向排列方式 15*45e17157SR0ronoa #[derive(PartialEq, Copy, Clone)] 16*45e17157SR0ronoa pub enum VerticalPlacement { 17*45e17157SR0ronoa /// 向上对齐 18*45e17157SR0ronoa Top, 19*45e17157SR0ronoa /// 居中对齐 20*45e17157SR0ronoa Center, 21*45e17157SR0ronoa /// 向下对齐 22*45e17157SR0ronoa Bottom, 23*45e17157SR0ronoa /// 绝对位置 24*45e17157SR0ronoa Absolute, 25*45e17157SR0ronoa /// 拉伸 26*45e17157SR0ronoa Stretch, 27*45e17157SR0ronoa } 28*45e17157SR0ronoa 29*45e17157SR0ronoa /// 组件的横向排列方式 30*45e17157SR0ronoa #[derive(PartialEq, Copy, Clone)] 31*45e17157SR0ronoa pub enum HorizontalPlacement { 32*45e17157SR0ronoa /// 靠左对齐 33*45e17157SR0ronoa Left, 34*45e17157SR0ronoa /// 居中对齐 35*45e17157SR0ronoa Center, 36*45e17157SR0ronoa /// 靠右对齐 37*45e17157SR0ronoa Right, 38*45e17157SR0ronoa /// 绝对位置 39*45e17157SR0ronoa Absolute, 40*45e17157SR0ronoa /// 拉伸 41*45e17157SR0ronoa Stretch, 42*45e17157SR0ronoa } 43*45e17157SR0ronoa 44*45e17157SR0ronoa /// UI组件需要实现的特性 45*45e17157SR0ronoa pub trait Widget: Any { 46*45e17157SR0ronoa /// 返回渲染的矩形区域 47*45e17157SR0ronoa fn rect(&self) -> &Cell<Rect>; 48*45e17157SR0ronoa 49*45e17157SR0ronoa /// 返回组件相对于父物体的相对位置 50*45e17157SR0ronoa fn local_position(&self) -> &Cell<Point>; 51*45e17157SR0ronoa 52*45e17157SR0ronoa /// 返回纵向排列方式 53*45e17157SR0ronoa fn vertical_placement(&self) -> &Cell<VerticalPlacement>; 54*45e17157SR0ronoa 55*45e17157SR0ronoa /// 返回横向排列方式 56*45e17157SR0ronoa fn horizontal_placement(&self) -> &Cell<HorizontalPlacement>; 57*45e17157SR0ronoa 58*45e17157SR0ronoa /// 返回组件的名字 59*45e17157SR0ronoa fn name(&self) -> &str; 60*45e17157SR0ronoa 61*45e17157SR0ronoa /// 返回子组件数组 62*45e17157SR0ronoa fn children(&self) -> &RefCell<Vec<Arc<dyn Widget>>>; 63*45e17157SR0ronoa 64*45e17157SR0ronoa /// 添加子组件 65*45e17157SR0ronoa fn add_child(&self, widget: Arc<dyn Widget>) { 66*45e17157SR0ronoa (*self.children().borrow_mut()).push(widget); 67*45e17157SR0ronoa self.arrange(); 68*45e17157SR0ronoa } 69*45e17157SR0ronoa 70*45e17157SR0ronoa /// 渲染组件 71*45e17157SR0ronoa fn draw(&self, renderer: &mut dyn Renderer); 72*45e17157SR0ronoa 73*45e17157SR0ronoa /// 更新组件状态 74*45e17157SR0ronoa fn update(&self) {} 75*45e17157SR0ronoa 76*45e17157SR0ronoa /// TODO 77*45e17157SR0ronoa fn arrange(&self) {} 78*45e17157SR0ronoa } 79