xref: /StarryEngine/starry_toolkit/src/traits/focus.rs (revision 1bee64b64bc410ee78964a11a40a0fff69945480)
1 use std::{cell::RefCell, sync::Arc};
2 
3 use crate::widgets::Widget;
4 
5 pub trait Focus {
6     /// 返回当前聚焦的Widget
7     fn focused_widget(&self) -> RefCell<Option<Arc<dyn Widget>>>;
8 
9     /// 聚焦于给定Widget
10     fn focus(&self, widget: &Arc<dyn Widget>);
11 
12     /// 判断当前是否聚焦于给定Widget
13     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