1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
use std::cell::RefCell; use std::sync::Arc; use widgets::Widget; pub struct FocusManager { focused_widget: RefCell<Option<Arc<dyn Widget>>> } impl FocusManager { pub fn new() -> Self { FocusManager { focused_widget: RefCell::new(None), } } pub fn request_focus(&self, widget: &Arc<dyn Widget>) { (*self.focused_widget.borrow_mut()) = Some(widget.clone()); } pub fn focused(&self, widget: &Arc<dyn Widget>) -> bool { if let Some(ref focused_widget) = *self.focused_widget.borrow_mut() { if Arc::ptr_eq(&widget, &focused_widget) { return true } } false } pub fn focused_widget(&self) -> &RefCell<Option<Arc<dyn Widget>>> { &self.focused_widget } }