1 use std::{cell::RefCell, sync::Arc}; 2 3 use crate::widgets::Widget; 4 5 pub trait Focus { 6 /// 返回当前聚焦的Widget focused_widget(&self) -> RefCell<Option<Arc<dyn Widget>>>7 fn focused_widget(&self) -> RefCell<Option<Arc<dyn Widget>>>; 8 9 /// 聚焦于给定Widget focus(&self, widget: &Arc<dyn Widget>)10 fn focus(&self, widget: &Arc<dyn Widget>); 11 12 /// 判断当前是否聚焦于给定Widget is_focused(&self, widget: &Arc<dyn Widget>) -> bool13 fn is_focused(&self, widget: &Arc<dyn Widget>) -> bool { 14 if let Some(ref focused_widget) = *self.focused_widget().borrow_mut() { 15 if Arc::ptr_eq(&widget, &focused_widget) { 16 return true; 17 } 18 } 19 20 false 21 } 22 } 23